Home | History | Annotate | Download | only in ulinux
      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 #include <stdio.h>
     19 #include <stdarg.h>
     20 #include <errno.h>
     21 
     22 #define GKI_DEBUG   FALSE
     23 
     24 #include <pthread.h>  /* must be 1st header defined  */
     25 #include <time.h>
     26 #include <hardware_legacy/power.h>  /* Android header */
     27 #include "gki_int.h"
     28 #include "gki_target.h"
     29 
     30 /* Temp android logging...move to android tgt config file */
     31 
     32 #ifndef LINUX_NATIVE
     33 #include <cutils/log.h>
     34 #else
     35 #define LOGV(format, ...)  fprintf (stdout, LOG_TAG format, ## __VA_ARGS__)
     36 #define LOGE(format, ...)  fprintf (stderr, LOG_TAG format, ## __VA_ARGS__)
     37 #define LOGI(format, ...)  fprintf (stdout, LOG_TAG format, ## __VA_ARGS__)
     38 
     39 #define SCHED_NORMAL 0
     40 #define SCHED_FIFO 1
     41 #define SCHED_RR 2
     42 #define SCHED_BATCH 3
     43 
     44 #define pthread_cond_timedwait_monotonic pthread_cond_timedwait
     45 
     46 #endif
     47 
     48 /* Define the structure that holds the GKI variables
     49 */
     50 #if GKI_DYNAMIC_MEMORY == FALSE
     51 tGKI_CB   gki_cb;
     52 #endif
     53 
     54 #define NANOSEC_PER_MILLISEC (1000000)
     55 #define NSEC_PER_SEC (1000*NANOSEC_PER_MILLISEC)
     56 
     57 /* works only for 1ms to 1000ms heart beat ranges */
     58 #define LINUX_SEC (1000/TICKS_PER_SEC)
     59 // #define GKI_TICK_TIMER_DEBUG
     60 
     61 #define LOCK(m)  pthread_mutex_lock(&m)
     62 #define UNLOCK(m) pthread_mutex_unlock(&m)
     63 #define INIT(m) pthread_mutex_init(&m, NULL)
     64 
     65 
     66 /* this kind of mutex go into tGKI_OS control block!!!! */
     67 /* static pthread_mutex_t GKI_sched_mutex; */
     68 /*static pthread_mutex_t thread_delay_mutex;
     69 static pthread_cond_t thread_delay_cond;
     70 static pthread_mutex_t gki_timer_update_mutex;
     71 static pthread_cond_t   gki_timer_update_cond;
     72 */
     73 #ifdef NO_GKI_RUN_RETURN
     74 static pthread_t            timer_thread_id = 0;
     75 #endif
     76 
     77 
     78 /* For Android */
     79 
     80 #ifndef GKI_SHUTDOWN_EVT
     81 #define GKI_SHUTDOWN_EVT    APPL_EVT_7
     82 #endif
     83 
     84 typedef struct
     85 {
     86     UINT8 task_id;          /* GKI task id */
     87     TASKPTR task_entry;     /* Task entry function*/
     88     UINT32 params;          /* Extra params to pass to task entry function */
     89     pthread_cond_t* pCond;	/* for android*/
     90     pthread_mutex_t* pMutex;  /* for android*/
     91 } gki_pthread_info_t;
     92 gki_pthread_info_t gki_pthread_info[GKI_MAX_TASKS];
     93 
     94 static void* GKI_run_worker_thread (void*);
     95 
     96 /*******************************************************************************
     97 **
     98 ** Function         gki_task_entry
     99 **
    100 ** Description      entry point of GKI created tasks
    101 **
    102 ** Returns          void
    103 **
    104 *******************************************************************************/
    105 void gki_task_entry(UINT32 params)
    106 {
    107     pthread_t thread_id = pthread_self();
    108     gki_pthread_info_t *p_pthread_info = (gki_pthread_info_t *)params;
    109     GKI_TRACE_5("gki_task_entry task_id=%i, thread_id=%x/%x, pCond/pMutex=%x/%x", p_pthread_info->task_id,
    110                 gki_cb.os.thread_id[p_pthread_info->task_id], pthread_self(),
    111                 p_pthread_info->pCond, p_pthread_info->pMutex);
    112 
    113     gki_cb.os.thread_id[p_pthread_info->task_id] = thread_id;
    114     /* Call the actual thread entry point */
    115     (p_pthread_info->task_entry)(p_pthread_info->params);
    116 
    117     GKI_TRACE_1("gki_task task_id=%i terminating", p_pthread_info->task_id);
    118     gki_cb.os.thread_id[p_pthread_info->task_id] = 0;
    119 
    120     pthread_exit(0);    /* GKI tasks have no return value */
    121 }
    122 /* end android */
    123 
    124 #ifndef ANDROID
    125 void GKI_TRACE(char *fmt, ...)
    126 {
    127     LOCK(gki_cb.os.GKI_trace_mutex);
    128     va_list ap;
    129 
    130     va_start(ap, fmt);
    131     vfprintf(stderr, fmt, ap);
    132     fprintf(stderr, "\n");
    133 
    134     va_end(ap);
    135     UNLOCK(gki_cb.os.GKI_trace_mutex);
    136 }
    137 #endif
    138 
    139 /*******************************************************************************
    140 **
    141 ** Function         GKI_init
    142 **
    143 ** Description      This function is called once at startup to initialize
    144 **                  all the timer structures.
    145 **
    146 ** Returns          void
    147 **
    148 *******************************************************************************/
    149 
    150 void GKI_init(void)
    151 {
    152     pthread_mutexattr_t attr;
    153     tGKI_OS             *p_os;
    154 
    155     memset (&gki_cb, 0, sizeof (gki_cb));
    156 
    157     gki_buffer_init();
    158     gki_timers_init();
    159     gki_cb.com.OSTicks = (UINT32) times(0);
    160 
    161     pthread_mutexattr_init(&attr);
    162 
    163 #ifndef __CYGWIN__
    164     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
    165 #endif
    166     p_os = &gki_cb.os;
    167     pthread_mutex_init(&p_os->GKI_mutex, &attr);
    168     /* pthread_mutex_init(&GKI_sched_mutex, NULL); */
    169 #if (GKI_DEBUG == TRUE)
    170     pthread_mutex_init(&p_os->GKI_trace_mutex, NULL);
    171 #endif
    172     /* pthread_mutex_init(&thread_delay_mutex, NULL); */  /* used in GKI_delay */
    173     /* pthread_cond_init (&thread_delay_cond, NULL); */
    174 
    175     /* Initialiase GKI_timer_update suspend variables & mutexes to be in running state.
    176      * this works too even if GKI_NO_TICK_STOP is defined in btld.txt */
    177     p_os->no_timer_suspend = GKI_TIMER_TICK_RUN_COND;
    178     pthread_mutex_init(&p_os->gki_timer_mutex, NULL);
    179     pthread_cond_init(&p_os->gki_timer_cond, NULL);
    180 }
    181 
    182 
    183 /*******************************************************************************
    184 **
    185 ** Function         GKI_get_os_tick_count
    186 **
    187 ** Description      This function is called to retrieve the native OS system tick.
    188 **
    189 ** Returns          Tick count of native OS.
    190 **
    191 *******************************************************************************/
    192 UINT32 GKI_get_os_tick_count(void)
    193 {
    194 
    195     /* TODO - add any OS specific code here
    196     **/
    197     return (gki_cb.com.OSTicks);
    198 }
    199 
    200 /*******************************************************************************
    201 **
    202 ** Function         GKI_create_task
    203 **
    204 ** Description      This function is called to create a new OSS task.
    205 **
    206 ** Parameters:      task_entry  - (input) pointer to the entry function of the task
    207 **                  task_id     - (input) Task id is mapped to priority
    208 **                  taskname    - (input) name given to the task
    209 **                  stack       - (input) pointer to the top of the stack (highest memory location)
    210 **                  stacksize   - (input) size of the stack allocated for the task
    211 **
    212 ** Returns          GKI_SUCCESS if all OK, GKI_FAILURE if any problem
    213 **
    214 ** NOTE             This function take some parameters that may not be needed
    215 **                  by your particular OS. They are here for compatability
    216 **                  of the function prototype.
    217 **
    218 *******************************************************************************/
    219 UINT8 GKI_create_task (TASKPTR task_entry, UINT8 task_id, INT8 *taskname, UINT16 *stack, UINT16 stacksize, void* pCondVar, void* pMutex)
    220 {
    221     UINT16  i;
    222     UINT8   *p;
    223     struct sched_param param;
    224     int policy, ret = 0;
    225     pthread_attr_t attr1;
    226 
    227     GKI_TRACE_5 ("GKI_create_task func=0x%x  id=%d  name=%s  stack=0x%x  stackSize=%d", task_entry, task_id, taskname, stack, stacksize);
    228 
    229     if (task_id >= GKI_MAX_TASKS)
    230     {
    231         GKI_TRACE_0("Error! task ID > max task allowed");
    232         return (GKI_FAILURE);
    233     }
    234 
    235 
    236     gki_cb.com.OSRdyTbl[task_id]    = TASK_READY;
    237     gki_cb.com.OSTName[task_id]     = taskname;
    238     gki_cb.com.OSWaitTmr[task_id]   = 0;
    239     gki_cb.com.OSWaitEvt[task_id]   = 0;
    240 
    241     /* Initialize mutex and condition variable objects for events and timeouts */
    242     pthread_mutex_init(&gki_cb.os.thread_evt_mutex[task_id], NULL);
    243     pthread_cond_init (&gki_cb.os.thread_evt_cond[task_id], NULL);
    244     pthread_mutex_init(&gki_cb.os.thread_timeout_mutex[task_id], NULL);
    245     pthread_cond_init (&gki_cb.os.thread_timeout_cond[task_id], NULL);
    246 
    247     pthread_attr_init(&attr1);
    248     /* by default, pthread creates a joinable thread */
    249 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    250     pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED);
    251 
    252     GKI_TRACE_3("GKI creating task %i, pCond/pMutex=%x/%x", task_id, pCondVar, pMutex);
    253 #else
    254     GKI_TRACE_1("GKI creating JOINABLE task %i", task_id);
    255 #endif
    256 
    257     /* On Android, the new tasks starts running before 'gki_cb.os.thread_id[task_id]' is initialized */
    258     /* Pass task_id to new task so it can initialize gki_cb.os.thread_id[task_id] for it calls GKI_wait */
    259     gki_pthread_info[task_id].task_id = task_id;
    260     gki_pthread_info[task_id].task_entry = task_entry;
    261     gki_pthread_info[task_id].params = 0;
    262     gki_pthread_info[task_id].pCond = (pthread_cond_t*)pCondVar;
    263     gki_pthread_info[task_id].pMutex = (pthread_mutex_t*)pMutex;
    264 
    265     ret = pthread_create( &gki_cb.os.thread_id[task_id],
    266               &attr1,
    267               (void *)gki_task_entry,
    268               &gki_pthread_info[task_id]);
    269 
    270     if (ret != 0)
    271     {
    272          GKI_TRACE_2("pthread_create failed(%d), %s!", ret, taskname);
    273          return GKI_FAILURE;
    274     }
    275 
    276     if(pthread_getschedparam(gki_cb.os.thread_id[task_id], &policy, &param)==0)
    277     {
    278 #if defined(PBS_SQL_TASK)
    279          if (task_id == PBS_SQL_TASK)
    280          {
    281              GKI_TRACE_0("PBS SQL lowest priority task");
    282              policy = SCHED_NORMAL;
    283          }
    284          else
    285 #endif
    286          {
    287              policy = SCHED_RR;
    288              param.sched_priority = 30 - task_id - 2;
    289          }
    290          pthread_setschedparam(gki_cb.os.thread_id[task_id], policy, &param);
    291      }
    292 
    293     GKI_TRACE_6( "Leaving GKI_create_task %x %d %x %s %x %d",
    294               task_entry,
    295               task_id,
    296               gki_cb.os.thread_id[task_id],
    297               taskname,
    298               stack,
    299               stacksize);
    300 
    301     return (GKI_SUCCESS);
    302 }
    303 
    304 /*******************************************************************************
    305 **
    306 ** Function         GKI_shutdown
    307 **
    308 ** Description      shutdowns the GKI tasks/threads in from max task id to 0 and frees
    309 **                  pthread resources!
    310 **                  IMPORTANT: in case of join method, GKI_shutdown must be called outside
    311 **                  a GKI thread context!
    312 **
    313 ** Returns          void
    314 **
    315 *******************************************************************************/
    316 #define WAKE_LOCK_ID "brcm_nfca"
    317 
    318 void GKI_shutdown(void)
    319 {
    320     UINT8 task_id;
    321     volatile int    *p_run_cond = &gki_cb.os.no_timer_suspend;
    322     int     oldCOnd = 0;
    323 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    324     int i = 0;
    325 #else
    326     int result;
    327 #endif
    328 
    329     /* release threads and set as TASK_DEAD. going from low to high priority fixes
    330      * GKI_exception problem due to btu->hci sleep request events  */
    331     for (task_id = GKI_MAX_TASKS; task_id > 0; task_id--)
    332     {
    333         if (gki_cb.com.OSRdyTbl[task_id - 1] != TASK_DEAD)
    334         {
    335             gki_cb.com.OSRdyTbl[task_id - 1] = TASK_DEAD;
    336 
    337             /* paranoi settings, make sure that we do not execute any mailbox events */
    338             gki_cb.com.OSWaitEvt[task_id-1] &= ~(TASK_MBOX_0_EVT_MASK|TASK_MBOX_1_EVT_MASK|
    339                                                 TASK_MBOX_2_EVT_MASK|TASK_MBOX_3_EVT_MASK);
    340             GKI_send_event(task_id - 1, EVENT_MASK(GKI_SHUTDOWN_EVT));
    341 
    342 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    343             i = 0;
    344 
    345             while ((gki_cb.com.OSWaitEvt[task_id - 1] != 0) && (++i < 10))
    346                 usleep(100 * 1000);
    347 #else
    348             /* wait for proper Arnold Schwarzenegger task state */
    349             result = pthread_join( gki_cb.os.thread_id[task_id-1], NULL );
    350             if ( result < 0 )
    351             {
    352                 GKI_TRACE_1( "pthread_join() FAILED: result: %d", result );
    353             }
    354 #endif
    355             GKI_TRACE_1( "GKI_shutdown(): task %s dead", gki_cb.com.OSTName[task_id]);
    356             GKI_exit_task(task_id - 1);
    357         }
    358     }
    359 
    360     /* Destroy mutex and condition variable objects */
    361     pthread_mutex_destroy(&gki_cb.os.GKI_mutex);
    362     /*    pthread_mutex_destroy(&GKI_sched_mutex); */
    363 #if (GKI_DEBUG == TRUE)
    364     pthread_mutex_destroy(&gki_cb.os.GKI_trace_mutex);
    365 #endif
    366     /*    pthread_mutex_destroy(&thread_delay_mutex);
    367      pthread_cond_destroy (&thread_delay_cond); */
    368 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    369     i = 0;
    370 #endif
    371 
    372 #ifdef NO_GKI_RUN_RETURN
    373     shutdown_timer = 1;
    374 #endif
    375     if (gki_cb.os.gki_timer_wake_lock_on)
    376     {
    377         GKI_TRACE_0("GKI_shutdown :  release_wake_lock(brcm_btld)");
    378         release_wake_lock(WAKE_LOCK_ID);
    379         gki_cb.os.gki_timer_wake_lock_on = 0;
    380     }
    381     oldCOnd = *p_run_cond;
    382     *p_run_cond = GKI_TIMER_TICK_EXIT_COND;
    383     if (oldCOnd == GKI_TIMER_TICK_STOP_COND)
    384         pthread_cond_signal( &gki_cb.os.gki_timer_cond );
    385 
    386 }
    387 
    388 /*******************************************************************************
    389  **
    390  ** Function        GKI_run
    391  **
    392  ** Description     This function runs a task
    393  **
    394  ** Parameters:     start: TRUE start system tick (again), FALSE stop
    395  **
    396  ** Returns         void
    397  **
    398  *********************************************************************************/
    399 void gki_system_tick_start_stop_cback(BOOLEAN start)
    400 {
    401     tGKI_OS         *p_os = &gki_cb.os;
    402     volatile int    *p_run_cond = &p_os->no_timer_suspend;
    403     volatile static int wake_lock_count;
    404     if ( FALSE == start )
    405     {
    406         /* this can lead to a race condition. however as we only read this variable in the timer loop
    407          * we should be fine with this approach. otherwise uncomment below mutexes.
    408          */
    409         /* GKI_disable(); */
    410         *p_run_cond = GKI_TIMER_TICK_STOP_COND;
    411         /* GKI_enable(); */
    412 #ifdef GKI_TICK_TIMER_DEBUG
    413         BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> STOP GKI_timer_update(), wake_lock_count:%d", --wake_lock_count);
    414 #endif
    415         release_wake_lock(WAKE_LOCK_ID);
    416         gki_cb.os.gki_timer_wake_lock_on = 0;
    417     }
    418     else
    419     {
    420         /* restart GKI_timer_update() loop */
    421         acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
    422         gki_cb.os.gki_timer_wake_lock_on = 1;
    423         *p_run_cond = GKI_TIMER_TICK_RUN_COND;
    424         pthread_mutex_lock( &p_os->gki_timer_mutex );
    425         pthread_cond_signal( &p_os->gki_timer_cond );
    426         pthread_mutex_unlock( &p_os->gki_timer_mutex );
    427 
    428 #ifdef GKI_TICK_TIMER_DEBUG
    429         BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> START GKI_timer_update(), wake_lock_count:%d", ++wake_lock_count );
    430 #endif
    431     }
    432 }
    433 
    434 
    435 /*******************************************************************************
    436 **
    437 ** Function         timer_thread
    438 **
    439 ** Description      Timer thread
    440 **
    441 ** Parameters:      id  - (input) timer ID
    442 **
    443 ** Returns          void
    444 **
    445 *********************************************************************************/
    446 #ifdef NO_GKI_RUN_RETURN
    447 void timer_thread(signed long id)
    448 {
    449     GKI_TRACE_1("%s enter", __func__);
    450     struct timespec delay;
    451     int timeout = 1000;  /* 10  ms per system tick  */
    452     int err;
    453 
    454     while(!shutdown_timer)
    455     {
    456         delay.tv_sec = timeout / 1000;
    457         delay.tv_nsec = 1000 * 1000 * (timeout%1000);
    458 
    459         /* [u]sleep can't be used because it uses SIGALRM */
    460 
    461         do
    462         {
    463             err = nanosleep(&delay, &delay);
    464         } while (err < 0 && errno ==EINTR);
    465 
    466         GKI_timer_update(1);
    467     }
    468     GKI_TRACE_1("%s exit", __func__);
    469     pthread_exit(NULL);
    470 }
    471 #endif
    472 
    473 /*******************************************************************************
    474 **
    475 ** Function         GKI_run
    476 **
    477 ** Description      This function runs a task
    478 **
    479 ** Parameters:      p_task_id  - (input) pointer to task id
    480 **
    481 ** Returns          void
    482 **
    483 ** NOTE             This function is only needed for operating systems where
    484 **                  starting a task is a 2-step process. Most OS's do it in
    485 **                  one step, If your OS does it in one step, this function
    486 **                  should be empty.
    487 *********************************************************************************/
    488 void GKI_run (void *p_task_id)
    489 {
    490     GKI_TRACE_1("%s enter", __func__);
    491     int retval = EACCES;
    492     static pthread_t workerThreadId = NULL;
    493 
    494     retval = pthread_create (&workerThreadId, NULL, GKI_run_worker_thread, NULL);
    495     if (retval != 0)
    496     {
    497         GKI_TRACE_ERROR_2 ("%s: fail create thread %d", __func__, retval);
    498     }
    499     GKI_TRACE_1("%s exit", __func__);
    500 }
    501 
    502 
    503 /*******************************************************************************
    504 **
    505 ** Function         GKI_run_worker_thread
    506 **
    507 ** Description      This function runs a task
    508 **
    509 ** Parameters:      None
    510 **
    511 ** Returns:         error code
    512 *********************************************************************************/
    513 void* GKI_run_worker_thread (void* dummy)
    514 {
    515     GKI_TRACE_1("%s: enter", __func__);
    516     struct timespec delay;
    517     int err = 0;
    518     volatile int * p_run_cond = &gki_cb.os.no_timer_suspend;
    519 
    520 #ifndef GKI_NO_TICK_STOP
    521     /* register start stop function which disable timer loop in GKI_run() when no timers are
    522      * in any GKI/BTA/BTU this should save power when BTLD is idle! */
    523     GKI_timer_queue_register_callback( gki_system_tick_start_stop_cback );
    524     GKI_TRACE_1( "%s: Start/Stop GKI_timer_update_registered!", __func__ );
    525 #endif
    526 
    527 #ifdef NO_GKI_RUN_RETURN
    528     GKI_TRACE_1("%s: GKI_run == NO_GKI_RUN_RETURN", __func__);
    529     pthread_attr_t timer_attr;
    530 
    531     shutdown_timer = 0;
    532 
    533     pthread_attr_init(&timer_attr);
    534     pthread_attr_setdetachstate(&timer_attr, PTHREAD_CREATE_DETACHED);
    535     if (pthread_create( &timer_thread_id,
    536               &timer_attr,
    537               timer_thread,
    538               NULL) != 0 )
    539     {
    540         GKI_TRACE_1("%s: pthread_create failed to create timer_thread!", __func__);
    541         return NULL;
    542     }
    543 #else
    544     GKI_TRACE_3("%s: run_cond(%x)=%d ", __func__, p_run_cond, *p_run_cond);
    545     for (;GKI_TIMER_TICK_EXIT_COND != *p_run_cond;)
    546     {
    547         do
    548         {
    549             /* adjust hear bit tick in btld by changning TICKS_PER_SEC!!!!! this formula works only for
    550              * 1-1000ms heart beat units! */
    551             delay.tv_sec = LINUX_SEC / 1000;
    552             delay.tv_nsec = 1000 * 1000 * (LINUX_SEC % 1000);
    553 
    554             /* [u]sleep can't be used because it uses SIGALRM */
    555             do
    556             {
    557                 err = nanosleep(&delay, &delay);
    558             } while (err < 0 && errno == EINTR);
    559 
    560             /* the unit should be alsways 1 (1 tick). only if you vary for some reason heart beat tick
    561              * e.g. power saving you may want to provide more ticks
    562              */
    563             GKI_timer_update( 1 );
    564             /* BT_TRACE_2( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, "update: tv_sec: %d, tv_nsec: %d", delay.tv_sec, delay.tv_nsec ); */
    565         } while ( GKI_TIMER_TICK_RUN_COND == *p_run_cond);
    566 
    567         /* currently on reason to exit above loop is no_timer_suspend == GKI_TIMER_TICK_STOP_COND
    568          * block timer main thread till re-armed by  */
    569 #ifdef GKI_TICK_TIMER_DEBUG
    570         BT_TRACE_0( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> SUSPENDED GKI_timer_update()" );
    571 #endif
    572         if (GKI_TIMER_TICK_EXIT_COND != *p_run_cond) {
    573             GKI_TRACE_1("%s: waiting timer mutex", __func__);
    574             pthread_mutex_lock( &gki_cb.os.gki_timer_mutex );
    575             pthread_cond_wait( &gki_cb.os.gki_timer_cond, &gki_cb.os.gki_timer_mutex );
    576             pthread_mutex_unlock( &gki_cb.os.gki_timer_mutex );
    577             GKI_TRACE_1("%s: exited timer mutex", __func__);
    578         }
    579         /* potentially we need to adjust os gki_cb.com.OSTicks */
    580 
    581 #ifdef GKI_TICK_TIMER_DEBUG
    582         BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> RESTARTED GKI_timer_update(): run_cond: %d",
    583                     *p_run_cond );
    584 #endif
    585     } /* for */
    586 #endif
    587     GKI_TRACE_1("%s: exit", __func__);
    588     return NULL;
    589 }
    590 
    591 
    592 /*******************************************************************************
    593 **
    594 ** Function         GKI_stop
    595 **
    596 ** Description      This function is called to stop
    597 **                  the tasks and timers when the system is being stopped
    598 **
    599 ** Returns          void
    600 **
    601 ** NOTE             This function is NOT called by the Widcomm stack and
    602 **                  profiles. If you want to use it in your own implementation,
    603 **                  put specific code here.
    604 **
    605 *******************************************************************************/
    606 void GKI_stop (void)
    607 {
    608     UINT8 task_id;
    609 
    610     /*  gki_queue_timer_cback(FALSE); */
    611     /* TODO - add code here if needed*/
    612 
    613     for(task_id = 0; task_id<GKI_MAX_TASKS; task_id++)
    614     {
    615         if(gki_cb.com.OSRdyTbl[task_id] != TASK_DEAD)
    616         {
    617             GKI_exit_task(task_id);
    618         }
    619     }
    620 }
    621 
    622 
    623 /*******************************************************************************
    624 **
    625 ** Function         GKI_wait
    626 **
    627 ** Description      This function is called by tasks to wait for a specific
    628 **                  event or set of events. The task may specify the duration
    629 **                  that it wants to wait for, or 0 if infinite.
    630 **
    631 ** Parameters:      flag -    (input) the event or set of events to wait for
    632 **                  timeout - (input) the duration that the task wants to wait
    633 **                                    for the specific events (in system ticks)
    634 **
    635 **
    636 ** Returns          the event mask of received events or zero if timeout
    637 **
    638 *******************************************************************************/
    639 UINT16 GKI_wait (UINT16 flag, UINT32 timeout)
    640 {
    641     UINT16 evt;
    642     UINT8 rtask;
    643     struct timespec abstime = { 0, 0 };
    644     int sec;
    645     int nano_sec;
    646 
    647     rtask = GKI_get_taskid();
    648     GKI_TRACE_3("GKI_wait %d %x %d", rtask, flag, timeout);
    649     if (rtask >= GKI_MAX_TASKS) {
    650         pthread_exit(NULL);
    651         return 0;
    652     }
    653 
    654     gki_pthread_info_t* p_pthread_info = &gki_pthread_info[rtask];
    655     if (p_pthread_info->pCond != NULL && p_pthread_info->pMutex != NULL) {
    656         int ret;
    657         GKI_TRACE_3("GKI_wait task=%i, pCond/pMutex = %x/%x", rtask, p_pthread_info->pCond, p_pthread_info->pMutex);
    658         ret = pthread_mutex_lock(p_pthread_info->pMutex);
    659         ret = pthread_cond_signal(p_pthread_info->pCond);
    660         ret = pthread_mutex_unlock(p_pthread_info->pMutex);
    661         p_pthread_info->pMutex = NULL;
    662         p_pthread_info->pCond = NULL;
    663     }
    664     gki_cb.com.OSWaitForEvt[rtask] = flag;
    665 
    666     /* protect OSWaitEvt[rtask] from modification from an other thread */
    667     pthread_mutex_lock(&gki_cb.os.thread_evt_mutex[rtask]);
    668 
    669 #if 0 /* for clean scheduling we probably should always call pthread_cond_wait() */
    670     /* Check if anything in any of the mailboxes. There is a potential race condition where OSTaskQFirst[rtask]
    671      has been modified. however this should only result in addtional call to  pthread_cond_wait() but as
    672      the cond is met, it will exit immediately (depending on schedulling) */
    673     if (gki_cb.com.OSTaskQFirst[rtask][0])
    674     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_0_EVT_MASK;
    675     if (gki_cb.com.OSTaskQFirst[rtask][1])
    676     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_1_EVT_MASK;
    677     if (gki_cb.com.OSTaskQFirst[rtask][2])
    678     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_2_EVT_MASK;
    679     if (gki_cb.com.OSTaskQFirst[rtask][3])
    680     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_3_EVT_MASK;
    681 #endif
    682 
    683     if (!(gki_cb.com.OSWaitEvt[rtask] & flag))
    684     {
    685         if (timeout)
    686         {
    687             //            timeout = GKI_MS_TO_TICKS(timeout);     /* convert from milliseconds to ticks */
    688 
    689             /* get current system time */
    690             //            clock_gettime(CLOCK_MONOTONIC, &currSysTime);
    691             //            abstime.tv_sec = currSysTime.time;
    692             //            abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
    693             clock_gettime(CLOCK_MONOTONIC, &abstime);
    694 
    695             /* add timeout */
    696             sec = timeout / 1000;
    697             nano_sec = (timeout % 1000) * NANOSEC_PER_MILLISEC;
    698             abstime.tv_nsec += nano_sec;
    699             if (abstime.tv_nsec > NSEC_PER_SEC)
    700             {
    701                 abstime.tv_sec += (abstime.tv_nsec / NSEC_PER_SEC);
    702                 abstime.tv_nsec = abstime.tv_nsec % NSEC_PER_SEC;
    703             }
    704             abstime.tv_sec += sec;
    705 
    706             pthread_cond_timedwait_monotonic(&gki_cb.os.thread_evt_cond[rtask],
    707                     &gki_cb.os.thread_evt_mutex[rtask], &abstime);
    708 
    709         }
    710         else
    711         {
    712             pthread_cond_wait(&gki_cb.os.thread_evt_cond[rtask], &gki_cb.os.thread_evt_mutex[rtask]);
    713         }
    714 
    715         /* TODO: check, this is probably neither not needed depending on phtread_cond_wait() implmentation,
    716          e.g. it looks like it is implemented as a counter in which case multiple cond_signal
    717          should NOT be lost! */
    718         // we are waking up after waiting for some events, so refresh variables
    719         // no need to call GKI_disable() here as we know that we will have some events as we've been waking up after condition pending or timeout
    720         if (gki_cb.com.OSTaskQFirst[rtask][0])
    721             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_0_EVT_MASK;
    722         if (gki_cb.com.OSTaskQFirst[rtask][1])
    723             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_1_EVT_MASK;
    724         if (gki_cb.com.OSTaskQFirst[rtask][2])
    725             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_2_EVT_MASK;
    726         if (gki_cb.com.OSTaskQFirst[rtask][3])
    727             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_3_EVT_MASK;
    728 
    729         if (gki_cb.com.OSRdyTbl[rtask] == TASK_DEAD)
    730         {
    731             gki_cb.com.OSWaitEvt[rtask] = 0;
    732             /* unlock thread_evt_mutex as pthread_cond_wait() does auto lock when cond is met */
    733             pthread_mutex_unlock(&gki_cb.os.thread_evt_mutex[rtask]);
    734             GKI_TRACE_1("GKI TASK_DEAD received. exit thread %d...", rtask );
    735 
    736             gki_cb.os.thread_id[rtask] = 0;
    737             pthread_exit(NULL);
    738             return (EVENT_MASK(GKI_SHUTDOWN_EVT));
    739         }
    740     }
    741 
    742     /* Clear the wait for event mask */
    743     gki_cb.com.OSWaitForEvt[rtask] = 0;
    744 
    745     /* Return only those bits which user wants... */
    746     evt = gki_cb.com.OSWaitEvt[rtask] & flag;
    747 
    748     /* Clear only those bits which user wants... */
    749     gki_cb.com.OSWaitEvt[rtask] &= ~flag;
    750 
    751     /* unlock thread_evt_mutex as pthread_cond_wait() does auto lock mutex when cond is met */
    752     pthread_mutex_unlock(&gki_cb.os.thread_evt_mutex[rtask]);
    753     GKI_TRACE_4("GKI_wait %d %x %d %x resumed", rtask, flag, timeout, evt);
    754 
    755     return (evt);
    756 }
    757 
    758 
    759 /*******************************************************************************
    760 **
    761 ** Function         GKI_delay
    762 **
    763 ** Description      This function is called by tasks to sleep unconditionally
    764 **                  for a specified amount of time. The duration is in milliseconds
    765 **
    766 ** Parameters:      timeout -    (input) the duration in milliseconds
    767 **
    768 ** Returns          void
    769 **
    770 *******************************************************************************/
    771 
    772 void GKI_delay (UINT32 timeout)
    773 {
    774     UINT8 rtask = GKI_get_taskid();
    775     struct timespec delay;
    776     int err;
    777 
    778     GKI_TRACE_2("GKI_delay %d %d", rtask, timeout);
    779 
    780     delay.tv_sec = timeout / 1000;
    781     delay.tv_nsec = 1000 * 1000 * (timeout%1000);
    782 
    783     /* [u]sleep can't be used because it uses SIGALRM */
    784 
    785     do {
    786         err = nanosleep(&delay, &delay);
    787     } while (err < 0 && errno ==EINTR);
    788 
    789     /* Check if task was killed while sleeping */
    790     /* NOTE
    791     **      if you do not implement task killing, you do not
    792     **      need this check.
    793     */
    794     if (rtask && gki_cb.com.OSRdyTbl[rtask] == TASK_DEAD)
    795     {
    796     }
    797 
    798     GKI_TRACE_2("GKI_delay %d %d done", rtask, timeout);
    799     return;
    800 }
    801 
    802 
    803 /*******************************************************************************
    804 **
    805 ** Function         GKI_send_event
    806 **
    807 ** Description      This function is called by tasks to send events to other
    808 **                  tasks. Tasks can also send events to themselves.
    809 **
    810 ** Parameters:      task_id -  (input) The id of the task to which the event has to
    811 **                  be sent
    812 **                  event   -  (input) The event that has to be sent
    813 **
    814 **
    815 ** Returns          GKI_SUCCESS if all OK, else GKI_FAILURE
    816 **
    817 *******************************************************************************/
    818 UINT8 GKI_send_event (UINT8 task_id, UINT16 event)
    819 {
    820     GKI_TRACE_2("GKI_send_event %d %x", task_id, event);
    821 
    822     /* use efficient coding to avoid pipeline stalls */
    823     if (task_id < GKI_MAX_TASKS)
    824     {
    825         /* protect OSWaitEvt[task_id] from manipulation in GKI_wait() */
    826         pthread_mutex_lock(&gki_cb.os.thread_evt_mutex[task_id]);
    827 
    828         /* Set the event bit */
    829         gki_cb.com.OSWaitEvt[task_id] |= event;
    830 
    831         pthread_cond_signal(&gki_cb.os.thread_evt_cond[task_id]);
    832 
    833         pthread_mutex_unlock(&gki_cb.os.thread_evt_mutex[task_id]);
    834 
    835         GKI_TRACE_2("GKI_send_event %d %x done", task_id, event);
    836         return ( GKI_SUCCESS );
    837     }
    838     return (GKI_FAILURE);
    839 }
    840 
    841 
    842 /*******************************************************************************
    843 **
    844 ** Function         GKI_isend_event
    845 **
    846 ** Description      This function is called from ISRs to send events to other
    847 **                  tasks. The only difference between this function and GKI_send_event
    848 **                  is that this function assumes interrupts are already disabled.
    849 **
    850 ** Parameters:      task_id -  (input) The destination task Id for the event.
    851 **                  event   -  (input) The event flag
    852 **
    853 ** Returns          GKI_SUCCESS if all OK, else GKI_FAILURE
    854 **
    855 ** NOTE             This function is NOT called by the Widcomm stack and
    856 **                  profiles. If you want to use it in your own implementation,
    857 **                  put your code here, otherwise you can delete the entire
    858 **                  body of the function.
    859 **
    860 *******************************************************************************/
    861 UINT8 GKI_isend_event (UINT8 task_id, UINT16 event)
    862 {
    863 
    864     GKI_TRACE_2("GKI_isend_event %d %x", task_id, event);
    865     GKI_TRACE_2("GKI_isend_event %d %x done", task_id, event);
    866     return    GKI_send_event(task_id, event);
    867 }
    868 
    869 
    870 /*******************************************************************************
    871 **
    872 ** Function         GKI_get_taskid
    873 **
    874 ** Description      This function gets the currently running task ID.
    875 **
    876 ** Returns          task ID
    877 **
    878 ** NOTE             The Widcomm upper stack and profiles may run as a single task.
    879 **                  If you only have one GKI task, then you can hard-code this
    880 **                  function to return a '1'. Otherwise, you should have some
    881 **                  OS-specific method to determine the current task.
    882 **
    883 *******************************************************************************/
    884 UINT8 GKI_get_taskid (void)
    885 {
    886     int i;
    887 
    888     pthread_t thread_id = pthread_self( );
    889     for (i = 0; i < GKI_MAX_TASKS; i++) {
    890         if (gki_cb.os.thread_id[i] == thread_id) {
    891             GKI_TRACE_2("GKI_get_taskid %x %d done", thread_id, i);
    892             return(i);
    893         }
    894     }
    895 
    896     GKI_TRACE_1("GKI_get_taskid: thread id = %x, task id = -1", thread_id);
    897 
    898     return(-1);
    899 }
    900 
    901 /*******************************************************************************
    902 **
    903 ** Function         GKI_map_taskname
    904 **
    905 ** Description      This function gets the task name of the taskid passed as arg.
    906 **                  If GKI_MAX_TASKS is passed as arg the currently running task
    907 **                  name is returned
    908 **
    909 ** Parameters:      task_id -  (input) The id of the task whose name is being
    910 **                  sought. GKI_MAX_TASKS is passed to get the name of the
    911 **                  currently running task.
    912 **
    913 ** Returns          pointer to task name
    914 **
    915 ** NOTE             this function needs no customization
    916 **
    917 *******************************************************************************/
    918 UINT8 *GKI_map_taskname (UINT8 task_id)
    919 {
    920     GKI_TRACE_1("GKI_map_taskname %d", task_id);
    921 
    922     if (task_id < GKI_MAX_TASKS)
    923     {
    924         GKI_TRACE_2("GKI_map_taskname %d %s done", task_id, gki_cb.com.OSTName[task_id]);
    925          return (gki_cb.com.OSTName[task_id]);
    926     }
    927     else if (task_id == GKI_MAX_TASKS )
    928     {
    929         return (gki_cb.com.OSTName[GKI_get_taskid()]);
    930     }
    931     else
    932     {
    933         return (UINT8*) "BAD";
    934     }
    935 }
    936 
    937 
    938 /*******************************************************************************
    939 **
    940 ** Function         GKI_enable
    941 **
    942 ** Description      This function enables interrupts.
    943 **
    944 ** Returns          void
    945 **
    946 *******************************************************************************/
    947 void GKI_enable (void)
    948 {
    949     GKI_TRACE_0("GKI_enable");
    950     pthread_mutex_unlock(&gki_cb.os.GKI_mutex);
    951 /* 	pthread_mutex_xx is nesting save, no need for this: already_disabled = 0; */
    952     GKI_TRACE_0("Leaving GKI_enable");
    953     return;
    954 }
    955 
    956 
    957 /*******************************************************************************
    958 **
    959 ** Function         GKI_disable
    960 **
    961 ** Description      This function disables interrupts.
    962 **
    963 ** Returns          void
    964 **
    965 *******************************************************************************/
    966 
    967 void GKI_disable (void)
    968 {
    969     //GKI_TRACE_0("GKI_disable");
    970 
    971 /*	pthread_mutex_xx is nesting save, no need for this: if (!already_disabled) {
    972     already_disabled = 1; */
    973     		pthread_mutex_lock(&gki_cb.os.GKI_mutex);
    974 /*  } */
    975     //GKI_TRACE_0("Leaving GKI_disable");
    976     return;
    977 }
    978 
    979 
    980 /*******************************************************************************
    981 **
    982 ** Function         GKI_exception
    983 **
    984 ** Description      This function throws an exception.
    985 **                  This is normally only called for a nonrecoverable error.
    986 **
    987 ** Parameters:      code    -  (input) The code for the error
    988 **                  msg     -  (input) The message that has to be logged
    989 **
    990 ** Returns          void
    991 **
    992 *******************************************************************************/
    993 
    994 void GKI_exception (UINT16 code, char *msg)
    995 {
    996     UINT8 task_id;
    997     int i = 0;
    998 
    999     GKI_TRACE_ERROR_0( "GKI_exception(): Task State Table");
   1000 
   1001     for(task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
   1002     {
   1003         GKI_TRACE_ERROR_3( "TASK ID [%d] task name [%s] state [%d]",
   1004                          task_id,
   1005                          gki_cb.com.OSTName[task_id],
   1006                          gki_cb.com.OSRdyTbl[task_id]);
   1007     }
   1008 
   1009     GKI_TRACE_ERROR_2("GKI_exception %d %s", code, msg);
   1010     GKI_TRACE_ERROR_0( "********************************************************************");
   1011     GKI_TRACE_ERROR_2( "* GKI_exception(): %d %s", code, msg);
   1012     GKI_TRACE_ERROR_0( "********************************************************************");
   1013 
   1014 #if (GKI_DEBUG == TRUE)
   1015     GKI_disable();
   1016 
   1017     if (gki_cb.com.ExceptionCnt < GKI_MAX_EXCEPTION)
   1018     {
   1019         EXCEPTION_T *pExp;
   1020 
   1021         pExp =  &gki_cb.com.Exception[gki_cb.com.ExceptionCnt++];
   1022         pExp->type = code;
   1023         pExp->taskid = GKI_get_taskid();
   1024         strncpy((char *)pExp->msg, msg, GKI_MAX_EXCEPTION_MSGLEN - 1);
   1025     }
   1026 
   1027     GKI_enable();
   1028 #endif
   1029 
   1030     GKI_TRACE_ERROR_2("GKI_exception %d %s done", code, msg);
   1031 
   1032 
   1033     return;
   1034 }
   1035 
   1036 
   1037 /*******************************************************************************
   1038 **
   1039 ** Function         GKI_get_time_stamp
   1040 **
   1041 ** Description      This function formats the time into a user area
   1042 **
   1043 ** Parameters:      tbuf -  (output) the address to the memory containing the
   1044 **                  formatted time
   1045 **
   1046 ** Returns          the address of the user area containing the formatted time
   1047 **                  The format of the time is ????
   1048 **
   1049 ** NOTE             This function is only called by OBEX.
   1050 **
   1051 *******************************************************************************/
   1052 INT8 *GKI_get_time_stamp (INT8 *tbuf)
   1053 {
   1054     UINT32 ms_time;
   1055     UINT32 s_time;
   1056     UINT32 m_time;
   1057     UINT32 h_time;
   1058     INT8   *p_out = tbuf;
   1059 
   1060     gki_cb.com.OSTicks = times(0);
   1061     ms_time = GKI_TICKS_TO_MS(gki_cb.com.OSTicks);
   1062     s_time  = ms_time/100;   /* 100 Ticks per second */
   1063     m_time  = s_time/60;
   1064     h_time  = m_time/60;
   1065 
   1066     ms_time -= s_time*100;
   1067     s_time  -= m_time*60;
   1068     m_time  -= h_time*60;
   1069 
   1070     *p_out++ = (INT8)((h_time / 10) + '0');
   1071     *p_out++ = (INT8)((h_time % 10) + '0');
   1072     *p_out++ = ':';
   1073     *p_out++ = (INT8)((m_time / 10) + '0');
   1074     *p_out++ = (INT8)((m_time % 10) + '0');
   1075     *p_out++ = ':';
   1076     *p_out++ = (INT8)((s_time / 10) + '0');
   1077     *p_out++ = (INT8)((s_time % 10) + '0');
   1078     *p_out++ = ':';
   1079     *p_out++ = (INT8)((ms_time / 10) + '0');
   1080     *p_out++ = (INT8)((ms_time % 10) + '0');
   1081     *p_out++ = ':';
   1082     *p_out   = 0;
   1083 
   1084     return (tbuf);
   1085 }
   1086 
   1087 
   1088 /*******************************************************************************
   1089 **
   1090 ** Function         GKI_register_mempool
   1091 **
   1092 ** Description      This function registers a specific memory pool.
   1093 **
   1094 ** Parameters:      p_mem -  (input) pointer to the memory pool
   1095 **
   1096 ** Returns          void
   1097 **
   1098 ** NOTE             This function is NOT called by the Widcomm stack and
   1099 **                  profiles. If your OS has different memory pools, you
   1100 **                  can tell GKI the pool to use by calling this function.
   1101 **
   1102 *******************************************************************************/
   1103 void GKI_register_mempool (void *p_mem)
   1104 {
   1105     gki_cb.com.p_user_mempool = p_mem;
   1106 
   1107     return;
   1108 }
   1109 
   1110 /*******************************************************************************
   1111 **
   1112 ** Function         GKI_os_malloc
   1113 **
   1114 ** Description      This function allocates memory
   1115 **
   1116 ** Parameters:      size -  (input) The size of the memory that has to be
   1117 **                  allocated
   1118 **
   1119 ** Returns          the address of the memory allocated, or NULL if failed
   1120 **
   1121 ** NOTE             This function is called by the Widcomm stack when
   1122 **                  dynamic memory allocation is used. (see dyn_mem.h)
   1123 **
   1124 *******************************************************************************/
   1125 void *GKI_os_malloc (UINT32 size)
   1126 {
   1127     return (malloc(size));
   1128 }
   1129 
   1130 /*******************************************************************************
   1131 **
   1132 ** Function         GKI_os_free
   1133 **
   1134 ** Description      This function frees memory
   1135 **
   1136 ** Parameters:      size -  (input) The address of the memory that has to be
   1137 **                  freed
   1138 **
   1139 ** Returns          void
   1140 **
   1141 ** NOTE             This function is NOT called by the Widcomm stack and
   1142 **                  profiles. It is only called from within GKI if dynamic
   1143 **
   1144 *******************************************************************************/
   1145 void GKI_os_free (void *p_mem)
   1146 {
   1147     if(p_mem != NULL)
   1148 		free(p_mem);
   1149     return;
   1150 }
   1151 
   1152 
   1153 /*******************************************************************************
   1154 **
   1155 ** Function         GKI_suspend_task()
   1156 **
   1157 ** Description      This function suspends the task specified in the argument.
   1158 **
   1159 ** Parameters:      task_id  - (input) the id of the task that has to suspended
   1160 **
   1161 ** Returns          GKI_SUCCESS if all OK, else GKI_FAILURE
   1162 **
   1163 ** NOTE             This function is NOT called by the Widcomm stack and
   1164 **                  profiles. If you want to implement task suspension capability,
   1165 **                  put specific code here.
   1166 **
   1167 *******************************************************************************/
   1168 UINT8 GKI_suspend_task (UINT8 task_id)
   1169 {
   1170     GKI_TRACE_1("GKI_suspend_task %d - NOT implemented", task_id);
   1171 
   1172 
   1173     GKI_TRACE_1("GKI_suspend_task %d done", task_id);
   1174 
   1175     return (GKI_SUCCESS);
   1176 }
   1177 
   1178 
   1179 /*******************************************************************************
   1180 **
   1181 ** Function         GKI_resume_task()
   1182 **
   1183 ** Description      This function resumes the task specified in the argument.
   1184 **
   1185 ** Parameters:      task_id  - (input) the id of the task that has to resumed
   1186 **
   1187 ** Returns          GKI_SUCCESS if all OK
   1188 **
   1189 ** NOTE             This function is NOT called by the Widcomm stack and
   1190 **                  profiles. If you want to implement task suspension capability,
   1191 **                  put specific code here.
   1192 **
   1193 *******************************************************************************/
   1194 UINT8 GKI_resume_task (UINT8 task_id)
   1195 {
   1196     GKI_TRACE_1("GKI_resume_task %d - NOT implemented", task_id);
   1197 
   1198 
   1199     GKI_TRACE_1("GKI_resume_task %d done", task_id);
   1200 
   1201     return (GKI_SUCCESS);
   1202 }
   1203 
   1204 
   1205 /*******************************************************************************
   1206 **
   1207 ** Function         GKI_exit_task
   1208 **
   1209 ** Description      This function is called to stop a GKI task.
   1210 **
   1211 ** Parameters:      task_id  - (input) the id of the task that has to be stopped
   1212 **
   1213 ** Returns          void
   1214 **
   1215 ** NOTE             This function is NOT called by the Widcomm stack and
   1216 **                  profiles. If you want to use it in your own implementation,
   1217 **                  put specific code here to kill a task.
   1218 **
   1219 *******************************************************************************/
   1220 void GKI_exit_task (UINT8 task_id)
   1221 {
   1222     GKI_disable();
   1223     gki_cb.com.OSRdyTbl[task_id] = TASK_DEAD;
   1224 
   1225     /* Destroy mutex and condition variable objects */
   1226     pthread_mutex_destroy(&gki_cb.os.thread_evt_mutex[task_id]);
   1227     pthread_cond_destroy (&gki_cb.os.thread_evt_cond[task_id]);
   1228     pthread_mutex_destroy(&gki_cb.os.thread_timeout_mutex[task_id]);
   1229     pthread_cond_destroy (&gki_cb.os.thread_timeout_cond[task_id]);
   1230 
   1231     GKI_enable();
   1232 
   1233 	//GKI_send_event(task_id, EVENT_MASK(GKI_SHUTDOWN_EVT));
   1234 
   1235     GKI_TRACE_1("GKI_exit_task %d done", task_id);
   1236     return;
   1237 }
   1238 
   1239 
   1240 /*******************************************************************************
   1241 **
   1242 ** Function         GKI_sched_lock
   1243 **
   1244 ** Description      This function is called by tasks to disable scheduler
   1245 **                  task context switching.
   1246 **
   1247 ** Returns          void
   1248 **
   1249 ** NOTE             This function is NOT called by the Widcomm stack and
   1250 **                  profiles. If you want to use it in your own implementation,
   1251 **                  put code here to tell the OS to disable context switching.
   1252 **
   1253 *******************************************************************************/
   1254 void GKI_sched_lock(void)
   1255 {
   1256     GKI_TRACE_0("GKI_sched_lock");
   1257     GKI_disable ();
   1258     return;
   1259 }
   1260 
   1261 
   1262 /*******************************************************************************
   1263 **
   1264 ** Function         GKI_sched_unlock
   1265 **
   1266 ** Description      This function is called by tasks to enable scheduler switching.
   1267 **
   1268 ** Returns          void
   1269 **
   1270 ** NOTE             This function is NOT called by the Widcomm stack and
   1271 **                  profiles. If you want to use it in your own implementation,
   1272 **                  put code here to tell the OS to re-enable context switching.
   1273 **
   1274 *******************************************************************************/
   1275 void GKI_sched_unlock(void)
   1276 {
   1277     GKI_TRACE_0("GKI_sched_unlock");
   1278     GKI_enable ();
   1279 }
   1280 
   1281 /*******************************************************************************
   1282 **
   1283 ** Function         GKI_shiftdown
   1284 **
   1285 ** Description      shift memory down (to make space to insert a record)
   1286 **
   1287 *******************************************************************************/
   1288 void GKI_shiftdown (UINT8 *p_mem, UINT32 len, UINT32 shift_amount)
   1289 {
   1290     register UINT8 *ps = p_mem + len - 1;
   1291     register UINT8 *pd = ps + shift_amount;
   1292     register UINT32 xx;
   1293 
   1294     for (xx = 0; xx < len; xx++)
   1295         *pd-- = *ps--;
   1296 }
   1297 
   1298 /*******************************************************************************
   1299 **
   1300 ** Function         GKI_shiftup
   1301 **
   1302 ** Description      shift memory up (to delete a record)
   1303 **
   1304 *******************************************************************************/
   1305 void GKI_shiftup (UINT8 *p_dest, UINT8 *p_src, UINT32 len)
   1306 {
   1307     register UINT8 *ps = p_src;
   1308     register UINT8 *pd = p_dest;
   1309     register UINT32 xx;
   1310 
   1311     for (xx = 0; xx < len; xx++)
   1312         *pd++ = *ps++;
   1313 }
   1314 
   1315 
   1316