2018-12-04 02:32:48 -05:00
|
|
|
# Need Python 3 string formatting functions
|
2017-12-18 07:32:29 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from threading import Thread
|
2019-11-26 22:58:07 -05:00
|
|
|
|
|
|
|
import ttfw_idf
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
# Define tuple of strings to expect for each DUT.
|
2017-12-18 07:32:29 -05:00
|
|
|
master_expect = ("CAN Master: Driver installed", "CAN Master: Driver uninstalled")
|
|
|
|
slave_expect = ("CAN Slave: Driver installed", "CAN Slave: Driver uninstalled")
|
2019-10-22 06:05:19 -04:00
|
|
|
listen_only_expect = ("CAN Listen Only: Driver installed", "CAN Listen Only: Driver uninstalled")
|
2017-12-18 07:32:29 -05:00
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
2017-12-18 07:32:29 -05:00
|
|
|
def dut_thread_callback(**kwargs):
|
2018-12-04 02:32:48 -05:00
|
|
|
# Parse keyword arguments
|
|
|
|
dut = kwargs['dut'] # Get DUT from kwargs
|
2017-12-18 07:32:29 -05:00
|
|
|
expected = kwargs['expected']
|
2018-12-04 02:32:48 -05:00
|
|
|
result = kwargs['result'] # Get result[out] from kwargs. MUST be of mutable type e.g. list
|
|
|
|
|
|
|
|
# Must reset again as flashing during start_app will reset multiple times, causing unexpected results
|
2017-12-18 07:32:29 -05:00
|
|
|
dut.reset()
|
|
|
|
|
|
|
|
for string in expected:
|
|
|
|
dut.expect(string, 20)
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# Mark thread has run to completion without any exceptions
|
|
|
|
result[0] = True
|
|
|
|
|
2017-12-18 07:32:29 -05:00
|
|
|
|
2019-11-26 22:58:07 -05:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_CAN2')
|
2017-12-18 07:32:29 -05:00
|
|
|
def test_can_network_example(env, extra_data):
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
# Get device under test. "dut1", "dut2", and "dut3" must be properly defined in EnvConfig
|
2019-11-26 22:58:07 -05:00
|
|
|
dut_master = env.get_dut("dut1", "examples/peripherals/can/can_network/can_network_master",
|
|
|
|
dut_class=ttfw_idf.ESP32DUT)
|
|
|
|
dut_slave = env.get_dut("dut2", "examples/peripherals/can/can_network/can_network_slave",
|
|
|
|
dut_class=ttfw_idf.ESP32DUT)
|
|
|
|
dut_listen_only = env.get_dut("dut3", "examples/peripherals/can/can_network/can_network_listen_only",
|
|
|
|
dut_class=ttfw_idf.ESP32DUT)
|
2017-12-18 07:32:29 -05:00
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# Flash app onto each DUT, each DUT is reset again at the start of each thread
|
2017-12-18 07:32:29 -05:00
|
|
|
dut_master.start_app()
|
|
|
|
dut_slave.start_app()
|
|
|
|
dut_listen_only.start_app()
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# Create dict of keyword arguments for each dut
|
2017-12-18 07:32:29 -05:00
|
|
|
results = [[False], [False], [False]]
|
2018-12-04 02:32:48 -05:00
|
|
|
master_kwargs = {"dut": dut_master, "result": results[0], "expected": master_expect}
|
|
|
|
slave_kwargs = {"dut": dut_slave, "result": results[1], "expected": slave_expect}
|
|
|
|
listen_only_kwargs = {"dut": dut_listen_only, "result": results[2], "expected": listen_only_expect}
|
2017-12-18 07:32:29 -05:00
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# Create thread for each dut
|
|
|
|
dut_master_thread = Thread(target=dut_thread_callback, name="Master Thread", kwargs=master_kwargs)
|
|
|
|
dut_slave_thread = Thread(target=dut_thread_callback, name="Slave Thread", kwargs=slave_kwargs)
|
|
|
|
dut_listen_only_thread = Thread(target=dut_thread_callback, name="Listen Only Thread", kwargs=listen_only_kwargs)
|
2017-12-18 07:32:29 -05:00
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# Start each thread
|
2017-12-18 07:32:29 -05:00
|
|
|
dut_listen_only_thread.start()
|
|
|
|
dut_master_thread.start()
|
|
|
|
dut_slave_thread.start()
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# Wait for threads to complete
|
2017-12-18 07:32:29 -05:00
|
|
|
dut_listen_only_thread.join()
|
|
|
|
dut_master_thread.join()
|
|
|
|
dut_slave_thread.join()
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
# check each thread ran to completion
|
2017-12-18 07:32:29 -05:00
|
|
|
for result in results:
|
2018-12-04 02:32:48 -05:00
|
|
|
if result[0] is not True:
|
2017-12-18 07:32:29 -05:00
|
|
|
raise Exception("One or more threads did not run successfully")
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
2017-12-18 07:32:29 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_can_network_example()
|