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