Coverage for d7a/dll/foreground_frame.py: 76%
38 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 pprint
21from d7a.d7anp.addressee import IdType
22from d7a.support.schema import Validatable, Types
23from d7a.dll.foreground_frame_control import ForegroundFrameControl
24from d7a.d7anp.frame import Frame as D7anpFrame
27class ForegroundFrame(Validatable):
29 SCHEMA = [{
30 "length": Types.BYTE(),
31 "subnet": Types.BYTE(),
32 "control": Types.OBJECT(ForegroundFrameControl),
33 "target_address": Types.BYTES(), # TODO max size?
34 "d7anp_frame": Types.OBJECT(D7anpFrame), # TODO assuming foreground frames for now
35 "crc16" : Types.BITS(16) # TODO does not work, look into this later {'validator': validate_crc }
36 }]
38 def __init__(self, length, subnet, control, target_address, d7anp_frame, crc16):
39 self.length = length
40 self.subnet = subnet
41 self.control = control
42 self.target_address = target_address
43 self.d7anp_frame = d7anp_frame
44 self.crc16 = crc16
45 # TODO validate CRC
47 super(ForegroundFrame, self).__init__()
49 # def validate_crc(self, value, error):
50 # raw_data = []
51 # raw_data.append(self.length)
52 # raw_data.append(self.subnet)
53 # raw_data.append(self.control)
54 # raw_data.append(self.target_address)
55 # raw_data.append(self.payload)
56 # crc = calculate_crc(raw_data)
59 @staticmethod
60 def parse(s):
61 length = s.read("int:8")
62 subnet = s.read("int:8")
63 control = ForegroundFrameControl.parse(s)
64 payload_length = length - 4 # substract subnet, control, crc
65 if control.id_type == IdType.VID:
66 target_address = list(s.read("bytes:2"))
67 payload_length = payload_length - 2
68 elif control.id_type == IdType.UID:
69 target_address = list(s.read("bytes:8"))
70 payload_length = payload_length - 8
71 else:
72 target_address = []
74 return ForegroundFrame(
75 length=length,
76 subnet=subnet,
77 control=control,
78 target_address=target_address,
79 d7anp_frame=D7anpFrame.parse(s, payload_length),
80 crc16=s.read("uint:16")
81 )
84 def __iter__(self):
85 yield self.length
86 yield self.subnet
87 for byte in self.control: yield byte
88 for byte in self.target_address: yield byte
89 for byte in self.d7anp_frame: yield byte
90 yield self.crc16
92 def __str__(self):
93 return pprint.PrettyPrinter().pformat(self.as_dict())