att_protocol.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2008-2014 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 ATT protocol functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #include "osi/allocator.h"
  25. #if BLE_INCLUDED == TRUE
  26. #include "gatt_int.h"
  27. #include "stack/l2c_api.h"
  28. #define GATT_HDR_FIND_TYPE_VALUE_LEN 21
  29. #define GATT_OP_CODE_SIZE 1
  30. /**********************************************************************
  31. ** ATT protocl message building utility *
  32. ***********************************************************************/
  33. /*******************************************************************************
  34. **
  35. ** Function attp_build_mtu_exec_cmd
  36. **
  37. ** Description Build a exchange MTU request
  38. **
  39. ** Returns None.
  40. **
  41. *******************************************************************************/
  42. BT_HDR *attp_build_mtu_cmd(UINT8 op_code, UINT16 rx_mtu)
  43. {
  44. BT_HDR *p_buf = NULL;
  45. UINT8 *p;
  46. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + GATT_HDR_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  47. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  48. UINT8_TO_STREAM (p, op_code);
  49. UINT16_TO_STREAM (p, rx_mtu);
  50. p_buf->offset = L2CAP_MIN_OFFSET;
  51. p_buf->len = GATT_HDR_SIZE; /* opcode + 2 bytes mtu */
  52. }
  53. return p_buf;
  54. }
  55. /*******************************************************************************
  56. **
  57. ** Function attp_build_exec_write_cmd
  58. **
  59. ** Description Build a execute write request or response.
  60. **
  61. ** Returns None.
  62. **
  63. *******************************************************************************/
  64. BT_HDR *attp_build_exec_write_cmd (UINT8 op_code, UINT8 flag)
  65. {
  66. BT_HDR *p_buf = NULL;
  67. UINT8 *p;
  68. if ((p_buf = (BT_HDR *)osi_malloc(GATT_DATA_BUF_SIZE)) != NULL) {
  69. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  70. p_buf->offset = L2CAP_MIN_OFFSET;
  71. p_buf->len = GATT_OP_CODE_SIZE;
  72. UINT8_TO_STREAM (p, op_code);
  73. if (op_code == GATT_REQ_EXEC_WRITE) {
  74. flag &= GATT_PREP_WRITE_EXEC;
  75. UINT8_TO_STREAM (p, flag);
  76. p_buf->len += 1;
  77. }
  78. }
  79. return p_buf;
  80. }
  81. /*******************************************************************************
  82. **
  83. ** Function attp_build_err_cmd
  84. **
  85. ** Description Build a exchange MTU request
  86. **
  87. ** Returns None.
  88. **
  89. *******************************************************************************/
  90. BT_HDR *attp_build_err_cmd(UINT8 cmd_code, UINT16 err_handle, UINT8 reason)
  91. {
  92. BT_HDR *p_buf = NULL;
  93. UINT8 *p;
  94. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + 5)) != NULL) {
  95. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  96. UINT8_TO_STREAM (p, GATT_RSP_ERROR);
  97. UINT8_TO_STREAM (p, cmd_code);
  98. UINT16_TO_STREAM(p, err_handle);
  99. UINT8_TO_STREAM (p, reason);
  100. p_buf->offset = L2CAP_MIN_OFFSET;
  101. /* GATT_HDR_SIZE (1B ERR_RSP op code+ 2B handle) + 1B cmd_op_code + 1B status */
  102. p_buf->len = GATT_HDR_SIZE + 1 + 1;
  103. }
  104. return p_buf;
  105. }
  106. /*******************************************************************************
  107. **
  108. ** Function attp_build_browse_cmd
  109. **
  110. ** Description Build a read information request or read by type request
  111. **
  112. ** Returns None.
  113. **
  114. *******************************************************************************/
  115. BT_HDR *attp_build_browse_cmd(UINT8 op_code, UINT16 s_hdl, UINT16 e_hdl, tBT_UUID uuid)
  116. {
  117. BT_HDR *p_buf = NULL;
  118. UINT8 *p;
  119. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + 8 + L2CAP_MIN_OFFSET)) != NULL) {
  120. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  121. /* Describe the built message location and size */
  122. p_buf->offset = L2CAP_MIN_OFFSET;
  123. p_buf->len = GATT_OP_CODE_SIZE + 4;
  124. UINT8_TO_STREAM (p, op_code);
  125. UINT16_TO_STREAM (p, s_hdl);
  126. UINT16_TO_STREAM (p, e_hdl);
  127. p_buf->len += gatt_build_uuid_to_stream(&p, uuid);
  128. }
  129. return p_buf;
  130. }
  131. /*******************************************************************************
  132. **
  133. ** Function attp_build_read_handles_cmd
  134. **
  135. ** Description Build a read by type and value request.
  136. **
  137. ** Returns pointer to the command buffer.
  138. **
  139. *******************************************************************************/
  140. BT_HDR *attp_build_read_by_type_value_cmd (UINT16 payload_size, tGATT_FIND_TYPE_VALUE *p_value_type)
  141. {
  142. BT_HDR *p_buf = NULL;
  143. UINT8 *p;
  144. UINT16 len = p_value_type->value_len;
  145. if ((p_buf = (BT_HDR *)osi_malloc((UINT16)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET))) != NULL) {
  146. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  147. p_buf->offset = L2CAP_MIN_OFFSET;
  148. p_buf->len = 5; /* opcode + s_handle + e_handle */
  149. UINT8_TO_STREAM (p, GATT_REQ_FIND_TYPE_VALUE);
  150. UINT16_TO_STREAM (p, p_value_type->s_handle);
  151. UINT16_TO_STREAM (p, p_value_type->e_handle);
  152. p_buf->len += gatt_build_uuid_to_stream(&p, p_value_type->uuid);
  153. if (p_value_type->value_len + p_buf->len > payload_size ) {
  154. len = payload_size - p_buf->len;
  155. }
  156. memcpy (p, p_value_type->value, len);
  157. p_buf->len += len;
  158. }
  159. return p_buf;
  160. }
  161. /*******************************************************************************
  162. **
  163. ** Function attp_build_read_multi_cmd
  164. **
  165. ** Description Build a read multiple request
  166. **
  167. ** Returns None.
  168. **
  169. *******************************************************************************/
  170. BT_HDR *attp_build_read_multi_cmd(UINT16 payload_size, UINT16 num_handle, UINT16 *p_handle)
  171. {
  172. BT_HDR *p_buf = NULL;
  173. UINT8 *p, i = 0;
  174. if ((p_buf = (BT_HDR *)osi_malloc((UINT16)(sizeof(BT_HDR) + num_handle * 2 + 1 + L2CAP_MIN_OFFSET))) != NULL) {
  175. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  176. p_buf->offset = L2CAP_MIN_OFFSET;
  177. p_buf->len = 1;
  178. UINT8_TO_STREAM (p, GATT_REQ_READ_MULTI);
  179. for (i = 0; i < num_handle && p_buf->len + 2 <= payload_size; i ++) {
  180. UINT16_TO_STREAM (p, *(p_handle + i));
  181. p_buf->len += 2;
  182. }
  183. }
  184. return p_buf;
  185. }
  186. /*******************************************************************************
  187. **
  188. ** Function attp_build_handle_cmd
  189. **
  190. ** Description Build a read /read blob request
  191. **
  192. ** Returns None.
  193. **
  194. *******************************************************************************/
  195. BT_HDR *attp_build_handle_cmd(UINT8 op_code, UINT16 handle, UINT16 offset)
  196. {
  197. BT_HDR *p_buf = NULL;
  198. UINT8 *p;
  199. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + 5 + L2CAP_MIN_OFFSET)) != NULL) {
  200. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  201. p_buf->offset = L2CAP_MIN_OFFSET;
  202. UINT8_TO_STREAM (p, op_code);
  203. p_buf->len = 1;
  204. UINT16_TO_STREAM (p, handle);
  205. p_buf->len += 2;
  206. if (op_code == GATT_REQ_READ_BLOB) {
  207. UINT16_TO_STREAM (p, offset);
  208. p_buf->len += 2;
  209. }
  210. }
  211. return p_buf;
  212. }
  213. /*******************************************************************************
  214. **
  215. ** Function attp_build_opcode_cmd
  216. **
  217. ** Description Build a request/response with opcode only.
  218. **
  219. ** Returns None.
  220. **
  221. *******************************************************************************/
  222. BT_HDR *attp_build_opcode_cmd(UINT8 op_code)
  223. {
  224. BT_HDR *p_buf = NULL;
  225. UINT8 *p;
  226. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + 1 + L2CAP_MIN_OFFSET)) != NULL) {
  227. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  228. p_buf->offset = L2CAP_MIN_OFFSET;
  229. UINT8_TO_STREAM (p, op_code);
  230. p_buf->len = 1;
  231. }
  232. return p_buf;
  233. }
  234. /*******************************************************************************
  235. **
  236. ** Function attp_build_value_cmd
  237. **
  238. ** Description Build a attribute value request
  239. **
  240. ** Returns None.
  241. **
  242. *******************************************************************************/
  243. BT_HDR *attp_build_value_cmd (UINT16 payload_size, UINT8 op_code, UINT16 handle,
  244. UINT16 offset, UINT16 len, UINT8 *p_data)
  245. {
  246. BT_HDR *p_buf = NULL;
  247. UINT8 *p, *pp, pair_len, *p_pair_len;
  248. if ((p_buf = (BT_HDR *)osi_malloc((UINT16)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET))) != NULL) {
  249. p = pp = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  250. UINT8_TO_STREAM (p, op_code);
  251. p_buf->offset = L2CAP_MIN_OFFSET;
  252. p_buf->len = 1;
  253. if (op_code == GATT_RSP_READ_BY_TYPE) {
  254. p_pair_len = p;
  255. pair_len = len + 2;
  256. UINT8_TO_STREAM (p, pair_len);
  257. p_buf->len += 1;
  258. }
  259. if (op_code != GATT_RSP_READ_BLOB && op_code != GATT_RSP_READ) {
  260. UINT16_TO_STREAM (p, handle);
  261. p_buf->len += 2;
  262. }
  263. if (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_RSP_PREPARE_WRITE ) {
  264. UINT16_TO_STREAM (p, offset);
  265. p_buf->len += 2;
  266. }
  267. if(payload_size < GATT_DEF_BLE_MTU_SIZE || payload_size > GATT_MAX_MTU_SIZE) {
  268. GATT_TRACE_ERROR("invalid payload_size %d", payload_size);
  269. osi_free(p_buf);
  270. return NULL;
  271. }
  272. if (len > 0 && p_data != NULL) {
  273. /* ensure data not exceed MTU size */
  274. if (payload_size - p_buf->len < len) {
  275. len = payload_size - p_buf->len;
  276. /* update handle value pair length */
  277. if (op_code == GATT_RSP_READ_BY_TYPE) {
  278. *p_pair_len = (len + 2);
  279. }
  280. GATT_TRACE_WARNING("attribute value too long, to be truncated to %d", len);
  281. }
  282. ARRAY_TO_STREAM (p, p_data, len);
  283. p_buf->len += len;
  284. }
  285. }
  286. return p_buf;
  287. }
  288. /*******************************************************************************
  289. **
  290. ** Function attp_send_msg_to_l2cap
  291. **
  292. ** Description Send message to L2CAP.
  293. **
  294. *******************************************************************************/
  295. tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB *p_tcb, BT_HDR *p_toL2CAP)
  296. {
  297. UINT16 l2cap_ret;
  298. if (p_tcb->att_lcid == L2CAP_ATT_CID) {
  299. l2cap_ret = L2CA_SendFixedChnlData (L2CAP_ATT_CID, p_tcb->peer_bda, p_toL2CAP);
  300. } else {
  301. #if (CLASSIC_BT_INCLUDED == TRUE)
  302. l2cap_ret = (UINT16) L2CA_DataWrite (p_tcb->att_lcid, p_toL2CAP);
  303. #else
  304. l2cap_ret = L2CAP_DW_FAILED;
  305. #endif ///CLASSIC_BT_INCLUDED == TRUE
  306. }
  307. if (l2cap_ret == L2CAP_DW_FAILED) {
  308. GATT_TRACE_DEBUG("ATT failed to pass msg:0x%0x to L2CAP",
  309. *((UINT8 *)(p_toL2CAP + 1) + p_toL2CAP->offset));
  310. return GATT_INTERNAL_ERROR;
  311. } else if (l2cap_ret == L2CAP_DW_CONGESTED) {
  312. GATT_TRACE_DEBUG("ATT congested, message accepted");
  313. return GATT_CONGESTED;
  314. }
  315. return GATT_SUCCESS;
  316. }
  317. /*******************************************************************************
  318. **
  319. ** Function attp_build_sr_msg
  320. **
  321. ** Description Build ATT Server PDUs.
  322. **
  323. *******************************************************************************/
  324. BT_HDR *attp_build_sr_msg(tGATT_TCB *p_tcb, UINT8 op_code, tGATT_SR_MSG *p_msg)
  325. {
  326. BT_HDR *p_cmd = NULL;
  327. UINT16 offset = 0;
  328. switch (op_code) {
  329. case GATT_RSP_READ_BLOB:
  330. case GATT_RSP_PREPARE_WRITE:
  331. case GATT_RSP_READ_BY_TYPE:
  332. case GATT_RSP_READ:
  333. case GATT_HANDLE_VALUE_NOTIF:
  334. case GATT_HANDLE_VALUE_IND:
  335. case GATT_RSP_ERROR:
  336. case GATT_RSP_MTU:
  337. /* Need to check the validation of parameter p_msg*/
  338. if (p_msg == NULL) {
  339. GATT_TRACE_ERROR("Invalid parameters in %s, op_code=0x%x, the p_msg should not be NULL.", __func__, op_code);
  340. return NULL;
  341. }
  342. break;
  343. default:
  344. break;
  345. }
  346. switch (op_code) {
  347. case GATT_RSP_READ_BLOB:
  348. case GATT_RSP_PREPARE_WRITE:
  349. GATT_TRACE_EVENT ("ATT_RSP_READ_BLOB/GATT_RSP_PREPARE_WRITE: len = %d offset = %d",
  350. p_msg->attr_value.len, p_msg->attr_value.offset);
  351. offset = p_msg->attr_value.offset;
  352. /* Coverity: [FALSE-POSITIVE error] intended fall through */
  353. /* Missing break statement between cases in switch statement */
  354. /* fall through */
  355. case GATT_RSP_READ_BY_TYPE:
  356. case GATT_RSP_READ:
  357. case GATT_HANDLE_VALUE_NOTIF:
  358. case GATT_HANDLE_VALUE_IND:
  359. p_cmd = attp_build_value_cmd(p_tcb->payload_size,
  360. op_code,
  361. p_msg->attr_value.handle,
  362. offset,
  363. p_msg->attr_value.len,
  364. p_msg->attr_value.value);
  365. break;
  366. case GATT_RSP_WRITE:
  367. p_cmd = attp_build_opcode_cmd(op_code);
  368. break;
  369. case GATT_RSP_ERROR:
  370. p_cmd = attp_build_err_cmd(p_msg->error.cmd_code, p_msg->error.handle, p_msg->error.reason);
  371. break;
  372. case GATT_RSP_EXEC_WRITE:
  373. p_cmd = attp_build_exec_write_cmd(op_code, 0);
  374. break;
  375. case GATT_RSP_MTU:
  376. p_cmd = attp_build_mtu_cmd(op_code, p_msg->mtu);
  377. break;
  378. default:
  379. GATT_TRACE_DEBUG("attp_build_sr_msg: unknown op code = %d", op_code);
  380. break;
  381. }
  382. if (!p_cmd) {
  383. GATT_TRACE_ERROR("No resources");
  384. }
  385. return p_cmd;
  386. }
  387. /*******************************************************************************
  388. **
  389. ** Function attp_send_sr_msg
  390. **
  391. ** Description This function sends the server response or indication message
  392. ** to client.
  393. **
  394. ** Parameter p_tcb: pointer to the connecton control block.
  395. ** p_msg: pointer to message parameters structure.
  396. **
  397. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  398. **
  399. **
  400. *******************************************************************************/
  401. tGATT_STATUS attp_send_sr_msg (tGATT_TCB *p_tcb, BT_HDR *p_msg)
  402. {
  403. tGATT_STATUS cmd_sent = GATT_NO_RESOURCES;
  404. if (p_tcb != NULL) {
  405. if (p_msg != NULL) {
  406. p_msg->offset = L2CAP_MIN_OFFSET;
  407. cmd_sent = attp_send_msg_to_l2cap (p_tcb, p_msg);
  408. }
  409. }
  410. return cmd_sent;
  411. }
  412. /*******************************************************************************
  413. **
  414. ** Function attp_cl_send_cmd
  415. **
  416. ** Description Send a ATT command or enqueue it.
  417. **
  418. ** Returns GATT_SUCCESS if command sent
  419. ** GATT_CONGESTED if command sent but channel congested
  420. ** GATT_CMD_STARTED if command queue up in GATT
  421. ** GATT_ERROR if command sending failure
  422. **
  423. *******************************************************************************/
  424. tGATT_STATUS attp_cl_send_cmd(tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 cmd_code, BT_HDR *p_cmd)
  425. {
  426. tGATT_STATUS att_ret = GATT_SUCCESS;
  427. if (p_tcb != NULL) {
  428. cmd_code &= ~GATT_AUTH_SIGN_MASK;
  429. /* no pending request or value confirmation */
  430. if (p_tcb->pending_cl_req == p_tcb->next_slot_inq ||
  431. cmd_code == GATT_HANDLE_VALUE_CONF) {
  432. att_ret = attp_send_msg_to_l2cap(p_tcb, p_cmd);
  433. if (att_ret == GATT_CONGESTED || att_ret == GATT_SUCCESS) {
  434. /* do not enq cmd if handle value confirmation or set request */
  435. if (cmd_code != GATT_HANDLE_VALUE_CONF && cmd_code != GATT_CMD_WRITE) {
  436. gatt_start_rsp_timer (clcb_idx);
  437. gatt_cmd_enq(p_tcb, clcb_idx, FALSE, cmd_code, NULL);
  438. }
  439. } else {
  440. att_ret = GATT_INTERNAL_ERROR;
  441. }
  442. } else {
  443. att_ret = GATT_CMD_STARTED;
  444. gatt_cmd_enq(p_tcb, clcb_idx, TRUE, cmd_code, p_cmd);
  445. }
  446. } else {
  447. att_ret = GATT_ERROR;
  448. }
  449. return att_ret;
  450. }
  451. /*******************************************************************************
  452. **
  453. ** Function attp_send_cl_msg
  454. **
  455. ** Description This function sends the client request or confirmation message
  456. ** to server.
  457. **
  458. ** Parameter p_tcb: pointer to the connection control block.
  459. ** clcb_idx: clcb index
  460. ** op_code: message op code.
  461. ** p_msg: pointer to message parameters structure.
  462. **
  463. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  464. **
  465. **
  466. *******************************************************************************/
  467. tGATT_STATUS attp_send_cl_msg (tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code, tGATT_CL_MSG *p_msg)
  468. {
  469. tGATT_STATUS status = GATT_NO_RESOURCES;
  470. BT_HDR *p_cmd = NULL;
  471. UINT16 offset = 0, handle;
  472. if (p_tcb != NULL) {
  473. switch (op_code) {
  474. case GATT_REQ_MTU:
  475. if (p_msg->mtu <= GATT_MAX_MTU_SIZE) {
  476. p_tcb->payload_size = p_msg->mtu;
  477. p_cmd = attp_build_mtu_cmd(GATT_REQ_MTU, p_msg->mtu);
  478. } else {
  479. status = GATT_ILLEGAL_PARAMETER;
  480. }
  481. break;
  482. case GATT_REQ_FIND_INFO:
  483. case GATT_REQ_READ_BY_TYPE:
  484. case GATT_REQ_READ_BY_GRP_TYPE:
  485. if (GATT_HANDLE_IS_VALID (p_msg->browse.s_handle) &&
  486. GATT_HANDLE_IS_VALID (p_msg->browse.e_handle) &&
  487. p_msg->browse.s_handle <= p_msg->browse.e_handle) {
  488. p_cmd = attp_build_browse_cmd(op_code,
  489. p_msg->browse.s_handle,
  490. p_msg->browse.e_handle,
  491. p_msg->browse.uuid);
  492. } else {
  493. status = GATT_ILLEGAL_PARAMETER;
  494. }
  495. break;
  496. case GATT_REQ_READ_BLOB:
  497. offset = p_msg->read_blob.offset;
  498. /* fall through */
  499. case GATT_REQ_READ:
  500. handle = (op_code == GATT_REQ_READ) ? p_msg->handle : p_msg->read_blob.handle;
  501. /* handle checking */
  502. if (GATT_HANDLE_IS_VALID (handle)) {
  503. p_cmd = attp_build_handle_cmd(op_code, handle, offset);
  504. } else {
  505. status = GATT_ILLEGAL_PARAMETER;
  506. }
  507. break;
  508. case GATT_HANDLE_VALUE_CONF:
  509. p_cmd = attp_build_opcode_cmd(op_code);
  510. break;
  511. case GATT_REQ_PREPARE_WRITE:
  512. offset = p_msg->attr_value.offset;
  513. /* fall through */
  514. case GATT_REQ_WRITE:
  515. case GATT_CMD_WRITE:
  516. case GATT_SIGN_CMD_WRITE:
  517. if (GATT_HANDLE_IS_VALID (p_msg->attr_value.handle)) {
  518. p_cmd = attp_build_value_cmd (p_tcb->payload_size,
  519. op_code, p_msg->attr_value.handle,
  520. offset,
  521. p_msg->attr_value.len,
  522. p_msg->attr_value.value);
  523. } else {
  524. status = GATT_ILLEGAL_PARAMETER;
  525. }
  526. break;
  527. case GATT_REQ_EXEC_WRITE:
  528. p_cmd = attp_build_exec_write_cmd(op_code, p_msg->exec_write);
  529. break;
  530. case GATT_REQ_FIND_TYPE_VALUE:
  531. p_cmd = attp_build_read_by_type_value_cmd(p_tcb->payload_size, &p_msg->find_type_value);
  532. break;
  533. case GATT_REQ_READ_MULTI:
  534. p_cmd = attp_build_read_multi_cmd(p_tcb->payload_size,
  535. p_msg->read_multi.num_handles,
  536. p_msg->read_multi.handles);
  537. break;
  538. default:
  539. break;
  540. }
  541. if (p_cmd != NULL) {
  542. status = attp_cl_send_cmd(p_tcb, clcb_idx, op_code, p_cmd);
  543. }
  544. } else {
  545. GATT_TRACE_ERROR("Peer device not connected");
  546. }
  547. return status;
  548. }
  549. #endif