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