forked from synapticon/siitool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esi.c
1047 lines (848 loc) · 28.5 KB
/
esi.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
/* ESI - EtherCAT Slave Information
*/
#include "esi.h"
#include "esifile.h"
#include "sii.h"
#include "crc8.h"
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#define Char2xmlChar(s) ((xmlChar *)s)
#define MAX_BOOTSTRAP_STRING 18
struct _esi_data {
SiiInfo *sii;
char *siifile; /* also opt for sii->outfile */
xmlDocPtr doc; /* do I need both? */
xmlNode *xmlroot;
char *xmlfile;
};
static char *type2str(int type)
{
switch (type) {
case XML_ELEMENT_NODE:
return "XML_ELEMENT_NODE";
case XML_ATTRIBUTE_NODE:
return "XML_ATTRIBUTE_NODE";
case XML_TEXT_NODE:
return "XML_TEXT_NODE";
case XML_CDATA_SECTION_NODE:
return "XML_CDATA_SECTION_NODE";
case XML_ENTITY_REF_NODE:
return "XML_ENTITY_REF_NODE";
case XML_ENTITY_NODE:
return "XML_ENTITY_NODE";
case XML_PI_NODE:
return "XML_PI_NODE";
case XML_COMMENT_NODE:
return "XML_COMMENT_NODE";
case XML_DOCUMENT_NODE:
return "XML_DOCUMENT_NODE";
case XML_DOCUMENT_TYPE_NODE:
return "XML_DOCUMENT_TYPE_NODE";
case XML_DOCUMENT_FRAG_NODE:
return "XML_DOCUMENT_FRAG_NODE";
case XML_NOTATION_NODE:
return "XML_NOTATION_NODE";
case XML_HTML_DOCUMENT_NODE:
return "XML_HTML_DOCUMENT_NODE";
case XML_DTD_NODE:
return "XML_DTD_NODE";
case XML_ELEMENT_DECL:
return "XML_ELEMENT_DECL";
case XML_ATTRIBUTE_DECL:
return "XML_ATTRIBUTE_DECL";
case XML_ENTITY_DECL:
return "XML_ENTITY_DECL";
case XML_NAMESPACE_DECL:
return "XML_NAMESPACE_DECL";
case XML_XINCLUDE_START:
return "XML_XINCLUDE_START";
case XML_XINCLUDE_END:
return "XML_XINCLUDE_END";
case XML_DOCB_DOCUMENT_NODE:
return "XML_DOCB_DOCUMENT_NODE";
}
return "empty";
}
#if DEBUG == 1
static void print_node(xmlNode *node)
{
if ((node->type == XML_TEXT_NODE) && (0x0a == *(node->content)))
return; // skip line feed node
printf("%d: type: %s name = %s, content = '%s'\n",
node->line, type2str(node->type), node->name, node->content);
}
#endif
static void print_all_nodes(xmlNode *root)
{
//xmlNode *node = root;
for (xmlNode *current = root; current; current = current->next) {
if (strncmp((const char *)current->name, "Profile", 7) == 0) /* skip profile sektion */
continue;
#if 0
printf("%d: note type: %s name: %s\n", current->line, type2str(current->type), current->name);
#else
if (current->type == XML_ELEMENT_NODE)
printf("%d: type: %s name = %s, content = '%s'\n",
current->line, type2str(current->type), current->name, current->content);
if (current->type == XML_TEXT_NODE)
printf("%d: type: %s name: %s content: %s\n",
current->line, type2str(current->type), current->name, current->content);
if (current->type == XML_COMMENT_NODE)
continue;
#endif
print_all_nodes(current->children);
}
}
#if DEBUG == 1
static void parse_example(xmlNode *root)
{
for (xmlNode *current = root; current; current = current->next) {
/*
printf("%d: note type: %s name: %s content: '%s'\n",
current->line, type2str(current->type), current->name, current->content);
*/
if (current->type == XML_ELEMENT_NODE && strncmp((const char *)current->name, "Device", 6) == 0) {
printf("Device found, now printing\n");
print_all_nodes(current);
printf("Finished\n");
return;
}
parse_example(current->children);
}
}
#endif
static int parse_boolean(xmlChar *boolean)
{
if (xmlStrcmp(boolean, Char2xmlChar("true")) == 0)
return 1;
return 0;
}
static uint16_t preamble_crc8(struct _sii_preamble *pa)
{
if (pa == NULL)
return 0;
uint8_t crc = 0xff;
crc8byte(&crc, pa->pdi_ctrl&0xff);
crc8byte(&crc, (pa->pdi_ctrl>>8)&0xff);
crc8byte(&crc, pa->pdi_conf&0xff);
crc8byte(&crc, (pa->pdi_conf>>8)&0xff);
crc8byte(&crc, pa->sync_impulse&0xff);
crc8byte(&crc, (pa->sync_impulse>>8)&0xff);
crc8byte(&crc, pa->pdi_conf2&0xff);
crc8byte(&crc, (pa->pdi_conf2>>8)&0xff);
crc8byte(&crc, pa->alias&0xff);
crc8byte(&crc, (pa->alias>>8)&0xff);
crc8byte(&crc, pa->reserved[0]&0xff);
crc8byte(&crc, pa->reserved[1]&0xff);
crc8byte(&crc, pa->reserved[2]&0xff);
crc8byte(&crc, pa->reserved[3]&0xff);
pa->checksum = crc;
pa->checksum_ok = 1;
return pa->checksum;
}
/* Searches the first occurence of node named 'name' */
static xmlNode *search_node(xmlNode *root, const char *name)
{
xmlNode *tmp = NULL;
for (xmlNode *curr = root; curr; curr = curr->next) {
if (curr->type == XML_ELEMENT_NODE &&
strncmp((const char *)curr->name, name, strlen((const char *)curr->name)) == 0)
return curr;
tmp = search_node(curr->children, name);
if (tmp != NULL)
return tmp;
}
return NULL;
}
static xmlNode *search_node_bfs(xmlNode *root, const char *name)
{
xmlNode *tmp = NULL;
for (xmlNode *curr = root; curr != NULL; curr = curr->next) {
if (curr->type == XML_ELEMENT_NODE &&
strncmp((const char *)curr->name, name, strlen((const char *)curr->name)) == 0) {
return curr;
}
}
for (xmlNode *curr = root; curr != NULL; curr = curr->next) {
tmp = search_node_bfs(curr->children, name);
if (tmp != NULL) {
return tmp;
}
}
return NULL;
}
/* TODO: Add function to search for all nodes named by 'name' (e.g. multiple <Sm>-Tags */
/* functions to parse xml */
static struct _sii_preamble *parse_preamble(xmlNode *node)
{
struct _sii_preamble *pa = calloc(1, sizeof(struct _sii_preamble));
char string[1024];
strncpy(string, (char *)node->children->content, 1024);
unsigned int lowbyte=0, highbyte=0;
char *b = string;
if (sscanf(b, "%2x%2x", &highbyte, &lowbyte) >= 2) {
pa->pdi_ctrl = BYTES_TO_WORD(highbyte, lowbyte);
b+=4;
}
if (sscanf(b, "%2x%2x", &highbyte, &lowbyte) >= 2) {
pa->pdi_conf = BYTES_TO_WORD(highbyte, lowbyte);
b+=4;
}
if (sscanf(b, "%2x%2x", &highbyte, &lowbyte) >= 2) {
pa->sync_impulse = BYTES_TO_WORD(highbyte, lowbyte);
b+=4;
}
if (sscanf(b, "%2x%2x", &highbyte, &lowbyte) >= 2) {
pa->pdi_conf2 = BYTES_TO_WORD(highbyte, lowbyte);
b+=4;
}
if (sscanf(b, "%2x%2x", &highbyte, &lowbyte) >= 2) {
pa->alias = BYTES_TO_WORD(highbyte, lowbyte);
//b+=4;
}
for (int i=0; i<4; i++)
pa->reserved[i] = 0x00;
pa->checksum = 0xff; /* set initial checksum */
pa->checksum = preamble_crc8(pa);
return pa;
}
static struct _sii_stdconfig *parse_config(xmlNode *root)
{
xmlNode *n, *tmp;
//xmlNode *n = search_node(root, "Vendor");
n = search_node(root, "Vendor");
if (n==NULL) {
return NULL;
}
struct _sii_stdconfig *sc = calloc(1, sizeof(struct _sii_stdconfig));
tmp = search_node(n, "Id");
//char *vendoridstr = tmp->children->content;
/* get id */
// FIXME add some error and validty checking, esp. if node is set correctly and has the type
sscanf((const char *)tmp->children->content, "#x%x", &(sc->vendor_id));
n = search_node(search_node(root, "Devices"), "Device");
tmp = search_node(n, "Type");
xmlAttr *prop = tmp->properties;
while (prop != NULL) {
if (xmlStrncmp(prop->name, Char2xmlChar("ProductCode"), xmlStrlen(prop->name)) == 0) {
sscanf((const char *)prop->children->content, "#x%x", &(sc->product_id));
}
if (xmlStrncmp(prop->name, Char2xmlChar("RevisionNo"), xmlStrlen(prop->name)) == 0) {
sscanf((const char *)prop->children->content, "#x%x", &(sc->revision_id));
}
prop = prop->next;
}
sc->serial = 0; /* FIXME the serial number is not in the esi? */
sc->bs_rec_mbox_offset = 0;
sc->bs_rec_mbox_size = 0;
sc->bs_snd_mbox_offset = 0;
sc->bs_snd_mbox_size = 0;
sc->std_rec_mbox_offset = 0x1000;
sc->std_rec_mbox_size = 128;
sc->std_snd_mbox_offset = 0x1080;
sc->std_snd_mbox_size = 128;
/* filter the std mailbox configuration; from <Sm> description */
tmp = n->children;
while (tmp != NULL) {
if (xmlStrncmp(tmp->name, Char2xmlChar("Sm"), xmlStrlen(tmp->name)) == 0) {
xmlNode *smchild = tmp->children;
while (smchild != NULL) {
if (xmlStrncmp(smchild->content, Char2xmlChar("MBoxOut"), xmlStrlen(smchild->content))== 0) {
xmlAttr *p = tmp->properties;
while (p!=NULL) {
if (xmlStrncmp(p->name, Char2xmlChar("DefaultSize"), xmlStrlen(p->name)) == 0) {
sc->std_rec_mbox_size = (uint16_t)atoi((const char *)p->children->content);
}
if (xmlStrncmp(p->name, Char2xmlChar("StartAddress"), xmlStrlen(p->name)) == 0) {
unsigned int atmp = 0;
sscanf((const char *)p->children->content, "#x%x", &atmp);
sc->std_rec_mbox_offset = atmp;
}
p = p->next;
}
}
if (xmlStrncmp(smchild->content, Char2xmlChar("MBoxIn"), xmlStrlen(smchild->content))== 0) {
xmlAttr *p = tmp->properties;
while (p!=NULL) {
if (xmlStrncmp(p->name, Char2xmlChar("DefaultSize"), xmlStrlen(p->name)) == 0) {
sc->std_snd_mbox_size = (uint16_t)atoi((const char *)p->children->content);
}
if (xmlStrncmp(p->name, Char2xmlChar("StartAddress"), xmlStrlen(p->name)) == 0) {
unsigned int atmp = 0;
sscanf((const char *)p->children->content, "#x%x", &atmp);
sc->std_snd_mbox_offset = atmp;
}
p = p->next;
}
}
smchild = smchild->next;
}
}
tmp = tmp->next;
}
/* get the supported mailboxes - these also occure again in the general section */
tmp = search_node_bfs(n, "Mailbox");
xmlNode *mbox;
mbox = search_node(tmp, "CoE");
if (mbox != NULL)
sc->mailbox_protocol.bit.coe = 1;
mbox = search_node(tmp, "EoE");
if (mbox != NULL)
sc->mailbox_protocol.bit.eoe = 1;
mbox = search_node(tmp, "FoE");
if (mbox != NULL)
sc->mailbox_protocol.bit.foe = 1;
mbox = search_node(tmp, "VoE");
if (mbox != NULL)
sc->mailbox_protocol.bit.voe = 1;
/* fetch eeprom size */
tmp = search_node(n, "ByteSize");
/* convert byte -> kbyte */
sc->eeprom_size = BYTES_TO_EE(atoi((char *)tmp->children->content));
sc->version = 1; /* also not in Esi */
tmp = search_node_bfs(n, "Eeprom");
if (tmp == NULL) {
fprintf(stderr, "Warning <Eeprom> tag not found");
} else {
xmlNode *bootstrap = search_node(tmp, "BootStrap");
if (bootstrap != NULL) {
char bsraw[MAX_BOOTSTRAP_STRING] = { 0 };
memmove(bsraw, (char *)bootstrap->children->content, MAX_BOOTSTRAP_STRING);
unsigned int braw[MAX_BOOTSTRAP_STRING] = { 0 };
sscanf(bsraw, "%2x%2x%2x%2x%2x%2x%2x%2x",
&braw[0], &braw[1], &braw[2], &braw[3],
&braw[4], &braw[5], &braw[6], &braw[7]);
sc->bs_rec_mbox_offset = braw[1] << 8 | braw[0];
sc->bs_rec_mbox_size = braw[3] << 8 | braw[2];
sc->bs_snd_mbox_offset = braw[5] << 8 | braw[4];
sc->bs_snd_mbox_size = braw[7] << 8 | braw[6];
}
}
return sc;
}
static struct _sii_general *parse_general(SiiInfo *sii, xmlNode *root)
{
xmlNode *parent;
xmlNode *node;
xmlNode *tmp;
struct _sii_general *general = calloc(1, sizeof(struct _sii_general));
/* Search group-,image-, order- and namestring and store these strings
* to the corresponding *index.
*
* Note, these strings are Vendor specific.
*/
parent = search_node(root, "Groups");
node = search_node(parent, "Group"); /* FIXME handle multiple groups??? */
tmp = search_node(node, "Type");
general->groupindex = sii_strings_add(sii, (const char *)tmp->children->content);
general->imageindex = 0;
general->orderindex = 0;
parent = search_node(root, "Devices");
node = search_node(parent, "Device"); /* FIXME handle multiple groups??? */
tmp = search_node(node, "Name"); /* FIXME check language id and use the english version */
general->nameindex = sii_strings_add(sii, (const char *)tmp->children->content);
/* reset temporial nodes */
parent = NULL;
node = NULL;
tmp = NULL;
/* fetch CoE details */
//printf("[DEBUG %s] get CoE details\n", __func__);
parent = search_node(root, "Device");
for (xmlAttr *attr = parent->properties; attr; attr = attr->next) {
if (xmlStrcmp(attr->name, Char2xmlChar("Physics")) == 0) {
char *phys = malloc(xmlStrlen(attr->children->content)+1);
memmove(phys, attr->children->content, xmlStrlen(attr->children->content)+1);
for (char *c = phys, port=0; *c != '\0'; c++, port++) {
int val = 0;
switch (*c) {
case 'Y': /* MII */
val = 0x01;
break;
case 'K': /* EBUS */
val = 0x03;
break;
default:
val = 0x00;
break;
}
switch (port) {
case 0:
general->phys_port_0 = val&0xf;
break;
case 1:
general->phys_port_1 = val&0xf;
break;
case 2:
general->phys_port_2 = val&0xf;
break;
case 3:
general->phys_port_3 = val&0xf;
break;
default:
fprintf(stderr, "Error invalid port %d\n", port);
break;
}
}
free(phys);
}
}
node = search_node_bfs(parent, "Mailbox");
tmp = search_node(node, "CoE");
if (tmp != NULL) {
general->coe_enable_sdo = 1;
/* parse the attributes */
for (xmlAttr *attr = tmp->properties; attr; attr = attr->next) {
if (xmlStrcmp(attr->name, Char2xmlChar("SdoInfo")) == 0)
general->coe_enable_sdo_info = parse_boolean(attr->children->content);
if (xmlStrcmp(attr->name, Char2xmlChar("PdoAssign")) == 0)
general->coe_enable_pdo_assign = parse_boolean(attr->children->content);
if (xmlStrcmp(attr->name, Char2xmlChar("PdoConfig")) == 0)
general->coe_enable_pdo_conf = parse_boolean(attr->children->content);
if (xmlStrcmp(attr->name, Char2xmlChar("PdoUpload")) == 0)
general->coe_enable_upload_start = parse_boolean(attr->children->content);
if (xmlStrcmp(attr->name, Char2xmlChar("??sdoComplete")) == 0)
general->coe_enable_sdo_complete = parse_boolean(attr->children->content);
//attr = attr->next;
}
}
tmp = search_node(node, "EoE");
if (tmp != NULL)
general->eoe_enabled = 1;
tmp = search_node(node, "FoE");
if (tmp != NULL)
general->foe_enabled = 1;
return general;
}
static void parse_fmmu(xmlNode *current, SiiInfo *sii)
{
struct _sii_cat *cat = sii_category_find(sii, SII_CAT_FMMU);
if (cat == NULL) { /* create new category */
cat = calloc(1, sizeof(struct _sii_cat));
cat->type = SII_CAT_FMMU;
sii_category_add(sii, cat);
}
if (cat->data == NULL) { /* if category exists but doesn't contain any data */
cat->data = (void *)calloc(1, sizeof(struct _sii_fmmu));
}
struct _sii_fmmu *fmmu = (struct _sii_fmmu *)cat->data;
/* now fetch the data */
xmlChar *content = current->children->content;
if (xmlStrncmp(content, Char2xmlChar("Inputs"), xmlStrlen(content)) == 0)
fmmu_add_entry(fmmu, FMMU_INPUTS);
else if (xmlStrncmp(content, Char2xmlChar("Outputs"), xmlStrlen(content)) == 0)
fmmu_add_entry(fmmu, FMMU_OUTPUTS);
else if (xmlStrncmp(content, Char2xmlChar("SynmanagerStat"), xmlStrlen(content)) == 0)
fmmu_add_entry(fmmu, FMMU_SYNCMSTAT);
else
fmmu_add_entry(fmmu, FMMU_UNUSED);
/* add entry to fmmu struct */
cat->size += 8; /* add size of new fmmu entry */
}
static void parse_syncm(xmlNode *current, SiiInfo *sii)
{
struct _sii_cat *cat = sii_category_find(sii, SII_CAT_SYNCM);
if (cat == NULL) {
cat = calloc(1, sizeof(struct _sii_cat));
cat->type = SII_CAT_SYNCM;
sii_category_add(sii, cat);
}
if (cat->data == NULL) {
cat->data = (void *)calloc(1, sizeof(struct _sii_syncm));
}
/* now fetch the data */
//size_t smsize = 0;
struct _sii_syncm *sm = (struct _sii_syncm *)cat->data;
struct _syncm_entry *entry = calloc(1, sizeof(struct _syncm_entry));
entry->id = -1;
xmlAttr *args = current->properties;
for (xmlAttr *a = args; a ; a = a->next) {
if (xmlStrncmp(a->name, Char2xmlChar("DefaultSize"), xmlStrlen(a->name)) == 0) {
entry->length = atoi((char *)a->children->content);
} else if (xmlStrncmp(a->name, Char2xmlChar("StartAddress"), xmlStrlen(a->name)) == 0) {
int tmp = 0;
sscanf((char *)a->children->content, "#x%x", &tmp);
entry->phys_address = tmp&0xffff;
} else if (xmlStrncmp(a->name, Char2xmlChar("ControlByte"), xmlStrlen(a->name)) == 0) {
int tmp = 0;
sscanf((char *)a->children->content, "#x%x", &tmp);
entry->control = tmp&0xff;
} else if (xmlStrncmp(a->name, Char2xmlChar("Enable"), xmlStrlen(a->name)) == 0) {
entry->enable = atoi((char *)a->children->content);
}
}
entry->status = 0; /* don't care */
/* type is encoded in the value of the node */
xmlChar *type = current->children->content;
if (xmlStrncmp(type, Char2xmlChar("MBoxIn"), xmlStrlen(type)) == 0)
entry->type = SMT_MBOXIN;
else if (xmlStrncmp(type, Char2xmlChar("MBoxOut"), xmlStrlen(type)) == 0)
entry->type = SMT_MBOXOUT;
else if (xmlStrncmp(type, Char2xmlChar("Inputs"), xmlStrlen(type)) == 0)
entry->type = SMT_INPUTS;
else if (xmlStrncmp(type, Char2xmlChar("Outputs"), xmlStrlen(type)) == 0)
entry->type = SMT_OUTPUTS;
else
entry->type = SMT_UNUSED;
syncm_entry_add(sm, entry);
cat->size += 8; /* a syncmanager entry is 8 bytes */
}
static void get_dc_proto(SiiInfo *sii)
{
struct _sii_cat *cat = calloc(1, sizeof(struct _sii_cat));
cat->type = SII_CAT_DCLOCK;
/* now fetch the data */
struct _sii_dclock *dc = dclock_get_default();
cat->data = (void *)dc;
cat->size = 48;
sii_category_add(sii, cat);
}
#if 0 // FIXME this is a future feature - don't remove
static void parse_dclock(xmlNode *current, SiiInfo *sii)
{
struct _sii_cat *cat = calloc(1, sizeof(struct _sii_cat));
cat->type = SII_CAT_DCLOCK;
/* now fetch the data */
size_t dcsize = 0;
struct _sii_dclock *dc = calloc(1, sizeof(struct _sii_dclock));
/* FIXME add entries, only use the first (default) OpMode */
printf("[DEBUG %s] FIXME implementation of dclock parsing is incomplete!\n", __func__);
xmlNode *op = search_node(current, "OpMode");
for (xmlNode *vals = op->children; vals; vals = vals->next) {
if (xmlStrncmp(vals->name, Char2xmlChar("Name"), xmlStrlen(vals->name)) == 0) {
//fprintf(stderr, "[DistributedClock] Warning, unknown handling of '%s'\n", (char *)vals->name);
} else if (xmlStrncmp(vals->name, Char2xmlChar("Desc"), xmlStrlen(vals->name)) == 0) {
//fprintf(stderr, "[DistributedClock] Warning, unknown handling of '%s'\n", (char *)vals->name);
} else if (xmlStrncmp(vals->name, Char2xmlChar("AssignActivate"), xmlStrlen(vals->name)) == 0) {
//fprintf(stderr, "[DistributedClock] Warning, unknown handling of '%s'\n", (char *)vals->name);
} else if (xmlStrncmp(vals->name, Char2xmlChar("CycleTimeSync0"), xmlStrlen(vals->name)) == 0) {
int tmp = atoi((char *)vals->children->content);
dc->sync0_cycle_time = (uint32_t)tmp;
} else if (xmlStrncmp(vals->name, Char2xmlChar("ShiftTimeSync0"), xmlStrlen(vals->name)) == 0) {
//fprintf(stderr, "[DistributedClock] Warning, unknown handling of '%s'\n", (char *)vals->name);
} else if (xmlStrncmp(vals->name, Char2xmlChar("CycleTimeSync1"), xmlStrlen(vals->name))) {
int tmp = atoi((char *)vals->children->content);
dc->sync1_cycle_time = (uint32_t)tmp;
} else if (xmlStrncmp(vals->name, Char2xmlChar("ShiftTimeSync1"), xmlStrlen(vals->name)) == 0) {
//fprintf(stderr, "[DistributedClock] Warning, unknown handling of '%s'\n", (char *)vals->name);
} else {
//fprintf(stderr, "[DistributedClock] Warning, unknown handling of '%s'\n", (char *)vals->name);
}
}
cat->data = (void *)dc;
cat->size = dcsize;
sii_category_add(sii, cat);
}
#endif
struct _coe_datatypes {
int index;
char *coename;
char *esiname;
};
static int parse_pdo_get_data_type(char *xmldatatype)
{
/* FIXME make match of CoE datatypes and xml description of types
* see table in document ETG 2000.S Page 18
*/
struct _coe_datatypes datatypes[] = {
{ 0, "UNDEF", "UNDEF" },
{ 0x0001, "BOOLEAN", "" },
{ 0x0002, "INTEGER8", "" },
{ 0x0003, "INTEGER16", "" },
{ 0x0004, "INTEGER32", "" },
{ 0x0005, "UNSIGNED8", "USINT" },
{ 0x0006, "UNSIGNED16", "UINT" },
{ 0x0007, "UNSIGNED32", "UDINT" },
{ 0x0008, "REAL32", "" },
{ 0x0009, "VISIBLE_STRING", "" },
{ 0x000A, "OCTET_STRING", "" },
{ 0x000B, "UNICODE_STRING", "" },
{ 0x000C, "TIME_OF_DAY", "" },
{ 0x000D, "TIME_DIFFERENCE", "" },
//{ 0x000E, "Reserved", "" },
{ 0x000F, "DOMAIN", "" },
{ 0x0010, "INTEGER24", "" },
{ 0x0011, "REAL64", "" },
{ 0x0012, "INTEGER40", "" },
{ 0x0013, "INTEGER48", "" },
{ 0x0014, "INTEGER56", "" },
{ 0x0015, "INTEGER64", "" },
{ 0x0016, "UNSIGNED24", "" },
//{ 0x0017, "Reserved", "" },
{ 0x0018, "UNSIGNED40", "" },
{ 0x0019, "UNSIGNED48", "" },
{ 0x001A, "UNSIGNED56", "" },
{ 0x001B, "UNSIGNED64", "" },
//{ 0x001C-0x001F, "reserved", "" },
{ 0, NULL, NULL }
};
for (struct _coe_datatypes *dt = datatypes; dt->esiname != NULL; dt++) {
if (strncmp(dt->esiname, xmldatatype, strlen(dt->esiname)) == 0)
return dt->index;
}
return -1; /* unrecognized */
}
static struct _pdo_entry *parse_pdo_entry(xmlNode *val, SiiInfo *sii)
{
struct _pdo_entry *entry = calloc(1, sizeof(struct _pdo_entry));
int tmp = 0;
for (xmlNode *child = val->children; child; child = child->next) {
if (xmlStrncmp(child->name, Char2xmlChar("Index"), xmlStrlen(child->name)) == 0) {
sscanf((char *)child->children->content, "#x%x", &tmp);
entry->index = tmp&0xffff;
tmp = 0;
} else if (xmlStrncmp(child->name, Char2xmlChar("SubIndex"), xmlStrlen(child->name)) == 0) {
tmp = atoi((char *)child->children->content);
entry->subindex = tmp&0xff;
tmp = 0;
} else if (xmlStrncmp(child->name, Char2xmlChar("BitLen"), xmlStrlen(child->name)) == 0) {
entry->bit_length = atoi((char *)child->children->content);
} else if (xmlStrncmp(child->name, Char2xmlChar("Name"), xmlStrlen(child->name)) == 0) {
/* again, write this to the string category and store index to string here. */
if (child->children == NULL) {
fprintf(stderr, "[WARNING] Reading child content of size 0 (line: %d)\n", child->line);
tmp = -1;
} else {
tmp = sii_strings_add(sii, (char *)child->children->content);
}
if (tmp < 0) {
fprintf(stderr, "Error creating input string!\n");
entry->string_index = 0;
} else {
entry->string_index = (uint8_t)tmp&0xff;
}
} else if (xmlStrncmp(child->name, Char2xmlChar("DataType"), xmlStrlen(child->name)) == 0) {
if (child->children == NULL) {
fprintf(stderr, "Warning unspecified datatype found. Please check your ESI, datatype is set to 0\n");
continue;
}
int dt = parse_pdo_get_data_type((char *)child->children->content);
if (dt <= 0)
fprintf(stderr, "Warning unrecognized esi data type '%s'\n", (char *)child->children->content);
else
entry->data_type = (uint8_t)dt;
#if 0 /* doesn't help much, except for debugging */
} else {
fprintf(stderr, "Warning, unrecognized pdo setting: '%s'\n",
(char *)child->name);
#endif
}
}
return entry;
}
static void parse_pdo(xmlNode *current, SiiInfo *sii)
{
enum eSection type;
if (xmlStrcmp(current->name, Char2xmlChar("RxPdo")) == 0)
type = SII_CAT_RXPDO;
else if(xmlStrcmp(current->name, Char2xmlChar("TxPdo")) == 0)
type = SII_CAT_TXPDO;
else {
fprintf(stderr, "[%s] Error, no PDO type\n", __func__);
return;
}
struct _sii_cat *cat = sii_category_find(sii, type);
if (cat == NULL) {
cat = calloc(1, sizeof(struct _sii_cat));
cat->type = type;
sii_category_add(sii, cat);
}
if (cat->data == NULL) {
cat->data = (void *)calloc(1, sizeof(struct _sii_pdo));
}
struct _sii_pdo *pdo = (struct _sii_pdo *)cat->data;
size_t pdosize = 8; /* base size of pdo */
/* get Arguments for syncmanager */
for (xmlAttr *attr = current->properties; attr; attr = attr->next) {
if (xmlStrncmp(attr->name, Char2xmlChar("Sm"), xmlStrlen(attr->name)) == 0) {
pdo->syncmanager = atoi((char *)attr->children->content);
}
/* currently fixed is unhandled - check where this setting should reside
else if (xmlStrncmp(attr->name, Char2xmlChar("Fixed"), xmlStrlen(attr->name))) {
}
*/
}
/* set uncrecognized values to default */
pdo->dcsync = 0;
pdo->flags = 0;
/* get node Name and node Index */
/* then parse the pdo list - all <Entry> children */
for (xmlNode *val = current->children; val; val = val->next) {
if (xmlStrncmp(val->name, Char2xmlChar("Name"), xmlStrlen(val->name)) == 0) {
int tmp = sii_strings_add(sii, (char *)val->children->content);
if (tmp < 0) {
fprintf(stderr, "Error creating input string!\n");
pdo->name_index = 0;
} else {
pdo->name_index = (uint8_t)tmp&0xff;
}
} else if (xmlStrncmp(val->name, Char2xmlChar("Index"), xmlStrlen(val->name)) == 0) {
int tmp = 0;
sscanf((char *)val->children->content, "#x%x", &tmp);
pdo->index = tmp&0xffff;
} else if (xmlStrncmp(val->name, Char2xmlChar("Entry"), xmlStrlen(val->name)) == 0) {
/* add new pdo entry */
pdo->entries += 1;
struct _pdo_entry *entry = parse_pdo_entry(val, sii);
pdo_entry_add(pdo, entry);
pdosize += 8; /* size of every pdo entry */
}
}
cat->size = pdosize;
}
/* API function */
struct _esi_data *esi_init(const char *file)
{
if (file == NULL) {
fprintf(stderr, "Warning, init with empty filename\n");
return NULL;
}
struct _esi_data *esi = (struct _esi_data *)calloc(1, sizeof(struct _esi_data));
enum eFileType type = efile_type(file);
switch (type) {
case SIIBIN:
/* init with sii bin */
esi->sii = sii_init_file(file);
if (esi->sii == NULL) {
fprintf(stderr, "Failed to read SII bin file '%s'\n", file);
free(esi);
return NULL;
}
break;
case XML:
/* init with xml */
LIBXML_TEST_VERSION
esi->sii = sii_init();
esi->doc = xmlReadFile(file, NULL, 0);
if (esi->doc == NULL) {
fprintf(stderr, "Failed to parse XML file '%s'\n", file);
free(esi->sii);
free(esi);
return NULL;
}
break;
case UNKNOWN:
default:
/* init empty ??? */
free(esi);
esi = NULL;
break;
}
return esi;
}
/* FIXME Input type is already given in the calling function parse_xml_input() */
EsiData *esi_init_string(const unsigned char *buf, size_t size)
{
EsiData *esi = calloc(1, sizeof(struct _esi_data));
enum eFileType type = UNKNOWN;
if (strncmp((const char *)buf, "<?xml", 5) == 0)
type = XML;
else
type = SIIBIN;
switch (type) {
case SIIBIN:
/* init with sii bin */
esi->sii = sii_init_string(buf, size);
if (esi->sii == NULL) {
fprintf(stderr, "Failed to read SII bin.\n");
free(esi);
return NULL;
}
break;
case XML:
/* init with xml */
LIBXML_TEST_VERSION
esi->sii = sii_init();
esi->doc = xmlReadMemory((const char *)buf, size, "noname.xml", NULL, 0);
if (esi->doc == NULL) {
fprintf(stderr, "Failed to parse XML.\n");
sii_release(esi->sii);
esi_release(esi);
return NULL;
}
break;
case UNKNOWN:
default:
/* init empty ??? */
free(esi);
esi = NULL;
break;
}
return esi;
}
void esi_release(struct _esi_data *esi)
{
xmlFreeDoc(esi->doc);
if (esi->sii != NULL)
sii_release(esi->sii);
if (esi->siifile != NULL)
free(esi->siifile);
if (esi->xmlroot != NULL)
free(esi->xmlroot);
if (esi->xmlfile != NULL)
free(esi->xmlfile);
free(esi);
}
int esi_parse(EsiData *esi)
{
xmlNode *root = xmlDocGetRootElement(esi->doc);
/* first, prepare category strings, since this is always needed */
struct _sii_cat *strings = sii_category_find(esi->sii, SII_CAT_STRINGS);
if (strings == NULL) {
strings = calloc(1, sizeof(struct _sii_cat));
strings->type = SII_CAT_STRINGS;
struct _sii_strings *strdata = calloc(1, sizeof(struct _sii_strings));
strings->data = strdata;
sii_category_add(esi->sii, strings);
}
xmlNode *n = search_node(root, "ConfigData");
esi->sii->preamble = parse_preamble(n);
esi->sii->config = parse_config(root);
struct _sii_general *general = parse_general(esi->sii, root);
struct _sii_cat *gencat = calloc(1, sizeof(struct _sii_cat));
gencat->type = SII_CAT_GENERAL;
gencat->size = sizeof(struct _sii_general);
gencat->data = (void *)general;
sii_category_add(esi->sii, gencat);