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