Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2014 Google, Inc. |
| 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_vendor" |
| 20 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame^] | 21 | #include "vendor.h" |
| 22 | |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | #include <dlfcn.h> |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 25 | |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 26 | #include "buffer_allocator.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 27 | #include "osi/include/log.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame^] | 28 | #include "osi/include/osi.h" |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 29 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 30 | #define LAST_VENDOR_OPCODE_VALUE VENDOR_DO_EPILOG |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 31 | |
| 32 | static const char *VENDOR_LIBRARY_NAME = "libbt-vendor.so"; |
| 33 | static const char *VENDOR_LIBRARY_SYMBOL_NAME = "BLUETOOTH_VENDOR_LIB_INTERFACE"; |
| 34 | |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 35 | static const vendor_t interface; |
| 36 | static const allocator_t *buffer_allocator; |
| 37 | static const hci_t *hci; |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 38 | static vendor_cb callbacks[LAST_VENDOR_OPCODE_VALUE + 1]; |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 39 | |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 40 | static void *lib_handle; |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 41 | static bt_vendor_interface_t *lib_interface; |
| 42 | static const bt_vendor_callbacks_t lib_callbacks; |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 43 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 44 | // Interface functions |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 45 | |
Zach Johnson | 218f375 | 2014-09-03 14:36:44 -0700 | [diff] [blame] | 46 | static bool vendor_open( |
| 47 | const uint8_t *local_bdaddr, |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 48 | const hci_t *hci_interface) { |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 49 | assert(lib_handle == NULL); |
Zach Johnson | 218f375 | 2014-09-03 14:36:44 -0700 | [diff] [blame] | 50 | hci = hci_interface; |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 51 | |
| 52 | lib_handle = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW); |
| 53 | if (!lib_handle) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 54 | LOG_ERROR(LOG_TAG, "%s unable to open %s: %s", __func__, VENDOR_LIBRARY_NAME, dlerror()); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 55 | goto error; |
| 56 | } |
| 57 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 58 | lib_interface = (bt_vendor_interface_t *)dlsym(lib_handle, VENDOR_LIBRARY_SYMBOL_NAME); |
| 59 | if (!lib_interface) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 60 | LOG_ERROR(LOG_TAG, "%s unable to find symbol %s in %s: %s", __func__, VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror()); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 61 | goto error; |
| 62 | } |
| 63 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 64 | LOG_INFO(LOG_TAG, "alloc value %p", lib_callbacks.alloc); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 65 | |
| 66 | int status = lib_interface->init(&lib_callbacks, (unsigned char *)local_bdaddr); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 67 | if (status) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 68 | LOG_ERROR(LOG_TAG, "%s unable to initialize vendor library: %d", __func__, status); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 69 | goto error; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | |
| 74 | error:; |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 75 | lib_interface = NULL; |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 76 | if (lib_handle) |
| 77 | dlclose(lib_handle); |
| 78 | lib_handle = NULL; |
| 79 | return false; |
| 80 | } |
| 81 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 82 | static void vendor_close(void) { |
| 83 | if (lib_interface) |
| 84 | lib_interface->cleanup(); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 85 | |
| 86 | if (lib_handle) |
| 87 | dlclose(lib_handle); |
| 88 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 89 | lib_interface = NULL; |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 90 | lib_handle = NULL; |
| 91 | } |
| 92 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 93 | static int send_command(vendor_opcode_t opcode, void *param) { |
| 94 | assert(lib_interface != NULL); |
| 95 | return lib_interface->op(opcode, param); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 98 | static int send_async_command(vendor_async_opcode_t opcode, void *param) { |
| 99 | assert(lib_interface != NULL); |
| 100 | return lib_interface->op(opcode, param); |
| 101 | } |
| 102 | |
| 103 | static void set_callback(vendor_async_opcode_t opcode, vendor_cb callback) { |
| 104 | callbacks[opcode] = callback; |
| 105 | } |
| 106 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 107 | // Internal functions |
| 108 | |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 109 | // Called back from vendor library when the firmware configuration |
| 110 | // completes. |
| 111 | static void firmware_config_cb(bt_vendor_op_result_t result) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 112 | LOG_INFO(LOG_TAG, "firmware callback"); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 113 | vendor_cb callback = callbacks[VENDOR_CONFIGURE_FIRMWARE]; |
| 114 | assert(callback != NULL); |
| 115 | callback(result == BT_VND_OP_RESULT_SUCCESS); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Called back from vendor library to indicate status of previous |
| 119 | // SCO configuration request. This should only happen during the |
| 120 | // postload process. |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 121 | static void sco_config_cb(bt_vendor_op_result_t result) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 122 | LOG_INFO(LOG_TAG, "%s", __func__); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 123 | vendor_cb callback = callbacks[VENDOR_CONFIGURE_SCO]; |
| 124 | assert(callback != NULL); |
| 125 | callback(result == BT_VND_OP_RESULT_SUCCESS); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Called back from vendor library to indicate status of previous |
| 129 | // LPM enable/disable request. |
| 130 | static void low_power_mode_cb(bt_vendor_op_result_t result) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 131 | LOG_INFO(LOG_TAG, "%s", __func__); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 132 | vendor_cb callback = callbacks[VENDOR_SET_LPM_MODE]; |
| 133 | assert(callback != NULL); |
| 134 | callback(result == BT_VND_OP_RESULT_SUCCESS); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 137 | /****************************************************************************** |
| 138 | ** |
| 139 | ** Function sco_audiostate_cb |
| 140 | ** |
| 141 | ** Description HOST/CONTROLLER VENDOR LIB CALLBACK API - This function is |
| 142 | ** called when the libbt-vendor completed vendor specific codec |
| 143 | ** setup request |
| 144 | ** |
| 145 | ** Returns None |
| 146 | ** |
| 147 | ******************************************************************************/ |
| 148 | static void sco_audiostate_cb(bt_vendor_op_result_t result) |
| 149 | { |
| 150 | uint8_t status = (result == BT_VND_OP_RESULT_SUCCESS) ? 0 : 1; |
| 151 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 152 | LOG_INFO(LOG_TAG, "sco_audiostate_cb(status: %d)",status); |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 155 | // Called by vendor library when it needs an HCI buffer. |
Chris Manton | fb5ab99 | 2014-07-31 09:22:09 -0700 | [diff] [blame] | 156 | static void *buffer_alloc_cb(int size) { |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 157 | return buffer_allocator->alloc(size); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Called by vendor library when it needs to free a buffer allocated with |
Chris Manton | fb5ab99 | 2014-07-31 09:22:09 -0700 | [diff] [blame] | 161 | // |buffer_alloc_cb|. |
| 162 | static void buffer_free_cb(void *buffer) { |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 163 | buffer_allocator->free(buffer); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Zach Johnson | 218f375 | 2014-09-03 14:36:44 -0700 | [diff] [blame] | 166 | static void transmit_completed_callback(BT_HDR *response, void *context) { |
Zach Johnson | cab7816 | 2014-10-16 15:52:45 -0700 | [diff] [blame] | 167 | // Call back to the vendor library if it provided a callback to call. |
| 168 | if (context) |
| 169 | ((tINT_CMD_CBACK)context)(response); |
Zach Johnson | 218f375 | 2014-09-03 14:36:44 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 172 | // Called back from vendor library when it wants to send an HCI command. |
Zach Johnson | 218f375 | 2014-09-03 14:36:44 -0700 | [diff] [blame] | 173 | static uint8_t transmit_cb(UNUSED_ATTR uint16_t opcode, void *buffer, tINT_CMD_CBACK callback) { |
| 174 | assert(hci != NULL); |
| 175 | hci->transmit_command((BT_HDR *)buffer, transmit_completed_callback, NULL, callback); |
| 176 | return true; |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Called back from vendor library when the epilog procedure has |
| 180 | // completed. It is safe to call vendor_interface->cleanup() after |
| 181 | // this callback has been received. |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 182 | static void epilog_cb(bt_vendor_op_result_t result) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 183 | LOG_INFO(LOG_TAG, "%s", __func__); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 184 | vendor_cb callback = callbacks[VENDOR_DO_EPILOG]; |
| 185 | assert(callback != NULL); |
| 186 | callback(result == BT_VND_OP_RESULT_SUCCESS); |
| 187 | } |
| 188 | |
| 189 | static const bt_vendor_callbacks_t lib_callbacks = { |
| 190 | sizeof(lib_callbacks), |
| 191 | firmware_config_cb, |
| 192 | sco_config_cb, |
| 193 | low_power_mode_cb, |
| 194 | sco_audiostate_cb, |
| 195 | buffer_alloc_cb, |
| 196 | buffer_free_cb, |
| 197 | transmit_cb, |
| 198 | epilog_cb |
| 199 | }; |
| 200 | |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 201 | static const vendor_t interface = { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 202 | vendor_open, |
| 203 | vendor_close, |
| 204 | send_command, |
| 205 | send_async_command, |
| 206 | set_callback, |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 209 | const vendor_t *vendor_get_interface() { |
| 210 | buffer_allocator = buffer_allocator_get_interface(); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 211 | return &interface; |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 212 | } |