• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include "bt_vendor.h"
18 
19 #define UNUSED_PARAM __attribute__((unused))
20 #define HCI_CMD_PREAMBLE_SIZE 3
21 #define HCI_RESET 0x0C03
22 #define HCI_EVT_CMD_CMPL_OPCODE 3
23 #define HCI_EVT_CMD_CMPL_STATUS_RET_BYTE 5
24 #define MSG_STACK_TO_HC_HCI_CMD 0x2000
25 #define BT_HC_HDR_SIZE (sizeof(HC_BT_HDR))
26 #define STREAM_TO_UINT16(u16, p)                                \
27   {                                                             \
28     u16 = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); \
29     (p) += 2;                                                   \
30   }
31 #define UINT16_TO_STREAM(p, u16)    \
32   {                                 \
33     *(p)++ = (uint8_t)(u16);        \
34     *(p)++ = (uint8_t)((u16) >> 8); \
35   }
36 bt_vendor_callbacks_t* bt_vendor_cbacks = nullptr;
37 
hw_epilog_cback(void * p_mem)38 void hw_epilog_cback(void* p_mem) {
39   HC_BT_HDR* p_evt_buf = (HC_BT_HDR*)p_mem;
40   uint8_t *p, status;
41   uint16_t opcode;
42 
43   status = *((uint8_t*)(p_evt_buf + 1) + HCI_EVT_CMD_CMPL_STATUS_RET_BYTE);
44   p = (uint8_t*)(p_evt_buf + 1) + HCI_EVT_CMD_CMPL_OPCODE;
45   STREAM_TO_UINT16(opcode, p);
46 
47   if (!bt_vendor_cbacks) {
48     return;
49   }
50   /* Must free the RX event buffer */
51   bt_vendor_cbacks->dealloc(p_evt_buf);
52 
53   /* Once epilog process is done, must call callback to notify caller */
54   bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
55   return;
56 }
57 
testInit(const bt_vendor_callbacks_t * cb,unsigned char * bdaddr UNUSED_PARAM)58 static int testInit(const bt_vendor_callbacks_t* cb,
59                     unsigned char* bdaddr UNUSED_PARAM) {
60   if (cb == nullptr) {
61     return -1;
62   }
63   /*store reference to user callbacks */
64   bt_vendor_cbacks = (bt_vendor_callbacks_t*)cb;
65   return 0;
66 }
67 
testOperations(bt_vendor_opcode_t opcode,void * param UNUSED_PARAM)68 static int testOperations(bt_vendor_opcode_t opcode, void* param UNUSED_PARAM) {
69   BtVendor* btVendor = BtVendor::getInstance();
70   if (bt_vendor_cbacks) {
71     btVendor->setVendorCback(bt_vendor_cbacks, opcode);
72   }
73   switch (opcode) {
74     case BT_VND_OP_POWER_CTRL: {
75       // No callback for this opcode
76       break;
77     }
78     case BT_VND_OP_USERIAL_OPEN: {
79       int32_t(*fd_array)[] = (int32_t(*)[])param;
80       int32_t fdArray[CH_MAX];
81       *fdArray = *(btVendor->queryFdList());
82       size_t fdcount = btVendor->queryFdCount();
83       for (size_t i = 0; i < fdcount; ++i) {
84         (*fd_array)[i] = fdArray[i];
85       }
86       return fdcount;
87       break;
88     }
89     case BT_VND_OP_FW_CFG: {
90       if (bt_vendor_cbacks) {
91         bt_vendor_cbacks->fwcfg_cb(BT_VND_OP_RESULT_SUCCESS);
92       }
93       break;
94     }
95     case BT_VND_OP_GET_LPM_IDLE_TIMEOUT: {
96       // No callback for this opcode
97       uint32_t* timeout_ms = (uint32_t*)param;
98       *timeout_ms = 0;
99       break;
100     }
101     case BT_VND_OP_LPM_SET_MODE: {
102       if (bt_vendor_cbacks) {
103         bt_vendor_cbacks->lpm_cb(BT_VND_OP_RESULT_SUCCESS);
104       }
105       break;
106     }
107     case BT_VND_OP_USERIAL_CLOSE: {
108       // No callback for this opcode
109       break;
110     }
111     case BT_VND_OP_LPM_WAKE_SET_STATE: {
112       // No callback for this opcode
113       break;
114     }
115     default:
116       break;
117   }
118   return 0;
119 }
120 
testCleanup(void)121 static void testCleanup(void) { bt_vendor_cbacks = nullptr; }
122 
123 const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE = {
124     sizeof(bt_vendor_interface_t), testInit, testOperations, testCleanup};
125 
populateFdList(int32_t list[],size_t count)126 void BtVendor::populateFdList(int32_t list[], size_t count) {
127   fdCount = count;
128   for (size_t i = 0; i < count; ++i) {
129     fdList[i] = list[i];
130   }
131 }
132 
callRemainingCbacks()133 void BtVendor::callRemainingCbacks() {
134   if (mCbacks) {
135     mCbacks->audio_state_cb(BT_VND_OP_RESULT_SUCCESS);
136     mCbacks->scocfg_cb(BT_VND_OP_RESULT_SUCCESS);
137     mCbacks->a2dp_offload_cb(BT_VND_OP_RESULT_SUCCESS, mOpcode, 0);
138     mCbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
139 
140     HC_BT_HDR* p_buf = NULL;
141     uint8_t* p;
142 
143     /* Sending a HCI_RESET */
144     /* Must allocate command buffer via HC's alloc API */
145     p_buf = (HC_BT_HDR*)mCbacks->alloc(BT_HC_HDR_SIZE + HCI_CMD_PREAMBLE_SIZE);
146     if (p_buf) {
147       p_buf->event = MSG_STACK_TO_HC_HCI_CMD;
148       p_buf->offset = 0;
149       p_buf->layer_specific = 0;
150       p_buf->len = HCI_CMD_PREAMBLE_SIZE;
151 
152       p = (uint8_t*)(p_buf + 1);
153       UINT16_TO_STREAM(p, HCI_RESET);
154       *p = 0; /* parameter length */
155 
156       /* Send command via HC's xmit_cb API */
157       mCbacks->xmit_cb(HCI_RESET, p_buf, hw_epilog_cback);
158     } else {
159       mCbacks->epilog_cb(BT_VND_OP_RESULT_FAIL);
160     }
161   }
162 }
163