Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2009-2013 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 | ******************************************************************************/ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 18 | #include "bt_target.h" |
| 19 | |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 20 | #include <string.h> |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 21 | #include "bt_utils.h" |
| 22 | #include "btcore/include/uuid.h" |
| 23 | #include "btm_int.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 24 | #include "gap_api.h" |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 25 | #include "gap_int.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 26 | #include "gatt_api.h" |
| 27 | #include "gatt_int.h" |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 28 | #include "gattdefs.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 29 | #include "hcimsgs.h" |
Myles Watson | d7ffd64 | 2016-10-27 10:27:36 -0700 | [diff] [blame] | 30 | #include "osi/include/osi.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 31 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 32 | #define GAP_CHAR_ICON_SIZE 2 |
| 33 | #define GAP_CHAR_DEV_NAME_SIZE 248 |
| 34 | #define GAP_MAX_NUM_INC_SVR 0 |
| 35 | #define GAP_MAX_ATTR_NUM (2 * GAP_MAX_CHAR_NUM + GAP_MAX_NUM_INC_SVR + 1) |
| 36 | #define GAP_MAX_CHAR_VALUE_SIZE (30 + GAP_CHAR_DEV_NAME_SIZE) |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 37 | |
| 38 | #ifndef GAP_ATTR_DB_SIZE |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 39 | #define GAP_ATTR_DB_SIZE \ |
| 40 | GATT_DB_MEM_SIZE(GAP_MAX_NUM_INC_SVR, GAP_MAX_CHAR_NUM, \ |
| 41 | GAP_MAX_CHAR_VALUE_SIZE) |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 42 | #endif |
| 43 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 44 | static void gap_ble_s_attr_request_cback(uint16_t conn_id, uint32_t trans_id, |
| 45 | tGATTS_REQ_TYPE op_code, |
| 46 | tGATTS_DATA* p_data); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 47 | |
| 48 | /* client connection callback */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 49 | static void gap_ble_c_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, |
| 50 | uint16_t conn_id, bool connected, |
| 51 | tGATT_DISCONN_REASON reason, |
| 52 | tGATT_TRANSPORT transport); |
| 53 | static void gap_ble_c_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op, |
| 54 | tGATT_STATUS status, |
| 55 | tGATT_CL_COMPLETE* p_data); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 56 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 57 | static tGATT_CBACK gap_cback = {gap_ble_c_connect_cback, |
| 58 | gap_ble_c_cmpl_cback, |
| 59 | NULL, |
| 60 | NULL, |
| 61 | gap_ble_s_attr_request_cback, |
| 62 | NULL, |
Jakub Pawlowski | eafd45d | 2017-03-22 19:00:47 -0700 | [diff] [blame] | 63 | NULL, |
Jakub Pawlowski | b5ba4fd | 2017-03-23 18:11:04 -0700 | [diff] [blame^] | 64 | NULL, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 65 | NULL}; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 66 | |
| 67 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 68 | * |
| 69 | * Function gap_find_clcb_by_bd_addr |
| 70 | * |
| 71 | * Description The function searches all LCB with macthing bd address |
| 72 | * |
| 73 | * Returns total number of clcb found. |
| 74 | * |
| 75 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 76 | tGAP_CLCB* gap_find_clcb_by_bd_addr(BD_ADDR bda) { |
| 77 | uint8_t i_clcb; |
| 78 | tGAP_CLCB* p_clcb = NULL; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 79 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 80 | for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL; |
| 81 | i_clcb++, p_clcb++) { |
| 82 | if (p_clcb->in_use && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN)) { |
| 83 | return p_clcb; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 84 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 85 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 86 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 87 | return NULL; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 91 | * |
| 92 | * Function gap_ble_find_clcb_by_conn_id |
| 93 | * |
| 94 | * Description The function searches all LCB with macthing connection ID |
| 95 | * |
| 96 | * Returns total number of clcb found. |
| 97 | * |
| 98 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 99 | tGAP_CLCB* gap_ble_find_clcb_by_conn_id(uint16_t conn_id) { |
| 100 | uint8_t i_clcb; |
| 101 | tGAP_CLCB* p_clcb = NULL; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 102 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 103 | for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL; |
| 104 | i_clcb++, p_clcb++) { |
| 105 | if (p_clcb->in_use && p_clcb->connected && p_clcb->conn_id == conn_id) { |
| 106 | return p_clcb; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 107 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 108 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 109 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 110 | return p_clcb; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 114 | * |
| 115 | * Function gap_clcb_alloc |
| 116 | * |
| 117 | * Description The function allocates a GAP connection link control block |
| 118 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 119 | * Returns NULL if not found. Otherwise pointer to the connection link |
| 120 | * block. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 121 | * |
| 122 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 123 | tGAP_CLCB* gap_clcb_alloc(BD_ADDR bda) { |
| 124 | uint8_t i_clcb = 0; |
| 125 | tGAP_CLCB* p_clcb = NULL; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 126 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 127 | for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL; |
| 128 | i_clcb++, p_clcb++) { |
| 129 | if (!p_clcb->in_use) { |
| 130 | fixed_queue_free(p_clcb->pending_req_q, NULL); |
| 131 | memset(p_clcb, 0, sizeof(tGAP_CLCB)); |
| 132 | p_clcb->in_use = true; |
| 133 | memcpy(p_clcb->bda, bda, BD_ADDR_LEN); |
| 134 | p_clcb->pending_req_q = fixed_queue_new(SIZE_MAX); |
| 135 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 136 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 137 | } |
| 138 | return p_clcb; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 142 | * |
| 143 | * Function gap_ble_dealloc_clcb |
| 144 | * |
| 145 | * Description The function clean up the pending request queue in GAP |
| 146 | * |
| 147 | * Returns none |
| 148 | * |
| 149 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 150 | void gap_ble_dealloc_clcb(tGAP_CLCB* p_clcb) { |
| 151 | tGAP_BLE_REQ* p_q; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 152 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 153 | while ((p_q = (tGAP_BLE_REQ*)fixed_queue_try_dequeue( |
| 154 | p_clcb->pending_req_q)) != NULL) { |
| 155 | /* send callback to all pending requests if being removed*/ |
| 156 | if (p_q->p_cback != NULL) (*p_q->p_cback)(false, p_clcb->bda, 0, NULL); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 157 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 158 | osi_free(p_q); |
| 159 | } |
| 160 | fixed_queue_free(p_clcb->pending_req_q, NULL); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 161 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 162 | memset(p_clcb, 0, sizeof(tGAP_CLCB)); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 166 | * |
| 167 | * Function gap_ble_enqueue_request |
| 168 | * |
| 169 | * Description The function enqueue a GAP client request |
| 170 | * |
| 171 | * Returns true is successul; false otherwise |
| 172 | * |
| 173 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 174 | bool gap_ble_enqueue_request(tGAP_CLCB* p_clcb, uint16_t uuid, |
| 175 | tGAP_BLE_CMPL_CBACK* p_cback) { |
| 176 | tGAP_BLE_REQ* p_q = (tGAP_BLE_REQ*)osi_malloc(sizeof(tGAP_BLE_REQ)); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 177 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 178 | p_q->p_cback = p_cback; |
| 179 | p_q->uuid = uuid; |
| 180 | fixed_queue_enqueue(p_clcb->pending_req_q, p_q); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 181 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 182 | return true; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 183 | } |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 184 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 185 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 186 | * |
| 187 | * Function gap_ble_dequeue_request |
| 188 | * |
| 189 | * Description The function dequeue a GAP client request if any |
| 190 | * |
| 191 | * Returns true is successul; false otherwise |
| 192 | * |
| 193 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 194 | bool gap_ble_dequeue_request(tGAP_CLCB* p_clcb, uint16_t* p_uuid, |
| 195 | tGAP_BLE_CMPL_CBACK** p_cback) { |
| 196 | tGAP_BLE_REQ* p_q = |
| 197 | (tGAP_BLE_REQ*)fixed_queue_try_dequeue(p_clcb->pending_req_q); |
| 198 | ; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 199 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 200 | if (p_q != NULL) { |
| 201 | *p_cback = p_q->p_cback; |
| 202 | *p_uuid = p_q->uuid; |
| 203 | osi_free(p_q); |
| 204 | return true; |
| 205 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 206 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 207 | return false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 208 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 209 | |
| 210 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 211 | * GAP Attributes Database Request callback |
| 212 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 213 | tGATT_STATUS gap_read_attr_value(uint16_t handle, tGATT_VALUE* p_value, |
| 214 | bool is_long) { |
| 215 | tGAP_ATTR* p_db_attr = gap_cb.gatt_attr; |
| 216 | uint8_t *p = p_value->value, i; |
| 217 | uint16_t offset = p_value->offset; |
| 218 | uint8_t* p_dev_name = NULL; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 219 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 220 | for (i = 0; i < GAP_MAX_CHAR_NUM; i++, p_db_attr++) { |
| 221 | if (handle == p_db_attr->handle) { |
| 222 | if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME && is_long == true) |
| 223 | return GATT_NOT_LONG; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 224 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 225 | switch (p_db_attr->uuid) { |
| 226 | case GATT_UUID_GAP_DEVICE_NAME: |
| 227 | BTM_ReadLocalDeviceName((char**)&p_dev_name); |
| 228 | if (strlen((char*)p_dev_name) > GATT_MAX_ATTR_LEN) |
| 229 | p_value->len = GATT_MAX_ATTR_LEN; |
| 230 | else |
| 231 | p_value->len = (uint16_t)strlen((char*)p_dev_name); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 232 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 233 | if (offset > p_value->len) |
| 234 | return GATT_INVALID_OFFSET; |
| 235 | else { |
| 236 | p_value->len -= offset; |
| 237 | p_dev_name += offset; |
| 238 | ARRAY_TO_STREAM(p, p_dev_name, p_value->len); |
| 239 | GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME len=0x%04x", |
| 240 | p_value->len); |
| 241 | } |
| 242 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 243 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 244 | case GATT_UUID_GAP_ICON: |
| 245 | UINT16_TO_STREAM(p, p_db_attr->attr_value.icon); |
| 246 | p_value->len = 2; |
| 247 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 248 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 249 | case GATT_UUID_GAP_PREF_CONN_PARAM: |
| 250 | UINT16_TO_STREAM( |
| 251 | p, p_db_attr->attr_value.conn_param.int_min); /* int_min */ |
| 252 | UINT16_TO_STREAM( |
| 253 | p, p_db_attr->attr_value.conn_param.int_max); /* int_max */ |
| 254 | UINT16_TO_STREAM( |
| 255 | p, p_db_attr->attr_value.conn_param.latency); /* latency */ |
| 256 | UINT16_TO_STREAM( |
| 257 | p, p_db_attr->attr_value.conn_param.sp_tout); /* sp_tout */ |
| 258 | p_value->len = 8; |
| 259 | break; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 260 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 261 | /* address resolution */ |
| 262 | case GATT_UUID_GAP_CENTRAL_ADDR_RESOL: |
| 263 | UINT8_TO_STREAM(p, p_db_attr->attr_value.addr_resolution); |
| 264 | p_value->len = 1; |
| 265 | break; |
| 266 | } |
| 267 | return GATT_SUCCESS; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 268 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 269 | } |
| 270 | return GATT_NOT_FOUND; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 274 | * GAP Attributes Database Read/Read Blob Request process |
| 275 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 276 | tGATT_STATUS gap_proc_read(UNUSED_ATTR tGATTS_REQ_TYPE type, |
| 277 | tGATT_READ_REQ* p_data, tGATTS_RSP* p_rsp) { |
| 278 | tGATT_STATUS status = GATT_NO_RESOURCES; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 279 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 280 | if (p_data->is_long) p_rsp->attr_value.offset = p_data->offset; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 281 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 282 | p_rsp->attr_value.handle = p_data->handle; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 283 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 284 | status = |
| 285 | gap_read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 286 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 287 | return status; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 288 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 289 | |
| 290 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 291 | * |
| 292 | * Function gap_proc_write_req |
| 293 | * |
| 294 | * Description GAP ATT server process a write request. |
| 295 | * |
| 296 | * Returns void. |
| 297 | * |
| 298 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 299 | uint8_t gap_proc_write_req(UNUSED_ATTR tGATTS_REQ_TYPE type, |
| 300 | tGATT_WRITE_REQ* p_data) { |
| 301 | tGAP_ATTR* p_db_attr = gap_cb.gatt_attr; |
| 302 | uint8_t i; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 303 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 304 | for (i = 0; i < GAP_MAX_CHAR_NUM; i++, p_db_attr++) { |
| 305 | if (p_data->handle == p_db_attr->handle) { |
| 306 | return GATT_WRITE_NOT_PERMIT; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 307 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 308 | } |
| 309 | return GATT_NOT_FOUND; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 313 | * |
| 314 | * Function gap_ble_s_attr_request_cback |
| 315 | * |
| 316 | * Description GAP ATT server attribute access request callback. |
| 317 | * |
| 318 | * Returns void. |
| 319 | * |
| 320 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 321 | void gap_ble_s_attr_request_cback(uint16_t conn_id, uint32_t trans_id, |
| 322 | tGATTS_REQ_TYPE type, tGATTS_DATA* p_data) { |
| 323 | uint8_t status = GATT_INVALID_PDU; |
| 324 | tGATTS_RSP rsp_msg; |
| 325 | bool ignore = false; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 326 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 327 | GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 328 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 329 | memset(&rsp_msg, 0, sizeof(tGATTS_RSP)); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 330 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 331 | switch (type) { |
| 332 | case GATTS_REQ_TYPE_READ_CHARACTERISTIC: |
| 333 | case GATTS_REQ_TYPE_READ_DESCRIPTOR: |
| 334 | status = gap_proc_read(type, &p_data->read_req, &rsp_msg); |
| 335 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 336 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 337 | case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC: |
| 338 | case GATTS_REQ_TYPE_WRITE_DESCRIPTOR: |
| 339 | if (!p_data->write_req.need_rsp) ignore = true; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 340 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 341 | status = gap_proc_write_req(type, &p_data->write_req); |
| 342 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 343 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 344 | case GATTS_REQ_TYPE_WRITE_EXEC: |
| 345 | ignore = true; |
| 346 | GAP_TRACE_EVENT("Ignore GATTS_REQ_TYPE_WRITE_EXEC"); |
| 347 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 348 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 349 | case GATTS_REQ_TYPE_MTU: |
| 350 | GAP_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu); |
| 351 | ignore = true; |
| 352 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 353 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 354 | default: |
| 355 | GAP_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type); |
| 356 | break; |
| 357 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 358 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 359 | if (!ignore) GATTS_SendRsp(conn_id, trans_id, status, &rsp_msg); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 363 | * |
| 364 | * Function btm_ble_att_db_init |
| 365 | * |
| 366 | * Description GAP ATT database initalization. |
| 367 | * |
| 368 | * Returns void. |
| 369 | * |
| 370 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 371 | void gap_attr_db_init(void) { |
| 372 | tBT_UUID app_uuid = {LEN_UUID_128, {0}}; |
| 373 | uint16_t service_handle; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 374 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 375 | /* Fill our internal UUID with a fixed pattern 0x82 */ |
| 376 | memset(&app_uuid.uu.uuid128, 0x82, LEN_UUID_128); |
| 377 | memset(gap_cb.gatt_attr, 0, sizeof(tGAP_ATTR) * GAP_MAX_CHAR_NUM); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 378 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 379 | gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 380 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 381 | GATT_StartIf(gap_cb.gatt_if); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 382 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 383 | bt_uuid_t svc_uuid, name_uuid, icon_uuid, pref_uuid, addr_res_uuid; |
| 384 | uuid_128_from_16(&svc_uuid, UUID_SERVCLASS_GAP_SERVER); |
| 385 | uuid_128_from_16(&name_uuid, GATT_UUID_GAP_DEVICE_NAME); |
| 386 | uuid_128_from_16(&icon_uuid, GATT_UUID_GAP_ICON); |
| 387 | uuid_128_from_16(&pref_uuid, GATT_UUID_GAP_PREF_CONN_PARAM); |
| 388 | uuid_128_from_16(&addr_res_uuid, GATT_UUID_GAP_CENTRAL_ADDR_RESOL); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 389 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 390 | btgatt_db_element_t service[] = { |
| 391 | {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = svc_uuid}, |
| 392 | {.type = BTGATT_DB_CHARACTERISTIC, |
| 393 | .uuid = name_uuid, |
| 394 | .properties = GATT_CHAR_PROP_BIT_READ, |
| 395 | .permissions = GATT_PERM_READ}, |
| 396 | {.type = BTGATT_DB_CHARACTERISTIC, |
| 397 | .uuid = icon_uuid, |
| 398 | .properties = GATT_CHAR_PROP_BIT_READ, |
| 399 | .permissions = GATT_PERM_READ}, |
| 400 | {.type = BTGATT_DB_CHARACTERISTIC, |
| 401 | .uuid = addr_res_uuid, |
| 402 | .properties = GATT_CHAR_PROP_BIT_READ, |
| 403 | .permissions = GATT_PERM_READ} |
| 404 | #if (BTM_PERIPHERAL_ENABLED == TRUE) /* Only needed for peripheral testing */ |
| 405 | , |
| 406 | {.type = BTGATT_DB_CHARACTERISTIC, |
| 407 | .uuid = pref_uuid, |
| 408 | .properties = GATT_CHAR_PROP_BIT_READ, |
| 409 | .permissions = GATT_PERM_READ} |
Ganesh Ganapathi Batta | 7fa4fba | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 410 | #endif |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 411 | }; |
Ganesh Ganapathi Batta | 7fa4fba | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 412 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 413 | /* Add a GAP service */ |
| 414 | GATTS_AddService(gap_cb.gatt_if, service, |
| 415 | sizeof(service) / sizeof(btgatt_db_element_t)); |
| 416 | service_handle = service[0].attribute_handle; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 417 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 418 | GAP_TRACE_EVENT("%s: service_handle = %d", __func__, service_handle); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 419 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 420 | gap_cb.gatt_attr[0].uuid = GATT_UUID_GAP_DEVICE_NAME; |
| 421 | gap_cb.gatt_attr[0].handle = service[1].attribute_handle; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 422 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 423 | gap_cb.gatt_attr[1].uuid = GATT_UUID_GAP_ICON; |
| 424 | gap_cb.gatt_attr[1].handle = service[2].attribute_handle; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 425 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 426 | gap_cb.gatt_attr[2].uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL; |
| 427 | gap_cb.gatt_attr[2].handle = service[3].attribute_handle; |
| 428 | gap_cb.gatt_attr[2].attr_value.addr_resolution = 0; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 429 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 430 | #if (BTM_PERIPHERAL_ENABLED == TRUE) /* Only needed for peripheral testing */ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 431 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 432 | gap_cb.gatt_attr[3].uuid = GATT_UUID_GAP_PREF_CONN_PARAM; |
| 433 | gap_cb.gatt_attr[3].attr_value.conn_param.int_max = |
| 434 | GAP_PREFER_CONN_INT_MAX; /* 6 */ |
| 435 | gap_cb.gatt_attr[3].attr_value.conn_param.int_min = |
| 436 | GAP_PREFER_CONN_INT_MIN; /* 0 */ |
| 437 | gap_cb.gatt_attr[3].attr_value.conn_param.latency = |
| 438 | GAP_PREFER_CONN_LATENCY; /* 0 */ |
| 439 | gap_cb.gatt_attr[3].attr_value.conn_param.sp_tout = |
| 440 | GAP_PREFER_CONN_SP_TOUT; /* 2000 */ |
| 441 | gap_cb.gatt_attr[3].handle = service[4].attribute_handle; |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 442 | #endif |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 446 | * |
| 447 | * Function GAP_BleAttrDBUpdate |
| 448 | * |
| 449 | * Description GAP ATT database update. |
| 450 | * |
| 451 | * Returns void. |
| 452 | * |
| 453 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 454 | void GAP_BleAttrDBUpdate(uint16_t attr_uuid, tGAP_BLE_ATTR_VALUE* p_value) { |
| 455 | tGAP_ATTR* p_db_attr = gap_cb.gatt_attr; |
| 456 | uint8_t i = 0; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 457 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 458 | GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x", attr_uuid); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 459 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 460 | for (i = 0; i < GAP_MAX_CHAR_NUM; i++, p_db_attr++) { |
| 461 | if (p_db_attr->uuid == attr_uuid) { |
| 462 | GAP_TRACE_EVENT("Found attr_uuid=0x%04x", attr_uuid); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 463 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 464 | switch (attr_uuid) { |
| 465 | case GATT_UUID_GAP_ICON: |
| 466 | p_db_attr->attr_value.icon = p_value->icon; |
| 467 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 468 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 469 | case GATT_UUID_GAP_PREF_CONN_PARAM: |
| 470 | memcpy((void*)&p_db_attr->attr_value.conn_param, |
| 471 | (const void*)&p_value->conn_param, |
| 472 | sizeof(tGAP_BLE_PREF_PARAM)); |
| 473 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 474 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 475 | case GATT_UUID_GAP_DEVICE_NAME: |
| 476 | BTM_SetLocalDeviceName((char*)p_value->p_dev_name); |
| 477 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 478 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 479 | case GATT_UUID_GAP_CENTRAL_ADDR_RESOL: |
| 480 | p_db_attr->attr_value.addr_resolution = p_value->addr_resolution; |
| 481 | break; |
| 482 | } |
| 483 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 484 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 485 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 486 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 487 | return; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 491 | * |
| 492 | * Function gap_ble_send_cl_read_request |
| 493 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 494 | * Description utility function to send a read request for a GAP |
| 495 | * charactersitic |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 496 | * |
| 497 | * Returns true if read started, else false if GAP is busy |
| 498 | * |
| 499 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 500 | bool gap_ble_send_cl_read_request(tGAP_CLCB* p_clcb) { |
| 501 | tGATT_READ_PARAM param; |
| 502 | uint16_t uuid = 0; |
| 503 | bool started = false; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 504 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 505 | if (gap_ble_dequeue_request(p_clcb, &uuid, &p_clcb->p_cback)) { |
| 506 | memset(¶m, 0, sizeof(tGATT_READ_PARAM)); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 507 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 508 | param.service.uuid.len = LEN_UUID_16; |
| 509 | param.service.uuid.uu.uuid16 = uuid; |
| 510 | param.service.s_handle = 1; |
| 511 | param.service.e_handle = 0xFFFF; |
| 512 | param.service.auth_req = 0; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 513 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 514 | if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, ¶m) == |
| 515 | GATT_SUCCESS) { |
| 516 | p_clcb->cl_op_uuid = uuid; |
| 517 | started = true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 518 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 519 | } |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 520 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 521 | return started; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 525 | * |
| 526 | * Function gap_ble_cl_op_cmpl |
| 527 | * |
| 528 | * Description GAP client operation complete callback |
| 529 | * |
| 530 | * Returns void |
| 531 | * |
| 532 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 533 | void gap_ble_cl_op_cmpl(tGAP_CLCB* p_clcb, bool status, uint16_t len, |
| 534 | uint8_t* p_name) { |
| 535 | tGAP_BLE_CMPL_CBACK* p_cback = p_clcb->p_cback; |
| 536 | uint16_t op = p_clcb->cl_op_uuid; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 537 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 538 | GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 539 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 540 | p_clcb->cl_op_uuid = 0; |
| 541 | p_clcb->p_cback = NULL; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 542 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 543 | if (p_cback && op) { |
| 544 | GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl"); |
| 545 | (*p_cback)(status, p_clcb->bda, len, (char*)p_name); |
| 546 | } |
| 547 | |
| 548 | /* if no further activity is requested in callback, drop the link */ |
| 549 | if (p_clcb->connected) { |
| 550 | if (!gap_ble_send_cl_read_request(p_clcb)) { |
| 551 | GATT_Disconnect(p_clcb->conn_id); |
| 552 | gap_ble_dealloc_clcb(p_clcb); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 553 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 554 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 558 | * |
| 559 | * Function gap_ble_c_connect_cback |
| 560 | * |
| 561 | * Description Client connection callback. |
| 562 | * |
| 563 | * Returns void |
| 564 | * |
| 565 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 566 | static void gap_ble_c_connect_cback(UNUSED_ATTR tGATT_IF gatt_if, BD_ADDR bda, |
| 567 | uint16_t conn_id, bool connected, |
| 568 | tGATT_DISCONN_REASON reason, |
| 569 | UNUSED_ATTR tGATT_TRANSPORT transport) { |
| 570 | tGAP_CLCB* p_clcb = gap_find_clcb_by_bd_addr(bda); |
Ganesh Ganapathi Batta | 7fa4fba | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 571 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 572 | if (p_clcb != NULL) { |
| 573 | if (connected) { |
| 574 | p_clcb->conn_id = conn_id; |
| 575 | p_clcb->connected = true; |
| 576 | /* start operation is pending */ |
| 577 | gap_ble_send_cl_read_request(p_clcb); |
| 578 | } else { |
| 579 | p_clcb->connected = false; |
| 580 | gap_ble_cl_op_cmpl(p_clcb, false, 0, NULL); |
| 581 | /* clean up clcb */ |
| 582 | gap_ble_dealloc_clcb(p_clcb); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 583 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 584 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 588 | * |
| 589 | * Function gap_ble_c_cmpl_cback |
| 590 | * |
| 591 | * Description Client operation complete callback. |
| 592 | * |
| 593 | * Returns void |
| 594 | * |
| 595 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 596 | static void gap_ble_c_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op, |
| 597 | tGATT_STATUS status, tGATT_CL_COMPLETE* p_data) |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 598 | |
| 599 | { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 600 | tGAP_CLCB* p_clcb = gap_ble_find_clcb_by_conn_id(conn_id); |
| 601 | uint16_t op_type; |
| 602 | uint16_t min, max, latency, tout; |
| 603 | uint16_t len; |
| 604 | uint8_t* pp; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 605 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 606 | if (p_clcb == NULL) return; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 607 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 608 | op_type = p_clcb->cl_op_uuid; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 609 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 610 | GAP_TRACE_EVENT( |
| 611 | "gap_ble_c_cmpl_cback() - op_code: 0x%02x status: 0x%02x read_type: " |
| 612 | "0x%04x", |
| 613 | op, status, op_type); |
| 614 | /* Currently we only issue read commands */ |
| 615 | if (op != GATTC_OPTYPE_READ) return; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 616 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 617 | if (status != GATT_SUCCESS) { |
| 618 | gap_ble_cl_op_cmpl(p_clcb, false, 0, NULL); |
| 619 | return; |
| 620 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 621 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 622 | pp = p_data->att_value.value; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 623 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 624 | switch (op_type) { |
| 625 | case GATT_UUID_GAP_PREF_CONN_PARAM: |
| 626 | GAP_TRACE_EVENT("GATT_UUID_GAP_PREF_CONN_PARAM"); |
| 627 | /* Extract the peripheral preferred connection parameters and save them */ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 628 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 629 | STREAM_TO_UINT16(min, pp); |
| 630 | STREAM_TO_UINT16(max, pp); |
| 631 | STREAM_TO_UINT16(latency, pp); |
| 632 | STREAM_TO_UINT16(tout, pp); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 633 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 634 | BTM_BleSetPrefConnParams(p_clcb->bda, min, max, latency, tout); |
| 635 | /* release the connection here */ |
| 636 | gap_ble_cl_op_cmpl(p_clcb, true, 0, NULL); |
| 637 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 638 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 639 | case GATT_UUID_GAP_DEVICE_NAME: |
| 640 | GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME"); |
| 641 | len = (uint16_t)strlen((char*)pp); |
| 642 | if (len > GAP_CHAR_DEV_NAME_SIZE) len = GAP_CHAR_DEV_NAME_SIZE; |
| 643 | gap_ble_cl_op_cmpl(p_clcb, true, len, pp); |
| 644 | break; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 645 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 646 | case GATT_UUID_GAP_CENTRAL_ADDR_RESOL: |
| 647 | gap_ble_cl_op_cmpl(p_clcb, true, 1, pp); |
| 648 | break; |
| 649 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 653 | * |
| 654 | * Function gap_ble_accept_cl_operation |
| 655 | * |
| 656 | * Description Start a process to read peer address resolution capability |
| 657 | * |
| 658 | * Returns true if request accepted |
| 659 | * |
| 660 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 661 | bool gap_ble_accept_cl_operation(BD_ADDR peer_bda, uint16_t uuid, |
| 662 | tGAP_BLE_CMPL_CBACK* p_cback) { |
| 663 | tGAP_CLCB* p_clcb; |
| 664 | bool started = false; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 665 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 666 | if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM) |
| 667 | return (started); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 668 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 669 | p_clcb = gap_find_clcb_by_bd_addr(peer_bda); |
| 670 | if (p_clcb == NULL) { |
| 671 | p_clcb = gap_clcb_alloc(peer_bda); |
| 672 | if (p_clcb == NULL) { |
| 673 | GAP_TRACE_ERROR("gap_ble_accept_cl_operation max connection reached"); |
| 674 | return started; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 675 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 676 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 677 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 678 | GAP_TRACE_EVENT("%s() - BDA: %08x%04x cl_op_uuid: 0x%04x", __func__, |
| 679 | (peer_bda[0] << 24) + (peer_bda[1] << 16) + |
| 680 | (peer_bda[2] << 8) + peer_bda[3], |
| 681 | (peer_bda[4] << 8) + peer_bda[5], uuid); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 682 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 683 | if (GATT_GetConnIdIfConnected(gap_cb.gatt_if, peer_bda, &p_clcb->conn_id, |
| 684 | BT_TRANSPORT_LE)) |
| 685 | p_clcb->connected = true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 686 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 687 | /* hold the link here */ |
| 688 | if (!GATT_Connect(gap_cb.gatt_if, p_clcb->bda, true, BT_TRANSPORT_LE, false)) |
| 689 | return started; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 690 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 691 | /* enqueue the request */ |
| 692 | gap_ble_enqueue_request(p_clcb, uuid, p_cback); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 693 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 694 | if (p_clcb->connected && p_clcb->cl_op_uuid == 0) |
| 695 | started = gap_ble_send_cl_read_request(p_clcb); |
| 696 | else /* wait for connection up or pending operation to finish */ |
| 697 | started = true; |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 698 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 699 | return started; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 700 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 701 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 702 | * |
| 703 | * Function GAP_BleReadPeerPrefConnParams |
| 704 | * |
| 705 | * Description Start a process to read a connected peripheral's preferred |
| 706 | * connection parameters |
| 707 | * |
| 708 | * Returns true if read started, else false if GAP is busy |
| 709 | * |
| 710 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 711 | bool GAP_BleReadPeerPrefConnParams(BD_ADDR peer_bda) { |
| 712 | return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM, |
| 713 | NULL); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 717 | * |
| 718 | * Function GAP_BleReadPeerDevName |
| 719 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 720 | * Description Start a process to read a connected peripheral's device |
| 721 | * name. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 722 | * |
| 723 | * Returns true if request accepted |
| 724 | * |
| 725 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 726 | bool GAP_BleReadPeerDevName(BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK* p_cback) { |
| 727 | return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_DEVICE_NAME, |
| 728 | p_cback); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 731 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 732 | * |
| 733 | * Function GAP_BleReadPeerAddressResolutionCap |
| 734 | * |
| 735 | * Description Start a process to read peer address resolution capability |
| 736 | * |
| 737 | * Returns true if request accepted |
| 738 | * |
| 739 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 740 | bool GAP_BleReadPeerAddressResolutionCap(BD_ADDR peer_bda, |
| 741 | tGAP_BLE_CMPL_CBACK* p_cback) { |
| 742 | return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_CENTRAL_ADDR_RESOL, |
| 743 | p_cback); |
Satya Calloji | 444a8da | 2015-03-06 10:38:22 -0800 | [diff] [blame] | 744 | } |
Ganesh Ganapathi Batta | 7fa4fba | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 745 | |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 746 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 747 | * |
| 748 | * Function GAP_BleCancelReadPeerDevName |
| 749 | * |
| 750 | * Description Cancel reading a peripheral's device name. |
| 751 | * |
| 752 | * Returns true if request accepted |
| 753 | * |
| 754 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 755 | bool GAP_BleCancelReadPeerDevName(BD_ADDR peer_bda) { |
| 756 | tGAP_CLCB* p_clcb = gap_find_clcb_by_bd_addr(peer_bda); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 757 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 758 | GAP_TRACE_EVENT( |
| 759 | "GAP_BleCancelReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x", |
| 760 | (peer_bda[0] << 24) + (peer_bda[1] << 16) + (peer_bda[2] << 8) + |
| 761 | peer_bda[3], |
| 762 | (peer_bda[4] << 8) + peer_bda[5], |
| 763 | (p_clcb == NULL) ? 0 : p_clcb->cl_op_uuid); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 764 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 765 | if (p_clcb == NULL) { |
| 766 | GAP_TRACE_ERROR("Cannot cancel current op is not get dev name"); |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | if (!p_clcb->connected) { |
| 771 | if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, true)) { |
| 772 | GAP_TRACE_ERROR("Cannot cancel where No connection id"); |
| 773 | return false; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 774 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 775 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 776 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 777 | gap_ble_cl_op_cmpl(p_clcb, false, 0, NULL); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 778 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 779 | return (true); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 780 | } |