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