Coverage for d7a/d7anp/frame.py: 78%

36 statements  

« 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# 

19from d7a.d7anp.addressee import NlsMethod, IdType 

20from d7a.support.schema import Validatable, Types 

21from d7a.d7anp.control import Control 

22from d7a.d7atp.frame import Frame as D7atpFrame 

23 

24class Frame(Validatable): 

25 

26 SCHEMA = [{ 

27 "control": Types.OBJECT(Control), 

28 "origin_access_class": Types.BYTE(), 

29 "origin_access_id": Types.BYTES(), # TODO refactor to use OriginAddressee (subclass of addressee containing control and access_id) 

30 "d7atp_frame": Types.OBJECT(D7atpFrame) 

31 }] 

32 

33 def __init__(self, control, origin_access_class, origin_access_id, d7atp_frame): 

34 self.control = control 

35 self.origin_access_class = origin_access_class 

36 self.origin_access_id = origin_access_id 

37 self.d7atp_frame = d7atp_frame # TODO 

38 super(Frame, self).__init__() 

39 

40 @staticmethod 

41 def parse(bitstream, payload_length): 

42 control = Control.parse(bitstream) 

43 payload_length -= 1 # substract control 

44 

45 origin_access_class = bitstream.read("uint:8") 

46 payload_length -= 1 

47 

48 assert control.has_hopping == False, "Not implemented yet" 

49 assert control.nls_method == NlsMethod.NONE, "Not implemented yet" 

50 

51 if not control.has_no_origin_access_id: 

52 if control.origin_id_type == IdType.VID: 

53 origin_access_id = list(map(ord, bitstream.read("bytes:2"))) 

54 payload_length = payload_length - 2 

55 elif control.origin_id_type == IdType.UID: 

56 origin_access_id = list(bitstream.read("bytes:8")) 

57 payload_length = payload_length - 8 

58 else: 

59 assert False 

60 else: 

61 origin_access_id = [] 

62 

63 #payload=map(ord,bitstream.read("bytes:" + str(payload_length))) 

64 d7atp_frame = D7atpFrame.parse(bitstream, payload_length) 

65 return Frame(control=control, origin_access_class=origin_access_class, origin_access_id=origin_access_id, d7atp_frame=d7atp_frame) 

66 

67 def __iter__(self): 

68 for byte in self.control: yield byte 

69 yield self.origin_access_class 

70 for byte in self.origin_access_id: yield byte 

71 for byte in self.d7atp_frame: yield byte