1from __future__ import print_function
2
3import ttfw_idf
4
5# Timer events
6TIMER_EVENT_LIMIT = 3
7
8TIMER_EXPIRY_HANDLING = 'TIMER_EVENTS:TIMER_EVENT_EXPIRY: timer_expiry_handler, executed {} out of ' + str(TIMER_EVENT_LIMIT) + ' times'
9
10# Task events
11TASK_ITERATION_LIMIT = 5
12TASK_UNREGISTRATION_LIMIT = 3
13
14TASK_ITERATION_POST = 'TASK_EVENTS:TASK_ITERATION_EVENT: posting to default loop, {} out of ' + str(TASK_ITERATION_LIMIT)
15TASK_ITERATION_HANDLING = 'TASK_EVENTS:TASK_ITERATION_EVENT: task_iteration_handler, executed {} times'
16
17
18def _test_timer_events(dut):
19    dut.start_app()
20
21    print('Checking timer events posting and handling')
22
23    dut.expect('setting up')
24    dut.expect('starting event sources')
25
26    print('Finished setup')
27
28    dut.expect('TIMER_EVENTS:TIMER_EVENT_STARTED: posting to default loop')
29    print('Posted timer started event')
30    dut.expect('TIMER_EVENTS:TIMER_EVENT_STARTED: timer_started_handler, instance 0')
31    dut.expect('TIMER_EVENTS:TIMER_EVENT_STARTED: timer_started_handler, instance 1')
32    dut.expect('TIMER_EVENTS:TIMER_EVENT_STARTED: timer_started_handler_2')
33    dut.expect('TIMER_EVENTS:TIMER_EVENT_STARTED: timer_any_handler')
34    dut.expect('TIMER_EVENTS:TIMER_EVENT_STARTED: all_event_handler')
35    print('Handled timer started event')
36
37    for expiries in range(1, TIMER_EVENT_LIMIT + 1):
38        dut.expect('TIMER_EVENTS:TIMER_EVENT_EXPIRY: posting to default loop')
39        print('Posted timer expiry event {} out of {}'.format(expiries, TIMER_EVENT_LIMIT))
40
41        if expiries >= TIMER_EVENT_LIMIT:
42            dut.expect('TIMER_EVENTS:TIMER_EVENT_STOPPED: posting to default loop')
43            print('Posted timer stopped event')
44
45        dut.expect(TIMER_EXPIRY_HANDLING.format(expiries))
46        dut.expect('TIMER_EVENTS:TIMER_EVENT_EXPIRY: timer_any_handler')
47        dut.expect('TIMER_EVENTS:TIMER_EVENT_EXPIRY: all_event_handler')
48
49        print('Handled timer expiry event {} out of {}'.format(expiries, TIMER_EVENT_LIMIT))
50
51    dut.expect('TIMER_EVENTS:TIMER_EVENT_STOPPED: timer_stopped_handler')
52    dut.expect('TIMER_EVENTS:TIMER_EVENT_STOPPED: deleted timer event source')
53    print('Handled timer stopped event')
54
55
56def _test_iteration_events(dut):
57    dut.start_app()
58
59    print('Checking iteration events posting and handling')
60    dut.expect('setting up')
61    dut.expect('starting event sources')
62    print('Finished setup')
63
64    for iteration in range(1, TASK_ITERATION_LIMIT + 1):
65        dut.expect(TASK_ITERATION_POST.format(iteration))
66        print('Posted iteration {} out of {}'.format(iteration, TASK_ITERATION_LIMIT))
67
68        if iteration < TASK_UNREGISTRATION_LIMIT:
69            dut.expect(TASK_ITERATION_HANDLING.format(iteration))
70            dut.expect('TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler')
71        elif iteration == TASK_UNREGISTRATION_LIMIT:
72            dut.expect_all('TASK_EVENTS:TASK_ITERATION_EVENT: unregistering task_iteration_handler',
73                           'TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler')
74            print('Unregistered handler at iteration {} out of {}'.format(iteration, TASK_ITERATION_LIMIT))
75        else:
76            dut.expect('TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler')
77
78        print('Handled iteration {} out of {}'.format(iteration, TASK_ITERATION_LIMIT))
79
80    dut.expect('TASK_EVENTS:TASK_ITERATION_EVENT: deleting task event source')
81    print('Deleted task event source')
82
83
84@ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32c3'])
85def test_default_event_loop_example(env, extra_data):
86    dut = env.get_dut('default_event_loop', 'examples/system/esp_event/default_event_loop')
87
88    _test_iteration_events(dut)
89    _test_timer_events(dut)
90
91
92if __name__ == '__main__':
93    test_default_event_loop_example()
94