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