gatt_db.c 59 KB

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