xref: /btstack/test/embedded/embedded_test.cpp (revision c589a9bb896bd3ca6ce0b0556b913c50596ccdf8)
1 #include "CppUTest/TestHarness.h"
2 #include "CppUTest/CommandLineTestRunner.h"
3 
4 #include "hal_cpu.h"
5 #include "hal_time_ms.h"
6 
7 #include "btstack_run_loop.h"
8 #include "btstack_run_loop_embedded.h"
9 #include "btstack_memory.h"
10 
11 // quick mock
12 
13 // hal_cpu
14 void hal_cpu_disable_irqs(void){}
15 void hal_cpu_enable_irqs(void){}
16 void hal_cpu_enable_irqs_and_sleep(void){}
17 
18 // hal_time_ms_h
19 uint32_t hal_time_ms(void){
20     return 0;
21 }
22 
23 #define HEARTBEAT_PERIOD_MS 1000
24 
25 static btstack_timer_source_t timer_1;
26 static btstack_data_source_t  data_source;
27 static bool data_source_called;
28 static bool timer_called;
29 
30 static void heartbeat_timeout_handler(btstack_timer_source_t * ts){
31     UNUSED(ts);
32     timer_called = true;
33 }
34 static void data_source_handler(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){
35     UNUSED(ds);
36     UNUSED(callback_type);
37     data_source_called = true;
38 }
39 
40 static void data_source_handler_trigger_exit(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){
41     UNUSED(ds);
42     UNUSED(callback_type);
43     btstack_run_loop_trigger_exit();
44 }
45 
46 TEST_GROUP(Embedded){
47 
48     void setup(void){
49         // start with BTstack init - especially configure HCI Transport
50         btstack_memory_init();
51         btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
52         btstack_run_loop_set_timer_handler(&timer_1, heartbeat_timeout_handler);
53     }
54     void teardown(void){
55         btstack_run_loop_deinit();
56         btstack_memory_deinit();
57     }
58 };
59 
60 TEST(Embedded, Init){
61     btstack_run_loop_set_timer(&timer_1, HEARTBEAT_PERIOD_MS);
62     btstack_run_loop_add_timer(&timer_1);
63 
64     btstack_run_loop_embedded_execute_once();
65     btstack_run_loop_embedded_trigger();
66     btstack_run_loop_embedded_execute_once();
67     btstack_run_loop_get_time_ms();
68     btstack_run_loop_timer_dump();
69     btstack_run_loop_remove_timer(&timer_1);
70     (void) btstack_run_loop_get_timer_context(&timer_1);
71     static btstack_context_callback_registration_t callback_registration;
72     btstack_run_loop_execute_on_main_thread(&callback_registration);
73 }
74 
75 TEST(Embedded, DataSource){
76     btstack_run_loop_set_data_source_handler(&data_source, &data_source_handler);
77     btstack_run_loop_set_data_source_fd(&data_source, 0);
78     btstack_run_loop_set_data_source_handle(&data_source, NULL);
79     btstack_run_loop_enable_data_source_callbacks(&data_source, DATA_SOURCE_CALLBACK_POLL);
80     btstack_run_loop_disable_data_source_callbacks(&data_source, DATA_SOURCE_CALLBACK_POLL);
81     btstack_run_loop_add_data_source(&data_source);
82     btstack_run_loop_remove_data_source(&data_source);
83 }
84 
85 TEST(Embedded, ExitRunLoop){
86     btstack_run_loop_set_data_source_handler(&data_source, &data_source_handler_trigger_exit);
87     btstack_run_loop_set_data_source_fd(&data_source, 0);
88     btstack_run_loop_set_data_source_handle(&data_source, NULL);
89     btstack_run_loop_enable_data_source_callbacks(&data_source, DATA_SOURCE_CALLBACK_POLL);
90     btstack_run_loop_add_data_source(&data_source);
91     btstack_run_loop_execute();
92 }
93 
94 
95 int main (int argc, const char * argv[]){
96     return CommandLineTestRunner::RunAllTests(argc, argv);
97 }
98