Lines Matching +full:2 +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
99 * Starting Address 2 Bytes
100 * Quantity of Coils 2 Bytes
103 * Function code 1 Byte
104 * Byte count 1 Bytes
105 * Coil status N * 1 Byte
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
198 * Starting Address 2 Bytes
199 * Quantity of Inputs 2 Bytes
202 * Function code 1 Byte
203 * Byte count 1 Bytes
204 * Input status N * 1 Byte
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
299 * Starting Address 2 Bytes
300 * Quantity of Registers 2 Bytes
303 * Function code 1 Byte
304 * Byte count 1 Bytes
305 * Register Value N * 2 Byte
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()
328 if (ctx->mbs_user_cb->holding_reg_rd == NULL) { in mbs_fc03_hreg_read()
342 /* Read floating-point register */ in mbs_fc03_hreg_read()
343 if (ctx->mbs_user_cb->holding_reg_rd_fp == NULL) { in mbs_fc03_hreg_read()
348 if (reg_qty == 0 || reg_qty > (regs_limit / 2)) { in mbs_fc03_hreg_read()
358 /* Number of data bytes + byte count. */ in mbs_fc03_hreg_read()
359 ctx->tx_adu.length = num_bytes + 1; in mbs_fc03_hreg_read()
361 ctx->tx_adu.data[0] = (uint8_t)num_bytes; in mbs_fc03_hreg_read()
364 presp = &ctx->tx_adu.data[1]; in mbs_fc03_hreg_read()
371 err = ctx->mbs_user_cb->holding_reg_rd(reg_addr, ®); in mbs_fc03_hreg_read()
381 /* Read floating-point register */ in mbs_fc03_hreg_read()
382 err = ctx->mbs_user_cb->holding_reg_rd_fp(reg_addr, &fp); in mbs_fc03_hreg_read()
398 reg_qty--; in mbs_fc03_hreg_read()
408 * Function code 1 Byte
409 * Starting Address 2 Bytes
410 * Quantity of Registers 2 Bytes
413 * Function code 1 Byte
414 * Byte count 1 Bytes
415 * Register Value N * 2 Byte
427 if (ctx->rx_adu.length != request_len) { in mbs_fc04_inreg_read()
432 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc04_inreg_read()
433 reg_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc04_inreg_read()
438 if (ctx->mbs_user_cb->input_reg_rd == NULL) { in mbs_fc04_inreg_read()
452 /* Read floating-point register */ in mbs_fc04_inreg_read()
453 if (ctx->mbs_user_cb->input_reg_rd_fp == NULL) { in mbs_fc04_inreg_read()
458 if (reg_qty == 0 || reg_qty > (regs_limit / 2)) { 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, ®); in mbs_fc04_inreg_read()
491 /* Read floating-point register */ in mbs_fc04_inreg_read()
492 err = ctx->mbs_user_cb->input_reg_rd_fp(reg_addr, &fp); in mbs_fc04_inreg_read()
508 reg_qty--; in mbs_fc04_inreg_read()
518 * Function code 1 Byte
519 * Output Address 2 Bytes
520 * Output Value 2 Bytes
523 * Function code 1 Byte
524 * Output Address 2 Bytes
525 * Output Value 2 Bytes
536 if (ctx->rx_adu.length != request_len) { in mbs_fc05_coil_write()
537 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc05_coil_write()
541 if (ctx->mbs_user_cb->coil_wr == NULL) { in mbs_fc05_coil_write()
547 coil_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc05_coil_write()
548 coil_val = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc05_coil_write()
557 err = ctx->mbs_user_cb->coil_wr(coil_addr, coil_state); in mbs_fc05_coil_write()
566 ctx->tx_adu.length = response_len; in mbs_fc05_coil_write()
567 sys_put_be16(coil_addr, &ctx->tx_adu.data[0]); in mbs_fc05_coil_write()
568 sys_put_be16(coil_val, &ctx->tx_adu.data[2]); in mbs_fc05_coil_write()
577 * Function code 1 Byte
578 * Register Address 2 Bytes
579 * Register Value 2 Bytes
582 * Function code 1 Byte
583 * Register Address 2 Bytes
584 * Register Value 2 Bytes
594 if (ctx->rx_adu.length != request_len) { in mbs_fc06_hreg_write()
595 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc06_hreg_write()
599 if (ctx->mbs_user_cb->holding_reg_wr == NULL) { in mbs_fc06_hreg_write()
604 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc06_hreg_write()
605 reg_val = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc06_hreg_write()
607 err = ctx->mbs_user_cb->holding_reg_wr(reg_addr, reg_val); in mbs_fc06_hreg_write()
616 ctx->tx_adu.length = response_len; in mbs_fc06_hreg_write()
617 sys_put_be16(reg_addr, &ctx->tx_adu.data[0]); in mbs_fc06_hreg_write()
618 sys_put_be16(reg_val, &ctx->tx_adu.data[2]); in mbs_fc06_hreg_write()
627 * Function code 1 Byte
628 * Sub-function code 2 Bytes
629 * Data N * 2 Byte
632 * Function code 1 Byte
633 * Sub-function code 2 Bytes
634 * Data N * 2 Byte
644 if (ctx->rx_adu.length != request_len) { in mbs_fc08_diagnostics()
645 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc08_diagnostics()
649 sfunc = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc08_diagnostics()
650 data = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc08_diagnostics()
654 /* Sub-function 0x00 return Query Data */ in mbs_fc08_diagnostics()
658 /* Sub-function 0x0A clear Counters and Diagnostic */ in mbs_fc08_diagnostics()
663 /* Sub-function 0x0B return Bus Message Count */ in mbs_fc08_diagnostics()
664 data = ctx->mbs_msg_ctr; in mbs_fc08_diagnostics()
668 /* Sub-function 0x0C return Bus Communication Error Count */ in mbs_fc08_diagnostics()
669 data = ctx->mbs_crc_err_ctr; in mbs_fc08_diagnostics()
673 /* Sub-function 0x0D return Bus Exception Error Count */ in mbs_fc08_diagnostics()
674 data = ctx->mbs_except_ctr; in mbs_fc08_diagnostics()
678 /* Sub-function 0x0E return Server Message Count */ in mbs_fc08_diagnostics()
679 data = ctx->mbs_server_msg_ctr; in mbs_fc08_diagnostics()
683 /* Sub-function 0x0F return Server No Response Count */ in mbs_fc08_diagnostics()
684 data = ctx->mbs_noresp_ctr; in mbs_fc08_diagnostics()
688 LOG_INF("Sub-function not supported"); in mbs_fc08_diagnostics()
694 ctx->tx_adu.length = response_len; in mbs_fc08_diagnostics()
695 sys_put_be16(sfunc, &ctx->tx_adu.data[0]); in mbs_fc08_diagnostics()
696 sys_put_be16(data, &ctx->tx_adu.data[2]); in mbs_fc08_diagnostics()
713 * Function code 1 Byte
714 * Starting Address 2 Bytes
715 * Quantity of Outputs 2 Bytes
716 * Byte Count 1 Byte
717 * Outputs Value N * 1 Byte
720 * Function code 1 Byte
721 * Starting Address 2 Bytes
722 * Quantity of Outputs 2 Bytes
738 if (ctx->rx_adu.length < request_len) { in mbs_fc15_coils_write()
739 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc15_coils_write()
743 if (ctx->mbs_user_cb->coil_wr == NULL) { in mbs_fc15_coils_write()
748 coil_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc15_coils_write()
749 coil_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc15_coils_write()
750 /* Get the byte count for the data. */ in mbs_fc15_coils_write()
751 num_bytes = ctx->rx_adu.data[4]; in mbs_fc15_coils_write()
759 /* Be sure byte count is valid for quantity of coils. */ in mbs_fc15_coils_write()
760 if (((((coil_qty - 1) / 8) + 1) != num_bytes) || in mbs_fc15_coils_write()
761 (ctx->rx_adu.length != (num_bytes + 5))) { in mbs_fc15_coils_write()
768 /* The 1st coil data byte is 6th element in payload */ in mbs_fc15_coils_write()
772 /* Move to the next data byte after every eight bits. */ in mbs_fc15_coils_write()
774 temp = ctx->rx_adu.data[data_ix++]; in mbs_fc15_coils_write()
783 err = ctx->mbs_user_cb->coil_wr(coil_addr + coil_cntr, in mbs_fc15_coils_write()
799 ctx->tx_adu.length = response_len; in mbs_fc15_coils_write()
800 sys_put_be16(coil_addr, &ctx->tx_adu.data[0]); in mbs_fc15_coils_write()
801 sys_put_be16(coil_qty, &ctx->tx_adu.data[2]); in mbs_fc15_coils_write()
810 * Function code 1 Byte
811 * Starting Address 2 Bytes
812 * Quantity of Registers 2 Bytes
813 * Byte Count 1 Byte
814 * Registers Value N * 1 Byte
817 * Function code 1 Byte
818 * Starting Address 2 Bytes
819 * Quantity of Registers 2 Bytes
822 * then the function would write to multiple 'floating-point' according to
824 * requested is considered as a 32-bit IEEE-754 floating-point format.
838 if (ctx->rx_adu.length < request_len) { in mbs_fc16_hregs_write()
839 LOG_ERR("Wrong request length %u", ctx->rx_adu.length); in mbs_fc16_hregs_write()
843 reg_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbs_fc16_hregs_write()
844 reg_qty = sys_get_be16(&ctx->rx_adu.data[2]); in mbs_fc16_hregs_write()
845 /* Get the byte count for the data. */ in mbs_fc16_hregs_write()
846 num_bytes = ctx->rx_adu.data[4]; in mbs_fc16_hregs_write()
851 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()
870 if (reg_qty == 0 || reg_qty > (regs_limit / 2)) { in mbs_fc16_hregs_write()
880 if ((ctx->rx_adu.length - 5) != num_bytes) { in mbs_fc16_hregs_write()
892 /* The 1st registers data byte is 6th element in payload */ in mbs_fc16_hregs_write()
893 prx_data = &ctx->rx_adu.data[5]; in mbs_fc16_hregs_write()
903 err = ctx->mbs_user_cb->holding_reg_wr(addr, reg_val); in mbs_fc16_hregs_write()
911 err = ctx->mbs_user_cb->holding_reg_wr_fp(addr, fp); in mbs_fc16_hregs_write()
922 ctx->tx_adu.length = response_len; in mbs_fc16_hregs_write()
923 sys_put_be16(reg_addr, &ctx->tx_adu.data[0]); in mbs_fc16_hregs_write()
924 sys_put_be16(reg_qty, &ctx->tx_adu.data[2]); in mbs_fc16_hregs_write()
935 SYS_SLIST_FOR_EACH_CONTAINER(&ctx->user_defined_cbs, p, node) { in mbs_try_user_fc()
936 if (p->fc == fc) { in mbs_try_user_fc()
942 p->excep_code = MODBUS_EXC_NONE; in mbs_try_user_fc()
943 rval = p->cb(iface, &ctx->rx_adu, &ctx->tx_adu, &p->excep_code, in mbs_try_user_fc()
944 p->user_data); in mbs_try_user_fc()
946 if (p->excep_code != MODBUS_EXC_NONE) { in mbs_try_user_fc()
947 LOG_INF("Custom handler failed with code %d", p->excep_code); in mbs_try_user_fc()
948 mbs_exception_rsp(ctx, p->excep_code); in mbs_try_user_fc()
964 uint8_t addr = ctx->rx_adu.unit_id; in modbus_server_handler()
965 uint8_t fc = ctx->rx_adu.fc; in modbus_server_handler()
970 if (ctx->rx_adu_err != 0) { in modbus_server_handler()
972 if (ctx->rx_adu_err == -EIO) { in modbus_server_handler()
979 if (addr != 0 && addr != ctx->unit_id) { in modbus_server_handler()
980 LOG_DBG("Unit ID doesn't match %u != %u", addr, ctx->unit_id); in modbus_server_handler()
986 ctx->tx_adu.trans_id = ctx->rx_adu.trans_id; in modbus_server_handler()
987 ctx->tx_adu.proto_id = ctx->rx_adu.proto_id; in modbus_server_handler()
988 ctx->tx_adu.unit_id = addr; in modbus_server_handler()
989 ctx->tx_adu.fc = fc; in modbus_server_handler()