gatt_sr.c 69 KB

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