-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcic.c
1172 lines (870 loc) · 30.2 KB
/
xcic.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
/*
Copyright (c) 2020 Maxim Galaganov
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 <tarantool/module.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <small/small.h>
#include <small/static.h>
#include <small/ibuf.h>
#include <scom_property.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define XCIC_PORT_LUA_UDATA_NAME "__tnt_xcic_port"
LUA_API int luaopen_xcic(lua_State *L);
static int xcic_open_port(lua_State *L);
static int xcic_calc_checksum(lua_State *L);
static int xcic_pack_le32(lua_State *L);
static int xcic_unpack_le32(lua_State *L);
static int xcic_pack_le16(lua_State *L);
static int xcic_unpack_le16(lua_State *L);
static int xcic_pack_le_float(lua_State *L);
static int xcic_unpack_le_float(lua_State *L);
static int xcic_pack_bool(lua_State *L);
static int xcic_unpack_bool(lua_State *L);
static int xcic_pack_signal(lua_State *L);
static int xcic_unpack_software_version(lua_State *L);
static int xcic_port_close(lua_State *L);
static int xcic_port_usable(lua_State *L);
static int xcic_port_to_string(lua_State *L);
static int xcic_port_gc(lua_State *L);
static int xcic_port_read_user_info(lua_State *L);
static int xcic_port_read_parameter_property(lua_State *L);
static int xcic_port_write_parameter_property(lua_State *L);
static int xcic_port_read_message(lua_State *L);
static int xcic_port_read_datalog_dir(lua_State *L);
static int xcic_port_read_datalog_file(lua_State *L);
/** Xcom-232i serial port handle. */
struct xcic_port {
/** The file descriptor of the opened serial port. */
int fd;
/** Latch for mutual exclusion of DTE exchanges. */
box_latch_t *latch;
};
static int xcic_scom_read_property(lua_State *L, struct xcic_port *xp, struct ibuf *ibuf,
scom_property_t *property, const char *data, size_t data_len);
static int xcic_scom_write_property(lua_State *L, struct xcic_port *xp, struct ibuf *ibuf,
scom_property_t *property, const char *data, size_t data_len);
static int xcic_scom_xfer_datalog(lua_State *L, struct xcic_port *xp, uint32_t dst_addr,
uint32_t object_id, const char *data, size_t data_len,
struct ibuf *rbuf);
static int xcic_scom_port_exchange(lua_State *L, struct xcic_port *xp, struct ibuf *ibuf,
scom_frame_t *frame);
static int xcic_scom_encode_read_property(lua_State *L, struct ibuf *ibuf,
scom_property_t *property, const char *data,
size_t data_len);
static int xcic_scom_encode_write_property(lua_State *L, struct ibuf *ibuf,
scom_property_t *property, const char *data,
size_t data_len);
static int xcic_scom_encode_request_frame(lua_State *L, struct ibuf *ibuf, scom_frame_t *frame);
static int xcic_scom_decode_frame_header(lua_State *L, scom_frame_t *frame);
static int xcic_scom_decode_frame_data(lua_State *L, scom_frame_t *frame);
static int xcic_scom_decode_read_property(lua_State *L, scom_property_t *property);
static int xcic_scom_decode_write_property(lua_State *L, scom_property_t *property);
static uint16_t xcic_scom_calc_checksum(const char *data, uint_fast16_t length);
static void xcic_scom_dump_faulty_frame(struct ibuf *ibuf, scom_frame_t *frame);
static char *xcic_scom_strerror(scom_error_t error);
static ssize_t xcic_intl_port_read(struct xcic_port *xp, void *buf, size_t count);
static ssize_t xcic_intl_port_write(struct xcic_port *xp, void *buf, size_t count);
static void xcic_intl_port_close(struct xcic_port *xp);
static ssize_t xcic_intl_open_cb(va_list ap);
#define xcic_lua_except_to(label, L, ...) \
({ \
(void)lua_pushfstring(L, __VA_ARGS__); \
goto label; \
})
#define xcic_lua_except(L, ...) xcic_lua_except_to(except, L, __VA_ARGS__)
int xcic_open_port(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.open_port(pathname)");
struct xcic_port *xp = (struct xcic_port *)lua_newuserdata(L, sizeof(*xp));
memset(xp, 0, sizeof(*xp));
const char *pathname = lua_tostring(L, 1);
xp->fd = coio_call(xcic_intl_open_cb, pathname, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
if (xp->fd == -1)
xcic_lua_except(L, "open: %s", strerror(errno));
struct termios tty = {.c_cflag = CS8 | CLOCAL | CREAD | PARENB};
(void)cfsetospeed(&tty, B38400);
(void)cfsetispeed(&tty, B38400);
if (tcsetattr(xp->fd, TCSANOW, &tty) == -1)
xcic_lua_except(L, "tcsetattr: %s", strerror(errno));
xp->latch = box_latch_new();
luaL_getmetatable(L, XCIC_PORT_LUA_UDATA_NAME);
lua_setmetatable(L, -2);
return 1;
except:
xcic_intl_port_close(xp);
return lua_error(L);
}
int xcic_calc_checksum(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.calc_checksum(data)");
size_t data_len;
const char *data = lua_tolstring(L, 1, &data_len);
lua_pushinteger(L, xcic_scom_calc_checksum(data, (uint16_t)data_len));
return 1;
}
int xcic_port_close(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xp:close()");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
xcic_intl_port_close(xp);
return 0;
}
int xcic_port_usable(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xp:usable()");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
lua_pushboolean(L, xp->fd != -1);
return 1;
}
int xcic_port_to_string(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xp:tostring()");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
lua_pushfstring(L, "xcic_port: %p (%d)", xp, xp->fd);
return 1;
}
int xcic_port_gc(lua_State *L)
{
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
xcic_intl_port_close(xp);
box_latch_delete(xp->latch);
return 0;
}
int xcic_port_read_user_info(lua_State *L)
{
if (lua_gettop(L) < 3)
return luaL_error(L, "Usage: xp:read_user_info(dst_addr, object_id)");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
scom_frame_t frame;
scom_initialize_frame(&frame, NULL, 0);
frame.src_addr = 1;
frame.dst_addr = lua_tointeger(L, 2);
scom_property_t property;
scom_initialize_property(&property, &frame);
property.object_type = SCOM_USER_INFO_OBJECT_TYPE;
property.object_id = lua_tointeger(L, 3);
property.property_id = 1;
struct ibuf ibuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&ibuf, cord_slab_cache(), 32);
box_latch_lock(xp->latch);
int ret = xcic_scom_read_property(L, xp, &ibuf, &property, NULL, 0);
box_latch_unlock(xp->latch);
if (ret)
goto except;
lua_pushlstring(L, property.value_buffer, property.value_length);
return 1;
except:
return lua_error(L);
}
int xcic_port_read_parameter_property(lua_State *L)
{
if (lua_gettop(L) < 3)
return luaL_error(L, "Usage: xp:read_parameter_property(dst_addr, "
"object_id, property_id)");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
scom_frame_t frame;
scom_initialize_frame(&frame, NULL, 0);
frame.src_addr = 1;
frame.dst_addr = lua_tointeger(L, 2);
scom_property_t property;
scom_initialize_property(&property, &frame);
property.object_type = SCOM_PARAMETER_OBJECT_TYPE;
property.object_id = lua_tointeger(L, 3);
property.property_id = lua_tointeger(L, 4);
struct ibuf ibuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&ibuf, cord_slab_cache(), 32);
box_latch_lock(xp->latch);
int ret = xcic_scom_read_property(L, xp, &ibuf, &property, NULL, 0);
box_latch_unlock(xp->latch);
if (ret)
goto except;
lua_pushlstring(L, property.value_buffer, property.value_length);
return 1;
except:
return lua_error(L);
}
int xcic_scom_read_property(lua_State *L, struct xcic_port *xp, struct ibuf *ibuf,
scom_property_t *property, const char *data, size_t data_len)
{
uint32_t object_id = property->object_id;
if (xcic_scom_encode_read_property(L, ibuf, property, data, data_len))
goto except;
if (xcic_scom_encode_request_frame(L, ibuf, property->frame))
goto except;
if (xcic_scom_port_exchange(L, xp, ibuf, property->frame))
goto except;
if (xcic_scom_decode_frame_data(L, property->frame)) {
xcic_scom_dump_faulty_frame(ibuf, property->frame);
goto except;
}
scom_initialize_property(property, property->frame);
if (xcic_scom_decode_read_property(L, property))
goto except;
if (object_id != property->object_id)
xcic_lua_except(L, "mismatch on object_id `%d` != `%d`", property->object_id,
object_id);
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_port_write_parameter_property(lua_State *L)
{
if (lua_gettop(L) < 4)
return luaL_error(L, "Usage: xp:write_parameter_property(dst_addr, "
"object_id, property_id, data)");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
scom_frame_t frame;
scom_initialize_frame(&frame, NULL, 0);
frame.src_addr = 1;
frame.dst_addr = lua_tointeger(L, 2);
scom_property_t property;
scom_initialize_property(&property, &frame);
property.object_type = SCOM_PARAMETER_OBJECT_TYPE;
property.object_id = lua_tointeger(L, 3);
property.property_id = lua_tointeger(L, 4);
size_t data_len;
const char *data = lua_tolstring(L, 5, &data_len);
struct ibuf ibuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&ibuf, cord_slab_cache(), 32);
box_latch_lock(xp->latch);
int ret = xcic_scom_write_property(L, xp, &ibuf, &property, data, data_len);
box_latch_unlock(xp->latch);
if (ret)
goto except;
return 0;
except:
return lua_error(L);
}
int xcic_scom_write_property(lua_State *L, struct xcic_port *xp, struct ibuf *ibuf,
scom_property_t *property, const char *data, size_t data_len)
{
uint32_t object_id = property->object_id;
if (xcic_scom_encode_write_property(L, ibuf, property, data, data_len))
goto except;
if (xcic_scom_encode_request_frame(L, ibuf, property->frame))
goto except;
if (xcic_scom_port_exchange(L, xp, ibuf, property->frame))
goto except;
if (xcic_scom_decode_frame_data(L, property->frame)) {
xcic_scom_dump_faulty_frame(ibuf, property->frame);
goto except;
}
scom_initialize_property(property, property->frame);
if (xcic_scom_decode_write_property(L, property))
goto except;
if (object_id != property->object_id)
xcic_lua_except(L, "mismatch on object_id `%d` != `%d`", property->object_id,
object_id);
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_port_read_message(lua_State *L)
{
if (lua_gettop(L) < 3)
return luaL_error(L, "Usage: xp:read_message(dst_addr, object_id)");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
scom_frame_t frame;
scom_initialize_frame(&frame, NULL, 0);
frame.src_addr = 1;
frame.dst_addr = lua_tointeger(L, 2);
scom_property_t property;
scom_initialize_property(&property, &frame);
property.object_type = 3; // message
property.object_id = lua_tointeger(L, 3);
property.property_id = 0;
struct ibuf ibuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&ibuf, cord_slab_cache(), 32);
box_latch_lock(xp->latch);
int ret = xcic_scom_read_property(L, xp, &ibuf, &property, NULL, 0);
box_latch_unlock(xp->latch);
if (ret)
goto except;
if (property.value_length != 4 + 2 + 4 + 4 + 4)
xcic_lua_except(L, "invalid message data length %d", property.value_length);
lua_pushinteger(L, scom_read_le32(&property.value_buffer[0]));
lua_pushinteger(L, scom_read_le16(&property.value_buffer[4]));
lua_pushinteger(L, scom_read_le32(&property.value_buffer[6]));
lua_pushinteger(L, scom_read_le32(&property.value_buffer[10]));
lua_pushinteger(L, scom_read_le32(&property.value_buffer[14]));
return 5;
except:
return lua_error(L);
}
int xcic_port_read_datalog_dir(lua_State *L)
{
if (lua_gettop(L) < 2)
return luaL_error(L, "Usage: xp:read_datalog_dir(dst_addr)");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
uint32_t dst_addr = lua_tointeger(L, 2);
uint32_t object_id = 1; // directory list
struct ibuf rbuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&rbuf, cord_slab_cache(), 4096);
box_latch_lock(xp->latch);
int ret = xcic_scom_xfer_datalog(L, xp, dst_addr, object_id, NULL, 0, &rbuf);
box_latch_unlock(xp->latch);
if (ret)
goto except;
lua_pushlstring(L, rbuf.rpos, ibuf_used(&rbuf));
return 1;
except:
return lua_error(L);
}
int xcic_port_read_datalog_file(lua_State *L)
{
if (lua_gettop(L) < 3)
return luaL_error(L, "Usage: xp:read_datalog_file(dst_addr, filename)");
struct xcic_port *xp = (struct xcic_port *)luaL_checkudata(L, 1, XCIC_PORT_LUA_UDATA_NAME);
uint32_t dst_addr = lua_tointeger(L, 2);
uint32_t object_id = 2; // file access
size_t data_len;
const char *data = lua_tolstring(L, 3, &data_len);
struct ibuf rbuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&rbuf, cord_slab_cache(), 4096);
box_latch_lock(xp->latch);
int ret = xcic_scom_xfer_datalog(L, xp, dst_addr, object_id, data, data_len, &rbuf);
box_latch_unlock(xp->latch);
if (ret)
goto except;
lua_pushlstring(L, rbuf.rpos, ibuf_used(&rbuf));
return 1;
except:
return lua_error(L);
}
enum xcic_xfer_state {
XCIC_XFER_START = 0x21, /*SD_Start*/
XCIC_XFER_CONTINUE = 0x23, /*SD_Ack_Continue*/
XCIC_XFER_RETRY = 0x24, /*SD_Nack_Retry*/
XCIC_XFER_ABORT = 0x25, /*SD_Abort*/
};
int xcic_scom_xfer_datalog(lua_State *L, struct xcic_port *xp, uint32_t dst_addr,
uint32_t object_id, const char *data, size_t data_len, struct ibuf *rbuf)
{
struct ibuf ibuf __attribute__((cleanup(ibuf_destroy))) = {0};
ibuf_create(&ibuf, cord_slab_cache(), 256);
scom_frame_t frame;
scom_property_t property;
void *ptr;
enum xcic_xfer_state xfst = XCIC_XFER_START;
for (;;) {
ibuf_reset(&ibuf);
scom_initialize_frame(&frame, NULL, 0);
frame.src_addr = 1;
frame.dst_addr = dst_addr;
scom_initialize_property(&property, &frame);
property.object_type = 0x101; // datalog transfer
property.object_id = object_id;
property.property_id = (uint32_t)xfst;
say_info(" -> xfst 0x%x prop 0x%x data `%.*s`", xfst, property.property_id,
data_len, data);
if (xcic_scom_read_property(L, xp, &ibuf, &property, data, data_len))
switch (xfst) {
case XCIC_XFER_ABORT:
lua_pop(L, -1); // drop last error
goto except;
case XCIC_XFER_CONTINUE:
lua_pop(L, -1); // drop last error
xfst = XCIC_XFER_RETRY;
continue;
default:
goto abort; // error saved
}
if (xfst == XCIC_XFER_ABORT)
goto except;
say_info("<- xfst 0x%x prop 0x%x vl %d rl %d", xfst, property.property_id,
property.value_length, ibuf_used(rbuf));
if (property.value_length) {
ptr = ibuf_alloc(rbuf, property.value_length);
if (!ptr)
xcic_lua_except_to(abort, L, "alloc failed");
memcpy(ptr, property.value_buffer, property.value_length);
}
xfst = XCIC_XFER_CONTINUE;
if (property.property_id == 0x22 /*SD_Datablock*/)
continue;
else if (property.property_id == 0x26 /*SD_Finish*/)
break;
abort:
xfst = XCIC_XFER_ABORT;
}
return 0;
except:
return -1; // caller must invoke `lua_error'
}
int xcic_scom_port_exchange(lua_State *L, struct xcic_port *xp, struct ibuf *ibuf,
scom_frame_t *frame)
{
ssize_t nb;
uint32_t dst_addr = frame->dst_addr;
nb = xcic_intl_port_write(xp, frame->buffer, scom_frame_length(frame));
if (nb != (ssize_t)scom_frame_length(frame))
xcic_lua_except(L, "error when writing to the com port");
ibuf_reset(ibuf);
if (!ibuf_alloc(ibuf, SCOM_FRAME_HEADER_SIZE))
xcic_lua_except(L, "alloc failed");
scom_initialize_frame(frame, ibuf->rpos, ibuf_used(ibuf));
nb = xcic_intl_port_read(xp, frame->buffer, SCOM_FRAME_HEADER_SIZE);
if (nb != SCOM_FRAME_HEADER_SIZE)
xcic_lua_except(L, "error when reading the header from the com port");
/* scom_frame_length() is incorrect as `frame->data_length` is still
* empty */
ssize_t rlen = scom_read_le16(&frame->buffer[10]) + 2;
if (!ibuf_alloc(ibuf, rlen))
xcic_lua_except(L, "alloc failed");
frame->buffer = ibuf->rpos;
frame->buffer_size = ibuf_used(ibuf);
if (xcic_scom_decode_frame_header(L, frame))
goto except;
nb = xcic_intl_port_read(xp, &frame->buffer[SCOM_FRAME_HEADER_SIZE], rlen);
if (nb != rlen)
xcic_lua_except(L, "error when reading the data from the com port");
if (dst_addr != frame->src_addr)
xcic_lua_except(L, "mismatch on address `%d` != `%d`", dst_addr, frame->dst_addr);
return 0;
except:
xcic_intl_port_close(xp);
return -1; // caller must invoke `lua_error`
}
uint16_t xcic_scom_calc_checksum(const char *data, uint_fast16_t length)
{
uint_fast8_t A = 0xFF, B = 0;
while (length--) {
A = (A + *data++) & 0xFF;
B = (B + A) & 0xFF;
}
return (B & 0xFF) << 8 | (A & 0xFF);
}
void xcic_scom_dump_faulty_frame(struct ibuf *ibuf, scom_frame_t *frame)
{
size_t len = scom_frame_length(frame);
char *esc = (char *)static_reserve(len * 4 + 1);
if (esc) {
char *ptr = esc;
for (size_t i = 0; i < scom_frame_length(frame); i++)
ptr += sprintf(ptr, "\\%d", (uint8_t)frame->buffer[i]);
*ptr = '\0';
}
uint16_t cs =
xcic_scom_calc_checksum(&frame->buffer[SCOM_FRAME_HEADER_SIZE], frame->data_length);
say_info("xcic: faulty frame len %d iu %d cs %d esc %s", len, ibuf_used(ibuf), cs,
esc ?: "-");
}
int xcic_scom_encode_read_property(lua_State *L, struct ibuf *ibuf, scom_property_t *property,
const char *data, size_t data_len)
{
size_t offset = SCOM_FRAME_HEADER_SIZE + 2 + 8;
if (!ibuf_alloc(ibuf, offset + data_len))
xcic_lua_except(L, "alloc failed");
memcpy(ibuf->rpos + offset, data, data_len);
property->frame->buffer = ibuf->rpos;
property->frame->buffer_size = ibuf_used(ibuf);
property->value_length = data_len;
scom_encode_read_property(property);
/* scom_encode_read_property() resets `property->value_length` */
property->value_length = data_len;
property->frame->data_length += property->value_length;
if (property->frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "read property frame encoding failed with error %d (%s)",
property->frame->last_error,
xcic_scom_strerror(property->frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_scom_encode_write_property(lua_State *L, struct ibuf *ibuf, scom_property_t *property,
const char *data, size_t data_len)
{
size_t offset = SCOM_FRAME_HEADER_SIZE + 2 + 8;
if (!ibuf_alloc(ibuf, offset + data_len))
xcic_lua_except(L, "alloc failed");
memcpy(ibuf->rpos + offset, data, data_len);
property->frame->buffer = ibuf->rpos;
property->frame->buffer_size = ibuf_used(ibuf);
property->value_length = data_len;
scom_encode_write_property(property);
if (property->frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "write property frame encoding failed with error %d (%s)",
property->frame->last_error,
xcic_scom_strerror(property->frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_scom_encode_request_frame(lua_State *L, struct ibuf *ibuf, scom_frame_t *frame)
{
if (!ibuf_alloc(ibuf, scom_frame_length(frame) - frame->buffer_size))
xcic_lua_except(L, "alloc failed");
frame->buffer = ibuf->rpos;
frame->buffer_size = ibuf_used(ibuf);
scom_encode_request_frame(frame);
if (frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "data link frame encoding failed with error %d (%s)",
frame->last_error, xcic_scom_strerror(frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_scom_decode_frame_header(lua_State *L, scom_frame_t *frame)
{
scom_decode_frame_header(frame);
if (frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "data link header decoding failed with error %d (%s)",
frame->last_error, xcic_scom_strerror(frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_scom_decode_frame_data(lua_State *L, scom_frame_t *frame)
{
scom_decode_frame_data(frame);
if (frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "data link data decoding failed with error %d (%s)",
frame->last_error, xcic_scom_strerror(frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_scom_decode_read_property(lua_State *L, scom_property_t *property)
{
scom_decode_read_property(property);
if (property->frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "read property decoding failed with error %d (%s)",
property->frame->last_error,
xcic_scom_strerror(property->frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
int xcic_scom_decode_write_property(lua_State *L, scom_property_t *property)
{
scom_decode_write_property(property);
if (property->frame->last_error != SCOM_ERROR_NO_ERROR)
xcic_lua_except(L, "write property decoding failed with error %d (%s)",
property->frame->last_error,
xcic_scom_strerror(property->frame->last_error));
return 0;
except:
return -1; // caller must invoke `lua_error`
}
char *xcic_scom_strerror(scom_error_t error)
{
switch (error) {
case SCOM_ERROR_NO_ERROR:
return "no_error";
case SCOM_ERROR_INVALID_FRAME:
return "invalid_frame";
case SCOM_ERROR_DEVICE_NOT_FOUND:
return "device_not_found";
case SCOM_ERROR_RESPONSE_TIMEOUT:
return "response_timeout";
case SCOM_ERROR_SERVICE_NOT_SUPPORTED:
return "service_not_supported";
case SCOM_ERROR_INVALID_SERVICE_ARGUMENT:
return "invalid_service_argument";
case SCOM_ERROR_GATEWAY_BUSY:
return "gateway_busy";
case SCOM_ERROR_TYPE_NOT_SUPPORTED:
return "type_not_supported";
case SCOM_ERROR_OBJECT_ID_NOT_FOUND:
return "object_id_not_found";
case SCOM_ERROR_PROPERTY_NOT_SUPPORTED:
return "property_not_supported";
case SCOM_ERROR_INVALID_DATA_LENGTH:
return "invalid_data_length";
case SCOM_ERROR_PROPERTY_IS_READ_ONLY:
return "property_is_read_only";
case SCOM_ERROR_INVALID_DATA:
return "invalid_data";
case SCOM_ERROR_DATA_TOO_SMALL:
return "data_too_small";
case SCOM_ERROR_DATA_TOO_BIG:
return "data_too_big";
case SCOM_ERROR_WRITE_PROPERTY_FAILED:
return "write_property_failed";
case SCOM_ERROR_READ_PROPERTY_FAILED:
return "read_property_failed";
case SCOM_ERROR_ACCESS_DENIED:
return "access_denied";
case SCOM_ERROR_OBJECT_NOT_SUPPORTED:
return "object_not_supported";
case SCOM_ERROR_MULTICAST_READ_NOT_SUPPORTED:
return "multicast_read_not_supported";
case SCOM_ERROR_INVALID_SHELL_ARG:
return "invalid_shell_arg";
case SCOM_ERROR_STACK_PORT_NOT_FOUND:
return "stack_port_not_found";
case SCOM_ERROR_STACK_PORT_INIT_FAILED:
return "stack_port_init_failed";
case SCOM_ERROR_STACK_PORT_WRITE_FAILED:
return "stack_port_write_failed";
case SCOM_ERROR_STACK_PORT_READ_FAILED:
return "stack_port_read_failed";
case SCOM_ERROR_STACK_BUFFER_TOO_SMALL:
return "stack_buffer_too_small";
case SCOM_ERROR_STACK_PROPERTY_HEADER_DOESNT_MATCH:
return "stack_property_header_doesnt_match";
default:
return "unknown";
}
}
int xcic_pack_le32(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.pack_le32(val)");
uint32_t val;
scom_write_le32((char *const) & val, (uint32_t)lua_tointeger(L, 1));
lua_pushlstring(L, (const char *)&val, 4);
return 1;
}
int xcic_unpack_le32(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.unpack_le32(data)");
size_t data_len;
const char *data = lua_tolstring(L, 1, &data_len);
if (data_len != 4)
xcic_lua_except(L, "invalid le32 length %d", data_len);
lua_pushinteger(L, scom_read_le32(data));
return 1;
except:
return lua_error(L);
}
int xcic_pack_le16(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.pack_le16(val)");
uint16_t val;
scom_write_le16((char *const) & val, (uint16_t)lua_tointeger(L, 1));
lua_pushlstring(L, (const char *)&val, 2);
return 1;
}
int xcic_unpack_le16(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.unpack_le16(data)");
size_t data_len;
const char *data = lua_tolstring(L, 1, &data_len);
if (data_len != 2)
xcic_lua_except(L, "invalid le16 length %d", data_len);
lua_pushinteger(L, scom_read_le16(data));
return 1;
except:
return lua_error(L);
}
int xcic_pack_le_float(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.pack_le_float(val)");
uint32_t val;
scom_write_le_float((char *const) & val, (float)lua_tonumber(L, 1));
lua_pushlstring(L, (const char *)&val, 4);
return 1;
}
int xcic_unpack_le_float(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.unpack_le_float(data)");
size_t data_len;
const char *data = lua_tolstring(L, 1, &data_len);
if (data_len != 4)
xcic_lua_except(L, "invalid le float length %d", data_len);
lua_pushnumber(L, (lua_Number)scom_read_le_float(data));
return 1;
except:
return lua_error(L);
}
int xcic_pack_bool(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.pack_bool(val)");
uint8_t val = (uint8_t)lua_toboolean(L, 1);
lua_pushlstring(L, (const char *)&val, 1);
return 1;
}
int xcic_unpack_bool(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.unpack_bool(data)");
size_t data_len;
const char *data = lua_tolstring(L, 1, &data_len);
if (data_len != 1)
xcic_lua_except(L, "invalid bool length %d", data_len);
lua_pushboolean(L, *data);
return 1;
except:
return lua_error(L);
}
int xcic_pack_signal(lua_State *L)
{
uint32_t val = 1;
lua_pushlstring(L, (const char *)&val, 4);
return 1;
}
int xcic_unpack_software_version(lua_State *L)
{
if (lua_gettop(L) < 1)
return luaL_error(L, "Usage: xcic.unpack_software_version(msb_data, lsb_data)");
size_t msb_data_len;