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 <assert.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 "dyn_mem.h"
     30 #include "l2c_int.h"
     31 #include "osi/include/alarm.h"
     32 #include "osi/include/fixed_queue.h"
     33 #include "osi/include/hash_functions.h"
     34 #include "osi/include/hash_map.h"
     35 #include "osi/include/log.h"
     36 #include "osi/include/thread.h"
     37 #include "sdpint.h"
     38 
     39 #if (BLE_INCLUDED == TRUE)
     40 #include "gatt_api.h"
     41 #include "gatt_int.h"
     42 #if SMP_INCLUDED == TRUE
     43 #include "smp_int.h"
     44 #endif
     45 #endif
     46 
     47 // Increase BTU task thread priority to avoid pre-emption
     48 // of audio realated tasks.
     49 #define BTU_TASK_THREAD_PRIORITY -19
     50 
     51 extern fixed_queue_t *btif_msg_queue;
     52 
     53 // Communication queue from bta thread to bt_workqueue.
     54 fixed_queue_t *btu_bta_msg_queue;
     55 
     56 // Communication queue from hci thread to bt_workqueue.
     57 extern fixed_queue_t *btu_hci_msg_queue;
     58 
     59 // General timer queue.
     60 fixed_queue_t *btu_general_alarm_queue;
     61 
     62 thread_t *bt_workqueue_thread;
     63 static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
     64 
     65 extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
     66 /*****************************************************************************
     67 **                          V A R I A B L E S                                *
     68 ******************************************************************************/
     69 // TODO(cmanton) Move this out of this file
     70 const BD_ADDR   BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
     71 
     72 void btu_task_start_up(void *context);
     73 void btu_task_shut_down(void *context);
     74 
     75 /*****************************************************************************
     76 **
     77 ** Function         btu_init_core
     78 **
     79 ** Description      Initialize control block memory for each core component.
     80 **
     81 **
     82 ** Returns          void
     83 **
     84 ******************************************************************************/
     85 void btu_init_core(void)
     86 {
     87     /* Initialize the mandatory core stack components */
     88     btm_init();
     89 
     90     l2c_init();
     91 
     92     sdp_init();
     93 
     94 #if BLE_INCLUDED == TRUE
     95     gatt_init();
     96 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
     97     SMP_Init();
     98 #endif
     99     btm_ble_init();
    100 #endif
    101 }
    102 
    103 /*****************************************************************************
    104 **
    105 ** Function         btu_free_core
    106 **
    107 ** Description      Releases control block memory for each core component.
    108 **
    109 **
    110 ** Returns          void
    111 **
    112 ******************************************************************************/
    113 void btu_free_core(void)
    114 {
    115       /* Free the mandatory core stack components */
    116       l2c_free();
    117 
    118 #if BLE_INCLUDED == TRUE
    119       gatt_free();
    120 #endif
    121 }
    122 
    123 /*****************************************************************************
    124 **
    125 ** Function         BTU_StartUp
    126 **
    127 ** Description      Initializes the BTU control block.
    128 **
    129 **                  NOTE: Must be called before creating any tasks
    130 **                      (RPC, BTU, HCIT, APPL, etc.)
    131 **
    132 ** Returns          void
    133 **
    134 ******************************************************************************/
    135 void BTU_StartUp(void)
    136 {
    137     btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
    138 
    139     btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
    140     if (btu_bta_msg_queue == NULL)
    141         goto error_exit;
    142 
    143     btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
    144     if (btu_general_alarm_queue == NULL)
    145         goto error_exit;
    146 
    147     bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
    148     if (bt_workqueue_thread == NULL)
    149         goto error_exit;
    150 
    151     thread_set_priority(bt_workqueue_thread, BTU_TASK_THREAD_PRIORITY);
    152 
    153     // Continue startup on bt workqueue thread.
    154     thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
    155     return;
    156 
    157   error_exit:;
    158     LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", __func__);
    159     BTU_ShutDown();
    160 }
    161 
    162 void BTU_ShutDown(void) {
    163   btu_task_shut_down(NULL);
    164 
    165   fixed_queue_free(btu_bta_msg_queue, NULL);
    166   btu_bta_msg_queue = NULL;
    167 
    168   fixed_queue_free(btu_general_alarm_queue, NULL);
    169   btu_general_alarm_queue = NULL;
    170 
    171   thread_free(bt_workqueue_thread);
    172 
    173   bt_workqueue_thread = NULL;
    174 }
    175