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