Coverage for d7a/phy/channel_id.py: 100%
24 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.phy.channel_header import ChannelHeader
22from d7a.support.schema import Validatable, Types
25class ChannelID(Validatable):
27 SCHEMA = [{
28 "channel_header": Types.OBJECT(ChannelHeader),
29 "channel_index": Types.INTEGER(min=0, max=0xFFFF),
30 }]
32 def __init__(self, channel_header, channel_index):
33 self.channel_header = channel_header
34 self.channel_index = channel_index
35 super(ChannelID, self).__init__()
37 def __iter__(self):
38 for byte in self.channel_header: yield byte
39 for byte in bytearray(struct.pack(">H", self.channel_index)): yield byte
42 @staticmethod
43 def parse(s):
44 channel_header = ChannelHeader.parse(s)
45 channel_index = s.read("uint:16")
46 return ChannelID(channel_header=channel_header, channel_index=channel_index)
48 def __str__(self):
49 return "{0}{1:0>3}".format(self.channel_header, self.channel_index)
51 @staticmethod
52 def from_string(s):
53 channel_header = ChannelHeader.from_string(s[0:5])
54 channel_index = int(s[5:8])
55 return ChannelID(channel_header=channel_header, channel_index=channel_index)