forked from caran/can4python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_caninterface_bcm.py
executable file
·411 lines (322 loc) · 19.9 KB
/
test_caninterface_bcm.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_caninterface_bcm
----------------------------------
Tests for `caninterface_bcm` module.
Notes:
A virtual CAN interface 'vcan' must be enabled for this test. See enable_virtual_can_bus().
Must be run as sudo.
"""
import subprocess
import sys
import time
import unittest
assert sys.version_info >= (3, 4, 0), "Python version 3.4 or later required!"
from can4python import canframe
from can4python import caninterface_bcm
from can4python import exceptions
try:
from .test_caninterface_raw import VIRTUAL_CAN_BUS_NAME, enable_virtual_can_bus, disable_virtual_can_bus
except SystemError: # When running this file directly
from test_caninterface_raw import VIRTUAL_CAN_BUS_NAME, enable_virtual_can_bus, disable_virtual_can_bus
class TestSocketCanBcmInterface(unittest.TestCase):
# Scaffolding #
NUMBER_OF_LOOPS = 10000
FRAME_SENDER_SPACING_MILLISECONDS = 0.1
FRAME_ID_RECEIVE = 4
FRAME_ID_SEND = 1
FRAME_NUMBER_OF_DATABYTES = 8
NONEXISTING_CAN_BUS_NAME = "vcan8"
NONEXISTING_FRAME_ID = 22
def setUp(self):
enable_virtual_can_bus()
self.interface = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.simulated_can_process = None
def tearDown(self):
self.interface.close()
try:
self.simulated_can_process.terminate()
except (AttributeError, ProcessLookupError) as _:
pass
enable_virtual_can_bus()
def start_can_frame_sender(self, interval_milliseconds=FRAME_SENDER_SPACING_MILLISECONDS):
"""Send CAN frames using the cangen command. Unlimited number of frames."""
self.simulated_can_process = subprocess.Popen(["cangen", VIRTUAL_CAN_BUS_NAME,
"-I", str(self.FRAME_ID_RECEIVE),
"-L", str(self.FRAME_NUMBER_OF_DATABYTES),
"-D", "i",
"-g", str(interval_milliseconds)],
shell=False, stderr=subprocess.STDOUT)
# Creation etc #
def testConstructor(self):
a = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(a.interfacename, VIRTUAL_CAN_BUS_NAME)
a.close()
self.assertEqual(a.interfacename, VIRTUAL_CAN_BUS_NAME)
b = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(b.interfacename, VIRTUAL_CAN_BUS_NAME)
b.close()
c = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(c.interfacename, VIRTUAL_CAN_BUS_NAME)
c.close()
d = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(d.interfacename, VIRTUAL_CAN_BUS_NAME)
d.close()
def testConstructorWrongValue(self):
self.assertRaises(exceptions.CanException, caninterface_bcm.SocketCanBcmInterface, VIRTUAL_CAN_BUS_NAME, -1)
self.assertRaises(exceptions.CanException, caninterface_bcm.SocketCanBcmInterface, VIRTUAL_CAN_BUS_NAME, -1.0)
def testConstructorWrongType(self):
self.assertRaises(exceptions.CanException, caninterface_bcm.SocketCanBcmInterface, VIRTUAL_CAN_BUS_NAME, "ABC")
def testConstructorSeveralInterfaces(self):
a = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(a.interfacename, VIRTUAL_CAN_BUS_NAME)
b = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(b.interfacename, VIRTUAL_CAN_BUS_NAME)
c = caninterface_bcm.SocketCanBcmInterface(VIRTUAL_CAN_BUS_NAME, timeout=1.0)
self.assertEqual(c.interfacename, VIRTUAL_CAN_BUS_NAME)
a.close()
b.close()
c.close()
def testCreateNonExistingBus(self):
self.assertRaises(exceptions.CanException,
caninterface_bcm.SocketCanBcmInterface,
self.NONEXISTING_CAN_BUS_NAME, timeout=1.0)
self.assertRaises(exceptions.CanException, caninterface_bcm.SocketCanBcmInterface, 1, 1.0)
def testWriteToInterfacenameAttribute(self):
self.assertRaises(AttributeError, setattr, self.interface, 'interfacename', 'can0')
def testRepr(self):
result = repr(self.interface)
known_result = "SocketCan BCM interface: {}".format(VIRTUAL_CAN_BUS_NAME.strip())
self.assertEqual(result.strip(), known_result.strip())
# Receive #
def testSetupReception(self):
self.interface.setup_reception(self.FRAME_ID_RECEIVE)
self.interface.setup_reception(self.FRAME_ID_RECEIVE, min_interval=None)
self.interface.setup_reception(self.FRAME_ID_RECEIVE, min_interval=0) # 0 and None should be the same
def testSetupReceptionWrongValue(self):
self.assertRaises(exceptions.CanException, self.interface.setup_reception, -1)
self.assertRaises(exceptions.CanException, self.interface.setup_reception, 0x007, "ABC")
self.assertRaises(exceptions.CanException, self.interface.setup_reception, 0x007, 'standard', -1.0)
self.assertRaises(exceptions.CanException, self.interface.setup_reception, 0x007, 'standard', 0.05, b"\x00")
self.assertRaises(exceptions.CanException, self.interface.setup_reception, 0x007, 'standard', 0.05, "ABC")
def testSetupReceptionWrongType(self):
self.assertRaises(exceptions.CanException, self.interface.setup_reception, "ABC")
self.assertRaises(exceptions.CanException, self.interface.setup_reception, 0x007, 1)
self.assertRaises(TypeError, self.interface.setup_reception, 0x007, 'standard', "ABC")
self.assertRaises(TypeError, self.interface.setup_reception, 0x007, 'standard', 0.05, 1)
def testReceiveData(self):
self.start_can_frame_sender()
self.interface.setup_reception(self.FRAME_ID_RECEIVE)
received_frame = self.interface.recv_next_frame()
self.assertEqual(len(received_frame), self.FRAME_NUMBER_OF_DATABYTES)
def testReceiveStoppedReception(self):
self.start_can_frame_sender()
self.interface.setup_reception(self.FRAME_ID_RECEIVE)
self.interface.stop_reception(self.FRAME_ID_RECEIVE)
self.assertRaises(exceptions.CanTimeoutException, self.interface.recv_next_frame)
def testReceiveWrongId(self):
self.start_can_frame_sender()
self.interface.setup_reception(self.NONEXISTING_FRAME_ID)
self.assertRaises(exceptions.CanTimeoutException, self.interface.recv_next_frame)
def testReceiveSpeed(self):
self.start_can_frame_sender()
self.interface.setup_reception(self.FRAME_ID_RECEIVE)
time.sleep(0.1)
starttime = time.time()
for i in range(self.NUMBER_OF_LOOPS):
self.interface.recv_next_frame()
execution_time = time.time() - starttime
time_per_loop_ms = 1000 * execution_time / self.NUMBER_OF_LOOPS
outputstring = "\n --> Received {} frames in {:.1f} s ({:.1f} ms per frame). Frame sender spacing {:.1f} ms.\n".\
format(self.NUMBER_OF_LOOPS, execution_time, time_per_loop_ms, self.FRAME_SENDER_SPACING_MILLISECONDS)
print(outputstring)
def testReceiveSpeedThrottled(self):
THROTTLE_INTERVAL_MILLISECONDS = 20
THROTTLE_LOOPS = 100
ALLOWED_RELATIVE_ERROR = 0.1
nominal_time = THROTTLE_INTERVAL_MILLISECONDS * THROTTLE_LOOPS / 1000 # seconds
self.start_can_frame_sender()
self.interface.setup_reception(self.FRAME_ID_RECEIVE, min_interval=THROTTLE_INTERVAL_MILLISECONDS)
time.sleep(0.1)
starttime = time.time()
for i in range(THROTTLE_LOOPS):
self.interface.recv_next_frame()
execution_time = time.time() - starttime
self.assertLess(abs(execution_time - nominal_time), ALLOWED_RELATIVE_ERROR * nominal_time)
def testReceiveDataChanged(self):
"""Verify that data change filtering works, by measuring time to receive a small number of frames from a larger data flow."""
SENDER_INTERVAL_MILLISECONDS = 1
DATA_CHANGED_LOOPS = 20
DATA_MASK = b"\x80\x00\x00\x00\x00\x00\x00\x00" # One frame out of 128
ALLOWED_RELATIVE_ERROR = 2.0
nominal_time = SENDER_INTERVAL_MILLISECONDS * 128 * DATA_CHANGED_LOOPS / 1000 # seconds
self.start_can_frame_sender(SENDER_INTERVAL_MILLISECONDS)
self.interface.setup_reception(self.FRAME_ID_RECEIVE, data_mask=DATA_MASK)
time.sleep(0.1)
starttime = time.time()
for i in range(DATA_CHANGED_LOOPS):
self.interface.recv_next_frame()
execution_time = time.time() - starttime
allowed_error = ALLOWED_RELATIVE_ERROR * nominal_time
time_error = abs(execution_time - nominal_time)
outputstring = "\n --> Received {} frames in {:.1f} s, nominal time {:.1f} s. Error {:.1f} s (allowed {:.1f} s).\n".\
format(DATA_CHANGED_LOOPS, execution_time, nominal_time, time_error, allowed_error)
print(outputstring)
self.assertLess(time_error, allowed_error)
def testReceiveDataChangedShorterDlcThanMask(self):
"""Verify that filtering works also when the input frame is shorter (3 bytes) than the data mask (8 bytes)."""
SENDER_INTERVAL_MILLISECONDS = 1
DATA_CHANGED_LOOPS = 20
DATA_MASK = b"\x80\x00\x00\x00\x00\x00\x00\x00" # One frame out of 128
ALLOWED_RELATIVE_ERROR = 2.0
nominal_time = SENDER_INTERVAL_MILLISECONDS * DATA_CHANGED_LOOPS * 128 / 1000
self.interface.setup_reception(self.FRAME_ID_RECEIVE, data_mask=DATA_MASK)
time.sleep(0.1)
self.simulated_can_process = subprocess.Popen(["cangen", VIRTUAL_CAN_BUS_NAME,
"-I", str(self.FRAME_ID_RECEIVE),
"-L", "3",
"-D", "i",
"-g", str(SENDER_INTERVAL_MILLISECONDS)],
shell=False, stderr=subprocess.STDOUT)
starttime = time.time()
for i in range(DATA_CHANGED_LOOPS):
self.interface.recv_next_frame()
execution_time = time.time() - starttime
allowed_error = ALLOWED_RELATIVE_ERROR * nominal_time
time_error = abs(execution_time - nominal_time)
outputstring = "\n --> Received {} frames in {:.1f} s, nominal time {:.1f} s. Error {:.1f} s (allowed {:.1f} s).\n".\
format(DATA_CHANGED_LOOPS, execution_time, nominal_time, time_error, allowed_error)
print(outputstring)
self.assertLess(time_error, allowed_error)
def testReceiveDlcChanged(self):
"""Verify that we are receiving frames where each frame is longer (or no data) than the previous."""
SENDER_INTERVAL_MILLISECONDS = 1
NUMBER_OF_DLC_FRAMES = 30
DATA_MASK = b"\x00\x00\x00\x00\x00\x00\x00\x00" # Do not look at data changes
self.interface.setup_reception(self.FRAME_ID_RECEIVE, data_mask=DATA_MASK)
self.simulated_can_process = subprocess.Popen(["cangen", VIRTUAL_CAN_BUS_NAME,
"-I", str(self.FRAME_ID_RECEIVE),
"-L", "i",
"-D", "0102030405060708",
"-n", str(NUMBER_OF_DLC_FRAMES),
"-g", str(SENDER_INTERVAL_MILLISECONDS)],
shell=False, stderr=subprocess.STDOUT)
number_of_received_frames = 0
while True:
try:
self.interface.recv_next_frame()
number_of_received_frames += 1
except exceptions.CanTimeoutException:
break
self.assertEqual(number_of_received_frames, NUMBER_OF_DLC_FRAMES)
def testReceiveNoData(self):
self.assertRaises(exceptions.CanTimeoutException, self.interface.recv_next_frame)
def testReceiveClosedBus(self):
disable_virtual_can_bus()
self.assertRaises(exceptions.CanException, self.interface.recv_next_frame)
def testReceiveClosedInterface(self):
self.interface.close()
self.assertRaises(exceptions.CanException, self.interface.recv_next_frame)
# Send #
def testSendSingleFrameWrongType(self):
self.assertRaises(exceptions.CanException, self.interface.send_frame, 1)
self.assertRaises(exceptions.CanException, self.interface.send_frame, "ABC")
def testSendSingleFrame(self):
self.simulated_can_process = subprocess.Popen(['candump', VIRTUAL_CAN_BUS_NAME, '-n', '1'], shell=False,
universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(0.1)
frame = canframe.CanFrame.from_empty_bytes(self.FRAME_ID_SEND, self.FRAME_NUMBER_OF_DATABYTES)
self.interface.send_frame(frame)
print("\nWaiting for a subprocess to receive a single CAN frame, sent via 'send_frame'.")
out, err = self.simulated_can_process.communicate()
print("Confirmed CAN frame.")
known_result = "[8] 00 00 00 00 00 00 00 00"
self.assertIn(known_result, out)
def testSetupPeriodicSendWrongValue(self):
frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, frame_zeros, -1)
def testSetupPeriodicSendWrongType(self):
frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, 1)
self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, frame_zeros, "ABC")
def testSetupPeriodicSendWrongType(self):
frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, 1)
self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, frame_zeros, "ABC")
def testSendPeriodicAndChangeFrameAndStop(self):
"""Start periodic CAN transmission, update frame data (not interval), and finally stop transmission."""
RESULT_ALLOWED_DIFFERENCE = 5 # Number of missed frames, or when stopping sending too late.
TRANSMISSION_INTERVAL_MILLISECONDS = 20
MEASUREMENT_TIME = 1 # seconds
self.simulated_can_process = subprocess.Popen(['candump', VIRTUAL_CAN_BUS_NAME], shell=False,
universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(0.1)
frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00')
frame_ones = canframe.CanFrame(self.FRAME_ID_SEND, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')
print("\nSetting up periodic CAN frame transmission.")
self.interface.setup_periodic_send(frame_zeros, TRANSMISSION_INTERVAL_MILLISECONDS)
time.sleep(MEASUREMENT_TIME)
self.interface.setup_periodic_send(frame_ones, restart_timer=False)
time.sleep(MEASUREMENT_TIME)
self.interface.stop_periodic_send(self.FRAME_ID_SEND)
time.sleep(MEASUREMENT_TIME)
self.simulated_can_process.terminate()
out, err = self.simulated_can_process.communicate()
print("Periodic CAN frame transmission done.")
projected_number_of_frames = MEASUREMENT_TIME * 1000 / TRANSMISSION_INTERVAL_MILLISECONDS
number_of_frames_zeros = out.count(" {:03.0f} [8] 00 00 00 00 00 00 00 00".format(self.FRAME_ID_SEND))
self.assertGreaterEqual(number_of_frames_zeros, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE)
self.assertLessEqual(number_of_frames_zeros, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
number_of_frames_ones = out.count(" {:03.0f} [8] FF FF FF FF FF FF FF FF".format(self.FRAME_ID_SEND))
self.assertGreaterEqual(number_of_frames_ones, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE)
self.assertLessEqual(number_of_frames_ones, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
def testSendPeriodicAndChangeFrameAndStopExtended(self):
"""Start periodic CAN transmission, update frame data (not interval), and finally stop transmission."""
RESULT_ALLOWED_DIFFERENCE = 5 # Number of missed frames, or when stopping sending too late.
TRANSMISSION_INTERVAL_MILLISECONDS = 20
MEASUREMENT_TIME = 1 # seconds
self.simulated_can_process = subprocess.Popen(['candump', VIRTUAL_CAN_BUS_NAME], shell=False,
universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(0.1)
frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00', 'extended')
frame_ones = canframe.CanFrame(self.FRAME_ID_SEND, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF', 'extended')
self.interface.setup_periodic_send(frame_zeros, TRANSMISSION_INTERVAL_MILLISECONDS)
time.sleep(MEASUREMENT_TIME)
self.interface.setup_periodic_send(frame_ones, restart_timer=False)
time.sleep(MEASUREMENT_TIME)
self.interface.stop_periodic_send(self.FRAME_ID_SEND, 'extended')
time.sleep(MEASUREMENT_TIME)
self.simulated_can_process.terminate()
out, err = self.simulated_can_process.communicate()
projected_number_of_frames = MEASUREMENT_TIME * 1000 / TRANSMISSION_INTERVAL_MILLISECONDS
number_of_frames_zeros = out.count(" {:08.0f} [8] 00 00 00 00 00 00 00 00".format(self.FRAME_ID_SEND))
self.assertGreaterEqual(number_of_frames_zeros, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE)
self.assertLessEqual(number_of_frames_zeros, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
number_of_frames_ones = out.count(" {:08.0f} [8] FF FF FF FF FF FF FF FF".format(self.FRAME_ID_SEND))
self.assertGreaterEqual(number_of_frames_ones, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE)
self.assertLessEqual(number_of_frames_ones, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
def testStopNonexistingPeriodicTask(self):
self.assertRaises(exceptions.CanException, self.interface.stop_periodic_send, self.FRAME_ID_SEND)
TRANSMISSION_INTERVAL_MILLISECONDS = 20
frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00', 'standard')
self.interface.setup_periodic_send(frame_zeros, TRANSMISSION_INTERVAL_MILLISECONDS)
self.assertRaises(exceptions.CanException, self.interface.stop_periodic_send, self.FRAME_ID_SEND, 'extended')
def testSendClosedBus(self):
disable_virtual_can_bus()
frame = canframe.CanFrame.from_empty_bytes(self.FRAME_ID_SEND, self.FRAME_NUMBER_OF_DATABYTES)
self.assertRaises(exceptions.CanException, self.interface.send_frame, frame)
def testSendClosedInterface(self):
frame = canframe.CanFrame.from_empty_bytes(self.FRAME_ID_SEND, self.FRAME_NUMBER_OF_DATABYTES)
self.interface.close()
self.assertRaises(exceptions.CanException, self.interface.send_frame, frame)
if __name__ == '__main__':
# Run all tests #
unittest.main()
# Run a single test #
# suite = unittest.TestSuite()
# suite.addTest(TestSocketCanBcmInterface("testReceiveDlcChanged"))
# unittest.TextTestRunner(verbosity=2).run(suite)