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