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