-
Notifications
You must be signed in to change notification settings - Fork 42
/
sii.c
2234 lines (1841 loc) · 52.2 KB
/
sii.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
/* sii.c
*
* 2013 Frank Jeschke <[email protected]>
*/
#include "sii.h"
#include "crc8.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define SKIP_DC 0x0001
#define SKIP_TXPDO 0x0010
#define SKIP_RXPDO 0x0020
/* category functions */
static struct _sii_cat *cat_new(uint16_t type, uint16_t size);
static void cat_data_cleanup(struct _sii_cat *cat);
static int cat_add(SiiInfo *sii, struct _sii_cat *new);
static int cat_rm(SiiInfo *sii);
static struct _sii_cat * cat_next(SiiInfo *sii);
static void cat_rewind(SiiInfo *sii);
static void cat_print(struct _sii_cat *cats);
static void cat_print_strings(struct _sii_cat *cat);
static void cat_print_datatypes(struct _sii_cat *cat);
static void cat_print_general(struct _sii_cat *cat);
static void cat_print_fmmu(struct _sii_cat *cat);
static void cat_print_syncm(struct _sii_cat *cat);
static void cat_print_rxpdo(struct _sii_cat *cat);
static void cat_print_txpdo(struct _sii_cat *cat);
static void cat_print_pdo(struct _sii_cat *cat);
static void cat_print_dc(struct _sii_cat *cat);
static uint16_t sii_cat_write_strings(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_datatypes(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_general(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_fmmu(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_syncm(struct _sii_cat *cat, unsigned char *buf);
//static void sii_cat_write_rxpdo(struct _sii_cat *cat);
//static void sii_cat_write_txpdo(struct _sii_cat *cat);
static uint16_t sii_cat_write_pdo(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_dc(struct _sii_cat *cat, unsigned char *buf);
#if DEBUG == 1
static uint16_t sii_cat_write_cat(struct _sii_cat *cat, unsigned char *buf);
#endif
static size_t sii_cat_write(struct _sii *sii, uint16_t skip_mask);
static void sii_write(SiiInfo *sii, unsigned int add_pdo_mapping, unsigned int add_dc_config);
static int read_eeprom(FILE *f, unsigned char *buffer, size_t size)
{
size_t count = 0;
int input;
while ((input=fgetc(f)) != EOF) {
if (count >= size) {
fprintf(stderr, "Error, buffer too small\n");
return -1;
}
buffer[count++] = (unsigned char)(input&0xff);
}
return count;
}
static struct _sii_preamble * parse_preamble(const unsigned char *buffer, size_t size)
{
struct _sii_preamble *preamble = calloc(1, sizeof(struct _sii_preamble));
size_t count = 0;
uint8_t crc = 0xff; /* init value for crc */
/* pdi control */
preamble->pdi_ctrl = ((int)(buffer[1]&0xff)<<8) | buffer[0];
crc8byte(&crc, buffer[0]);
crc8byte(&crc, buffer[1]);
/* pdi config */
preamble->pdi_conf = ((int)(buffer[3]&0xff)<<8) | buffer[2];
crc8byte(&crc, buffer[2]);
crc8byte(&crc, buffer[3]);
count = 4;
/* sync impulse len (multiples of 10ns), stored as nano seconds */
preamble->sync_impulse = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count+=2;
/* PDI config 2 */
preamble->pdi_conf2 = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count+=2;
/* configured station alias */
preamble->alias = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count+=2;
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
crc8byte(&crc, buffer[count+2]);
crc8byte(&crc, buffer[count+3]);
count+=4; /* next 4 bytes are reserved */
/* checksum test */
preamble->checksum = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count += 2;
if (size != count)
printf("%s: Warning counter differs from size\n", __func__);
preamble->checksum_ok = 1;
if (crc != 0) {
preamble->checksum_ok = 0;
fprintf(stderr, "Error, checksum is not correct!\n");
}
return preamble;
}
static struct _sii_stdconfig *parse_stdconfig(const unsigned char *buffer, size_t size)
{
size_t count =0;
const unsigned char *b = buffer;
struct _sii_stdconfig *stdc = calloc(1, sizeof(struct _sii_stdconfig));
stdc->vendor_id = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
stdc->product_id = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
stdc->revision_id = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
stdc->serial = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
b+=8; /* another reserved 8 bytes */
stdc->bs_rec_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->bs_rec_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->bs_snd_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->bs_snd_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_rec_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_rec_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_snd_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_snd_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->mailbox_protocol.word = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
b+=66; /* more reserved bytes */
stdc->eeprom_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->version = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
count = b-buffer;
if (size != count)
printf("%s: Warning counter differs from size\n", __func__);
return stdc;
}
static struct _string *string_new(const char *string, size_t size)
{
struct _string *new = calloc(1, sizeof(struct _string));
new->length = size;
new->data = malloc(size+1);
memmove(new->data, string, size);
new->data[size] = 0;
return new;
}
static void strings_entry_add(struct _sii_strings *str, struct _string *new)
{
if (str->head == NULL) { /* first entry */
str->head = new;
new->id = 1;
} else {
struct _string *s = str->head;
while (s->next)
s = s->next;
s->next = new;
new->prev = s;
new->id = s->id+1;
}
str->count += 1;
str->size += new->length;
}
static struct _sii_strings *parse_string_section(const unsigned char *buffer, size_t size)
{
const unsigned char *pos = buffer;
unsigned index = 0;
char str[1024];
size_t len = 0;
memset(str, '\0', 1024);
struct _sii_strings *strings = (struct _sii_strings *)calloc(1, sizeof(struct _sii_strings));
int stringcount = *pos++;
int counter = 0;
for (index=0; counter < stringcount; index++) {
len = *pos++;
memmove(str, pos, len);
pos += len;
strings_entry_add(strings, string_new(str, len));
counter++;
}
if ((size_t)(pos-buffer) > size)
printf("%s: Warning counter differs from size\n", __func__);
return strings;
}
static void parse_datatype_section(const unsigned char *buffer, size_t size)
{
printf("\n+++ parsing of datatype section not yet implemented (first byte: 0x%.2x, size: %zu)\n",
*buffer, size);
}
static char *physport_type(uint8_t b)
{
switch (b) {
case 0x00:
return "not used";
case 0x01:
return "MII";
case 0x02:
return "reserved";
case 0x03:
return "EBUS";
default:
return "unknown";
}
return NULL;
}
static struct _sii_general *parse_general_section(const unsigned char *buffer, size_t size)
{
const unsigned char *b = buffer;
struct _sii_general *siig = calloc(1, sizeof(struct _sii_general));
siig->groupindex = *b;
b++;
siig->imageindex = *b;
b++;
siig->orderindex = *b;
b++;
siig->nameindex = *b;
b++;
b++;
siig->coe_enable_sdo = (*b&0x01) == 0 ? 0 : 1;
siig->coe_enable_sdo_info = (*b&0x02) == 0 ? 0 : 1;
siig->coe_enable_pdo_assign = (*b&0x04) == 0 ? 0 : 1;
siig->coe_enable_pdo_conf = (*b&0x08) == 0 ? 0 : 1;
siig->coe_enable_upload_start = (*b&0x10) == 0 ? 0 : 1;
siig->coe_enable_sdo_complete = (*b&0x20) == 0 ? 0 : 1;
b++;
siig->foe_enabled = (*b & 0x01) == 0 ? 0 : 1;
b++;
siig->eoe_enabled = (*b & 0x01) == 0 ? 0 : 1;
b++;
b+=3; /* SoE Channels, DS402 Channels, Sysman Class - reserved */
siig->flag_safe_op = (*b & 0x01) == 0 ? 0 : 1;
siig->flag_notLRW = (*b & 0x02) == 0 ? 0 : 1;
siig->flag_MBoxDataLinkLayer = (*b & 0x04) == 0 ? 0 : 1;
siig->flag_IdentALSts = (*b & 0x08) == 0 ? 0 : 1;
siig->flag_IdentPhyM = (*b & 0x10) == 0 ? 0 : 1;
b++;
siig->current_ebus = BYTES_TO_WORD(*b, *(b+1));
b+=2;
b+=2;
siig->phys_port_0 = *b&0x0f;
siig->phys_port_1 = (*b>>4)&0xf;
b++;
siig->phys_port_2 = *b&0xf;
siig->phys_port_3 = (*b>>4)&0xf;
b++;
siig->physical_address = BYTES_TO_WORD(*b, *(b+1));
b+=2;
b+=12; /* reserved */
size_t count = b-buffer;
if (size != count)
printf("%s: Warning counter differs from size\n", __func__);
return siig;
}
static void fmmu_rm_entry(struct _sii_fmmu *fmmu)
{
struct _fmmu_entry *fe = fmmu->list;
while (fe->next != NULL)
fe = fe->next;
if (fe->prev != NULL)
fe->prev->next = NULL;
else
fmmu->list = NULL;
free(fe);
}
void fmmu_add_entry(struct _sii_fmmu *fmmu, int usage)
{
struct _fmmu_entry *new = calloc(1, sizeof(struct _fmmu_entry));
new->id = -1;
new->usage = usage;
if (usage == FMMU_UNUSED) { /* skip unused entries */
free(new);
return;
}
if (fmmu->list == NULL) {
new->id = 0;
fmmu->list = new;
fmmu->count = 1;
} else {
struct _fmmu_entry *f = fmmu->list;
while (f->next != NULL)
f = f->next;
f->next = new;
new->prev = f;
new->id = f->id+1;
fmmu->count += 1;
}
}
static struct _sii_fmmu *parse_fmmu_section(const unsigned char *buffer, size_t secsize)
{
const unsigned char *b = buffer;
struct _sii_fmmu *fmmu = calloc(1, sizeof(struct _sii_fmmu));
while ((unsigned int)(b-buffer)<secsize) {
fmmu_add_entry(fmmu, *b);
b++;
}
return fmmu;
}
static void syncm_rm_entry(struct _sii_syncm *sm)
{
struct _syncm_entry *entry = sm->list;
if (entry == NULL)
return;
while (entry->next != NULL)
entry = entry->next;
if (entry->prev != NULL)
entry->prev->next = NULL;
else
sm->list = NULL;
free(entry);
}
void syncm_entry_add(struct _sii_syncm *sm, struct _syncm_entry *entry)
{
if (sm->list == NULL) { /* first element */
entry->id = 0;
sm->list = entry;
sm->count++;
} else {
struct _syncm_entry *list = sm->list;
while (list->next != NULL)
list = list->next;
list->next = entry;
entry->id = list->id+1;
entry->prev = list;
entry->next = NULL;
sm->count++;
}
}
static void syncm_add_entry(struct _sii_syncm *sm,
int phys_address, int length, int control, int status, int enable, int type)
{
struct _syncm_entry *entry = calloc(1, sizeof(struct _syncm_entry));
entry->id = -1;
entry->phys_address = phys_address;
entry->length = length;
entry->control = control;
entry->status = status;
entry->enable = enable;
entry->type = type;
// FIXME substitude with syncm_entry_add()
if (sm->list == NULL) { /* first element */
entry->id = 0;
sm->list = entry;
sm->count++;
} else {
struct _syncm_entry *list = sm->list;
while (list->next != NULL)
list = list->next;
list->next = entry;
entry->id = list->id+1;
entry->prev = list;
entry->next = NULL;
sm->count++;
}
}
static struct _sii_syncm *parse_syncm_section(const unsigned char *buffer, size_t secsize)
{
size_t count=0;
int smnbr = 0;
const unsigned char *b = buffer;
struct _sii_syncm *sm = calloc(1, sizeof(struct _sii_syncm));
while (count<secsize) {
int physadr = BYTES_TO_WORD(*b, *(b+1));
b+=2;
int length = BYTES_TO_WORD(*b, *(b+1));
b+=2;
int control = *b;
b++;
int status = *b;
b++;
int enable = *b;
b++;
int type = *b;
b++;
syncm_add_entry(sm, physadr, length, control, status, enable, type);
count=(size_t)(b-buffer);
smnbr++;
}
return sm;
}
static void pdo_rm_entry(struct _sii_pdo *pdo)
{
struct _pdo_entry *pe = pdo->list;
if (pe == NULL)
return;
while (pe->next != NULL)
pe = pe->next;
if (pe->prev != NULL)
pe->prev->next = NULL;
if (pe->id == 0)
pdo->list = NULL;
free(pe);
}
void pdo_entry_add(struct _sii_pdo *pdo, struct _pdo_entry *entry)
{
if (pdo->list == NULL) {
entry->id = 0;
entry->next = NULL;
entry->prev = NULL;
pdo->list = entry;
} else {
struct _pdo_entry *list = pdo->list;
while (list->next != NULL)
list = list->next;
list->next = entry;
entry->id = list->id+1;
entry->prev = list;
entry->next = NULL;
}
}
static void pdo_add_entry(struct _sii_pdo *pdo,
int index, int subindex, int string_index, int data_type,
int bit_length, int flags)
{
struct _pdo_entry *entry = calloc(1, sizeof(struct _pdo_entry));
entry->id = -1;
entry->index = index;
entry->subindex = subindex;
entry->string_index = string_index;
entry->data_type = data_type;
entry->bit_length = bit_length;
entry->flags = flags;
/* FIXME make use of pdo_entry_add() */
if (pdo->list == NULL) {
entry->id = 0;
pdo->list = entry;
} else {
struct _pdo_entry *list = pdo->list;
while (list->next != NULL)
list = list->next;
list->next = entry;
entry->id = list->id+1;
entry->prev = list;
entry->next = NULL;
}
}
static struct _sii_pdo *parse_pdo_section(const unsigned char *buffer, size_t secsize, enum ePdoType t)
{
const unsigned char *b = buffer;
int entry = 0;
struct _sii_pdo *pdo = calloc(1, sizeof(struct _sii_pdo));
switch (t) {
case RxPDO:
pdo->type = SII_RX_PDO;
break;
case TxPDO:
pdo->type = SII_TX_PDO;
break;
default:
pdo->type = SII_UNDEF_PDO;
break;
}
pdo->index = BYTES_TO_WORD(*b, *(b+1));
b+=2;
pdo->entries = *b;
b++;
pdo->syncmanager = *b;
b++;
pdo->dcsync = *b;
b++;
pdo->name_index = *b;
b++;
pdo->flags = BYTES_TO_WORD(*b, *(b+1));
b+=2;
while ((unsigned int)(b-buffer)<secsize) {
int index = BYTES_TO_WORD(*b, *(b+1));
b+=2;
int subindex = *b;
b++;
int string_index = *b;
b++;
int data_type = *b;
b++;
int bit_length = *b;
b++;
int flags = BYTES_TO_WORD(*b, *(b+1));
b+=2;
pdo_add_entry(pdo, index, subindex, string_index, data_type, bit_length, flags);
entry++;
}
return pdo;
}
static enum eSection get_next_section(const unsigned char *b, size_t *secsize)
{
enum eSection next = SII_CAT_NOP;
next = BYTES_TO_WORD(*b, *(b+1)) & 0xffff; /* attention, on some section bit 7 is vendor specific indicator */
unsigned wordsize = BYTES_TO_WORD(*(b+2), *(b+3));
*secsize = (wordsize<<1); /* bytesize = 2*wordsize */
return next;
}
static struct _sii_dclock *parse_dclock_section(const unsigned char *buffer, size_t size)
{
const unsigned char *b = buffer;
struct _sii_dclock *dc = calloc(1, sizeof(struct _sii_dclock));
dc->cycleTime0 = BYTES_TO_DWORD(*b, *(b+1), *(b+2), *(b+3));
b+=4;
dc->shiftTime0 = BYTES_TO_DWORD(*b, *(b+1), *(b+2), *(b+3));
b+=4;
dc->shiftTime1 = BYTES_TO_DWORD(*b, *(b+1), *(b+2), *(b+3));
b+=4;
dc->sync1CycleFactor = BYTES_TO_WORD(*b, *(b+1));
b+=2;
dc->assignActivate = BYTES_TO_WORD(*b, *(b+1));
b+=2;
dc->sync0CycleFactor = BYTES_TO_WORD(*b, *(b+1));
b+=2;
dc->nameIdx = *b;
b++;
dc->descIdx = *b;
b += 5; // don't forget the 4 reserved bytes
size_t count = b-buffer;
if (size != count)
printf("%s: Warning counter differs from size\n", __func__);
return dc;
}
#if 0 // FIXME this could become usefull in the future.
static int g_print_offsets = 0;
static void print_offsets(const unsigned char *start, const unsigned char *current)
{
if (!g_print_offsets) {
printf("\n");
return;
}
printf("\n[Offset: 0x%0x (%zu)] ", (unsigned int)(current-start), current-start);
}
#endif
static int parse_content(struct _sii *sii, const unsigned char *eeprom, size_t maxsize)
{
enum eSection section = SII_PREAMBLE;
const size_t max_bytes_size = 2 * maxsize;
size_t secsize = 0;
const unsigned char *buffer = eeprom;
struct _sii_cat *newcat;
while (max_bytes_size > (size_t)(buffer - eeprom)) {
switch (section) {
case SII_PREAMBLE:
sii->preamble = parse_preamble(buffer, 16);
buffer = eeprom+16;
section = SII_STD_CONFIG;
break;
case SII_STD_CONFIG:
sii->config = parse_stdconfig(buffer, 46+66);
buffer = buffer+46+66;
section = get_next_section(buffer, &secsize);
buffer += 4;
break;
case SII_CAT_STRINGS:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_string_section(buffer, secsize);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added string section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_DATATYPES:
parse_datatype_section(buffer, secsize);
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_GENERAL:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_general_section(buffer, secsize);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added general section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_FMMU:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_fmmu_section(buffer, secsize);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added fmmu section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_SYNCM:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_syncm_section(buffer, secsize);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added syncm section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_TXPDO:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_pdo_section(buffer, secsize, TxPDO);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added txpdo section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_RXPDO:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_pdo_section(buffer, secsize, RxPDO);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added rxpdo section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_CAT_DCLOCK:
newcat = cat_new((uint16_t)(section&0xffff), (uint16_t)(secsize&0xffff));
newcat->data = (void *)parse_dclock_section(buffer, secsize);
cat_add(sii, newcat);
#if DEBUG == 1
printf("DEBUG Added dclock section\n");
#endif
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
case SII_END:
goto finish;
break;
case SII_CAT_NOP:
default:
fprintf(stderr, "[WARNING] Category 0x%.4x unknown, skipping ....\n", section);
buffer+=secsize;
section = get_next_section(buffer, &secsize);
buffer+=4;
break;
}
}
fprintf(stderr, "Error, SII probably malformed. No 0xffff at the end found\n");
return 1;
finish:
return 0;
}
/*** categroy list handling ***/
static void cat_data_cleanup_fmmu(struct _sii_fmmu *fmmu)
{
while (fmmu->list != NULL)
fmmu_rm_entry(fmmu);
free(fmmu);
}
static void cat_data_cleanup_syncm(struct _sii_syncm *syncm)
{
while (syncm->list != NULL)
syncm_rm_entry(syncm);
free(syncm);
}
static void cat_data_cleanup_pdo(struct _sii_pdo *pdo)
{
while (pdo->list != NULL)
pdo_rm_entry(pdo);
free(pdo);
}
static void cat_data_cleanup_strings(struct _sii_strings *str)
{
if (str == NULL)
return;
struct _string *current = str->head;
struct _string *tmp;
for (struct _string *s = current; s; ) {
free(s->data);
if (s->next)
s->next->prev = NULL;
tmp = s;
s = s->next;
free(tmp);
}
free(str);
}
static void cat_data_cleanup(struct _sii_cat *cat)
{
/* clean up type specific data */
switch (cat->type) {
case SII_CAT_STRINGS:
cat_data_cleanup_strings((struct _sii_strings *)cat->data);
break;
case SII_CAT_DATATYPES:
fprintf(stderr, "The Datatype categroie isn't implemented.\n");
break;
case SII_CAT_GENERAL:
free(cat->data); /* section general is simple */
break;
case SII_CAT_FMMU:
cat_data_cleanup_fmmu((struct _sii_fmmu *)cat->data);
break;
case SII_CAT_SYNCM:
cat_data_cleanup_syncm((struct _sii_syncm *)cat->data);
break;
case SII_CAT_TXPDO:
case SII_CAT_RXPDO:
cat_data_cleanup_pdo((struct _sii_pdo *)cat->data);
break;
case SII_CAT_DCLOCK:
free(cat->data); /* section dc is simple */
break;
default:
printf("Warning: cleanup received unknown category\n");
break;
}
}
#if 0
static struct _sii_cat *cat_new_data(uint16_t type, uint16_t size, void *data)
{
struct _sii_cat *new = calloc(1, sizeof(struct _sii_cat));
new->type = type&0x7fff;
new->vendor = (type>>16)&0x1;
new->size = size;
new->data = data;
return new;
}
#endif
static struct _sii_cat *cat_new(uint16_t type, uint16_t size)
{
struct _sii_cat *new = calloc(1, sizeof(struct _sii_cat));
new->type = type&0x7fff;
new->vendor = (type>>16)&0x1;
new->size = size;
return new;
}
/* add new element to the end of the list */
static int cat_add(SiiInfo *sii, struct _sii_cat *new)
{
struct _sii_cat *curr = sii->cat_head;
if (curr == NULL) {
sii->cat_head = new;
new->prev = NULL;
new->next = NULL;
return 0;
}
while (curr->next != NULL)
curr = curr->next;
curr->next = new;
new->prev = curr;
return 0;
}
/* removes the last category element */
static int cat_rm(SiiInfo *sii)
{
struct _sii_cat *curr = sii->cat_head;
if (curr == NULL)
return 1;
while (curr->next != NULL)
curr = curr->next;
if (curr->prev != NULL) {
curr->prev->next = NULL;
curr->prev = NULL;
}
cat_data_cleanup(curr);
curr->data = NULL;
curr->type = 0;
if (curr == sii->cat_head)
sii->cat_head = NULL;
free(curr);
return 0;
}
static struct _sii_cat * cat_next(SiiInfo *sii)
{
sii->cat_current = sii->cat_current->next;
return sii->cat_current;
}
static void cat_rewind(SiiInfo *sii)
{
sii->cat_current = sii->cat_head;
}
static const char *cat_name(enum eSection type)
{
const char *cat_string[] = {
"Unknown",
"Strings",
"Datatypes",
"Standard Config",
"General",
"FMMU", "SyncManager",
"TX PDO", "RX PDO",
"DC CLOCK"
};
unsigned int string_index = 0;
switch (type) {
case SII_CAT_STRINGS:
string_index = 1;
break;
case SII_CAT_DATATYPES:
string_index = 2;
break;
case SII_CAT_GENERAL:
string_index = 4;
break;
case SII_CAT_FMMU:
string_index = 5;
break;
case SII_CAT_SYNCM:
string_index = 6;
break;
case SII_CAT_TXPDO:
string_index = 7;