gatt_db.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. if (i_rcb == GATT_MAX_SR_PROFILES) {
  1256. GATT_TRACE_ERROR("Failed to find i_rcb,Error in %s, line=%d, \n", __func__, __LINE__);
  1257. return (tGATT_STATUS) GATT_ERROR;
  1258. }
  1259. p_sreg = &gatt_cb.sr_reg[i_rcb];
  1260. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
  1261. if (trans_id == 0) {
  1262. trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
  1263. gatt_sr_update_cback_cnt(p_tcb, p_sreg->gatt_if, TRUE, TRUE);
  1264. }
  1265. if (trans_id != 0 ) {
  1266. memset(&sr_data, 0, sizeof(tGATTS_DATA));
  1267. sr_data.read_req.handle = handle;
  1268. sr_data.read_req.is_long = (BOOLEAN)(op_code == GATT_REQ_READ_BLOB);
  1269. sr_data.read_req.offset = offset;
  1270. sr_data.read_req.need_rsp = need_rsp;
  1271. gatt_sr_send_req_callback(conn_id,
  1272. trans_id, GATTS_REQ_TYPE_READ, &sr_data);
  1273. if (need_rsp) {
  1274. return (tGATT_STATUS) GATT_PENDING;
  1275. }
  1276. else{
  1277. return (tGATT_STATUS) GATT_STACK_RSP;
  1278. }
  1279. } else {
  1280. return (tGATT_STATUS) GATT_BUSY; /* max pending command, application error */
  1281. }
  1282. }
  1283. /*******************************************************************************
  1284. **
  1285. ** Function gatts_db_add_service_declaration
  1286. **
  1287. ** Description Update a service database service declaration record.
  1288. **
  1289. ** Parameter p_db: database pointer.
  1290. ** service: UUID of the service.
  1291. **
  1292. ** Returns void
  1293. **
  1294. *******************************************************************************/
  1295. static BOOLEAN gatts_db_add_service_declaration(tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri)
  1296. {
  1297. tGATT_ATTR16 *p_attr;
  1298. tBT_UUID uuid = {LEN_UUID_16, {0}};
  1299. BOOLEAN rt = FALSE;
  1300. GATT_TRACE_DEBUG( "add_service_declaration");
  1301. if (is_pri) {
  1302. uuid.uu.uuid16 = GATT_UUID_PRI_SERVICE;
  1303. } else {
  1304. uuid.uu.uuid16 = GATT_UUID_SEC_SERVICE;
  1305. }
  1306. /* add service declration record */
  1307. if ((p_attr = (tGATT_ATTR16 *)(allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ))) != NULL) {
  1308. if (copy_extra_byte_in_db (p_db, (void **)&p_attr->p_value, sizeof(tBT_UUID))) {
  1309. if (p_service->len == LEN_UUID_16) {
  1310. p_attr->p_value->uuid.len = LEN_UUID_16;
  1311. p_attr->p_value->uuid.uu.uuid16 = p_service->uu.uuid16;
  1312. } else if (p_service->len == LEN_UUID_32) {
  1313. p_attr->p_value->uuid.len = LEN_UUID_128;
  1314. gatt_convert_uuid32_to_uuid128(p_attr->p_value->uuid.uu.uuid128, p_service->uu.uuid32);
  1315. } else {
  1316. p_attr->p_value->uuid.len = LEN_UUID_128;
  1317. memcpy(p_attr->p_value->uuid.uu.uuid128, p_service->uu.uuid128, LEN_UUID_128);
  1318. }
  1319. rt = TRUE;
  1320. }
  1321. }
  1322. return rt;
  1323. }
  1324. /*******************************************************************************
  1325. **
  1326. ** Function gatts_add_char_desc_value_check
  1327. **
  1328. ** Description parameters validation check for gatts add char/descriptor functions
  1329. **
  1330. ** Parameter attr_val: attribute value for char/descriptor.
  1331. ** control: control variable for char/descriptor.
  1332. **
  1333. ** Returns void
  1334. **
  1335. *******************************************************************************/
  1336. static BOOLEAN gatts_add_char_desc_value_check (tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
  1337. {
  1338. if ((control != NULL) && ((control->auto_rsp != GATT_RSP_BY_APP) && (control->auto_rsp != GATT_RSP_BY_STACK))){
  1339. 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",\
  1340. __func__, __LINE__);
  1341. return FALSE;
  1342. }
  1343. if ((control != NULL) && (control->auto_rsp == GATT_RSP_BY_STACK)){
  1344. if (attr_val == NULL){
  1345. GATT_TRACE_ERROR("Error in %s, line=%d, for stack respond attribute, attr_val should not be NULL here\n",\
  1346. __func__, __LINE__);
  1347. return FALSE;
  1348. } else if (attr_val->attr_max_len == 0){
  1349. GATT_TRACE_ERROR("Error in %s, line=%d, for stack respond attribute, attribute max length should not be 0\n",\
  1350. __func__, __LINE__);
  1351. return FALSE;
  1352. }
  1353. }
  1354. if (attr_val != NULL){
  1355. if (attr_val->attr_len > attr_val->attr_max_len){
  1356. GATT_TRACE_ERROR("Error in %s, line=%d,attribute actual length should not be larger than max length\n",\
  1357. __func__, __LINE__);
  1358. return FALSE;
  1359. }
  1360. }
  1361. return TRUE ;
  1362. }
  1363. #endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */