Home | History | Annotate | Download | only in common
      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 
     22 /* Include platform-specific over-rides */
     23 #if (defined(NFC_STANDALONE) && (NFC_STANDALONE == TRUE))
     24     #include "gki_target.h"
     25 #else
     26     /* For non-nfc_standalone, include Bluetooth definitions */
     27     #include "bt_target.h"
     28 #endif
     29 
     30 
     31 
     32 #include "bt_types.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 /************************************************************************
     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 
     67 #define TIMER_0             0
     68 #define TIMER_1             1
     69 #define TIMER_2             2
     70 #define TIMER_3             3
     71 
     72 #define TIMER_0_EVT_MASK    0x0010
     73 #define TIMER_1_EVT_MASK    0x0020
     74 #define TIMER_2_EVT_MASK    0x0040
     75 #define TIMER_3_EVT_MASK    0x0080
     76 
     77 #define APPL_EVT_0          8
     78 #define APPL_EVT_1          9
     79 #define APPL_EVT_2          10
     80 #define APPL_EVT_3          11
     81 #define APPL_EVT_4          12
     82 #define APPL_EVT_5          13
     83 #define APPL_EVT_6          14
     84 #define APPL_EVT_7          15
     85 
     86 #define EVENT_MASK(evt)       ((UINT16)(0x0001 << (evt)))
     87 
     88 /************************************************************************
     89 **  Max Time Queue
     90 **/
     91 #ifndef GKI_MAX_TIMER_QUEUES
     92 #define GKI_MAX_TIMER_QUEUES    3
     93 #endif
     94 
     95 /************************************************************************
     96 **  Macro to determine the pool buffer size based on the GKI POOL ID at compile time.
     97 **  Pool IDs index from 0 to GKI_NUM_FIXED_BUF_POOLS - 1
     98 */
     99 
    100 #if (GKI_NUM_FIXED_BUF_POOLS < 1)
    101 
    102 #ifndef GKI_POOL_ID_0
    103 #define GKI_POOL_ID_0                0
    104 #endif /* ifndef GKI_POOL_ID_0 */
    105 
    106 #ifndef GKI_BUF0_SIZE
    107 #define GKI_BUF0_SIZE                0
    108 #endif /* ifndef GKI_BUF0_SIZE */
    109 
    110 #endif /* GKI_NUM_FIXED_BUF_POOLS < 1 */
    111 
    112 
    113 #if (GKI_NUM_FIXED_BUF_POOLS < 2)
    114 
    115 #ifndef GKI_POOL_ID_1
    116 #define GKI_POOL_ID_1                0
    117 #endif /* ifndef GKI_POOL_ID_1 */
    118 
    119 #ifndef GKI_BUF1_SIZE
    120 #define GKI_BUF1_SIZE                0
    121 #endif /* ifndef GKI_BUF1_SIZE */
    122 
    123 #endif /* GKI_NUM_FIXED_BUF_POOLS < 2 */
    124 
    125 
    126 #if (GKI_NUM_FIXED_BUF_POOLS < 3)
    127 
    128 #ifndef GKI_POOL_ID_2
    129 #define GKI_POOL_ID_2                0
    130 #endif /* ifndef GKI_POOL_ID_2 */
    131 
    132 #ifndef GKI_BUF2_SIZE
    133 #define GKI_BUF2_SIZE                0
    134 #endif /* ifndef GKI_BUF2_SIZE */
    135 
    136 #endif /* GKI_NUM_FIXED_BUF_POOLS < 3 */
    137 
    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 
    152 #if (GKI_NUM_FIXED_BUF_POOLS < 5)
    153 
    154 #ifndef GKI_POOL_ID_4
    155 #define GKI_POOL_ID_4                0
    156 #endif /* ifndef GKI_POOL_ID_4 */
    157 
    158 #ifndef GKI_BUF4_SIZE
    159 #define GKI_BUF4_SIZE                0
    160 #endif /* ifndef GKI_BUF4_SIZE */
    161 
    162 #endif /* GKI_NUM_FIXED_BUF_POOLS < 5 */
    163 
    164 
    165 #if (GKI_NUM_FIXED_BUF_POOLS < 6)
    166 
    167 #ifndef GKI_POOL_ID_5
    168 #define GKI_POOL_ID_5                0
    169 #endif /* ifndef GKI_POOL_ID_5 */
    170 
    171 #ifndef GKI_BUF5_SIZE
    172 #define GKI_BUF5_SIZE                0
    173 #endif /* ifndef GKI_BUF5_SIZE */
    174 
    175 #endif /* GKI_NUM_FIXED_BUF_POOLS < 6 */
    176 
    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 
    191 #if (GKI_NUM_FIXED_BUF_POOLS < 8)
    192 
    193 #ifndef GKI_POOL_ID_7
    194 #define GKI_POOL_ID_7                0
    195 #endif /* ifndef GKI_POOL_ID_7 */
    196 
    197 #ifndef GKI_BUF7_SIZE
    198 #define GKI_BUF7_SIZE                0
    199 #endif /* ifndef GKI_BUF7_SIZE */
    200 
    201 #endif /* GKI_NUM_FIXED_BUF_POOLS < 8 */
    202 
    203 
    204 #if (GKI_NUM_FIXED_BUF_POOLS < 9)
    205 
    206 #ifndef GKI_POOL_ID_8
    207 #define GKI_POOL_ID_8                0
    208 #endif /* ifndef GKI_POOL_ID_8 */
    209 
    210 #ifndef GKI_BUF8_SIZE
    211 #define GKI_BUF8_SIZE                0
    212 #endif /* ifndef GKI_BUF8_SIZE */
    213 
    214 #endif /* GKI_NUM_FIXED_BUF_POOLS < 9 */
    215 
    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 
    230 #if (GKI_NUM_FIXED_BUF_POOLS < 11)
    231 
    232 #ifndef GKI_POOL_ID_10
    233 #define GKI_POOL_ID_10                0
    234 #endif /* ifndef GKI_POOL_ID_10 */
    235 
    236 #ifndef GKI_BUF10_SIZE
    237 #define GKI_BUF10_SIZE                0
    238 #endif /* ifndef GKI_BUF10_SIZE */
    239 
    240 #endif /* GKI_NUM_FIXED_BUF_POOLS < 11 */
    241 
    242 
    243 #if (GKI_NUM_FIXED_BUF_POOLS < 12)
    244 
    245 #ifndef GKI_POOL_ID_11
    246 #define GKI_POOL_ID_11                0
    247 #endif /* ifndef GKI_POOL_ID_11 */
    248 
    249 #ifndef GKI_BUF11_SIZE
    250 #define GKI_BUF11_SIZE                0
    251 #endif /* ifndef GKI_BUF11_SIZE */
    252 
    253 #endif /* GKI_NUM_FIXED_BUF_POOLS < 12 */
    254 
    255 
    256 #if (GKI_NUM_FIXED_BUF_POOLS < 13)
    257 
    258 #ifndef GKI_POOL_ID_12
    259 #define GKI_POOL_ID_12                0
    260 #endif /* ifndef GKI_POOL_ID_12 */
    261 
    262 #ifndef GKI_BUF12_SIZE
    263 #define GKI_BUF12_SIZE                0
    264 #endif /* ifndef GKI_BUF12_SIZE */
    265 
    266 #endif /* GKI_NUM_FIXED_BUF_POOLS < 13 */
    267 
    268 
    269 #if (GKI_NUM_FIXED_BUF_POOLS < 14)
    270 
    271 #ifndef GKI_POOL_ID_13
    272 #define GKI_POOL_ID_13                0
    273 #endif /* ifndef GKI_POOL_ID_13 */
    274 
    275 #ifndef GKI_BUF13_SIZE
    276 #define GKI_BUF13_SIZE                0
    277 #endif /* ifndef GKI_BUF13_SIZE */
    278 
    279 #endif /* GKI_NUM_FIXED_BUF_POOLS < 14 */
    280 
    281 
    282 #if (GKI_NUM_FIXED_BUF_POOLS < 15)
    283 
    284 #ifndef GKI_POOL_ID_14
    285 #define GKI_POOL_ID_14                0
    286 #endif /* ifndef GKI_POOL_ID_14 */
    287 
    288 #ifndef GKI_BUF14_SIZE
    289 #define GKI_BUF14_SIZE                0
    290 #endif /* ifndef GKI_BUF14_SIZE */
    291 
    292 #endif /* GKI_NUM_FIXED_BUF_POOLS < 15 */
    293 
    294 
    295 #if (GKI_NUM_FIXED_BUF_POOLS < 16)
    296 
    297 #ifndef GKI_POOL_ID_15
    298 #define GKI_POOL_ID_15                0
    299 #endif /* ifndef GKI_POOL_ID_15 */
    300 
    301 #ifndef GKI_BUF15_SIZE
    302 #define GKI_BUF15_SIZE                0
    303 #endif /* ifndef GKI_BUF15_SIZE */
    304 
    305 #endif /* GKI_NUM_FIXED_BUF_POOLS < 16 */
    306 
    307 
    308 /* Timer list entry callback type
    309 */
    310 typedef void (TIMER_CBACK)(void *p_tle);
    311 #ifndef TIMER_PARAM_TYPE
    312 #ifdef  WIN2000
    313 #define TIMER_PARAM_TYPE    void *
    314 #else
    315 #define TIMER_PARAM_TYPE    UINT32
    316 #endif
    317 #endif
    318 /* Define a timer list entry
    319 */
    320 typedef struct _tle
    321 {
    322     struct _tle  *p_next;
    323     struct _tle  *p_prev;
    324     TIMER_CBACK  *p_cback;
    325     INT32         ticks;
    326     TIMER_PARAM_TYPE   param;
    327     UINT16        event;
    328     UINT8         in_use;
    329 } TIMER_LIST_ENT;
    330 
    331 /* Define a timer list queue
    332 */
    333 typedef struct
    334 {
    335     TIMER_LIST_ENT   *p_first;
    336     TIMER_LIST_ENT   *p_last;
    337     INT32             last_ticks;
    338 } TIMER_LIST_Q;
    339 
    340 
    341 /***********************************************************************
    342 ** This queue is a general purpose buffer queue, for application use.
    343 */
    344 typedef struct
    345 {
    346     void    *p_first;
    347     void    *p_last;
    348     UINT16   count;
    349 } BUFFER_Q;
    350 
    351 #define GKI_IS_QUEUE_EMPTY(p_q) ((p_q)->count == 0)
    352 
    353 /* Task constants
    354 */
    355 #ifndef TASKPTR
    356 typedef void (*TASKPTR)(UINT32);
    357 #endif
    358 
    359 
    360 #define GKI_PUBLIC_POOL         0       /* General pool accessible to GKI_getbuf() */
    361 #define GKI_RESTRICTED_POOL     1       /* Inaccessible pool to GKI_getbuf() */
    362 
    363 /***********************************************************************
    364 ** Function prototypes
    365 */
    366 
    367 #ifdef __cplusplus
    368 extern "C" {
    369 #endif
    370 
    371 /* Task management
    372 */
    373 GKI_API extern UINT8   GKI_create_task (TASKPTR, UINT8, INT8 *, UINT16 *, UINT16);
    374 GKI_API extern void    GKI_destroy_task(UINT8 task_id);
    375 GKI_API extern void    GKI_task_self_cleanup(UINT8 task_id);
    376 GKI_API extern void    GKI_exit_task(UINT8);
    377 GKI_API extern UINT8   GKI_get_taskid(void);
    378 GKI_API extern void    GKI_init(void);
    379 GKI_API extern void    GKI_shutdown(void);
    380 GKI_API extern INT8   *GKI_map_taskname(UINT8);
    381 GKI_API extern UINT8   GKI_resume_task(UINT8);
    382 GKI_API extern void    GKI_run(void *);
    383 GKI_API extern void    GKI_freeze(void);
    384 GKI_API extern void    GKI_stop(void);
    385 GKI_API extern UINT8   GKI_suspend_task(UINT8);
    386 GKI_API extern UINT8   GKI_is_task_running(UINT8);
    387 
    388 /* memory management
    389 */
    390 GKI_API extern void GKI_shiftdown (UINT8 *p_mem, UINT32 len, UINT32 shift_amount);
    391 GKI_API extern void GKI_shiftup (UINT8 *p_dest, UINT8 *p_src, UINT32 len);
    392 
    393 /* To send buffers and events between tasks
    394 */
    395 GKI_API extern UINT8   GKI_isend_event (UINT8, UINT16);
    396 GKI_API extern void    GKI_isend_msg (UINT8, UINT8, void *);
    397 GKI_API extern void   *GKI_read_mbox  (UINT8);
    398 GKI_API extern void    GKI_send_msg   (UINT8, UINT8, void *);
    399 GKI_API extern UINT8   GKI_send_event (UINT8, UINT16);
    400 
    401 
    402 /* To get and release buffers, change owner and get size
    403 */
    404 GKI_API extern void    GKI_change_buf_owner (void *, UINT8);
    405 GKI_API extern UINT8   GKI_create_pool (UINT16, UINT16, UINT8, void *);
    406 GKI_API extern void    GKI_delete_pool (UINT8);
    407 GKI_API extern void   *GKI_find_buf_start (void *);
    408 GKI_API extern void    GKI_freebuf (void *);
    409 GKI_API extern void   *GKI_getbuf (UINT16);
    410 GKI_API extern UINT16  GKI_get_buf_size (void *);
    411 GKI_API extern void   *GKI_getpoolbuf (UINT8);
    412 GKI_API extern UINT16  GKI_poolcount (UINT8);
    413 GKI_API extern UINT16  GKI_poolfreecount (UINT8);
    414 GKI_API extern UINT16  GKI_poolutilization (UINT8);
    415 GKI_API extern void    GKI_register_mempool (void *p_mem);
    416 GKI_API extern UINT8   GKI_set_pool_permission(UINT8, UINT8);
    417 
    418 
    419 /* User buffer queue management
    420 */
    421 GKI_API extern void   *GKI_dequeue  (BUFFER_Q *);
    422 GKI_API extern void    GKI_enqueue (BUFFER_Q *, void *);
    423 GKI_API extern void    GKI_enqueue_head (BUFFER_Q *, void *);
    424 GKI_API extern void   *GKI_getfirst (BUFFER_Q *);
    425 GKI_API extern void   *GKI_getlast (BUFFER_Q *);
    426 GKI_API extern void   *GKI_getnext (void *);
    427 GKI_API extern void    GKI_init_q (BUFFER_Q *);
    428 GKI_API extern BOOLEAN GKI_queue_is_empty(BUFFER_Q *);
    429 GKI_API extern void   *GKI_remove_from_queue (BUFFER_Q *, void *);
    430 GKI_API extern UINT16  GKI_get_pool_bufsize (UINT8);
    431 
    432 /* Timer management
    433 */
    434 GKI_API extern void    GKI_add_to_timer_list (TIMER_LIST_Q *, TIMER_LIST_ENT  *);
    435 GKI_API extern void    GKI_delay(UINT32);
    436 GKI_API extern UINT32  GKI_get_tick_count(void);
    437 GKI_API extern INT8   *GKI_get_time_stamp(INT8 *);
    438 GKI_API extern void    GKI_init_timer_list (TIMER_LIST_Q *);
    439 GKI_API extern void    GKI_init_timer_list_entry (TIMER_LIST_ENT  *);
    440 GKI_API extern INT32   GKI_ready_to_sleep (void);
    441 GKI_API extern void    GKI_remove_from_timer_list (TIMER_LIST_Q *, TIMER_LIST_ENT  *);
    442 GKI_API extern void    GKI_start_timer(UINT8, INT32, BOOLEAN);
    443 GKI_API extern void    GKI_stop_timer (UINT8);
    444 GKI_API extern void    GKI_timer_update(INT32);
    445 GKI_API extern UINT16  GKI_update_timer_list (TIMER_LIST_Q *, INT32);
    446 GKI_API extern UINT32  GKI_get_remaining_ticks (TIMER_LIST_Q *, TIMER_LIST_ENT  *);
    447 GKI_API extern UINT16  GKI_wait(UINT16, UINT32);
    448 
    449 /* Start and Stop system time tick callback
    450  * true for start system tick if time queue is not empty
    451  * false to stop system tick if time queue is empty
    452 */
    453 typedef void (SYSTEM_TICK_CBACK)(BOOLEAN);
    454 
    455 /* Time queue management for system ticks
    456 */
    457 GKI_API extern BOOLEAN GKI_timer_queue_empty (void);
    458 GKI_API extern void    GKI_timer_queue_register_callback(SYSTEM_TICK_CBACK *);
    459 
    460 /* Disable Interrupts, Enable Interrupts
    461 */
    462 GKI_API extern void    GKI_enable(void);
    463 GKI_API extern void    GKI_disable(void);
    464 GKI_API extern void    GKI_sched_lock(void);
    465 GKI_API extern void    GKI_sched_unlock(void);
    466 
    467 /* Allocate (Free) memory from an OS
    468 */
    469 GKI_API extern void     *GKI_os_malloc (UINT32);
    470 GKI_API extern void      GKI_os_free (void *);
    471 
    472 /* os timer operation */
    473 GKI_API extern UINT32 GKI_get_os_tick_count(void);
    474 
    475 /* Exception handling
    476 */
    477 GKI_API extern void    GKI_exception (UINT16, char *);
    478 
    479 #if GKI_DEBUG == TRUE
    480 GKI_API extern void    GKI_PrintBufferUsage(UINT8 *p_num_pools, UINT16 *p_cur_used);
    481 GKI_API extern void    GKI_PrintBuffer(void);
    482 GKI_API extern void    GKI_print_task(void);
    483 #else
    484 #undef GKI_PrintBufferUsage
    485 #define GKI_PrintBuffer() NULL
    486 #endif
    487 
    488 #ifdef __cplusplus
    489 }
    490 #endif
    491 
    492 
    493 #endif
    494 
    495