NbusBridge.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @file NbusBridge.h
  3. * @brief Declaration of nBus Bridge
  4. * @date Nov 27, 2025
  5. * @author Juraj Dudak, Matus Necas
  6. */
  7. #ifndef SRC_NBUSBRIDGE_H_
  8. #define SRC_NBUSBRIDGE_H_
  9. #include "NbusSlave.h"
  10. /**
  11. * Class representing nBus Bridge.
  12. */
  13. class NbusBridge
  14. {
  15. public:
  16. /** Constructor
  17. * @param nbus_communicator: hardware communicator.
  18. */
  19. NbusBridge(NbusCommunicator *nbus_communicator);
  20. /** Provide scan of internal nBus net and initialize local slave devices data.
  21. */
  22. void scanNetwork();
  23. /** Process incoming requests.
  24. * @param rx_frame: RX data packet
  25. * @param size: size of rx_frame
  26. */
  27. void processRequest(uint8_t *rxFrame, uint8_t size);
  28. /**
  29. * Implementation of the STATE_RUNNING state.
  30. * In this state, automatic reading of data from all slaves/all sensors is running.
  31. * The data is sent to the Master in a batch (for all slaves).
  32. */
  33. void processRunningState();
  34. private:
  35. /** Get slave pointer.
  36. * @param index: slave id
  37. * @return pointer to slave
  38. * @note: can be nullptr
  39. */
  40. NbusSlave* _getSlave(uint8_t index);
  41. /** Calculate 8-bit CRC.
  42. * @param mem: data buffer
  43. * @param len: buffer length
  44. * @return> crc
  45. */
  46. uint8_t _crc8x_fast(void const *mem, uint16_t len);
  47. /** Process broadcast request.
  48. * @param rx_frame: RX data packet
  49. * @param size: size of rx_frame
  50. */
  51. void _processBroadcast(uint8_t *rx_frame, uint8_t size);
  52. /** Process bridge request (e.g. bridge-cast).
  53. * @param rx_frame: RX data packet
  54. * @param size: size of rx_frame
  55. */
  56. void _processBridgeRequest(uint8_t *rxFrame, uint8_t size);
  57. /** Process slave request (e.g. uni-cast).
  58. * @param rx_frame: RX data packet
  59. * @param size: size of rx_frame
  60. */
  61. void _processSlaveRequest(uint8_t *rxFrame, uint8_t size);
  62. /** Implementation of CMD_GET_DATA for bridge.
  63. * @return: response to master
  64. */
  65. DataFrame * _cmdGetData();
  66. /** Implementation of CMD_GET_INFO for bridge.
  67. * @return: response to master
  68. */
  69. DataFrame * _cmdGetInfo();
  70. /** Implementation of CMD_GET_FORMAT for bridge.
  71. * @return: response to master
  72. */
  73. DataFrame * _cmdGetFormat();
  74. /** Implementation of CMD_GET_SLAVES for bridge.
  75. * @return: response to master
  76. */
  77. DataFrame * _cmdGetSlaves();
  78. /** Implementation of CMD_SET_STOPfor bridge.
  79. */
  80. void _cmdSetStop();
  81. /** Implementation of CMD_SET_START for bridge.
  82. */
  83. void _cmdSetStart();
  84. /** Set error response to master.
  85. * @param error_code: error code
  86. * @return: response to master
  87. */
  88. DataFrame * _rspSetError(Nbus_EC_e error_code);
  89. /** Set status response to master.
  90. * @param error_code: error code
  91. * @return: response to master
  92. */
  93. DataFrame * _rspSetStatus(Nbus_SC_e status_code);
  94. /** Make default packet from pdu and data.
  95. * @param pdu: current pdu
  96. * @param data: application data
  97. * @param size: application data size
  98. * @return: response to master
  99. */
  100. DataFrame * _makePacket(Nbus_PDU_t pdu, uint8_t *data, uint8_t size);
  101. DataFrame * _makeBridgeDataPacket();
  102. /** Make raw packet (just copy whole input frame).
  103. * @param daa: application data
  104. * @param size: application data size
  105. * @return: response to master
  106. */
  107. DataFrame * _forwardPacket(uint8_t *data, uint8_t size);
  108. void _addDataPayload(DataFrame * data_frame);
  109. NbusCommunicator *_communicator{nullptr}; ///< nBus HW communicator
  110. NbusSlave _slaves[NBUS_BRIDGE_MAX_SLAVES]; ///< array of nBus slaves
  111. uint8_t _num_slaves{0}; ///< number of slaves
  112. Nbus_RunState_e _run_state{STATE_STOPPED}; ///< running state flag
  113. DataFrame *_worker_frame_ptr{nullptr}; ///< pointer to actual data frame
  114. uint8_t _data_packet[NBUS_BRIDGE_DATAPACKET_SIZE]{0}; ///< data packet buffer
  115. DataFrame _bridge_cast_frame{_data_packet, NBUS_BRIDGE_DATAPACKET_SIZE, TYPE_RAW, CRC_OFF}; ///< data frame for bridge cast
  116. DataFrame _default_frame{_data_packet, NBUS_BRIDGE_DATAPACKET_SIZE, TYPE_PLAIN, CRC_ON}; ///< data frame for default operation
  117. DataFrame _raw_data_frame{_data_packet, NBUS_BRIDGE_DATAPACKET_SIZE, TYPE_RAW, CRC_OFF}; ///< raw data frame for copying
  118. uint8_t _scan_request{0}; ///< flag if scan request is necessary
  119. Nbus_PDU_t _pdu{NBUS_BROADCAST_ADDRESS, NBUS_BROADCAST_ADDRESS, FC_ECHO}; ///< nBus actual PDU
  120. uint8_t _bridge_cast_header[NBUS_BRIDGE_HEADER_SIZE]{NBUS_BRIDGE_HEADER_SEQ}; ///< data header for bridge-cast
  121. };
  122. #endif /* SRC_NBUSBRIDGE_H_ */