gatt_sr.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2008-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 GATT server functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #include "osi/allocator.h"
  25. #if BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE
  26. #include <string.h>
  27. #include "gatt_int.h"
  28. #include "stack/l2c_api.h"
  29. #include "l2c_int.h"
  30. #define GATT_MTU_REQ_MIN_LEN 2
  31. /*******************************************************************************
  32. **
  33. ** Function gatt_send_packet
  34. **
  35. ** Description This function is called to send gatt packets directly
  36. **
  37. ** Returns status
  38. **
  39. *******************************************************************************/
  40. tGATT_STATUS gatt_send_packet (tGATT_TCB *p_tcb, UINT8 *p_data, UINT16 len)
  41. {
  42. BT_HDR *p_msg = NULL;
  43. UINT8 *p_m = NULL;
  44. UINT16 buf_len;
  45. tGATT_STATUS status;
  46. if (len > p_tcb->payload_size){
  47. return GATT_ILLEGAL_PARAMETER;
  48. }
  49. buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
  50. if ((p_msg = (BT_HDR *)osi_malloc(buf_len)) == NULL) {
  51. return GATT_NO_RESOURCES;
  52. }
  53. memset(p_msg, 0, buf_len);
  54. p_msg->len = len;
  55. p_m = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
  56. memcpy(p_m, p_data, len);
  57. status = attp_send_sr_msg(p_tcb, p_msg);
  58. return status;
  59. }
  60. /*******************************************************************************
  61. **
  62. ** Function gatt_sr_enqueue_cmd
  63. **
  64. ** Description This function enqueue the request from client which needs a
  65. ** application response, and update the transaction ID.
  66. **
  67. ** Returns void
  68. **
  69. *******************************************************************************/
  70. UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle)
  71. {
  72. tGATT_SR_CMD *p_cmd = &p_tcb->sr_cmd;
  73. UINT32 trans_id = 0;
  74. if ( (p_cmd->op_code == 0) ||
  75. (op_code == GATT_HANDLE_VALUE_CONF)) { /* no pending request */
  76. if (op_code == GATT_CMD_WRITE ||
  77. op_code == GATT_SIGN_CMD_WRITE ||
  78. op_code == GATT_REQ_MTU ||
  79. op_code == GATT_HANDLE_VALUE_CONF) {
  80. trans_id = ++p_tcb->trans_id;
  81. } else {
  82. p_cmd->trans_id = ++p_tcb->trans_id;
  83. p_cmd->op_code = op_code;
  84. p_cmd->handle = handle;
  85. p_cmd->status = GATT_NOT_FOUND;
  86. p_tcb->trans_id %= GATT_TRANS_ID_MAX;
  87. trans_id = p_cmd->trans_id;
  88. }
  89. }
  90. return trans_id;
  91. }
  92. /*******************************************************************************
  93. **
  94. ** Function gatt_sr_cmd_empty
  95. **
  96. ** Description This function check the server command queue is empty or not.
  97. **
  98. ** Returns TRUE if empty, FALSE if there is pending command.
  99. **
  100. *******************************************************************************/
  101. BOOLEAN gatt_sr_cmd_empty (tGATT_TCB *p_tcb)
  102. {
  103. return (p_tcb->sr_cmd.op_code == 0);
  104. }
  105. /*******************************************************************************
  106. **
  107. ** Function gatt_dequeue_sr_cmd
  108. **
  109. ** Description This function dequeue the request from command queue.
  110. **
  111. ** Returns void
  112. **
  113. *******************************************************************************/
  114. void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb)
  115. {
  116. /* Double check in case any buffers are queued */
  117. GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd" );
  118. if (p_tcb->sr_cmd.p_rsp_msg) {
  119. GATT_TRACE_ERROR("%s free msg %p", __func__, p_tcb->sr_cmd.p_rsp_msg);
  120. osi_free(p_tcb->sr_cmd.p_rsp_msg);
  121. p_tcb->sr_cmd.p_rsp_msg = NULL;
  122. }
  123. if (p_tcb->sr_cmd.multi_rsp_q) {
  124. while (!fixed_queue_is_empty(p_tcb->sr_cmd.multi_rsp_q)) {
  125. osi_free(fixed_queue_dequeue(p_tcb->sr_cmd.multi_rsp_q, 0));
  126. }
  127. fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
  128. }
  129. memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
  130. }
  131. /*******************************************************************************
  132. **
  133. ** Function process_read_multi_rsp
  134. **
  135. ** Description This function check the read multiple response.
  136. **
  137. ** Returns BOOLEAN if all replies have been received
  138. **
  139. *******************************************************************************/
  140. static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
  141. tGATTS_RSP *p_msg, UINT16 mtu)
  142. {
  143. UINT16 ii, total_len, len;
  144. UINT8 *p;
  145. BOOLEAN is_overflow = FALSE;
  146. GATT_TRACE_DEBUG ("process_read_multi_rsp status=%d mtu=%d", status, mtu);
  147. if (p_cmd->multi_rsp_q == NULL) {
  148. p_cmd->multi_rsp_q = fixed_queue_new(QUEUE_SIZE_MAX);
  149. }
  150. /* Enqueue the response */
  151. BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(tGATTS_RSP));
  152. if (p_buf == NULL) {
  153. p_cmd->status = GATT_INSUF_RESOURCE;
  154. return FALSE;
  155. }
  156. memcpy((void *)p_buf, (const void *)p_msg, sizeof(tGATTS_RSP));
  157. fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  158. p_cmd->status = status;
  159. if (status == GATT_SUCCESS) {
  160. GATT_TRACE_DEBUG ("Multi read count=%d num_hdls=%d",
  161. fixed_queue_length(p_cmd->multi_rsp_q),
  162. p_cmd->multi_req.num_handles);
  163. /* Wait till we get all the responses */
  164. if (fixed_queue_length(p_cmd->multi_rsp_q) == p_cmd->multi_req.num_handles) {
  165. len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
  166. if ((p_buf = (BT_HDR *)osi_calloc(len)) == NULL) {
  167. p_cmd->status = GATT_INSUF_RESOURCE;
  168. return (TRUE);
  169. }
  170. p_buf->offset = L2CAP_MIN_OFFSET;
  171. p = (UINT8 *)(p_buf + 1) + p_buf->offset;
  172. /* First byte in the response is the opcode */
  173. *p++ = GATT_RSP_READ_MULTI;
  174. p_buf->len = 1;
  175. /* Now walk through the buffers puting the data into the response in order */
  176. list_t *list = NULL;
  177. const list_node_t *node = NULL;
  178. if (! fixed_queue_is_empty(p_cmd->multi_rsp_q)) {
  179. list = fixed_queue_get_list(p_cmd->multi_rsp_q);
  180. }
  181. for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
  182. tGATTS_RSP *p_rsp = NULL;
  183. if (list != NULL) {
  184. if (ii == 0) {
  185. node = list_begin(list);
  186. } else {
  187. node = list_next(node);
  188. }
  189. if (node != list_end(list)) {
  190. p_rsp = (tGATTS_RSP *)list_node(node);
  191. }
  192. }
  193. if (p_rsp != NULL) {
  194. total_len = (p_buf->len + p_rsp->attr_value.len);
  195. if (total_len > mtu) {
  196. /* just send the partial response for the overflow case */
  197. len = p_rsp->attr_value.len - (total_len - mtu);
  198. is_overflow = TRUE;
  199. GATT_TRACE_DEBUG ("multi read overflow available len=%d val_len=%d", len, p_rsp->attr_value.len );
  200. } else {
  201. len = p_rsp->attr_value.len;
  202. }
  203. if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
  204. memcpy (p, p_rsp->attr_value.value, len);
  205. if (!is_overflow) {
  206. p += len;
  207. }
  208. p_buf->len += len;
  209. } else {
  210. p_cmd->status = GATT_NOT_FOUND;
  211. break;
  212. }
  213. if (is_overflow) {
  214. break;
  215. }
  216. } else {
  217. p_cmd->status = GATT_NOT_FOUND;
  218. break;
  219. }
  220. } /* loop through all handles*/
  221. /* Sanity check on the buffer length */
  222. if (p_buf->len == 0) {
  223. GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
  224. p_cmd->status = GATT_NOT_FOUND;
  225. osi_free (p_buf);
  226. GATT_TRACE_DEBUG(" osi_free (p_buf)");
  227. } else if (p_cmd->p_rsp_msg != NULL) {
  228. osi_free (p_buf);
  229. } else {
  230. p_cmd->p_rsp_msg = p_buf;
  231. }
  232. return (TRUE);
  233. }
  234. } else { /* any handle read exception occurs, return error */
  235. return (TRUE);
  236. }
  237. /* If here, still waiting */
  238. return (FALSE);
  239. }
  240. /*******************************************************************************
  241. **
  242. ** Function gatt_sr_process_app_rsp
  243. **
  244. ** Description This function checks whether the response message from application
  245. ** match any pending request or not.
  246. **
  247. ** Returns void
  248. **
  249. *******************************************************************************/
  250. tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
  251. UINT32 trans_id, UINT8 op_code,
  252. tGATT_STATUS status, tGATTS_RSP *p_msg)
  253. {
  254. tGATT_STATUS ret_code = GATT_SUCCESS;
  255. UNUSED(trans_id);
  256. GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d\n", gatt_if);
  257. gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
  258. if (op_code == GATT_REQ_READ_MULTI) {
  259. /* If no error and still waiting, just return */
  260. if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size)) {
  261. return (GATT_SUCCESS);
  262. }
  263. } else {
  264. if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS) {
  265. gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
  266. }
  267. if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS) {
  268. gatt_sr_reset_cback_cnt(p_tcb);
  269. }
  270. p_tcb->sr_cmd.status = status;
  271. if (gatt_sr_is_cback_cnt_zero(p_tcb)
  272. && status == GATT_SUCCESS) {
  273. if (p_tcb->sr_cmd.p_rsp_msg == NULL) {
  274. p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
  275. } else {
  276. GATT_TRACE_ERROR("Exception!!! already has respond message\n");
  277. }
  278. }
  279. }
  280. if (gatt_sr_is_cback_cnt_zero(p_tcb)) {
  281. if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) ) {
  282. ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
  283. p_tcb->sr_cmd.p_rsp_msg = NULL;
  284. } else {
  285. if (p_tcb->sr_cmd.status == GATT_SUCCESS){
  286. status = GATT_UNKNOWN_ERROR;
  287. }
  288. ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
  289. }
  290. gatt_dequeue_sr_cmd(p_tcb);
  291. }
  292. GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d\n", ret_code);
  293. return ret_code;
  294. }
  295. /*******************************************************************************
  296. **
  297. ** Function gatt_process_exec_write_req
  298. **
  299. ** Description This function is called to process the execute write request
  300. ** from client.
  301. **
  302. ** Returns void
  303. **
  304. *******************************************************************************/
  305. void gatt_process_exec_write_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
  306. {
  307. UINT8 *p = p_data, flag, i = 0;
  308. UINT32 trans_id = 0;
  309. tGATT_IF gatt_if;
  310. UINT16 conn_id;
  311. UINT16 queue_num = 0;
  312. BOOLEAN is_first = TRUE;
  313. BOOLEAN is_prepare_write_valid = FALSE;
  314. BOOLEAN is_need_dequeue_sr_cmd = FALSE;
  315. tGATT_PREPARE_WRITE_RECORD *prepare_record = NULL;
  316. tGATT_PREPARE_WRITE_QUEUE_DATA * queue_data = NULL;
  317. UNUSED(len);
  318. #if GATT_CONFORMANCE_TESTING == TRUE
  319. if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
  320. GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
  321. gatt_cb.err_status);
  322. gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, gatt_cb.handle, FALSE);
  323. return;
  324. }
  325. #endif
  326. STREAM_TO_UINT8(flag, p);
  327. /* mask the flag */
  328. flag &= GATT_PREP_WRITE_EXEC;
  329. prepare_record = &(p_tcb->prepare_write_record);
  330. queue_num = fixed_queue_length(prepare_record->queue);
  331. //if received prepare_write packets include stack_rsp and app_rsp,
  332. //stack respond to execute_write only when stack_rsp handle has invalid_offset
  333. //or invalid_length error;
  334. //app need to respond to execute_write if it has received app_rsp handle packets
  335. if (((prepare_record->error_code_app == GATT_SUCCESS) &&
  336. (prepare_record->total_num == queue_num))
  337. || (flag == GATT_PREP_WRITE_CANCEL)){
  338. tGATT_EXEC_WRITE_RSP gatt_exec_write_rsp;
  339. gatt_exec_write_rsp.op_code = GATT_RSP_EXEC_WRITE;
  340. gatt_send_packet(p_tcb, (UINT8 *)(&gatt_exec_write_rsp), sizeof(gatt_exec_write_rsp));
  341. gatt_dequeue_sr_cmd(p_tcb);
  342. if (flag != GATT_PREP_WRITE_CANCEL){
  343. is_prepare_write_valid = TRUE;
  344. }
  345. GATT_TRACE_DEBUG("Send execute_write_rsp\n");
  346. } else if ((prepare_record->error_code_app == GATT_SUCCESS) &&
  347. (prepare_record->total_num > queue_num)){
  348. //No error for stack_rsp's handles and there exist some app_rsp's handles,
  349. //so exec_write_rsp depends to app's response; but stack_rsp's data is valid
  350. //TODO: there exist problem if stack_rsp's data is valid but app_rsp's data is not valid.
  351. is_prepare_write_valid = TRUE;
  352. } else if(prepare_record->total_num < queue_num) {
  353. GATT_TRACE_ERROR("Error in %s, line=%d, prepare write total number (%d) \
  354. should not smaller than prepare queue number (%d)\n", \
  355. __func__, __LINE__,prepare_record->total_num, queue_num);
  356. } else if (prepare_record->error_code_app != GATT_SUCCESS){
  357. GATT_TRACE_DEBUG("Send error code for execute_write, code=0x%x\n", prepare_record->error_code_app);
  358. is_need_dequeue_sr_cmd = (prepare_record->total_num == queue_num) ? TRUE : FALSE;
  359. gatt_send_error_rsp(p_tcb, prepare_record->error_code_app, GATT_REQ_EXEC_WRITE, 0, is_need_dequeue_sr_cmd);
  360. }
  361. //dequeue prepare write data
  362. while(fixed_queue_try_peek_first(prepare_record->queue)) {
  363. queue_data = fixed_queue_dequeue(prepare_record->queue, FIXED_QUEUE_MAX_TIMEOUT);
  364. if (is_prepare_write_valid){
  365. if((queue_data->p_attr->p_value != NULL) && (queue_data->p_attr->p_value->attr_val.attr_val != NULL)){
  366. if(is_first) {
  367. //clear attr_val.attr_len before handle prepare write data
  368. queue_data->p_attr->p_value->attr_val.attr_len = 0;
  369. is_first = FALSE;
  370. }
  371. memcpy(queue_data->p_attr->p_value->attr_val.attr_val+queue_data->offset, queue_data->value, queue_data->len);
  372. //don't forget to increase the attribute value length in the gatts database.
  373. queue_data->p_attr->p_value->attr_val.attr_len += queue_data->len;
  374. }
  375. }
  376. osi_free(queue_data);
  377. }
  378. fixed_queue_free(prepare_record->queue, NULL);
  379. prepare_record->queue = NULL;
  380. /* according to ble spec, even if there is no prep write queued,
  381. * need to respond execute_write_response
  382. * Note: exec_write_rsp callback should be called after all data has been written*/
  383. if (!gatt_sr_is_prep_cnt_zero(p_tcb)) {
  384. if (prepare_record->total_num > queue_num){
  385. trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
  386. gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
  387. }
  388. for (i = 0; i < GATT_MAX_APPS; i++) {
  389. if (p_tcb->prep_cnt[i]) {
  390. gatt_if = (tGATT_IF) (i + 1);
  391. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
  392. gatt_sr_send_req_callback(conn_id,
  393. trans_id,
  394. GATTS_REQ_TYPE_WRITE_EXEC,
  395. (tGATTS_DATA *)&flag);
  396. p_tcb->prep_cnt[i] = 0;
  397. }
  398. }
  399. }
  400. prepare_record->total_num = 0;
  401. prepare_record->error_code_app = GATT_SUCCESS;
  402. }
  403. /*******************************************************************************
  404. **
  405. ** Function gatt_process_read_multi_req
  406. **
  407. ** Description This function is called to process the read multiple request
  408. ** from client.
  409. **
  410. ** Returns void
  411. **
  412. *******************************************************************************/
  413. void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
  414. {
  415. UINT32 trans_id;
  416. UINT16 handle = 0, ll = len;
  417. UINT8 *p = p_data, i_rcb;
  418. tGATT_STATUS err = GATT_SUCCESS;
  419. UINT8 sec_flag, key_size;
  420. tGATTS_RSP *p_msg;
  421. GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
  422. p_tcb->sr_cmd.multi_req.num_handles = 0;
  423. gatt_sr_get_sec_info(p_tcb->peer_bda,
  424. p_tcb->transport,
  425. &sec_flag,
  426. &key_size);
  427. #if GATT_CONFORMANCE_TESTING == TRUE
  428. if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
  429. GATT_TRACE_DEBUG("Conformance tst: forced err rspvofr ReadMultiple: error status=%d\n", gatt_cb.err_status);
  430. STREAM_TO_UINT16(handle, p);
  431. gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
  432. return;
  433. }
  434. #endif
  435. while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
  436. STREAM_TO_UINT16(handle, p);
  437. if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES) {
  438. p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
  439. /* check read permission */
  440. if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
  441. FALSE,
  442. handle,
  443. sec_flag,
  444. key_size))
  445. != GATT_SUCCESS) {
  446. GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
  447. break;
  448. }
  449. } else {
  450. /* invalid handle */
  451. err = GATT_INVALID_HANDLE;
  452. break;
  453. }
  454. ll -= 2;
  455. }
  456. if (ll != 0) {
  457. GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
  458. err = GATT_INVALID_HANDLE;
  459. }
  460. if (p_tcb->sr_cmd.multi_req.num_handles == 0) {
  461. err = GATT_INVALID_HANDLE;
  462. }
  463. if (err == GATT_SUCCESS) {
  464. if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0) {
  465. gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/
  466. for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++) {
  467. if ((p_msg = (tGATTS_RSP *)osi_malloc(sizeof(tGATTS_RSP))) != NULL) {
  468. memset(p_msg, 0, sizeof(tGATTS_RSP))
  469. ;
  470. handle = p_tcb->sr_cmd.multi_req.handles[ll];
  471. i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
  472. p_msg->attr_value.handle = handle;
  473. err = gatts_read_attr_value_by_handle(p_tcb,
  474. gatt_cb.sr_reg[i_rcb].p_db,
  475. op_code,
  476. handle,
  477. 0,
  478. p_msg->attr_value.value,
  479. &p_msg->attr_value.len,
  480. GATT_MAX_ATTR_LEN,
  481. sec_flag,
  482. key_size,
  483. trans_id);
  484. if (err == GATT_SUCCESS || err == GATT_STACK_RSP) {
  485. gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if , trans_id, op_code, GATT_SUCCESS, p_msg);
  486. }
  487. /* either not using or done using the buffer, release it now */
  488. osi_free(p_msg);
  489. } else {
  490. err = GATT_NO_RESOURCES;
  491. gatt_dequeue_sr_cmd(p_tcb);
  492. break;
  493. }
  494. }
  495. } else {
  496. err = GATT_NO_RESOURCES;
  497. }
  498. }
  499. /* in theroy BUSY is not possible(should already been checked), protected check */
  500. if (err != GATT_SUCCESS && err != GATT_STACK_RSP && err != GATT_PENDING && err != GATT_BUSY) {
  501. gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
  502. }
  503. }
  504. /*******************************************************************************
  505. **
  506. ** Function gatt_build_primary_service_rsp
  507. **
  508. ** Description Primamry service request processed internally. Theretically
  509. ** only deal with ReadByTypeVAlue and ReadByGroupType.
  510. **
  511. ** Returns void
  512. **
  513. *******************************************************************************/
  514. static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
  515. UINT8 op_code, UINT16 s_hdl,
  516. UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
  517. {
  518. tGATT_STATUS status = GATT_NOT_FOUND;
  519. UINT8 handle_len = 4, *p ;
  520. tGATT_SR_REG *p_rcb;
  521. tGATT_SRV_LIST_INFO *p_list = &gatt_cb.srv_list_info;
  522. tGATT_SRV_LIST_ELEM *p_srv = NULL;
  523. tBT_UUID *p_uuid;
  524. UNUSED(p_data);
  525. p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
  526. p_srv = p_list->p_first;
  527. while (p_srv) {
  528. p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
  529. if (p_rcb->in_use &&
  530. p_rcb->s_hdl >= s_hdl &&
  531. p_rcb->s_hdl <= e_hdl &&
  532. p_rcb->type == GATT_UUID_PRI_SERVICE) {
  533. if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL) {
  534. if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
  535. handle_len = 4 + p_uuid->len;
  536. }
  537. /* get the length byte in the repsonse */
  538. if (p_msg->offset == 0) {
  539. *p ++ = op_code + 1;
  540. p_msg->len ++;
  541. p_msg->offset = handle_len;
  542. if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
  543. *p ++ = (UINT8)p_msg->offset; /* length byte */
  544. p_msg->len ++;
  545. }
  546. }
  547. if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
  548. handle_len == p_msg->offset) {
  549. if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
  550. gatt_uuid_compare(value, *p_uuid)) {
  551. UINT16_TO_STREAM(p, p_rcb->s_hdl);
  552. if (p_list->p_last_primary == p_srv &&
  553. p_list->p_last_primary == p_list->p_last) {
  554. GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
  555. UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
  556. } else {
  557. UINT16_TO_STREAM(p, p_rcb->e_hdl);
  558. }
  559. if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
  560. gatt_build_uuid_to_stream(&p, *p_uuid);
  561. }
  562. status = GATT_SUCCESS;
  563. p_msg->len += p_msg->offset;
  564. }
  565. } else {
  566. break;
  567. }
  568. }
  569. }
  570. p_srv = p_srv->p_next;
  571. }
  572. p_msg->offset = L2CAP_MIN_OFFSET;
  573. return status;
  574. }
  575. /*******************************************************************************
  576. **
  577. ** Function gatt_build_find_info_rsp
  578. **
  579. ** Description fill the find information response information in the given
  580. ** buffer.
  581. **
  582. ** Returns TRUE: if data filled successfully.
  583. ** FALSE: packet full, or format mismatch.
  584. **
  585. *******************************************************************************/
  586. static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
  587. UINT16 s_hdl, UINT16 e_hdl)
  588. {
  589. tGATT_STATUS status = GATT_NOT_FOUND;
  590. UINT8 *p;
  591. UINT16 len = *p_len;
  592. tGATT_ATTR16 *p_attr = NULL;
  593. UINT8 info_pair_len[2] = {4, 18};
  594. if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list) {
  595. return status;
  596. }
  597. /* check the attribute database */
  598. p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
  599. p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
  600. while (p_attr) {
  601. if (p_attr->handle > e_hdl) {
  602. break;
  603. }
  604. if (p_attr->handle >= s_hdl) {
  605. if (p_msg->offset == 0) {
  606. p_msg->offset = (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) ? GATT_INFO_TYPE_PAIR_16 : GATT_INFO_TYPE_PAIR_128;
  607. }
  608. if (len >= info_pair_len[p_msg->offset - 1]) {
  609. if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  610. UINT16_TO_STREAM(p, p_attr->handle);
  611. UINT16_TO_STREAM(p, p_attr->uuid);
  612. } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 ) {
  613. UINT16_TO_STREAM(p, p_attr->handle);
  614. ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
  615. } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32) {
  616. UINT16_TO_STREAM(p, p_attr->handle);
  617. gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
  618. p += LEN_UUID_128;
  619. } else {
  620. GATT_TRACE_ERROR("format mismatch");
  621. status = GATT_NO_RESOURCES;
  622. break;
  623. /* format mismatch */
  624. }
  625. p_msg->len += info_pair_len[p_msg->offset - 1];
  626. len -= info_pair_len[p_msg->offset - 1];
  627. status = GATT_SUCCESS;
  628. } else {
  629. status = GATT_NO_RESOURCES;
  630. break;
  631. }
  632. }
  633. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  634. }
  635. *p_len = len;
  636. return status;
  637. }
  638. /*******************************************************************************
  639. **
  640. ** Function gatts_internal_read_by_type_req
  641. **
  642. ** Description check to see if the ReadByType request can be handled internally.
  643. **
  644. ** Returns void
  645. **
  646. *******************************************************************************/
  647. static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
  648. UINT8 **p_data, tBT_UUID *p_uuid_filter,
  649. UINT16 *p_s_hdl, UINT16 *p_e_hdl)
  650. {
  651. tGATT_STATUS reason = GATT_SUCCESS;
  652. UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
  653. UINT16 len = *p_len;
  654. UINT8 *p = *p_data;
  655. if (len >= 4) {
  656. /* obtain starting handle, and ending handle */
  657. STREAM_TO_UINT16(s_hdl, p);
  658. STREAM_TO_UINT16(e_hdl, p);
  659. len -= 4;
  660. if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl)) {
  661. reason = GATT_INVALID_HANDLE;
  662. }
  663. /* for these PDUs, uuid filter must present */
  664. else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
  665. op_code == GATT_REQ_FIND_TYPE_VALUE ||
  666. op_code == GATT_REQ_READ_BY_TYPE) {
  667. if (len >= 2 && p_uuid_filter != NULL) {
  668. uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
  669. /* parse uuid now */
  670. if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
  671. p_uuid_filter->len == 0) {
  672. GATT_TRACE_DEBUG("UUID filter does not exsit");
  673. reason = GATT_INVALID_PDU;
  674. } else {
  675. len -= p_uuid_filter->len;
  676. }
  677. } else {
  678. reason = GATT_INVALID_PDU;
  679. }
  680. }
  681. } else {
  682. reason = GATT_INVALID_PDU;
  683. }
  684. *p_data = p;
  685. *p_len = len;
  686. *p_s_hdl = s_hdl;
  687. *p_e_hdl = e_hdl;
  688. return reason;
  689. }
  690. /*******************************************************************************
  691. **
  692. ** Function gatts_process_primary_service_req
  693. **
  694. ** Description process ReadByGroupType/ReadByTypeValue request, for discover
  695. ** all primary services or discover primary service by UUID request.
  696. **
  697. ** Returns void
  698. **
  699. *******************************************************************************/
  700. void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
  701. {
  702. UINT8 reason = GATT_INVALID_PDU;
  703. UINT16 s_hdl = 0, e_hdl = 0;
  704. tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
  705. BT_HDR *p_msg = NULL;
  706. UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
  707. memset (&value, 0, sizeof(tBT_UUID));
  708. reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
  709. if (reason == GATT_SUCCESS) {
  710. if (gatt_uuid_compare(uuid, primary_service)) {
  711. if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
  712. if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE) {
  713. reason = GATT_INVALID_PDU;
  714. }
  715. }
  716. if (reason == GATT_SUCCESS) {
  717. if ((p_msg = (BT_HDR *)osi_calloc(msg_len)) == NULL) {
  718. GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
  719. reason = GATT_NO_RESOURCES;
  720. } else {
  721. reason = gatt_build_primary_service_rsp (p_msg, p_tcb, op_code, s_hdl, e_hdl, p_data, value);
  722. }
  723. }
  724. } else {
  725. if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
  726. reason = GATT_UNSUPPORT_GRP_TYPE;
  727. GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
  728. } else {
  729. /* we do not support ReadByTypeValue with any non-primamry_service type */
  730. reason = GATT_NOT_FOUND;
  731. GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
  732. }
  733. }
  734. }
  735. if (reason != GATT_SUCCESS) {
  736. if (p_msg) {
  737. osi_free(p_msg);
  738. }
  739. gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
  740. } else {
  741. attp_send_sr_msg(p_tcb, p_msg);
  742. }
  743. }
  744. /*******************************************************************************
  745. **
  746. ** Function gatts_process_find_info
  747. **
  748. ** Description process find information request, for discover character
  749. ** descriptors.
  750. **
  751. ** Returns void
  752. **
  753. *******************************************************************************/
  754. static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
  755. {
  756. UINT8 reason = GATT_INVALID_PDU, *p;
  757. UINT16 s_hdl = 0, e_hdl = 0, buf_len;
  758. BT_HDR *p_msg = NULL;
  759. tGATT_SR_REG *p_rcb;
  760. tGATT_SRV_LIST_INFO *p_list = &gatt_cb.srv_list_info;
  761. tGATT_SRV_LIST_ELEM *p_srv = NULL;
  762. reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
  763. if (reason == GATT_SUCCESS) {
  764. buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
  765. if ((p_msg = (BT_HDR *)osi_calloc(buf_len)) == NULL) {
  766. reason = GATT_NO_RESOURCES;
  767. } else {
  768. reason = GATT_NOT_FOUND;
  769. p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
  770. *p ++ = op_code + 1;
  771. p_msg->len = 2;
  772. buf_len = p_tcb->payload_size - 2;
  773. p_srv = p_list->p_first;
  774. while (p_srv) {
  775. p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
  776. if (p_rcb->in_use &&
  777. !(p_rcb->s_hdl > e_hdl ||
  778. p_rcb->e_hdl < s_hdl)) {
  779. reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
  780. if (reason == GATT_NO_RESOURCES) {
  781. reason = GATT_SUCCESS;
  782. break;
  783. }
  784. }
  785. p_srv = p_srv->p_next;
  786. }
  787. *p = (UINT8)p_msg->offset;
  788. p_msg->offset = L2CAP_MIN_OFFSET;
  789. }
  790. }
  791. if (reason != GATT_SUCCESS) {
  792. if (p_msg) {
  793. osi_free(p_msg);
  794. }
  795. gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
  796. } else {
  797. attp_send_sr_msg(p_tcb, p_msg);
  798. }
  799. }
  800. /*******************************************************************************
  801. **
  802. ** Function gatts_process_mtu_req
  803. **
  804. ** Description This function is called to process excahnge MTU request.
  805. ** Only used on LE.
  806. **
  807. ** Returns void
  808. **
  809. *******************************************************************************/
  810. static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
  811. {
  812. UINT16 mtu = 0;
  813. UINT8 *p = p_data, i;
  814. BT_HDR *p_buf;
  815. UINT16 conn_id;
  816. /* BR/EDR conenction, send error response */
  817. if (p_tcb->att_lcid != L2CAP_ATT_CID) {
  818. gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
  819. } else if (len < GATT_MTU_REQ_MIN_LEN) {
  820. GATT_TRACE_ERROR("invalid MTU request PDU received.\n");
  821. gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
  822. } else {
  823. STREAM_TO_UINT16 (mtu, p);
  824. /* mtu must be greater than default MTU which is 23/48 */
  825. if (mtu < GATT_DEF_BLE_MTU_SIZE) {
  826. p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
  827. } else if (mtu > gatt_default.local_mtu) {
  828. p_tcb->payload_size = gatt_default.local_mtu;
  829. } else {
  830. p_tcb->payload_size = mtu;
  831. }
  832. /* host will set packet data length to 251 automatically if remote device support set packet data length,
  833. so l2cble_set_fixed_channel_tx_data_length() is not necessary.
  834. l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size);
  835. */
  836. if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL) {
  837. attp_send_sr_msg (p_tcb, p_buf);
  838. /* Notify all registered application with new MTU size. Us a transaction ID */
  839. /* of 0, as no response is allowed from applcations */
  840. for (i = 0; i < GATT_MAX_APPS; i ++) {
  841. if (gatt_cb.cl_rcb[i].in_use ) {
  842. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
  843. gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
  844. (tGATTS_DATA *)&p_tcb->payload_size);
  845. }
  846. }
  847. }
  848. }
  849. }
  850. /*******************************************************************************
  851. **
  852. ** Function gatts_process_read_by_type_req
  853. **
  854. ** Description process Read By type request.
  855. ** This PDU can be used to perform:
  856. ** - read characteristic value
  857. ** - read characteristic descriptor value
  858. ** - discover characteristic
  859. ** - discover characteristic by UUID
  860. ** - relationship discovery
  861. **
  862. ** Returns void
  863. **
  864. *******************************************************************************/
  865. void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
  866. {
  867. tBT_UUID uuid;
  868. tGATT_SR_REG *p_rcb;
  869. UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
  870. buf_len,
  871. s_hdl, e_hdl, err_hdl = 0;
  872. BT_HDR *p_msg = NULL;
  873. tGATT_STATUS reason, ret;
  874. UINT8 *p;
  875. UINT8 sec_flag, key_size;
  876. tGATT_SRV_LIST_INFO *p_list = &gatt_cb.srv_list_info;
  877. tGATT_SRV_LIST_ELEM *p_srv = NULL;
  878. reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
  879. GATT_TRACE_DEBUG("%s, op_code =%x, len = %x\n", __func__, op_code, len);
  880. #if GATT_CONFORMANCE_TESTING == TRUE
  881. if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
  882. GATT_TRACE_DEBUG("Conformance tst: forced err rsp for ReadByType: error status=%d\n", gatt_cb.err_status);
  883. gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
  884. return;
  885. }
  886. #endif
  887. if (reason == GATT_SUCCESS) {
  888. if ((p_msg = (BT_HDR *)osi_calloc(msg_len)) == NULL) {
  889. GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.\n");
  890. reason = GATT_NO_RESOURCES;
  891. } else {
  892. p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
  893. *p ++ = op_code + 1;
  894. /* reserve length byte */
  895. p_msg->len = 2;
  896. buf_len = p_tcb->payload_size - 2;
  897. reason = GATT_NOT_FOUND;
  898. p_srv = p_list->p_first;
  899. while (p_srv) {
  900. p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
  901. if (p_rcb->in_use &&
  902. !(p_rcb->s_hdl > e_hdl ||
  903. p_rcb->e_hdl < s_hdl)) {
  904. gatt_sr_get_sec_info(p_tcb->peer_bda,
  905. p_tcb->transport,
  906. &sec_flag,
  907. &key_size);
  908. ret = gatts_db_read_attr_value_by_type(p_tcb,
  909. p_rcb->p_db,
  910. op_code,
  911. p_msg,
  912. s_hdl,
  913. e_hdl,
  914. uuid,
  915. &buf_len,
  916. sec_flag,
  917. key_size,
  918. 0,
  919. &err_hdl);
  920. if (ret != GATT_NOT_FOUND) {
  921. reason = ret;
  922. if (ret == GATT_NO_RESOURCES) {
  923. reason = GATT_SUCCESS;
  924. }
  925. }
  926. if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
  927. s_hdl = err_hdl;
  928. break;
  929. }
  930. }
  931. p_srv = p_srv->p_next;
  932. }
  933. *p = (UINT8)p_msg->offset;
  934. p_msg->offset = L2CAP_MIN_OFFSET;
  935. }
  936. }
  937. if (reason != GATT_SUCCESS && reason != GATT_STACK_RSP) {
  938. if (p_msg) {
  939. osi_free(p_msg);
  940. }
  941. /* in theroy BUSY is not possible(should already been checked), protected check */
  942. if (reason != GATT_PENDING && reason != GATT_BUSY) {
  943. gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
  944. }
  945. } else {
  946. attp_send_sr_msg(p_tcb, p_msg);
  947. gatt_dequeue_sr_cmd(p_tcb);
  948. }
  949. }
  950. /*******************************************************************************
  951. **
  952. ** Function gatts_process_write_req
  953. **
  954. ** Description This function is called to process the write request
  955. ** from client.
  956. **
  957. ** Returns void
  958. **
  959. *******************************************************************************/
  960. void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
  961. UINT8 op_code, UINT16 len, UINT8 *p_data)
  962. {
  963. tGATTS_DATA sr_data;
  964. UINT32 trans_id;
  965. tGATT_STATUS status;
  966. UINT8 sec_flag, key_size, *p = p_data;
  967. tGATT_SR_REG *p_sreg;
  968. UINT16 conn_id, offset = 0;
  969. memset(&sr_data, 0, sizeof(tGATTS_DATA));
  970. sr_data.write_req.need_rsp = FALSE;
  971. switch (op_code) {
  972. case GATT_SIGN_CMD_WRITE:
  973. if (op_code == GATT_SIGN_CMD_WRITE) {
  974. GATT_TRACE_DEBUG("Write CMD with data signing" );
  975. len -= GATT_AUTH_SIGN_LEN;
  976. }
  977. /* fall through */
  978. case GATT_CMD_WRITE:
  979. case GATT_REQ_WRITE:
  980. sr_data.write_req.handle = handle;
  981. sr_data.write_req.len = len;
  982. if (len != 0 && p != NULL) {
  983. memcpy (sr_data.write_req.value, p, len);
  984. }
  985. break;
  986. }
  987. gatt_sr_get_sec_info(p_tcb->peer_bda,
  988. p_tcb->transport,
  989. &sec_flag,
  990. &key_size);
  991. status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
  992. op_code,
  993. handle,
  994. sr_data.write_req.offset,
  995. p,
  996. len,
  997. sec_flag,
  998. key_size);
  999. if (status == GATT_SUCCESS) {
  1000. if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0) {
  1001. p_sreg = &gatt_cb.sr_reg[i_rcb];
  1002. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
  1003. status = gatts_write_attr_value_by_handle(gatt_cb.sr_reg[i_rcb].p_db,
  1004. handle, offset, p, len);
  1005. if((op_code == GATT_REQ_WRITE) && (status == GATT_APP_RSP)){
  1006. sr_data.write_req.need_rsp = TRUE;
  1007. status = GATT_PENDING;
  1008. }
  1009. gatt_sr_send_req_callback(conn_id,
  1010. trans_id,
  1011. GATTS_REQ_TYPE_WRITE,
  1012. &sr_data);
  1013. } else {
  1014. GATT_TRACE_ERROR("Error in %s, line=%d, max pending command, send error\n", __func__, __LINE__);
  1015. status = GATT_BUSY; /* max pending command, application error */
  1016. }
  1017. }
  1018. /* response should be sent only for write_request */
  1019. if ((op_code == GATT_REQ_WRITE) && (sr_data.write_req.need_rsp == FALSE)){
  1020. if (status == GATT_SUCCESS){
  1021. tGATT_WRITE_REQ_RSP gatt_write_req_rsp;
  1022. gatt_write_req_rsp.op_code = GATT_RSP_WRITE;
  1023. gatt_send_packet(p_tcb, (UINT8 *)(&gatt_write_req_rsp), sizeof(gatt_write_req_rsp));
  1024. gatt_dequeue_sr_cmd(p_tcb);
  1025. } else if (status != GATT_PENDING){
  1026. /* note: in case of GATT_BUSY, will respond this application error to remote device */
  1027. gatt_send_error_rsp (p_tcb, status, op_code, handle, TRUE);
  1028. }
  1029. }
  1030. return;
  1031. }
  1032. /*******************************************************************************
  1033. **
  1034. ** Function gatts_attr_process_preapre_write
  1035. **
  1036. ** Description This function is called to process the prepare write request
  1037. ** from client.
  1038. **
  1039. ** Returns void
  1040. **
  1041. *******************************************************************************/
  1042. void gatt_attr_process_prepare_write (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
  1043. UINT8 op_code, UINT16 len, UINT8 *p_data)
  1044. {
  1045. tGATT_STATUS status;
  1046. tGATT_PREPARE_WRITE_QUEUE_DATA * queue_data = NULL;
  1047. tGATT_ATTR16 *p_attr;
  1048. tGATT_ATTR16 *p_attr_temp;
  1049. tGATTS_DATA sr_data;
  1050. UINT32 trans_id = 0;
  1051. UINT8 sec_flag, key_size, *p = p_data;
  1052. tGATT_SR_REG *p_sreg;
  1053. UINT16 conn_id, offset = 0;
  1054. tGATT_SVC_DB *p_db;
  1055. BOOLEAN is_need_prepare_write_rsp = FALSE;
  1056. BOOLEAN is_need_queue_data = FALSE;
  1057. tGATT_PREPARE_WRITE_RECORD *prepare_record = NULL;
  1058. memset(&sr_data, 0, sizeof(tGATTS_DATA));
  1059. if (len < 2) {
  1060. GATT_TRACE_ERROR("%s: Prepare write request was invalid - missing offset, sending error response", __func__);
  1061. gatt_send_error_rsp(p_tcb, GATT_INVALID_PDU, op_code, handle, FALSE);
  1062. return;
  1063. }
  1064. //get offset from p_data
  1065. STREAM_TO_UINT16(offset, p);
  1066. len -= 2;
  1067. p_sreg = &gatt_cb.sr_reg[i_rcb];
  1068. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
  1069. //prepare_record = &(prepare_write_record);
  1070. prepare_record = &(p_tcb->prepare_write_record);
  1071. gatt_sr_get_sec_info(p_tcb->peer_bda,
  1072. p_tcb->transport,
  1073. &sec_flag,
  1074. &key_size);
  1075. status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
  1076. op_code,
  1077. handle,
  1078. sr_data.write_req.offset,
  1079. p,
  1080. len,
  1081. sec_flag,
  1082. key_size);
  1083. if (status == GATT_SUCCESS){
  1084. if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0) {
  1085. p_db = gatt_cb.sr_reg[i_rcb].p_db;
  1086. if (p_db && p_db->p_attr_list) {
  1087. p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
  1088. while (p_attr && handle >= p_attr->handle) {
  1089. if (p_attr->handle == handle ) {
  1090. p_attr_temp = p_attr;
  1091. if (p_attr->control.auto_rsp == GATT_RSP_BY_APP) {
  1092. status = GATT_APP_RSP;
  1093. } else if (p_attr->p_value != NULL &&
  1094. offset > p_attr->p_value->attr_val.attr_max_len) {
  1095. status = GATT_INVALID_OFFSET;
  1096. is_need_prepare_write_rsp = TRUE;
  1097. is_need_queue_data = TRUE;
  1098. } else if (p_attr->p_value != NULL &&
  1099. ((offset + len) > p_attr->p_value->attr_val.attr_max_len)){
  1100. status = GATT_INVALID_ATTR_LEN;
  1101. is_need_prepare_write_rsp = TRUE;
  1102. is_need_queue_data = TRUE;
  1103. } else if (p_attr->p_value == NULL) {
  1104. GATT_TRACE_ERROR("Error in %s, attribute of handle 0x%x not allocate value buffer\n",
  1105. __func__, handle);
  1106. status = GATT_UNKNOWN_ERROR;
  1107. } else {
  1108. //valid prepare write request, need to send response and queue the data
  1109. //status: GATT_SUCCESS
  1110. is_need_prepare_write_rsp = TRUE;
  1111. is_need_queue_data = TRUE;
  1112. }
  1113. }
  1114. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  1115. }
  1116. }
  1117. } else{
  1118. status = GATT_UNKNOWN_ERROR;
  1119. GATT_TRACE_ERROR("Error in %s, Line %d: GATT BUSY\n", __func__, __LINE__);
  1120. }
  1121. }
  1122. if (is_need_queue_data){
  1123. queue_data = (tGATT_PREPARE_WRITE_QUEUE_DATA *)osi_malloc(len + sizeof(tGATT_PREPARE_WRITE_QUEUE_DATA));
  1124. if (queue_data == NULL){
  1125. status = GATT_PREPARE_Q_FULL;
  1126. } else {
  1127. queue_data->p_attr = p_attr_temp;
  1128. queue_data->len = len;
  1129. queue_data->handle = handle;
  1130. queue_data->offset = offset;
  1131. memcpy(queue_data->value, p, len);
  1132. if (prepare_record->queue == NULL) {
  1133. prepare_record->queue = fixed_queue_new(QUEUE_SIZE_MAX);
  1134. }
  1135. fixed_queue_enqueue(prepare_record->queue, queue_data, FIXED_QUEUE_MAX_TIMEOUT);
  1136. }
  1137. }
  1138. if (is_need_prepare_write_rsp){
  1139. //send prepare write response
  1140. if (queue_data != NULL){
  1141. queue_data->op_code = op_code + 1;
  1142. //5: op_code 1 + handle 2 + offset 2
  1143. tGATT_STATUS rsp_send_status = gatt_send_packet(p_tcb, &(queue_data->op_code), queue_data->len + 5);
  1144. gatt_sr_update_prep_cnt(p_tcb, p_sreg->gatt_if, TRUE, FALSE);
  1145. gatt_dequeue_sr_cmd(p_tcb);
  1146. if (rsp_send_status != GATT_SUCCESS){
  1147. GATT_TRACE_ERROR("Error in %s, line=%d, fail to send prepare_write_rsp, status=0x%x\n",
  1148. __func__, __LINE__, rsp_send_status);
  1149. }
  1150. } else{
  1151. GATT_TRACE_ERROR("Error in %s, line=%d, queue_data should not be NULL here, fail to send prepare_write_rsp\n",
  1152. __func__, __LINE__);
  1153. }
  1154. }
  1155. if ((status == GATT_APP_RSP) || (is_need_prepare_write_rsp)){
  1156. prepare_record->total_num++;
  1157. memset(&sr_data, 0, sizeof(sr_data));
  1158. sr_data.write_req.is_prep = TRUE;
  1159. sr_data.write_req.handle = handle;
  1160. sr_data.write_req.offset = offset;
  1161. sr_data.write_req.len = len;
  1162. sr_data.write_req.need_rsp = (status == GATT_APP_RSP) ? TRUE : FALSE;
  1163. memcpy(sr_data.write_req.value, p, len);
  1164. gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE, &sr_data);
  1165. } else{
  1166. gatt_send_error_rsp(p_tcb, status, GATT_REQ_PREPARE_WRITE, handle, TRUE);
  1167. }
  1168. if ((prepare_record->error_code_app == GATT_SUCCESS)
  1169. && ((status == GATT_INVALID_OFFSET) || (status == GATT_INVALID_ATTR_LEN))){
  1170. prepare_record->error_code_app = status;
  1171. }
  1172. }
  1173. /*******************************************************************************
  1174. **
  1175. ** Function gatts_process_read_req
  1176. **
  1177. ** Description This function is called to process the read request
  1178. ** from client.
  1179. **
  1180. ** Returns void
  1181. **
  1182. *******************************************************************************/
  1183. static void gatts_process_read_req(tGATT_TCB *p_tcb, tGATT_SR_REG *p_rcb, UINT8 op_code,
  1184. UINT16 handle, UINT16 len, UINT8 *p_data)
  1185. {
  1186. UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
  1187. tGATT_STATUS reason;
  1188. BT_HDR *p_msg = NULL;
  1189. UINT8 sec_flag, key_size, *p;
  1190. UINT16 offset = 0, value_len = 0;
  1191. UNUSED (len);
  1192. if ((p_msg = (BT_HDR *)osi_calloc(buf_len)) == NULL) {
  1193. GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.\n");
  1194. reason = GATT_NO_RESOURCES;
  1195. } else {
  1196. if (op_code == GATT_REQ_READ_BLOB) {
  1197. STREAM_TO_UINT16(offset, p_data);
  1198. }
  1199. p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
  1200. *p ++ = op_code + 1;
  1201. p_msg->len = 1;
  1202. buf_len = p_tcb->payload_size - 1;
  1203. gatt_sr_get_sec_info(p_tcb->peer_bda,
  1204. p_tcb->transport,
  1205. &sec_flag,
  1206. &key_size);
  1207. reason = gatts_read_attr_value_by_handle(p_tcb,
  1208. p_rcb->p_db,
  1209. op_code,
  1210. handle,
  1211. offset,
  1212. p,
  1213. &value_len,
  1214. buf_len,
  1215. sec_flag,
  1216. key_size,
  1217. 0);
  1218. p_msg->len += value_len;
  1219. }
  1220. if (reason != GATT_SUCCESS && reason != GATT_PENDING && reason != GATT_STACK_RSP) {
  1221. if (p_msg) {
  1222. osi_free(p_msg);
  1223. }
  1224. /* in theroy BUSY is not possible(should already been checked), protected check */
  1225. if (reason != GATT_BUSY) {
  1226. gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
  1227. gatt_dequeue_sr_cmd(p_tcb);
  1228. }
  1229. } else if (reason == GATT_SUCCESS || reason == GATT_STACK_RSP) {
  1230. attp_send_sr_msg(p_tcb, p_msg);
  1231. gatt_dequeue_sr_cmd(p_tcb);
  1232. } else {
  1233. if (p_msg) {
  1234. osi_free(p_msg);
  1235. }
  1236. }
  1237. }
  1238. /*******************************************************************************
  1239. **
  1240. ** Function gatts_process_attribute_req
  1241. **
  1242. ** Description This function is called to process the per attribute handle request
  1243. ** from client.
  1244. **
  1245. ** Returns void
  1246. **
  1247. *******************************************************************************/
  1248. void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
  1249. UINT16 len, UINT8 *p_data)
  1250. {
  1251. UINT16 handle = 0;
  1252. UINT8 *p = p_data, i;
  1253. tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
  1254. tGATT_STATUS status = GATT_INVALID_HANDLE;
  1255. tGATT_ATTR16 *p_attr;
  1256. if (len < 2) {
  1257. GATT_TRACE_ERROR("Illegal PDU length, discard request\n");
  1258. status = GATT_INVALID_PDU;
  1259. } else {
  1260. STREAM_TO_UINT16(handle, p);
  1261. len -= 2;
  1262. }
  1263. #if GATT_CONFORMANCE_TESTING == TRUE
  1264. gatt_cb.handle = handle;
  1265. if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
  1266. GATT_TRACE_DEBUG("Conformance tst: forced err rsp: error status=%d\n", gatt_cb.err_status);
  1267. gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
  1268. return;
  1269. }
  1270. #endif
  1271. if (GATT_HANDLE_IS_VALID(handle)) {
  1272. for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++) {
  1273. if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle) {
  1274. p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
  1275. while (p_attr) {
  1276. if (p_attr->handle == handle) {
  1277. switch (op_code) {
  1278. case GATT_REQ_READ: /* read char/char descriptor value */
  1279. case GATT_REQ_READ_BLOB:
  1280. gatts_process_read_req(p_tcb, p_rcb, op_code, handle, len, p);
  1281. break;
  1282. case GATT_REQ_WRITE: /* write char/char descriptor value */
  1283. case GATT_CMD_WRITE:
  1284. case GATT_SIGN_CMD_WRITE:
  1285. gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
  1286. break;
  1287. case GATT_REQ_PREPARE_WRITE:
  1288. gatt_attr_process_prepare_write (p_tcb, i, handle, op_code, len, p);
  1289. default:
  1290. break;
  1291. }
  1292. status = GATT_SUCCESS;
  1293. break;
  1294. }
  1295. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  1296. }
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE) {
  1302. gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
  1303. }
  1304. }
  1305. /*******************************************************************************
  1306. **
  1307. ** Function gatts_proc_srv_chg_ind_ack
  1308. **
  1309. ** Description This function process the service changed indicaiton ACK
  1310. **
  1311. ** Returns void
  1312. **
  1313. *******************************************************************************/
  1314. static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
  1315. {
  1316. tGATTS_SRV_CHG_REQ req;
  1317. tGATTS_SRV_CHG *p_buf = NULL;
  1318. GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack");
  1319. if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL) {
  1320. GATT_TRACE_DEBUG("NV update set srv chg = FALSE");
  1321. p_buf->srv_changed = FALSE;
  1322. memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
  1323. if (gatt_cb.cb_info.p_srv_chg_callback) {
  1324. (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT, &req, NULL);
  1325. }
  1326. }
  1327. }
  1328. /*******************************************************************************
  1329. **
  1330. ** Function gatts_chk_pending_ind
  1331. **
  1332. ** Description This function check any pending indication needs to be sent if
  1333. ** there is a pending indication then sent the indication
  1334. **
  1335. ** Returns void
  1336. **
  1337. *******************************************************************************/
  1338. static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
  1339. {
  1340. #if (GATTS_INCLUDED == TRUE)
  1341. tGATT_VALUE *p_buf = (tGATT_VALUE *)fixed_queue_try_peek_first(p_tcb->pending_ind_q);
  1342. GATT_TRACE_DEBUG("gatts_chk_pending_ind");
  1343. if (p_buf ) {
  1344. GATTS_HandleValueIndication (p_buf->conn_id,
  1345. p_buf->handle,
  1346. p_buf->len,
  1347. p_buf->value);
  1348. osi_free(fixed_queue_try_remove_from_queue(p_tcb->pending_ind_q,
  1349. p_buf));
  1350. }
  1351. #endif ///GATTS_INCLUDED == TRUE
  1352. }
  1353. /*******************************************************************************
  1354. **
  1355. ** Function gatts_proc_ind_ack
  1356. **
  1357. ** Description This function process the Indication ack
  1358. **
  1359. ** Returns TRUE continue to process the indication ack by the aaplication
  1360. ** if the ACk is not a Service Changed Indication Ack
  1361. **
  1362. *******************************************************************************/
  1363. static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
  1364. {
  1365. BOOLEAN continue_processing = TRUE;
  1366. GATT_TRACE_DEBUG ("gatts_proc_ind_ack ack handle=%d", ack_handle);
  1367. if (ack_handle == gatt_cb.handle_of_h_r) {
  1368. gatts_proc_srv_chg_ind_ack(p_tcb);
  1369. /* there is no need to inform the application since srv chg is handled internally by GATT */
  1370. continue_processing = FALSE;
  1371. }
  1372. gatts_chk_pending_ind(p_tcb);
  1373. return continue_processing;
  1374. }
  1375. /*******************************************************************************
  1376. **
  1377. ** Function gatts_process_value_conf
  1378. **
  1379. ** Description This function is called to process the handle value confirmation.
  1380. **
  1381. ** Returns void
  1382. **
  1383. *******************************************************************************/
  1384. void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
  1385. {
  1386. UINT16 handle = p_tcb->indicate_handle;
  1387. UINT32 trans_id;
  1388. UINT8 i;
  1389. tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
  1390. BOOLEAN continue_processing;
  1391. UINT16 conn_id;
  1392. btu_stop_timer (&p_tcb->conf_timer_ent);
  1393. if (GATT_HANDLE_IS_VALID(handle)) {
  1394. p_tcb->indicate_handle = 0;
  1395. continue_processing = gatts_proc_ind_ack(p_tcb, handle);
  1396. if (continue_processing) {
  1397. for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++) {
  1398. if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle) {
  1399. trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
  1400. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
  1401. tGATTS_DATA p_data = {0};
  1402. p_data.handle = handle;
  1403. gatt_sr_send_req_callback(conn_id,
  1404. trans_id, GATTS_REQ_TYPE_CONF, &p_data);
  1405. }
  1406. }
  1407. }
  1408. } else {
  1409. GATT_TRACE_ERROR("unexpected handle value confirmation");
  1410. }
  1411. }
  1412. /*******************************************************************************
  1413. **
  1414. ** Function gatt_server_handle_client_req
  1415. **
  1416. ** Description This function is called to handle the client requests to
  1417. ** server.
  1418. **
  1419. **
  1420. ** Returns void
  1421. **
  1422. *******************************************************************************/
  1423. void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
  1424. UINT16 len, UINT8 *p_data)
  1425. {
  1426. /* there is pending command, discard this one */
  1427. if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF) {
  1428. return;
  1429. }
  1430. /* the size of the message may not be bigger than the local max PDU size*/
  1431. /* The message has to be smaller than the agreed MTU, len does not include op code */
  1432. if (len >= p_tcb->payload_size) {
  1433. GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1, p_tcb->payload_size );
  1434. /* for invalid request expecting response, send it now */
  1435. if (op_code != GATT_CMD_WRITE &&
  1436. op_code != GATT_SIGN_CMD_WRITE &&
  1437. op_code != GATT_HANDLE_VALUE_CONF) {
  1438. gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
  1439. }
  1440. /* otherwise, ignore the pkt */
  1441. } else {
  1442. switch (op_code) {
  1443. case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
  1444. case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
  1445. gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
  1446. break;
  1447. case GATT_REQ_FIND_INFO: /* discover char descrptor */
  1448. gatts_process_find_info(p_tcb, op_code, len, p_data);
  1449. break;
  1450. case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor value */
  1451. /* discover characteristic, discover char by UUID */
  1452. gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
  1453. break;
  1454. case GATT_REQ_READ: /* read char/char descriptor value */
  1455. case GATT_REQ_READ_BLOB:
  1456. case GATT_REQ_WRITE: /* write char/char descriptor value */
  1457. case GATT_CMD_WRITE:
  1458. case GATT_SIGN_CMD_WRITE:
  1459. case GATT_REQ_PREPARE_WRITE:
  1460. gatts_process_attribute_req (p_tcb, op_code, len, p_data);
  1461. break;
  1462. case GATT_HANDLE_VALUE_CONF:
  1463. gatts_process_value_conf (p_tcb, op_code);
  1464. break;
  1465. case GATT_REQ_MTU:
  1466. gatts_process_mtu_req (p_tcb, len, p_data);
  1467. break;
  1468. case GATT_REQ_EXEC_WRITE:
  1469. gatt_process_exec_write_req (p_tcb, op_code, len, p_data);
  1470. break;
  1471. case GATT_REQ_READ_MULTI:
  1472. gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
  1473. break;
  1474. default:
  1475. break;
  1476. }
  1477. }
  1478. }
  1479. #endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */