forked from acburigo/python-bgapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcapi-test.py
282 lines (223 loc) · 10.1 KB
/
lcapi-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from bglcapi import (bgapi, lcapi, from_binary)
import unittest
def check_rsp_evt_header(obj, data):
return obj['msg_type'] == data[0] and obj['payload_len'] == data[
1] and obj['msg_class'] == data[2] and obj['msg_id'] == data[3]
class TestLcapiGeneralCommands(unittest.TestCase):
def test_cmd_attention(self):
cmd = b'\x78\x00\x00\x00'
self.assertEqual(cmd, lcapi.general.cmd.attention())
def test_cmd_reset(self):
cmd = b'\x78\x00\x00\x01'
self.assertEqual(cmd, lcapi.general.cmd.reset())
class TestLcapiGeneralResponses(unittest.TestCase):
def test_rsp_attention(self):
rsp = b'\x78\x02\x00\x00\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_reset(self):
rsp = b'\x78\x02\x00\x01\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
class TestLcapiKeyCommands(unittest.TestCase):
def test_cmd_set(self):
cmd = b'\x78\x0A\x01\x00\x08\x08\x01\x02\x03\xFF\xF3\xF7\xF8\x89'
self.assertEqual(
cmd, lcapi.key.cmd.set(0x08, b'\x01\x02\x03\xFF\xF3\xF7\xF8\x89'))
def test_cmd_get(self):
cmd = b'\x78\x01\x01\x01\x0A'
self.assertEqual(cmd, lcapi.key.cmd.get(10))
class TestLcapiKeyResponses(unittest.TestCase):
def test_rsp_set(self):
rsp = b'\x78\x02\x01\x00\x01\x04'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0x0401, obj['payload']['result'])
def test_rsp_get(self):
rsp = b'\x78\x0B\x01\x01\x00\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0x0000, obj['payload']['result'])
self.assertEqual(8, len(obj['payload']['key_value']))
self.assertEqual(b'\x01\x02\x03\x04\x05\x06\x07\x08',
obj['payload']['key_value'])
class TestLcapiDataCommands(unittest.TestCase):
def test_cmd_join_mode(self):
cmd = b'\x78\x01\x02\x00\x01'
self.assertEqual(cmd, lcapi.data.cmd.join_mode(0x01))
def test_cmd_join_status(self):
cmd = b'\x78\x00\x02\x01'
self.assertEqual(cmd, lcapi.data.cmd.join_status())
def test_cmd_join(self):
cmd = b'\x78\x00\x02\x02'
self.assertEqual(cmd, lcapi.data.cmd.join())
def test_cmd_send(self):
cmd = b'\x78\x0B\x02\x03\x02\x01\x08\x01\x02\x03\x04\x05\x06\x07\x08'
self.assertEqual(
cmd,
lcapi.data.cmd.send(0x02, 0x01,
b'\x01\x02\x03\x04\x05\x06\x07\x08'))
def test_cmd_confirm_status(self):
cmd = b'\x78\x00\x02\x04'
self.assertEqual(cmd, lcapi.data.cmd.confirm_status())
def test_cmd_read(self):
cmd = b'\x78\x00\x02\x05'
self.assertEqual(cmd, lcapi.data.cmd.read())
class TestLcapiDataResponse(unittest.TestCase):
def test_rsp_join_mode(self):
rsp = b'\x78\x02\x02\x00\x02\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(2, obj['payload']['result'])
def test_rsp_join_status(self):
rsp = b'\x78\x02\x02\x01\x01\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(1, obj['payload']['result'])
def test_rsp_join(self):
rsp = b'\x78\x02\x02\x02\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_send(self):
rsp = b'\x78\x06\x02\x03\x00\x00\x64\x00\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
self.assertEqual(100, obj['payload']['uplink_counter'])
def test_rsp_confirm_status(self):
rsp = b'\x78\x02\x02\x04\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_read(self):
rsp = b'\x78\x0C\x02\x05\x00\x00\x03\x08\x01\x02\x03\x04\x05\x06\x07\x08'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
self.assertEqual(3, obj['payload']['port'])
self.assertEqual(8, len(obj['payload']['data']))
self.assertEqual(b'\x01\x02\x03\x04\x05\x06\x07\x08',
obj['payload']['data'])
class TestLcapiDataEvent(unittest.TestCase):
def test_evt_join(self):
evt = b'\xF8\x02\x02\x02\x04\x00'
obj, n = from_binary(evt)
self.assertTrue(check_rsp_evt_header(obj, evt))
self.assertEqual(4, obj['payload']['result'])
def test_evt_ack(self):
evt = b'\xF8\x04\x02\x06\x0B\x00\x00\x00'
obj, n = from_binary(evt)
self.assertTrue(check_rsp_evt_header(obj, evt))
self.assertEqual(11, obj['payload']['downlink_counter'])
def test_evt_downlink(self):
evt = b'\xF8\x16\x02\x07\xD7\x01\x00\x00\x07\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'
obj, n = from_binary(evt)
self.assertTrue(check_rsp_evt_header(obj, evt))
self.assertEqual(471, obj['payload']['downlink_counter'])
self.assertEqual(7, obj['payload']['port'])
self.assertEqual(16, len(obj['payload']['data']))
self.assertEqual(
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F',
obj['payload']['data'])
def test_evt_send(self):
evt = b'\xF8\x04\x02\x03\x20\x10\x00\x00'
obj, n = from_binary(evt)
self.assertTrue(check_rsp_evt_header(obj, evt))
self.assertEqual(4128, obj['payload']['uplink_counter'])
class TestLcapiNetCommand(unittest.TestCase):
def test_cmd_adr(self):
cmd = b'\x78\x01\x03\x00\x01'
self.assertEqual(cmd, lcapi.net.cmd.adr(0x01))
def test_cmd_class(self):
cmd = b'\x78\x01\x03\x01\x02'
self.assertEqual(cmd, lcapi.net.cmd.nclass(0x02))
def test_cmd_join_receive_delay_1(self):
cmd = b'\x78\x04\x03\x02\x88\x13\x00\x00'
self.assertEqual(cmd, lcapi.net.cmd.join_receive_delay_1(5000))
def test_cmd_join_receive_delay_2(self):
cmd = b'\x78\x04\x03\x03\x70\x17\x00\x00'
self.assertEqual(cmd, lcapi.net.cmd.join_receive_delay_2(6000))
def test_cmd_receive_delay_1(self):
cmd = b'\x78\x04\x03\x04\x88\x13\x00\x00'
self.assertEqual(cmd, lcapi.net.cmd.receive_delay_1(5000))
def test_cmd_receive_delay_2(self):
cmd = b'\x78\x04\x03\x05\x70\x17\x00\x00'
self.assertEqual(cmd, lcapi.net.cmd.receive_delay_2(6000))
def test_cmd_datarate(self):
cmd = b'\x78\x01\x03\x06\x05'
self.assertEqual(cmd, lcapi.net.cmd.datarate(0x05))
def test_cmd_rx2_channel(self):
cmd = b'\x78\x05\x03\x07\x07\xC0\xCa\x89\x36'
self.assertEqual(cmd, lcapi.net.cmd.rx2_channel(0x07, 915000000))
def test_cmd_pnm(self):
cmd = b'\x78\x01\x03\x08\x01'
self.assertEqual(cmd, lcapi.net.cmd.pnm(0x01))
def test_cmd_tx_power(self):
cmd = b'\x78\x01\x03\x09\x04'
self.assertEqual(cmd, lcapi.net.cmd.tx_power(0x04))
def test_cmd_channel_mask(self):
cmd = b'\x78\x09\x03\x0A\x08\xFF\x00\x00\x00\x00\x00\x00\xFF'
self.assertEqual(
cmd,
lcapi.net.cmd.channel_mask(b'\xFF\x00\x00\x00\x00\x00\x00\xFF'))
class TestLcapiNetResponse(unittest.TestCase):
def test_rsp_adr(self):
rsp = b'\x78\x02\x03\x00\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_class(self):
rsp = b'\x78\x02\x03\x01\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_join_receive_delay_1(self):
rsp = b'\x78\x02\x03\x02\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_join_receive_delay_2(self):
rsp = b'\x78\x02\x03\x03\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_receive_delay_1(self):
rsp = b'\x78\x02\x03\x04\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_receive_delay_2(self):
rsp = b'\x78\x02\x03\x05\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_datarate(self):
rsp = b'\x78\x02\x03\x06\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_rx2_channel(self):
rsp = b'\x78\x02\x03\x07\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_pnm(self):
rsp = b'\x78\x02\x03\x08\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_tx_power(self):
rsp = b'\x78\x02\x03\x09\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
def test_rsp_channel_mask(self):
rsp = b'\x78\x02\x03\x0A\x00\x00'
obj, n = from_binary(rsp)
self.assertTrue(check_rsp_evt_header(obj, rsp))
self.assertEqual(0, obj['payload']['result'])
if __name__ == "__main__":
unittest.main()