Coverage for test/d7a/dll/test_dll_parser.py: 100%
101 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
1#
2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV.
3#
4# This file is part of pyd7a.
5# See https://github.com/Sub-IoT/pyd7a for further info.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19import unittest
20from d7a.alp.operands.file import DataRequest, Data
21from d7a.alp.operations.requests import ReadFileData
22from d7a.alp.operations.responses import ReturnFileData
23from d7a.d7anp.addressee import IdType, NlsMethod
25from d7a.dll.parser import Parser, FrameType
26from d7a.support.Crc import calculate_crc
27from d7a.types.ct import CT
30class TestForegroundFrameParser(unittest.TestCase):
31 def setUp(self):
32 self.parser = Parser(FrameType.FOREGROUND)
34 def test_read_id_command_frame(self):
35 read_id_command = [
36 0x15, # length
37 0x00, # subnet
38 0x6a, # DLL control
39 0x20, # D7ANP control
40 0x01,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, # Origin Access ID
42 0xa8, # D7ATP control
43 0xe9, # dialog ID
44 0x00, # transaction ID
45 0x05, # Tl
46 0x05, # Tc
47 0x01, # ALP control (read file data operation)
48 0x00, 0x00, 0x08, # file data request operand (file ID 0x00)
49 0x25, 0xDA # CRC
50 ]
52 (frameType, frames, info) = self.parser.parse(read_id_command)
53 self.assertEqual(len(frames), 1)
54 self.assertEqual(frameType, FrameType.FOREGROUND)
55 frame = frames[0]
56 self.assertEqual(frame.length, 21)
57 self.assertEqual(frame.subnet, 0)
58 self.assertEqual(frame.control.id_type, IdType.NOID)
59 self.assertEqual(frame.control.eirp_index, 42)
60 self.assertEqual(len(frame.target_address), 0)
61 self.assertEqual(frame.d7anp_frame.control.nls_method, NlsMethod.NONE)
62 self.assertFalse(frame.d7anp_frame.control.has_hopping)
63 self.assertFalse(frame.d7anp_frame.control.has_no_origin_access_id)
64 self.assertEqual(frame.d7anp_frame.control.origin_id_type, IdType.UID)
65 self.assertEqual(frame.d7anp_frame.origin_access_class, 0x01)
66 self.assertEqual(frame.d7anp_frame.origin_access_id, [0, 0, 0, 0, 0, 0, 0, 1])
67 self.assertTrue(frame.d7anp_frame.d7atp_frame.control.is_dialog_start)
68 self.assertTrue(frame.d7anp_frame.d7atp_frame.control.has_tl)
69 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.has_te)
70 self.assertTrue(frame.d7anp_frame.d7atp_frame.control.is_ack_requested)
71 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.is_ack_not_void)
72 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.is_ack_record_requested)
73 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.has_agc)
74 self.assertEqual(frame.d7anp_frame.d7atp_frame.dialog_id, 0xe9)
75 self.assertEqual(frame.d7anp_frame.d7atp_frame.transaction_id, 0)
76 self.assertEqual(frame.d7anp_frame.d7atp_frame.tl.exp, CT(exp=0, mant=5).exp)
77 self.assertEqual(frame.d7anp_frame.d7atp_frame.tl.mant, CT(exp=0, mant=5).mant)
78 self.assertEqual(frame.d7anp_frame.d7atp_frame.tc.exp, CT(exp=0, mant=5).exp)
79 self.assertEqual(frame.d7anp_frame.d7atp_frame.tc.mant, CT(exp=0, mant=5).mant)
80 self.assertEqual(len(frame.d7anp_frame.d7atp_frame.alp_command.actions), 1)
81 alp_action = frame.d7anp_frame.d7atp_frame.alp_command.actions[0]
82 self.assertEqual(type(alp_action.operation), ReadFileData)
83 self.assertEqual(type(alp_action.operand), DataRequest)
84 self.assertEqual(alp_action.operand.offset.id, 0)
85 self.assertEqual(alp_action.operand.offset.offset.value, 0)
86 self.assertEqual(alp_action.operand.length, 8)
87 # TODO self.assertEqual(len(frame.payload), 16)
88 crc = calculate_crc(bytearray(read_id_command[:-2]))
89 self.assertEqual(frame.crc16, (crc[0] << 8) + crc[1])
91 # TODO tmp
92 def test_read_id_response_frame(self):
93 frame_data = [ 0x25, # length
94 0x00, # subnet
95 0x80, # dll control
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, # target_address
97 0x20, # D7ANP control
98 0x01, # origin access class
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, # origin access ID
100 0xa8, # D7ATP control
101 0xe9, # dialog ID
102 0x00, # transaction ID
103 0x05, # Tl
104 0x05, # Tc
105 0x20, # ALP control (return file data operation)
106 0x00, 0x00, 0x08, # file data operand
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, # UID
108 ]
110 frame_data = frame_data + calculate_crc(bytearray(frame_data))
112 (frameType, frames, info) = self.parser.parse(frame_data)
113 self.assertEqual(len(frames), 1)
114 self.assertEqual(frameType, FrameType.FOREGROUND)
115 frame = frames[0]
116 self.assertEqual(frame.length, 37)
117 self.assertEqual(frame.subnet, 0)
118 self.assertEqual(frame.control.id_type, IdType.UID)
119 self.assertEqual(frame.control.eirp_index, 0)
120 self.assertEqual(len(frame.target_address), 8)
121 self.assertEqual(frame.target_address, [0, 0, 0, 0, 0, 0, 0, 1])
122 self.assertEqual(frame.d7anp_frame.control.nls_method, NlsMethod.NONE)
123 self.assertFalse(frame.d7anp_frame.control.has_hopping)
124 self.assertFalse(frame.d7anp_frame.control.has_no_origin_access_id)
125 self.assertEqual(frame.d7anp_frame.control.origin_id_type, IdType.UID)
126 self.assertEqual(frame.d7anp_frame.origin_access_class, 0x01)
127 self.assertEqual(frame.d7anp_frame.origin_access_id, [0, 0, 0, 0, 0, 0, 0, 2])
128 self.assertTrue(frame.d7anp_frame.d7atp_frame.control.is_dialog_start)
129 self.assertTrue(frame.d7anp_frame.d7atp_frame.control.has_tl)
130 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.has_te)
131 self.assertTrue(frame.d7anp_frame.d7atp_frame.control.is_ack_requested)
132 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.is_ack_not_void)
133 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.is_ack_record_requested)
134 self.assertFalse(frame.d7anp_frame.d7atp_frame.control.has_agc)
135 self.assertEqual(frame.d7anp_frame.d7atp_frame.dialog_id, 0xe9)
136 self.assertEqual(frame.d7anp_frame.d7atp_frame.transaction_id, 0)
137 self.assertEqual(frame.d7anp_frame.d7atp_frame.tl.exp, CT(exp=0, mant=5).exp)
138 self.assertEqual(frame.d7anp_frame.d7atp_frame.tl.mant, CT(exp=0, mant=5).mant)
139 self.assertEqual(frame.d7anp_frame.d7atp_frame.tc.exp, CT(exp=0, mant=5).exp)
140 self.assertEqual(frame.d7anp_frame.d7atp_frame.tc.mant, CT(exp=0, mant=5).mant)
141 self.assertEqual(len(frame.d7anp_frame.d7atp_frame.alp_command.actions), 1)
142 alp_action = frame.d7anp_frame.d7atp_frame.alp_command.actions[0]
143 self.assertEqual(type(alp_action.operation), ReturnFileData)
144 self.assertEqual(type(alp_action.operand), Data)
145 self.assertEqual(alp_action.operand.offset.id, 0)
146 self.assertEqual(alp_action.operand.offset.offset.value, 0)
147 self.assertEqual(alp_action.operand.length, 8)
149class TestBackgroundFrameParser(unittest.TestCase):
150 def setUp(self):
151 self.parser = Parser(FrameType.BACKGROUND)
153 def test_background_frame(self):
154 frame = [
155 0x01, # subnet
156 0x80, # control
157 0x00, # payload
158 0x01, # payload
159 0x25, 0xDA # CRC
160 ]
162 (frameType, frames, info) = self.parser.parse(frame)
163 self.assertEqual(len(frames), 1)
164 self.assertEqual(frameType, FrameType.BACKGROUND)
165 self.assertEqual(frames[0].control.id_type, IdType.UID)
166 self.assertEqual(frames[0].control.tag, 0)
167 self.assertEqual(frames[0].payload, 1)
168 self.assertEqual(frames[0].crc16, 9690)