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