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