gatt_db.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2009-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 GATT database building and query functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #if BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE
  25. #include "common/bt_trace.h"
  26. #include "osi/allocator.h"
  27. //#include <stdio.h>
  28. #include <string.h>
  29. #include "gatt_int.h"
  30. #include "stack/l2c_api.h"
  31. #include "btm_int.h"
  32. extern tGATT_STATUS gap_proc_read(tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp);
  33. extern tGATT_STATUS gatt_proc_read(UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp);
  34. /********************************************************************************
  35. ** L O C A L F U N C T I O N P R O T O T Y P E S *
  36. *********************************************************************************/
  37. static BOOLEAN allocate_svc_db_buf(tGATT_SVC_DB *p_db);
  38. static void *allocate_attr_in_db(tGATT_SVC_DB *p_db, tBT_UUID *p_uuid, tGATT_PERM perm);
  39. static BOOLEAN deallocate_attr_in_db(tGATT_SVC_DB *p_db, void *p_attr);
  40. static BOOLEAN copy_extra_byte_in_db(tGATT_SVC_DB *p_db, void **p_dst, UINT16 len);
  41. static BOOLEAN gatts_db_add_service_declaration(tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri);
  42. static tGATT_STATUS gatts_send_app_read_request(tGATT_TCB *p_tcb, UINT8 op_code,
  43. UINT16 handle, UINT16 offset, UINT32 trans_id, BOOLEAN need_rsp);
  44. static BOOLEAN gatts_add_char_desc_value_check (tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control);
  45. /*******************************************************************************
  46. **
  47. ** Function gatts_init_service_db
  48. **
  49. ** Description This function initialize a memory space to be a service database.
  50. **
  51. ** Parameter p_db: database pointer.
  52. ** len: size of the memory space.
  53. **
  54. ** Returns Status of te operation.
  55. **
  56. *******************************************************************************/
  57. BOOLEAN gatts_init_service_db (tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri,
  58. UINT16 s_hdl, UINT16 num_handle)
  59. {
  60. if (p_db->svc_buffer == NULL) { //in case already alloc
  61. p_db->svc_buffer = fixed_queue_new(QUEUE_SIZE_MAX);
  62. }
  63. if (!allocate_svc_db_buf(p_db)) {
  64. GATT_TRACE_ERROR("gatts_init_service_db failed, no resources\n");
  65. return FALSE;
  66. }
  67. GATT_TRACE_DEBUG("gatts_init_service_db\n");
  68. GATT_TRACE_DEBUG("s_hdl = %d num_handle = %d\n", s_hdl, num_handle );
  69. /* update service database information */
  70. p_db->next_handle = s_hdl;
  71. p_db->end_handle = s_hdl + num_handle;
  72. return gatts_db_add_service_declaration(p_db, p_service, is_pri);
  73. }
  74. /*******************************************************************************
  75. **
  76. ** Function gatts_init_service_db
  77. **
  78. ** Description This function initialize a memory space to be a service database.
  79. **
  80. ** Parameter p_db: database pointer.
  81. ** len: size of the memory space.
  82. **
  83. ** Returns Status of te operation.
  84. **
  85. *******************************************************************************/
  86. tBT_UUID *gatts_get_service_uuid (tGATT_SVC_DB *p_db)
  87. {
  88. if (!p_db || !p_db->p_attr_list) {
  89. GATT_TRACE_ERROR("service DB empty\n");
  90. return NULL;
  91. } else {
  92. return &((tGATT_ATTR16 *)p_db->p_attr_list)->p_value->uuid;
  93. }
  94. }
  95. /*******************************************************************************
  96. **
  97. ** Function gatts_check_attr_readability
  98. **
  99. ** Description check attribute readability
  100. **
  101. ** Returns status of operation.
  102. **
  103. *******************************************************************************/
  104. static tGATT_STATUS gatts_check_attr_readability(tGATT_ATTR16 *p_attr,
  105. UINT16 offset,
  106. BOOLEAN read_long,
  107. tGATT_SEC_FLAG sec_flag,
  108. UINT8 key_size)
  109. {
  110. UINT16 min_key_size;
  111. tGATT_PERM perm = p_attr->permission;
  112. UNUSED(offset);
  113. min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
  114. if (min_key_size != 0 ) {
  115. min_key_size += 6;
  116. }
  117. if (!(perm & GATT_READ_ALLOWED)) {
  118. GATT_TRACE_ERROR( "GATT_READ_NOT_PERMIT\n");
  119. return GATT_READ_NOT_PERMIT;
  120. }
  121. if ((perm & GATT_READ_AUTH_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_UNAUTHED) &&
  122. !(sec_flag & BTM_SEC_FLAG_ENCRYPTED)) {
  123. GATT_TRACE_ERROR( "GATT_INSUF_AUTHENTICATION\n");
  124. return GATT_INSUF_AUTHENTICATION;
  125. }
  126. if ((perm & GATT_READ_MITM_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_AUTHED)) {
  127. GATT_TRACE_ERROR( "GATT_INSUF_AUTHENTICATION: MITM Required\n");
  128. return GATT_INSUF_AUTHENTICATION;
  129. }
  130. if ((perm & GATT_READ_ENCRYPTED_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_ENCRYPTED)) {
  131. GATT_TRACE_ERROR( "GATT_INSUF_ENCRYPTION\n");
  132. return GATT_INSUF_ENCRYPTION;
  133. }
  134. if ( (perm & GATT_READ_ENCRYPTED_REQUIRED) && (sec_flag & GATT_SEC_FLAG_ENCRYPTED) && (key_size < min_key_size)) {
  135. GATT_TRACE_ERROR( "GATT_INSUF_KEY_SIZE\n");
  136. return GATT_INSUF_KEY_SIZE;
  137. }
  138. /* LE Authorization check*/
  139. if ((perm & GATT_READ_AUTHORIZATION) && (!(sec_flag & GATT_SEC_FLAG_LKEY_AUTHED) || !(sec_flag & GATT_SEC_FLAG_AUTHORIZATION))) {
  140. GATT_TRACE_ERROR( "GATT_INSUF_AUTHORIZATION\n");
  141. return GATT_INSUF_AUTHORIZATION;
  142. }
  143. if (read_long) {
  144. switch (p_attr->uuid) {
  145. case GATT_UUID_PRI_SERVICE:
  146. case GATT_UUID_SEC_SERVICE:
  147. case GATT_UUID_CHAR_DECLARE:
  148. case GATT_UUID_INCLUDE_SERVICE:
  149. case GATT_UUID_CHAR_EXT_PROP:
  150. case GATT_UUID_CHAR_CLIENT_CONFIG:
  151. case GATT_UUID_CHAR_SRVR_CONFIG:
  152. case GATT_UUID_CHAR_PRESENT_FORMAT:
  153. GATT_TRACE_ERROR("GATT_NOT_LONG\n");
  154. return GATT_NOT_LONG;
  155. default:
  156. break;
  157. }
  158. }
  159. return GATT_SUCCESS;
  160. }
  161. /*******************************************************************************
  162. **
  163. ** Function read_attr_value
  164. **
  165. ** Description Utility function to read an attribute value.
  166. **
  167. ** Parameter p_attr: pointer to the attribute to read.
  168. ** offset: read offset.
  169. ** p_value: output parameter to carry out the attribute value.
  170. ** p_len: output parameter to carry out the attribute length.
  171. ** read_long: this is a read blob request.
  172. ** mtu: MTU
  173. ** sec_flag: current link security status.
  174. ** key_size: encryption key size.
  175. **
  176. ** Returns status of operation.
  177. **
  178. *******************************************************************************/
  179. static tGATT_STATUS read_attr_value (void *p_attr,
  180. UINT16 offset,
  181. UINT8 **p_data,
  182. BOOLEAN read_long,
  183. UINT16 mtu,
  184. UINT16 *p_len,
  185. tGATT_SEC_FLAG sec_flag,
  186. UINT8 key_size)
  187. {
  188. UINT16 len = 0, uuid16 = 0;
  189. UINT8 *p = *p_data;
  190. tGATT_STATUS status;
  191. tGATT_ATTR16 *p_attr16 = (tGATT_ATTR16 *)p_attr;
  192. GATT_TRACE_DEBUG("read_attr_value uuid=0x%04x perm=0x%0x sec_flag=0x%x offset=%d read_long=%d\n",
  193. p_attr16->uuid,
  194. p_attr16->permission,
  195. sec_flag,
  196. offset,
  197. read_long);
  198. status = gatts_check_attr_readability((tGATT_ATTR16 *)p_attr, offset, read_long, sec_flag, key_size);
  199. if (status != GATT_SUCCESS) {
  200. return status;
  201. }
  202. if (p_attr16->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  203. uuid16 = p_attr16->uuid;
  204. }
  205. status = GATT_NO_RESOURCES;
  206. if (uuid16 == GATT_UUID_PRI_SERVICE || uuid16 == GATT_UUID_SEC_SERVICE) {
  207. len = p_attr16->p_value->uuid.len;
  208. if (mtu >= p_attr16->p_value->uuid.len) {
  209. gatt_build_uuid_to_stream(&p, p_attr16->p_value->uuid);
  210. status = GATT_SUCCESS;
  211. }
  212. } else if (uuid16 == GATT_UUID_CHAR_DECLARE) {
  213. len = (((tGATT_ATTR16 *)(p_attr16->p_next))->uuid_type == GATT_ATTR_UUID_TYPE_16) ? 5 : 19;
  214. if (mtu >= len) {
  215. UINT8_TO_STREAM(p, p_attr16->p_value->char_decl.property);
  216. UINT16_TO_STREAM(p, p_attr16->p_value->char_decl.char_val_handle);
  217. if (((tGATT_ATTR16 *)(p_attr16->p_next))->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  218. UINT16_TO_STREAM(p, ((tGATT_ATTR16 *)(p_attr16->p_next))->uuid);
  219. }
  220. /* convert a 32bits UUID to 128 bits */
  221. else if (((tGATT_ATTR32 *)(p_attr16->p_next))->uuid_type == GATT_ATTR_UUID_TYPE_32) {
  222. gatt_convert_uuid32_to_uuid128 (p, ((tGATT_ATTR32 *)(p_attr16->p_next))->uuid);
  223. p += LEN_UUID_128;
  224. } else {
  225. ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *)(p_attr16->p_next))->uuid, LEN_UUID_128);
  226. }
  227. status = GATT_SUCCESS;
  228. }
  229. } else if (uuid16 == GATT_UUID_INCLUDE_SERVICE) {
  230. if (p_attr16->p_value->incl_handle.service_type.len == LEN_UUID_16) {
  231. len = 6;
  232. } else {
  233. len = 4;
  234. }
  235. if (mtu >= len) {
  236. UINT16_TO_STREAM(p, p_attr16->p_value->incl_handle.s_handle);
  237. UINT16_TO_STREAM(p, p_attr16->p_value->incl_handle.e_handle);
  238. if (p_attr16->p_value->incl_handle.service_type.len == LEN_UUID_16) {
  239. UINT16_TO_STREAM(p, p_attr16->p_value->incl_handle.service_type.uu.uuid16);
  240. }
  241. status = GATT_SUCCESS;
  242. }
  243. } else { /* characteristic description or characteristic value */
  244. if (p_attr16->control.auto_rsp == GATT_RSP_BY_STACK) {
  245. if (p_attr16->p_value == NULL || p_attr16->p_value->attr_val.attr_val == NULL) {
  246. status = GATT_UNKNOWN_ERROR;
  247. }
  248. else if (offset > p_attr16->p_value->attr_val.attr_len){
  249. /*if offset equal to max_len, should respond with zero byte value
  250. //if offset is greater than max_len, should respond with an error*/
  251. status = GATT_INVALID_OFFSET;
  252. } else {
  253. UINT8 *value = (UINT8 *)(p_attr16->p_value->attr_val.attr_val) + offset;
  254. UINT16 len_left = p_attr16->p_value->attr_val.attr_len - offset;
  255. len = (mtu >= len_left) ? (len_left) : mtu;
  256. ARRAY_TO_STREAM(p, value, len);
  257. status = GATT_STACK_RSP;
  258. }
  259. } else {
  260. status = GATT_PENDING;
  261. }
  262. }
  263. *p_len = len;
  264. *p_data = p;
  265. return status;
  266. }
  267. /*******************************************************************************
  268. **
  269. ** Function gatts_db_read_attr_value_by_type
  270. **
  271. ** Description Query attribute value by attribute type.
  272. **
  273. ** Parameter p_db: pointer to the attribute database.
  274. ** p_rsp: Read By type response data.
  275. ** s_handle: starting handle of the range we are looking for.
  276. ** e_handle: ending handle of the range we are looking for.
  277. ** type: Attribute type.
  278. ** mtu: MTU.
  279. ** sec_flag: current link security status.
  280. ** key_size: encryption key size.
  281. **
  282. ** Returns Status of the operation.
  283. **
  284. *******************************************************************************/
  285. tGATT_STATUS gatts_db_read_attr_value_by_type (tGATT_TCB *p_tcb,
  286. tGATT_SVC_DB *p_db,
  287. UINT8 op_code,
  288. BT_HDR *p_rsp,
  289. UINT16 s_handle,
  290. UINT16 e_handle,
  291. tBT_UUID type,
  292. UINT16 *p_len,
  293. tGATT_SEC_FLAG sec_flag,
  294. UINT8 key_size,
  295. UINT32 trans_id,
  296. UINT16 *p_cur_handle)
  297. {
  298. tGATT_STATUS status = GATT_NOT_FOUND;
  299. tGATT_ATTR16 *p_attr;
  300. UINT16 len = 0;
  301. UINT8 *p = (UINT8 *)(p_rsp + 1) + p_rsp->len + L2CAP_MIN_OFFSET;
  302. tBT_UUID attr_uuid;
  303. #if (defined(BLE_DELAY_REQUEST_ENC) && (BLE_DELAY_REQUEST_ENC == TRUE))
  304. UINT8 flag;
  305. #endif
  306. BOOLEAN need_rsp;
  307. BOOLEAN have_send_request = false;
  308. if (p_db && p_db->p_attr_list) {
  309. p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
  310. while (p_attr && p_attr->handle <= e_handle) {
  311. if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  312. attr_uuid.len = LEN_UUID_16;
  313. attr_uuid.uu.uuid16 = p_attr->uuid;
  314. } else if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32) {
  315. attr_uuid.len = LEN_UUID_32;
  316. attr_uuid.uu.uuid32 = ((tGATT_ATTR32 *)p_attr)->uuid;
  317. } else {
  318. attr_uuid.len = LEN_UUID_128;
  319. memcpy(attr_uuid.uu.uuid128, ((tGATT_ATTR128 *)p_attr)->uuid, LEN_UUID_128);
  320. }
  321. if (p_attr->handle >= s_handle && gatt_uuid_compare(type, attr_uuid)) {
  322. if (*p_len <= 2) {
  323. status = GATT_NO_RESOURCES;
  324. break;
  325. }
  326. UINT16_TO_STREAM (p, p_attr->handle);
  327. status = read_attr_value ((void *)p_attr, 0, &p, FALSE, (UINT16)(*p_len - 2), &len, sec_flag, key_size);
  328. if (status == GATT_PENDING) {
  329. need_rsp = TRUE;
  330. status = gatts_send_app_read_request(p_tcb, op_code, p_attr->handle, 0, trans_id, need_rsp);
  331. /* one callback at a time */
  332. break;
  333. } else if (status == GATT_SUCCESS || status == GATT_STACK_RSP) {
  334. if (status == GATT_STACK_RSP){
  335. need_rsp = FALSE;
  336. status = gatts_send_app_read_request(p_tcb, op_code, p_attr->handle, 0, trans_id, need_rsp);
  337. if(status == GATT_BUSY)
  338. break;
  339. if (!have_send_request){
  340. have_send_request = true;
  341. trans_id = p_tcb->sr_cmd.trans_id;
  342. }
  343. }
  344. if (p_rsp->offset == 0) {
  345. p_rsp->offset = len + 2;
  346. }
  347. if (p_rsp->offset == len + 2) {
  348. p_rsp->len += (len + 2);
  349. *p_len -= (len + 2);
  350. } else {
  351. GATT_TRACE_WARNING("format mismatch");
  352. status = GATT_NO_RESOURCES;
  353. break;
  354. }
  355. } else {
  356. *p_cur_handle = p_attr->handle;
  357. break;
  358. }
  359. }
  360. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  361. }
  362. }
  363. #if (defined(BLE_DELAY_REQUEST_ENC) && (BLE_DELAY_REQUEST_ENC == TRUE))
  364. if (BTM_GetSecurityFlags(p_tcb->peer_bda, &flag)) {
  365. if ((p_tcb->att_lcid == L2CAP_ATT_CID) && (status == GATT_PENDING) &&
  366. (type.uu.uuid16 == GATT_UUID_GAP_DEVICE_NAME)) {
  367. if ((flag & (BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_FLAG_ENCRYPTED)) ==
  368. BTM_SEC_LINK_KEY_KNOWN) {
  369. tACL_CONN *p;
  370. p = btm_bda_to_acl(p_tcb->peer_bda, BT_TRANSPORT_LE);
  371. if ((p != NULL) && (p->link_role == BTM_ROLE_MASTER)) {
  372. tBTM_BLE_SEC_ACT sec_act = BTM_BLE_SEC_ENCRYPT;
  373. btm_ble_set_encryption(p_tcb->peer_bda, &sec_act, p->link_role);
  374. }
  375. }
  376. }
  377. }
  378. #endif
  379. return status;
  380. }
  381. /*******************************************************************************
  382. **
  383. ** Function gatts_add_included_service
  384. **
  385. ** Description This function adds an included service into a database.
  386. **
  387. ** Parameter p_db: database pointer.
  388. ** inc_srvc_type: included service type.
  389. **
  390. ** Returns Status of the operation.
  391. **
  392. *******************************************************************************/
  393. UINT16 gatts_add_included_service (tGATT_SVC_DB *p_db, UINT16 s_handle, UINT16 e_handle,
  394. tBT_UUID service)
  395. {
  396. tGATT_ATTR16 *p_attr;
  397. tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_INCLUDE_SERVICE}};
  398. GATT_TRACE_DEBUG("gatts_add_included_service: s_hdl = 0x%04x e_hdl = 0x%04x uuid = 0x%04x",
  399. s_handle, e_handle, service.uu.uuid16);
  400. if (service.len == 0 || s_handle == 0 || e_handle == 0) {
  401. GATT_TRACE_ERROR("gatts_add_included_service Illegal Params.");
  402. return 0;
  403. }
  404. BOOLEAN is_include_service_allowed = TRUE;
  405. // service declaration
  406. tGATT_ATTR16 *first_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
  407. if (p_db->p_attr_list != NULL) {
  408. tGATT_ATTR16 *next_attr = (tGATT_ATTR16 *)first_attr->p_next;
  409. /* This service already has other attributes */
  410. while (next_attr != NULL) {
  411. if (!(next_attr->uuid_type == GATT_ATTR_UUID_TYPE_16 && next_attr->uuid == GATT_UUID_INCLUDE_SERVICE)) {
  412. is_include_service_allowed = FALSE;
  413. break;
  414. }
  415. next_attr = (tGATT_ATTR16 *)next_attr->p_next;
  416. }
  417. }
  418. if (!is_include_service_allowed) {
  419. GATT_TRACE_ERROR("%s error, The include service should be added before adding the characteristics", __func__);
  420. return 0;
  421. }
  422. if ((p_attr = (tGATT_ATTR16 *) allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ)) != NULL) {
  423. if (copy_extra_byte_in_db(p_db, (void **)&p_attr->p_value, sizeof(tGATT_INCL_SRVC))) {
  424. p_attr->p_value->incl_handle.s_handle = s_handle;
  425. p_attr->p_value->incl_handle.e_handle = e_handle;
  426. memcpy(&p_attr->p_value->incl_handle.service_type, &service, sizeof(tBT_UUID));
  427. return p_attr->handle;
  428. } else {
  429. deallocate_attr_in_db(p_db, p_attr);
  430. }
  431. }
  432. return 0;
  433. }
  434. /*******************************************************************************
  435. **
  436. ** Function gatts_add_characteristic
  437. **
  438. ** Description This function add a characteristics and its descriptor into
  439. ** a servce identified by the service database pointer.
  440. **
  441. ** Parameter p_db: database pointer.
  442. ** perm: permission (authentication and key size requirements)
  443. ** property: property of the characteristic.
  444. ** p_char: characteristic value information.
  445. **
  446. ** Returns Status of te operation.
  447. **
  448. *******************************************************************************/
  449. UINT16 gatts_add_characteristic (tGATT_SVC_DB *p_db, tGATT_PERM perm,
  450. tGATT_CHAR_PROP property,
  451. tBT_UUID *p_char_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
  452. {
  453. tGATT_ATTR16 *p_char_decl, *p_char_val;
  454. tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_DECLARE}};
  455. BOOLEAN status;
  456. GATT_TRACE_DEBUG("gatts_add_characteristic perm=0x%0x property=0x%0x\n", perm, property);
  457. /* parameter validation check */
  458. status = gatts_add_char_desc_value_check(attr_val, control);
  459. if (status == FALSE){
  460. return 0;
  461. }
  462. if ((p_char_decl = (tGATT_ATTR16 *)allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ)) != NULL) {
  463. if (!copy_extra_byte_in_db(p_db, (void **)&p_char_decl->p_value, sizeof(tGATT_CHAR_DECL))) {
  464. deallocate_attr_in_db(p_db, p_char_decl);
  465. return 0;
  466. }
  467. p_char_val = (tGATT_ATTR16 *)allocate_attr_in_db(p_db, p_char_uuid, perm);
  468. if (p_char_val == NULL) {
  469. deallocate_attr_in_db(p_db, p_char_decl);
  470. return 0;
  471. }
  472. p_char_decl->p_value->char_decl.property = property;
  473. p_char_decl->p_value->char_decl.char_val_handle = p_char_val->handle;
  474. if (control != NULL) {
  475. p_char_val->control.auto_rsp = control->auto_rsp;
  476. } else {
  477. p_char_val->control.auto_rsp = GATT_RSP_DEFAULT;
  478. }
  479. if (attr_val != NULL) {
  480. if (!copy_extra_byte_in_db(p_db, (void **)&p_char_val->p_value, sizeof(tGATT_ATTR_VAL))) {
  481. deallocate_attr_in_db(p_db, p_char_val);
  482. return 0;
  483. }
  484. GATT_TRACE_DEBUG("attr_val->attr_len = %x, attr_val->attr_max_len = %x\n", attr_val->attr_len, attr_val->attr_max_len);
  485. GATT_TRACE_DEBUG("attribute handle = %x\n", p_char_val->handle);
  486. p_char_val->p_value->attr_val.attr_len = attr_val->attr_len;
  487. p_char_val->p_value->attr_val.attr_max_len = attr_val->attr_max_len;
  488. p_char_val->p_value->attr_val.attr_val = osi_malloc(attr_val->attr_max_len);
  489. if (p_char_val->p_value->attr_val.attr_val == NULL) {
  490. deallocate_attr_in_db(p_db, p_char_decl);
  491. deallocate_attr_in_db(p_db, p_char_val);
  492. GATT_TRACE_WARNING("Warning in %s, line=%d, insufficient resource to allocate for attribute value\n", __func__, __LINE__);
  493. return 0;
  494. }
  495. else {
  496. //add mask to indicate that p_value->attr_val.attr_val is dynamic allocated
  497. p_char_val->mask |= GATT_ATTR_VALUE_ALLOCATED;
  498. }
  499. //initiate characteristic attribute value part
  500. memset(p_char_val->p_value->attr_val.attr_val, 0, attr_val->attr_max_len);
  501. if (attr_val->attr_val != NULL) {
  502. if (attr_val->attr_max_len < attr_val->attr_len){
  503. GATT_TRACE_ERROR("Error in %s, Line=%d, attribute actual length (%d) should not larger than max size (%d)\n",
  504. __func__, __LINE__, attr_val->attr_len, attr_val->attr_max_len);
  505. }
  506. UINT16 actual_len = (attr_val->attr_max_len < attr_val->attr_len) ? (attr_val->attr_max_len) : (attr_val->attr_len);
  507. memcpy(p_char_val->p_value->attr_val.attr_val, attr_val->attr_val, actual_len);
  508. }
  509. }
  510. return p_char_val->handle;
  511. }
  512. return 0;
  513. }
  514. /*******************************************************************************
  515. **
  516. ** Function gatt_convertchar_descr_type
  517. **
  518. ** Description This function convert a char descript UUID into descriptor type.
  519. **
  520. ** Returns descriptor type.
  521. **
  522. *******************************************************************************/
  523. UINT8 gatt_convertchar_descr_type(tBT_UUID *p_descr_uuid)
  524. {
  525. tBT_UUID std_descr = {LEN_UUID_16, {GATT_UUID_CHAR_EXT_PROP}};
  526. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  527. return GATT_DESCR_EXT_DSCPTOR;
  528. }
  529. std_descr.uu.uuid16 ++;
  530. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  531. return GATT_DESCR_USER_DSCPTOR;
  532. }
  533. std_descr.uu.uuid16 ++;
  534. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  535. return GATT_DESCR_CLT_CONFIG;
  536. }
  537. std_descr.uu.uuid16 ++;
  538. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  539. return GATT_DESCR_SVR_CONFIG;
  540. }
  541. std_descr.uu.uuid16 ++;
  542. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  543. return GATT_DESCR_PRES_FORMAT;
  544. }
  545. std_descr.uu.uuid16 ++;
  546. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  547. return GATT_DESCR_AGGR_FORMAT;
  548. }
  549. std_descr.uu.uuid16 ++;
  550. if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
  551. return GATT_DESCR_VALID_RANGE;
  552. }
  553. return GATT_DESCR_UNKNOWN;
  554. }
  555. /*******************************************************************************
  556. **
  557. ** Function gatts_add_char_descr
  558. **
  559. ** Description This function add a characteristics descriptor.
  560. **
  561. ** Parameter p_db: database pointer.
  562. ** perm: characteristic descriptor permission type.
  563. ** char_dscp_tpye: the characteristic descriptor masks.
  564. ** p_dscp_params: characteristic descriptors values.
  565. **
  566. ** Returns Status of the operation.
  567. **
  568. *******************************************************************************/
  569. UINT16 gatts_add_char_descr (tGATT_SVC_DB *p_db, tGATT_PERM perm,
  570. tBT_UUID *p_descr_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
  571. {
  572. tGATT_ATTR16 *p_char_dscptr;
  573. BOOLEAN status;
  574. GATT_TRACE_DEBUG("gatts_add_char_descr uuid=0x%04x\n", p_descr_uuid->uu.uuid16);
  575. /* parameter validation check */
  576. status = gatts_add_char_desc_value_check(attr_val, control);
  577. if (status == FALSE){
  578. return 0;
  579. }
  580. /* Add characteristic descriptors */
  581. if ((p_char_dscptr = (tGATT_ATTR16 *)allocate_attr_in_db(p_db, p_descr_uuid, perm)) == NULL) {
  582. deallocate_attr_in_db(p_db, p_char_dscptr);
  583. GATT_TRACE_DEBUG("gatts_add_char_descr Fail for adding char descriptors.");
  584. return 0;
  585. } else {
  586. p_char_dscptr->control.auto_rsp = (control == NULL) ? GATT_RSP_DEFAULT : (control->auto_rsp);
  587. if (attr_val != NULL) {
  588. if (!copy_extra_byte_in_db(p_db, (void **)&p_char_dscptr->p_value, sizeof(tGATT_ATTR_VAL))) {
  589. deallocate_attr_in_db(p_db, p_char_dscptr);
  590. return 0;
  591. }
  592. p_char_dscptr->p_value->attr_val.attr_len = attr_val->attr_len;
  593. p_char_dscptr->p_value->attr_val.attr_max_len = attr_val->attr_max_len;
  594. if (attr_val->attr_max_len != 0) {
  595. p_char_dscptr->p_value->attr_val.attr_val = osi_malloc(attr_val->attr_max_len);
  596. if (p_char_dscptr->p_value->attr_val.attr_val == NULL) {
  597. deallocate_attr_in_db(p_db, p_char_dscptr);
  598. GATT_TRACE_WARNING("Warning in %s, line=%d, insufficient resource to allocate for descriptor value\n", __func__, __LINE__);
  599. return 0;
  600. }
  601. else {
  602. //add mask to indicate that p_value->attr_val.attr_val is dynamic allocated
  603. p_char_dscptr->mask |= GATT_ATTR_VALUE_ALLOCATED;
  604. }
  605. //initiate characteristic attribute value part
  606. memset(p_char_dscptr->p_value->attr_val.attr_val, 0, attr_val->attr_max_len);
  607. if(attr_val->attr_val != NULL) {
  608. memcpy(p_char_dscptr->p_value->attr_val.attr_val, attr_val->attr_val, attr_val->attr_len);
  609. }
  610. }
  611. }
  612. return p_char_dscptr->handle;
  613. }
  614. }
  615. /*******************************************************************************
  616. **
  617. ** Function gatts_set_attribute_value
  618. **
  619. ** Description This function add the attribute value in the database
  620. **
  621. ** Parameter p_db: database pointer.
  622. ** attr_handle: the attribute handle
  623. ** length: the attribute value length
  624. ** value: the pointer to the data to be set to the attribute value in the database
  625. **
  626. ** Returns Status of the operation.
  627. **
  628. *******************************************************************************/
  629. tGATT_STATUS gatts_set_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle,
  630. UINT16 length, UINT8 *value)
  631. {
  632. tGATT_ATTR16 *p_cur;
  633. if (p_db == NULL) {
  634. GATT_TRACE_DEBUG("gatts_set_attribute_value Fail:p_db is NULL.\n");
  635. return GATT_INVALID_PDU;
  636. }
  637. if (p_db->p_attr_list == NULL) {
  638. GATT_TRACE_DEBUG("gatts_set_attribute_value Fail:p_db->p_attr_list is NULL.\n");
  639. return GATT_INVALID_PDU;
  640. }
  641. if ((length > 0) && (value == NULL)){
  642. GATT_TRACE_ERROR("Error in %s, line=%d, value should not be NULL here\n",__func__, __LINE__);
  643. return GATT_INVALID_PDU;
  644. }
  645. p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
  646. while (p_cur != NULL) {
  647. if (p_cur->handle == attr_handle) {
  648. /* for characteristic should not be set, return GATT_NOT_FOUND */
  649. if (p_cur->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  650. switch (p_cur->uuid) {
  651. case GATT_UUID_PRI_SERVICE:
  652. case GATT_UUID_SEC_SERVICE:
  653. case GATT_UUID_CHAR_DECLARE:
  654. return GATT_NOT_FOUND;
  655. break;
  656. }
  657. }
  658. /* in other cases, value can be set*/
  659. if ((p_cur->p_value == NULL) || (p_cur->p_value->attr_val.attr_val == NULL) \
  660. || (p_cur->p_value->attr_val.attr_max_len == 0)){
  661. GATT_TRACE_ERROR("Error in %s, line=%d, attribute value should not be NULL here\n", __func__, __LINE__);
  662. return GATT_NOT_FOUND;
  663. } else if (p_cur->p_value->attr_val.attr_max_len < length) {
  664. GATT_TRACE_ERROR("gatts_set_attribute_value failed:Invalid value length");
  665. return GATT_INVALID_ATTR_LEN;
  666. } else{
  667. memcpy(p_cur->p_value->attr_val.attr_val, value, length);
  668. p_cur->p_value->attr_val.attr_len = length;
  669. }
  670. break;
  671. }
  672. p_cur = p_cur->p_next;
  673. }
  674. return GATT_SUCCESS;
  675. }
  676. /*******************************************************************************
  677. **
  678. ** Function gatts_get_attr_value_internal
  679. **
  680. ** Description This function get the attribute value in gap service and gatt service
  681. **
  682. ** Parameter attr_handle: the attribute handle
  683. ** length: the attribute value length
  684. ** value: the pointer to the data to be get to the attribute value in the database
  685. **
  686. ** Returns Status of the operation.
  687. **
  688. *******************************************************************************/
  689. tGATT_STATUS gatts_get_attr_value_internal(UINT16 attr_handle, UINT16 *length, UINT8 **value)
  690. {
  691. UINT8 i;
  692. tGATT_READ_REQ read_req;
  693. tGATT_STATUS status = GATT_NOT_FOUND;
  694. tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
  695. UINT8 service_uuid[LEN_UUID_128] = {0};
  696. if (length == NULL){
  697. GATT_TRACE_ERROR("gatts_get_attr_value_internal Fail:length is NULL.\n");
  698. return GATT_INVALID_PDU;
  699. }
  700. if (value == NULL){
  701. GATT_TRACE_ERROR("gatts_get_attr_value_internal Fail:value is NULL.\n");
  702. *length = 0;
  703. return GATT_INVALID_PDU;
  704. }
  705. // find the service by handle
  706. for (i = 0; i < GATT_MAX_SR_PROFILES; i++, p_rcb++) {
  707. if (p_rcb->in_use && p_rcb->s_hdl <= attr_handle && p_rcb->e_hdl >= attr_handle) {
  708. break;
  709. }
  710. }
  711. // service cb not found
  712. if (i == GATT_MAX_SR_PROFILES) {
  713. return status;
  714. }
  715. if (p_rcb->app_uuid.len != LEN_UUID_128) {
  716. return status;
  717. }
  718. memset(&read_req, 0, sizeof(tGATT_READ_REQ));
  719. read_req.handle = attr_handle;
  720. // read gatt service attribute value
  721. memset(service_uuid, 0x81, LEN_UUID_128);
  722. if (!memcmp(p_rcb->app_uuid.uu.uuid128, service_uuid, LEN_UUID_128)) {
  723. status = gatt_proc_read(0, GATTS_REQ_TYPE_READ, &read_req, &gatt_cb.rsp);
  724. }
  725. // read gap service attribute value
  726. memset(service_uuid, 0x82, LEN_UUID_128);
  727. if (!memcmp(p_rcb->app_uuid.uu.uuid128, service_uuid, LEN_UUID_128)) {
  728. status = gap_proc_read(GATTS_REQ_TYPE_READ, &read_req, &gatt_cb.rsp);
  729. }
  730. if (status == GATT_SUCCESS) {
  731. *length = gatt_cb.rsp.attr_value.len;
  732. *value = gatt_cb.rsp.attr_value.value;
  733. }
  734. return status;
  735. }
  736. /*******************************************************************************
  737. **
  738. ** Function gatts_get_attribute_value
  739. **
  740. ** Description This function get the attribute value in the database
  741. **
  742. ** Parameter p_db: database pointer.
  743. ** attr_handle: the attribute handle
  744. ** length: the attribute value length
  745. ** value: the pointer to the data to be get to the attribute value in the database
  746. **
  747. ** Returns Status of the operation.
  748. **
  749. *******************************************************************************/
  750. tGATT_STATUS gatts_get_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle,
  751. UINT16 *length, UINT8 **value)
  752. {
  753. tGATT_ATTR16 *p_cur;
  754. GATT_TRACE_DEBUG("attr_handle = %x\n", attr_handle);
  755. if (p_db == NULL) {
  756. GATT_TRACE_ERROR("gatts_get_attribute_value Fail:p_db is NULL.\n");
  757. *length = 0;
  758. return GATT_INVALID_PDU;
  759. }
  760. if (p_db->p_attr_list == NULL) {
  761. GATT_TRACE_ERROR("gatts_get_attribute_value Fail:p_db->p_attr_list is NULL.\n");
  762. *length = 0;
  763. return GATT_INVALID_PDU;
  764. }
  765. if (length == NULL){
  766. GATT_TRACE_ERROR("gatts_get_attribute_value Fail:length is NULL.\n");
  767. return GATT_INVALID_PDU;
  768. }
  769. if (value == NULL){
  770. GATT_TRACE_ERROR("gatts_get_attribute_value Fail:value is NULL.\n");
  771. *length = 0;
  772. return GATT_INVALID_PDU;
  773. }
  774. p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
  775. while (p_cur != NULL) {
  776. if (p_cur->handle == attr_handle) {
  777. if (p_cur->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  778. switch (p_cur->uuid) {
  779. case GATT_UUID_CHAR_DECLARE:
  780. case GATT_UUID_INCLUDE_SERVICE:
  781. break;
  782. default:
  783. if (p_cur->p_value && p_cur->p_value->attr_val.attr_len != 0) {
  784. *length = p_cur->p_value->attr_val.attr_len;
  785. *value = p_cur->p_value->attr_val.attr_val;
  786. return GATT_SUCCESS;
  787. } else {
  788. *length = 0;
  789. return GATT_SUCCESS;
  790. }
  791. break;
  792. }
  793. } else {
  794. if (p_cur->p_value && p_cur->p_value->attr_val.attr_len != 0) {
  795. *length = p_cur->p_value->attr_val.attr_len;
  796. *value = p_cur->p_value->attr_val.attr_val;
  797. return GATT_SUCCESS;
  798. } else {
  799. *length = 0;
  800. return GATT_SUCCESS;
  801. }
  802. }
  803. break;
  804. }
  805. p_cur = p_cur->p_next;
  806. }
  807. return GATT_NOT_FOUND;
  808. }
  809. BOOLEAN gatts_is_auto_response(UINT16 attr_handle)
  810. {
  811. tGATT_HDL_LIST_ELEM *p_decl = NULL;
  812. BOOLEAN rsp = FALSE;
  813. tGATT_SVC_DB *p_db = NULL;
  814. if ((p_decl = gatt_find_hdl_buffer_by_attr_handle(attr_handle)) == NULL) {
  815. GATT_TRACE_DEBUG("Service not created\n");
  816. return rsp;
  817. }
  818. p_db = &p_decl->svc_db;
  819. tGATT_ATTR16 *p_cur, *p_next;
  820. if (p_db == NULL) {
  821. GATT_TRACE_DEBUG("gatts_get_attribute_value Fail:p_db is NULL.\n");
  822. return rsp;
  823. }
  824. if (p_db->p_attr_list == NULL) {
  825. GATT_TRACE_DEBUG("gatts_get_attribute_value Fail:p_db->p_attr_list is NULL.\n");
  826. return rsp;
  827. }
  828. p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
  829. p_next = (tGATT_ATTR16 *) p_cur->p_next;
  830. for (; p_cur != NULL && p_next != NULL;
  831. p_cur = p_next, p_next = (tGATT_ATTR16 *)p_next->p_next) {
  832. if (p_cur->handle == attr_handle) {
  833. if (p_cur->p_value != NULL && p_cur->control.auto_rsp == GATT_RSP_BY_STACK) {
  834. rsp = true;
  835. return rsp;
  836. }
  837. }
  838. }
  839. return rsp;
  840. }
  841. /*******************************************************************************/
  842. /* Service Attribute Database Query Utility Functions */
  843. /*******************************************************************************/
  844. /*******************************************************************************
  845. **
  846. ** Function gatts_read_attr_value_by_handle
  847. **
  848. ** Description Query attribute value by attribute handle.
  849. **
  850. ** Parameter p_db: pointer to the attribute database.
  851. ** handle: Attribute handle to read.
  852. ** offset: Read offset.
  853. ** p_value: output parameter to carry out the attribute value.
  854. ** p_len: output parameter as attribute length read.
  855. ** read_long: this is a read blob request.
  856. ** mtu: MTU.
  857. ** sec_flag: current link security status.
  858. ** key_size: encryption key size
  859. **
  860. ** Returns Status of operation.
  861. **
  862. *******************************************************************************/
  863. tGATT_STATUS gatts_read_attr_value_by_handle(tGATT_TCB *p_tcb,
  864. tGATT_SVC_DB *p_db,
  865. UINT8 op_code,
  866. UINT16 handle, UINT16 offset,
  867. UINT8 *p_value, UINT16 *p_len,
  868. UINT16 mtu,
  869. tGATT_SEC_FLAG sec_flag,
  870. UINT8 key_size,
  871. UINT32 trans_id)
  872. {
  873. tGATT_STATUS status = GATT_NOT_FOUND;
  874. tGATT_ATTR16 *p_attr;
  875. UINT8 *pp = p_value;
  876. if (p_db && p_db->p_attr_list) {
  877. p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
  878. while (p_attr && handle >= p_attr->handle) {
  879. if (p_attr->handle == handle) {
  880. status = read_attr_value (p_attr, offset, &pp,
  881. (BOOLEAN)(op_code == GATT_REQ_READ_BLOB),
  882. mtu, p_len, sec_flag, key_size);
  883. if ((status == GATT_PENDING) || (status == GATT_STACK_RSP)) {
  884. BOOLEAN need_rsp = (status != GATT_STACK_RSP);
  885. status = gatts_send_app_read_request(p_tcb, op_code, p_attr->handle, offset, trans_id, need_rsp);
  886. }
  887. break;
  888. }
  889. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  890. }
  891. }
  892. return status;
  893. }
  894. tGATT_STATUS gatts_write_attr_value_by_handle(tGATT_SVC_DB *p_db,
  895. UINT16 handle, UINT16 offset,
  896. UINT8 *p_value, UINT16 len)
  897. {
  898. tGATT_STATUS status = GATT_NOT_FOUND;
  899. tGATT_ATTR16 *p_attr;
  900. if (p_db && p_db->p_attr_list) {
  901. p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
  902. while (p_attr && handle >= p_attr->handle) {
  903. if (p_attr->handle == handle ) {
  904. if (p_attr->control.auto_rsp == GATT_RSP_BY_APP) {
  905. return GATT_APP_RSP;
  906. }
  907. if ((p_attr->p_value != NULL) &&
  908. (p_attr->p_value->attr_val.attr_max_len >= offset + len) &&
  909. p_attr->p_value->attr_val.attr_val != NULL) {
  910. memcpy(p_attr->p_value->attr_val.attr_val + offset, p_value, len);
  911. p_attr->p_value->attr_val.attr_len = len + offset;
  912. return GATT_SUCCESS;
  913. } else if (p_attr->p_value && p_attr->p_value->attr_val.attr_max_len < offset + len){
  914. GATT_TRACE_DEBUG("Remote device try to write with a length larger then attribute's max length\n");
  915. return GATT_INVALID_ATTR_LEN;
  916. } else if ((p_attr->p_value == NULL) || (p_attr->p_value->attr_val.attr_val == NULL)){
  917. GATT_TRACE_ERROR("Error in %s, line=%d, %s should not be NULL here\n", __func__, __LINE__, \
  918. (p_attr->p_value == NULL) ? "p_value" : "attr_val.attr_val");
  919. return GATT_UNKNOWN_ERROR;
  920. }
  921. }
  922. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  923. }
  924. }
  925. return status;
  926. }
  927. /*******************************************************************************
  928. **
  929. ** Function gatts_read_attr_perm_check
  930. **
  931. ** Description Check attribute readability.
  932. **
  933. ** Parameter p_db: pointer to the attribute database.
  934. ** handle: Attribute handle to read.
  935. ** offset: Read offset.
  936. ** p_value: output parameter to carry out the attribute value.
  937. ** p_len: output parameter as attribute length read.
  938. ** read_long: this is a read blob request.
  939. ** mtu: MTU.
  940. ** sec_flag: current link security status.
  941. ** key_size: encryption key size
  942. **
  943. ** Returns Status of operation.
  944. **
  945. *******************************************************************************/
  946. tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB *p_db,
  947. BOOLEAN is_long,
  948. UINT16 handle,
  949. tGATT_SEC_FLAG sec_flag,
  950. UINT8 key_size)
  951. {
  952. tGATT_STATUS status = GATT_NOT_FOUND;
  953. tGATT_ATTR16 *p_attr;
  954. if (p_db && p_db->p_attr_list) {
  955. p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
  956. while (p_attr && handle >= p_attr->handle) {
  957. if (p_attr->handle == handle) {
  958. status = gatts_check_attr_readability (p_attr, 0,
  959. is_long,
  960. sec_flag, key_size);
  961. break;
  962. }
  963. p_attr = (tGATT_ATTR16 *) p_attr->p_next;
  964. }
  965. }
  966. return status;
  967. }
  968. /*******************************************************************************
  969. **
  970. ** Function gatts_write_attr_perm_check
  971. **
  972. ** Description Write attribute value into database.
  973. **
  974. ** Parameter p_db: pointer to the attribute database.
  975. ** op_code:op code of this write.
  976. ** handle: handle of the attribute to write.
  977. ** offset: Write offset if write op code is write blob.
  978. ** p_data: Attribute value to write.
  979. ** len: attribute data length.
  980. ** sec_flag: current link security status.
  981. ** key_size: encryption key size
  982. **
  983. ** Returns Status of the operation.
  984. **
  985. *******************************************************************************/
  986. tGATT_STATUS gatts_write_attr_perm_check (tGATT_SVC_DB *p_db, UINT8 op_code,
  987. UINT16 handle, UINT16 offset, UINT8 *p_data,
  988. UINT16 len, tGATT_SEC_FLAG sec_flag, UINT8 key_size)
  989. {
  990. tGATT_STATUS status = GATT_NOT_FOUND;
  991. tGATT_ATTR16 *p_attr;
  992. UINT16 max_size = 0;
  993. tGATT_PERM perm;
  994. UINT16 min_key_size;
  995. GATT_TRACE_DEBUG( "gatts_write_attr_perm_check op_code=0x%0x handle=0x%04x offset=%d len=%d sec_flag=0x%0x key_size=%d",
  996. op_code, handle, offset, len, sec_flag, key_size);
  997. if (p_db != NULL) {
  998. p_attr = (tGATT_ATTR16 *) p_db->p_attr_list;
  999. while (p_attr != NULL) {
  1000. if (p_attr->handle == handle) {
  1001. perm = p_attr->permission;
  1002. min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
  1003. if (min_key_size != 0 ) {
  1004. min_key_size += 6;
  1005. }
  1006. GATT_TRACE_DEBUG( "gatts_write_attr_perm_check p_attr->permission =0x%04x min_key_size==0x%04x",
  1007. p_attr->permission,
  1008. min_key_size);
  1009. if ((op_code == GATT_CMD_WRITE || op_code == GATT_REQ_WRITE)
  1010. && (perm & GATT_WRITE_SIGNED_PERM)) {
  1011. /* use the rules for the mixed security see section 10.2.3*/
  1012. /* use security mode 1 level 2 when the following condition follows */
  1013. /* LE security mode 2 level 1 and LE security mode 1 level 2 */
  1014. if ((perm & GATT_PERM_WRITE_SIGNED) && (perm & GATT_PERM_WRITE_ENCRYPTED)) {
  1015. perm = GATT_PERM_WRITE_ENCRYPTED;
  1016. }
  1017. /* use security mode 1 level 3 when the following condition follows */
  1018. /* LE security mode 2 level 2 and security mode 1 and LE */
  1019. else if (((perm & GATT_PERM_WRITE_SIGNED_MITM) && (perm & GATT_PERM_WRITE_ENCRYPTED)) ||
  1020. /* LE security mode 2 and security mode 1 level 3 */
  1021. ((perm & GATT_WRITE_SIGNED_PERM) && (perm & GATT_PERM_WRITE_ENC_MITM))) {
  1022. perm = GATT_PERM_WRITE_ENC_MITM;
  1023. }
  1024. }
  1025. if ((op_code == GATT_SIGN_CMD_WRITE) && !(perm & GATT_WRITE_SIGNED_PERM)) {
  1026. status = GATT_WRITE_NOT_PERMIT;
  1027. GATT_TRACE_DEBUG( "gatts_write_attr_perm_check - sign cmd write not allowed,handle:0x%04x",handle);
  1028. }
  1029. if ((op_code == GATT_SIGN_CMD_WRITE) && (sec_flag & GATT_SEC_FLAG_ENCRYPTED)) {
  1030. status = GATT_INVALID_PDU;
  1031. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - Error!! sign cmd write sent on a encypted link,handle:0x%04x",handle);
  1032. } else if (!(perm & GATT_WRITE_ALLOWED)) {
  1033. status = GATT_WRITE_NOT_PERMIT;
  1034. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_WRITE_NOT_PERMIT,handle:0x%04x",handle);
  1035. }
  1036. /* require authentication, but not been authenticated */
  1037. else if ((perm & GATT_WRITE_AUTH_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_UNAUTHED)) {
  1038. status = GATT_INSUF_AUTHENTICATION;
  1039. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHENTICATION,handle:0x%04x",handle);
  1040. } else if ((perm & GATT_WRITE_MITM_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_AUTHED)) {
  1041. status = GATT_INSUF_AUTHENTICATION;
  1042. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHENTICATION: MITM required,handle:0x%04x",handle);
  1043. } else if ((perm & GATT_WRITE_ENCRYPTED_PERM ) && !(sec_flag & GATT_SEC_FLAG_ENCRYPTED)) {
  1044. status = GATT_INSUF_ENCRYPTION;
  1045. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_ENCRYPTION,handle:0x%04x",handle);
  1046. } else if ((perm & GATT_WRITE_ENCRYPTED_PERM ) && (sec_flag & GATT_SEC_FLAG_ENCRYPTED) && (key_size < min_key_size)) {
  1047. status = GATT_INSUF_KEY_SIZE;
  1048. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_KEY_SIZE,handle:0x%04x",handle);
  1049. }
  1050. /* LE Authorization check*/
  1051. else if ((perm & GATT_WRITE_AUTHORIZATION) && (!(sec_flag & GATT_SEC_FLAG_LKEY_AUTHED) || !(sec_flag & GATT_SEC_FLAG_AUTHORIZATION))){
  1052. status = GATT_INSUF_AUTHORIZATION;
  1053. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHORIZATION,handle:0x%04x",handle);
  1054. }
  1055. /* LE security mode 2 attribute */
  1056. else if (perm & GATT_WRITE_SIGNED_PERM && op_code != GATT_SIGN_CMD_WRITE && !(sec_flag & GATT_SEC_FLAG_ENCRYPTED)
  1057. && (perm & GATT_WRITE_ALLOWED) == 0) {
  1058. status = GATT_INSUF_AUTHENTICATION;
  1059. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHENTICATION: LE security mode 2 required,handle:0x%04x",handle);
  1060. } else { /* writable: must be char value declaration or char descritpors */
  1061. if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  1062. switch (p_attr->uuid) {
  1063. case GATT_UUID_CHAR_PRESENT_FORMAT:/* should be readable only */
  1064. case GATT_UUID_CHAR_EXT_PROP:/* should be readable only */
  1065. case GATT_UUID_CHAR_AGG_FORMAT: /* should be readable only */
  1066. case GATT_UUID_CHAR_VALID_RANGE:
  1067. status = GATT_WRITE_NOT_PERMIT;
  1068. break;
  1069. case GATT_UUID_CHAR_CLIENT_CONFIG:
  1070. /* coverity[MISSING_BREAK] */
  1071. /* intnended fall through, ignored */
  1072. /* fall through */
  1073. case GATT_UUID_CHAR_SRVR_CONFIG:
  1074. max_size = 2;
  1075. case GATT_UUID_CHAR_DESCRIPTION:
  1076. default: /* any other must be character value declaration */
  1077. status = GATT_SUCCESS;
  1078. break;
  1079. }
  1080. } else if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 ||
  1081. p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32) {
  1082. status = GATT_SUCCESS;
  1083. } else {
  1084. status = GATT_INVALID_PDU;
  1085. }
  1086. if (p_data == NULL && len > 0) {
  1087. status = GATT_INVALID_PDU;
  1088. }
  1089. /* these attribute does not allow write blob */
  1090. // btla-specific ++
  1091. else if ( (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) &&
  1092. (p_attr->uuid == GATT_UUID_CHAR_CLIENT_CONFIG ||
  1093. p_attr->uuid == GATT_UUID_CHAR_SRVR_CONFIG) )
  1094. // btla-specific --
  1095. {
  1096. if (op_code == GATT_REQ_PREPARE_WRITE && offset != 0) { /* does not allow write blob */
  1097. status = GATT_NOT_LONG;
  1098. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_NOT_LONG,handle:0x%04x",handle);
  1099. } else if (len != max_size) { /* data does not match the required format */
  1100. status = GATT_INVALID_ATTR_LEN;
  1101. GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INVALID_PDU,handle:0x%04x",handle);
  1102. } else {
  1103. status = GATT_SUCCESS;
  1104. }
  1105. }
  1106. }
  1107. break;
  1108. } else {
  1109. p_attr = (tGATT_ATTR16 *)p_attr->p_next;
  1110. }
  1111. }
  1112. }
  1113. return status;
  1114. }
  1115. /*******************************************************************************
  1116. **
  1117. ** Function allocate_attr_in_db
  1118. **
  1119. ** Description Allocate a memory space for a new attribute, and link this
  1120. ** attribute into the database attribute list.
  1121. **
  1122. **
  1123. ** Parameter p_db : database pointer.
  1124. ** p_uuid: pointer to attribute UUID
  1125. ** service : type of attribute to be added.
  1126. **
  1127. ** Returns pointer to the newly allocated attribute.
  1128. **
  1129. *******************************************************************************/
  1130. static void *allocate_attr_in_db(tGATT_SVC_DB *p_db, tBT_UUID *p_uuid, tGATT_PERM perm)
  1131. {
  1132. tGATT_ATTR16 *p_attr16 = NULL, *p_last;
  1133. tGATT_ATTR32 *p_attr32 = NULL;
  1134. tGATT_ATTR128 *p_attr128 = NULL;
  1135. UINT16 len = sizeof(tGATT_ATTR128);
  1136. if (p_uuid == NULL) {
  1137. GATT_TRACE_ERROR("illegal UUID\n");
  1138. return NULL;
  1139. }
  1140. if (p_uuid->len == LEN_UUID_16) {
  1141. len = sizeof(tGATT_ATTR16);
  1142. } else if (p_uuid->len == LEN_UUID_32) {
  1143. len = sizeof(tGATT_ATTR32);
  1144. }
  1145. GATT_TRACE_DEBUG("allocate attr %d bytes\n", len);
  1146. if (p_db->end_handle <= p_db->next_handle) {
  1147. GATT_TRACE_DEBUG("handle space full. handle_max = %d next_handle = %d\n",
  1148. p_db->end_handle, p_db->next_handle);
  1149. return NULL;
  1150. }
  1151. if (p_db->mem_free < len) {
  1152. if (!allocate_svc_db_buf(p_db)) {
  1153. GATT_TRACE_ERROR("allocate_attr_in_db failed, no resources\n");
  1154. return NULL;
  1155. }
  1156. }
  1157. memset(p_db->p_free_mem, 0, len);
  1158. p_attr16 = (tGATT_ATTR16 *) p_db->p_free_mem;
  1159. if (p_uuid->len == LEN_UUID_16 && p_uuid->uu.uuid16 != GATT_ILLEGAL_UUID) {
  1160. p_attr16->uuid_type = GATT_ATTR_UUID_TYPE_16;
  1161. p_attr16->uuid = p_uuid->uu.uuid16;
  1162. } else if (p_uuid->len == LEN_UUID_32) {
  1163. p_attr32 = (tGATT_ATTR32 *) p_db->p_free_mem;
  1164. p_attr32->uuid_type = GATT_ATTR_UUID_TYPE_32;
  1165. p_attr32->uuid = p_uuid->uu.uuid32;
  1166. } else if (p_uuid->len == LEN_UUID_128) {
  1167. p_attr128 = (tGATT_ATTR128 *) p_db->p_free_mem;
  1168. p_attr128->uuid_type = GATT_ATTR_UUID_TYPE_128;
  1169. memcpy(p_attr128->uuid, p_uuid->uu.uuid128, LEN_UUID_128);
  1170. }
  1171. p_db->p_free_mem += len;
  1172. p_db->mem_free -= len;
  1173. p_attr16->handle = p_db->next_handle++;
  1174. p_attr16->permission = perm;
  1175. p_attr16->p_next = NULL;
  1176. /* link the attribute record into the end of DB */
  1177. if (p_db->p_attr_list == NULL) {
  1178. p_db->p_attr_list = p_attr16;
  1179. } else {
  1180. p_last = (tGATT_ATTR16 *)p_db->p_attr_list;
  1181. while (p_last != NULL && p_last->p_next != NULL) {
  1182. p_last = (tGATT_ATTR16 *)p_last->p_next;
  1183. }
  1184. p_last->p_next = p_attr16;
  1185. }
  1186. if (p_attr16->uuid_type == GATT_ATTR_UUID_TYPE_16) {
  1187. GATT_TRACE_DEBUG("=====> handle = [0x%04x] uuid16 = [0x%04x] perm=0x%02x\n",
  1188. p_attr16->handle, p_attr16->uuid, p_attr16->permission);
  1189. } else if (p_attr16->uuid_type == GATT_ATTR_UUID_TYPE_32) {
  1190. GATT_TRACE_DEBUG("=====> handle = [0x%04x] uuid32 = [0x%08x] perm=0x%02x\n",
  1191. p_attr32->handle, p_attr32->uuid, p_attr32->permission);
  1192. } else {
  1193. GATT_TRACE_DEBUG("=====> handle = [0x%04x] uuid128 = [0x%02x:0x%02x] perm=0x%02x\n",
  1194. p_attr128->handle, p_attr128->uuid[0], p_attr128->uuid[1],
  1195. p_attr128->permission);
  1196. }
  1197. return (void *)p_attr16;
  1198. }
  1199. /*******************************************************************************
  1200. **
  1201. ** Function deallocate_attr_in_db
  1202. **
  1203. ** Description Free an attribute within the database.
  1204. **
  1205. ** Parameter p_db: database pointer.
  1206. ** p_attr: pointer to the attribute record to be freed.
  1207. **
  1208. ** Returns BOOLEAN: success
  1209. **
  1210. *******************************************************************************/
  1211. static BOOLEAN deallocate_attr_in_db(tGATT_SVC_DB *p_db, void *p_attr)
  1212. {
  1213. tGATT_ATTR16 *p_cur, *p_next;
  1214. BOOLEAN found = FALSE;
  1215. if (p_db->p_attr_list == NULL) {
  1216. return found;
  1217. }
  1218. p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
  1219. p_next = (tGATT_ATTR16 *) p_cur->p_next;
  1220. for (; p_cur != NULL && p_next != NULL;
  1221. p_cur = p_next, p_next = (tGATT_ATTR16 *)p_next->p_next) {
  1222. if (p_next == p_attr) {
  1223. p_cur->p_next = p_next->p_next;
  1224. found = TRUE;
  1225. }
  1226. }
  1227. if (p_cur == p_attr && p_cur == p_db->p_attr_list) {
  1228. p_db->p_attr_list = p_cur->p_next;
  1229. found = TRUE;
  1230. }
  1231. /* else attr not found */
  1232. if ( found) {
  1233. p_db->next_handle --;
  1234. }
  1235. return found;
  1236. }
  1237. /*******************************************************************************
  1238. **
  1239. ** Function copy_extra_byte_in_db
  1240. **
  1241. ** Description Utility function to allocate extra bytes memory in DB and copy
  1242. ** the value from a source place.
  1243. **
  1244. **
  1245. ** Parameter p_db: database pointer.
  1246. ** p_dst: destination data pointer.
  1247. ** p_src: source data pointer.
  1248. ** len: data length to be copied.
  1249. **
  1250. ** Returns None.
  1251. **
  1252. *******************************************************************************/
  1253. static BOOLEAN copy_extra_byte_in_db(tGATT_SVC_DB *p_db, void **p_dst, UINT16 len)
  1254. {
  1255. UINT8 *p = (UINT8 *)*p_dst;
  1256. if (p_db->mem_free < len) {
  1257. if (!allocate_svc_db_buf(p_db)) {
  1258. GATT_TRACE_ERROR("copy_extra_byte_in_db failed, no resources\n");
  1259. return FALSE;
  1260. }
  1261. }
  1262. p = p_db->p_free_mem;
  1263. p_db->p_free_mem += len;
  1264. p_db->mem_free -= len;
  1265. memset((void *)p, 0, len);
  1266. *p_dst = (void *)p;
  1267. return TRUE;
  1268. }
  1269. /*******************************************************************************
  1270. **
  1271. ** Function allocate_svc_db_buf
  1272. **
  1273. ** Description Utility function to allocate extra buffer for service database.
  1274. **
  1275. ** Returns TRUE if allocation succeed, otherwise FALSE.
  1276. **
  1277. *******************************************************************************/
  1278. static BOOLEAN allocate_svc_db_buf(tGATT_SVC_DB *p_db)
  1279. {
  1280. BT_HDR *p_buf;
  1281. GATT_TRACE_DEBUG("allocate_svc_db_buf allocating extra buffer");
  1282. if ((p_buf = (BT_HDR *)osi_calloc(GATT_DB_BUF_SIZE)) == NULL) {
  1283. GATT_TRACE_ERROR("allocate_svc_db_buf failed, no resources");
  1284. return FALSE;
  1285. }
  1286. p_db->p_free_mem = (UINT8 *) p_buf;
  1287. p_db->mem_free = GATT_DB_BUF_SIZE;
  1288. fixed_queue_enqueue(p_db->svc_buffer, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  1289. return TRUE;
  1290. }
  1291. /*******************************************************************************
  1292. **
  1293. ** Function gatts_send_app_read_request
  1294. **
  1295. ** Description Send application read request callback
  1296. **
  1297. ** Returns status of operation.
  1298. **
  1299. *******************************************************************************/
  1300. static tGATT_STATUS gatts_send_app_read_request(tGATT_TCB *p_tcb, UINT8 op_code,
  1301. UINT16 handle, UINT16 offset, UINT32 trans_id, BOOLEAN need_rsp)
  1302. {
  1303. tGATTS_DATA sr_data;
  1304. UINT8 i_rcb;
  1305. tGATT_SR_REG *p_sreg;
  1306. UINT16 conn_id;
  1307. i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
  1308. if (i_rcb == GATT_MAX_SR_PROFILES) {
  1309. GATT_TRACE_ERROR("Failed to find i_rcb,Error in %s, line=%d, \n", __func__, __LINE__);
  1310. return (tGATT_STATUS) GATT_ERROR;
  1311. }
  1312. p_sreg = &gatt_cb.sr_reg[i_rcb];
  1313. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
  1314. if (trans_id == 0) {
  1315. trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
  1316. gatt_sr_update_cback_cnt(p_tcb, p_sreg->gatt_if, TRUE, TRUE);
  1317. }
  1318. if (trans_id != 0 ) {
  1319. memset(&sr_data, 0, sizeof(tGATTS_DATA));
  1320. sr_data.read_req.handle = handle;
  1321. sr_data.read_req.is_long = (BOOLEAN)(op_code == GATT_REQ_READ_BLOB);
  1322. sr_data.read_req.offset = offset;
  1323. sr_data.read_req.need_rsp = need_rsp;
  1324. gatt_sr_send_req_callback(conn_id,
  1325. trans_id, GATTS_REQ_TYPE_READ, &sr_data);
  1326. if (need_rsp) {
  1327. return (tGATT_STATUS) GATT_PENDING;
  1328. }
  1329. else{
  1330. return (tGATT_STATUS) GATT_STACK_RSP;
  1331. }
  1332. } else {
  1333. return (tGATT_STATUS) GATT_BUSY; /* max pending command, application error */
  1334. }
  1335. }
  1336. /*******************************************************************************
  1337. **
  1338. ** Function gatts_db_add_service_declaration
  1339. **
  1340. ** Description Update a service database service declaration record.
  1341. **
  1342. ** Parameter p_db: database pointer.
  1343. ** service: UUID of the service.
  1344. **
  1345. ** Returns void
  1346. **
  1347. *******************************************************************************/
  1348. static BOOLEAN gatts_db_add_service_declaration(tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri)
  1349. {
  1350. tGATT_ATTR16 *p_attr;
  1351. tBT_UUID uuid = {LEN_UUID_16, {0}};
  1352. BOOLEAN rt = FALSE;
  1353. GATT_TRACE_DEBUG( "add_service_declaration");
  1354. if (is_pri) {
  1355. uuid.uu.uuid16 = GATT_UUID_PRI_SERVICE;
  1356. } else {
  1357. uuid.uu.uuid16 = GATT_UUID_SEC_SERVICE;
  1358. }
  1359. /* add service declration record */
  1360. if ((p_attr = (tGATT_ATTR16 *)(allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ))) != NULL) {
  1361. if (copy_extra_byte_in_db (p_db, (void **)&p_attr->p_value, sizeof(tBT_UUID))) {
  1362. if (p_service->len == LEN_UUID_16) {
  1363. p_attr->p_value->uuid.len = LEN_UUID_16;
  1364. p_attr->p_value->uuid.uu.uuid16 = p_service->uu.uuid16;
  1365. } else if (p_service->len == LEN_UUID_32) {
  1366. p_attr->p_value->uuid.len = LEN_UUID_128;
  1367. gatt_convert_uuid32_to_uuid128(p_attr->p_value->uuid.uu.uuid128, p_service->uu.uuid32);
  1368. } else {
  1369. p_attr->p_value->uuid.len = LEN_UUID_128;
  1370. memcpy(p_attr->p_value->uuid.uu.uuid128, p_service->uu.uuid128, LEN_UUID_128);
  1371. }
  1372. rt = TRUE;
  1373. }
  1374. }
  1375. return rt;
  1376. }
  1377. /*******************************************************************************
  1378. **
  1379. ** Function gatts_add_char_desc_value_check
  1380. **
  1381. ** Description parameters validation check for gatts add char/descriptor functions
  1382. **
  1383. ** Parameter attr_val: attribute value for char/descriptor.
  1384. ** control: control variable for char/descriptor.
  1385. **
  1386. ** Returns void
  1387. **
  1388. *******************************************************************************/
  1389. static BOOLEAN gatts_add_char_desc_value_check (tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
  1390. {
  1391. if ((control != NULL) && ((control->auto_rsp != GATT_RSP_BY_APP) && (control->auto_rsp != GATT_RSP_BY_STACK))){
  1392. GATT_TRACE_ERROR("Error in %s, line=%d, control->auto_rsp should be set to GATT_RSP_BY_APP or GATT_RSP_BY_STACK here\n",\
  1393. __func__, __LINE__);
  1394. return FALSE;
  1395. }
  1396. if ((control != NULL) && (control->auto_rsp == GATT_RSP_BY_STACK)){
  1397. if (attr_val == NULL){
  1398. GATT_TRACE_ERROR("Error in %s, line=%d, for stack respond attribute, attr_val should not be NULL here\n",\
  1399. __func__, __LINE__);
  1400. return FALSE;
  1401. } else if (attr_val->attr_max_len == 0){
  1402. GATT_TRACE_ERROR("Error in %s, line=%d, for stack respond attribute, attribute max length should not be 0\n",\
  1403. __func__, __LINE__);
  1404. return FALSE;
  1405. }
  1406. }
  1407. if (attr_val != NULL){
  1408. if (attr_val->attr_len > attr_val->attr_max_len){
  1409. GATT_TRACE_ERROR("Error in %s, line=%d,attribute actual length should not be larger than max length\n",\
  1410. __func__, __LINE__);
  1411. return FALSE;
  1412. }
  1413. }
  1414. return TRUE ;
  1415. }
  1416. #endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */