/** * @file NbusBridge.h * @brief Declaration of nBus Bridge * @date Nov 27, 2025 * @author Juraj Dudak, Matus Necas */ #ifndef SRC_NBUSBRIDGE_H_ #define SRC_NBUSBRIDGE_H_ #include "NbusSlave.h" /** * Class representing nBus Bridge. */ class NbusBridge { public: /** Constructor * @param nbus_communicator: hardware communicator. */ NbusBridge(NbusCommunicator *nbus_communicator); /** Provide scan of internal nBus net and initialize local slave devices data. */ void scanNetwork(); /** Process incoming requests. * @param rx_frame: RX data packet * @param size: size of rx_frame */ void processRequest(uint8_t *rxFrame, uint8_t size); /** * Implementation of the STATE_RUNNING state. * In this state, automatic reading of data from all slaves/all sensors is running. * The data is sent to the Master in a batch (for all slaves). */ void processRunningState(); private: /** Get slave pointer. * @param index: slave id * @return pointer to slave * @note: can be nullptr */ NbusSlave* _getSlave(uint8_t index); /** Calculate 8-bit CRC. * @param mem: data buffer * @param len: buffer length * @return> crc */ uint8_t _crc8x_fast(void const *mem, uint16_t len); /** Process broadcast request. * @param rx_frame: RX data packet * @param size: size of rx_frame */ void _processBroadcast(uint8_t *rx_frame, uint8_t size); /** Process bridge request (e.g. bridge-cast). * @param rx_frame: RX data packet * @param size: size of rx_frame */ void _processBridgeRequest(uint8_t *rxFrame, uint8_t size); /** Process slave request (e.g. uni-cast). * @param rx_frame: RX data packet * @param size: size of rx_frame */ void _processSlaveRequest(uint8_t *rxFrame, uint8_t size); /** Implementation of CMD_GET_DATA for bridge. * @return: response to master */ DataFrame * _cmdGetData(); /** Implementation of CMD_GET_INFO for bridge. * @return: response to master */ DataFrame * _cmdGetInfo(); /** Implementation of CMD_GET_FORMAT for bridge. * @return: response to master */ DataFrame * _cmdGetFormat(); /** Implementation of CMD_GET_SLAVES for bridge. * @return: response to master */ DataFrame * _cmdGetSlaves(); /** Implementation of CMD_SET_STOPfor bridge. */ void _cmdSetStop(); /** Implementation of CMD_SET_START for bridge. */ void _cmdSetStart(); /** Set error response to master. * @param error_code: error code * @return: response to master */ DataFrame * _rspSetError(Nbus_EC_e error_code); /** Set status response to master. * @param error_code: error code * @return: response to master */ DataFrame * _rspSetStatus(Nbus_SC_e status_code); /** Make default packet from pdu and data. * @param pdu: current pdu * @param data: application data * @param size: application data size * @return: response to master */ DataFrame * _makePacket(Nbus_PDU_t pdu, uint8_t *data, uint8_t size); DataFrame * _makeBridgeDataPacket(); /** Make raw packet (just copy whole input frame). * @param daa: application data * @param size: application data size * @return: response to master */ DataFrame * _forwardPacket(uint8_t *data, uint8_t size); void _addDataPayload(DataFrame * data_frame); NbusCommunicator *_communicator{nullptr}; ///< nBus HW communicator NbusSlave _slaves[NBUS_BRIDGE_MAX_SLAVES]; ///< array of nBus slaves uint8_t _slave_adress[NBUS_BRIDGE_MAX_SLAVES]{0}; ///< array of slave addresses uint8_t _num_slaves{0}; ///< number of slaves Nbus_RunState_e _run_state{STATE_STOPPED}; ///< running state flag DataFrame *_worker_frame_ptr{nullptr}; ///< pointer to actual data frame uint8_t _data_packet[NBUS_BRIDGE_DATAPACKET_SIZE]{0}; ///< data packet buffer DataFrame _bridge_cast_frame{_data_packet, NBUS_BRIDGE_DATAPACKET_SIZE, TYPE_RAW, CRC_OFF}; ///< data frame for bridge cast DataFrame _default_frame{_data_packet, NBUS_BRIDGE_DATAPACKET_SIZE, TYPE_PLAIN, CRC_ON}; ///< data frame for default operation DataFrame _raw_data_frame{_data_packet, NBUS_BRIDGE_DATAPACKET_SIZE, TYPE_RAW, CRC_OFF}; ///< raw data frame for copying uint8_t _scan_request{0}; ///< flag if scan request is necessary Nbus_PDU_t _pdu{NBUS_BROADCAST_ADDRESS, NBUS_BROADCAST_ADDRESS, FC_ECHO}; ///< nBus actual PDU uint8_t _bridge_cast_header[NBUS_BRIDGE_HEADER_SIZE]{NBUS_BRIDGE_HEADER_SEQ}; ///< data header for bridge-cast }; #endif /* SRC_NBUSBRIDGE_H_ */