Coverage for test/d7a/alp/operands/test_lorawan_interface_config.py: 100%

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

19import unittest 

20 

21from bitstring import ConstBitStream 

22 

23from d7a.alp.operands.lorawan_interface_configuration_abp import LoRaWANInterfaceConfigurationABP 

24from d7a.alp.operands.lorawan_interface_configuration_otaa import LoRaWANInterfaceConfigurationOTAA 

25 

26class TestLoRaWANInterfaceConfiguration(unittest.TestCase): 

27 def test_byte_generation(self): 

28 lorawan_config_abp = LoRaWANInterfaceConfigurationABP( 

29 adr_enabled=True, 

30 request_ack=True, 

31 app_port=2, 

32 data_rate=0, 

33 dev_addr=1, 

34 netw_id=2, 

35 ) 

36 lorawan_config_otaa = LoRaWANInterfaceConfigurationOTAA( 

37 adr_enabled=True, 

38 request_ack=True, 

39 app_port=2, 

40 data_rate=0, 

41 ) 

42 

43 

44 bytes = bytearray(lorawan_config_abp) 

45 self.assertEqual(len(bytes), 11) 

46 self.assertEqual(bytes[0], (1 << 1) + (1 << 2)) # control byte 

47 self.assertEqual(bytes[1], 2) # app port 

48 self.assertEqual(bytes[2], 0) # data rate 

49 

50 self.assertEqual(bytes[10:14], bytearray(b'\x02')) # netw id 

51 

52 

53 bytes = bytearray(lorawan_config_otaa) 

54 self.assertEqual(len(bytes), 3) 

55 self.assertEqual(bytes[0], (1 << 1) + (1 << 2)) # control byte 

56 self.assertEqual(bytes[1], 2) # app port 

57 self.assertEqual(bytes[2], 0) # data rate 

58 

59 

60 def test_parsing(self): 

61 bytes_abp = [ 

62 (1 << 1) + (1 << 2), # control byte 

63 2, # app port 

64 0, # data rate 

65 ] 

66 

67 bytes_abp.extend([0, 0, 0, 1]) # dev addr 

68 bytes_abp.extend([0, 0, 0, 2]) # netw id 

69 

70 config_abp = LoRaWANInterfaceConfigurationABP.parse(ConstBitStream(bytes=bytes_abp)) 

71 self.assertEqual(type(config_abp), LoRaWANInterfaceConfigurationABP) 

72 self.assertEqual(config_abp.request_ack, True) 

73 self.assertEqual(config_abp.adr_enabled, True) 

74 self.assertEqual(config_abp.app_port, 2) 

75 self.assertEqual(config_abp.data_rate, 0) 

76 self.assertEqual(config_abp.dev_addr, 1) 

77 self.assertEqual(config_abp.netw_id, 2) 

78 

79 bytes_otaa = [ 

80 (1 << 1) + (1 << 2), # control byte 

81 2, # app port 

82 0, # data rate 

83 ] 

84 

85 config_otaa = LoRaWANInterfaceConfigurationOTAA.parse(ConstBitStream(bytes=bytes_otaa)) 

86 self.assertEqual(type(config_otaa), LoRaWANInterfaceConfigurationOTAA) 

87 self.assertEqual(config_otaa.request_ack, True) 

88 self.assertEqual(config_otaa.adr_enabled, True) 

89 self.assertEqual(config_otaa.app_port, 2) 

90 self.assertEqual(config_otaa.data_rate, 0) 

91