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 "OverrideLog.h" 19 #include "NfcAdaptation.h" 20 extern "C" 21 { 22 #include "gki.h" 23 #include "nfa_api.h" 24 #include "nfc_int.h" 25 #include "vendor_cfg.h" 26 } 27 #include "config.h" 28 #include "android_logmsg.h" 29 30 #define LOG_TAG "NfcAdaptation" 31 32 extern "C" void GKI_shutdown(); 33 extern void resetConfig(); 34 extern "C" void verify_stack_non_volatile_store (); 35 extern "C" void delete_stack_non_volatile_store (BOOLEAN forceDelete); 36 37 NfcAdaptation* NfcAdaptation::mpInstance = NULL; 38 ThreadMutex NfcAdaptation::sLock; 39 nfc_nci_device_t* NfcAdaptation::mHalDeviceContext = NULL; 40 tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = NULL; 41 tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = NULL; 42 ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent; 43 ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent; 44 45 UINT32 ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00; 46 UINT8 appl_trace_level = 0xff; 47 char bcm_nfc_location[120]; 48 char nci_hal_module[64]; 49 50 static UINT8 nfa_dm_cfg[sizeof ( tNFA_DM_CFG ) ]; 51 static UINT8 nfa_proprietary_cfg[sizeof ( tNFA_PROPRIETARY_CFG )]; 52 extern tNFA_DM_CFG *p_nfa_dm_cfg; 53 extern tNFA_PROPRIETARY_CFG *p_nfa_proprietary_cfg; 54 extern UINT8 nfa_ee_max_ee_cfg; 55 extern const UINT8 nfca_version_string []; 56 extern const UINT8 nfa_version_string []; 57 static UINT8 deviceHostWhiteList [NFA_HCI_MAX_HOST_IN_NETWORK]; 58 static tNFA_HCI_CFG jni_nfa_hci_cfg; 59 extern tNFA_HCI_CFG *p_nfa_hci_cfg; 60 extern BOOLEAN nfa_poll_bail_out_mode; 61 62 /******************************************************************************* 63 ** 64 ** Function: NfcAdaptation::NfcAdaptation() 65 ** 66 ** Description: class constructor 67 ** 68 ** Returns: none 69 ** 70 *******************************************************************************/ 71 NfcAdaptation::NfcAdaptation() 72 { 73 memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs)); 74 } 75 76 /******************************************************************************* 77 ** 78 ** Function: NfcAdaptation::~NfcAdaptation() 79 ** 80 ** Description: class destructor 81 ** 82 ** Returns: none 83 ** 84 *******************************************************************************/ 85 NfcAdaptation::~NfcAdaptation() 86 { 87 mpInstance = NULL; 88 } 89 90 /******************************************************************************* 91 ** 92 ** Function: NfcAdaptation::GetInstance() 93 ** 94 ** Description: access class singleton 95 ** 96 ** Returns: pointer to the singleton object 97 ** 98 *******************************************************************************/ 99 NfcAdaptation& NfcAdaptation::GetInstance() 100 { 101 AutoThreadMutex a(sLock); 102 103 if (!mpInstance) 104 mpInstance = new NfcAdaptation; 105 return *mpInstance; 106 } 107 108 /******************************************************************************* 109 ** 110 ** Function: NfcAdaptation::Initialize() 111 ** 112 ** Description: class initializer 113 ** 114 ** Returns: none 115 ** 116 *******************************************************************************/ 117 void NfcAdaptation::Initialize () 118 { 119 const char* func = "NfcAdaptation::Initialize"; 120 ALOGD("%s: enter", func); 121 ALOGE("%s: ver=%s nfa=%s", func, nfca_version_string, nfa_version_string); 122 unsigned long num; 123 124 if ( GetNumValue ( NAME_USE_RAW_NCI_TRACE, &num, sizeof ( num ) ) ) 125 { 126 if (num == 1) 127 { 128 // display protocol traces in raw format 129 ProtoDispAdapterUseRawOutput (TRUE); 130 ALOGD("%s: logging protocol in raw format", func); 131 } 132 } 133 if ( !GetStrValue ( NAME_NFA_STORAGE, bcm_nfc_location, sizeof ( bcm_nfc_location ) ) ) 134 { 135 strlcpy (bcm_nfc_location, "/data/nfc", sizeof(bcm_nfc_location)); 136 } 137 138 initializeProtocolLogLevel (); 139 140 if ( GetStrValue ( NAME_NFA_DM_CFG, (char*)nfa_dm_cfg, sizeof ( nfa_dm_cfg ) ) ) 141 p_nfa_dm_cfg = ( tNFA_DM_CFG * ) &nfa_dm_cfg[0]; 142 143 if ( GetNumValue ( NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof ( num ) ) ) 144 { 145 nfa_ee_max_ee_cfg = num; 146 ALOGD("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d", func, nfa_ee_max_ee_cfg); 147 } 148 if ( GetNumValue ( NAME_NFA_POLL_BAIL_OUT_MODE, &num, sizeof ( num ) ) ) 149 { 150 nfa_poll_bail_out_mode = num; 151 ALOGD("%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func, nfa_poll_bail_out_mode); 152 } 153 154 if ( GetStrValue ( NAME_NFA_PROPRIETARY_CFG, (char*)nfa_proprietary_cfg, sizeof ( tNFA_PROPRIETARY_CFG ) ) ) 155 { 156 p_nfa_proprietary_cfg = (tNFA_PROPRIETARY_CFG*) &nfa_proprietary_cfg[0]; 157 } 158 159 //configure device host whitelist of HCI host ID's; see specification ETSI TS 102 622 V11.1.10 160 //(2012-10), section 6.1.3.1 161 num = GetStrValue ( NAME_DEVICE_HOST_WHITE_LIST, (char*) deviceHostWhiteList, sizeof ( deviceHostWhiteList ) ); 162 if (num) 163 { 164 memmove (&jni_nfa_hci_cfg, p_nfa_hci_cfg, sizeof(jni_nfa_hci_cfg)); 165 jni_nfa_hci_cfg.num_whitelist_host = (UINT8) num; //number of HCI host ID's in the whitelist 166 jni_nfa_hci_cfg.p_whitelist = deviceHostWhiteList; //array of HCI host ID's 167 p_nfa_hci_cfg = &jni_nfa_hci_cfg; 168 } 169 170 initializeGlobalAppLogLevel (); 171 172 verify_stack_non_volatile_store (); 173 if ( GetNumValue ( NAME_PRESERVE_STORAGE, (char*)&num, sizeof ( num ) ) && 174 (num == 1) ) 175 ALOGD ("%s: preserve stack NV store", __FUNCTION__); 176 else 177 { 178 delete_stack_non_volatile_store (FALSE); 179 } 180 181 GKI_init (); 182 GKI_enable (); 183 GKI_create_task ((TASKPTR)NFCA_TASK, BTU_TASK, (INT8*)"NFCA_TASK", 0, 0, (pthread_cond_t*)NULL, NULL); 184 { 185 AutoThreadMutex guard(mCondVar); 186 GKI_create_task ((TASKPTR)Thread, MMI_TASK, (INT8*)"NFCA_THREAD", 0, 0, (pthread_cond_t*)NULL, NULL); 187 mCondVar.wait(); 188 } 189 190 mHalDeviceContext = NULL; 191 mHalCallback = NULL; 192 memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs)); 193 InitializeHalDeviceContext (); 194 ALOGD ("%s: exit", func); 195 } 196 197 /******************************************************************************* 198 ** 199 ** Function: NfcAdaptation::Finalize() 200 ** 201 ** Description: class finalizer 202 ** 203 ** Returns: none 204 ** 205 *******************************************************************************/ 206 void NfcAdaptation::Finalize() 207 { 208 const char* func = "NfcAdaptation::Finalize"; 209 AutoThreadMutex a(sLock); 210 211 ALOGD ("%s: enter", func); 212 GKI_shutdown (); 213 214 resetConfig(); 215 216 nfc_nci_close(mHalDeviceContext); //close the HAL's device context 217 mHalDeviceContext = NULL; 218 mHalCallback = NULL; 219 memset (&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs)); 220 221 ALOGD ("%s: exit", func); 222 delete this; 223 } 224 225 /******************************************************************************* 226 ** 227 ** Function: NfcAdaptation::signal() 228 ** 229 ** Description: signal the CondVar to release the thread that is waiting 230 ** 231 ** Returns: none 232 ** 233 *******************************************************************************/ 234 void NfcAdaptation::signal () 235 { 236 mCondVar.signal(); 237 } 238 239 /******************************************************************************* 240 ** 241 ** Function: NfcAdaptation::NFCA_TASK() 242 ** 243 ** Description: NFCA_TASK runs the GKI main task 244 ** 245 ** Returns: none 246 ** 247 *******************************************************************************/ 248 UINT32 NfcAdaptation::NFCA_TASK (UINT32 arg) 249 { 250 const char* func = "NfcAdaptation::NFCA_TASK"; 251 ALOGD ("%s: enter", func); 252 GKI_run (0); 253 ALOGD ("%s: exit", func); 254 return 0; 255 } 256 257 /******************************************************************************* 258 ** 259 ** Function: NfcAdaptation::Thread() 260 ** 261 ** Description: Creates work threads 262 ** 263 ** Returns: none 264 ** 265 *******************************************************************************/ 266 UINT32 NfcAdaptation::Thread (UINT32 arg) 267 { 268 const char* func = "NfcAdaptation::Thread"; 269 ALOGD ("%s: enter", func); 270 271 { 272 ThreadCondVar CondVar; 273 AutoThreadMutex guard(CondVar); 274 GKI_create_task ((TASKPTR)nfc_task, NFC_TASK, (INT8*)"NFC_TASK", 0, 0, (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar); 275 CondVar.wait(); 276 } 277 278 NfcAdaptation::GetInstance().signal(); 279 280 GKI_exit_task (GKI_get_taskid ()); 281 ALOGD ("%s: exit", func); 282 return 0; 283 } 284 285 /******************************************************************************* 286 ** 287 ** Function: NfcAdaptation::GetHalEntryFuncs() 288 ** 289 ** Description: Get the set of HAL entry points. 290 ** 291 ** Returns: Functions pointers for HAL entry points. 292 ** 293 *******************************************************************************/ 294 tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs () 295 { 296 return &mHalEntryFuncs; 297 } 298 299 /******************************************************************************* 300 ** 301 ** Function: NfcAdaptation::InitializeHalDeviceContext 302 ** 303 ** Description: Ask the generic Android HAL to find the Broadcom-specific HAL. 304 ** 305 ** Returns: None. 306 ** 307 *******************************************************************************/ 308 void NfcAdaptation::InitializeHalDeviceContext () 309 { 310 const char* func = "NfcAdaptation::InitializeHalDeviceContext"; 311 ALOGD ("%s: enter", func); 312 int ret = 0; //0 means success 313 if ( !GetStrValue ( NAME_NCI_HAL_MODULE, nci_hal_module, sizeof ( nci_hal_module) ) ) 314 { 315 ALOGE("No HAL module specified in config, falling back to BCM2079x"); 316 strlcpy (nci_hal_module, "nfc_nci.bcm2079x", sizeof(nci_hal_module)); 317 } 318 const hw_module_t* hw_module = NULL; 319 320 mHalEntryFuncs.initialize = HalInitialize; 321 mHalEntryFuncs.terminate = HalTerminate; 322 mHalEntryFuncs.open = HalOpen; 323 mHalEntryFuncs.close = HalClose; 324 mHalEntryFuncs.core_initialized = HalCoreInitialized; 325 mHalEntryFuncs.write = HalWrite; 326 mHalEntryFuncs.prediscover = HalPrediscover; 327 mHalEntryFuncs.control_granted = HalControlGranted; 328 mHalEntryFuncs.power_cycle = HalPowerCycle; 329 mHalEntryFuncs.get_max_ee = HalGetMaxNfcee; 330 331 ret = hw_get_module (nci_hal_module, &hw_module); 332 if (ret == 0) 333 { 334 ret = nfc_nci_open (hw_module, &mHalDeviceContext); 335 if (ret != 0) 336 ALOGE ("%s: nfc_nci_open fail", func); 337 } 338 else 339 ALOGE ("%s: fail hw_get_module %s", func, nci_hal_module); 340 ALOGD ("%s: exit", func); 341 } 342 343 /******************************************************************************* 344 ** 345 ** Function: NfcAdaptation::HalInitialize 346 ** 347 ** Description: Not implemented because this function is only needed 348 ** within the HAL. 349 ** 350 ** Returns: None. 351 ** 352 *******************************************************************************/ 353 void NfcAdaptation::HalInitialize () 354 { 355 const char* func = "NfcAdaptation::HalInitialize"; 356 ALOGD ("%s", func); 357 } 358 359 /******************************************************************************* 360 ** 361 ** Function: NfcAdaptation::HalTerminate 362 ** 363 ** Description: Not implemented because this function is only needed 364 ** within the HAL. 365 ** 366 ** Returns: None. 367 ** 368 *******************************************************************************/ 369 void NfcAdaptation::HalTerminate () 370 { 371 const char* func = "NfcAdaptation::HalTerminate"; 372 ALOGD ("%s", func); 373 } 374 375 /******************************************************************************* 376 ** 377 ** Function: NfcAdaptation::HalOpen 378 ** 379 ** Description: Turn on controller, download firmware. 380 ** 381 ** Returns: None. 382 ** 383 *******************************************************************************/ 384 void NfcAdaptation::HalOpen (tHAL_NFC_CBACK *p_hal_cback, tHAL_NFC_DATA_CBACK* p_data_cback) 385 { 386 const char* func = "NfcAdaptation::HalOpen"; 387 ALOGD ("%s", func); 388 if (mHalDeviceContext) 389 { 390 mHalCallback = p_hal_cback; 391 mHalDataCallback = p_data_cback; 392 mHalDeviceContext->open (mHalDeviceContext, HalDeviceContextCallback, HalDeviceContextDataCallback); 393 } 394 } 395 396 /******************************************************************************* 397 ** 398 ** Function: NfcAdaptation::HalClose 399 ** 400 ** Description: Turn off controller. 401 ** 402 ** Returns: None. 403 ** 404 *******************************************************************************/ 405 void NfcAdaptation::HalClose () 406 { 407 const char* func = "NfcAdaptation::HalClose"; 408 ALOGD ("%s", func); 409 if (mHalDeviceContext) 410 { 411 mHalDeviceContext->close (mHalDeviceContext); 412 } 413 } 414 415 /******************************************************************************* 416 ** 417 ** Function: NfcAdaptation::HalDeviceContextCallback 418 ** 419 ** Description: Translate generic Android HAL's callback into Broadcom-specific 420 ** callback function. 421 ** 422 ** Returns: None. 423 ** 424 *******************************************************************************/ 425 void NfcAdaptation::HalDeviceContextCallback (nfc_event_t event, nfc_status_t event_status) 426 { 427 const char* func = "NfcAdaptation::HalDeviceContextCallback"; 428 ALOGD ("%s: event=%u", func, event); 429 if (mHalCallback) 430 mHalCallback (event, (tHAL_NFC_STATUS) event_status); 431 } 432 433 /******************************************************************************* 434 ** 435 ** Function: NfcAdaptation::HalDeviceContextDataCallback 436 ** 437 ** Description: Translate generic Android HAL's callback into Broadcom-specific 438 ** callback function. 439 ** 440 ** Returns: None. 441 ** 442 *******************************************************************************/ 443 void NfcAdaptation::HalDeviceContextDataCallback (uint16_t data_len, uint8_t* p_data) 444 { 445 const char* func = "NfcAdaptation::HalDeviceContextDataCallback"; 446 ALOGD ("%s: len=%u", func, data_len); 447 if (mHalDataCallback) 448 mHalDataCallback (data_len, p_data); 449 } 450 451 /******************************************************************************* 452 ** 453 ** Function: NfcAdaptation::HalWrite 454 ** 455 ** Description: Write NCI message to the controller. 456 ** 457 ** Returns: None. 458 ** 459 *******************************************************************************/ 460 void NfcAdaptation::HalWrite (UINT16 data_len, UINT8* p_data) 461 { 462 const char* func = "NfcAdaptation::HalWrite"; 463 ALOGD ("%s", func); 464 if (mHalDeviceContext) 465 { 466 mHalDeviceContext->write (mHalDeviceContext, data_len, p_data); 467 } 468 } 469 470 /******************************************************************************* 471 ** 472 ** Function: NfcAdaptation::HalCoreInitialized 473 ** 474 ** Description: Adjust the configurable parameters in the controller. 475 ** 476 ** Returns: None. 477 ** 478 *******************************************************************************/ 479 void NfcAdaptation::HalCoreInitialized (UINT8* p_core_init_rsp_params) 480 { 481 const char* func = "NfcAdaptation::HalCoreInitialized"; 482 ALOGD ("%s", func); 483 if (mHalDeviceContext) 484 { 485 mHalDeviceContext->core_initialized (mHalDeviceContext, p_core_init_rsp_params); 486 } 487 } 488 489 /******************************************************************************* 490 ** 491 ** Function: NfcAdaptation::HalPrediscover 492 ** 493 ** Description: Perform any vendor-specific pre-discovery actions (if needed) 494 ** If any actions were performed TRUE will be returned, and 495 ** HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are 496 ** completed. 497 ** 498 ** Returns: TRUE if vendor-specific pre-discovery actions initialized 499 ** FALSE if no vendor-specific pre-discovery actions are needed. 500 ** 501 *******************************************************************************/ 502 BOOLEAN NfcAdaptation::HalPrediscover () 503 { 504 const char* func = "NfcAdaptation::HalPrediscover"; 505 ALOGD ("%s", func); 506 BOOLEAN retval = FALSE; 507 508 if (mHalDeviceContext) 509 { 510 retval = mHalDeviceContext->pre_discover (mHalDeviceContext); 511 } 512 return retval; 513 } 514 515 /******************************************************************************* 516 ** 517 ** Function: HAL_NfcControlGranted 518 ** 519 ** Description: Grant control to HAL control for sending NCI commands. 520 ** Call in response to HAL_REQUEST_CONTROL_EVT. 521 ** Must only be called when there are no NCI commands pending. 522 ** HAL_RELEASE_CONTROL_EVT will notify when HAL no longer 523 ** needs control of NCI. 524 ** 525 ** Returns: void 526 ** 527 *******************************************************************************/ 528 void NfcAdaptation::HalControlGranted () 529 { 530 const char* func = "NfcAdaptation::HalControlGranted"; 531 ALOGD ("%s", func); 532 if (mHalDeviceContext) 533 { 534 mHalDeviceContext->control_granted (mHalDeviceContext); 535 } 536 } 537 538 /******************************************************************************* 539 ** 540 ** Function: NfcAdaptation::HalPowerCycle 541 ** 542 ** Description: Turn off and turn on the controller. 543 ** 544 ** Returns: None. 545 ** 546 *******************************************************************************/ 547 void NfcAdaptation::HalPowerCycle () 548 { 549 const char* func = "NfcAdaptation::HalPowerCycle"; 550 ALOGD ("%s", func); 551 if (mHalDeviceContext) 552 { 553 mHalDeviceContext->power_cycle (mHalDeviceContext); 554 } 555 } 556 557 /******************************************************************************* 558 ** 559 ** Function: NfcAdaptation::HalGetMaxNfcee 560 ** 561 ** Description: Turn off and turn on the controller. 562 ** 563 ** Returns: None. 564 ** 565 *******************************************************************************/ 566 UINT8 NfcAdaptation::HalGetMaxNfcee() 567 { 568 const char* func = "NfcAdaptation::HalPowerCycle"; 569 UINT8 maxNfcee = 0; 570 ALOGD ("%s", func); 571 if (mHalDeviceContext) 572 { 573 // TODO maco call into HAL when we figure out binary compatibility. 574 return nfa_ee_max_ee_cfg; 575 576 //mHalDeviceContext->get_max_ee (mHalDeviceContext, &maxNfcee); 577 } 578 579 return maxNfcee; 580 } 581 582 583 /******************************************************************************* 584 ** 585 ** Function: NfcAdaptation::DownloadFirmware 586 ** 587 ** Description: Download firmware patch files. 588 ** 589 ** Returns: None. 590 ** 591 *******************************************************************************/ 592 void NfcAdaptation::DownloadFirmware () 593 { 594 const char* func = "NfcAdaptation::DownloadFirmware"; 595 ALOGD ("%s: enter", func); 596 HalInitialize (); 597 598 mHalOpenCompletedEvent.lock (); 599 ALOGD ("%s: try open HAL", func); 600 HalOpen (HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback); 601 mHalOpenCompletedEvent.wait (); 602 603 mHalCloseCompletedEvent.lock (); 604 ALOGD ("%s: try close HAL", func); 605 HalClose (); 606 mHalCloseCompletedEvent.wait (); 607 608 HalTerminate (); 609 ALOGD ("%s: exit", func); 610 } 611 612 /******************************************************************************* 613 ** 614 ** Function: NfcAdaptation::HalDownloadFirmwareCallback 615 ** 616 ** Description: Receive events from the HAL. 617 ** 618 ** Returns: None. 619 ** 620 *******************************************************************************/ 621 void NfcAdaptation::HalDownloadFirmwareCallback (nfc_event_t event, nfc_status_t event_status) 622 { 623 const char* func = "NfcAdaptation::HalDownloadFirmwareCallback"; 624 ALOGD ("%s: event=0x%X", func, event); 625 switch (event) 626 { 627 case HAL_NFC_OPEN_CPLT_EVT: 628 { 629 ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT", func); 630 mHalOpenCompletedEvent.signal (); 631 break; 632 } 633 case HAL_NFC_CLOSE_CPLT_EVT: 634 { 635 ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", func); 636 mHalCloseCompletedEvent.signal (); 637 break; 638 } 639 } 640 } 641 642 /******************************************************************************* 643 ** 644 ** Function: NfcAdaptation::HalDownloadFirmwareDataCallback 645 ** 646 ** Description: Receive data events from the HAL. 647 ** 648 ** Returns: None. 649 ** 650 *******************************************************************************/ 651 void NfcAdaptation::HalDownloadFirmwareDataCallback (uint16_t data_len, uint8_t* p_data) 652 { 653 } 654 655 656 /******************************************************************************* 657 ** 658 ** Function: ThreadMutex::ThreadMutex() 659 ** 660 ** Description: class constructor 661 ** 662 ** Returns: none 663 ** 664 *******************************************************************************/ 665 ThreadMutex::ThreadMutex() 666 { 667 pthread_mutexattr_t mutexAttr; 668 669 pthread_mutexattr_init(&mutexAttr); 670 pthread_mutex_init(&mMutex, &mutexAttr); 671 pthread_mutexattr_destroy(&mutexAttr); 672 } 673 674 /******************************************************************************* 675 ** 676 ** Function: ThreadMutex::~ThreadMutex() 677 ** 678 ** Description: class destructor 679 ** 680 ** Returns: none 681 ** 682 *******************************************************************************/ 683 ThreadMutex::~ThreadMutex() 684 { 685 pthread_mutex_destroy(&mMutex); 686 } 687 688 /******************************************************************************* 689 ** 690 ** Function: ThreadMutex::lock() 691 ** 692 ** Description: lock kthe mutex 693 ** 694 ** Returns: none 695 ** 696 *******************************************************************************/ 697 void ThreadMutex::lock() 698 { 699 pthread_mutex_lock(&mMutex); 700 } 701 702 /******************************************************************************* 703 ** 704 ** Function: ThreadMutex::unblock() 705 ** 706 ** Description: unlock the mutex 707 ** 708 ** Returns: none 709 ** 710 *******************************************************************************/ 711 void ThreadMutex::unlock() 712 { 713 pthread_mutex_unlock(&mMutex); 714 } 715 716 /******************************************************************************* 717 ** 718 ** Function: ThreadCondVar::ThreadCondVar() 719 ** 720 ** Description: class constructor 721 ** 722 ** Returns: none 723 ** 724 *******************************************************************************/ 725 ThreadCondVar::ThreadCondVar() 726 { 727 pthread_condattr_t CondAttr; 728 729 pthread_condattr_init(&CondAttr); 730 pthread_cond_init(&mCondVar, &CondAttr); 731 732 pthread_condattr_destroy(&CondAttr); 733 } 734 735 /******************************************************************************* 736 ** 737 ** Function: ThreadCondVar::~ThreadCondVar() 738 ** 739 ** Description: class destructor 740 ** 741 ** Returns: none 742 ** 743 *******************************************************************************/ 744 ThreadCondVar::~ThreadCondVar() 745 { 746 pthread_cond_destroy(&mCondVar); 747 } 748 749 /******************************************************************************* 750 ** 751 ** Function: ThreadCondVar::wait() 752 ** 753 ** Description: wait on the mCondVar 754 ** 755 ** Returns: none 756 ** 757 *******************************************************************************/ 758 void ThreadCondVar::wait() 759 { 760 pthread_cond_wait(&mCondVar, *this); 761 pthread_mutex_unlock(*this); 762 } 763 764 /******************************************************************************* 765 ** 766 ** Function: ThreadCondVar::signal() 767 ** 768 ** Description: signal the mCondVar 769 ** 770 ** Returns: none 771 ** 772 *******************************************************************************/ 773 void ThreadCondVar::signal() 774 { 775 AutoThreadMutex a(*this); 776 pthread_cond_signal(&mCondVar); 777 } 778 779 /******************************************************************************* 780 ** 781 ** Function: AutoThreadMutex::AutoThreadMutex() 782 ** 783 ** Description: class constructor, automatically lock the mutex 784 ** 785 ** Returns: none 786 ** 787 *******************************************************************************/ 788 AutoThreadMutex::AutoThreadMutex(ThreadMutex &m) 789 : mm(m) 790 { 791 mm.lock(); 792 } 793 794 /******************************************************************************* 795 ** 796 ** Function: AutoThreadMutex::~AutoThreadMutex() 797 ** 798 ** Description: class destructor, automatically unlock the mutex 799 ** 800 ** Returns: none 801 ** 802 *******************************************************************************/ 803 AutoThreadMutex::~AutoThreadMutex() 804 { 805 mm.unlock(); 806 } 807