Lines Matching +full:4 +full:- +full:byte

4  * SPDX-License-Identifier: Apache-2.0
13 * Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
15 * SPDX-License-Identifier: APACHE-2.0
19 * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
37 ctx->mbs_msg_ctr = 0; in modbus_reset_stats()
38 ctx->mbs_crc_err_ctr = 0; in modbus_reset_stats()
39 ctx->mbs_except_ctr = 0; in modbus_reset_stats()
40 ctx->mbs_server_msg_ctr = 0; in modbus_reset_stats()
41 ctx->mbs_noresp_ctr = 0; in modbus_reset_stats()
46 ctx->mbs_msg_ctr++; in update_msg_ctr()
51 ctx->mbs_crc_err_ctr++; in update_crcerr_ctr()
56 ctx->mbs_except_ctr++; in update_excep_ctr()
61 ctx->mbs_server_msg_ctr++; in update_server_msg_ctr()
66 ctx->mbs_noresp_ctr++; in update_noresp_ctr()
85 LOG_INF("FC 0x%02x Error 0x%02x", ctx->rx_adu.fc, excep_code); in mbs_exception_rsp()
89 ctx->tx_adu.fc |= excep_bit; in mbs_exception_rsp()
90 ctx->tx_adu.data[0] = excep_code; in mbs_exception_rsp()
91 ctx->tx_adu.length = 1; in mbs_exception_rsp()
98 * Function code 1 Byte
103 * Function code 1 Byte
104 * Byte count 1 Bytes
105 * Coil status N * 1 Byte
110 const uint8_t request_len = 4; in mbs_fc01_coil_read()
120 if (ctx->rx_adu.length != request_len) { in mbs_fc01_coil_read()
125 if (ctx->mbs_user_cb->coil_rd == NULL) { in mbs_fc01_coil_read()
130 coil_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc01_coil_read()
131 coil_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc01_coil_read()
140 /* Calculate byte count for response. */ in mbs_fc01_coil_read()
141 num_bytes = ((coil_qty - 1) / 8) + 1; in mbs_fc01_coil_read()
142 /* Number of data bytes + byte count. */ in mbs_fc01_coil_read()
143 ctx->tx_adu.length = num_bytes + 1; in mbs_fc01_coil_read()
145 ctx->tx_adu.data[0] = (uint8_t)num_bytes; in mbs_fc01_coil_read()
148 presp = &ctx->tx_adu.data[1]; in mbs_fc01_coil_read()
152 presp = &ctx->tx_adu.data[1]; in mbs_fc01_coil_read()
153 /* Start with bit 0 in response byte data mask. */ in mbs_fc01_coil_read()
161 err = ctx->mbs_user_cb->coil_rd(coil_addr, &coil_state); in mbs_fc01_coil_read()
183 * Still in same data byte, so shift the data mask in mbs_fc01_coil_read()
197 * Function code 1 Byte
202 * Function code 1 Byte
203 * Byte count 1 Bytes
204 * Input status N * 1 Byte
209 const uint8_t request_len = 4; in mbs_fc02_di_read()
219 if (ctx->rx_adu.length != request_len) { in mbs_fc02_di_read()
224 if (ctx->mbs_user_cb->discrete_input_rd == NULL) { in mbs_fc02_di_read()
229 di_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc02_di_read()
230 di_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc02_di_read()
240 num_bytes = ((di_qty - 1) / 8) + 1; in mbs_fc02_di_read()
241 /* Number of data bytes + byte count. */ in mbs_fc02_di_read()
242 ctx->tx_adu.length = num_bytes + 1; in mbs_fc02_di_read()
244 ctx->tx_adu.data[0] = (uint8_t)num_bytes; in mbs_fc02_di_read()
247 presp = &ctx->tx_adu.data[1]; in mbs_fc02_di_read()
253 presp = &ctx->tx_adu.data[1]; in mbs_fc02_di_read()
254 /* Start with bit 0 in response byte data mask. */ in mbs_fc02_di_read()
262 err = ctx->mbs_user_cb->discrete_input_rd(di_addr, &di_state); in mbs_fc02_di_read()
284 * Still in same data byte, so shift the data mask in mbs_fc02_di_read()
298 * Function code 1 Byte
303 * Function code 1 Byte
304 * Byte count 1 Bytes
305 * Register Value N * 2 Byte
310 const uint8_t request_len = 4; in mbs_fc03_hreg_read()
317 if (ctx->rx_adu.length != request_len) { in mbs_fc03_hreg_read()
322 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc03_hreg_read()
323 reg_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc03_hreg_read()
338 if (ctx->mbs_user_cb->holding_reg_rd == NULL) { in mbs_fc03_hreg_read()
344 /* Read floating-point register */ in mbs_fc03_hreg_read()
345 if (ctx->mbs_user_cb->holding_reg_rd_fp == NULL) { in mbs_fc03_hreg_read()
356 /* Number of data bytes + byte count. */ in mbs_fc03_hreg_read()
357 ctx->tx_adu.length = num_bytes + 1; in mbs_fc03_hreg_read()
359 ctx->tx_adu.data[0] = (uint8_t)num_bytes; in mbs_fc03_hreg_read()
362 presp = &ctx->tx_adu.data[1]; in mbs_fc03_hreg_read()
369 err = ctx->mbs_user_cb->holding_reg_rd(reg_addr, &reg); in mbs_fc03_hreg_read()
377 reg_qty--; in mbs_fc03_hreg_read()
382 /* Read floating-point register */ in mbs_fc03_hreg_read()
383 err = ctx->mbs_user_cb->holding_reg_rd_fp(reg_addr, &fp); in mbs_fc03_hreg_read()
392 reg_qty -= 2; in mbs_fc03_hreg_read()
410 * Function code 1 Byte
415 * Function code 1 Byte
416 * Byte count 1 Bytes
417 * Register Value N * 2 Byte
422 const uint8_t request_len = 4; in mbs_fc04_inreg_read()
429 if (ctx->rx_adu.length != request_len) { in mbs_fc04_inreg_read()
434 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc04_inreg_read()
435 reg_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc04_inreg_read()
450 if (ctx->mbs_user_cb->input_reg_rd == NULL) { in mbs_fc04_inreg_read()
456 /* Read floating-point register */ in mbs_fc04_inreg_read()
457 if (ctx->mbs_user_cb->input_reg_rd_fp == NULL) { in mbs_fc04_inreg_read()
468 /* Number of data bytes + byte count. */ in mbs_fc04_inreg_read()
469 ctx->tx_adu.length = num_bytes + 1; in mbs_fc04_inreg_read()
471 ctx->tx_adu.data[0] = (uint8_t)num_bytes; in mbs_fc04_inreg_read()
474 presp = &ctx->tx_adu.data[1]; in mbs_fc04_inreg_read()
481 err = ctx->mbs_user_cb->input_reg_rd(reg_addr, &reg); in mbs_fc04_inreg_read()
489 reg_qty--; in mbs_fc04_inreg_read()
494 /* Read floating-point register */ in mbs_fc04_inreg_read()
495 err = ctx->mbs_user_cb->input_reg_rd_fp(reg_addr, &fp); in mbs_fc04_inreg_read()
504 reg_qty -= 2; in mbs_fc04_inreg_read()
521 * Function code 1 Byte
526 * Function code 1 Byte
532 const uint8_t request_len = 4; in mbs_fc05_coil_write()
533 const uint8_t response_len = 4; in mbs_fc05_coil_write()
539 if (ctx->rx_adu.length != request_len) { in mbs_fc05_coil_write()
540 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc05_coil_write()
544 if (ctx->mbs_user_cb->coil_wr == NULL) { in mbs_fc05_coil_write()
550 coil_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc05_coil_write()
551 coil_val = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc05_coil_write()
560 err = ctx->mbs_user_cb->coil_wr(coil_addr, coil_state); in mbs_fc05_coil_write()
569 ctx->tx_adu.length = response_len; in mbs_fc05_coil_write()
570 sys_put_be16(coil_addr, &ctx->tx_adu.data[0]); in mbs_fc05_coil_write()
571 sys_put_be16(coil_val, &ctx->tx_adu.data[2]); in mbs_fc05_coil_write()
580 * Function code 1 Byte
585 * Function code 1 Byte
591 const uint8_t request_len = 4; in mbs_fc06_hreg_write()
592 const uint8_t response_len = 4; in mbs_fc06_hreg_write()
597 if (ctx->rx_adu.length != request_len) { in mbs_fc06_hreg_write()
598 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc06_hreg_write()
602 if (ctx->mbs_user_cb->holding_reg_wr == NULL) { in mbs_fc06_hreg_write()
607 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc06_hreg_write()
608 reg_val = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc06_hreg_write()
610 err = ctx->mbs_user_cb->holding_reg_wr(reg_addr, reg_val); in mbs_fc06_hreg_write()
619 ctx->tx_adu.length = response_len; in mbs_fc06_hreg_write()
620 sys_put_be16(reg_addr, &ctx->tx_adu.data[0]); in mbs_fc06_hreg_write()
621 sys_put_be16(reg_val, &ctx->tx_adu.data[2]); in mbs_fc06_hreg_write()
630 * Function code 1 Byte
631 * Sub-function code 2 Bytes
632 * Data N * 2 Byte
635 * Function code 1 Byte
636 * Sub-function code 2 Bytes
637 * Data N * 2 Byte
642 const uint8_t request_len = 4; in mbs_fc08_diagnostics()
643 const uint8_t response_len = 4; in mbs_fc08_diagnostics()
647 if (ctx->rx_adu.length != request_len) { in mbs_fc08_diagnostics()
648 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc08_diagnostics()
652 sfunc = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc08_diagnostics()
653 data = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc08_diagnostics()
657 /* Sub-function 0x00 return Query Data */ in mbs_fc08_diagnostics()
661 /* Sub-function 0x0A clear Counters and Diagnostic */ in mbs_fc08_diagnostics()
666 /* Sub-function 0x0B return Bus Message Count */ in mbs_fc08_diagnostics()
667 data = ctx->mbs_msg_ctr; in mbs_fc08_diagnostics()
671 /* Sub-function 0x0C return Bus Communication Error Count */ in mbs_fc08_diagnostics()
672 data = ctx->mbs_crc_err_ctr; in mbs_fc08_diagnostics()
676 /* Sub-function 0x0D return Bus Exception Error Count */ in mbs_fc08_diagnostics()
677 data = ctx->mbs_except_ctr; in mbs_fc08_diagnostics()
681 /* Sub-function 0x0E return Server Message Count */ in mbs_fc08_diagnostics()
682 data = ctx->mbs_server_msg_ctr; in mbs_fc08_diagnostics()
686 /* Sub-function 0x0F return Server No Response Count */ in mbs_fc08_diagnostics()
687 data = ctx->mbs_noresp_ctr; in mbs_fc08_diagnostics()
691 LOG_INF("Sub-function not supported"); in mbs_fc08_diagnostics()
697 ctx->tx_adu.length = response_len; in mbs_fc08_diagnostics()
698 sys_put_be16(sfunc, &ctx->tx_adu.data[0]); in mbs_fc08_diagnostics()
699 sys_put_be16(data, &ctx->tx_adu.data[2]); in mbs_fc08_diagnostics()
716 * Function code 1 Byte
719 * Byte Count 1 Byte
720 * Outputs Value N * 1 Byte
723 * Function code 1 Byte
731 const uint8_t response_len = 4; in mbs_fc15_coils_write()
741 if (ctx->rx_adu.length < request_len) { in mbs_fc15_coils_write()
742 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc15_coils_write()
746 if (ctx->mbs_user_cb->coil_wr == NULL) { in mbs_fc15_coils_write()
751 coil_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc15_coils_write()
752 coil_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc15_coils_write()
753 /* Get the byte count for the data. */ in mbs_fc15_coils_write()
754 num_bytes = ctx->rx_adu.data[4]; in mbs_fc15_coils_write()
762 /* Be sure byte count is valid for quantity of coils. */ in mbs_fc15_coils_write()
763 if (((((coil_qty - 1) / 8) + 1) != num_bytes) || in mbs_fc15_coils_write()
764 (ctx->rx_adu.length != (num_bytes + 5))) { in mbs_fc15_coils_write()
771 /* The 1st coil data byte is 6th element in payload */ in mbs_fc15_coils_write()
775 /* Move to the next data byte after every eight bits. */ in mbs_fc15_coils_write()
777 temp = ctx->rx_adu.data[data_ix++]; in mbs_fc15_coils_write()
786 err = ctx->mbs_user_cb->coil_wr(coil_addr + coil_cntr, in mbs_fc15_coils_write()
802 ctx->tx_adu.length = response_len; in mbs_fc15_coils_write()
803 sys_put_be16(coil_addr, &ctx->tx_adu.data[0]); in mbs_fc15_coils_write()
804 sys_put_be16(coil_qty, &ctx->tx_adu.data[2]); in mbs_fc15_coils_write()
813 * Function code 1 Byte
816 * Byte Count 1 Byte
817 * Registers Value N * 1 Byte
820 * Function code 1 Byte
825 * then the function would write to multiple 'floating-point' according to
827 * requested is considered as a 32-bit IEEE-754 floating-point format.
833 const uint8_t response_len = 4; in mbs_fc16_hregs_write()
840 if (ctx->rx_adu.length < request_len) { in mbs_fc16_hregs_write()
841 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc16_hregs_write()
845 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc16_hregs_write()
846 reg_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc16_hregs_write()
847 /* Get the byte count for the data. */ in mbs_fc16_hregs_write()
848 num_bytes = ctx->rx_adu.data[4]; in mbs_fc16_hregs_write()
859 if (ctx->mbs_user_cb->holding_reg_wr == NULL) { in mbs_fc16_hregs_write()
864 /* Write floating-point register */ in mbs_fc16_hregs_write()
865 if (ctx->mbs_user_cb->holding_reg_wr_fp == NULL) { in mbs_fc16_hregs_write()
877 if ((ctx->rx_adu.length - 5) != num_bytes) { in mbs_fc16_hregs_write()
889 /* The 1st registers data byte is 6th element in payload */ in mbs_fc16_hregs_write()
890 prx_data = &ctx->rx_adu.data[5]; in mbs_fc16_hregs_write()
900 err = ctx->mbs_user_cb->holding_reg_wr(addr, reg_val); in mbs_fc16_hregs_write()
909 err = ctx->mbs_user_cb->holding_reg_wr_fp(addr, fp); in mbs_fc16_hregs_write()
921 ctx->tx_adu.length = response_len; in mbs_fc16_hregs_write()
922 sys_put_be16(reg_addr, &ctx->tx_adu.data[0]); in mbs_fc16_hregs_write()
923 sys_put_be16(reg_qty, &ctx->tx_adu.data[2]); in mbs_fc16_hregs_write()
934 SYS_SLIST_FOR_EACH_CONTAINER(&ctx->user_defined_cbs, p, node) { in mbs_try_user_fc()
935 if (p->fc == fc) { in mbs_try_user_fc()
941 p->excep_code = MODBUS_EXC_NONE; in mbs_try_user_fc()
942 rval = p->cb(iface, &ctx->rx_adu, &ctx->tx_adu, &p->excep_code, in mbs_try_user_fc()
943 p->user_data); in mbs_try_user_fc()
945 if (p->excep_code != MODBUS_EXC_NONE) { in mbs_try_user_fc()
946 LOG_INF("Custom handler failed with code %d", p->excep_code); in mbs_try_user_fc()
947 mbs_exception_rsp(ctx, p->excep_code); in mbs_try_user_fc()
963 uint8_t addr = ctx->rx_adu.unit_id; in modbus_server_handler()
964 uint8_t fc = ctx->rx_adu.fc; in modbus_server_handler()
969 if (ctx->rx_adu_err != 0) { in modbus_server_handler()
971 if (ctx->rx_adu_err == -EIO) { in modbus_server_handler()
978 if (addr != 0 && addr != ctx->unit_id) { in modbus_server_handler()
979 LOG_DBG("Unit ID doesn't match %u != %u", addr, ctx->unit_id); in modbus_server_handler()
985 ctx->tx_adu.trans_id = ctx->rx_adu.trans_id; in modbus_server_handler()
986 ctx->tx_adu.proto_id = ctx->rx_adu.proto_id; in modbus_server_handler()
987 ctx->tx_adu.unit_id = addr; in modbus_server_handler()
988 ctx->tx_adu.fc = fc; in modbus_server_handler()