forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_data_model_xml.py
executable file
·677 lines (605 loc) · 26.7 KB
/
test_data_model_xml.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
#!/usr/bin/env python3
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import unittest
from difflib import unified_diff
from typing import List, Optional, Union
try:
from matter_idl.data_model_xml import ParseSource, ParseXmls
except ImportError:
import os
import sys
sys.path.append(os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
from matter_idl.data_model_xml import ParseSource, ParseXmls
from matter_idl.generators import GeneratorStorage
from matter_idl.generators.idl import IdlGenerator
from matter_idl.matter_idl_parser import CreateParser
from matter_idl.matter_idl_types import Idl
class GeneratorContentStorage(GeneratorStorage):
def __init__(self):
super().__init__()
self.content: Optional[str] = None
def get_existing_data(self, relative_path: str):
# Force re-generation each time
return None
def write_new_data(self, relative_path: str, content: str):
if self.content:
raise Exception(
"Unexpected extra data: single file generation expected")
self.content = content
def RenderAsIdlTxt(idl: Idl) -> str:
storage = GeneratorContentStorage()
IdlGenerator(storage=storage, idl=idl).render(dry_run=False)
return storage.content or ""
def XmlToIdl(what: Union[str, List[str]]) -> Idl:
if not isinstance(what, list):
what = [what]
sources = []
for idx, txt in enumerate(what):
sources.append(ParseSource(source=io.StringIO(
txt), name=("Input %d" % (idx + 1))))
return ParseXmls(sources, include_meta_data=False)
def IdlTextToIdl(what: str) -> Idl:
return CreateParser(skip_meta=True).parse(what)
class TestXmlParser(unittest.TestCase):
def __init__(self, *args, **kargs):
super().__init__(*args, **kargs)
self.maxDiff = None
def assertIdlEqual(self, a: Idl, b: Idl):
if a == b:
# seems the same. This will just pass
self.assertEqual(a, b)
return
# Not the same. Try to make a human readable diff:
a_txt = RenderAsIdlTxt(a)
b_txt = RenderAsIdlTxt(b)
delta = unified_diff(a_txt.splitlines(keepends=True),
b_txt.splitlines(keepends=True),
fromfile='actual.matter',
tofile='expected.matter',
)
self.assertEqual(a, b, '\n' + ''.join(delta))
def testBasicInput(self):
xml_idl = XmlToIdl('''
<cluster id="123" name="Test" revision="1"/>
''')
expected_idl = IdlTextToIdl('''
client cluster Test = 123 {
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
def testClusterDerivation(self):
# This test is based on a subset of ModeBase and Mode_Dishwasher original xml files
xml_idl = XmlToIdl([
# base ...
'''
<cluster xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="types types.xsd cluster cluster.xsd" id="" name="Mode Base" revision="2">
<revisionHistory>
<revision revision="1" summary="Initial version"/>
<revision revision="2" summary="ChangeToModeResponse command: StatusText must be provided for InvalidInMode status"/>
</revisionHistory>
<classification hierarchy="base" role="application" picsCode="MODB" scope="Endpoint"/>
<features>
<feature name="base reserved" summary="This range of bits is reserved for this base cluster">
<optionalConform/>
</feature>
<feature name="derived reserved" summary="This range of bits is reserved for derived clusters">
<optionalConform/>
</feature>
<feature bit="0" code="DEPONOFF" name="OnOff" summary="Dependency with the OnOff cluster">
<optionalConform/>
</feature>
</features>
<dataTypes>
<struct name="ModeOptionStruct">
<field id="0" name="Label" type="string" default="MS">
<access read="true"/>
<mandatoryConform/>
<constraint type="maxLength" value="64"/>
</field>
<field id="1" name="Mode" type="uint8" default="MS">
<access read="true"/>
<mandatoryConform/>
</field>
<field id="2" name="ModeTags" type="list[ModeTagStruct Type]" default="MS">
<access read="true"/>
<mandatoryConform/>
<constraint type="max" value="8"/>
</field>
</struct>
</dataTypes>
<attributes>
<attribute name="base reserved">
<mandatoryConform/>
</attribute>
<attribute name="derived reserved">
<mandatoryConform/>
</attribute>
<attribute id="0x0000" name="SupportedModes" type="list[ModeOptionStruct Type]" default="MS">
<access read="true" readPrivilege="view"/>
<quality changeOmitted="false" nullable="false" scene="false" persistence="fixed" reportable="false"/>
<mandatoryConform/>
<constraint type="between" from="2" to="255"/>
</attribute>
</attributes>
</cluster>
''',
# derived ...
'''
<cluster xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="types types.xsd cluster cluster.xsd" id="0x0059" name="Dishwasher Mode" revision="2">
<revisionHistory>
<revision revision="1" summary="Initial Release"/>
<revision revision="2" summary="ChangeToModeResponse command: StatusText must be provided for InvalidInMode status"/>
</revisionHistory>
<classification hierarchy="derived" baseCluster="Mode Base" role="application" picsCode="DISHM" scope="Endpoint"/>
<dataTypes>
<struct name="ModeOptionStruct">
<field id="0" name="Label">
<mandatoryConform/>
</field>
<field id="1" name="Mode">
<mandatoryConform/>
</field>
<field id="2" name="ModeTags">
<mandatoryConform/>
<constraint type="between" from="1" to="8"/>
</field>
</struct>
</dataTypes>
<attributes>
<attribute id="0x0000" name="SupportedModes">
<mandatoryConform/>
</attribute>
</attributes>
</cluster>
''',
])
expected_idl = IdlTextToIdl('''
client cluster DishwasherMode = 89 {
revision 2;
bitmap Feature: bitmap32 {
kOnOff = 0x1;
}
struct ModeOptionStruct {
char_string<64> label = 0;
int8u mode = 1;
ModeTagStruct modeTags[] = 2;
}
readonly attribute ModeOptionStruct supportedModes[] = 0;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
def testSignedTypes(self):
xml_idl = XmlToIdl('''
<cluster id="123" name="Test" revision="1">
<attributes>
<attribute id="0x0000" name="First" type="int16">
<access read="true" readPrivilege="view"/>
<mandatoryConform/>
</attribute>
<attribute id="0x0001" name="Second" type="int16s">
<access read="true" readPrivilege="view"/>
<mandatoryConform/>
</attribute>
<attribute id="0x0002" name="Third" type="int32u">
<access read="true" readPrivilege="view"/>
<mandatoryConform/>
</attribute>
</attributes>
</cluster>
''')
expected_idl = IdlTextToIdl('''
client cluster Test = 123 {
readonly attribute int16s first = 0;
readonly attribute int16s second = 1;
readonly attribute int32u third = 2;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
def testEnumRange(self):
# Check heuristic for enum ranges
xml_idl = XmlToIdl('''
<cluster id="123" name="Test" revision="1">
<dataTypes>
<bitmap name="Basic">
<bitfield name="One" bit="0">
<mandatoryConform/>
</bitfield>
<bitfield name="Two" bit="1">
<mandatoryConform/>
</bitfield>
<bitfield name="Three" bit="2">
<mandatoryConform/>
</bitfield>
</bitmap>
<bitmap name="OneLarge">
<bitfield name="Ten" bit="10">
<mandatoryConform/>
</bitfield>
</bitmap>
<bitmap name="LargeBitmap">
<bitfield name="One" bit="0">
<mandatoryConform/>
</bitfield>
<bitfield name="Ten" bit="10">
<mandatoryConform/>
</bitfield>
<bitfield name="Twenty" bit="20">
<mandatoryConform/>
</bitfield>
</bitmap>
<bitmap name="HugeBitmap">
<bitfield name="Forty" bit="40">
<mandatoryConform/>
</bitfield>
</bitmap>
</dataTypes>
</cluster>
''')
expected_idl = IdlTextToIdl('''
client cluster Test = 123 {
bitmap Basic: bitmap8 {
kOne = 0x01;
kTwo = 0x02;
kThree = 0x04;
}
bitmap OneLarge: bitmap16 {
kTen = 0x400;
}
bitmap LargeBitmap: bitmap32 {
kOne = 0x1;
kTen = 0x400;
kTwenty = 0x100000;
}
bitmap HugeBitmap: bitmap64 {
kForty = 0x10000000000;
}
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
def testAttributes(self):
# Validate an attribute with a type list
# This is a very stripped down version from the original AudioOutput.xml
xml_idl = XmlToIdl('''
<cluster id="123" name="Test" revision="1">
<dataTypes>
<struct name="OutputInfoStruct">
<field id="0" name="Index" type="uint8">
<access read="true" write="true"/>
<mandatoryConform/>
</field>
</struct>
</dataTypes>
<attributes>
<attribute id="0x0000" name="OutputList" type="list[OutputInfoStruct Type]">
<access read="true" readPrivilege="view"/>
<mandatoryConform/>
</attribute>
<attribute id="0x0001" name="TestConform" type="enum8">
<access read="true" readPrivilege="view"/>
<otherwiseConform>
<mandatoryConform>
<feature name="PRSCONST"/>
</mandatoryConform>
<optionalConform>
<feature name="AUTO"/>
</optionalConform>
</otherwiseConform>
</attribute>
</attributes>
</cluster>
''')
expected_idl = IdlTextToIdl('''
client cluster Test = 123 {
struct OutputInfoStruct {
int8u index = 0;
}
readonly attribute OutputInfoStruct outputList[] = 0;
readonly attribute optional enum8 testConform = 1;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
def testXmlNameWorkarounds(self):
# Validate an attribute with a type list
# This is a manually-edited copy of an attribute test (not real data)
xml_idl = XmlToIdl('''
<cluster id="123" name="Test" revision="1">
<dataTypes>
<struct name="OutputInfoStruct">
<field id="0" name="ID" type="<<ref_DataTypeString>>">
<access read="true" write="true"/>
<mandatoryConform/>
</field>
<field id="1" name="items" type="<<ref_DataTypeList>>[uint8]">
<access read="true" write="true"/>
<mandatoryConform/>
</field>
<field id="2" name="endpoints" type="<<ref_DataTypeList>>[<<ref_DataTypeEndpointNumber>> Type]">
<access read="true" write="true"/>
<mandatoryConform/>
</field>
<field id="3" name="NominalPower" type="power-mW">
<access read="true" write="true"/>
<mandatoryConform>
<feature name="PFR"/>
</mandatoryConform>
<constraint type="desc"/>
</field>
<field id="4" name="MaximumEnergy" type="energy-mWh">
<access read="true" write="true"/>
<mandatoryConform>
<feature name="PFR"/>
</mandatoryConform>
</field>
</struct>
</dataTypes>
<attributes>
<attribute id="0x0000" name="OutputList" type="list[OutputInfoStruct Type]">
<access read="true" readPrivilege="view"/>
<mandatoryConform/>
</attribute>
<attribute id="0x0001" name="TestConform" type="enum8">
<access read="true" readPrivilege="view"/>
<otherwiseConform>
<mandatoryConform>
<feature name="PRSCONST"/>
</mandatoryConform>
<optionalConform>
<feature name="AUTO"/>
</optionalConform>
</otherwiseConform>
</attribute>
</attributes>
</cluster>
''')
expected_idl = IdlTextToIdl('''
client cluster Test = 123 {
struct OutputInfoStruct {
char_string id = 0;
int8u items[] = 1;
endpoint_no endpoints[] = 2;
optional power_mw nominalPower = 3;
optional energy_mwh maximumEnergy = 4;
}
readonly attribute OutputInfoStruct outputList[] = 0;
readonly attribute optional enum8 testConform = 1;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
def testComplexInput(self):
# This parses a known copy of Switch.xml which happens to be fully
# spec-conformant (so assuming it as a good input)
xml_idl = XmlToIdl('''
<cluster xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="types types.xsd cluster cluster.xsd" id="0x003b" name="Switch" revision="1">
<revisionHistory>
<revision revision="1" summary="Initial Release"/>
</revisionHistory>
<classification hierarchy="base" role="application" picsCode="SWTCH" scope="Endpoint"/>
<features>
<feature bit="0" code="LS" name="LatchingSwitch" summary="Switch is latching">
<optionalConform choice="a"/>
</feature>
<feature bit="1" code="MS" name="MomentarySwitch" summary="Switch is momentary">
<optionalConform choice="a"/>
</feature>
<feature bit="2" code="MSR" name="MomentarySwitchRelease" summary="Switch supports release">
<optionalConform>
<feature name="MS"/>
</optionalConform>
</feature>
<feature bit="3" code="MSL" name="MomentarySwitchLongPress" summary="Switch supports long press">
<optionalConform>
<andTerm>
<feature name="MS"/>
<feature name="MSR"/>
</andTerm>
</optionalConform>
</feature>
<feature bit="4" code="MSM" name="MomentarySwitchMultiPress" summary="Switch supports multi-press">
<optionalConform>
<andTerm>
<feature name="MS"/>
<feature name="MSR"/>
</andTerm>
</optionalConform>
</feature>
</features>
<attributes>
<attribute id="0x0000" name="NumberOfPositions" type="uint8" default="2">
<access read="true" readPrivilege="view"/>
<quality changeOmitted="false" nullable="false" scene="false" persistence="fixed" reportable="false"/>
<mandatoryConform/>
<constraint type="between" from="2" to="max"/>
</attribute>
<attribute id="0x0001" name="CurrentPosition" type="uint8" default="0">
<access read="true" readPrivilege="view"/>
<quality changeOmitted="false" nullable="false" scene="false" persistence="nonVolatile" reportable="false"/>
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</attribute>
<attribute id="0x0002" name="MultiPressMax" type="uint8" default="2">
<access read="true" readPrivilege="view"/>
<quality changeOmitted="false" nullable="false" scene="false" persistence="fixed" reportable="false"/>
<mandatoryConform>
<feature name="MSM"/>
</mandatoryConform>
<constraint type="between" from="2" to="max"/>
</attribute>
</attributes>
<events>
<event id="0x00" name="SwitchLatched" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="LS"/>
</mandatoryConform>
<field id="0" name="NewPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
</event>
<event id="0x01" name="InitialPress" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="MS"/>
</mandatoryConform>
<field id="0" name="NewPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
</event>
<event id="0x02" name="LongPress" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="MSL"/>
</mandatoryConform>
<field id="0" name="NewPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
</event>
<event id="0x03" name="ShortRelease" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="MSR"/>
</mandatoryConform>
<field id="0" name="PreviousPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
</event>
<event id="0x04" name="LongRelease" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="MSL"/>
</mandatoryConform>
<field id="0" name="PreviousPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
</event>
<event id="0x05" name="MultiPressOngoing" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="MSM"/>
</mandatoryConform>
<field id="0" name="NewPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
<field id="1" name="CurrentNumberOfPressesCounted" type="uint8">
<mandatoryConform/>
<constraint type="between" from="2" to="MultiPressMax"/>
</field>
</event>
<event id="0x06" name="MultiPressComplete" priority="info">
<access readPrivilege="view"/>
<mandatoryConform>
<feature name="MSM"/>
</mandatoryConform>
<field id="0" name="PreviousPosition" type="uint8">
<mandatoryConform/>
<constraint type="between" from="0" to="NumberOfPositions-1"/>
</field>
<field id="1" name="TotalNumberOfPressesCounted" type="uint8">
<mandatoryConform/>
<constraint type="between" from="1" to="MultiPressMax"/>
</field>
</event>
</events>
</cluster>
''')
expected_idl = IdlTextToIdl('''
client cluster Switch = 59 {
bitmap Feature : bitmap32 {
kLatchingSwitch = 0x1;
kMomentarySwitch = 0x2;
kMomentarySwitchRelease = 0x4;
kMomentarySwitchLongPress = 0x8;
kMomentarySwitchMultiPress = 0x10;
}
info event SwitchLatched = 0 {
int8u newPosition = 0;
}
info event InitialPress = 1 {
int8u newPosition = 0;
}
info event LongPress = 2 {
int8u newPosition = 0;
}
info event ShortRelease = 3 {
int8u previousPosition = 0;
}
info event LongRelease = 4 {
int8u previousPosition = 0;
}
info event MultiPressOngoing = 5 {
int8u newPosition = 0;
int8u currentNumberOfPressesCounted = 1;
}
info event MultiPressComplete = 6 {
int8u previousPosition = 0;
int8u totalNumberOfPressesCounted = 1;
}
readonly attribute int8u numberOfPositions = 0;
readonly attribute int8u currentPosition = 1;
readonly attribute optional int8u multiPressMax = 2;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')
self.assertIdlEqual(xml_idl, expected_idl)
if __name__ == '__main__':
unittest.main()