1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #ifndef GKI_H
19 #define GKI_H
20 
21 #ifdef BUILDCFG
22 #if (!defined(NFC_HAL_TARGET) || (NFC_HAL_TARGET == FALSE))
23 #include "buildcfg.h"
24 #else
25 /* Build config when building HAL */
26 #include "buildcfg_hal.h"
27 #endif
28 #endif
29 
30 /* Include platform-specific over-rides */
31 #if (defined(NFC_STANDALONE) && (NFC_STANDALONE == TRUE))
32 #include "bt_types.h"
33 #include "gki_target.h"
34 #elif (defined(NFC_HAL_TARGET) && (NFC_HAL_TARGET == TRUE))
35 /* If building NFC HAL, then use hal target file */
36 #include "gki_hal_target.h"
37 #else
38 /* For non-nfc_standalone, include Bluetooth definitions */
39 #include "bt_target.h"
40 #include "bt_types.h"
41 #endif
42 
43 /* Uncomment this line for verbose GKI debugging and buffer tracking */
44 /*#define GKI_BUFFER_DEBUG   TRUE*/
45 
46 /* Error codes */
47 #define GKI_SUCCESS 0x00
48 #define GKI_FAILURE 0x01
49 #define GKI_INVALID_TASK 0xF0
50 #define GKI_INVALID_POOL 0xFF
51 
52 /************************************************************************
53 ** Mailbox definitions. Each task has 4 mailboxes that are used to
54 ** send buffers to the task.
55 */
56 #define TASK_MBOX_0 0
57 #define TASK_MBOX_1 1
58 #define TASK_MBOX_2 2
59 #define TASK_MBOX_3 3
60 
61 #define NUM_TASK_MBOX 4
62 
63 /************************************************************************
64 ** Event definitions.
65 **
66 ** There are 4 reserved events used to signal messages rcvd in task mailboxes.
67 ** There are 4 reserved events used to signal timeout events.
68 ** There are 8 general purpose events available for applications.
69 */
70 #define MAX_EVENTS 16
71 
72 #define TASK_MBOX_0_EVT_MASK 0x0001
73 #define TASK_MBOX_1_EVT_MASK 0x0002
74 #define TASK_MBOX_2_EVT_MASK 0x0004
75 #define TASK_MBOX_3_EVT_MASK 0x0008
76 
77 #define TIMER_0 0
78 #define TIMER_1 1
79 #define TIMER_2 2
80 #define TIMER_3 3
81 
82 #define TIMER_0_EVT_MASK 0x0010
83 #define TIMER_1_EVT_MASK 0x0020
84 #define TIMER_2_EVT_MASK 0x0040
85 #define TIMER_3_EVT_MASK 0x0080
86 
87 #define APPL_EVT_0 8
88 #define APPL_EVT_1 9
89 #define APPL_EVT_2 10
90 #define APPL_EVT_3 11
91 #define APPL_EVT_4 12
92 #define APPL_EVT_5 13
93 #define APPL_EVT_6 14
94 #define APPL_EVT_7 15
95 
96 #define EVENT_MASK(evt) ((uint16_t)(0x0001 << (evt)))
97 
98 /************************************************************************
99 **  Max Time Queue
100 **/
101 #ifndef GKI_MAX_TIMER_QUEUES
102 #define GKI_MAX_TIMER_QUEUES 3
103 #endif
104 
105 /************************************************************************
106 **  Macro to determine the pool buffer size based on the GKI POOL ID at compile
107 *time.
108 **  Pool IDs index from 0 to GKI_NUM_FIXED_BUF_POOLS - 1
109 */
110 
111 #if (GKI_NUM_FIXED_BUF_POOLS < 1)
112 
113 #ifndef GKI_POOL_ID_0
114 #define GKI_POOL_ID_0 0
115 #endif /* ifndef GKI_POOL_ID_0 */
116 
117 #ifndef GKI_BUF0_SIZE
118 #define GKI_BUF0_SIZE 0
119 #endif /* ifndef GKI_BUF0_SIZE */
120 
121 #endif /* GKI_NUM_FIXED_BUF_POOLS < 1 */
122 
123 #if (GKI_NUM_FIXED_BUF_POOLS < 2)
124 
125 #ifndef GKI_POOL_ID_1
126 #define GKI_POOL_ID_1 0
127 #endif /* ifndef GKI_POOL_ID_1 */
128 
129 #ifndef GKI_BUF1_SIZE
130 #define GKI_BUF1_SIZE 0
131 #endif /* ifndef GKI_BUF1_SIZE */
132 
133 #endif /* GKI_NUM_FIXED_BUF_POOLS < 2 */
134 
135 #if (GKI_NUM_FIXED_BUF_POOLS < 3)
136 
137 #ifndef GKI_POOL_ID_2
138 #define GKI_POOL_ID_2 0
139 #endif /* ifndef GKI_POOL_ID_2 */
140 
141 #ifndef GKI_BUF2_SIZE
142 #define GKI_BUF2_SIZE 0
143 #endif /* ifndef GKI_BUF2_SIZE */
144 
145 #endif /* GKI_NUM_FIXED_BUF_POOLS < 3 */
146 
147 #if (GKI_NUM_FIXED_BUF_POOLS < 4)
148 
149 #ifndef GKI_POOL_ID_3
150 #define GKI_POOL_ID_3 0
151 #endif /* ifndef GKI_POOL_ID_4 */
152 
153 #ifndef GKI_BUF3_SIZE
154 #define GKI_BUF3_SIZE 0
155 #endif /* ifndef GKI_BUF3_SIZE */
156 
157 #endif /* GKI_NUM_FIXED_BUF_POOLS < 4 */
158 
159 #if (GKI_NUM_FIXED_BUF_POOLS < 5)
160 
161 #ifndef GKI_POOL_ID_4
162 #define GKI_POOL_ID_4 0
163 #endif /* ifndef GKI_POOL_ID_4 */
164 
165 #ifndef GKI_BUF4_SIZE
166 #define GKI_BUF4_SIZE 0
167 #endif /* ifndef GKI_BUF4_SIZE */
168 
169 #endif /* GKI_NUM_FIXED_BUF_POOLS < 5 */
170 
171 #if (GKI_NUM_FIXED_BUF_POOLS < 6)
172 
173 #ifndef GKI_POOL_ID_5
174 #define GKI_POOL_ID_5 0
175 #endif /* ifndef GKI_POOL_ID_5 */
176 
177 #ifndef GKI_BUF5_SIZE
178 #define GKI_BUF5_SIZE 0
179 #endif /* ifndef GKI_BUF5_SIZE */
180 
181 #endif /* GKI_NUM_FIXED_BUF_POOLS < 6 */
182 
183 #if (GKI_NUM_FIXED_BUF_POOLS < 7)
184 
185 #ifndef GKI_POOL_ID_6
186 #define GKI_POOL_ID_6 0
187 #endif /* ifndef GKI_POOL_ID_6 */
188 
189 #ifndef GKI_BUF6_SIZE
190 #define GKI_BUF6_SIZE 0
191 #endif /* ifndef GKI_BUF6_SIZE */
192 
193 #endif /* GKI_NUM_FIXED_BUF_POOLS < 7 */
194 
195 #if (GKI_NUM_FIXED_BUF_POOLS < 8)
196 
197 #ifndef GKI_POOL_ID_7
198 #define GKI_POOL_ID_7 0
199 #endif /* ifndef GKI_POOL_ID_7 */
200 
201 #ifndef GKI_BUF7_SIZE
202 #define GKI_BUF7_SIZE 0
203 #endif /* ifndef GKI_BUF7_SIZE */
204 
205 #endif /* GKI_NUM_FIXED_BUF_POOLS < 8 */
206 
207 #if (GKI_NUM_FIXED_BUF_POOLS < 9)
208 
209 #ifndef GKI_POOL_ID_8
210 #define GKI_POOL_ID_8 0
211 #endif /* ifndef GKI_POOL_ID_8 */
212 
213 #ifndef GKI_BUF8_SIZE
214 #define GKI_BUF8_SIZE 0
215 #endif /* ifndef GKI_BUF8_SIZE */
216 
217 #endif /* GKI_NUM_FIXED_BUF_POOLS < 9 */
218 
219 #if (GKI_NUM_FIXED_BUF_POOLS < 10)
220 
221 #ifndef GKI_POOL_ID_9
222 #define GKI_POOL_ID_9 0
223 #endif /* ifndef GKI_POOL_ID_9 */
224 
225 #ifndef GKI_BUF9_SIZE
226 #define GKI_BUF9_SIZE 0
227 #endif /* ifndef GKI_BUF9_SIZE */
228 
229 #endif /* GKI_NUM_FIXED_BUF_POOLS < 10 */
230 
231 #if (GKI_NUM_FIXED_BUF_POOLS < 11)
232 
233 #ifndef GKI_POOL_ID_10
234 #define GKI_POOL_ID_10 0
235 #endif /* ifndef GKI_POOL_ID_10 */
236 
237 #ifndef GKI_BUF10_SIZE
238 #define GKI_BUF10_SIZE 0
239 #endif /* ifndef GKI_BUF10_SIZE */
240 
241 #endif /* GKI_NUM_FIXED_BUF_POOLS < 11 */
242 
243 #if (GKI_NUM_FIXED_BUF_POOLS < 12)
244 
245 #ifndef GKI_POOL_ID_11
246 #define GKI_POOL_ID_11 0
247 #endif /* ifndef GKI_POOL_ID_11 */
248 
249 #ifndef GKI_BUF11_SIZE
250 #define GKI_BUF11_SIZE 0
251 #endif /* ifndef GKI_BUF11_SIZE */
252 
253 #endif /* GKI_NUM_FIXED_BUF_POOLS < 12 */
254 
255 #if (GKI_NUM_FIXED_BUF_POOLS < 13)
256 
257 #ifndef GKI_POOL_ID_12
258 #define GKI_POOL_ID_12 0
259 #endif /* ifndef GKI_POOL_ID_12 */
260 
261 #ifndef GKI_BUF12_SIZE
262 #define GKI_BUF12_SIZE 0
263 #endif /* ifndef GKI_BUF12_SIZE */
264 
265 #endif /* GKI_NUM_FIXED_BUF_POOLS < 13 */
266 
267 #if (GKI_NUM_FIXED_BUF_POOLS < 14)
268 
269 #ifndef GKI_POOL_ID_13
270 #define GKI_POOL_ID_13 0
271 #endif /* ifndef GKI_POOL_ID_13 */
272 
273 #ifndef GKI_BUF13_SIZE
274 #define GKI_BUF13_SIZE 0
275 #endif /* ifndef GKI_BUF13_SIZE */
276 
277 #endif /* GKI_NUM_FIXED_BUF_POOLS < 14 */
278 
279 #if (GKI_NUM_FIXED_BUF_POOLS < 15)
280 
281 #ifndef GKI_POOL_ID_14
282 #define GKI_POOL_ID_14 0
283 #endif /* ifndef GKI_POOL_ID_14 */
284 
285 #ifndef GKI_BUF14_SIZE
286 #define GKI_BUF14_SIZE 0
287 #endif /* ifndef GKI_BUF14_SIZE */
288 
289 #endif /* GKI_NUM_FIXED_BUF_POOLS < 15 */
290 
291 #if (GKI_NUM_FIXED_BUF_POOLS < 16)
292 
293 #ifndef GKI_POOL_ID_15
294 #define GKI_POOL_ID_15 0
295 #endif /* ifndef GKI_POOL_ID_15 */
296 
297 #ifndef GKI_BUF15_SIZE
298 #define GKI_BUF15_SIZE 0
299 #endif /* ifndef GKI_BUF15_SIZE */
300 
301 #endif /* GKI_NUM_FIXED_BUF_POOLS < 16 */
302 
303 /* Timer list entry callback type
304 */
305 typedef void(TIMER_CBACK)(void* p_tle);
306 #ifndef TIMER_PARAM_TYPE
307 #ifdef WIN2000
308 #define TIMER_PARAM_TYPE void*
309 #else
310 #define TIMER_PARAM_TYPE uint32_t
311 #endif
312 #endif
313 /* Define a timer list entry
314 */
315 typedef struct _tle {
316   struct _tle* p_next;
317   struct _tle* p_prev;
318   TIMER_CBACK* p_cback;
319   int32_t ticks;
320   TIMER_PARAM_TYPE param;
321   uint16_t event;
322   uint8_t in_use;
323 } TIMER_LIST_ENT;
324 
325 /* Define a timer list queue
326 */
327 typedef struct {
328   TIMER_LIST_ENT* p_first;
329   TIMER_LIST_ENT* p_last;
330   int32_t last_ticks;
331 } TIMER_LIST_Q;
332 
333 /***********************************************************************
334 ** This queue is a general purpose buffer queue, for application use.
335 */
336 typedef struct {
337   void* p_first;
338   void* p_last;
339   uint16_t count;
340 } BUFFER_Q;
341 
342 #define GKI_IS_QUEUE_EMPTY(p_q) ((p_q)->count == 0)
343 
344 /* Task constants
345 */
346 #ifndef TASKPTR
347 typedef void (*TASKPTR)(uint32_t);
348 #endif
349 
350 #define GKI_PUBLIC_POOL 0     /* General pool accessible to GKI_getbuf() */
351 #define GKI_RESTRICTED_POOL 1 /* Inaccessible pool to GKI_getbuf() */
352 
353 /***********************************************************************
354 ** Function prototypes
355 */
356 
357 #ifdef __cplusplus
358 extern "C" {
359 #endif
360 
361 /* Task management
362 */
363 extern uint8_t GKI_create_task(TASKPTR, uint8_t, int8_t*, uint16_t*, uint16_t,
364                                void*, void*);
365 extern void GKI_exit_task(uint8_t);
366 extern uint8_t GKI_get_taskid(void);
367 extern void GKI_init(void);
368 extern uint8_t* GKI_map_taskname(uint8_t);
369 extern uint8_t GKI_resume_task(uint8_t);
370 extern void GKI_run(void*);
371 extern void GKI_stop(void);
372 extern uint8_t GKI_suspend_task(uint8_t);
373 extern uint8_t GKI_is_task_running(uint8_t);
374 extern void GKI_shutdown(void);
375 
376 /* memory management
377 */
378 extern void GKI_shiftdown(uint8_t* p_mem, uint32_t len, uint32_t shift_amount);
379 extern void GKI_shiftup(uint8_t* p_dest, uint8_t* p_src, uint32_t len);
380 
381 /* To send buffers and events between tasks
382 */
383 extern uint8_t GKI_isend_event(uint8_t, uint16_t);
384 extern void GKI_isend_msg(uint8_t, uint8_t, void*);
385 extern void* GKI_read_mbox(uint8_t);
386 extern void GKI_send_msg(uint8_t, uint8_t, void*);
387 extern uint8_t GKI_send_event(uint8_t, uint16_t);
388 
389 /* To get and release buffers, change owner and get size
390 */
391 extern void GKI_change_buf_owner(void*, uint8_t);
392 extern uint8_t GKI_create_pool(uint16_t, uint16_t, uint8_t, void*);
393 extern void GKI_delete_pool(uint8_t);
394 extern void* GKI_find_buf_start(void*);
395 extern void GKI_freebuf(void*);
396 #if GKI_BUFFER_DEBUG
397 #define GKI_getbuf(size) GKI_getbuf_debug(size, __func__, __LINE__)
398 extern void* GKI_getbuf_debug(uint16_t, const char*, int);
399 #else
400 extern void* GKI_getbuf(uint16_t);
401 #endif
402 extern uint16_t GKI_get_buf_size(void*);
403 #if GKI_BUFFER_DEBUG
404 #define GKI_getpoolbuf(id) GKI_getpoolbuf_debug(id, __func__, __LINE__)
405 extern void* GKI_getpoolbuf_debug(uint8_t, const char*, int);
406 #else
407 extern void* GKI_getpoolbuf(uint8_t);
408 #endif
409 
410 extern uint16_t GKI_poolcount(uint8_t);
411 extern uint16_t GKI_poolfreecount(uint8_t);
412 extern uint16_t GKI_poolutilization(uint8_t);
413 extern void GKI_register_mempool(void* p_mem);
414 extern uint8_t GKI_set_pool_permission(uint8_t, uint8_t);
415 
416 /* User buffer queue management
417 */
418 extern void* GKI_dequeue(BUFFER_Q*);
419 extern void GKI_enqueue(BUFFER_Q*, void*);
420 extern void GKI_enqueue_head(BUFFER_Q*, void*);
421 extern void* GKI_getfirst(BUFFER_Q*);
422 extern void* GKI_getlast(BUFFER_Q*);
423 extern void* GKI_getnext(void*);
424 extern void GKI_init_q(BUFFER_Q*);
425 extern bool GKI_queue_is_empty(BUFFER_Q*);
426 extern void* GKI_remove_from_queue(BUFFER_Q*, void*);
427 extern uint16_t GKI_get_pool_bufsize(uint8_t);
428 
429 /* Timer management
430 */
431 extern void GKI_add_to_timer_list(TIMER_LIST_Q*, TIMER_LIST_ENT*);
432 extern void GKI_delay(uint32_t);
433 extern uint32_t GKI_get_tick_count(void);
434 extern int8_t* GKI_get_time_stamp(int8_t*);
435 extern void GKI_init_timer_list(TIMER_LIST_Q*);
436 extern void GKI_init_timer_list_entry(TIMER_LIST_ENT*);
437 extern int32_t GKI_ready_to_sleep(void);
438 extern void GKI_remove_from_timer_list(TIMER_LIST_Q*, TIMER_LIST_ENT*);
439 extern void GKI_start_timer(uint8_t, int32_t, bool);
440 extern void GKI_stop_timer(uint8_t);
441 extern void GKI_timer_update(int32_t);
442 extern uint16_t GKI_update_timer_list(TIMER_LIST_Q*, int32_t);
443 extern uint32_t GKI_get_remaining_ticks(TIMER_LIST_Q*, TIMER_LIST_ENT*);
444 extern uint16_t GKI_wait(uint16_t, uint32_t);
445 
446 /* Start and Stop system time tick callback
447  * true for start system tick if time queue is not empty
448  * false to stop system tick if time queue is empty
449 */
450 typedef void(SYSTEM_TICK_CBACK)(bool);
451 
452 /* Time queue management for system ticks
453 */
454 extern bool GKI_timer_queue_empty(void);
455 extern void GKI_timer_queue_register_callback(SYSTEM_TICK_CBACK*);
456 
457 /* Disable Interrupts, Enable Interrupts
458 */
459 extern void GKI_enable(void);
460 extern void GKI_disable(void);
461 extern void GKI_sched_lock(void);
462 extern void GKI_sched_unlock(void);
463 
464 /* Allocate (Free) memory from an OS
465 */
466 extern void* GKI_os_malloc(uint32_t);
467 extern void GKI_os_free(void*);
468 
469 /* os timer operation */
470 extern uint32_t GKI_get_os_tick_count(void);
471 
472 /* Exception handling
473 */
474 extern void GKI_exception(uint16_t, char*);
475 
476 #if GKI_DEBUG == TRUE
477 extern void GKI_PrintBufferUsage(uint8_t* p_num_pools, uint16_t* p_cur_used);
478 extern void GKI_PrintBuffer(void);
479 extern void GKI_print_task(void);
480 #else
481 #undef GKI_PrintBufferUsage
482 #define GKI_PrintBuffer() NULL
483 #endif
484 
485 #ifdef __cplusplus
486 }
487 #endif
488 
489 #endif
490