Home | History | Annotate | Download | only in btu
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2000-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 
     19 #define LOG_TAG "bt_task"
     20 
     21 #include <base/logging.h>
     22 #include <pthread.h>
     23 #include <string.h>
     24 
     25 #include "bt_target.h"
     26 #include "btm_int.h"
     27 #include "btu.h"
     28 #include "device/include/controller.h"
     29 #include "gatt_api.h"
     30 #include "gatt_int.h"
     31 #include "l2c_int.h"
     32 #include "osi/include/alarm.h"
     33 #include "osi/include/fixed_queue.h"
     34 #include "osi/include/log.h"
     35 #include "osi/include/thread.h"
     36 #include "sdpint.h"
     37 #include "smp_int.h"
     38 
     39 // RT priority for audio-related tasks
     40 #define BTU_TASK_RT_PRIORITY 1
     41 
     42 // Communication queue from hci thread to bt_workqueue.
     43 extern fixed_queue_t* btu_hci_msg_queue;
     44 
     45 thread_t* bt_workqueue_thread;
     46 static const char* BT_WORKQUEUE_NAME = "bt_workqueue";
     47 
     48 extern void PLATFORM_DisableHciTransport(uint8_t bDisable);
     49 
     50 void btu_task_start_up(void* context);
     51 void btu_task_shut_down(void* context);
     52 
     53 /*****************************************************************************
     54  *
     55  * Function         btu_init_core
     56  *
     57  * Description      Initialize control block memory for each core component.
     58  *
     59  *
     60  * Returns          void
     61  *
     62  *****************************************************************************/
     63 void btu_init_core(void) {
     64   /* Initialize the mandatory core stack components */
     65   btm_init();
     66 
     67   l2c_init();
     68 
     69   sdp_init();
     70 
     71   gatt_init();
     72 
     73   SMP_Init();
     74 
     75   btm_ble_init();
     76 }
     77 
     78 /*****************************************************************************
     79  *
     80  * Function         btu_free_core
     81  *
     82  * Description      Releases control block memory for each core component.
     83  *
     84  *
     85  * Returns          void
     86  *
     87  *****************************************************************************/
     88 void btu_free_core(void) {
     89   /* Free the mandatory core stack components */
     90   l2c_free();
     91 
     92   gatt_free();
     93 }
     94 
     95 /*****************************************************************************
     96  *
     97  * Function         BTU_StartUp
     98  *
     99  * Description      Initializes the BTU control block.
    100  *
    101  *                  NOTE: Must be called before creating any tasks
    102  *                      (RPC, BTU, HCIT, APPL, etc.)
    103  *
    104  * Returns          void
    105  *
    106  *****************************************************************************/
    107 void BTU_StartUp(void) {
    108   btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
    109 
    110   bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
    111   if (bt_workqueue_thread == NULL) goto error_exit;
    112 
    113   thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
    114 
    115   // Continue startup on bt workqueue thread.
    116   thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
    117   return;
    118 
    119 error_exit:;
    120   LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue",
    121             __func__);
    122   BTU_ShutDown();
    123 }
    124 
    125 void BTU_ShutDown(void) {
    126   btu_task_shut_down(NULL);
    127 
    128 
    129   thread_free(bt_workqueue_thread);
    130 
    131   bt_workqueue_thread = NULL;
    132 }
    133