-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathmodbus_client.c
642 lines (514 loc) · 14.2 KB
/
modbus_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Copyright (c) 2020 PHYTEC Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This file is based on mbm_core.c from uC/Modbus Stack.
*
* uC/Modbus
* The Embedded Modbus Stack
*
* Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*/
#include <string.h>
#include <zephyr/sys/byteorder.h>
#include <modbus_internal.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(modbus_c, CONFIG_MODBUS_LOG_LEVEL);
static int mbc_validate_response_fc(struct modbus_context *ctx,
const uint8_t unit_id,
uint8_t fc)
{
uint8_t resp_fc = ctx->rx_adu.fc;
uint8_t excep_code = ctx->rx_adu.data[0];
const uint8_t excep_bit = BIT(7);
const uint8_t excep_mask = BIT_MASK(7);
if (unit_id != ctx->rx_adu.unit_id) {
return -EIO;
}
if (fc != (resp_fc & excep_mask)) {
return -EIO;
}
if (resp_fc & excep_bit) {
if (excep_code > MODBUS_EXC_NONE) {
return excep_code;
}
return -EIO;
}
return 0;
}
static int mbc_validate_fc03fp_response(struct modbus_context *ctx, float *ptbl)
{
size_t resp_byte_cnt;
size_t req_byte_cnt;
uint16_t req_qty;
uint8_t *resp_data;
resp_byte_cnt = ctx->rx_adu.data[0];
resp_data = &ctx->rx_adu.data[1];
req_qty = sys_get_be16(&ctx->tx_adu.data[2]);
req_byte_cnt = req_qty * sizeof(float);
if (req_byte_cnt != resp_byte_cnt) {
LOG_ERR("Mismatch in the number of registers");
return -EINVAL;
}
for (uint16_t i = 0; i < req_qty; i++) {
uint32_t reg_val = sys_get_be32(resp_data);
memcpy(&ptbl[i], ®_val, sizeof(float));
resp_data += sizeof(uint32_t);
}
return 0;
}
static int mbc_validate_rd_response(struct modbus_context *ctx,
const uint8_t unit_id,
uint8_t fc,
uint8_t *data)
{
int err;
size_t resp_byte_cnt;
size_t req_byte_cnt;
uint16_t req_qty;
uint16_t req_addr;
uint8_t *resp_data;
uint16_t *data_p16 = (uint16_t *)data;
if (data == NULL) {
return -EINVAL;
}
resp_byte_cnt = ctx->rx_adu.data[0];
resp_data = &ctx->rx_adu.data[1];
req_qty = sys_get_be16(&ctx->tx_adu.data[2]);
req_addr = sys_get_be16(&ctx->tx_adu.data[0]);
if ((resp_byte_cnt + 1) > sizeof(ctx->rx_adu.data)) {
LOG_ERR("Byte count exceeds buffer length");
return -EINVAL;
}
switch (fc) {
case MODBUS_FC01_COIL_RD:
case MODBUS_FC02_DI_RD:
req_byte_cnt = ((req_qty - 1) / 8) + 1;
if (req_byte_cnt != resp_byte_cnt) {
LOG_ERR("Mismatch in the number of coils or inputs");
err = -EINVAL;
} else {
for (uint8_t i = 0; i < resp_byte_cnt; i++) {
data[i] = resp_data[i];
}
err = 0;
}
break;
case MODBUS_FC03_HOLDING_REG_RD:
if (IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS) &&
(req_addr >= MODBUS_FP_EXTENSIONS_ADDR)) {
err = mbc_validate_fc03fp_response(ctx, (float *)data);
break;
}
__fallthrough;
case MODBUS_FC04_IN_REG_RD:
req_byte_cnt = req_qty * sizeof(uint16_t);
if (req_byte_cnt != resp_byte_cnt) {
LOG_ERR("Mismatch in the number of registers");
err = -EINVAL;
} else {
for (uint16_t i = 0; i < req_qty; i++) {
data_p16[i] = sys_get_be16(resp_data);
resp_data += sizeof(uint16_t);
}
err = 0;
}
break;
default:
LOG_ERR("Validation not implemented for FC 0x%02x", fc);
err = -ENOTSUP;
}
return err;
}
static int mbc_validate_fc08_response(struct modbus_context *ctx,
const uint8_t unit_id,
uint16_t *data)
{
int err;
uint16_t resp_sfunc;
uint16_t resp_data;
uint16_t req_sfunc;
uint16_t req_data;
if (data == NULL) {
return -EINVAL;
}
req_sfunc = sys_get_be16(&ctx->tx_adu.data[0]);
req_data = sys_get_be16(&ctx->tx_adu.data[2]);
resp_sfunc = sys_get_be16(&ctx->rx_adu.data[0]);
resp_data = sys_get_be16(&ctx->rx_adu.data[2]);
if (req_sfunc != resp_sfunc) {
LOG_ERR("Mismatch in the sub-function code");
return -EINVAL;
}
switch (resp_sfunc) {
case MODBUS_FC08_SUBF_QUERY:
case MODBUS_FC08_SUBF_CLR_CTR:
if (req_data != resp_data) {
LOG_ERR("Request and response data are different");
err = -EINVAL;
} else {
*data = resp_data;
err = 0;
}
break;
case MODBUS_FC08_SUBF_BUS_MSG_CTR:
case MODBUS_FC08_SUBF_BUS_CRC_CTR:
case MODBUS_FC08_SUBF_BUS_EXCEPT_CTR:
case MODBUS_FC08_SUBF_SERVER_MSG_CTR:
case MODBUS_FC08_SUBF_SERVER_NO_RESP_CTR:
*data = resp_data;
err = 0;
break;
default:
err = -EINVAL;
}
return err;
}
static int mbc_validate_wr_response(struct modbus_context *ctx,
const uint8_t unit_id,
uint8_t fc)
{
int err;
uint16_t req_addr;
uint16_t req_value;
uint16_t resp_addr;
uint16_t resp_value;
req_addr = sys_get_be16(&ctx->tx_adu.data[0]);
req_value = sys_get_be16(&ctx->tx_adu.data[2]);
resp_addr = sys_get_be16(&ctx->rx_adu.data[0]);
resp_value = sys_get_be16(&ctx->rx_adu.data[2]);
switch (fc) {
case MODBUS_FC05_COIL_WR:
case MODBUS_FC06_HOLDING_REG_WR:
case MODBUS_FC15_COILS_WR:
case MODBUS_FC16_HOLDING_REGS_WR:
if (req_addr != resp_addr || req_value != resp_value) {
err = ENXIO;
} else {
err = 0;
}
break;
default:
LOG_ERR("Validation not implemented for FC 0x%02x", fc);
err = -ENOTSUP;
}
return err;
}
static int mbc_send_cmd(struct modbus_context *ctx, const uint8_t unit_id,
uint8_t fc, void *data)
{
int err;
ctx->tx_adu.unit_id = unit_id;
ctx->tx_adu.fc = fc;
err = modbus_tx_wait_rx_adu(ctx);
if (err != 0) {
return err;
}
err = mbc_validate_response_fc(ctx, unit_id, fc);
if (err < 0) {
LOG_ERR("Failed to validate unit ID or function code");
return err;
} else if (err > 0) {
LOG_INF("Modbus FC %u, error code %u", fc, err);
return err;
}
switch (fc) {
case MODBUS_FC01_COIL_RD:
case MODBUS_FC02_DI_RD:
case MODBUS_FC03_HOLDING_REG_RD:
case MODBUS_FC04_IN_REG_RD:
err = mbc_validate_rd_response(ctx, unit_id, fc, data);
break;
case MODBUS_FC08_DIAGNOSTICS:
err = mbc_validate_fc08_response(ctx, unit_id, data);
break;
case MODBUS_FC05_COIL_WR:
case MODBUS_FC06_HOLDING_REG_WR:
case MODBUS_FC15_COILS_WR:
case MODBUS_FC16_HOLDING_REGS_WR:
err = mbc_validate_wr_response(ctx, unit_id, fc);
break;
default:
LOG_ERR("FC 0x%02x not implemented", fc);
err = -ENOTSUP;
}
return err;
}
int modbus_read_coils(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint8_t *const coil_tbl,
const uint16_t num_coils)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
sys_put_be16(num_coils, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC01_COIL_RD, coil_tbl);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_read_dinputs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint8_t *const di_tbl,
const uint16_t num_di)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
sys_put_be16(num_di, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC02_DI_RD, di_tbl);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_read_holding_regs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint16_t *const reg_buf,
const uint16_t num_regs)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC03_HOLDING_REG_RD, reg_buf);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
int modbus_read_holding_regs_fp(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
float *const reg_buf,
const uint16_t num_regs)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC03_HOLDING_REG_RD, reg_buf);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
#endif
int modbus_read_input_regs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint16_t *const reg_buf,
const uint16_t num_regs)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC04_IN_REG_RD, reg_buf);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_write_coil(const int iface,
const uint8_t unit_id,
const uint16_t coil_addr,
const bool coil_state)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
uint16_t coil_val;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
if (coil_state == false) {
coil_val = MODBUS_COIL_OFF_CODE;
} else {
coil_val = MODBUS_COIL_ON_CODE;
}
ctx->tx_adu.length = 4;
sys_put_be16(coil_addr, &ctx->tx_adu.data[0]);
sys_put_be16(coil_val, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC05_COIL_WR, NULL);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_write_holding_reg(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
const uint16_t reg_val)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
sys_put_be16(reg_val, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC06_HOLDING_REG_WR, NULL);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_request_diagnostic(const int iface,
const uint8_t unit_id,
const uint16_t sfunc,
const uint16_t data,
uint16_t *const data_out)
{
struct modbus_context *ctx = modbus_get_context(iface);
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
ctx->tx_adu.length = 4;
sys_put_be16(sfunc, &ctx->tx_adu.data[0]);
sys_put_be16(data, &ctx->tx_adu.data[2]);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC08_DIAGNOSTICS, data_out);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_write_coils(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint8_t *const coil_tbl,
const uint16_t num_coils)
{
struct modbus_context *ctx = modbus_get_context(iface);
size_t length = 0;
uint8_t *data_ptr;
size_t num_bytes;
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
length += sizeof(start_addr);
sys_put_be16(num_coils, &ctx->tx_adu.data[2]);
length += sizeof(num_coils);
num_bytes = (uint8_t)(((num_coils - 1) / 8) + 1);
ctx->tx_adu.data[4] = num_bytes;
length += num_bytes + 1;
if (length > sizeof(ctx->tx_adu.data)) {
LOG_ERR("Length of data buffer is not sufficient");
k_mutex_unlock(&ctx->iface_lock);
return -ENOBUFS;
}
ctx->tx_adu.length = length;
data_ptr = &ctx->tx_adu.data[5];
memcpy(data_ptr, coil_tbl, num_bytes);
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC15_COILS_WR, NULL);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
int modbus_write_holding_regs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint16_t *const reg_buf,
const uint16_t num_regs)
{
struct modbus_context *ctx = modbus_get_context(iface);
size_t length = 0;
uint8_t *data_ptr;
size_t num_bytes;
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
length += sizeof(start_addr);
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
length += sizeof(num_regs);
num_bytes = num_regs * sizeof(uint16_t);
ctx->tx_adu.data[4] = num_bytes;
length += num_bytes + 1;
if (length > sizeof(ctx->tx_adu.data)) {
LOG_ERR("Length of data buffer is not sufficient");
k_mutex_unlock(&ctx->iface_lock);
return -ENOBUFS;
}
ctx->tx_adu.length = length;
data_ptr = &ctx->tx_adu.data[5];
for (uint16_t i = 0; i < num_regs; i++) {
sys_put_be16(reg_buf[i], data_ptr);
data_ptr += sizeof(uint16_t);
}
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC16_HOLDING_REGS_WR, NULL);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
int modbus_write_holding_regs_fp(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
float *const reg_buf,
const uint16_t num_regs)
{
struct modbus_context *ctx = modbus_get_context(iface);
size_t length = 0;
uint8_t *data_ptr;
size_t num_bytes;
int err;
if (ctx == NULL) {
return -ENODEV;
}
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
length += sizeof(start_addr);
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
length += sizeof(num_regs);
num_bytes = num_regs * sizeof(float);
ctx->tx_adu.data[4] = num_bytes;
length += num_bytes + 1;
if (length > sizeof(ctx->tx_adu.data)) {
LOG_ERR("Length of data buffer is not sufficient");
k_mutex_unlock(&ctx->iface_lock);
return -ENOBUFS;
}
ctx->tx_adu.length = length;
data_ptr = &ctx->tx_adu.data[5];
for (uint16_t i = 0; i < num_regs; i++) {
uint32_t reg_val;
memcpy(®_val, ®_buf[i], sizeof(reg_val));
sys_put_be32(reg_val, data_ptr);
data_ptr += sizeof(uint32_t);
}
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC16_HOLDING_REGS_WR, NULL);
k_mutex_unlock(&ctx->iface_lock);
return err;
}
#endif