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 struct TIMER_LIST_ENT; 301 typedef void(TIMER_CBACK)(TIMER_LIST_ENT* p_tle); 302 303 /* Define a timer list entry */ 304 #ifdef NFC_INTEGRATION_FUZZER 305 struct TIMER_LIST_ENT { 306 TIMER_CBACK* p_cback; 307 int32_t ticks; 308 uintptr_t param; 309 uint16_t event; 310 uint8_t in_use; 311 }; 312 #else 313 struct TIMER_LIST_ENT { 314 TIMER_LIST_ENT* p_next; 315 TIMER_LIST_ENT* p_prev; 316 TIMER_CBACK* p_cback; 317 int32_t ticks; 318 uintptr_t param; 319 uint16_t event; 320 uint8_t in_use; 321 }; 322 #endif 323 324 /* Define a timer list queue */ 325 #ifdef NFC_INTEGRATION_FUZZER 326 typedef std::list<TIMER_LIST_ENT*> TIMER_LIST_Q; 327 #else 328 typedef struct { 329 TIMER_LIST_ENT* p_first; 330 TIMER_LIST_ENT* p_last; 331 int32_t last_ticks; 332 } TIMER_LIST_Q; 333 #endif 334 335 /*********************************************************************** 336 ** This queue is a general purpose buffer queue, for application use. 337 */ 338 typedef struct { 339 void* p_first; 340 void* p_last; 341 uint16_t count; 342 } BUFFER_Q; 343 344 /* Task constants */ 345 #ifndef TASKPTR 346 typedef uint32_t (*TASKPTR)(uint32_t); 347 #endif 348 349 /* General pool accessible to GKI_getbuf() */ 350 #define GKI_RESTRICTED_POOL 1 /* Inaccessible pool to GKI_getbuf() */ 351 352 /*********************************************************************** 353 ** Function prototypes 354 */ 355 356 /* Task management */ 357 extern uint8_t GKI_create_task(TASKPTR, uint8_t, int8_t*, uint16_t*, uint16_t, 358 void*, void*); 359 extern void GKI_exit_task(uint8_t); 360 extern uint8_t GKI_get_taskid(void); 361 extern void GKI_init(void); 362 extern int8_t* GKI_map_taskname(uint8_t); 363 extern uint8_t GKI_resume_task(uint8_t); 364 extern void GKI_run(void*); 365 extern void GKI_stop(void); 366 extern uint8_t GKI_suspend_task(uint8_t); 367 368 /* memory management */ 369 extern void GKI_shiftdown(uint8_t* p_mem, uint32_t len, uint32_t shift_amount); 370 extern void GKI_shiftup(uint8_t* p_dest, uint8_t* p_src, uint32_t len); 371 372 /* To send buffers and events between tasks */ 373 extern uint8_t GKI_isend_event(uint8_t, uint16_t); 374 extern void GKI_isend_msg(uint8_t, uint8_t, void*); 375 extern void* GKI_read_mbox(uint8_t); 376 extern void GKI_send_msg(uint8_t, uint8_t, void*); 377 extern uint8_t GKI_send_event(uint8_t, uint16_t); 378 379 /* To get and release buffers, change owner and get size */ 380 extern void GKI_change_buf_owner(void*, uint8_t); 381 extern uint8_t GKI_create_pool(uint16_t, uint16_t, uint8_t, void*); 382 extern void GKI_delete_pool(uint8_t); 383 extern void* GKI_find_buf_start(void*); 384 extern void GKI_freebuf(void*); 385 extern void* GKI_getbuf(uint16_t); 386 extern uint16_t GKI_get_buf_size(void*); 387 extern void* GKI_getpoolbuf(uint8_t); 388 389 extern uint16_t GKI_poolcount(uint8_t); 390 extern uint16_t GKI_poolfreecount(uint8_t); 391 extern uint16_t GKI_poolutilization(uint8_t); 392 extern void GKI_register_mempool(void* p_mem); 393 extern uint8_t GKI_set_pool_permission(uint8_t, uint8_t); 394 395 /* User buffer queue management */ 396 extern void* GKI_dequeue(BUFFER_Q*); 397 extern void GKI_enqueue(BUFFER_Q*, void*); 398 extern void GKI_enqueue_head(BUFFER_Q*, void*); 399 extern void* GKI_getfirst(BUFFER_Q*); 400 extern void* GKI_getlast(BUFFER_Q*); 401 extern void* GKI_getnext(void*); 402 extern void GKI_init_q(BUFFER_Q*); 403 extern bool GKI_queue_is_empty(BUFFER_Q*); 404 extern void* GKI_remove_from_queue(BUFFER_Q*, void*); 405 extern uint16_t GKI_get_pool_bufsize(uint8_t); 406 407 /* Timer management */ 408 extern void GKI_add_to_timer_list(TIMER_LIST_Q*, TIMER_LIST_ENT*); 409 extern void GKI_delay(uint32_t); 410 extern uint32_t GKI_get_tick_count(void); 411 extern int8_t* GKI_get_time_stamp(int8_t*); 412 extern void GKI_init_timer_list(TIMER_LIST_Q*); 413 extern void GKI_init_timer_list_entry(TIMER_LIST_ENT*); 414 extern int32_t GKI_ready_to_sleep(void); 415 extern void GKI_remove_from_timer_list(TIMER_LIST_Q*, TIMER_LIST_ENT*); 416 extern void GKI_start_timer(uint8_t, int32_t, bool); 417 extern void GKI_stop_timer(uint8_t); 418 extern void GKI_timer_update(int32_t); 419 extern bool GKI_timer_list_empty(TIMER_LIST_Q*); 420 extern TIMER_LIST_ENT* GKI_timer_list_first(TIMER_LIST_Q*); 421 extern uint16_t GKI_update_timer_list(TIMER_LIST_Q*, int32_t); 422 extern uint32_t GKI_get_remaining_ticks(TIMER_LIST_Q*, TIMER_LIST_ENT*); 423 extern uint16_t GKI_wait(uint16_t, uint32_t); 424 425 /* Start and Stop system time tick callback 426 * true for start system tick if time queue is not empty 427 * false to stop system tick if time queue is empty 428 */ 429 typedef void(SYSTEM_TICK_CBACK)(bool); 430 431 /* Time queue management for system ticks */ 432 extern bool GKI_timer_queue_empty(void); 433 extern void GKI_timer_queue_register_callback(SYSTEM_TICK_CBACK*); 434 435 /* Disable Interrupts, Enable Interrupts */ 436 extern void GKI_enable(void); 437 extern void GKI_disable(void); 438 extern void GKI_sched_lock(void); 439 extern void GKI_sched_unlock(void); 440 441 /* Allocate (Free) memory from an OS */ 442 extern void* GKI_os_malloc(uint32_t); 443 extern void GKI_os_free(void*); 444 445 /* os timer operation */ 446 extern uint32_t GKI_get_os_tick_count(void); 447 448 /* Exception handling */ 449 extern void GKI_exception(uint16_t, std::string); 450 451 #endif 452