Coverage for d7a/system_files/phy_status.py: 43%
54 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 struct
21from d7a.support.schema import Validatable, Types
22from d7a.system_files.file import File
23from d7a.system_files.system_file_ids import SystemFileIds
24from d7a.phy.channel_status_identifier import ChannelStatusIdentifier
26class PhyStatusFile(File, Validatable):
27 SCHEMA = [{
28 "up_time": Types.INTEGER(min=0, max=0xFFFFFFFF),
29 "rx_time": Types.INTEGER(min=0, max=0xFFFFFFFF),
30 "tx_time": Types.INTEGER(min=0, max=0xFFFFFFFF),
31 "tx_duty_cycle": Types.INTEGER(min=0, max=1000),
32 "channel_status_list_length": Types.INTEGER(min=0, max=0xFF),
33 "channel_status_identifier": Types.LIST(ChannelStatusIdentifier, minlength=0, maxlength=0xFF),
34 "channel_noise_floor": Types.LIST(minlength=0, maxlength=0xFF)
35 }]
37 def __init__(self, up_time=0, rx_time=0, tx_time=0, tx_duty_cycle=0, channel_status_list_length=0, channel_status_identifier=[], channel_noise_floor=[]):
38 self.up_time = up_time
39 self.rx_time = rx_time
40 self.tx_time = tx_time
41 self.tx_duty_cycle = tx_duty_cycle
42 self.channel_status_list_length = channel_status_list_length
44 self.channel_status_identifier = channel_status_identifier
45 if len(channel_status_identifier) != channel_status_list_length:
46 self.channel_status_identifier.extend([ChannelStatusIdentifier()] * (channel_status_list_length - len(channel_status_identifier)))
48 self.channel_noise_floor = channel_noise_floor
49 if len(channel_noise_floor) != channel_status_list_length:
50 self.channel_noise_floor.extend([0] * (channel_status_list_length - len(channel_noise_floor)))
52 File.__init__(self, SystemFileIds.PHY_STATUS.value, 15 + (3 * 10)) # allocate enough space for 20 channels
53 Validatable.__init__(self)
55 @staticmethod
56 def parse(s, offset=0, length=15 + (3 * 10)):
57 up_time = s.read("uint:32")
58 rx_time = s.read("uint:32")
59 tx_time = s.read("uint:32")
60 tx_duty_cycle = s.read("uint:16")
61 channel_status_list_length = s.read("uint:8")
62 channel_status_identifier = []
63 channel_noise_floor = []
64 for counter in range(channel_status_list_length):
65 channel_status_identifier.append(ChannelStatusIdentifier().parse(s))
66 channel_noise_floor.append(s.read("uint:8"))
68 return PhyStatusFile(up_time=up_time, rx_time=rx_time, tx_time=tx_time, tx_duty_cycle=tx_duty_cycle,
69 channel_status_list_length=channel_status_list_length,
70 channel_status_identifier=channel_status_identifier, channel_noise_floor=channel_noise_floor)
72 def __iter__(self):
73 for byte in bytearray(struct.pack(">I", self.up_time)):
74 yield byte
75 for byte in bytearray(struct.pack(">I", self.rx_time)):
76 yield byte
77 for byte in bytearray(struct.pack(">I", self.tx_time)):
78 yield byte
79 for byte in bytearray(struct.pack(">H", self.tx_duty_cycle)):
80 yield byte
81 yield self.channel_status_list_length
82 for counter in range(self.channel_status_list_length):
83 for byte in self.channel_status_identifier[counter]:
84 yield byte
85 yield self.channel_noise_floor[counter]
87 def __str__(self):
88 channel_status = ""
89 for counter in range(self.channel_status_list_length):
90 channel_status = channel_status + "identifier={}, noise_floor={}; ".format(str(self.channel_status_identifier[counter]), self.channel_noise_floor[counter])
91 channel_status = "[{}]".format(channel_status[:-2])
93 return "up_time={}, rx_time={}, tx_time={}, tx_duty_cycle={}, channel_status_list_length={}, list={}".format(self.up_time, self.rx_time, self.tx_time, self.tx_duty_cycle, self.channel_status_list_length, channel_status)