Lines Matching full:ctx
29 static int mbc_validate_response_fc(struct modbus_context *ctx, in mbc_validate_response_fc() argument
33 uint8_t resp_fc = ctx->rx_adu.fc; in mbc_validate_response_fc()
34 uint8_t excep_code = ctx->rx_adu.data[0]; in mbc_validate_response_fc()
38 if (unit_id != ctx->rx_adu.unit_id) { in mbc_validate_response_fc()
57 static int mbc_validate_fc03fp_response(struct modbus_context *ctx, float *ptbl) in mbc_validate_fc03fp_response() argument
64 resp_byte_cnt = ctx->rx_adu.data[0]; in mbc_validate_fc03fp_response()
65 resp_data = &ctx->rx_adu.data[1]; in mbc_validate_fc03fp_response()
66 req_qty = sys_get_be16(&ctx->tx_adu.data[2]); in mbc_validate_fc03fp_response()
84 static int mbc_validate_rd_response(struct modbus_context *ctx, in mbc_validate_rd_response() argument
101 resp_byte_cnt = ctx->rx_adu.data[0]; in mbc_validate_rd_response()
102 resp_data = &ctx->rx_adu.data[1]; in mbc_validate_rd_response()
103 req_qty = sys_get_be16(&ctx->tx_adu.data[2]); in mbc_validate_rd_response()
104 req_addr = sys_get_be16(&ctx->tx_adu.data[0]); in mbc_validate_rd_response()
106 if ((resp_byte_cnt + 1) > sizeof(ctx->rx_adu.data)) { in mbc_validate_rd_response()
130 err = mbc_validate_fc03fp_response(ctx, (float *)data); in mbc_validate_rd_response()
157 static int mbc_validate_fc08_response(struct modbus_context *ctx, in mbc_validate_fc08_response() argument
171 req_sfunc = sys_get_be16(&ctx->tx_adu.data[0]); in mbc_validate_fc08_response()
172 req_data = sys_get_be16(&ctx->tx_adu.data[2]); in mbc_validate_fc08_response()
173 resp_sfunc = sys_get_be16(&ctx->rx_adu.data[0]); in mbc_validate_fc08_response()
174 resp_data = sys_get_be16(&ctx->rx_adu.data[2]); in mbc_validate_fc08_response()
209 static int mbc_validate_wr_response(struct modbus_context *ctx, in mbc_validate_wr_response() argument
219 req_addr = sys_get_be16(&ctx->tx_adu.data[0]); in mbc_validate_wr_response()
220 req_value = sys_get_be16(&ctx->tx_adu.data[2]); in mbc_validate_wr_response()
221 resp_addr = sys_get_be16(&ctx->rx_adu.data[0]); in mbc_validate_wr_response()
222 resp_value = sys_get_be16(&ctx->rx_adu.data[2]); in mbc_validate_wr_response()
244 static int mbc_send_cmd(struct modbus_context *ctx, const uint8_t unit_id, in mbc_send_cmd() argument
249 ctx->tx_adu.unit_id = unit_id; in mbc_send_cmd()
250 ctx->tx_adu.fc = fc; in mbc_send_cmd()
252 err = modbus_tx_wait_rx_adu(ctx); in mbc_send_cmd()
257 err = mbc_validate_response_fc(ctx, unit_id, fc); in mbc_send_cmd()
271 err = mbc_validate_rd_response(ctx, unit_id, fc, data); in mbc_send_cmd()
275 err = mbc_validate_fc08_response(ctx, unit_id, data); in mbc_send_cmd()
282 err = mbc_validate_wr_response(ctx, unit_id, fc); in mbc_send_cmd()
299 struct modbus_context *ctx = modbus_get_context(iface); in modbus_read_coils() local
302 if (ctx == NULL) { in modbus_read_coils()
306 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_read_coils()
308 ctx->tx_adu.length = 4; in modbus_read_coils()
309 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_read_coils()
310 sys_put_be16(num_coils, &ctx->tx_adu.data[2]); in modbus_read_coils()
312 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC01_COIL_RD, coil_tbl); in modbus_read_coils()
313 k_mutex_unlock(&ctx->iface_lock); in modbus_read_coils()
324 struct modbus_context *ctx = modbus_get_context(iface); in modbus_read_dinputs() local
327 if (ctx == NULL) { in modbus_read_dinputs()
331 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_read_dinputs()
333 ctx->tx_adu.length = 4; in modbus_read_dinputs()
334 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_read_dinputs()
335 sys_put_be16(num_di, &ctx->tx_adu.data[2]); in modbus_read_dinputs()
337 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC02_DI_RD, di_tbl); in modbus_read_dinputs()
338 k_mutex_unlock(&ctx->iface_lock); in modbus_read_dinputs()
349 struct modbus_context *ctx = modbus_get_context(iface); in modbus_read_holding_regs() local
352 if (ctx == NULL) { in modbus_read_holding_regs()
356 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_read_holding_regs()
358 ctx->tx_adu.length = 4; in modbus_read_holding_regs()
359 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_read_holding_regs()
360 sys_put_be16(num_regs, &ctx->tx_adu.data[2]); in modbus_read_holding_regs()
362 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC03_HOLDING_REG_RD, reg_buf); in modbus_read_holding_regs()
363 k_mutex_unlock(&ctx->iface_lock); in modbus_read_holding_regs()
376 struct modbus_context *ctx = modbus_get_context(iface); in modbus_read_holding_regs_fp() local
379 if (ctx == NULL) { in modbus_read_holding_regs_fp()
383 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_read_holding_regs_fp()
385 ctx->tx_adu.length = 4; in modbus_read_holding_regs_fp()
386 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_read_holding_regs_fp()
388 sys_put_be16(num_regs * 2, &ctx->tx_adu.data[2]); in modbus_read_holding_regs_fp()
390 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC03_HOLDING_REG_RD, reg_buf); in modbus_read_holding_regs_fp()
391 k_mutex_unlock(&ctx->iface_lock); in modbus_read_holding_regs_fp()
403 struct modbus_context *ctx = modbus_get_context(iface); in modbus_read_input_regs() local
406 if (ctx == NULL) { in modbus_read_input_regs()
410 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_read_input_regs()
412 ctx->tx_adu.length = 4; in modbus_read_input_regs()
413 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_read_input_regs()
414 sys_put_be16(num_regs, &ctx->tx_adu.data[2]); in modbus_read_input_regs()
416 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC04_IN_REG_RD, reg_buf); in modbus_read_input_regs()
417 k_mutex_unlock(&ctx->iface_lock); in modbus_read_input_regs()
427 struct modbus_context *ctx = modbus_get_context(iface); in modbus_write_coil() local
431 if (ctx == NULL) { in modbus_write_coil()
435 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_write_coil()
443 ctx->tx_adu.length = 4; in modbus_write_coil()
444 sys_put_be16(coil_addr, &ctx->tx_adu.data[0]); in modbus_write_coil()
445 sys_put_be16(coil_val, &ctx->tx_adu.data[2]); in modbus_write_coil()
447 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC05_COIL_WR, NULL); in modbus_write_coil()
448 k_mutex_unlock(&ctx->iface_lock); in modbus_write_coil()
458 struct modbus_context *ctx = modbus_get_context(iface); in modbus_write_holding_reg() local
461 if (ctx == NULL) { in modbus_write_holding_reg()
465 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_write_holding_reg()
467 ctx->tx_adu.length = 4; in modbus_write_holding_reg()
468 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_write_holding_reg()
469 sys_put_be16(reg_val, &ctx->tx_adu.data[2]); in modbus_write_holding_reg()
471 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC06_HOLDING_REG_WR, NULL); in modbus_write_holding_reg()
472 k_mutex_unlock(&ctx->iface_lock); in modbus_write_holding_reg()
483 struct modbus_context *ctx = modbus_get_context(iface); in modbus_request_diagnostic() local
486 if (ctx == NULL) { in modbus_request_diagnostic()
490 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_request_diagnostic()
492 ctx->tx_adu.length = 4; in modbus_request_diagnostic()
493 sys_put_be16(sfunc, &ctx->tx_adu.data[0]); in modbus_request_diagnostic()
494 sys_put_be16(data, &ctx->tx_adu.data[2]); in modbus_request_diagnostic()
496 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC08_DIAGNOSTICS, data_out); in modbus_request_diagnostic()
497 k_mutex_unlock(&ctx->iface_lock); in modbus_request_diagnostic()
508 struct modbus_context *ctx = modbus_get_context(iface); in modbus_write_coils() local
514 if (ctx == NULL) { in modbus_write_coils()
518 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_write_coils()
520 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_write_coils()
522 sys_put_be16(num_coils, &ctx->tx_adu.data[2]); in modbus_write_coils()
526 ctx->tx_adu.data[4] = num_bytes; in modbus_write_coils()
529 if (length > sizeof(ctx->tx_adu.data)) { in modbus_write_coils()
531 k_mutex_unlock(&ctx->iface_lock); in modbus_write_coils()
535 ctx->tx_adu.length = length; in modbus_write_coils()
536 data_ptr = &ctx->tx_adu.data[5]; in modbus_write_coils()
540 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC15_COILS_WR, NULL); in modbus_write_coils()
541 k_mutex_unlock(&ctx->iface_lock); in modbus_write_coils()
552 struct modbus_context *ctx = modbus_get_context(iface); in modbus_write_holding_regs() local
558 if (ctx == NULL) { in modbus_write_holding_regs()
562 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_write_holding_regs()
564 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_write_holding_regs()
566 sys_put_be16(num_regs, &ctx->tx_adu.data[2]); in modbus_write_holding_regs()
570 ctx->tx_adu.data[4] = num_bytes; in modbus_write_holding_regs()
573 if (length > sizeof(ctx->tx_adu.data)) { in modbus_write_holding_regs()
575 k_mutex_unlock(&ctx->iface_lock); in modbus_write_holding_regs()
579 ctx->tx_adu.length = length; in modbus_write_holding_regs()
580 data_ptr = &ctx->tx_adu.data[5]; in modbus_write_holding_regs()
587 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC16_HOLDING_REGS_WR, NULL); in modbus_write_holding_regs()
588 k_mutex_unlock(&ctx->iface_lock); in modbus_write_holding_regs()
600 struct modbus_context *ctx = modbus_get_context(iface); in modbus_write_holding_regs_fp() local
606 if (ctx == NULL) { in modbus_write_holding_regs_fp()
610 k_mutex_lock(&ctx->iface_lock, K_FOREVER); in modbus_write_holding_regs_fp()
612 sys_put_be16(start_addr, &ctx->tx_adu.data[0]); in modbus_write_holding_regs_fp()
615 sys_put_be16(num_regs * 2, &ctx->tx_adu.data[2]); in modbus_write_holding_regs_fp()
619 ctx->tx_adu.data[4] = num_bytes; in modbus_write_holding_regs_fp()
622 if (length > sizeof(ctx->tx_adu.data)) { in modbus_write_holding_regs_fp()
624 k_mutex_unlock(&ctx->iface_lock); in modbus_write_holding_regs_fp()
628 ctx->tx_adu.length = length; in modbus_write_holding_regs_fp()
629 data_ptr = &ctx->tx_adu.data[5]; in modbus_write_holding_regs_fp()
639 err = mbc_send_cmd(ctx, unit_id, MODBUS_FC16_HOLDING_REGS_WR, NULL); in modbus_write_holding_regs_fp()
640 k_mutex_unlock(&ctx->iface_lock); in modbus_write_holding_regs_fp()