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