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