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