Coverage for d7a/dll/background_frame.py: 81%
27 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.background_frame_control import BackgroundFrameControl
24from d7a.d7anp.frame import Frame as D7anpFrame
27class BackgroundFrame(Validatable):
29 SCHEMA = [{
30 "subnet": Types.BYTE(),
31 "control": Types.OBJECT(BackgroundFrameControl),
32 "payload": Types.BITS(16),
33 "crc16" : Types.BITS(16) # TODO does not work, look into this later {'validator': validate_crc }
34 }]
36 def __init__(self, subnet, control, payload, crc16):
37 self.subnet = subnet
38 self.control = control
39 self.payload = payload
40 self.crc16 = crc16
41 # TODO validate CRC
43 super(BackgroundFrame, self).__init__()
45 # def validate_crc(self, value, error):
46 # raw_data = []
47 # raw_data.append(self.length)
48 # raw_data.append(self.subnet)
49 # raw_data.append(self.control)
50 # raw_data.append(self.target_address)
51 # raw_data.append(self.payload)
52 # crc = calculate_crc(raw_data)
55 @staticmethod
56 def parse(s):
57 subnet = s.read("int:8")
58 control = BackgroundFrameControl.parse(s)
59 payload = s.read("uint:16")
60 crc = s.read("uint:16")
62 return BackgroundFrame(
63 subnet=subnet,
64 control=control,
65 payload=payload,
66 crc16=crc
67 )
70 def __iter__(self):
71 yield self.subnet
72 for byte in self.control: yield byte
73 for byte in self.payload: yield byte
74 yield self.crc16
76 def __str__(self):
77 return pprint.PrettyPrinter().pformat(self.as_dict())