gatt_cl.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * this file contains the main GATT client functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #if BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
  25. #include <string.h>
  26. #include "osi/allocator.h"
  27. #include "gatt_int.h"
  28. #include "l2c_int.h"
  29. #define GATT_WRITE_LONG_HDR_SIZE 5 /* 1 opcode + 2 handle + 2 offset */
  30. #define GATT_READ_CHAR_VALUE_HDL (GATT_READ_CHAR_VALUE | 0x80)
  31. #define GATT_READ_INC_SRV_UUID128 (GATT_DISC_INC_SRVC | 0x90)
  32. #define GATT_PREP_WRITE_RSP_MIN_LEN 4
  33. #define GATT_NOTIFICATION_MIN_LEN 2
  34. #define GATT_WRITE_RSP_MIN_LEN 2
  35. #define GATT_INFO_RSP_MIN_LEN 1
  36. #define GATT_MTU_RSP_MIN_LEN 2
  37. #define GATT_READ_BY_TYPE_RSP_MIN_LEN 1
  38. /********************************************************************************
  39. ** G L O B A L G A T T D A T A *
  40. *********************************************************************************/
  41. void gatt_send_prepare_write(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb);
  42. static const UINT8 disc_type_to_att_opcode[GATT_DISC_MAX] = {
  43. 0,
  44. GATT_REQ_READ_BY_GRP_TYPE, /* GATT_DISC_SRVC_ALL = 1, */
  45. GATT_REQ_FIND_TYPE_VALUE, /* GATT_DISC_SRVC_BY_UUID, */
  46. GATT_REQ_READ_BY_TYPE, /* GATT_DISC_INC_SRVC, */
  47. GATT_REQ_READ_BY_TYPE, /* GATT_DISC_CHAR, */
  48. GATT_REQ_FIND_INFO /* GATT_DISC_CHAR_DSCPT, */
  49. };
  50. static const UINT16 disc_type_to_uuid[GATT_DISC_MAX] = {
  51. 0, /* reserved */
  52. GATT_UUID_PRI_SERVICE, /* <service> DISC_SRVC_ALL */
  53. GATT_UUID_PRI_SERVICE, /* <service> for DISC_SERVC_BY_UUID */
  54. GATT_UUID_INCLUDE_SERVICE, /* <include_service> for DISC_INC_SRVC */
  55. GATT_UUID_CHAR_DECLARE, /* <characteristic> for DISC_CHAR */
  56. 0 /* no type filtering for DISC_CHAR_DSCPT */
  57. };
  58. /*******************************************************************************
  59. **
  60. ** Function gatt_act_discovery
  61. **
  62. ** Description GATT discovery operation.
  63. **
  64. ** Returns void.
  65. **
  66. *******************************************************************************/
  67. void gatt_act_discovery(tGATT_CLCB *p_clcb)
  68. {
  69. UINT8 op_code = disc_type_to_att_opcode[p_clcb->op_subtype];
  70. tGATT_CL_MSG cl_req;
  71. tGATT_STATUS st;
  72. if (p_clcb->s_handle <= p_clcb->e_handle && p_clcb->s_handle != 0) {
  73. memset(&cl_req, 0, sizeof(tGATT_CL_MSG));
  74. cl_req.browse.s_handle = p_clcb->s_handle;
  75. cl_req.browse.e_handle = p_clcb->e_handle;
  76. if (disc_type_to_uuid[p_clcb->op_subtype] != 0) {
  77. cl_req.browse.uuid.len = 2;
  78. cl_req.browse.uuid.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype];
  79. }
  80. if (p_clcb->op_subtype == GATT_DISC_SRVC_BY_UUID) { /* fill in the FindByTypeValue request info*/
  81. cl_req.find_type_value.uuid.len = 2;
  82. cl_req.find_type_value.uuid.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype];
  83. cl_req.find_type_value.s_handle = p_clcb->s_handle;
  84. cl_req.find_type_value.e_handle = p_clcb->e_handle;
  85. cl_req.find_type_value.value_len = p_clcb->uuid.len;
  86. /* if service type is 32 bits UUID, convert it now */
  87. if (p_clcb->uuid.len == LEN_UUID_32) {
  88. cl_req.find_type_value.value_len = LEN_UUID_128;
  89. gatt_convert_uuid32_to_uuid128(cl_req.find_type_value.value, p_clcb->uuid.uu.uuid32);
  90. } else {
  91. memcpy (cl_req.find_type_value.value, &p_clcb->uuid.uu, p_clcb->uuid.len);
  92. }
  93. }
  94. st = attp_send_cl_msg(p_clcb->p_tcb, p_clcb->clcb_idx, op_code, &cl_req);
  95. if (st != GATT_SUCCESS && st != GATT_CMD_STARTED) {
  96. gatt_end_operation(p_clcb, GATT_ERROR, NULL);
  97. }
  98. } else { /* end of handle range */
  99. gatt_end_operation(p_clcb, GATT_SUCCESS, NULL);
  100. }
  101. }
  102. /*******************************************************************************
  103. **
  104. ** Function gatt_act_read
  105. **
  106. ** Description GATT read operation.
  107. **
  108. ** Returns void.
  109. **
  110. *******************************************************************************/
  111. void gatt_act_read (tGATT_CLCB *p_clcb, UINT16 offset)
  112. {
  113. tGATT_TCB *p_tcb = p_clcb->p_tcb;
  114. UINT8 rt = GATT_INTERNAL_ERROR;
  115. tGATT_CL_MSG msg;
  116. UINT8 op_code = 0;
  117. memset (&msg, 0, sizeof(tGATT_CL_MSG));
  118. switch (p_clcb->op_subtype) {
  119. case GATT_READ_CHAR_VALUE:
  120. case GATT_READ_BY_TYPE:
  121. op_code = GATT_REQ_READ_BY_TYPE;
  122. msg.browse.s_handle = p_clcb->s_handle;
  123. msg.browse.e_handle = p_clcb->e_handle;
  124. if (p_clcb->op_subtype == GATT_READ_BY_TYPE) {
  125. memcpy(&msg.browse.uuid, &p_clcb->uuid, sizeof(tBT_UUID));
  126. } else {
  127. msg.browse.uuid.len = LEN_UUID_16;
  128. msg.browse.uuid.uu.uuid16 = GATT_UUID_CHAR_DECLARE;
  129. }
  130. break;
  131. case GATT_READ_CHAR_VALUE_HDL:
  132. case GATT_READ_BY_HANDLE:
  133. if (!p_clcb->counter) {
  134. op_code = GATT_REQ_READ;
  135. msg.handle = p_clcb->s_handle;
  136. } else {
  137. if (!p_clcb->first_read_blob_after_read) {
  138. p_clcb->first_read_blob_after_read = TRUE;
  139. } else {
  140. p_clcb->first_read_blob_after_read = FALSE;
  141. }
  142. GATT_TRACE_DEBUG("gatt_act_read first_read_blob_after_read=%d",
  143. p_clcb->first_read_blob_after_read);
  144. op_code = GATT_REQ_READ_BLOB;
  145. msg.read_blob.offset = offset;
  146. msg.read_blob.handle = p_clcb->s_handle;
  147. }
  148. p_clcb->op_subtype &= ~ 0x80;
  149. break;
  150. case GATT_READ_PARTIAL:
  151. op_code = GATT_REQ_READ_BLOB;
  152. msg.read_blob.handle = p_clcb->s_handle;
  153. msg.read_blob.offset = offset;
  154. break;
  155. case GATT_READ_MULTIPLE:
  156. op_code = GATT_REQ_READ_MULTI;
  157. memcpy (&msg.read_multi, p_clcb->p_attr_buf, sizeof(tGATT_READ_MULTI));
  158. break;
  159. case GATT_READ_INC_SRV_UUID128:
  160. op_code = GATT_REQ_READ;
  161. msg.handle = p_clcb->s_handle;
  162. p_clcb->op_subtype &= ~ 0x90;
  163. break;
  164. default:
  165. GATT_TRACE_ERROR("Unknown read type: %d", p_clcb->op_subtype);
  166. break;
  167. }
  168. if (op_code != 0) {
  169. rt = attp_send_cl_msg(p_tcb, p_clcb->clcb_idx, op_code, &msg);
  170. }
  171. if ( op_code == 0 || (rt != GATT_SUCCESS && rt != GATT_CMD_STARTED)) {
  172. gatt_end_operation(p_clcb, rt, NULL);
  173. }
  174. }
  175. /*******************************************************************************
  176. **
  177. ** Function gatt_act_write
  178. **
  179. ** Description GATT write operation.
  180. **
  181. ** Returns void.
  182. **
  183. *******************************************************************************/
  184. void gatt_act_write (tGATT_CLCB *p_clcb, UINT8 sec_act)
  185. {
  186. tGATT_TCB *p_tcb = p_clcb->p_tcb;
  187. UINT8 rt = GATT_SUCCESS, op_code = 0;
  188. tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
  189. if (p_attr) {
  190. switch (p_clcb->op_subtype) {
  191. case GATT_WRITE_NO_RSP:
  192. l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTU_NUM, NULL);
  193. p_clcb->s_handle = p_attr->handle;
  194. op_code = (sec_act == GATT_SEC_SIGN_DATA) ? GATT_SIGN_CMD_WRITE : GATT_CMD_WRITE;
  195. rt = gatt_send_write_msg(p_tcb,
  196. p_clcb->clcb_idx,
  197. op_code,
  198. p_attr->handle,
  199. p_attr->len,
  200. 0,
  201. p_attr->value);
  202. break;
  203. case GATT_WRITE:
  204. if (p_attr->len <= (p_tcb->payload_size - GATT_HDR_SIZE)) {
  205. p_clcb->s_handle = p_attr->handle;
  206. rt = gatt_send_write_msg(p_tcb,
  207. p_clcb->clcb_idx,
  208. GATT_REQ_WRITE,
  209. p_attr->handle,
  210. p_attr->len,
  211. 0,
  212. p_attr->value);
  213. } else { /* prepare write for long attribute */
  214. gatt_send_prepare_write(p_tcb, p_clcb);
  215. }
  216. break;
  217. case GATT_WRITE_PREPARE:
  218. gatt_send_prepare_write(p_tcb, p_clcb);
  219. break;
  220. default:
  221. rt = GATT_INTERNAL_ERROR;
  222. GATT_TRACE_ERROR("Unknown write type: %d", p_clcb->op_subtype);
  223. break;
  224. }
  225. } else {
  226. rt = GATT_INTERNAL_ERROR;
  227. }
  228. if ((rt != GATT_SUCCESS && rt != GATT_CMD_STARTED && rt != GATT_CONGESTED)
  229. || (rt != GATT_CMD_STARTED && p_clcb->op_subtype == GATT_WRITE_NO_RSP)) {
  230. if (rt != GATT_SUCCESS) {
  231. GATT_TRACE_DEBUG("gatt_act_write() failed op_code=0x%x rt=%d", op_code, rt);
  232. }
  233. gatt_end_operation(p_clcb, rt, NULL);
  234. }
  235. }
  236. /*******************************************************************************
  237. **
  238. ** Function gatt_send_queue_write_cancel
  239. **
  240. ** Description send queue write cancel
  241. **
  242. ** Returns void.
  243. **
  244. *******************************************************************************/
  245. void gatt_send_queue_write_cancel (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, tGATT_EXEC_FLAG flag)
  246. {
  247. UINT8 rt ;
  248. GATT_TRACE_DEBUG("gatt_send_queue_write_cancel ");
  249. rt = attp_send_cl_msg(p_tcb, p_clcb->clcb_idx, GATT_REQ_EXEC_WRITE, (tGATT_CL_MSG *)&flag);
  250. if (rt != GATT_SUCCESS) {
  251. gatt_end_operation(p_clcb, rt, NULL);
  252. }
  253. }
  254. /*******************************************************************************
  255. **
  256. ** Function gatt_check_write_long_terminate
  257. **
  258. ** Description To terminate write long or not.
  259. **
  260. ** Returns TRUE: write long is terminated; FALSE keep sending.
  261. **
  262. *******************************************************************************/
  263. BOOLEAN gatt_check_write_long_terminate(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, tGATT_VALUE *p_rsp_value)
  264. {
  265. tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
  266. BOOLEAN exec = FALSE;
  267. tGATT_EXEC_FLAG flag = GATT_PREP_WRITE_EXEC;
  268. GATT_TRACE_DEBUG("gatt_check_write_long_terminate ");
  269. /* check the first write response status */
  270. if (p_rsp_value != NULL) {
  271. if (p_rsp_value->handle != p_attr->handle ||
  272. p_rsp_value->len != p_clcb->counter ||
  273. memcmp(p_rsp_value->value, p_attr->value + p_attr->offset, p_rsp_value->len)) {
  274. /* data does not match */
  275. p_clcb->status = GATT_ERROR;
  276. flag = GATT_PREP_WRITE_CANCEL;
  277. exec = TRUE;
  278. } else { /* response checking is good */
  279. p_clcb->status = GATT_SUCCESS;
  280. /* update write offset and check if end of attribute value */
  281. if ((p_attr->offset += p_rsp_value->len) >= p_attr->len) {
  282. exec = TRUE;
  283. }
  284. }
  285. }
  286. if (exec) {
  287. gatt_send_queue_write_cancel (p_tcb, p_clcb, flag);
  288. return TRUE;
  289. }
  290. return FALSE;
  291. }
  292. /*******************************************************************************
  293. **
  294. ** Function gatt_send_prepare_write
  295. **
  296. ** Description Send prepare write.
  297. **
  298. ** Returns void.
  299. **
  300. *******************************************************************************/
  301. void gatt_send_prepare_write(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb)
  302. {
  303. tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
  304. UINT16 to_send, offset;
  305. UINT8 rt = GATT_SUCCESS;
  306. UINT8 type = p_clcb->op_subtype;
  307. GATT_TRACE_DEBUG("gatt_send_prepare_write type=0x%x", type );
  308. to_send = p_attr->len - p_attr->offset;
  309. if (to_send > (p_tcb->payload_size - GATT_WRITE_LONG_HDR_SIZE)) { /* 2 = UINT16 offset bytes */
  310. to_send = p_tcb->payload_size - GATT_WRITE_LONG_HDR_SIZE;
  311. }
  312. p_clcb->s_handle = p_attr->handle;
  313. offset = p_attr->offset;
  314. if (type == GATT_WRITE_PREPARE) {
  315. offset += p_clcb->start_offset;
  316. }
  317. GATT_TRACE_DEBUG("offset =0x%x len=%d", offset, to_send );
  318. rt = gatt_send_write_msg(p_tcb,
  319. p_clcb->clcb_idx,
  320. GATT_REQ_PREPARE_WRITE,
  321. p_attr->handle,
  322. to_send, /* length */
  323. offset, /* used as offset */
  324. p_attr->value + p_attr->offset); /* data */
  325. /* remember the write long attribute length */
  326. p_clcb->counter = to_send;
  327. if (rt != GATT_SUCCESS && rt != GATT_CMD_STARTED) {
  328. gatt_end_operation(p_clcb, rt, NULL);
  329. }
  330. }
  331. /*******************************************************************************
  332. **
  333. ** Function gatt_process_find_type_value_rsp
  334. **
  335. ** Description This function is called to handle find by type value response.
  336. **
  337. **
  338. ** Returns void
  339. **
  340. *******************************************************************************/
  341. void gatt_process_find_type_value_rsp (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT16 len, UINT8 *p_data)
  342. {
  343. tGATT_DISC_RES result;
  344. UINT8 *p = p_data;
  345. UNUSED(p_tcb);
  346. GATT_TRACE_DEBUG("gatt_process_find_type_value_rsp ");
  347. /* unexpected response */
  348. if (p_clcb->operation != GATTC_OPTYPE_DISCOVERY || p_clcb->op_subtype != GATT_DISC_SRVC_BY_UUID) {
  349. return;
  350. }
  351. memset (&result, 0, sizeof(tGATT_DISC_RES));
  352. result.type.len = 2;
  353. result.type.uu.uuid16 = GATT_UUID_PRI_SERVICE;
  354. /* returns a series of handle ranges */
  355. while (len >= 4) {
  356. STREAM_TO_UINT16 (result.handle, p);
  357. STREAM_TO_UINT16 (result.value.group_value.e_handle, p);
  358. memcpy (&result.value.group_value.service_type, &p_clcb->uuid, sizeof(tBT_UUID));
  359. len -= 4;
  360. if (p_clcb->p_reg->app_cb.p_disc_res_cb) {
  361. (*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &result);
  362. }
  363. }
  364. /* last handle + 1 */
  365. p_clcb->s_handle = (result.value.group_value.e_handle == 0) ? 0 : (result.value.group_value.e_handle + 1);
  366. /* initiate another request */
  367. gatt_act_discovery(p_clcb) ;
  368. }
  369. /*******************************************************************************
  370. **
  371. ** Function gatt_process_read_info_rsp
  372. **
  373. ** Description This function is called to handle the read information
  374. ** response.
  375. **
  376. **
  377. ** Returns void
  378. **
  379. *******************************************************************************/
  380. void gatt_process_read_info_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
  381. UINT16 len, UINT8 *p_data)
  382. {
  383. tGATT_DISC_RES result;
  384. UINT8 *p = p_data, uuid_len = 0, type;
  385. UNUSED(p_tcb);
  386. UNUSED(op_code);
  387. if (len < GATT_INFO_RSP_MIN_LEN) {
  388. GATT_TRACE_ERROR("invalid Info Response PDU received, discard.");
  389. gatt_end_operation(p_clcb, GATT_INVALID_PDU, NULL);
  390. return;
  391. }
  392. /* unexpected response */
  393. if (p_clcb->operation != GATTC_OPTYPE_DISCOVERY || p_clcb->op_subtype != GATT_DISC_CHAR_DSCPT) {
  394. return;
  395. }
  396. STREAM_TO_UINT8(type, p);
  397. len -= 1;
  398. if (type == GATT_INFO_TYPE_PAIR_16) {
  399. uuid_len = LEN_UUID_16;
  400. } else if (type == GATT_INFO_TYPE_PAIR_128) {
  401. uuid_len = LEN_UUID_128;
  402. }
  403. while (len >= uuid_len + 2) {
  404. STREAM_TO_UINT16 (result.handle, p);
  405. if (uuid_len > 0) {
  406. if (!gatt_parse_uuid_from_cmd(&result.type, uuid_len, &p)) {
  407. break;
  408. }
  409. } else {
  410. memcpy (&result.type, &p_clcb->uuid, sizeof(tBT_UUID));
  411. }
  412. len -= (uuid_len + 2);
  413. if (p_clcb->p_reg->app_cb.p_disc_res_cb) {
  414. (*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &result);
  415. }
  416. }
  417. p_clcb->s_handle = (result.handle == 0) ? 0 : (result.handle + 1);
  418. /* initiate another request */
  419. gatt_act_discovery(p_clcb) ;
  420. }
  421. /*******************************************************************************
  422. **
  423. ** Function gatt_proc_disc_error_rsp
  424. **
  425. ** Description This function process the read by type response and send another
  426. ** request if needed.
  427. **
  428. ** Returns void.
  429. **
  430. *******************************************************************************/
  431. void gatt_proc_disc_error_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 opcode,
  432. UINT16 handle, UINT8 reason)
  433. {
  434. tGATT_STATUS status = (tGATT_STATUS) reason;
  435. UNUSED(p_tcb);
  436. UNUSED(handle);
  437. GATT_TRACE_DEBUG("gatt_proc_disc_error_rsp reason: %02x cmd_code %04x", reason, opcode);
  438. switch (opcode) {
  439. case GATT_REQ_READ_BY_GRP_TYPE:
  440. case GATT_REQ_FIND_TYPE_VALUE:
  441. case GATT_REQ_READ_BY_TYPE:
  442. case GATT_REQ_FIND_INFO:
  443. if (reason == GATT_NOT_FOUND) {
  444. status = GATT_SUCCESS;
  445. GATT_TRACE_DEBUG("Discovery completed");
  446. }
  447. break;
  448. default:
  449. GATT_TRACE_ERROR("Incorrect discovery opcode %04x", opcode);
  450. break;
  451. }
  452. gatt_end_operation(p_clcb, status, NULL);
  453. }
  454. /*******************************************************************************
  455. **
  456. ** Function gatt_process_error_rsp
  457. **
  458. ** Description This function is called to handle the error response
  459. **
  460. **
  461. ** Returns void
  462. **
  463. *******************************************************************************/
  464. void gatt_process_error_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
  465. UINT16 len, UINT8 *p_data)
  466. {
  467. UINT8 opcode, reason, * p = p_data;
  468. UINT16 handle;
  469. tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
  470. UNUSED(op_code);
  471. UNUSED(len);
  472. GATT_TRACE_DEBUG("gatt_process_error_rsp ");
  473. STREAM_TO_UINT8(opcode, p);
  474. STREAM_TO_UINT16(handle, p);
  475. STREAM_TO_UINT8(reason, p);
  476. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY) {
  477. gatt_proc_disc_error_rsp(p_tcb, p_clcb, opcode, handle, reason);
  478. } else {
  479. if ( (p_clcb->operation == GATTC_OPTYPE_WRITE) &&
  480. (p_clcb->op_subtype == GATT_WRITE) &&
  481. (opcode == GATT_REQ_PREPARE_WRITE) &&
  482. (p_attr) &&
  483. (handle == p_attr->handle) ) {
  484. if (reason == GATT_SUCCESS){
  485. reason = GATT_ERROR;
  486. }
  487. p_clcb->status = reason;
  488. gatt_send_queue_write_cancel(p_tcb, p_clcb, GATT_PREP_WRITE_CANCEL);
  489. } else if ((p_clcb->operation == GATTC_OPTYPE_READ) &&
  490. ((p_clcb->op_subtype == GATT_READ_CHAR_VALUE_HDL) ||
  491. (p_clcb->op_subtype == GATT_READ_BY_HANDLE)) &&
  492. (opcode == GATT_REQ_READ_BLOB) &&
  493. p_clcb->first_read_blob_after_read &&
  494. (reason == GATT_NOT_LONG)) {
  495. gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p_clcb->p_attr_buf);
  496. } else {
  497. gatt_end_operation(p_clcb, reason, NULL);
  498. }
  499. }
  500. }
  501. /*******************************************************************************
  502. **
  503. ** Function gatt_process_prep_write_rsp
  504. **
  505. ** Description This function is called to handle the read response
  506. **
  507. **
  508. ** Returns void
  509. **
  510. *******************************************************************************/
  511. void gatt_process_prep_write_rsp (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
  512. UINT16 len, UINT8 *p_data)
  513. {
  514. tGATT_VALUE value = {0};
  515. UINT8 *p = p_data;
  516. GATT_TRACE_DEBUG("value resp op_code = %s len = %d", gatt_dbg_op_name(op_code), len);
  517. if (len < GATT_PREP_WRITE_RSP_MIN_LEN) {
  518. GATT_TRACE_ERROR("illegal prepare write response length, discard");
  519. gatt_end_operation(p_clcb, GATT_INVALID_PDU, &value);
  520. return;
  521. }
  522. STREAM_TO_UINT16 (value.handle, p);
  523. STREAM_TO_UINT16 (value.offset, p);
  524. value.len = len - 4;
  525. memcpy (value.value, p, value.len);
  526. if (p_clcb->op_subtype == GATT_WRITE_PREPARE) {
  527. p_clcb->status = GATT_SUCCESS;
  528. /* application should verify handle offset
  529. and value are matched or not */
  530. gatt_end_operation(p_clcb, p_clcb->status, &value);
  531. } else if (p_clcb->op_subtype == GATT_WRITE ) {
  532. if (!gatt_check_write_long_terminate(p_tcb, p_clcb, &value)) {
  533. gatt_send_prepare_write(p_tcb, p_clcb);
  534. }
  535. }
  536. }
  537. /*******************************************************************************
  538. **
  539. ** Function gatt_process_notification
  540. **
  541. ** Description This function is called to handle the handle value indication
  542. ** or handle value notification.
  543. **
  544. **
  545. ** Returns void
  546. **
  547. *******************************************************************************/
  548. void gatt_process_notification(tGATT_TCB *p_tcb, UINT8 op_code,
  549. UINT16 len, UINT8 *p_data)
  550. {
  551. tGATT_VALUE value = {0};
  552. tGATT_REG *p_reg;
  553. UINT16 conn_id;
  554. tGATT_STATUS encrypt_status;
  555. UINT8 *p = p_data, i,
  556. event = (op_code == GATT_HANDLE_VALUE_NOTIF) ? GATTC_OPTYPE_NOTIFICATION : GATTC_OPTYPE_INDICATION;
  557. GATT_TRACE_DEBUG("gatt_process_notification ");
  558. if (len < GATT_NOTIFICATION_MIN_LEN) {
  559. GATT_TRACE_ERROR("illegal notification PDU length, discard");
  560. return;
  561. }
  562. STREAM_TO_UINT16 (value.handle, p);
  563. value.len = len - 2;
  564. memcpy (value.value, p, value.len);
  565. if (!GATT_HANDLE_IS_VALID(value.handle)) {
  566. /* illegal handle, send ack now */
  567. if (op_code == GATT_HANDLE_VALUE_IND) {
  568. attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, NULL);
  569. }
  570. return;
  571. }
  572. if (event == GATTC_OPTYPE_INDICATION) {
  573. if (p_tcb->ind_count) {
  574. /* this is an error case that receiving an indication but we
  575. still has an indication not being acked yet.
  576. For now, just log the error reset the counter.
  577. Later we need to disconnect the link unconditionally.
  578. */
  579. GATT_TRACE_ERROR("gatt_process_notification rcv Ind. but ind_count=%d (will reset ind_count)", p_tcb->ind_count);
  580. }
  581. p_tcb->ind_count = 0;
  582. }
  583. /* should notify all registered client with the handle value notificaion/indication
  584. Note: need to do the indication count and start timer first then do callback
  585. */
  586. for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
  587. if (p_reg->in_use && p_reg->app_cb.p_cmpl_cb && (event == GATTC_OPTYPE_INDICATION)) {
  588. p_tcb->ind_count++;
  589. }
  590. }
  591. if (event == GATTC_OPTYPE_INDICATION) {
  592. /* start a timer for app confirmation */
  593. if (p_tcb->ind_count > 0) {
  594. gatt_start_ind_ack_timer(p_tcb);
  595. } else { /* no app to indicate, or invalid handle */
  596. attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, NULL);
  597. }
  598. }
  599. encrypt_status = gatt_get_link_encrypt_status(p_tcb);
  600. for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
  601. if (p_reg->in_use && p_reg->app_cb.p_cmpl_cb) {
  602. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  603. (*p_reg->app_cb.p_cmpl_cb) (conn_id, event, encrypt_status, (tGATT_CL_COMPLETE *)&value);
  604. }
  605. }
  606. }
  607. /*******************************************************************************
  608. **
  609. ** Function gatt_process_read_by_type_rsp
  610. **
  611. ** Description This function is called to handle the read by type response.
  612. ** read by type can be used for discovery, or read by type or
  613. ** read characteristic value.
  614. **
  615. ** Returns void
  616. **
  617. *******************************************************************************/
  618. void gatt_process_read_by_type_rsp (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
  619. UINT16 len, UINT8 *p_data)
  620. {
  621. tGATT_DISC_RES result;
  622. tGATT_DISC_VALUE record_value;
  623. UINT8 *p = p_data, value_len, handle_len = 2;
  624. UINT16 handle = 0;
  625. /* discovery procedure and no callback function registered */
  626. if (((!p_clcb->p_reg) || (!p_clcb->p_reg->app_cb.p_disc_res_cb)) && (p_clcb->operation == GATTC_OPTYPE_DISCOVERY)) {
  627. return;
  628. }
  629. if (len < GATT_READ_BY_TYPE_RSP_MIN_LEN) {
  630. GATT_TRACE_ERROR("Illegal ReadByType/ReadByGroupType Response length, discard");
  631. gatt_end_operation(p_clcb, GATT_INVALID_PDU, NULL);
  632. return;
  633. }
  634. STREAM_TO_UINT8(value_len, p);
  635. if ((value_len > (p_tcb->payload_size - 2)) || (value_len > (len - 1)) ) {
  636. /* this is an error case that server's response containing a value length which is larger than MTU-2
  637. or value_len > message total length -1 */
  638. GATT_TRACE_ERROR("gatt_process_read_by_type_rsp: Discard response op_code=%d vale_len=%d > (MTU-2=%d or msg_len-1=%d)",
  639. op_code, value_len, (p_tcb->payload_size - 2), (len - 1));
  640. gatt_end_operation(p_clcb, GATT_ERROR, NULL);
  641. return;
  642. }
  643. if (op_code == GATT_RSP_READ_BY_GRP_TYPE) {
  644. handle_len = 4;
  645. }
  646. value_len -= handle_len; /* substract the handle pairs bytes */
  647. len -= 1;
  648. while (len >= (handle_len + value_len)) {
  649. STREAM_TO_UINT16(handle, p);
  650. if (!GATT_HANDLE_IS_VALID(handle)) {
  651. gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
  652. return;
  653. }
  654. memset(&result, 0, sizeof(tGATT_DISC_RES));
  655. memset(&record_value, 0, sizeof(tGATT_DISC_VALUE));
  656. result.handle = handle;
  657. result.type.len = 2;
  658. result.type.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype];
  659. /* discover all services */
  660. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
  661. p_clcb->op_subtype == GATT_DISC_SRVC_ALL &&
  662. op_code == GATT_RSP_READ_BY_GRP_TYPE) {
  663. STREAM_TO_UINT16(handle, p);
  664. if (!GATT_HANDLE_IS_VALID(handle)) {
  665. gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
  666. return;
  667. } else {
  668. record_value.group_value.e_handle = handle;
  669. if (!gatt_parse_uuid_from_cmd(&record_value.group_value.service_type, value_len, &p)) {
  670. GATT_TRACE_ERROR("discover all service response parsing failure");
  671. break;
  672. }
  673. }
  674. }
  675. /* discover included service */
  676. else if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY && p_clcb->op_subtype == GATT_DISC_INC_SRVC) {
  677. STREAM_TO_UINT16(record_value.incl_service.s_handle, p);
  678. STREAM_TO_UINT16(record_value.incl_service.e_handle, p);
  679. if (!GATT_HANDLE_IS_VALID(record_value.incl_service.s_handle) ||
  680. !GATT_HANDLE_IS_VALID(record_value.incl_service.e_handle)) {
  681. gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
  682. return;
  683. }
  684. if (value_len == 6) {
  685. STREAM_TO_UINT16(record_value.incl_service.service_type.uu.uuid16, p);
  686. record_value.incl_service.service_type.len = LEN_UUID_16;
  687. } else if (value_len == 4) {
  688. p_clcb->s_handle = record_value.incl_service.s_handle;
  689. p_clcb->read_uuid128.wait_for_read_rsp = TRUE;
  690. p_clcb->read_uuid128.next_disc_start_hdl = handle + 1;
  691. memcpy(&p_clcb->read_uuid128.result, &result, sizeof(result));
  692. memcpy(&p_clcb->read_uuid128.result.value, &record_value, sizeof (result.value));
  693. p_clcb->op_subtype |= 0x90;
  694. gatt_act_read(p_clcb, 0);
  695. return;
  696. } else {
  697. GATT_TRACE_ERROR("gatt_process_read_by_type_rsp INCL_SRVC failed with invalid data value_len=%d", value_len);
  698. gatt_end_operation(p_clcb, GATT_INVALID_PDU, (void *)p);
  699. return;
  700. }
  701. }
  702. /* read by type */
  703. else if (p_clcb->operation == GATTC_OPTYPE_READ && p_clcb->op_subtype == GATT_READ_BY_TYPE) {
  704. p_clcb->counter = len - 2;
  705. p_clcb->s_handle = handle;
  706. if ( p_clcb->counter == (p_clcb->p_tcb->payload_size - 4)) {
  707. p_clcb->op_subtype = GATT_READ_BY_HANDLE;
  708. if (!p_clcb->p_attr_buf) {
  709. p_clcb->p_attr_buf = (UINT8 *)osi_malloc(GATT_MAX_ATTR_LEN);
  710. }
  711. if (p_clcb->p_attr_buf && p_clcb->counter <= GATT_MAX_ATTR_LEN) {
  712. memcpy(p_clcb->p_attr_buf, p, p_clcb->counter);
  713. gatt_act_read(p_clcb, p_clcb->counter);
  714. } else {
  715. gatt_end_operation(p_clcb, GATT_INTERNAL_ERROR, (void *)p);
  716. }
  717. } else {
  718. gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p);
  719. }
  720. return;
  721. } else { /* discover characterisitic */
  722. STREAM_TO_UINT8 (record_value.dclr_value.char_prop, p);
  723. STREAM_TO_UINT16(record_value.dclr_value.val_handle, p);
  724. if (!GATT_HANDLE_IS_VALID(record_value.dclr_value.val_handle)) {
  725. gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
  726. return;
  727. }
  728. if (!gatt_parse_uuid_from_cmd(&record_value.dclr_value.char_uuid, (UINT16)(value_len - 3), &p)) {
  729. gatt_end_operation(p_clcb, GATT_SUCCESS, NULL);
  730. /* invalid format, and skip the result */
  731. return;
  732. }
  733. /* UUID not matching */
  734. if (!gatt_uuid_compare(record_value.dclr_value.char_uuid, p_clcb->uuid)) {
  735. len -= (value_len + 2);
  736. continue; /* skip the result, and look for next one */
  737. } else if (p_clcb->operation == GATTC_OPTYPE_READ)
  738. /* UUID match for read characteristic value */
  739. {
  740. /* only read the first matching UUID characteristic value, and
  741. discard the rest results */
  742. p_clcb->s_handle = record_value.dclr_value.val_handle;
  743. p_clcb->op_subtype |= 0x80;
  744. gatt_act_read(p_clcb, 0);
  745. return;
  746. }
  747. }
  748. len -= (value_len + handle_len);
  749. /* result is (handle, 16bits UUID) pairs */
  750. memcpy (&result.value, &record_value, sizeof (result.value));
  751. /* send callback if is discover procedure */
  752. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY && p_clcb->p_reg->app_cb.p_disc_res_cb) {
  753. (*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &result);
  754. }
  755. }
  756. p_clcb->s_handle = (handle == 0) ? 0 : (handle + 1);
  757. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY) {
  758. /* initiate another request */
  759. gatt_act_discovery(p_clcb) ;
  760. } else { /* read characteristic value */
  761. gatt_act_read(p_clcb, 0);
  762. }
  763. }
  764. /*******************************************************************************
  765. **
  766. ** Function gatt_process_read_rsp
  767. **
  768. ** Description This function is called to handle the read BLOB response
  769. **
  770. **
  771. ** Returns void
  772. **
  773. *******************************************************************************/
  774. void gatt_process_read_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
  775. UINT16 len, UINT8 *p_data)
  776. {
  777. UINT16 offset = p_clcb->counter;
  778. UINT8 *p = p_data;
  779. UNUSED(op_code);
  780. if (p_clcb->operation == GATTC_OPTYPE_READ) {
  781. if (p_clcb->op_subtype != GATT_READ_BY_HANDLE) {
  782. p_clcb->counter = len;
  783. gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p);
  784. } else {
  785. /* allocate GKI buffer holding up long attribute value */
  786. if (!p_clcb->p_attr_buf) {
  787. p_clcb->p_attr_buf = (UINT8 *)osi_malloc(GATT_MAX_ATTR_LEN);
  788. }
  789. /* copy attrobute value into cb buffer */
  790. if (p_clcb->p_attr_buf && offset < GATT_MAX_ATTR_LEN) {
  791. if ((len + offset) > GATT_MAX_ATTR_LEN) {
  792. len = GATT_MAX_ATTR_LEN - offset;
  793. }
  794. p_clcb->counter += len;
  795. memcpy(p_clcb->p_attr_buf + offset, p, len);
  796. /* send next request if needed */
  797. if (len == (p_tcb->payload_size - 1) && /* full packet for read or read blob rsp */
  798. len + offset < GATT_MAX_ATTR_LEN) {
  799. GATT_TRACE_DEBUG("full pkt issue read blob for remianing bytes old offset=%d len=%d new offset=%d",
  800. offset, len, p_clcb->counter);
  801. gatt_act_read(p_clcb, p_clcb->counter);
  802. } else { /* end of request, send callback */
  803. gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p_clcb->p_attr_buf);
  804. }
  805. } else { /* exception, should not happen */
  806. GATT_TRACE_ERROR("attr offset = %d p_attr_buf = %p ", offset, p_clcb->p_attr_buf);
  807. gatt_end_operation(p_clcb, GATT_NO_RESOURCES, (void *)p_clcb->p_attr_buf);
  808. }
  809. }
  810. } else {
  811. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
  812. p_clcb->op_subtype == GATT_DISC_INC_SRVC &&
  813. p_clcb->read_uuid128.wait_for_read_rsp ) {
  814. p_clcb->s_handle = p_clcb->read_uuid128.next_disc_start_hdl;
  815. p_clcb->read_uuid128.wait_for_read_rsp = FALSE;
  816. if (len == LEN_UUID_128) {
  817. memcpy(p_clcb->read_uuid128.result.value.incl_service.service_type.uu.uuid128, p, len);
  818. p_clcb->read_uuid128.result.value.incl_service.service_type.len = LEN_UUID_128;
  819. if ( p_clcb->p_reg->app_cb.p_disc_res_cb) {
  820. (*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &p_clcb->read_uuid128.result);
  821. }
  822. gatt_act_discovery(p_clcb) ;
  823. } else {
  824. gatt_end_operation(p_clcb, GATT_INVALID_PDU, (void *)p);
  825. }
  826. }
  827. }
  828. }
  829. /*******************************************************************************
  830. **
  831. ** Function gatt_process_handle_rsp
  832. **
  833. ** Description This function is called to handle the write response
  834. **
  835. **
  836. ** Returns void
  837. **
  838. *******************************************************************************/
  839. void gatt_process_handle_rsp(tGATT_CLCB *p_clcb)
  840. {
  841. gatt_end_operation(p_clcb, GATT_SUCCESS, NULL);
  842. }
  843. /*******************************************************************************
  844. **
  845. ** Function gatt_process_mtu_rsp
  846. **
  847. ** Description This function is called to process the configure MTU response.
  848. **
  849. **
  850. ** Returns void
  851. **
  852. *******************************************************************************/
  853. void gatt_process_mtu_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT16 len, UINT8 *p_data)
  854. {
  855. UINT16 mtu;
  856. tGATT_STATUS status = GATT_SUCCESS;
  857. if (len < GATT_MTU_RSP_MIN_LEN) {
  858. GATT_TRACE_ERROR("invalid MTU response PDU received, discard.");
  859. status = GATT_INVALID_PDU;
  860. } else {
  861. STREAM_TO_UINT16(mtu, p_data);
  862. if (mtu < p_tcb->payload_size && mtu >= GATT_DEF_BLE_MTU_SIZE) {
  863. p_tcb->payload_size = mtu;
  864. }
  865. }
  866. /* host will set packet data length to 251 automatically if remote device support set packet data length,
  867. so l2cble_set_fixed_channel_tx_data_length() is not necessary.
  868. l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size);
  869. */
  870. gatt_end_operation(p_clcb, status, NULL);
  871. }
  872. /*******************************************************************************
  873. **
  874. ** Function gatt_cmd_to_rsp_code
  875. **
  876. ** Description The function convert a ATT command op code into the corresponding
  877. ** response code assume no error occurs.
  878. **
  879. ** Returns response code.
  880. **
  881. *******************************************************************************/
  882. UINT8 gatt_cmd_to_rsp_code (UINT8 cmd_code)
  883. {
  884. UINT8 rsp_code = 0;
  885. if (cmd_code > 1 && cmd_code != GATT_CMD_WRITE) {
  886. rsp_code = cmd_code + 1;
  887. }
  888. return rsp_code;
  889. }
  890. /*******************************************************************************
  891. **
  892. ** Function gatt_cl_send_next_cmd_inq
  893. **
  894. ** Description Find next command in queue and sent to server
  895. **
  896. ** Returns TRUE if command sent, otherwise FALSE.
  897. **
  898. *******************************************************************************/
  899. BOOLEAN gatt_cl_send_next_cmd_inq(tGATT_TCB *p_tcb)
  900. {
  901. tGATT_CMD_Q *p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
  902. BOOLEAN sent = FALSE;
  903. UINT8 rsp_code;
  904. tGATT_CLCB *p_clcb = NULL;
  905. tGATT_STATUS att_ret = GATT_SUCCESS;
  906. while (!sent &&
  907. p_tcb->pending_cl_req != p_tcb->next_slot_inq &&
  908. p_cmd->to_send && p_cmd->p_cmd != NULL) {
  909. att_ret = attp_send_msg_to_l2cap(p_tcb, p_cmd->p_cmd);
  910. if (att_ret == GATT_SUCCESS || att_ret == GATT_CONGESTED) {
  911. sent = TRUE;
  912. p_cmd->to_send = FALSE;
  913. if(p_cmd->p_cmd) {
  914. osi_free(p_cmd->p_cmd);
  915. p_cmd->p_cmd = NULL;
  916. }
  917. /* dequeue the request if is write command or sign write */
  918. if (p_cmd->op_code != GATT_CMD_WRITE && p_cmd->op_code != GATT_SIGN_CMD_WRITE) {
  919. gatt_start_rsp_timer (p_cmd->clcb_idx);
  920. } else {
  921. p_clcb = gatt_cmd_dequeue(p_tcb, &rsp_code);
  922. /* if no ack needed, keep sending */
  923. if (att_ret == GATT_SUCCESS) {
  924. sent = FALSE;
  925. }
  926. p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
  927. /* send command complete callback here */
  928. gatt_end_operation(p_clcb, att_ret, NULL);
  929. }
  930. } else {
  931. GATT_TRACE_ERROR("gatt_cl_send_next_cmd_inq: L2CAP sent error");
  932. memset(p_cmd, 0, sizeof(tGATT_CMD_Q));
  933. p_tcb->pending_cl_req ++;
  934. p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
  935. }
  936. }
  937. return sent;
  938. }
  939. /*******************************************************************************
  940. **
  941. ** Function gatt_client_handle_server_rsp
  942. **
  943. ** Description This function is called to handle the server response to
  944. ** client.
  945. **
  946. **
  947. ** Returns void
  948. **
  949. *******************************************************************************/
  950. void gatt_client_handle_server_rsp (tGATT_TCB *p_tcb, UINT8 op_code,
  951. UINT16 len, UINT8 *p_data)
  952. {
  953. tGATT_CLCB *p_clcb = NULL;
  954. UINT8 rsp_code;
  955. if (op_code != GATT_HANDLE_VALUE_IND && op_code != GATT_HANDLE_VALUE_NOTIF) {
  956. p_clcb = gatt_cmd_dequeue(p_tcb, &rsp_code);
  957. rsp_code = gatt_cmd_to_rsp_code(rsp_code);
  958. if (p_clcb == NULL || (rsp_code != op_code && op_code != GATT_RSP_ERROR)) {
  959. GATT_TRACE_WARNING ("ATT - Ignore wrong response. Receives (%02x) \
  960. Request(%02x) Ignored", op_code, rsp_code);
  961. return;
  962. } else {
  963. btu_stop_timer (&p_clcb->rsp_timer_ent);
  964. p_clcb->retry_count = 0;
  965. }
  966. }
  967. /* the size of the message may not be bigger than the local max PDU size*/
  968. /* The message has to be smaller than the agreed MTU, len does not count op_code */
  969. if (len >= p_tcb->payload_size) {
  970. GATT_TRACE_ERROR("invalid response/indicate pkt size: %d, PDU size: %d", len + 1, p_tcb->payload_size);
  971. if (op_code != GATT_HANDLE_VALUE_NOTIF &&
  972. op_code != GATT_HANDLE_VALUE_IND) {
  973. gatt_end_operation(p_clcb, GATT_ERROR, NULL);
  974. }
  975. } else {
  976. switch (op_code) {
  977. case GATT_RSP_ERROR:
  978. gatt_process_error_rsp(p_tcb, p_clcb, op_code, len, p_data);
  979. break;
  980. case GATT_RSP_MTU: /* 2 bytes mtu */
  981. gatt_process_mtu_rsp(p_tcb, p_clcb, len , p_data);
  982. break;
  983. case GATT_RSP_FIND_INFO:
  984. gatt_process_read_info_rsp(p_tcb, p_clcb, op_code, len, p_data);
  985. break;
  986. case GATT_RSP_READ_BY_TYPE:
  987. case GATT_RSP_READ_BY_GRP_TYPE:
  988. gatt_process_read_by_type_rsp(p_tcb, p_clcb, op_code, len, p_data);
  989. break;
  990. case GATT_RSP_READ:
  991. case GATT_RSP_READ_BLOB:
  992. case GATT_RSP_READ_MULTI:
  993. gatt_process_read_rsp(p_tcb, p_clcb, op_code, len, p_data);
  994. break;
  995. case GATT_RSP_FIND_TYPE_VALUE: /* disc service with UUID */
  996. gatt_process_find_type_value_rsp(p_tcb, p_clcb, len, p_data);
  997. break;
  998. case GATT_RSP_WRITE:
  999. gatt_process_handle_rsp(p_clcb);
  1000. break;
  1001. case GATT_RSP_PREPARE_WRITE:
  1002. gatt_process_prep_write_rsp(p_tcb, p_clcb, op_code, len, p_data);
  1003. break;
  1004. case GATT_RSP_EXEC_WRITE:
  1005. gatt_end_operation(p_clcb, p_clcb->status, NULL);
  1006. break;
  1007. case GATT_HANDLE_VALUE_NOTIF:
  1008. case GATT_HANDLE_VALUE_IND:
  1009. gatt_process_notification(p_tcb, op_code, len, p_data);
  1010. break;
  1011. default:
  1012. GATT_TRACE_ERROR("Unknown opcode = %d", op_code);
  1013. break;
  1014. }
  1015. }
  1016. if (op_code != GATT_HANDLE_VALUE_IND && op_code != GATT_HANDLE_VALUE_NOTIF) {
  1017. gatt_cl_send_next_cmd_inq(p_tcb);
  1018. }
  1019. return;
  1020. }
  1021. #endif /* BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE */