Coverage for d7a/system_files/engineering_mode.py: 79%
56 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 enum import Enum
23from d7a.phy.channel_id import ChannelID
24from d7a.support.schema import Validatable, Types
25from d7a.system_files.file import File
26from d7a.system_files.system_file_ids import SystemFileIds
27from d7a.phy.channel_header import ChannelHeader,ChannelCoding,ChannelClass,ChannelBand
29class EngineeringModeMode(Enum):
30 ENGINEERING_MODE_MODE_OFF = 0
31 ENGINEERING_MODE_MODE_CONT_TX = 1
32 ENGINEERING_MODE_MODE_TRANSIENT_TX = 2
33 ENGINEERING_MODE_MODE_PER_RX = 3
34 ENGINEERING_MODE_MODE_PER_TX = 4
36 @staticmethod
37 def from_string(s):
38 if s == "OFF": return EngineeringModeMode.ENGINEERING_MODE_MODE_OFF
39 if s == "CONT_TX": return EngineeringModeMode.ENGINEERING_MODE_MODE_CONT_TX
40 if s == "TRANSIENT_TX": return EngineeringModeMode.ENGINEERING_MODE_MODE_TRANSIENT_TX
41 if s == "PER_RX": return EngineeringModeMode.ENGINEERING_MODE_MODE_PER_RX
42 if s == "PER_TX": return EngineeringModeMode.ENGINEERING_MODE_MODE_PER_TX
44 raise NotImplementedError
46 def __str__(self):
47 if self.value == EngineeringModeMode.ENGINEERING_MODE_MODE_OFF.value: return "OFF"
48 if self.value == EngineeringModeMode.ENGINEERING_MODE_MODE_CONT_TX.value: return "CONT_TX"
49 if self.value == EngineeringModeMode.ENGINEERING_MODE_MODE_TRANSIENT_TX.value: return "TRANSIENT_TX"
50 if self.value == EngineeringModeMode.ENGINEERING_MODE_MODE_PER_RX.value: return "PER_RX"
51 if self.value == EngineeringModeMode.ENGINEERING_MODE_MODE_PER_TX.value: return "PER_TX"
53class EngineeringModeFile(File, Validatable):
55 SCHEMA = [{
56 "mode": Types.ENUM(EngineeringModeMode),
57 "flags": Types.INTEGER(min=0, max=255),
58 "timeout": Types.INTEGER(min=0, max=255),
59 "channel_id": Types.OBJECT(ChannelID),
60 "eirp": Types.INTEGER(min=-128, max=127)
61 }]
63 def __init__(self, mode=EngineeringModeMode.ENGINEERING_MODE_MODE_OFF, flags=0, timeout=0,
64 channel_id=ChannelID(channel_header=ChannelHeader(ChannelCoding.PN9,ChannelClass.LO_RATE,ChannelBand.BAND_868),
65 channel_index=0),
66 eirp=0):
67 self.mode = mode
68 self.flags = flags
69 self.timeout = timeout
70 self.channel_id = channel_id
71 self.eirp = eirp
72 File.__init__(self, SystemFileIds.ENGINEERING_MODE.value, 9)
73 Validatable.__init__(self)
75 @staticmethod
76 def parse(s, offset=0, length=9):
77 mode = EngineeringModeMode(int(s.read("uint:8")))
78 flags = s.read("uint:8")
79 timeout = s.read("uint:8")
80 channel_id= ChannelID.parse(s)
81 eirp = s.read("int:8")
82 return EngineeringModeFile(mode=mode, flags=flags, timeout=timeout, channel_id=channel_id, eirp=eirp)
84 def __iter__(self):
85 yield int(self.mode.value)
86 yield self.flags
87 yield self.timeout
88 for byte in self.channel_id:
89 yield byte
91 for byte in bytearray(struct.pack(">b", self.eirp)): yield byte
92 yield 0
93 yield 0
95 def __str__(self):
96 return "mode={}, flags={}, timeout={}, channel_id={{{}}}, eirp={}".format(self.mode, hex(self.flags),self.timeout, self.channel_id, self.eirp)