esp_modbus_slave.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "esp_err.h" // for esp_err_t
  16. #include "sdkconfig.h" // for KConfig defines
  17. #include "mbc_slave.h" // for slave private type definitions
  18. #include "mbutils.h" // for stack bit setting utilities
  19. #include "esp_modbus_common.h" // for common defines
  20. #include "esp_modbus_slave.h" // for public slave defines
  21. #include "esp_modbus_callbacks.h" // for modbus callbacks function pointers declaration
  22. #ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
  23. #define MB_ID_BYTE0(id) ((uint8_t)(id))
  24. #define MB_ID_BYTE1(id) ((uint8_t)(((uint16_t)(id) >> 8) & 0xFF))
  25. #define MB_ID_BYTE2(id) ((uint8_t)(((uint32_t)(id) >> 16) & 0xFF))
  26. #define MB_ID_BYTE3(id) ((uint8_t)(((uint32_t)(id) >> 24) & 0xFF))
  27. #define MB_CONTROLLER_SLAVE_ID (CONFIG_FMB_CONTROLLER_SLAVE_ID)
  28. #define MB_SLAVE_ID_SHORT (MB_ID_BYTE3(MB_CONTROLLER_SLAVE_ID))
  29. // Slave ID constant
  30. static uint8_t mb_slave_id[] = { MB_ID_BYTE0(MB_CONTROLLER_SLAVE_ID),
  31. MB_ID_BYTE1(MB_CONTROLLER_SLAVE_ID),
  32. MB_ID_BYTE2(MB_CONTROLLER_SLAVE_ID) };
  33. #endif
  34. #define REG_SIZE(type, nregs) ((type == MB_PARAM_INPUT) || (type == MB_PARAM_HOLDING)) ? (nregs >> 1) : (nregs << 3)
  35. // Common interface pointer for slave port
  36. static mb_slave_interface_t* slave_interface_ptr = NULL;
  37. // Searches the register in the area specified by type, returns descriptor if found, else NULL
  38. static mb_descr_entry_t* mbc_slave_find_reg_descriptor(mb_param_type_t type, uint16_t addr, size_t regs)
  39. {
  40. mb_descr_entry_t* it;
  41. uint16_t reg_size = 0;
  42. mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
  43. if (LIST_EMPTY(&mbs_opts->mbs_area_descriptors[type])) {
  44. return NULL;
  45. }
  46. // search for the register in each area
  47. for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[type]); it != NULL; it = LIST_NEXT(it, entries)) {
  48. reg_size = REG_SIZE(type, it->size);
  49. if ((addr >= it->start_offset)
  50. && (it->p_data)
  51. && (regs >= 1)
  52. && ((addr + regs) <= (it->start_offset + reg_size))
  53. && (reg_size >= 1)) {
  54. return it;
  55. }
  56. }
  57. return NULL;
  58. }
  59. static void mbc_slave_free_descriptors(void) {
  60. mb_descr_entry_t* it;
  61. mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
  62. for (int descr_type = 0; descr_type < MB_PARAM_COUNT; descr_type++) {
  63. for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[descr_type]); it != NULL; it = LIST_NEXT(it, entries)) {
  64. LIST_REMOVE(it, entries);
  65. free(it);
  66. }
  67. }
  68. }
  69. void mbc_slave_init_iface(void* handler)
  70. {
  71. slave_interface_ptr = (mb_slave_interface_t*) handler;
  72. mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
  73. // Initialize list head for register areas
  74. LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT]);
  75. LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING]);
  76. LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_COIL]);
  77. LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE]);
  78. }
  79. /**
  80. * Modbus controller destroy function
  81. */
  82. esp_err_t mbc_slave_destroy(void)
  83. {
  84. esp_err_t error = ESP_OK;
  85. // Is initialization done?
  86. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  87. ESP_ERR_INVALID_STATE,
  88. "Slave interface is not correctly initialized.");
  89. // Check if interface has been initialized
  90. MB_SLAVE_CHECK((slave_interface_ptr->destroy != NULL),
  91. ESP_ERR_INVALID_STATE,
  92. "Slave interface is not correctly initialized.");
  93. // Call the slave port destroy function
  94. error = slave_interface_ptr->destroy();
  95. MB_SLAVE_CHECK((error == ESP_OK),
  96. ESP_ERR_INVALID_STATE,
  97. "Slave destroy failure error=(0x%x).",
  98. error);
  99. // Destroy all opened descriptors
  100. mbc_slave_free_descriptors();
  101. free(slave_interface_ptr);
  102. slave_interface_ptr = NULL;
  103. return error;
  104. }
  105. /**
  106. * Setup Modbus controller parameters
  107. */
  108. esp_err_t mbc_slave_setup(void* comm_info)
  109. {
  110. esp_err_t error = ESP_OK;
  111. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  112. ESP_ERR_INVALID_STATE,
  113. "Slave interface is not correctly initialized.");
  114. MB_SLAVE_CHECK((slave_interface_ptr->setup != NULL),
  115. ESP_ERR_INVALID_STATE,
  116. "Slave interface is not correctly initialized.");
  117. error = slave_interface_ptr->setup(comm_info);
  118. MB_SLAVE_CHECK((error == ESP_OK),
  119. ESP_ERR_INVALID_STATE,
  120. "Slave setup failure error=(0x%x).",
  121. error);
  122. return error;
  123. }
  124. /**
  125. * Start Modbus controller start function
  126. */
  127. esp_err_t mbc_slave_start(void)
  128. {
  129. esp_err_t error = ESP_OK;
  130. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  131. ESP_ERR_INVALID_STATE,
  132. "Slave interface is not correctly initialized.");
  133. MB_SLAVE_CHECK((slave_interface_ptr->start != NULL),
  134. ESP_ERR_INVALID_STATE,
  135. "Slave interface is not correctly initialized.");
  136. #ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
  137. // Set the slave ID if the KConfig option is selected
  138. eMBErrorCode status = eMBSetSlaveID(MB_SLAVE_ID_SHORT, TRUE, (UCHAR*)mb_slave_id, sizeof(mb_slave_id));
  139. MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack set slave ID failure.");
  140. #endif
  141. error = slave_interface_ptr->start();
  142. MB_SLAVE_CHECK((error == ESP_OK),
  143. ESP_ERR_INVALID_STATE,
  144. "Slave start failure error=(0x%x).",
  145. error);
  146. return error;
  147. }
  148. /**
  149. * Blocking function to get event on parameter group change for application task
  150. */
  151. mb_event_group_t mbc_slave_check_event(mb_event_group_t group)
  152. {
  153. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  154. MB_EVENT_NO_EVENTS,
  155. "Slave interface is not correctly initialized.");
  156. MB_SLAVE_CHECK((slave_interface_ptr->check_event != NULL),
  157. MB_EVENT_NO_EVENTS,
  158. "Slave interface is not correctly initialized.");
  159. mb_event_group_t event = slave_interface_ptr->check_event(group);
  160. return event;
  161. }
  162. /**
  163. * Function to get notification about parameter change from application task
  164. */
  165. esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
  166. {
  167. esp_err_t error = ESP_OK;
  168. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  169. ESP_ERR_INVALID_STATE,
  170. "Slave interface is not correctly initialized.");
  171. MB_SLAVE_CHECK((slave_interface_ptr->get_param_info != NULL),
  172. ESP_ERR_INVALID_STATE,
  173. "Slave interface is not correctly initialized.");
  174. error = slave_interface_ptr->get_param_info(reg_info, timeout);
  175. MB_SLAVE_CHECK((error == ESP_OK),
  176. ESP_ERR_INVALID_STATE,
  177. "Slave get parameter info failure error=(0x%x).",
  178. error);
  179. return error;
  180. }
  181. /**
  182. * Function to set area descriptors for modbus parameters
  183. */
  184. esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data)
  185. {
  186. esp_err_t error = ESP_OK;
  187. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  188. ESP_ERR_INVALID_STATE,
  189. "Slave interface is not correctly initialized.");
  190. if (slave_interface_ptr->set_descriptor != NULL) {
  191. error = slave_interface_ptr->set_descriptor(descr_data);
  192. MB_SLAVE_CHECK((error == ESP_OK),
  193. ESP_ERR_INVALID_STATE,
  194. "Slave set descriptor failure error=(0x%x).",
  195. (uint16_t)error);
  196. } else {
  197. mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
  198. // Check if the address is already in the descriptor list
  199. mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(descr_data.type, descr_data.start_offset, 1);
  200. MB_SLAVE_CHECK((it == NULL), ESP_ERR_INVALID_ARG, "mb incorrect descriptor or already defined.");
  201. mb_descr_entry_t* new_descr = (mb_descr_entry_t*) heap_caps_malloc(sizeof(mb_descr_entry_t),
  202. MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  203. MB_SLAVE_CHECK((new_descr != NULL), ESP_ERR_NO_MEM, "mb can not allocate memory for descriptor.");
  204. new_descr->start_offset = descr_data.start_offset;
  205. new_descr->type = descr_data.type;
  206. new_descr->p_data = descr_data.address;
  207. new_descr->size = descr_data.size;
  208. LIST_INSERT_HEAD(&mbs_opts->mbs_area_descriptors[descr_data.type], new_descr, entries);
  209. error = ESP_OK;
  210. }
  211. return error;
  212. }
  213. // The helper function to get time stamp in microseconds
  214. static uint64_t mbc_slave_get_time_stamp(void)
  215. {
  216. uint64_t time_stamp = esp_timer_get_time();
  217. return time_stamp;
  218. }
  219. // Helper function to send parameter information to application task
  220. static esp_err_t mbc_slave_send_param_info(mb_event_group_t par_type, uint16_t mb_offset,
  221. uint8_t* par_address, uint16_t par_size)
  222. {
  223. MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
  224. mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
  225. esp_err_t error = ESP_FAIL;
  226. mb_param_info_t par_info;
  227. // Check if queue is not full the send parameter information
  228. par_info.type = par_type;
  229. par_info.size = par_size;
  230. par_info.address = par_address;
  231. par_info.time_stamp = mbc_slave_get_time_stamp();
  232. par_info.mb_offset = mb_offset;
  233. BaseType_t status = xQueueSend(mbs_opts->mbs_notification_queue_handle, &par_info, MB_PAR_INFO_TOUT);
  234. if (pdTRUE == status) {
  235. ESP_LOGD(MB_SLAVE_TAG, "Queue send parameter info (type, address, size): %d, 0x%.4x, %d",
  236. par_type, (uint32_t)par_address, par_size);
  237. error = ESP_OK;
  238. } else if (errQUEUE_FULL == status) {
  239. ESP_LOGD(MB_SLAVE_TAG, "Parameter queue is overflowed.");
  240. }
  241. return error;
  242. }
  243. // Helper function to send notification
  244. static esp_err_t mbc_slave_send_param_access_notification(mb_event_group_t event)
  245. {
  246. MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
  247. mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
  248. esp_err_t err = ESP_FAIL;
  249. mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mbs_opts->mbs_event_group, (EventBits_t)event);
  250. if (bits & event) {
  251. ESP_LOGD(MB_SLAVE_TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (uint8_t)event);
  252. err = ESP_OK;
  253. }
  254. return err;
  255. }
  256. /*
  257. * Below are the common slave read/write register callback functions
  258. * The concrete slave port can override them using interface function pointers
  259. */
  260. // Callback function for reading of MB Input Registers
  261. eMBErrorCode mbc_reg_input_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs)
  262. {
  263. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  264. MB_EILLSTATE, "Slave stack uninitialized.");
  265. MB_SLAVE_CHECK((reg_buffer != NULL),
  266. MB_EINVAL, "Slave stack call failed.");
  267. eMBErrorCode status = MB_ENOERR;
  268. address--; // address of register is already +1
  269. mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_INPUT, address, n_regs);
  270. if (it != NULL) {
  271. uint16_t input_reg_start = (uint16_t)it->start_offset; // Get Modbus start address
  272. uint8_t* input_buffer = (uint8_t*)it->p_data; // Get instance address
  273. uint16_t regs = n_regs;
  274. uint16_t reg_index;
  275. // If input or configuration parameters are incorrect then return an error to stack layer
  276. reg_index = (uint16_t)(address - input_reg_start);
  277. reg_index <<= 1; // register Address to byte address
  278. input_buffer += reg_index;
  279. uint8_t* buffer_start = input_buffer;
  280. while (regs > 0) {
  281. _XFER_2_RD(reg_buffer, input_buffer);
  282. reg_index += 2;
  283. regs -= 1;
  284. }
  285. // Send access notification
  286. (void)mbc_slave_send_param_access_notification(MB_EVENT_INPUT_REG_RD);
  287. // Send parameter info to application task
  288. (void)mbc_slave_send_param_info(MB_EVENT_INPUT_REG_RD, (uint16_t)address,
  289. (uint8_t*)buffer_start, (uint16_t)n_regs);
  290. } else {
  291. status = MB_ENOREG;
  292. }
  293. return status;
  294. }
  295. // Callback function for reading of MB Holding Registers
  296. // Executed by stack when request to read/write holding registers is received
  297. eMBErrorCode mbc_reg_holding_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs, eMBRegisterMode mode)
  298. {
  299. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  300. MB_EILLSTATE, "Slave stack uninitialized.");
  301. MB_SLAVE_CHECK((reg_buffer != NULL),
  302. MB_EINVAL, "Slave stack call failed.");
  303. eMBErrorCode status = MB_ENOERR;
  304. uint16_t reg_index;
  305. address--; // address of register is already +1
  306. mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_HOLDING, address, n_regs);
  307. if (it != NULL) {
  308. uint16_t reg_holding_start = (uint16_t)it->start_offset; // Get Modbus start address
  309. uint8_t* holding_buffer = (uint8_t*)it->p_data; // Get instance address
  310. uint16_t regs = n_regs;
  311. reg_index = (uint16_t) (address - reg_holding_start);
  312. reg_index <<= 1; // register Address to byte address
  313. holding_buffer += reg_index;
  314. uint8_t* buffer_start = holding_buffer;
  315. switch (mode) {
  316. case MB_REG_READ:
  317. while (regs > 0) {
  318. _XFER_2_RD(reg_buffer, holding_buffer);
  319. reg_index += 2;
  320. regs -= 1;
  321. };
  322. // Send access notification
  323. (void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_RD);
  324. // Send parameter info
  325. (void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_RD, (uint16_t)address,
  326. (uint8_t*)buffer_start, (uint16_t)n_regs);
  327. break;
  328. case MB_REG_WRITE:
  329. while (regs > 0) {
  330. _XFER_2_WR(holding_buffer, reg_buffer);
  331. holding_buffer += 2;
  332. reg_index += 2;
  333. regs -= 1;
  334. };
  335. // Send access notification
  336. (void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_WR);
  337. // Send parameter info
  338. (void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_WR, (uint16_t)address,
  339. (uint8_t*)buffer_start, (uint16_t)n_regs);
  340. break;
  341. }
  342. } else {
  343. status = MB_ENOREG;
  344. }
  345. return status;
  346. }
  347. // Callback function for reading of MB Coils Registers
  348. eMBErrorCode mbc_reg_coils_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_coils, eMBRegisterMode mode)
  349. {
  350. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  351. MB_EILLSTATE, "Slave stack uninitialized.");
  352. MB_SLAVE_CHECK((reg_buffer != NULL),
  353. MB_EINVAL, "Slave stack call failed.");
  354. eMBErrorCode status = MB_ENOERR;
  355. uint16_t reg_index;
  356. uint16_t coils = n_coils;
  357. address--; // The address is already +1
  358. mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_COIL, address, n_coils);
  359. if (it != NULL) {
  360. uint16_t reg_coils_start = (uint16_t)it->start_offset; // MB offset of coils
  361. uint8_t* reg_coils_buf = (uint8_t*)it->p_data;
  362. reg_index = (uint16_t) (address - it->start_offset);
  363. CHAR* coils_data_buf = (CHAR*)(reg_coils_buf + (reg_index >> 3));
  364. switch (mode) {
  365. case MB_REG_READ:
  366. while (coils > 0) {
  367. uint8_t result = xMBUtilGetBits((uint8_t*)reg_coils_buf, reg_index, 1);
  368. xMBUtilSetBits(reg_buffer, reg_index - (address - reg_coils_start), 1, result);
  369. reg_index++;
  370. coils--;
  371. }
  372. // Send an event to notify application task about event
  373. (void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_RD);
  374. (void)mbc_slave_send_param_info(MB_EVENT_COILS_RD, (uint16_t)address,
  375. (uint8_t*)(coils_data_buf), (uint16_t)n_coils);
  376. break;
  377. case MB_REG_WRITE:
  378. while (coils > 0) {
  379. uint8_t result = xMBUtilGetBits(reg_buffer,
  380. reg_index - (address - reg_coils_start), 1);
  381. xMBUtilSetBits((uint8_t*)reg_coils_buf, reg_index, 1, result);
  382. reg_index++;
  383. coils--;
  384. }
  385. // Send an event to notify application task about event
  386. (void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_WR);
  387. (void)mbc_slave_send_param_info(MB_EVENT_COILS_WR, (uint16_t)address,
  388. (uint8_t*)coils_data_buf, (uint16_t)n_coils);
  389. break;
  390. } // switch ( eMode )
  391. } else {
  392. // If the configuration or input parameters are incorrect then return error to stack
  393. status = MB_ENOREG;
  394. }
  395. return status;
  396. }
  397. // Callback function for reading of MB Discrete Input Registers
  398. eMBErrorCode mbc_reg_discrete_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_discrete)
  399. {
  400. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  401. MB_EILLSTATE, "Slave stack uninitialized.");
  402. MB_SLAVE_CHECK((reg_buffer != NULL),
  403. MB_EINVAL, "Slave stack call failed.");
  404. eMBErrorCode status = MB_ENOERR;
  405. uint16_t reg_index;
  406. uint16_t reg_bit_index;
  407. uint16_t n_reg;
  408. uint8_t* discrete_input_buf;
  409. // It already plus one in modbus function method.
  410. address--;
  411. mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_DISCRETE, address, n_discrete);
  412. if (it != NULL) {
  413. uint16_t reg_discrete_start = (uint16_t)it->start_offset; // MB offset of registers
  414. n_reg = (n_discrete >> 3) + 1;
  415. discrete_input_buf = (uint8_t*)it->p_data; // the storage address
  416. reg_index = (uint16_t) (address - reg_discrete_start) / 8; // Get register index in the buffer for bit number
  417. reg_bit_index = (uint16_t)(address - reg_discrete_start) % 8; // Get bit index
  418. uint8_t* temp_buf = &discrete_input_buf[reg_index];
  419. while (n_reg > 0) {
  420. *reg_buffer++ = xMBUtilGetBits(&discrete_input_buf[reg_index++], reg_bit_index, 8);
  421. n_reg--;
  422. }
  423. reg_buffer--;
  424. // Last discrete
  425. n_discrete = n_discrete % 8;
  426. // Filling zero to high bit
  427. *reg_buffer = *reg_buffer << (8 - n_discrete);
  428. *reg_buffer = *reg_buffer >> (8 - n_discrete);
  429. // Send an event to notify application task about event
  430. (void)mbc_slave_send_param_access_notification(MB_EVENT_DISCRETE_RD);
  431. (void)mbc_slave_send_param_info(MB_EVENT_DISCRETE_RD, (uint16_t)address,
  432. (uint8_t*)temp_buf, (uint16_t)n_discrete);
  433. } else {
  434. status = MB_ENOREG;
  435. }
  436. return status;
  437. }
  438. /**
  439. * Below are the stack callback functions to read/write registers
  440. */
  441. eMBErrorCode eMBRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
  442. {
  443. eMBErrorCode error = MB_ENOERR;
  444. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  445. ESP_ERR_INVALID_STATE,
  446. "Slave interface is not correctly initialized.");
  447. // Check if the callback is overridden in concrete port
  448. if (slave_interface_ptr->slave_reg_cb_discrete) {
  449. error = slave_interface_ptr->slave_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
  450. } else {
  451. error = mbc_reg_discrete_slave_cb(pucRegBuffer, usAddress, usNDiscrete);
  452. }
  453. return error;
  454. }
  455. eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
  456. USHORT usNCoils, eMBRegisterMode eMode)
  457. {
  458. eMBErrorCode error = MB_ENOERR;
  459. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  460. ESP_ERR_INVALID_STATE,
  461. "Slave interface is not correctly initialized.");
  462. if (slave_interface_ptr->slave_reg_cb_coils) {
  463. error = slave_interface_ptr->slave_reg_cb_coils(pucRegBuffer, usAddress, usNCoils, eMode);
  464. } else {
  465. error = mbc_reg_coils_slave_cb(pucRegBuffer, usAddress, usNCoils, eMode);
  466. }
  467. return error;
  468. }
  469. eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
  470. USHORT usNRegs, eMBRegisterMode eMode)
  471. {
  472. eMBErrorCode error = MB_ENOERR;
  473. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  474. ESP_ERR_INVALID_STATE,
  475. "Slave interface is not correctly initialized.");
  476. if (slave_interface_ptr->slave_reg_cb_holding) {
  477. error = slave_interface_ptr->slave_reg_cb_holding(pucRegBuffer, usAddress, usNRegs, eMode);
  478. } else {
  479. error = mbc_reg_holding_slave_cb(pucRegBuffer, usAddress, usNRegs, eMode);
  480. }
  481. return error;
  482. }
  483. eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs)
  484. {
  485. eMBErrorCode error = ESP_ERR_INVALID_STATE;
  486. MB_SLAVE_CHECK((slave_interface_ptr != NULL),
  487. ESP_ERR_INVALID_STATE,
  488. "Slave interface is not correctly initialized.");
  489. if (slave_interface_ptr->slave_reg_cb_input) {
  490. error = slave_interface_ptr->slave_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
  491. } else {
  492. error = mbc_reg_input_slave_cb(pucRegBuffer, usAddress, usNRegs);
  493. }
  494. return error;
  495. }