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