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