forked from LarsVoelker/FibexConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_to_wireshark_config.py
1376 lines (1069 loc) · 57.1 KB
/
configuration_to_wireshark_config.py
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
#!/usr/bin/python
# Automotive configuration file scripts
# Copyright (C) 2015-2023 Dr. Lars Voelker
# Copyright (C) 2018-2019 Dr. Lars Voelker, BMW AG
# Copyright (C) 2020-2023 Dr. Lars Voelker, Technica Engineering GmbH
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import sys
import time
import os.path
from parser_dispatcher import * # @UnusedWildImport
from configuration_base_classes import * # @UnusedWildImport
DEBUG_LEGACY_STRIPPING = False
class WiresharkParameterTypes:
basetype = 1
string = 2
array = 3
struct = 4
union = 5
typedef = 6
enum = 7
def cleanup_string(tmp):
ret = tmp.replace("\"", "'")
return ret
def cleanup_datatype_string(tmp):
ret = tmp[2:].lower()
if ret.startswith("uint"):
ret = "uint"
if ret.startswith("int"):
ret = "int"
if ret.startswith("float"):
ret = "float"
return ret
class WiresharkConfigurationFactory(BaseConfigurationFactory):
def __init__(self):
self.__services__ = dict()
self.__services_long__ = dict()
self.__param_arrays__ = dict()
self.__param_basetypes__ = dict()
self.__param_enums__ = dict()
self.__param_strings__ = dict()
self.__param_structs__ = dict()
self.__param_typedefs__ = dict()
self.__param_unions__ = dict()
self.__globalid_arrays__ = 1
self.__globalid_basetypes__ = 1
self.__globalid_enums__ = 1
self.__globalid_strings__ = 1
self.__globalid_structs__ = 1
self.__globalid_typedefs__ = 1
self.__globalid_unions__ = 1
self.__globalid_signal_pdus__ = 1
self.__space_optimized__ = True
self.__ecus__ = dict()
def create_backlinks(self):
for s in self.__services__.values():
s.create_backlinks(self)
def create_ecu(self, name, controllers):
tmp = BaseECU(name, controllers)
print(f"Adding ECU {name}")
if tmp in self.__ecus__:
print(f"Detected duplicate ECU {name}")
self.__ecus__[name] = tmp
return tmp
def create_someip_service(self, name, serviceid, majorver, minorver, methods, events, fields, eventgroups):
ret = SOMEIPService(name, serviceid, majorver, minorver, methods, events, fields, eventgroups)
print(f"Adding Service(ID: 0x{serviceid:04x} Ver: {majorver:d}.{minorver:d})")
self.add_service(serviceid, majorver, minorver, ret)
return ret
def create_someip_service_method(self, name, methodid, calltype, relia, inparams, outparams,
reqdebounce=-1, reqmaxretention=-1, resmaxretention=-1, tlv=False):
return SOMEIPServiceMethod(name, methodid, calltype, relia, inparams, outparams,
reqdebounce, reqmaxretention, resmaxretention, tlv)
def create_someip_service_event(self, name, methodid, relia, params,
debounce=-1, maxretention=-1, tlv=False):
return SOMEIPServiceEvent(name, methodid, relia, params,
debounce, maxretention, tlv)
def create_someip_service_field(self, name, getterid, setterid, notifierid,
getterreli, setterreli, notifierreli, params,
getter_debouncereq, getter_retentionreq, getter_retentionres,
setter_debouncereq, setter_retentionreq, setter_retentionres,
notifier_debounce, notifier_retention, tlv=False):
ret = SOMEIPServiceField(self, name, getterid, setterid, notifierid,
getterreli, setterreli, notifierreli, params,
getter_debouncereq, getter_retentionreq, getter_retentionres,
setter_debouncereq, setter_retentionreq, setter_retentionres,
notifier_debounce, notifier_retention, tlv)
return ret
def create_someip_parameter(self, position, name, desc, mandatory, datatype, signal):
return SOMEIPParameter(position, name, desc, mandatory, datatype, signal)
def create_someip_parameter_basetype(self, name, datatype, bigendian, bitlength_basetype, bitlength_encoded_type):
if bitlength_basetype != bitlength_encoded_type:
name = "%s-%d" % (name, bitlength_encoded_type)
ret = SOMEIPParameterBasetype(self.__globalid_basetypes__, name, datatype, bigendian, bitlength_basetype,
bitlength_encoded_type)
if self.__space_optimized__:
for key in self.__param_basetypes__:
tmp = self.__param_basetypes__[key]
if tmp == ret:
return tmp
self.__param_basetypes__[self.__globalid_basetypes__] = ret
self.__globalid_basetypes__ += 1
return ret
def create_someip_parameter_string(self, name, chartype, bigendian, lowerlimit, upperlimit, termination,
length_of_length, pad_to):
ret = SOMEIPParameterString(self.__globalid_strings__, name, chartype, bigendian, lowerlimit, upperlimit,
termination, length_of_length, pad_to)
if self.__space_optimized__:
for key in self.__param_strings__:
tmp = self.__param_strings__[key]
if tmp == ret:
return tmp
self.__param_strings__[self.__globalid_strings__] = ret
self.__globalid_strings__ += 1
return ret
def create_someip_parameter_array(self, name, dims, child):
ret = SOMEIPParameterArray(self.__globalid_arrays__, name, dims, child)
# if self.__space_optimized__:
# for key in self.__param_arrays__:
# tmp = self.__param_arrays__[key]
# if tmp == ret:
self.__param_arrays__[self.__globalid_arrays__] = ret
self.__globalid_arrays__ += 1
return ret
def create_someip_parameter_array_dim(self, dim, lowerlimit, upperlimit, length_of_length, pad_to):
return SOMEIPBaseParameterArrayDim(dim, lowerlimit, upperlimit, length_of_length, pad_to)
def create_someip_parameter_struct(self, name, length_of_length, pad_to, members, tlv=False):
ret = SOMEIPParameterStruct(self.__globalid_structs__, name, length_of_length, pad_to, members, tlv)
# if self.__space_optimized__:
# for key in self.__param_structs__:
# tmp = self.__param_structs__[key]
# if tmp == ret:
# return tmp
self.__param_structs__[self.__globalid_structs__] = ret
self.__globalid_structs__ += 1
return ret
def create_someip_parameter_struct_member(self, position, name, mandatory, child, signal):
return SOMEIPParameterStructMember(position, name, mandatory, child, signal)
def create_someip_parameter_typedef(self, name, name2, child):
ret = SOMEIPParameterTypedef(self.__globalid_typedefs__, name, name2, child)
# if self.__space_optimized__:
# for key in self.__param_typedefs__:
# tmp = self.__param_typedefs__[key]
# if tmp == ret:
# return tmp
self.__param_typedefs__[self.__globalid_typedefs__] = ret
self.__globalid_typedefs__ += 1
return ret
def create_someip_parameter_enumeration(self, name, items, child):
ret = SOMEIPParameterEnumeration(self.__globalid_enums__, name, items, child)
if self.__space_optimized__:
for key in self.__param_enums__:
tmp = self.__param_enums__[key]
if tmp == ret:
return tmp
self.__param_enums__[self.__globalid_enums__] = ret
self.__globalid_enums__ += 1
return ret
def create_someip_parameter_enumeration_item(self, value, name, desc):
return SOMEIPBaseParameterEnumerationItem(value, name, desc)
def create_someip_parameter_union(self, name, length_of_length, length_of_type, pad_to, members):
ret = SOMEIPParameterUnion(self.__globalid_unions__, name, length_of_length, length_of_type, pad_to, members)
# if self.__space_optimized__:
# for key in self.__param_unions__:
# tmp = self.__param_unions__[key]
# if tmp == ret:
# return tmp
self.__param_unions__[self.__globalid_unions__] = ret
self.__globalid_unions__ += 1
return ret
def create_someip_parameter_union_member(self, index, name, mandatory, child):
return SOMEIPParameterUnionMember(index, name, mandatory, child)
def add_service(self, serviceid, majorver, minorver, service):
sid = "%04x-%02x-%08x" % (serviceid, majorver, minorver)
if sid in self.__services_long__:
print(
f"ERROR: Service (SID: 0x{serviceid:04x}, Major-Ver: {majorver:d}, " +
f"Minor-Ver: {minorver:d}) already exists! Not overriding it!"
)
return False
self.__services_long__[sid] = service
sid = "%04x-%02x" % (serviceid, majorver)
if sid in self.__services__:
print(
f"ERROR: Service (SID: 0x{serviceid:04x}, Major-Ver: {majorver:d}) " +
f"already exists with a different Minor Version (not {minorver:d})! Not overriding it!"
)
return False
self.__services__[sid] = service
return True
def get_service(self, serviceid, majorver, minorver=None):
if minorver is None:
sid = "%04x-%02x" % (serviceid, majorver)
if sid in self.__services__:
return self.__services__[sid]
else:
return None
else:
sid = "%04x-%02x-%08x" % (serviceid, majorver, minorver)
if sid in self.__services_long__:
return self.__services_long__[sid]
else:
return None
def __str__(self):
ret = "Services: \n"
for serviceid in self.__services__:
ret += self.__services__[serviceid].str(2)
ret += "\nECUs: \n"
for name in self.__ecus__:
ret += self.__ecus__[name].str(2)
return ret
def write_name_configs(self, conf_services, conf_methods, conf_eventgroups, version=1):
count_services = 0
count_events = 0
count_methods = 0
count_fields = 0
d = dict()
for sid in self.__services__.keys():
count_services += 1
s = self.__services__[sid]
if s.serviceid() in d.keys():
if d[s.serviceid()] != s.name():
print(
f"ERROR: We got the same Service-ID 0x{s.serviceid():04x} " +
f"with different names {d[s.serviceid()].name()} {s.name()}"
)
else:
d[s.serviceid()] = s
keys = sorted(d.keys())
fs = open(conf_services, "w")
fm = open(conf_methods, "w")
fe = open(conf_eventgroups, "w")
fs.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
fm.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
fe.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
for key in keys:
s = d[key]
fs.write(f"\"{key:04x}\",\"{s.name()}\"\n")
dm = dict()
tmp = s.methods()
for mid in tmp:
count_methods += 1
dm[tmp[mid].methodid()] = tmp[mid].name()
tmp = s.events()
for eid in tmp:
count_events += 1
dm[tmp[eid].methodid()] = tmp[eid].name()
tmp = s.fields()
for fid in tmp:
count_fields += 1
if tmp[fid].getter() is not None:
count_methods += 1
dm[tmp[fid].getter().methodid()] = tmp[fid].name() + "_Getter"
if tmp[fid].setter() is not None:
count_methods += 1
dm[tmp[fid].setter().methodid()] = tmp[fid].name() + "_Setter"
if tmp[fid].notifier() is not None:
count_events += 1
dm[tmp[fid].notifier().methodid()] = tmp[fid].name() + "_Notifier"
for mkey in sorted(dm.keys()):
fm.write(f"\"{key:04x}\",\"{mkey:04x}\",\"{dm[mkey]}\"\n")
de = dict()
tmp = s.eventgroups()
for eg in tmp:
de[tmp[eg].id()] = tmp[eg].name()
for egkey in sorted(de.keys()):
fe.write(f"\"{key:04x}\",\"{egkey:04x}\",\"{de[egkey]}\"\n")
fs.close()
fm.close()
fe.close()
if version == 1:
print(f" Found {count_services} services, {count_methods} methods, and {count_events} events. "
f"This includes the methods and events of {count_fields} fields.")
@staticmethod
def write_ws_config(filename, arr, version=1):
f = open(filename, "w")
f.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
for key in arr:
i = arr[key]
f.write(f"{i.ws_config_line(version)}")
f.close()
@staticmethod
def write_parameter_configlines(f, service, method, msgtype, params, version):
for p in params:
if p.datatype() is not None:
tmp = "\"%04x\",\"%04x\",\"%d\",\"%x\"" % (service.serviceid(),
method.methodid(),
service.majorversion(),
msgtype)
if version > 1:
if method.tlv():
tmp += f",\"TRUE\""
else:
tmp += f",\"FALSE\""
tmp += ",\"%d\"" % (len(params))
tmp += ",\"%d\",\"%s\",\"%d\",\"%08x\"" % (p.position(),
p.name(),
p.datatype().paramtype(),
p.datatype().globalid())
if version > 1:
tmp += f",\"{service.name()}.{method.name()}.{p.name()}\""
tmp += "\n"
f.write(tmp)
else:
print(f"ERROR: Cannot write config, if p.datatype() = None! "
f"Service: {service.name()} Method: {method.name()} Param: {p.name()} Pos: {p.position()}")
def write_parameter_config(self, filename, version=1):
# Service-ID,Method-ID,Version,MessageType,Num-Of-Params,Position,Name,Datatype,Datatype-ID
f = open(filename, "w")
f.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
for sid in sorted(self.__services__):
serv = self.__services__[sid]
for key in sorted(serv.methods()):
method = serv.methods()[key]
if version == 1 or not method.legacy():
if method.calltype() == "REQUEST_RESPONSE":
self.write_parameter_configlines(f, serv, method, 0x00, method.inparams(), version)
self.write_parameter_configlines(f, serv, method, 0x80, method.outparams(), version)
else:
self.write_parameter_configlines(f, serv, method, 0x01, method.inparams(), version)
elif DEBUG_LEGACY_STRIPPING:
print(f"--> skipping legacy method: {serv.name()} (0x{serv.serviceid():x}) {method.name()} (0x{method.methodid():x})")
for key in sorted(serv.events()):
if version == 1 or not serv.events()[key].legacy():
self.write_parameter_configlines(f, serv, serv.events()[key], 0x02, serv.events()[key].params(),
version)
elif DEBUG_LEGACY_STRIPPING:
print(f"--> skipping legacy event: {serv.name()} (0x{serv.serviceid():x}) {method.name()} (0x{method.methodid():x})")
for key in sorted(serv.fields(), key=lambda x: (x is None, x)):
field = serv.fields()[key]
if version == 1 or not field.legacy():
if field.getter() is not None:
self.write_parameter_configlines(f, serv, field.getter(), 0x00, field.getter().inparams(), version)
self.write_parameter_configlines(f, serv, field.getter(), 0x80, field.getter().outparams(), version)
if field.setter() is not None:
self.write_parameter_configlines(f, serv, field.setter(), 0x00, field.setter().inparams(), version)
self.write_parameter_configlines(f, serv, field.setter(), 0x80, field.setter().outparams(), version)
if field.notifier() is not None:
self.write_parameter_configlines(f, serv, field.notifier(), 0x02, field.notifier().params(),
version)
elif DEBUG_LEGACY_STRIPPING:
print(f"--> skipping legacy field: {serv.name()} (0x{serv.serviceid():x}) {field.name()}")
f.close()
def write_parameter_basetypes(self, filename, version=1):
self.write_ws_config(filename, self.__param_basetypes__, version)
def write_parameter_strings(self, filename, version=1):
self.write_ws_config(filename, self.__param_strings__, version)
def write_parameter_arrays(self, filename, version=1):
self.write_ws_config(filename, self.__param_arrays__, version)
def write_parameter_structs(self, filename, version=1):
self.write_ws_config(filename, self.__param_structs__, version)
def write_parameter_typedefs(self, filename, version=1):
self.write_ws_config(filename, self.__param_typedefs__, version)
def write_parameter_unions(self, filename, version=1):
self.write_ws_config(filename, self.__param_unions__, version)
def write_parameter_enums(self, filename, version=1):
self.write_ws_config(filename, self.__param_enums__, version)
def write_hosts(self, filename, version=1):
# ip name
f = open(filename, "w")
f.write("# This file is automatically generated, DO NOT MODIFY (LV).\n")
ips = dict()
for ecuname in self.__ecus__:
for controller in self.__ecus__[ecuname].controllers():
for interface in controller.interfaces():
for socket in interface.sockets():
if not is_ip_mcast(socket.ip()):
tmp = ips.setdefault(socket.ip(), {})
tmp[ecuname] = ecuname
# let us also include IPs without sockets
for ip in interface.ips():
if is_ip(ip) and not is_ip_mcast(ip):
tmp = ips.setdefault(ip, {})
tmp[ecuname] = ecuname
for ip in sorted(ips.keys(), key=lambda x: ip_to_key(x)):
ecu_names = "__".join(ips[ip])
f.write(f"{ip}\t{ecu_names}\n")
f.close()
def write_vlanids(self, filename, version=1):
# vlanids name
f = open(filename, "w")
f.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
vlans = dict()
for ecu in self.__ecus__:
for controller in self.__ecus__[ecu].controllers():
for interface in controller.interfaces():
vlans[interface.vlanid()] = interface.vlanname()
for vlan in sorted(vlans):
if vlan != 0:
f.write(f"{vlan}\t{vlans[vlan]}\n")
f.close()
def write_signal_pdu_binding_someip_configline(self, f, service, method, msgtype, pdu_id):
# Service-ID, Method-ID, MessageType, Version, Legacy-ID
if not method.legacy():
return
f.write(f"\"{service.serviceid():04x}\","
f"\"{method.methodid():04x}\","
f"\"{service.majorversion():02x}\","
f"\"{msgtype:02x}\","
f"\"{pdu_id:08x}\"\n"
)
def write_someip_signal_pdunames_configline(self, f, pdu_id, name):
# Legacy-ID, Name
f.write(f"\"{pdu_id:08x}\","
f"\"{name}\"\n"
)
def write_someip_signals_configlines(self, f, f_enum, pdu_id, pdu_name, params):
# signals (f)
# ID, Num of Sigs, Pos, Name, Data Type, BE (TRUE/FALSE), bitlen base, bitlen coded, scaler, offset,
# Multiplexer (FALSE), Muliplex value (-1), Hidden (FALSE)
# enums (f_enum)
# ID, Pos, Num of Values, Value start, Value end, Value Name
if params is None or len(params) == 0:
return
# there might be exactly one struct as wrapper, if we are part of a field
tmp = params
if isinstance(params[0].datatype(), SOMEIPParameterStruct):
tmp = params[0].datatype().members()
for k in sorted(tmp.keys()):
m = tmp[k]
endian = "TRUE" if m.child().bigendian() else "FALSE"
hidden = "TRUE" if m.name().startswith("dummy") else "FALSE"
scaler = 1
offset = 0
if m.signal() is not None and m.signal().compu_scale() is not None and \
len(m.signal().compu_scale()) == 3:
num0, num1, denom = m.signal().compu_scale()
offset = float(num0)
scaler = float(num1) / float(denom)
if m.signal() is not None and m.signal().compu_consts() is not None:
cc = m.signal().compu_consts()
for value, start, end in m.signal().compu_consts():
if 0 <= int(start) <= pow(2, 64) and 0 <= int(end) <= pow(2, 64):
f_enum.write(f"\"{pdu_id:08x}\","
f"\"{m.position()}\","
f"\"{len(cc)}\","
f"\"{int(start):x}\","
f"\"{int(end):x}\","
f"\"{cleanup_string(value)}\""
"\n"
)
else:
print(f"Warning: CompuConst<0 or >2^64 not supported! "
f"{pdu_id:08x}:{m.position()} {start}-{end} {value}")
f.write(f"\"{pdu_id:08x}\","
f"\"{len(tmp)}\","
f"\"{m.position()}\","
f"\"{m.name()}\","
f"\"{pdu_name}.{m.name()}\","
f"\"{cleanup_datatype_string(m.child().datatype())}\","
f"\"{endian}\","
f"\"{m.child().bitlength_basetype()}\","
f"\"{m.child().bitlength_encoded_type()}\","
f"\"{scaler}\","
f"\"{offset}\","
f"\"FALSE\","
f"\"-1\","
f"\"{hidden}\""
"\n"
)
return
for p in params:
endian = "TRUE" if p.datatype().bigendian() else "FALSE"
hidden = "TRUE" if p.name().startswith("dummy") else "FALSE"
scaler = 1
offset = 0
if p.signal() is not None and p.signal().compu_scale() is not None and len(p.signal().compu_scale()) == 3:
num0, num1, denom = p.signal().compu_scale()
offset = float(num0)
scaler = float(num1) / float(denom)
if p.signal() is not None and p.signal().compu_consts() is not None:
cc = p.signal().compu_consts()
for value, start, end in p.signal().compu_consts():
if int(start) >= 0:
f_enum.write(f"\"{pdu_id:08x}\","
f"\"{p.position()}\","
f"\"{len(cc)}\","
f"\"{start}\","
f"\"{end}\","
f"\"{cleanup_string(value)}\""
"\n"
)
else:
print(f"Warning: CompuConst<0 unsupported! {pdu_id:08x}:{p.position()} {start}-{end} {value}")
f.write(f"\"{pdu_id:08x}\","
f"\"{len(params)}\","
f"\"{p.position()}\","
f"\"{p.name()}\","
f"\"{pdu_name}.{p.name()}\","
f"\"{cleanup_datatype_string(p.datatype().datatype())}\","
f"\"{endian}\","
f"\"{p.datatype().bitlength_basetype()}\","
f"\"{p.datatype().bitlength_encoded_type()}\","
f"\"{scaler}\","
f"\"{offset}\","
f"\"FALSE\","
f"\"-1\","
f"\"{hidden}\""
"\n"
)
def someip_pdu_id(self):
ret = self.__globalid_signal_pdus__
self.__globalid_signal_pdus__ += 1
return ret
def write_pdus_over_someip_config(self, target_dir, fn_bind, fn_id, fn_sig_list, fn_sig_values, version=1):
f_bind = open(os.path.join(target_dir, fn_bind), "w")
f_bind.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
f_id = open(os.path.join(target_dir, fn_id), "w")
f_id.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
f_sig = open(os.path.join(target_dir, fn_sig_list), "w")
f_sig.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
f_sigv = open(os.path.join(target_dir, fn_sig_values), "w")
f_sigv.write("# This file is automatically generated, DO NOT MODIFY. (LV)\n")
for sid in sorted(self.__services__):
serv = self.__services__[sid]
for key in sorted(serv.methods()):
method = serv.methods()[key]
if not method.legacy():
continue
if method.calltype() == "REQUEST_RESPONSE":
# Request:
pdu_id = self.someip_pdu_id()
# signal pdu
self.write_someip_signal_pdunames_configline(f_id, pdu_id, method.name())
# signals
self.write_someip_signals_configlines(f_sig, f_sigv, pdu_id, method.name(), method.inparams())
# binding
self.write_signal_pdu_binding_someip_configline(f_bind, serv, method, 0x00, pdu_id)
# Response:
pdu_id = self.someip_pdu_id()
# signal pdu
self.write_someip_signal_pdunames_configline(f_id, pdu_id, method.name())
# signals
self.write_someip_signals_configlines(f_sig, f_sigv, pdu_id, method.name(), method.outparams())
# binding
self.write_signal_pdu_binding_someip_configline(f_bind, serv, method, 0x80, pdu_id)
else:
pdu_id = self.someip_pdu_id()
# signal pdu
self.write_someip_signal_pdunames_configline(f_id, pdu_id, method.name())
# signals
self.write_someip_signals_configlines(f_sig, f_sigv, pdu_id, method.name(), method.inparams())
# binding
self.write_signal_pdu_binding_someip_configline(f_bind, serv, method, 0x01, pdu_id)
for key in sorted(serv.events()):
event = serv.events()[key]
if not event.legacy():
continue
pdu_id = self.someip_pdu_id()
# signal pdu
self.write_someip_signal_pdunames_configline(f_id, pdu_id, event.name())
# signals
self.write_someip_signals_configlines(f_sig, f_sigv, pdu_id, event.name(), event.params())
# binding
self.write_signal_pdu_binding_someip_configline(f_bind, serv, event, 0x02, pdu_id)
for key in sorted(serv.fields(), key=lambda x: (x is None, x)):
field = serv.fields()[key]
if not field.legacy():
continue
if field.getter() is not None or field.setter() is not None or field.notifier() is not None:
pdu_id = self.someip_pdu_id()
# signal pdu
self.write_someip_signal_pdunames_configline(f_id, pdu_id, field.name())
# signals
self.write_someip_signals_configlines(f_sig, f_sigv, pdu_id, field.name(), field.params())
if field.getter() is not None:
# binding (only response has payload)
self.write_signal_pdu_binding_someip_configline(f_bind, serv, field.getter(), 0x80, pdu_id)
if field.setter() is not None:
# binding
self.write_signal_pdu_binding_someip_configline(f_bind, serv, field.setter(), 0x00, pdu_id)
self.write_signal_pdu_binding_someip_configline(f_bind, serv, field.setter(), 0x80, pdu_id)
if field.notifier() is not None:
# binding
self.write_signal_pdu_binding_someip_configline(f_bind, serv, field.notifier(), 0x02, pdu_id)
f_bind.close()
f_id.close()
f_sig.close()
f_sigv.close()
class SOMEIPService(SOMEIPBaseService):
def create_backlinks(self, factory):
for i in self.__methods__.values():
i.create_backlinks(factory, self)
for i in self.__events__.values():
i.create_backlinks(factory, self)
for i in self.__fields__.values():
i.create_backlinks(factory, self)
class SOMEIPServiceMethod(SOMEIPBaseServiceMethod):
def create_backlinks(self, factory, service):
tmp = []
for p in self.__inparams__:
if p is not None:
tmp.append(p.create_backlinks(factory, service, self))
self.__inparams__ = tmp
tmp = []
for p in self.__outparams__:
if p is not None:
tmp.append(p.create_backlinks(factory, service, self))
self.__outparams__ = tmp
class SOMEIPServiceEvent(SOMEIPBaseServiceEvent):
def create_backlinks(self, factory, service):
tmp = []
for p in self.__params__:
if p is not None:
tmp.append(p.create_backlinks(factory, service, self))
self.__params__ = tmp
class SOMEIPServiceField(SOMEIPBaseServiceField):
def create_backlinks(self, factory, service):
if self.__getter__ is not None:
self.__getter__.create_backlinks(factory, service)
if self.__setter__ is not None:
self.__setter__.create_backlinks(factory, service)
if self.__notifier__ is not None:
self.__notifier__.create_backlinks(factory, service)
class SOMEIPParameter(SOMEIPBaseParameter):
def __init__(self, position, name, desc, mandatory, datatype, signal):
super(SOMEIPParameter, self).__init__(position, name, desc, mandatory, datatype, signal)
self.__parent_service__ = None
self.__parent_method__ = None
def create_backlinks(self, factory, service, method):
if factory is None or (self.__parent_service__ is None and self.__parent_method__ is None):
self.__parent_service__ = service
self.__parent_method__ = method
if self.__datatype__ is None:
print(f"ERROR: create_backlinks __datatype__ is None {service.name()} {method.name()}")
return self
self.__datatype__ = self.__datatype__.create_backlinks(factory, service, method)
return self
else:
ret = factory.create_someip_parameter(self.__position__,
self.__name__,
self.__desc__,
self.__mandatory__,
self.__datatype__,
self.__signal__)
return ret.create_backlinks(factory, service, method)
def parent_service(self):
return self.__parent_service__
def parent_method(self):
return self.__parent_method__
class SOMEIPParameterBasetype(SOMEIPBaseParameterBasetype):
def __init__(self, globalid, name, datatype, bigendian, bitlength_basetype, bitlength_encoded_type):
super(SOMEIPParameterBasetype, self).__init__(name, datatype, bigendian, bitlength_basetype,
bitlength_encoded_type)
self.__globalid__ = int(globalid)
def create_backlinks(self, factory, service, method):
return self
def globalid(self):
return self.__globalid__
@staticmethod
def paramtype():
return WiresharkParameterTypes.basetype
@staticmethod
def translate_datatype(dt):
if dt.lower().startswith("a_"):
return dt[2:].lower()
else:
return dt.lower()
def ws_config_line(self, version=1):
# Type-ID,Name,Datatype,BigEndian,BitlengthBase,BiglengthEncoded
if version == 1:
endianess = 1 if self.bigendian() else 0
return "\"%08x\",\"%s\",\"%s\",\"%d\",\"%d\",\"%d\"\n" % (self.globalid(),
self.name(),
self.translate_datatype(self.datatype()),
endianess,
self.bitlength_basetype(),
self.bitlength_encoded_type())
else:
# remove non SOME/IP datatypes since they are configured as Signal-PDU configs
if self.bitlength_basetype() not in (8, 16, 32, 64):
return ""
if self.bitlength_basetype() != self.bitlength_encoded_type():
return ""
endianess = "TRUE" if self.bigendian() else "FALSE"
return "\"%08x\",\"%s\",\"%s\",\"%s\",\"%d\",\"%d\"\n" % (self.globalid(),
self.name(),
self.translate_datatype(self.datatype()),
endianess,
self.bitlength_basetype(),
self.bitlength_encoded_type())
class SOMEIPParameterString(SOMEIPBaseParameterString):
def __init__(self, globalid, name, chartype, bigendian, lowerlimit, upperlimit, termination, length_of_length,
pad_to):
super(SOMEIPParameterString, self).__init__(name, chartype, bigendian, lowerlimit, upperlimit, termination,
length_of_length, pad_to)
self.__globalid__ = int(globalid)
def create_backlinks(self, factory, service, method):
return self
def globalid(self):
return self.__globalid__
@staticmethod
def paramtype():
return WiresharkParameterTypes.string
def ws_config_line(self, version=1):
if version == 1:
# String-ID,Name,Encoding,Dynamic_Length,Max-Length,Length-Field-Size,Big-Endian,Bit-Alignment
dynlength = 0 if self.lowerlimit() == self.upperlimit() else 1
endianess = 1 if self.bigendian() else 0
return "\"%08x\",\"%s\",\"%s\",\"%d\",\"%d\",\"%d\",\"%d\",\"%d\"\n" % (self.globalid(),
self.name(),
self.chartype().lower(),
dynlength, # self.lowerlimit(),
self.upperlimit(),
self.length_of_length(),
endianess,
self.pad_to())
else:
# String-ID,Name,Encoding,Dynamic_Length,Max-Length,Length-Field-Size,Big-Endian,Bit-Alignment
dynlength = "FALSE" if self.lowerlimit() == self.upperlimit() else "TRUE"
endianess = "TRUE" if self.bigendian() else "FALSE"
return "\"%08x\",\"%s\",\"%s\",\"%s\",\"%d\",\"%d\",\"%s\",\"%d\"\n" % (self.globalid(),
self.name(),
self.chartype().lower(),
dynlength, # self.lowerlimit(),
self.upperlimit(),
self.length_of_length(),
endianess,
self.pad_to())
class SOMEIPParameterArray(SOMEIPBaseParameterArray):
def __init__(self, globalid, name, dims, child):
super(SOMEIPParameterArray, self).__init__(name, dims, child)
self.__globalid__ = int(globalid)
self.__parent_service__ = None
self.__parent_method__ = None
def create_backlinks(self, factory, service, method):
if factory is None or (self.__parent_service__ is None and self.__parent_method__ is None):
self.__parent_service__ = service
self.__parent_method__ = method
self.__child__ = self.__child__.create_backlinks(factory, service, method)
return self
else:
ret = factory.create_someip_parameter_array(self.__name__, self.__dims__, self.__child__)
return ret.create_backlinks(factory, service, method)
def globalid(self):
return self.__globalid__
@staticmethod
def paramtype():
return WiresharkParameterTypes.array
def ws_config_line(self, version=1):
# Array-ID,Name,DT-Type,DT-ID,MaxDim,Dim,Min,Max,LenOfLen,PadTo
if self.__parent_service__ is None or self.__parent_method__ is None:
print(f" Warning: array ({self.name()}) is not attached to service!")
ret = ""
for key in self.dims():
d = self.dims()[key]
ret += "\"%08x\",\"%s\",\"%d\",\"%08x\",\"%d\"" % (self.globalid(),
self.name(),
self.child().paramtype(),
self.child().globalid(),
len(self.dims()))
if version > 1:
if self.__parent_service__ is None or self.__parent_method__ is None:
ret += f",\"invalid.invalid.{self.name()}\""
else:
ret += f",\"{self.__parent_service__.name()}.{self.__parent_method__.name()}.{self.name()}\""
ret += ",\"%d\",\"%d\",\"%d\",\"%d\",\"%d\"\n" % (d.dim() - 1,
d.lowerlimit(),
d.upperlimit(),
d.length_of_length(),
d.pad_to())
return ret
class SOMEIPParameterStruct(SOMEIPBaseParameterStruct):
def __init__(self, globalid, name, length_of_length, pad_to, members, tlv):
super(SOMEIPParameterStruct, self).__init__(name, length_of_length, pad_to, members, tlv)
self.__globalid__ = int(globalid)
self.__parent_service__ = None
self.__parent_method__ = None
def create_backlinks(self, factory, service, method):
if factory is None or (self.__parent_service__ is None and self.__parent_method__ is None):
self.__parent_service__ = service
self.__parent_method__ = method