Coverage for test/d7a/alp/test_command.py: 94%

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

19 

20# unit tests for the D7 ALP command byte generation 

21 

22import unittest 

23from d7a.alp.operations.status import InterfaceStatus 

24from d7a.alp.parser import Parser 

25from d7a.alp.command import Command 

26from d7a.alp.action import Action 

27from d7a.alp.operations.responses import ReturnFileData 

28from d7a.alp.operands.file import Data 

29from d7a.alp.operands.offset import Offset 

30from d7a.alp.regular_action import RegularAction 

31from d7a.alp.status_action import StatusAction, StatusActionOperandExtensions 

32from d7a.d7anp.addressee import Addressee 

33from d7a.phy.channel_header import ChannelHeader, ChannelBand, ChannelClass, ChannelCoding 

34from d7a.phy.channel_id import ChannelID 

35from d7a.sp.status import Status as D7ASpStatus 

36from d7a.alp.operands.interface_status import InterfaceStatusOperand 

37from d7a.types.ct import CT 

38 

39 

40class TestCommand(unittest.TestCase): 

41 def setUp(self): 

42 self.parser = Parser() 

43 

44 def test_simple_received_return_file_data_command(self): 

45 cmd = Command( 

46 generate_tag_request_action=False, 

47 actions=[ 

48 RegularAction( 

49 operation=ReturnFileData( 

50 operand=Data( 

51 data=bytearray("Hello world", "utf-8"), 

52 offset=Offset(id=0x51) 

53 ) 

54 ) 

55 ), 

56 StatusAction( 

57 status_operand_extension=StatusActionOperandExtensions.INTERFACE_STATUS, 

58 operation=InterfaceStatus( 

59 operand=InterfaceStatusOperand( 

60 interface_id=0xD7, 

61 interface_status=D7ASpStatus( 

62 channel_id=ChannelID( 

63 channel_header=ChannelHeader(channel_band=ChannelBand.BAND_433, 

64 channel_class=ChannelClass.LO_RATE, 

65 channel_coding=ChannelCoding.PN9), 

66 channel_index=16 

67 ), 

68 rx_level=70, 

69 link_budget=80, 

70 target_rx_level=80, 

71 nls=False, 

72 missed=False, 

73 retry=False, 

74 unicast=False, 

75 fifo_token=200, 

76 seq_nr=0, 

77 response_to=CT(mant=20), 

78 addressee=Addressee() 

79 ) 

80 ) 

81 ) 

82 ) 

83 ] 

84 ) 

85 expected = [ 

86 0x62, # Interface Status action 

87 0xD7, # D7ASP interface 

88 32, # channel header 

89 0, 16, # channel_id 

90 70, # rxlevel (- dBm) 

91 80, # link budget 

92 80, # target rx level 

93 0, # status 

94 200, # fifo token 

95 0, # seq 

96 20, # response timeout 

97 0x10, # addressee ctrl (NOID) 

98 0, # access class 

99 0x20, # action=32/ReturnFileData 

100 0x51, # File ID 

101 0x00, # offset 

102 0x0b, # length 

103 0x48, 0x65, 0x6c, 0x6c, 0x6f, # Hello 

104 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 # World 

105 ] 

106 cmdBytes = [cmdByte for cmdByte in cmd] 

107 self.assertEqual(cmdBytes, expected) 

108 

109 

110 def test_simple_received_return_file_data_command_with_tag_request(self): 

111 cmd = Command( 

112 tag_id=25, 

113 actions=[ 

114 RegularAction( 

115 operation=ReturnFileData( 

116 operand=Data( 

117 data=list(bytearray("Hello world", "utf-8")), 

118 offset=Offset(id=0x51) 

119 ) 

120 ) 

121 ), 

122 StatusAction( 

123 status_operand_extension=StatusActionOperandExtensions.INTERFACE_STATUS, 

124 operation=InterfaceStatus( 

125 operand=InterfaceStatusOperand( 

126 interface_id=0xD7, 

127 interface_status=D7ASpStatus( 

128 channel_id=ChannelID( 

129 channel_header=ChannelHeader(channel_band=ChannelBand.BAND_433, 

130 channel_class=ChannelClass.LO_RATE, 

131 channel_coding=ChannelCoding.PN9), 

132 channel_index=16 

133 ), 

134 rx_level=70, 

135 link_budget=80, 

136 target_rx_level=80, 

137 nls=False, 

138 missed=False, 

139 retry=False, 

140 unicast=False, 

141 fifo_token=200, 

142 seq_nr=0, 

143 response_to=CT(mant=20), 

144 addressee=Addressee() 

145 ) 

146 ) 

147 ) 

148 ) 

149 ] 

150 ) 

151 expected = [ 

152 0xB4, # tag request with send response bit set 

153 25, # tag ID 

154 0x62, # Interface Status action 

155 0xD7, # D7ASP interface 

156 32, # channel header 

157 0, 16, # channel_id 

158 70, # rxlevel (- dBm) 

159 80, # link budget 

160 80, # target rx level 

161 0, # status 

162 200, # fifo token 

163 0, # seq 

164 20, # response timeout 

165 0x10, # addressee ctrl (NOID) 

166 0, # access class 

167 0x20, # action=32/ReturnFileData 

168 0x51, # File ID 

169 0x00, # offset 

170 0x0b, # length 

171 0x48, 0x65, 0x6c, 0x6c, 0x6f, # Hello 

172 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 # World 

173 ] 

174 cmdBytes = [cmdByte for cmdByte in cmd] 

175 self.assertEqual(cmdBytes, expected) 

176 

177 

178if __name__ == '__main__': 

179 suite = unittest.TestLoader().loadTestsFromTestCase(TestCommand) 

180 unittest.TextTestRunner(verbosity=2).run(suite)