xref: /btstack/platform/wiced/btstack_run_loop_wiced.c (revision 85ce275a8798f83f0eaf5ab6d967f32d77931cb5)
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 MATTHIAS
24  * RINGWALD 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 btstack_linked_list_t timers;
69 
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
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 /**
82  * Add timer to run_loop (keep list sorted)
83  */
84 static void btstack_run_loop_wiced_add_timer(btstack_timer_source_t *ts){
85     btstack_linked_item_t *it;
86     for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
87         // don't add timer that's already in there
88         if ((btstack_timer_source_t *) it->next == ts){
89             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
90             return;
91         }
92         // exit if new timeout before list timeout
93         int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
94         if (delta < 0) break;
95     }
96     ts->item.next = it->next;
97     it->next = (btstack_linked_item_t *) ts;
98 }
99 
100 /**
101  * Remove timer from run loop
102  */
103 static int btstack_run_loop_wiced_remove_timer(btstack_timer_source_t *ts){
104     return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
105 }
106 
107 static void btstack_run_loop_wiced_dump_timer(void){
108 #ifdef ENABLE_LOG_INFO
109     btstack_linked_item_t *it;
110     int i = 0;
111     for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
112         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
113         log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
114     }
115 #endif
116 }
117 
118 // schedules execution similar to wiced_rtos_send_asynchronous_event for worker threads
119 void btstack_run_loop_wiced_execute_code_on_main_thread(wiced_result_t (*fn)(void *arg), void * arg){
120     function_call_t message;
121     message.fn  = fn;
122     message.arg = arg;
123     wiced_rtos_push_to_queue(&btstack_run_loop_queue, &message, WICED_NEVER_TIMEOUT);
124 }
125 
126 /**
127  * Execute run_loop
128  */
129 static void btstack_run_loop_wiced_execute(void) {
130     while (1) {
131         // get next timeout
132         uint32_t timeout_ms = WICED_NEVER_TIMEOUT;
133         if (timers) {
134             btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
135             uint32_t now = btstack_run_loop_wiced_get_time_ms();
136             int32_t delta_ms = btstack_time_delta(ts->timeout, now);
137             if (delta_ms <= 0){
138                 // remove timer before processing it to allow handler to re-register with run loop
139                 btstack_run_loop_wiced_remove_timer(ts);
140                 ts->process(ts);
141                 continue;
142             }
143             timeout_ms = delta_ms;
144         }
145 
146         // pop function call
147         function_call_t message = { NULL, NULL };
148         wiced_rtos_pop_from_queue( &btstack_run_loop_queue, &message, timeout_ms);
149         if (message.fn){
150             // execute code on run loop
151             message.fn(message.arg);
152         }
153     }
154 }
155 
156 static void btstack_run_loop_wiced_btstack_run_loop_init(void){
157     timers = NULL;
158 
159     // queue to receive events: up to 2 calls from transport, up to 3 for app
160     wiced_rtos_init_queue(&btstack_run_loop_queue, "BTstack Run Loop", sizeof(function_call_t), 5);
161 }
162 
163 /**
164  * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
165  */
166 const btstack_run_loop_t * btstack_run_loop_wiced_get_instance(void){
167     return &btstack_run_loop_wiced;
168 }
169 
170 static const btstack_run_loop_t btstack_run_loop_wiced = {
171     &btstack_run_loop_wiced_btstack_run_loop_init,
172     NULL,
173     NULL,
174     NULL,
175     NULL,
176     &btstack_run_loop_wiced_set_timer,
177     &btstack_run_loop_wiced_add_timer,
178     &btstack_run_loop_wiced_remove_timer,
179     &btstack_run_loop_wiced_execute,
180     &btstack_run_loop_wiced_dump_timer,
181     &btstack_run_loop_wiced_get_time_ms,
182 };
183