1 /*
2 * Copyright (C) 2015 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holders nor the names of
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38 #define BTSTACK_FILE__ "btstack_run_loop_wiced.c"
39
40 /*
41 * btstack_run_loop_wiced.c
42 *
43 * Run loop for Broadcom WICED SDK which currently supports FreeRTOS and ThreadX
44 * WICED 3.3.1 does not support Event Flags on FreeRTOS so a queue is used instead
45 */
46
47 #include "wiced.h"
48
49 #include "btstack_run_loop_wiced.h"
50
51 #include "btstack_linked_list.h"
52 #include "btstack_debug.h"
53 #include "btstack_util.h"
54 #include "btstack_run_loop.h"
55
56 #include <stddef.h> // NULL
57
58 typedef struct function_call {
59 wiced_result_t (*fn)(void * arg);
60 void * arg;
61 } function_call_t;
62
63 static const btstack_run_loop_t btstack_run_loop_wiced;
64
65 static wiced_queue_t btstack_run_loop_queue;
66
67 // the run loop
68 static bool run_loop_exit_requested;
69
btstack_run_loop_wiced_get_time_ms(void)70 static uint32_t btstack_run_loop_wiced_get_time_ms(void){
71 wiced_time_t time;
72 wiced_time_get_time(&time);
73 return time;
74 }
75
76 // set timer
btstack_run_loop_wiced_set_timer(btstack_timer_source_t * ts,uint32_t timeout_in_ms)77 static void btstack_run_loop_wiced_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
78 ts->timeout = btstack_run_loop_wiced_get_time_ms() + timeout_in_ms + 1;
79 }
80
81 // TODO: use wiced mutex to protect list of callbacks in run_loop_base
btstack_run_loop_wiced_execute_on_main_thread_new(btstack_context_callback_registration_t * callback_registration)82 static void btstack_run_loop_wiced_execute_on_main_thread_new(btstack_context_callback_registration_t * callback_registration){
83 function_call_t message;
84 message.fn = callback_registration->callback;
85 message.arg = callback_registration->context;
86 wiced_rtos_push_to_queue(&btstack_run_loop_queue, &message, WICED_NEVER_TIMEOUT);
87 }
88
89 // @deprecated use btstack_run_loop_execute_on_main_thread instead
btstack_run_loop_wiced_execute_code_on_main_thread(wiced_result_t (* fn)(void * arg),void * arg)90 void btstack_run_loop_wiced_execute_code_on_main_thread(wiced_result_t (*fn)(void *arg), void * arg){
91 function_call_t message;
92 message.fn = fn;
93 message.arg = arg;
94 wiced_rtos_push_to_queue(&btstack_run_loop_queue, &message, WICED_NEVER_TIMEOUT);
95 }
96
97 /**
98 * Execute run_loop
99 */
btstack_run_loop_wiced_execute(void)100 static void btstack_run_loop_wiced_execute(void) {
101 while (run_loop_exit_requested == false) {
102
103 // process timers
104 uint32_t now = btstack_run_loop_wiced_get_time_ms();
105 btstack_run_loop_base_process_timers(now);
106
107 // get time until next timeout
108 int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now);
109 uint32_t timeout_ms = WICED_NEVER_TIMEOUT;
110 if (timeout_next_timer_ms >= 0){
111 timeout_ms = (uint32_t) timeout_next_timer_ms;
112 }
113
114 // pop function call
115 function_call_t message = { NULL, NULL };
116 wiced_rtos_pop_from_queue( &btstack_run_loop_queue, &message, timeout_ms);
117 if (message.fn){
118 // execute code on run loop
119 message.fn(message.arg);
120 }
121 }
122 }
123
btstack_run_loop_wiced_trigger_exit(void)124 static void btstack_run_loop_wiced_trigger_exit(void){
125 run_loop_exit_requested = true;
126 }
127
btstack_run_loop_wiced_btstack_run_loop_init(void)128 static void btstack_run_loop_wiced_btstack_run_loop_init(void){
129 btstack_run_loop_base_init();
130
131 // queue to receive events: up to 2 calls from transport, up to 3 for app
132 wiced_rtos_init_queue(&btstack_run_loop_queue, "BTstack Run Loop", sizeof(function_call_t), 5);
133 }
134
135 /**
136 * @brief Provide btstack_run_loop_wiced instance for use with btstack_run_loop_init
137 */
btstack_run_loop_wiced_get_instance(void)138 const btstack_run_loop_t * btstack_run_loop_wiced_get_instance(void){
139 return &btstack_run_loop_wiced;
140 }
141
142 static const btstack_run_loop_t btstack_run_loop_wiced = {
143 &btstack_run_loop_wiced_btstack_run_loop_init,
144 NULL,
145 NULL,
146 NULL,
147 NULL,
148 &btstack_run_loop_wiced_set_timer,
149 &btstack_run_loop_base_add_timer,
150 &btstack_run_loop_base_remove_timer,
151 &btstack_run_loop_wiced_execute,
152 &btstack_run_loop_base_dump_timer,
153 &btstack_run_loop_wiced_get_time_ms,
154 NULL,
155 &btstack_run_loop_wiced_execute_on_main_thread_new,
156 &btstack_run_loop_wiced_trigger_exit
157 };
158