-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathnanomodbus.c
2461 lines (1836 loc) · 73.1 KB
/
nanomodbus.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
nanoMODBUS - A compact MODBUS RTU/TCP C library for microcontrollers
MIT License
Copyright (c) 2024 Valerio De Benedetto (@debevv)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "nanomodbus.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#define NMBS_UNUSED_PARAM(x) ((x) = (x))
#ifdef NMBS_DEBUG
#include <stdio.h>
#define NMBS_DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define NMBS_DEBUG_PRINT(...) (void) (0)
#endif
static uint8_t get_1(nmbs_t* nmbs) {
uint8_t result = nmbs->msg.buf[nmbs->msg.buf_idx];
nmbs->msg.buf_idx++;
return result;
}
static void put_1(nmbs_t* nmbs, uint8_t data) {
nmbs->msg.buf[nmbs->msg.buf_idx] = data;
nmbs->msg.buf_idx++;
}
static void discard_1(nmbs_t* nmbs) {
nmbs->msg.buf_idx++;
}
#ifndef NMBS_SERVER_DISABLED
#if !defined(NMBS_SERVER_READ_FILE_RECORD_DISABLED) || !defined(NMBS_SERVER_WRITE_FILE_RECORD_DISABLED)
static void discard_n(nmbs_t* nmbs, uint16_t n) {
nmbs->msg.buf_idx += n;
}
#endif
#endif
static uint16_t get_2(nmbs_t* nmbs) {
const uint16_t result =
((uint16_t) nmbs->msg.buf[nmbs->msg.buf_idx]) << 8 | (uint16_t) nmbs->msg.buf[nmbs->msg.buf_idx + 1];
nmbs->msg.buf_idx += 2;
return result;
}
static void put_2(nmbs_t* nmbs, uint16_t data) {
nmbs->msg.buf[nmbs->msg.buf_idx] = (uint8_t) ((data >> 8) & 0xFFU);
nmbs->msg.buf[nmbs->msg.buf_idx + 1] = (uint8_t) data;
nmbs->msg.buf_idx += 2;
}
#ifndef NMBS_SERVER_DISABLED
#if !defined(NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED)
static void set_1(nmbs_t* nmbs, uint8_t data, uint8_t index) {
nmbs->msg.buf[index] = data;
}
static void set_2(nmbs_t* nmbs, uint16_t data, uint8_t index) {
nmbs->msg.buf[index] = (uint8_t) ((data >> 8) & 0xFFU);
nmbs->msg.buf[index + 1] = (uint8_t) data;
}
#endif
#endif
static uint8_t* get_n(nmbs_t* nmbs, uint16_t n) {
uint8_t* msg_buf_ptr = nmbs->msg.buf + nmbs->msg.buf_idx;
nmbs->msg.buf_idx += n;
return msg_buf_ptr;
}
#ifndef NMBS_SERVER_DISABLED
#if !defined(NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED)
static void put_n(nmbs_t* nmbs, const uint8_t* data, uint8_t size) {
memcpy(&nmbs->msg.buf[nmbs->msg.buf_idx], data, size);
nmbs->msg.buf_idx += size;
}
#endif
#if !defined(NMBS_SERVER_WRITE_FILE_RECORD_DISABLED)
static uint16_t* get_regs(nmbs_t* nmbs, uint16_t n) {
uint16_t* msg_buf_ptr = (uint16_t*) (nmbs->msg.buf + nmbs->msg.buf_idx);
nmbs->msg.buf_idx += n * 2;
while (n--) {
msg_buf_ptr[n] = (msg_buf_ptr[n] << 8) | ((msg_buf_ptr[n] >> 8) & 0xFF);
}
return msg_buf_ptr;
}
#endif
#endif
#ifndef NMBS_CLIENT_DISABLED
static void put_regs(nmbs_t* nmbs, const uint16_t* data, uint16_t n) {
uint16_t* msg_buf_ptr = (uint16_t*) (nmbs->msg.buf + nmbs->msg.buf_idx);
nmbs->msg.buf_idx += n * 2;
while (n--) {
msg_buf_ptr[n] = (data[n] << 8) | ((data[n] >> 8) & 0xFF);
}
}
#endif
static void swap_regs(uint16_t* data, uint16_t n) {
while (n--) {
data[n] = (data[n] << 8) | ((data[n] >> 8) & 0xFF);
}
}
static nmbs_error recv(nmbs_t* nmbs, uint16_t count) {
if (nmbs->msg.complete) {
return NMBS_ERROR_NONE;
}
int32_t ret =
nmbs->platform.read(nmbs->msg.buf + nmbs->msg.buf_idx, count, nmbs->byte_timeout_ms, nmbs->platform.arg);
if (ret == count)
return NMBS_ERROR_NONE;
if (ret < count) {
if (ret < 0)
return NMBS_ERROR_TRANSPORT;
return NMBS_ERROR_TIMEOUT;
}
return NMBS_ERROR_TRANSPORT;
}
static nmbs_error send(const nmbs_t* nmbs, uint16_t count) {
int32_t ret = nmbs->platform.write(nmbs->msg.buf, count, nmbs->byte_timeout_ms, nmbs->platform.arg);
if (ret == count)
return NMBS_ERROR_NONE;
if (ret < count) {
if (ret < 0)
return NMBS_ERROR_TRANSPORT;
return NMBS_ERROR_TIMEOUT;
}
return NMBS_ERROR_TRANSPORT;
}
static void flush(nmbs_t* nmbs) {
nmbs->platform.read(nmbs->msg.buf, sizeof(nmbs->msg.buf), 0, nmbs->platform.arg);
}
static void msg_buf_reset(nmbs_t* nmbs) {
nmbs->msg.buf_idx = 0;
}
static void msg_state_reset(nmbs_t* nmbs) {
msg_buf_reset(nmbs);
nmbs->msg.unit_id = 0;
nmbs->msg.fc = 0;
nmbs->msg.transaction_id = 0;
nmbs->msg.broadcast = false;
nmbs->msg.ignored = false;
nmbs->msg.complete = false;
}
#ifndef NMBS_CLIENT_DISABLED
static void msg_state_req(nmbs_t* nmbs, uint8_t fc) {
if (nmbs->current_tid == UINT16_MAX)
nmbs->current_tid = 1;
else
nmbs->current_tid++;
// Flush the remaining data on the line before sending the request
flush(nmbs);
msg_state_reset(nmbs);
nmbs->msg.unit_id = nmbs->dest_address_rtu;
nmbs->msg.fc = fc;
nmbs->msg.transaction_id = nmbs->current_tid;
if (nmbs->msg.unit_id == 0 && nmbs->platform.transport == NMBS_TRANSPORT_RTU)
nmbs->msg.broadcast = true;
}
#endif
nmbs_error nmbs_create(nmbs_t* nmbs, const nmbs_platform_conf* platform_conf) {
if (!nmbs)
return NMBS_ERROR_INVALID_ARGUMENT;
memset(nmbs, 0, sizeof(nmbs_t));
nmbs->byte_timeout_ms = -1;
nmbs->read_timeout_ms = -1;
if (!platform_conf || platform_conf->initialized != 0xFFFFDEBE)
return NMBS_ERROR_INVALID_ARGUMENT;
if (platform_conf->transport != NMBS_TRANSPORT_RTU && platform_conf->transport != NMBS_TRANSPORT_TCP)
return NMBS_ERROR_INVALID_ARGUMENT;
if (!platform_conf->read || !platform_conf->write)
return NMBS_ERROR_INVALID_ARGUMENT;
nmbs->platform = *platform_conf;
return NMBS_ERROR_NONE;
}
void nmbs_set_read_timeout(nmbs_t* nmbs, int32_t timeout_ms) {
nmbs->read_timeout_ms = timeout_ms;
}
void nmbs_set_byte_timeout(nmbs_t* nmbs, int32_t timeout_ms) {
nmbs->byte_timeout_ms = timeout_ms;
}
void nmbs_platform_conf_create(nmbs_platform_conf* platform_conf) {
memset(platform_conf, 0, sizeof(nmbs_platform_conf));
platform_conf->crc_calc = nmbs_crc_calc;
// Workaround for older user code not calling nmbs_platform_conf_create()
platform_conf->initialized = 0xFFFFDEBE;
}
void nmbs_set_destination_rtu_address(nmbs_t* nmbs, uint8_t address) {
nmbs->dest_address_rtu = address;
}
void nmbs_set_platform_arg(nmbs_t* nmbs, void* arg) {
nmbs->platform.arg = arg;
}
uint16_t nmbs_crc_calc(const uint8_t* data, uint32_t length, void* arg) {
NMBS_UNUSED_PARAM(arg);
uint16_t crc = 0xFFFF;
for (uint32_t i = 0; i < length; i++) {
crc ^= (uint16_t) data[i];
for (int j = 8; j != 0; j--) {
if ((crc & 0x0001) != 0) {
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
return (uint16_t) (crc << 8) | (uint16_t) (crc >> 8);
}
static nmbs_error recv_msg_footer(nmbs_t* nmbs) {
NMBS_DEBUG_PRINT("\n");
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
uint16_t crc = nmbs->platform.crc_calc(nmbs->msg.buf, nmbs->msg.buf_idx, nmbs->platform.arg);
nmbs_error err = recv(nmbs, 2);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t recv_crc = get_2(nmbs);
if (recv_crc != crc)
return NMBS_ERROR_CRC;
}
return NMBS_ERROR_NONE;
}
static nmbs_error recv_msg_header(nmbs_t* nmbs, bool* first_byte_received) {
// We wait for the read timeout here, just for the first message byte
int32_t old_byte_timeout = nmbs->byte_timeout_ms;
nmbs->byte_timeout_ms = nmbs->read_timeout_ms;
msg_state_reset(nmbs);
*first_byte_received = false;
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
nmbs_error err = recv(nmbs, 1);
nmbs->byte_timeout_ms = old_byte_timeout;
if (err != NMBS_ERROR_NONE)
return err;
*first_byte_received = true;
nmbs->msg.unit_id = get_1(nmbs);
err = recv(nmbs, 1);
if (err != NMBS_ERROR_NONE)
return err;
nmbs->msg.fc = get_1(nmbs);
}
else if (nmbs->platform.transport == NMBS_TRANSPORT_TCP) {
nmbs_error err = recv(nmbs, 1);
nmbs->byte_timeout_ms = old_byte_timeout;
if (err != NMBS_ERROR_NONE)
return err;
*first_byte_received = true;
// Advance buf_idx
discard_1(nmbs);
err = recv(nmbs, 7);
if (err != NMBS_ERROR_NONE)
return err;
// Starting over
msg_buf_reset(nmbs);
nmbs->msg.transaction_id = get_2(nmbs);
uint16_t protocol_id = get_2(nmbs);
uint16_t length = get_2(nmbs);
nmbs->msg.unit_id = get_1(nmbs);
nmbs->msg.fc = get_1(nmbs);
if (length < 2 || length > 255)
return NMBS_ERROR_INVALID_TCP_MBAP;
// Receive the rest of the message
err = recv(nmbs, length - 2);
if (err != NMBS_ERROR_NONE)
return err;
if (protocol_id != 0)
return NMBS_ERROR_INVALID_TCP_MBAP;
nmbs->msg.complete = true;
}
return NMBS_ERROR_NONE;
}
static void put_msg_header(nmbs_t* nmbs, uint16_t data_length) {
msg_buf_reset(nmbs);
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
put_1(nmbs, nmbs->msg.unit_id);
}
else if (nmbs->platform.transport == NMBS_TRANSPORT_TCP) {
put_2(nmbs, nmbs->msg.transaction_id);
put_2(nmbs, 0);
put_2(nmbs, (uint16_t) (1 + 1 + data_length));
put_1(nmbs, nmbs->msg.unit_id);
}
put_1(nmbs, nmbs->msg.fc);
}
#ifndef NMBS_SERVER_DISABLED
#if !defined(NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED)
static void set_msg_header_size(nmbs_t* nmbs, uint16_t data_length) {
if (nmbs->platform.transport == NMBS_TRANSPORT_TCP) {
data_length += 2;
set_2(nmbs, data_length, 4);
}
}
#endif
#endif
static nmbs_error send_msg(nmbs_t* nmbs) {
NMBS_DEBUG_PRINT("\n");
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
uint16_t crc = nmbs->platform.crc_calc(nmbs->msg.buf, nmbs->msg.buf_idx, nmbs->platform.arg);
put_2(nmbs, crc);
}
nmbs_error err = send(nmbs, nmbs->msg.buf_idx);
return err;
}
#ifndef NMBS_SERVER_DISABLED
static nmbs_error recv_req_header(nmbs_t* nmbs, bool* first_byte_received) {
const nmbs_error err = recv_msg_header(nmbs, first_byte_received);
if (err != NMBS_ERROR_NONE)
return err;
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
// Check if request is for us
if (nmbs->msg.unit_id == NMBS_BROADCAST_ADDRESS)
nmbs->msg.broadcast = true;
else if (nmbs->msg.unit_id != nmbs->address_rtu)
nmbs->msg.ignored = true;
else
nmbs->msg.ignored = false;
}
return NMBS_ERROR_NONE;
}
static void put_res_header(nmbs_t* nmbs, uint16_t data_length) {
put_msg_header(nmbs, data_length);
NMBS_DEBUG_PRINT("%d NMBS res -> address_rtu %d\tfc %d\t", nmbs->address_rtu, nmbs->address_rtu, nmbs->msg.fc);
}
static nmbs_error send_exception_msg(nmbs_t* nmbs, uint8_t exception) {
nmbs->msg.fc += 0x80;
put_msg_header(nmbs, 1);
put_1(nmbs, exception);
NMBS_DEBUG_PRINT("%d NMBS res -> address_rtu %d\texception %d", nmbs->address_rtu, nmbs->address_rtu, exception);
return send_msg(nmbs);
}
#endif
static nmbs_error recv_res_header(nmbs_t* nmbs) {
uint16_t req_transaction_id = nmbs->msg.transaction_id;
uint8_t req_unit_id = nmbs->msg.unit_id;
uint8_t req_fc = nmbs->msg.fc;
bool first_byte_received = false;
nmbs_error err = recv_msg_header(nmbs, &first_byte_received);
if (err != NMBS_ERROR_NONE)
return err;
if (nmbs->platform.transport == NMBS_TRANSPORT_TCP) {
if (nmbs->msg.transaction_id != req_transaction_id)
return NMBS_ERROR_INVALID_TCP_MBAP;
}
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU && nmbs->msg.unit_id != req_unit_id)
return NMBS_ERROR_INVALID_UNIT_ID;
if (nmbs->msg.fc != req_fc) {
if (nmbs->msg.fc - 0x80 == req_fc) {
err = recv(nmbs, 1);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t exception = get_1(nmbs);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (exception < 1 || exception > 4)
return NMBS_ERROR_INVALID_RESPONSE;
NMBS_DEBUG_PRINT("%d NMBS res <- address_rtu %d\texception %d\n", nmbs->address_rtu, nmbs->msg.unit_id,
exception);
return (nmbs_error) exception;
}
return NMBS_ERROR_INVALID_RESPONSE;
}
NMBS_DEBUG_PRINT("%d NMBS res <- address_rtu %d\tfc %d\t", nmbs->address_rtu, nmbs->msg.unit_id, nmbs->msg.fc);
return NMBS_ERROR_NONE;
}
#ifndef NMBS_CLIENT_DISABLED
static void put_req_header(nmbs_t* nmbs, uint16_t data_length) {
put_msg_header(nmbs, data_length);
#ifdef NMBS_DEBUG
printf("%d ", nmbs->address_rtu);
printf("NMBS req -> ");
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
if (nmbs->msg.broadcast)
printf("broadcast\t");
else
printf("address_rtu %d\t", nmbs->dest_address_rtu);
}
printf("fc %d\t", nmbs->msg.fc);
#endif
}
#endif
#if !defined(NMBS_CLIENT_DISABLED) || \
(!defined(NMBS_SERVER_DISABLED) && \
(!defined(NMBS_SERVER_READ_COILS_DISABLED) || !defined(NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED)))
static nmbs_error recv_read_discrete_res(nmbs_t* nmbs, nmbs_bitfield values) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 1);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t coils_bytes = get_1(nmbs);
NMBS_DEBUG_PRINT("b %d\t", coils_bytes);
if (coils_bytes > 250) {
return NMBS_ERROR_INVALID_RESPONSE;
}
err = recv(nmbs, coils_bytes);
if (err != NMBS_ERROR_NONE)
return err;
NMBS_DEBUG_PRINT("coils ");
for (int i = 0; i < coils_bytes; i++) {
uint8_t coil = get_1(nmbs);
if (values)
values[i] = coil;
NMBS_DEBUG_PRINT("%d ", coil);
}
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
return NMBS_ERROR_NONE;
}
#endif
#if !defined(NMBS_CLIENT_DISABLED) || \
(!defined(NMBS_SERVER_DISABLED) && (!defined(NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED) || \
!defined(NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED)))
static nmbs_error recv_read_registers_res(nmbs_t* nmbs, uint16_t quantity, uint16_t* registers) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 1);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t registers_bytes = get_1(nmbs);
NMBS_DEBUG_PRINT("b %d\t", registers_bytes);
if (registers_bytes > 250)
return NMBS_ERROR_INVALID_RESPONSE;
err = recv(nmbs, registers_bytes);
if (err != NMBS_ERROR_NONE)
return err;
NMBS_DEBUG_PRINT("regs ");
for (int i = 0; i < registers_bytes / 2; i++) {
uint16_t reg = get_2(nmbs);
if (registers)
registers[i] = reg;
NMBS_DEBUG_PRINT("%d ", reg);
}
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (registers_bytes != quantity * 2)
return NMBS_ERROR_INVALID_RESPONSE;
return NMBS_ERROR_NONE;
}
#endif
nmbs_error recv_write_single_coil_res(nmbs_t* nmbs, uint16_t address, uint16_t value_req) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t address_res = get_2(nmbs);
uint16_t value_res = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tvalue %d", address, value_res);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (address_res != address)
return NMBS_ERROR_INVALID_RESPONSE;
if (value_res != value_req)
return NMBS_ERROR_INVALID_RESPONSE;
return NMBS_ERROR_NONE;
}
nmbs_error recv_write_single_register_res(nmbs_t* nmbs, uint16_t address, uint16_t value_req) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t address_res = get_2(nmbs);
uint16_t value_res = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tvalue %d ", address, value_res);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (address_res != address)
return NMBS_ERROR_INVALID_RESPONSE;
if (value_res != value_req)
return NMBS_ERROR_INVALID_RESPONSE;
return NMBS_ERROR_NONE;
}
nmbs_error recv_write_multiple_coils_res(nmbs_t* nmbs, uint16_t address, uint16_t quantity) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t address_res = get_2(nmbs);
uint16_t quantity_res = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tq %d", address_res, quantity_res);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (address_res != address)
return NMBS_ERROR_INVALID_RESPONSE;
if (quantity_res != quantity)
return NMBS_ERROR_INVALID_RESPONSE;
return NMBS_ERROR_NONE;
}
nmbs_error recv_write_multiple_registers_res(nmbs_t* nmbs, uint16_t address, uint16_t quantity) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t address_res = get_2(nmbs);
uint16_t quantity_res = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tq %d", address_res, quantity_res);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (address_res != address)
return NMBS_ERROR_INVALID_RESPONSE;
if (quantity_res != quantity)
return NMBS_ERROR_INVALID_RESPONSE;
return NMBS_ERROR_NONE;
}
nmbs_error recv_read_file_record_res(nmbs_t* nmbs, uint16_t* registers, uint16_t count) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 1);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t response_size = get_1(nmbs);
if (response_size > 250) {
return NMBS_ERROR_INVALID_RESPONSE;
}
err = recv(nmbs, response_size);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t subreq_data_size = get_1(nmbs) - 1;
uint8_t subreq_reference_type = get_1(nmbs);
uint16_t* subreq_record_data = (uint16_t*) get_n(nmbs, subreq_data_size);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (registers) {
if (subreq_reference_type != 6)
return NMBS_ERROR_INVALID_RESPONSE;
if (count != (subreq_data_size / 2))
return NMBS_ERROR_INVALID_RESPONSE;
swap_regs(subreq_record_data, subreq_data_size / 2);
memcpy(registers, subreq_record_data, subreq_data_size);
}
return NMBS_ERROR_NONE;
}
nmbs_error recv_write_file_record_res(nmbs_t* nmbs, uint16_t file_number, uint16_t record_number,
const uint16_t* registers, uint16_t count) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 1);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t response_size = get_1(nmbs);
if (response_size > 251)
return NMBS_ERROR_INVALID_RESPONSE;
err = recv(nmbs, response_size);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t subreq_reference_type = get_1(nmbs);
uint16_t subreq_file_number = get_2(nmbs);
uint16_t subreq_record_number = get_2(nmbs);
uint16_t subreq_record_length = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tr %d\tl %d\t fwrite ", subreq_file_number, subreq_record_number, subreq_record_length);
uint16_t subreq_data_size = subreq_record_length * 2;
uint16_t* subreq_record_data = (uint16_t*) get_n(nmbs, subreq_data_size);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (registers) {
if (subreq_reference_type != 6)
return NMBS_ERROR_INVALID_RESPONSE;
if (subreq_file_number != file_number)
return NMBS_ERROR_INVALID_RESPONSE;
if (subreq_record_number != record_number)
return NMBS_ERROR_INVALID_RESPONSE;
if (subreq_record_length != count)
return NMBS_ERROR_INVALID_RESPONSE;
swap_regs(subreq_record_data, subreq_record_length);
if (memcmp(registers, subreq_record_data, subreq_data_size) != 0)
return NMBS_ERROR_INVALID_RESPONSE;
}
return NMBS_ERROR_NONE;
}
nmbs_error recv_read_device_identification_res(nmbs_t* nmbs, uint8_t buffers_count, char** buffers_out,
uint8_t buffers_length, const uint8_t* order, uint8_t* ids_out,
uint8_t* next_object_id_out, uint8_t* objects_count_out) {
nmbs_error err = recv_res_header(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
err = recv(nmbs, 6);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t mei_type = get_1(nmbs);
if (mei_type != 0x0E)
return NMBS_ERROR_INVALID_RESPONSE;
uint8_t read_device_id_code = get_1(nmbs);
if (read_device_id_code < 1 || read_device_id_code > 4)
return NMBS_ERROR_INVALID_RESPONSE;
uint8_t conformity_level = get_1(nmbs);
if (conformity_level < 1 || (conformity_level > 3 && conformity_level < 0x81) || conformity_level > 0x83)
return NMBS_ERROR_INVALID_RESPONSE;
uint8_t more_follows = get_1(nmbs);
if (more_follows != 0 && more_follows != 0xFF)
return NMBS_ERROR_INVALID_RESPONSE;
uint8_t next_object_id = get_1(nmbs);
uint8_t objects_count = get_1(nmbs);
if (objects_count_out)
*objects_count_out = objects_count;
if (buffers_count == 0) {
buffers_out = NULL;
}
else if (objects_count > buffers_count)
return NMBS_ERROR_INVALID_ARGUMENT;
if (more_follows == 0)
next_object_id = 0x7F; // This value is reserved in the spec, we use it to signal stream is finished
if (next_object_id_out)
*next_object_id_out = next_object_id;
uint8_t res_size_left = 253 - 7;
for (int i = 0; i < objects_count; i++) {
err = recv(nmbs, 2);
if (err != NMBS_ERROR_NONE)
return err;
uint8_t object_id = get_1(nmbs);
uint8_t object_length = get_1(nmbs);
res_size_left -= 2;
if (object_length > res_size_left)
return NMBS_ERROR_INVALID_RESPONSE;
err = recv(nmbs, object_length);
if (err != NMBS_ERROR_NONE)
return err;
const char* str = (const char*) get_n(nmbs, object_length);
if (ids_out)
ids_out[i] = object_id;
uint8_t buf_index = i;
if (order)
buf_index = order[object_id];
if (buffers_out) {
strncpy(buffers_out[buf_index], str, buffers_length);
buffers_out[buf_index][object_length] = 0;
}
}
return recv_msg_footer(nmbs);
}
#ifndef NMBS_SERVER_DISABLED
#if !defined(NMBS_SERVER_READ_COILS_DISABLED) || !defined(NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED)
static nmbs_error handle_read_discrete(nmbs_t* nmbs,
nmbs_error (*callback)(uint16_t, uint16_t, nmbs_bitfield, uint8_t, void*)) {
nmbs_error err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t address = get_2(nmbs);
uint16_t quantity = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tq %d", address, quantity);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (!nmbs->msg.ignored) {
if (quantity < 1 || quantity > 2000)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_VALUE);
if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS);
if (callback) {
nmbs_bitfield bitfield = {0};
err = callback(address, quantity, bitfield, nmbs->msg.unit_id, nmbs->callbacks.arg);
if (err != NMBS_ERROR_NONE) {
if (nmbs_error_is_exception(err))
return send_exception_msg(nmbs, err);
return send_exception_msg(nmbs, NMBS_EXCEPTION_SERVER_DEVICE_FAILURE);
}
if (!nmbs->msg.broadcast) {
uint8_t discrete_bytes = (quantity + 7) / 8;
put_res_header(nmbs, 1 + discrete_bytes);
put_1(nmbs, discrete_bytes);
NMBS_DEBUG_PRINT("b %d\t", discrete_bytes);
NMBS_DEBUG_PRINT("coils ");
for (int i = 0; i < discrete_bytes; i++) {
put_1(nmbs, bitfield[i]);
NMBS_DEBUG_PRINT("%d ", bitfield[i]);
}
err = send_msg(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
}
}
else {
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_FUNCTION);
}
}
else {
return recv_read_discrete_res(nmbs, NULL);
}
return NMBS_ERROR_NONE;
}
#endif
#if !defined(NMBS_SERVER_READ_HOLDING_REGISTERS_DISABLED) || !defined(NMBS_SERVER_READ_INPUT_REGISTERS_DISABLED)
static nmbs_error handle_read_registers(nmbs_t* nmbs,
nmbs_error (*callback)(uint16_t, uint16_t, uint16_t*, uint8_t, void*)) {
nmbs_error err = recv(nmbs, 4);
if (err != NMBS_ERROR_NONE)
return err;
uint16_t address = get_2(nmbs);
uint16_t quantity = get_2(nmbs);
NMBS_DEBUG_PRINT("a %d\tq %d", address, quantity);
err = recv_msg_footer(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
if (!nmbs->msg.ignored) {
if (quantity < 1 || quantity > 125)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_VALUE);
if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS);
if (callback) {
uint16_t regs[125] = {0};
err = callback(address, quantity, regs, nmbs->msg.unit_id, nmbs->callbacks.arg);
if (err != NMBS_ERROR_NONE) {
if (nmbs_error_is_exception(err))
return send_exception_msg(nmbs, err);
return send_exception_msg(nmbs, NMBS_EXCEPTION_SERVER_DEVICE_FAILURE);
}
// TODO check all these read request broadcast use cases
if (!nmbs->msg.broadcast) {
const uint8_t regs_bytes = quantity * 2;
put_res_header(nmbs, 1 + regs_bytes);