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