gatt_api.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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 interface functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
  25. #include "osi/allocator.h"
  26. #include <string.h>
  27. #include "stack/gatt_api.h"
  28. #include "gatt_int.h"
  29. #include "stack/l2c_api.h"
  30. #include "btm_int.h"
  31. #include "stack/sdpdefs.h"
  32. #include "stack/sdp_api.h"
  33. /*******************************************************************************
  34. **
  35. ** Function GATT_SetTraceLevel
  36. **
  37. ** Description This function sets the trace level. If called with
  38. ** a value of 0xFF, it simply returns the current trace level.
  39. **
  40. ** Input Parameters:
  41. ** level: The level to set the GATT tracing to:
  42. ** 0xff-returns the current setting.
  43. ** 0-turns off tracing.
  44. ** >= 1-Errors.
  45. ** >= 2-Warnings.
  46. ** >= 3-APIs.
  47. ** >= 4-Events.
  48. ** >= 5-Debug.
  49. **
  50. ** Returns The new or current trace level
  51. **
  52. *******************************************************************************/
  53. UINT8 GATT_SetTraceLevel (UINT8 new_level)
  54. {
  55. if (new_level != 0xFF) {
  56. gatt_cb.trace_level = new_level;
  57. }
  58. return (gatt_cb.trace_level);
  59. }
  60. #if (GATTS_INCLUDED == TRUE)
  61. /*****************************************************************************
  62. **
  63. ** GATT SERVER API
  64. **
  65. ******************************************************************************/
  66. /*******************************************************************************
  67. **
  68. ** Function GATTS_AddHandleRange
  69. **
  70. ** Description This function add the allocated handles range for the specifed
  71. ** application UUID, service UUID and service instance
  72. **
  73. ** Parameter p_hndl_range: pointer to allocated handles information
  74. **
  75. ** Returns TRUE if handle range is added successfully; otherwise FALSE.
  76. **
  77. *******************************************************************************/
  78. BOOLEAN GATTS_AddHandleRange(tGATTS_HNDL_RANGE *p_hndl_range)
  79. {
  80. tGATT_HDL_LIST_ELEM *p_buf;
  81. BOOLEAN status = FALSE;
  82. if ((p_buf = gatt_alloc_hdl_buffer()) != NULL) {
  83. p_buf->asgn_range = *p_hndl_range;
  84. status = gatt_add_an_item_to_list(&gatt_cb.hdl_list_info, p_buf);
  85. }
  86. return status;
  87. }
  88. /*******************************************************************************
  89. **
  90. ** Function GATTS_NVRegister
  91. **
  92. ** Description Application manager calls this function to register for
  93. ** NV save callback function. There can be one and only one
  94. ** NV save callback function.
  95. **
  96. ** Parameter p_cb_info : callback informaiton
  97. **
  98. ** Returns TRUE if registered OK, else FALSE
  99. **
  100. *******************************************************************************/
  101. BOOLEAN GATTS_NVRegister (const tGATT_APPL_INFO *p_cb_info)
  102. {
  103. BOOLEAN status = FALSE;
  104. if (p_cb_info) {
  105. gatt_cb.cb_info = *p_cb_info;
  106. status = TRUE;
  107. gatt_init_srv_chg();
  108. }
  109. return status;
  110. }
  111. /*******************************************************************************
  112. **
  113. ** Function GATTS_CreateService
  114. **
  115. ** Description This function is called to reserve a block of handles for a service.
  116. **
  117. ** *** It should be called only once per service instance ***
  118. **
  119. ** Parameter gatt_if : application if
  120. ** p_svc_uuid : service UUID
  121. ** svc_inst : instance of the service inside the application
  122. ** num_handles : number of handles needed by the service.
  123. ** is_pri : is a primary service or not.
  124. **
  125. ** Returns service handle if sucessful, otherwise 0.
  126. **
  127. *******************************************************************************/
  128. UINT16 GATTS_CreateService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid,
  129. UINT16 svc_inst, UINT16 num_handles, BOOLEAN is_pri)
  130. {
  131. tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
  132. tGATT_HDL_LIST_ELEM *p_list = NULL;
  133. UINT16 s_hdl = 0;
  134. BOOLEAN save_hdl = FALSE;
  135. tGATTS_PENDING_NEW_SRV_START *p_buf = NULL;
  136. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  137. tBT_UUID *p_app_uuid128;
  138. GATT_TRACE_API ("GATTS_CreateService\n" );
  139. if (p_reg == NULL) {
  140. GATT_TRACE_ERROR ("Inavlid gatt_if=%d\n", gatt_if);
  141. return (0);
  142. }
  143. p_app_uuid128 = &p_reg->app_uuid128;
  144. if ((p_list = gatt_find_hdl_buffer_by_app_id(p_app_uuid128, p_svc_uuid, svc_inst)) != NULL) {
  145. s_hdl = p_list->asgn_range.s_handle;
  146. GATT_TRACE_DEBUG ("Service already been created!!\n");
  147. } else {
  148. if ( (p_svc_uuid->len == LEN_UUID_16) && (p_svc_uuid->uu.uuid16 == UUID_SERVCLASS_GATT_SERVER)) {
  149. s_hdl = gatt_cb.hdl_cfg.gatt_start_hdl;
  150. } else if ((p_svc_uuid->len == LEN_UUID_16) && (p_svc_uuid->uu.uuid16 == UUID_SERVCLASS_GAP_SERVER)) {
  151. s_hdl = gatt_cb.hdl_cfg.gap_start_hdl;
  152. } else {
  153. p_list = p_list_info->p_first;
  154. if (p_list) {
  155. s_hdl = p_list->asgn_range.e_handle + 1;
  156. }
  157. if (s_hdl < gatt_cb.hdl_cfg.app_start_hdl) {
  158. s_hdl = gatt_cb.hdl_cfg.app_start_hdl;
  159. }
  160. save_hdl = TRUE;
  161. }
  162. /* check for space */
  163. if (num_handles > (0xFFFF - s_hdl + 1)) {
  164. GATT_TRACE_ERROR ("GATTS_ReserveHandles: no handles, s_hdl: %u needed: %u\n", s_hdl, num_handles);
  165. return (0);
  166. }
  167. if ( (p_list = gatt_alloc_hdl_buffer()) == NULL) {
  168. /* No free entry */
  169. GATT_TRACE_ERROR ("GATTS_ReserveHandles: no free handle blocks\n");
  170. return (0);
  171. }
  172. p_list->asgn_range.app_uuid128 = *p_app_uuid128;
  173. p_list->asgn_range.svc_uuid = *p_svc_uuid;
  174. p_list->asgn_range.svc_inst = svc_inst;
  175. p_list->asgn_range.s_handle = s_hdl;
  176. p_list->asgn_range.e_handle = s_hdl + num_handles - 1;
  177. p_list->asgn_range.is_primary = is_pri;
  178. gatt_add_an_item_to_list(p_list_info, p_list);
  179. if (save_hdl) {
  180. if (gatt_cb.cb_info.p_nv_save_callback) {
  181. (*gatt_cb.cb_info.p_nv_save_callback)(TRUE, &p_list->asgn_range);
  182. }
  183. /* add a pending new service change item to the list */
  184. if ( (p_buf = gatt_add_pending_new_srv_start(&p_list->asgn_range)) == NULL) {
  185. /* No free entry */
  186. GATT_TRACE_ERROR ("gatt_add_pending_new_srv_start: no free blocks\n");
  187. if (p_list) {
  188. gatt_remove_an_item_from_list(p_list_info, p_list);
  189. gatt_free_attr_value_buffer(p_list);
  190. gatt_free_hdl_buffer(p_list);
  191. }
  192. return (0);
  193. }
  194. GATT_TRACE_DEBUG ("Add a new srv chg item\n");
  195. }
  196. }
  197. if (!gatts_init_service_db(&p_list->svc_db, p_svc_uuid, is_pri, s_hdl , num_handles)) {
  198. GATT_TRACE_ERROR ("GATTS_ReserveHandles: service DB initialization failed\n");
  199. if (p_list) {
  200. gatt_remove_an_item_from_list(p_list_info, p_list);
  201. gatt_free_attr_value_buffer(p_list);
  202. gatt_free_hdl_buffer(p_list);
  203. }
  204. if (p_buf) {
  205. osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
  206. }
  207. return (0);
  208. }
  209. return (s_hdl);
  210. }
  211. /*******************************************************************************
  212. **
  213. ** Function GATTS_AddIncludeService
  214. **
  215. ** Description This function is called to add an included service.
  216. **
  217. ** Parameter service_handle : To which service this included service is added to.
  218. ** include_svc_handle : included service handle.
  219. **
  220. ** Returns included service attribute handle. If 0, add included service
  221. ** fail.
  222. **
  223. *******************************************************************************/
  224. UINT16 GATTS_AddIncludeService (UINT16 service_handle, UINT16 include_svc_handle)
  225. {
  226. tGATT_HDL_LIST_ELEM *p_decl, *p_incl_decl;
  227. if ((p_decl = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
  228. GATT_TRACE_DEBUG("Service not created");
  229. return 0;
  230. }
  231. if ((p_incl_decl = gatt_find_hdl_buffer_by_handle(include_svc_handle)) == NULL) {
  232. GATT_TRACE_DEBUG("Included Service not created");
  233. return 0;
  234. }
  235. return gatts_add_included_service(&p_decl->svc_db,
  236. p_incl_decl->asgn_range.s_handle,
  237. p_incl_decl->asgn_range.e_handle,
  238. p_incl_decl->asgn_range.svc_uuid);
  239. }
  240. /*******************************************************************************
  241. **
  242. ** Function GATTS_AddCharacteristic
  243. **
  244. ** Description This function is called to add a characteristic into a service.
  245. ** It will add a characteristic declaration and characteristic
  246. ** value declaration into the service database identified by the
  247. ** service handle.
  248. **
  249. ** Parameter service_handle : To which service this included service is added to.
  250. ** char_uuid : Characteristic UUID.
  251. ** perm : Characteristic value declaration attribute permission.
  252. ** property : Characteristic Properties
  253. **
  254. ** Returns Characteristic value declaration attribute handle. 0 if failed.
  255. **
  256. *******************************************************************************/
  257. UINT16 GATTS_AddCharacteristic (UINT16 service_handle, tBT_UUID *p_char_uuid,
  258. tGATT_PERM perm, tGATT_CHAR_PROP property,
  259. tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
  260. {
  261. tGATT_HDL_LIST_ELEM *p_decl;
  262. if ((p_decl = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
  263. GATT_TRACE_DEBUG("Service not created\n");
  264. return 0;
  265. }
  266. /* data validity checking */
  267. if ( ((property & GATT_CHAR_PROP_BIT_AUTH) && !(perm & GATT_WRITE_SIGNED_PERM)) ||
  268. ((perm & GATT_WRITE_SIGNED_PERM) && !(property & GATT_CHAR_PROP_BIT_AUTH)) ) {
  269. GATT_TRACE_DEBUG("Invalid configuration property=0x%x perm=0x%x\n ", property, perm);
  270. return 0;
  271. }
  272. return gatts_add_characteristic(&p_decl->svc_db,
  273. perm,
  274. property,
  275. p_char_uuid,
  276. attr_val, control);
  277. }
  278. /*******************************************************************************
  279. **
  280. ** Function GATTS_AddCharDescriptor
  281. **
  282. ** Description This function is called to add a characteristic descriptor
  283. ** into a service database. Add descriptor should follow add char
  284. ** to which it belongs, and next add char should be done only
  285. ** after all add descriptors for the previous char.
  286. **
  287. ** Parameter service_handle : To which service this characteristic descriptor
  288. ** is added to.
  289. ** perm : Characteristic value declaration attribute
  290. ** permission.
  291. ** p_descr_uuid : Characteristic descriptor UUID
  292. **
  293. ** Returns Characteristic descriptor attribute handle. 0 if add
  294. ** characteristic descriptor failed.
  295. **
  296. *******************************************************************************/
  297. UINT16 GATTS_AddCharDescriptor (UINT16 service_handle,
  298. tGATT_PERM perm,
  299. tBT_UUID *p_descr_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
  300. {
  301. tGATT_HDL_LIST_ELEM *p_decl;
  302. if ((p_decl = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
  303. GATT_TRACE_DEBUG("Service not created");
  304. return 0;
  305. }
  306. if (p_descr_uuid == NULL ||
  307. (p_descr_uuid->len != LEN_UUID_128 && p_descr_uuid->len != LEN_UUID_16
  308. && p_descr_uuid->len != LEN_UUID_32)) {
  309. GATT_TRACE_DEBUG("Illegal parameter");
  310. return 0;
  311. }
  312. return gatts_add_char_descr(&p_decl->svc_db,
  313. perm,
  314. p_descr_uuid,
  315. attr_val, control);
  316. }
  317. /*******************************************************************************
  318. **
  319. ** Function GATTS_DeleteService
  320. **
  321. ** Description This function is called to delete a service.
  322. **
  323. ** Parameter gatt_if : application interface
  324. ** p_svc_uuid : service UUID
  325. ** svc_inst : instance of the service inside the application
  326. **
  327. ** Returns TRUE if operation succeed, FALSE if handle block was not found.
  328. **
  329. *******************************************************************************/
  330. BOOLEAN GATTS_DeleteService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, UINT16 svc_inst)
  331. {
  332. tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
  333. tGATT_HDL_LIST_ELEM *p_list = NULL;
  334. UINT8 i_sreg;
  335. tGATTS_PENDING_NEW_SRV_START *p_buf;
  336. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  337. tBT_UUID *p_app_uuid128;
  338. GATT_TRACE_DEBUG ("GATTS_DeleteService");
  339. if (p_reg == NULL) {
  340. GATT_TRACE_ERROR ("Application not foud");
  341. return (FALSE);
  342. }
  343. p_app_uuid128 = &p_reg->app_uuid128;
  344. if ((p_list = gatt_find_hdl_buffer_by_app_id(p_app_uuid128, p_svc_uuid, svc_inst)) == NULL) {
  345. GATT_TRACE_ERROR ("No Service found");
  346. return (FALSE);
  347. }
  348. if ( (p_buf = gatt_sr_is_new_srv_chg(&p_list->asgn_range.app_uuid128,
  349. &p_list->asgn_range.svc_uuid,
  350. p_list->asgn_range.svc_inst)) != NULL) {
  351. GATT_TRACE_DEBUG ("Delete a new service changed item - the service has not yet started");
  352. osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
  353. } else {
  354. if (GATTS_SEND_SERVICE_CHANGE_MODE == GATTS_SEND_SERVICE_CHANGE_AUTO) {
  355. gatt_proc_srv_chg();
  356. }
  357. }
  358. if ((i_sreg = gatt_sr_find_i_rcb_by_app_id (p_app_uuid128,
  359. p_svc_uuid,
  360. svc_inst)) != GATT_MAX_SR_PROFILES) {
  361. GATTS_StopService(gatt_cb.sr_reg[i_sreg].s_hdl);
  362. }
  363. GATT_TRACE_DEBUG ("released handles s_hdl=%u e_hdl=%u",
  364. p_list->asgn_range.s_handle , p_list->asgn_range.e_handle );
  365. if ( (p_list->asgn_range.s_handle >= gatt_cb.hdl_cfg.app_start_hdl)
  366. && gatt_cb.cb_info.p_nv_save_callback) {
  367. (*gatt_cb.cb_info.p_nv_save_callback)(FALSE, &p_list->asgn_range);
  368. }
  369. gatt_remove_an_item_from_list(p_list_info, p_list);
  370. gatt_free_attr_value_buffer(p_list);
  371. gatt_free_hdl_buffer(p_list);
  372. return (TRUE);
  373. }
  374. /*******************************************************************************
  375. **
  376. ** Function GATTS_StartService
  377. **
  378. ** Description This function is called to start a service with GATT
  379. **
  380. ** Parameter gatt_if : service handle.
  381. ** p_cback : application service callback functions.
  382. ** sup_transport : supported transport(s) for this primary service
  383. **
  384. ** return GATT_SUCCESS if successfully started; otherwise error code.
  385. **
  386. *******************************************************************************/
  387. tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle,
  388. tGATT_TRANSPORT sup_transport)
  389. {
  390. tGATT_SR_REG *p_sreg;
  391. tGATT_HDL_LIST_ELEM *p_list = NULL;
  392. UINT8 i_sreg;
  393. #if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
  394. tBT_UUID *p_uuid;
  395. #endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE
  396. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  397. tGATTS_PENDING_NEW_SRV_START *p_buf;
  398. GATT_TRACE_API ("GATTS_StartService");
  399. if (p_reg == NULL) {
  400. /* Not found */
  401. GATT_TRACE_ERROR ("Application not found ");
  402. return GATT_NOT_FOUND;
  403. }
  404. if ((p_list = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
  405. /* Not found */
  406. GATT_TRACE_ERROR ("no service found");
  407. return GATT_NOT_FOUND;
  408. }
  409. if (gatt_sr_find_i_rcb_by_app_id (&p_list->asgn_range.app_uuid128,
  410. &p_list->asgn_range.svc_uuid,
  411. p_list->asgn_range.svc_inst) != GATT_MAX_SR_PROFILES) {
  412. GATT_TRACE_ERROR ("Duplicate Service start - Service already started");
  413. return GATT_SERVICE_STARTED;
  414. }
  415. /*this is a new application servoce start */
  416. if ((i_sreg = gatt_sr_alloc_rcb(p_list)) == GATT_MAX_SR_PROFILES) {
  417. GATT_TRACE_ERROR ("GATTS_StartService: no free server registration block");
  418. return GATT_NO_RESOURCES;
  419. }
  420. p_sreg = &gatt_cb.sr_reg[i_sreg];
  421. p_sreg->gatt_if = gatt_if;
  422. switch (sup_transport) {
  423. case GATT_TRANSPORT_BR_EDR:
  424. case GATT_TRANSPORT_LE_BR_EDR:
  425. if (p_sreg->type == GATT_UUID_PRI_SERVICE) {
  426. #if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
  427. p_uuid = gatts_get_service_uuid (p_sreg->p_db);
  428. p_sreg->sdp_handle = gatt_add_sdp_record(p_uuid, p_sreg->s_hdl, p_sreg->e_hdl);
  429. #endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE
  430. }
  431. break;
  432. default:
  433. break;
  434. }
  435. gatts_update_srv_list_elem(i_sreg, p_sreg->s_hdl,
  436. p_list->asgn_range.is_primary);
  437. gatt_add_a_srv_to_list(&gatt_cb.srv_list_info, &gatt_cb.srv_list[i_sreg]);
  438. GATT_TRACE_DEBUG ("allocated i_sreg=%d\n", i_sreg);
  439. GATT_TRACE_DEBUG ("s_hdl=%d e_hdl=%d type=0x%x svc_inst=%d sdp_hdl=0x%x\n",
  440. p_sreg->s_hdl, p_sreg->e_hdl,
  441. p_sreg->type, p_sreg->service_instance,
  442. p_sreg->sdp_handle);
  443. if ( (p_buf = gatt_sr_is_new_srv_chg(&p_list->asgn_range.app_uuid128,
  444. &p_list->asgn_range.svc_uuid,
  445. p_list->asgn_range.svc_inst)) != NULL) {
  446. if (GATTS_SEND_SERVICE_CHANGE_MODE == GATTS_SEND_SERVICE_CHANGE_AUTO) {
  447. gatt_proc_srv_chg();
  448. }
  449. /* remove the new service element after the srv changed processing is completed*/
  450. osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
  451. }
  452. return GATT_SUCCESS;
  453. }
  454. /*******************************************************************************
  455. **
  456. ** Function GATTS_StopService
  457. **
  458. ** Description This function is called to stop a service
  459. **
  460. ** Parameter service_handle : this is the start handle of a service
  461. **
  462. ** Returns None.
  463. **
  464. *******************************************************************************/
  465. void GATTS_StopService (UINT16 service_handle)
  466. {
  467. UINT8 ii = gatt_sr_find_i_rcb_by_handle(service_handle);
  468. GATT_TRACE_API("GATTS_StopService %u", service_handle);
  469. /* Index 0 is reserved for GATT, and is never stopped */
  470. if ( (ii > 0) && (ii < GATT_MAX_SR_PROFILES) && (gatt_cb.sr_reg[ii].in_use) ) {
  471. #if(SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
  472. if (gatt_cb.sr_reg[ii].sdp_handle) {
  473. SDP_DeleteRecord(gatt_cb.sr_reg[ii].sdp_handle);
  474. }
  475. #endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE
  476. gatt_remove_a_srv_from_list(&gatt_cb.srv_list_info, &gatt_cb.srv_list[ii]);
  477. gatt_cb.srv_list[ii].in_use = FALSE;
  478. memset (&gatt_cb.sr_reg[ii], 0, sizeof(tGATT_SR_REG));
  479. } else {
  480. GATT_TRACE_ERROR("GATTS_StopService service_handle: %u is not in use", service_handle);
  481. }
  482. }
  483. /*******************************************************************************
  484. **
  485. ** Function GATTs_HandleValueIndication
  486. **
  487. ** Description This function sends a handle value indication to a client.
  488. **
  489. ** Parameter conn_id: connection identifier.
  490. ** attr_handle: Attribute handle of this handle value indication.
  491. ** val_len: Length of the indicated attribute value.
  492. ** p_val: Pointer to the indicated attribute value data.
  493. **
  494. ** Returns GATT_SUCCESS if successfully sent or queued; otherwise error code.
  495. **
  496. *******************************************************************************/
  497. tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_handle, UINT16 val_len, UINT8 *p_val)
  498. {
  499. tGATT_STATUS cmd_status = GATT_NO_RESOURCES;
  500. tGATT_VALUE indication;
  501. BT_HDR *p_msg;
  502. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  503. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  504. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  505. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  506. GATT_TRACE_API ("GATTS_HandleValueIndication");
  507. if ( (p_reg == NULL) || (p_tcb == NULL)) {
  508. GATT_TRACE_ERROR ("GATTS_HandleValueIndication Unknown conn_id: %u ", conn_id);
  509. return (tGATT_STATUS) GATT_INVALID_CONN_ID;
  510. }
  511. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  512. GATT_TRACE_ERROR("connection not established\n");
  513. return GATT_WRONG_STATE;
  514. }
  515. if (! GATT_HANDLE_IS_VALID (attr_handle)) {
  516. return GATT_ILLEGAL_PARAMETER;
  517. }
  518. indication.conn_id = conn_id;
  519. indication.handle = attr_handle;
  520. indication.len = val_len;
  521. memcpy (indication.value, p_val, val_len);
  522. indication.auth_req = GATT_AUTH_REQ_NONE;
  523. if (GATT_HANDLE_IS_VALID(p_tcb->indicate_handle)) {
  524. /* TODO: need to further check whether deleting pending queue here cause reducing transport performance */
  525. /*
  526. GATT_TRACE_DEBUG ("Add a pending indication");
  527. if ((p_buf = gatt_add_pending_ind(p_tcb, &indication)) != NULL) {
  528. cmd_status = GATT_SUCCESS;
  529. } else {
  530. cmd_status = GATT_NO_RESOURCES;
  531. }
  532. */
  533. return GATT_BUSY;
  534. } else {
  535. if ( (p_msg = attp_build_sr_msg (p_tcb, GATT_HANDLE_VALUE_IND, (tGATT_SR_MSG *)&indication)) != NULL) {
  536. cmd_status = attp_send_sr_msg (p_tcb, p_msg);
  537. if (cmd_status == GATT_SUCCESS || cmd_status == GATT_CONGESTED) {
  538. p_tcb->indicate_handle = indication.handle;
  539. gatt_start_conf_timer(p_tcb);
  540. }
  541. }
  542. }
  543. return cmd_status;
  544. }
  545. /*******************************************************************************
  546. **
  547. ** Function GATTS_HandleValueNotification
  548. **
  549. ** Description This function sends a handle value notification to a client.
  550. **
  551. ** Parameter conn_id: connection identifier.
  552. ** attr_handle: Attribute handle of this handle value indication.
  553. ** val_len: Length of the indicated attribute value.
  554. ** p_val: Pointer to the indicated attribute value data.
  555. **
  556. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  557. **
  558. *******************************************************************************/
  559. tGATT_STATUS GATTS_HandleValueNotification (UINT16 conn_id, UINT16 attr_handle,
  560. UINT16 val_len, UINT8 *p_val)
  561. {
  562. tGATT_STATUS cmd_sent = GATT_ILLEGAL_PARAMETER;
  563. BT_HDR *p_buf;
  564. tGATT_VALUE notif;
  565. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  566. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  567. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  568. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  569. GATT_TRACE_API ("GATTS_HandleValueNotification");
  570. if ( (p_reg == NULL) || (p_tcb == NULL)) {
  571. GATT_TRACE_ERROR ("GATTS_HandleValueNotification Unknown conn_id: %u \n", conn_id);
  572. return (tGATT_STATUS) GATT_INVALID_CONN_ID;
  573. }
  574. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  575. GATT_TRACE_ERROR("connection not established\n");
  576. return GATT_WRONG_STATE;
  577. }
  578. if (GATT_HANDLE_IS_VALID (attr_handle)) {
  579. notif.handle = attr_handle;
  580. notif.len = val_len;
  581. memcpy (notif.value, p_val, val_len);
  582. notif.auth_req = GATT_AUTH_REQ_NONE;
  583. if ((p_buf = attp_build_sr_msg (p_tcb, GATT_HANDLE_VALUE_NOTIF, (tGATT_SR_MSG *)&notif))
  584. != NULL) {
  585. cmd_sent = attp_send_sr_msg (p_tcb, p_buf);
  586. } else {
  587. cmd_sent = GATT_NO_RESOURCES;
  588. }
  589. }
  590. return cmd_sent;
  591. }
  592. /*******************************************************************************
  593. **
  594. ** Function GATTS_SendRsp
  595. **
  596. ** Description This function sends the server response to client.
  597. **
  598. ** Parameter conn_id: connection identifier.
  599. ** trans_id: transaction id
  600. ** status: response status
  601. ** p_msg: pointer to message parameters structure.
  602. **
  603. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  604. **
  605. *******************************************************************************/
  606. tGATT_STATUS GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id,
  607. tGATT_STATUS status, tGATTS_RSP *p_msg)
  608. {
  609. tGATT_STATUS cmd_sent = GATT_ILLEGAL_PARAMETER;
  610. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  611. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  612. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  613. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  614. GATT_TRACE_API ("GATTS_SendRsp: conn_id: %u trans_id: %u Status: 0x%04x\n",
  615. conn_id, trans_id, status);
  616. if ( (p_reg == NULL) || (p_tcb == NULL)) {
  617. GATT_TRACE_ERROR ("GATTS_SendRsp Unknown conn_id: %u\n", conn_id);
  618. return (tGATT_STATUS) GATT_INVALID_CONN_ID;
  619. }
  620. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  621. GATT_TRACE_ERROR("connection not established\n");
  622. return GATT_WRONG_STATE;
  623. }
  624. if (p_tcb->sr_cmd.trans_id != trans_id) {
  625. GATT_TRACE_ERROR ("GATTS_SendRsp conn_id: %u waiting for op_code = %02x\n",
  626. conn_id, p_tcb->sr_cmd.op_code);
  627. return (GATT_WRONG_STATE);
  628. }
  629. /* Process App response */
  630. cmd_sent = gatt_sr_process_app_rsp (p_tcb, gatt_if, trans_id, p_tcb->sr_cmd.op_code, status, p_msg);
  631. return cmd_sent;
  632. }
  633. /*******************************************************************************
  634. **
  635. ** Function GATTS_SetAttributeValue
  636. **
  637. ** Description This function sends to set the attribute value .
  638. **
  639. ** Parameter attr_handle:the attribute handle
  640. ** length: the attribute length
  641. ** value: the value to be set to the attribute in the database
  642. **
  643. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  644. **
  645. *******************************************************************************/
  646. tGATT_STATUS GATTS_SetAttributeValue(UINT16 attr_handle, UINT16 length, UINT8 *value)
  647. {
  648. tGATT_STATUS status;
  649. tGATT_HDL_LIST_ELEM *p_decl = NULL;
  650. GATT_TRACE_DEBUG("GATTS_SetAttributeValue: attr_handle: %u length: %u \n",
  651. attr_handle, length);
  652. if (length <= 0){
  653. return GATT_INVALID_ATTR_LEN;
  654. }
  655. if ((p_decl = gatt_find_hdl_buffer_by_attr_handle(attr_handle)) == NULL) {
  656. GATT_TRACE_DEBUG("Service not created\n");
  657. return GATT_INVALID_HANDLE;
  658. }
  659. status = gatts_set_attribute_value(&p_decl->svc_db, attr_handle, length, value);
  660. return status;
  661. }
  662. /*******************************************************************************
  663. **
  664. ** Function GATTS_GetAttributeValue
  665. **
  666. ** Description This function sends to set the attribute value .
  667. **
  668. ** Parameter attr_handle: the attribute handle
  669. ** length:the attribute value length in the database
  670. ** value: the attribute value out put
  671. **
  672. ** Returns GATT_SUCCESS if successfully sent; otherwise error code.
  673. **
  674. *******************************************************************************/
  675. tGATT_STATUS GATTS_GetAttributeValue(UINT16 attr_handle, UINT16 *length, UINT8 **value)
  676. {
  677. tGATT_STATUS status;
  678. tGATT_HDL_LIST_ELEM *p_decl;
  679. GATT_TRACE_DEBUG("GATTS_GetAttributeValue: attr_handle: %u\n",
  680. attr_handle);
  681. if ((p_decl = gatt_find_hdl_buffer_by_attr_handle(attr_handle)) == NULL) {
  682. GATT_TRACE_ERROR("Service not created\n");
  683. *length = 0;
  684. return GATT_INVALID_HANDLE;
  685. }
  686. status = gatts_get_attribute_value(&p_decl->svc_db, attr_handle, length, value);
  687. return status;
  688. }
  689. #endif ///GATTS_INCLUDED == TRUE
  690. #if (GATTC_INCLUDED == TRUE)
  691. /*******************************************************************************/
  692. /* GATT Profile Srvr Functions */
  693. /*******************************************************************************/
  694. /*******************************************************************************/
  695. /* */
  696. /* GATT CLIENT APIs */
  697. /* */
  698. /*******************************************************************************/
  699. /*******************************************************************************
  700. **
  701. ** Function GATTC_ConfigureMTU
  702. **
  703. ** Description This function is called to configure the ATT MTU size.
  704. **
  705. ** Parameters conn_id: connection identifier.
  706. ** mtu - attribute MTU size..
  707. **
  708. ** Returns GATT_SUCCESS if command started successfully.
  709. **
  710. *******************************************************************************/
  711. tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id)
  712. {
  713. UINT8 ret = GATT_NO_RESOURCES;
  714. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  715. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  716. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  717. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  718. tGATT_CLCB *p_clcb;
  719. uint16_t mtu = gatt_get_local_mtu();
  720. GATT_TRACE_API ("GATTC_ConfigureMTU conn_id=%d mtu=%d", conn_id, mtu );
  721. if ( (p_tcb == NULL) || (p_reg == NULL) || (mtu < GATT_DEF_BLE_MTU_SIZE) || (mtu > GATT_MAX_MTU_SIZE)) {
  722. return GATT_ILLEGAL_PARAMETER;
  723. }
  724. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  725. GATT_TRACE_ERROR("connection not established\n");
  726. return GATT_ERROR;
  727. }
  728. /* Validate that the link is BLE, not BR/EDR */
  729. if (p_tcb->transport != BT_TRANSPORT_LE) {
  730. return GATT_ERROR;
  731. }
  732. if (gatt_is_clcb_allocated(conn_id)) {
  733. GATT_TRACE_ERROR("GATTC_ConfigureMTU GATT_BUSY conn_id = %d", conn_id);
  734. return GATT_BUSY;
  735. }
  736. if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL) {
  737. p_clcb->p_tcb->payload_size = mtu;
  738. p_clcb->operation = GATTC_OPTYPE_CONFIG;
  739. ret = attp_send_cl_msg (p_clcb->p_tcb, p_clcb->clcb_idx, GATT_REQ_MTU, (tGATT_CL_MSG *)&mtu);
  740. }
  741. return ret;
  742. }
  743. /*******************************************************************************
  744. **
  745. ** Function GATTC_Discover
  746. **
  747. ** Description This function is called to do a discovery procedure on ATT server.
  748. **
  749. ** Parameters conn_id: connection identifier.
  750. ** disc_type:discovery type.
  751. ** p_param: parameters of discovery requirement.
  752. **
  753. ** Returns GATT_SUCCESS if command received/sent successfully.
  754. **
  755. *******************************************************************************/
  756. tGATT_STATUS GATTC_Discover (UINT16 conn_id, tGATT_DISC_TYPE disc_type,
  757. tGATT_DISC_PARAM *p_param)
  758. {
  759. tGATT_STATUS status = GATT_SUCCESS;
  760. tGATT_CLCB *p_clcb;
  761. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  762. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  763. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  764. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  765. GATT_TRACE_API ("GATTC_Discover conn_id=%d disc_type=%d", conn_id, disc_type);
  766. if ( (p_tcb == NULL) || (p_reg == NULL) || (p_param == NULL) ||
  767. (disc_type >= GATT_DISC_MAX)) {
  768. GATT_TRACE_ERROR("GATTC_Discover Illegal param: disc_type %d conn_id = %d", disc_type, conn_id);
  769. return GATT_ILLEGAL_PARAMETER;
  770. }
  771. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  772. GATT_TRACE_ERROR("connection not established\n");
  773. return GATT_ERROR;
  774. }
  775. if (gatt_is_clcb_allocated(conn_id)) {
  776. GATT_TRACE_ERROR("GATTC_Discover GATT_BUSY conn_id = %d", conn_id);
  777. return GATT_BUSY;
  778. }
  779. if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL ) {
  780. if (!GATT_HANDLE_IS_VALID(p_param->s_handle) ||
  781. !GATT_HANDLE_IS_VALID(p_param->e_handle) ||
  782. /* search by type does not have a valid UUID param */
  783. (disc_type == GATT_DISC_SRVC_BY_UUID &&
  784. p_param->service.len == 0)) {
  785. gatt_clcb_dealloc(p_clcb);
  786. return GATT_ILLEGAL_PARAMETER;
  787. }
  788. p_clcb->operation = GATTC_OPTYPE_DISCOVERY;
  789. p_clcb->op_subtype = disc_type;
  790. p_clcb->s_handle = p_param->s_handle;
  791. p_clcb->e_handle = p_param->e_handle;
  792. p_clcb->uuid = p_param->service;
  793. gatt_act_discovery(p_clcb);
  794. } else {
  795. status = GATT_NO_RESOURCES;
  796. }
  797. return status;
  798. }
  799. /*******************************************************************************
  800. **
  801. ** Function GATTC_Read
  802. **
  803. ** Description This function is called to read the value of an attribute from
  804. ** the server.
  805. **
  806. ** Parameters conn_id: connection identifier.
  807. ** type - attribute read type.
  808. ** p_read - read operation parameters.
  809. **
  810. ** Returns GATT_SUCCESS if command started successfully.
  811. **
  812. *******************************************************************************/
  813. tGATT_STATUS GATTC_Read (UINT16 conn_id, tGATT_READ_TYPE type, tGATT_READ_PARAM *p_read)
  814. {
  815. tGATT_STATUS status = GATT_SUCCESS;
  816. tGATT_CLCB *p_clcb;
  817. tGATT_READ_MULTI *p_read_multi;
  818. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  819. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  820. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  821. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  822. GATT_TRACE_API ("GATTC_Read conn_id=%d type=%d", conn_id, type);
  823. if ( (p_tcb == NULL) || (p_reg == NULL) || (p_read == NULL) || ((type >= GATT_READ_MAX) || (type == 0))) {
  824. GATT_TRACE_ERROR("GATT_Read Illegal param: conn_id %d, type 0%d,", conn_id, type);
  825. return GATT_ILLEGAL_PARAMETER;
  826. }
  827. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  828. GATT_TRACE_ERROR("connection not established\n");
  829. return GATT_ERROR;
  830. }
  831. if (gatt_is_clcb_allocated(conn_id)) {
  832. GATT_TRACE_ERROR("GATTC_Read GATT_BUSY conn_id = %d", conn_id);
  833. return GATT_BUSY;
  834. }
  835. if ( (p_clcb = gatt_clcb_alloc(conn_id)) != NULL ) {
  836. p_clcb->operation = GATTC_OPTYPE_READ;
  837. p_clcb->op_subtype = type;
  838. p_clcb->auth_req = p_read->by_handle.auth_req;
  839. p_clcb->counter = 0;
  840. switch (type) {
  841. case GATT_READ_BY_TYPE:
  842. case GATT_READ_CHAR_VALUE:
  843. p_clcb->s_handle = p_read->service.s_handle;
  844. p_clcb->e_handle = p_read->service.e_handle;
  845. memcpy(&p_clcb->uuid, &p_read->service.uuid, sizeof(tBT_UUID));
  846. break;
  847. case GATT_READ_MULTIPLE:
  848. p_clcb->s_handle = 0;
  849. /* copy multiple handles in CB */
  850. p_read_multi = (tGATT_READ_MULTI *)osi_malloc(sizeof(tGATT_READ_MULTI));
  851. p_clcb->p_attr_buf = (UINT8 *)p_read_multi;
  852. memcpy (p_read_multi, &p_read->read_multiple, sizeof(tGATT_READ_MULTI));
  853. case GATT_READ_BY_HANDLE:
  854. case GATT_READ_PARTIAL:
  855. memset(&p_clcb->uuid, 0, sizeof(tBT_UUID));
  856. p_clcb->s_handle = p_read->by_handle.handle;
  857. if (type == GATT_READ_PARTIAL) {
  858. p_clcb->counter = p_read->partial.offset;
  859. }
  860. break;
  861. default:
  862. break;
  863. }
  864. /* start security check */
  865. if (gatt_security_check_start(p_clcb) == FALSE) {
  866. status = GATT_NO_RESOURCES;
  867. gatt_clcb_dealloc(p_clcb);
  868. }
  869. } else {
  870. status = GATT_NO_RESOURCES;
  871. }
  872. return status;
  873. }
  874. /*******************************************************************************
  875. **
  876. ** Function GATTC_Write
  877. **
  878. ** Description This function is called to write the value of an attribute to
  879. ** the server.
  880. **
  881. ** Parameters conn_id: connection identifier.
  882. ** type - attribute write type.
  883. ** p_write - write operation parameters.
  884. **
  885. ** Returns GATT_SUCCESS if command started successfully.
  886. **
  887. *******************************************************************************/
  888. tGATT_STATUS GATTC_Write (UINT16 conn_id, tGATT_WRITE_TYPE type, tGATT_VALUE *p_write)
  889. {
  890. tGATT_STATUS status = GATT_SUCCESS;
  891. tGATT_CLCB *p_clcb;
  892. tGATT_VALUE *p;
  893. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  894. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  895. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  896. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  897. if ( (p_tcb == NULL) || (p_reg == NULL) || (p_write == NULL) ||
  898. ((type != GATT_WRITE) && (type != GATT_WRITE_PREPARE) && (type != GATT_WRITE_NO_RSP)) ) {
  899. GATT_TRACE_ERROR("GATT_Write Illegal param: conn_id %d, type 0%d,", conn_id, type);
  900. return GATT_ILLEGAL_PARAMETER;
  901. }
  902. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  903. GATT_TRACE_ERROR("connection not established\n");
  904. return GATT_ERROR;
  905. }
  906. if (gatt_is_clcb_allocated(conn_id)) {
  907. GATT_TRACE_ERROR("GATTC_Write GATT_BUSY conn_id = %d", conn_id);
  908. return GATT_BUSY;
  909. }
  910. if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL ) {
  911. p_clcb->operation = GATTC_OPTYPE_WRITE;
  912. p_clcb->op_subtype = type;
  913. p_clcb->auth_req = p_write->auth_req;
  914. if (( p_clcb->p_attr_buf = (UINT8 *)osi_malloc((UINT16)sizeof(tGATT_VALUE))) != NULL) {
  915. memcpy(p_clcb->p_attr_buf, (void *)p_write, sizeof(tGATT_VALUE));
  916. p = (tGATT_VALUE *)p_clcb->p_attr_buf;
  917. if (type == GATT_WRITE_PREPARE) {
  918. p_clcb->start_offset = p_write->offset;
  919. p->offset = 0;
  920. }
  921. if (gatt_security_check_start(p_clcb) == FALSE) {
  922. status = GATT_NO_RESOURCES;
  923. }
  924. } else {
  925. status = GATT_NO_RESOURCES;
  926. }
  927. if (status == GATT_NO_RESOURCES) {
  928. gatt_clcb_dealloc(p_clcb);
  929. }
  930. } else {
  931. status = GATT_NO_RESOURCES;
  932. }
  933. return status;
  934. }
  935. /*******************************************************************************
  936. **
  937. ** Function GATTC_ExecuteWrite
  938. **
  939. ** Description This function is called to send an Execute write request to
  940. ** the server.
  941. **
  942. ** Parameters conn_id: connection identifier.
  943. ** is_execute - to execute or cancel the prepare write request(s)
  944. **
  945. ** Returns GATT_SUCCESS if command started successfully.
  946. **
  947. *******************************************************************************/
  948. tGATT_STATUS GATTC_ExecuteWrite (UINT16 conn_id, BOOLEAN is_execute)
  949. {
  950. tGATT_STATUS status = GATT_SUCCESS;
  951. tGATT_CLCB *p_clcb;
  952. tGATT_EXEC_FLAG flag;
  953. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  954. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  955. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  956. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  957. GATT_TRACE_API ("GATTC_ExecuteWrite conn_id=%d is_execute=%d", conn_id, is_execute);
  958. if ( (p_tcb == NULL) || (p_reg == NULL) ) {
  959. GATT_TRACE_ERROR("GATTC_ExecuteWrite Illegal param: conn_id %d", conn_id);
  960. return GATT_ILLEGAL_PARAMETER;
  961. }
  962. if (!gatt_check_connection_state_by_tcb(p_tcb)) {
  963. GATT_TRACE_ERROR("connection not established\n");
  964. return GATT_ERROR;
  965. }
  966. if (gatt_is_clcb_allocated(conn_id)) {
  967. GATT_TRACE_ERROR("GATTC_Write GATT_BUSY conn_id = %d", conn_id);
  968. return GATT_BUSY;
  969. }
  970. if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL) {
  971. p_clcb->operation = GATTC_OPTYPE_EXE_WRITE;
  972. flag = is_execute ? GATT_PREP_WRITE_EXEC : GATT_PREP_WRITE_CANCEL;
  973. gatt_send_queue_write_cancel (p_clcb->p_tcb, p_clcb, flag);
  974. } else {
  975. GATT_TRACE_ERROR("Unable to allocate client CB for conn_id %d ", conn_id);
  976. status = GATT_NO_RESOURCES;
  977. }
  978. return status;
  979. }
  980. /*******************************************************************************
  981. **
  982. ** Function GATTC_SendHandleValueConfirm
  983. **
  984. ** Description This function is called to send a handle value confirmation
  985. ** as response to a handle value notification from server.
  986. **
  987. ** Parameters conn_id: connection identifier.
  988. ** handle: the handle of the attribute confirmation.
  989. **
  990. ** Returns GATT_SUCCESS if command started successfully.
  991. **
  992. *******************************************************************************/
  993. tGATT_STATUS GATTC_SendHandleValueConfirm (UINT16 conn_id, UINT16 handle)
  994. {
  995. tGATT_STATUS ret = GATT_ILLEGAL_PARAMETER;
  996. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(GATT_GET_TCB_IDX(conn_id));
  997. GATT_TRACE_API ("GATTC_SendHandleValueConfirm conn_id=%d handle=0x%x", conn_id, handle);
  998. if (p_tcb) {
  999. if (p_tcb->ind_count > 0 ) {
  1000. btu_stop_timer (&p_tcb->ind_ack_timer_ent);
  1001. GATT_TRACE_DEBUG ("notif_count=%d ", p_tcb->ind_count);
  1002. /* send confirmation now */
  1003. ret = attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, (tGATT_CL_MSG *)&handle);
  1004. p_tcb->ind_count = 0;
  1005. } else {
  1006. GATT_TRACE_DEBUG ("GATTC_SendHandleValueConfirm - conn_id: %u - ignored not waiting for indicaiton ack", conn_id);
  1007. ret = GATT_SUCCESS;
  1008. }
  1009. } else {
  1010. GATT_TRACE_ERROR ("GATTC_SendHandleValueConfirm - Unknown conn_id: %u", conn_id);
  1011. }
  1012. return ret;
  1013. }
  1014. #endif ///GATTC_INCLUDED == TRUE
  1015. /*******************************************************************************/
  1016. /* */
  1017. /* GATT APIs */
  1018. /* */
  1019. /*******************************************************************************/
  1020. /*******************************************************************************
  1021. **
  1022. ** Function GATT_SetIdleTimeout
  1023. **
  1024. ** Description This function (common to both client and server) sets the idle
  1025. ** timeout for a tansport connection
  1026. **
  1027. ** Parameter bd_addr: target device bd address.
  1028. ** idle_tout: timeout value in seconds.
  1029. **
  1030. ** Returns void
  1031. **
  1032. *******************************************************************************/
  1033. void GATT_SetIdleTimeout (BD_ADDR bd_addr, UINT16 idle_tout, tBT_TRANSPORT transport)
  1034. {
  1035. tGATT_TCB *p_tcb;
  1036. BOOLEAN status = FALSE;
  1037. if ((p_tcb = gatt_find_tcb_by_addr (bd_addr, transport)) != NULL) {
  1038. if (p_tcb->att_lcid == L2CAP_ATT_CID) {
  1039. status = L2CA_SetFixedChannelTout (bd_addr, L2CAP_ATT_CID, idle_tout);
  1040. if (idle_tout == GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP) {
  1041. L2CA_SetIdleTimeoutByBdAddr(p_tcb->peer_bda,
  1042. GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP, BT_TRANSPORT_LE);
  1043. }
  1044. } else {
  1045. status = L2CA_SetIdleTimeout (p_tcb->att_lcid, idle_tout, FALSE);
  1046. }
  1047. }
  1048. #if (CONFIG_BT_STACK_NO_LOG)
  1049. (void) status;
  1050. #endif
  1051. GATT_TRACE_API ("GATT_SetIdleTimeout idle_tout=%d status=%d(1-OK 0-not performed)",
  1052. idle_tout, status);
  1053. }
  1054. /*******************************************************************************
  1055. **
  1056. ** Function GATT_Register
  1057. **
  1058. ** Description This function is called to register an application
  1059. ** with GATT
  1060. **
  1061. ** Parameter p_app_uuid128: Application UUID
  1062. ** p_cb_info: callback functions.
  1063. **
  1064. ** Returns 0 for error, otherwise the index of the client registered with GATT
  1065. **
  1066. *******************************************************************************/
  1067. tGATT_IF GATT_Register (tBT_UUID *p_app_uuid128, const tGATT_CBACK *p_cb_info)
  1068. {
  1069. tGATT_REG *p_reg;
  1070. UINT8 i_gatt_if = 0;
  1071. tGATT_IF gatt_if = 0;
  1072. GATT_TRACE_API ("GATT_Register");
  1073. gatt_dbg_display_uuid(*p_app_uuid128);
  1074. for (i_gatt_if = 0, p_reg = gatt_cb.cl_rcb; i_gatt_if < GATT_MAX_APPS; i_gatt_if++, p_reg++) {
  1075. if (p_reg->in_use && !memcmp(p_app_uuid128->uu.uuid128, p_reg->app_uuid128.uu.uuid128, LEN_UUID_128)) {
  1076. GATT_TRACE_ERROR("application already registered.");
  1077. return 0;
  1078. }
  1079. }
  1080. for (i_gatt_if = 0, p_reg = gatt_cb.cl_rcb; i_gatt_if < GATT_MAX_APPS; i_gatt_if++, p_reg++) {
  1081. if (!p_reg->in_use) {
  1082. memset(p_reg, 0 , sizeof(tGATT_REG));
  1083. i_gatt_if++; /* one based number */
  1084. p_reg->app_uuid128 = *p_app_uuid128;
  1085. gatt_if =
  1086. p_reg->gatt_if = (tGATT_IF)i_gatt_if;
  1087. p_reg->app_cb = *p_cb_info;
  1088. p_reg->in_use = TRUE;
  1089. break;
  1090. }
  1091. }
  1092. GATT_TRACE_API ("allocated gatt_if=%d\n", gatt_if);
  1093. return gatt_if;
  1094. }
  1095. /*******************************************************************************
  1096. **
  1097. ** Function GATT_Deregister
  1098. **
  1099. ** Description This function deregistered the application from GATT.
  1100. **
  1101. ** Parameters gatt_if: application interface.
  1102. **
  1103. ** Returns None.
  1104. **
  1105. *******************************************************************************/
  1106. void GATT_Deregister (tGATT_IF gatt_if)
  1107. {
  1108. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1109. tGATT_TCB *p_tcb;
  1110. tGATT_CLCB *p_clcb;
  1111. list_node_t *p_node = NULL;
  1112. list_node_t *p_next = NULL;
  1113. #if (GATTS_INCLUDED == TRUE)
  1114. UINT8 ii;
  1115. tGATT_SR_REG *p_sreg;
  1116. #endif ///GATTS_INCLUDED == TRUE
  1117. GATT_TRACE_API ("GATT_Deregister gatt_if=%d", gatt_if);
  1118. /* Index 0 is GAP and is never deregistered */
  1119. if ( (gatt_if == 0) || (p_reg == NULL) ) {
  1120. GATT_TRACE_ERROR ("GATT_Deregister with invalid gatt_if: %u", gatt_if);
  1121. return;
  1122. }
  1123. /* stop all services */
  1124. /* todo an applcaiton can not be deregistered if its services is also used by other application
  1125. deregisteration need to bed performed in an orderly fashion
  1126. no check for now */
  1127. #if (GATTS_INCLUDED == TRUE)
  1128. for (ii = 0, p_sreg = gatt_cb.sr_reg; ii < GATT_MAX_SR_PROFILES; ii++, p_sreg++) {
  1129. if (p_sreg->in_use && (p_sreg->gatt_if == gatt_if)) {
  1130. GATTS_StopService(p_sreg->s_hdl);
  1131. }
  1132. }
  1133. /* free all services db buffers if owned by this application */
  1134. gatt_free_srvc_db_buffer_app_id(&p_reg->app_uuid128);
  1135. #endif ///GATTS_INCLUDED == TRUE
  1136. /* When an application deregisters, check remove the link associated with the app */
  1137. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = p_next) {
  1138. p_tcb = list_node(p_node);
  1139. p_next = list_next(p_node);
  1140. if (p_tcb->in_use) {
  1141. if (gatt_get_ch_state(p_tcb) != GATT_CH_CLOSE) {
  1142. gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
  1143. if (!gatt_num_apps_hold_link(p_tcb)) {
  1144. /* this will disconnect the link or cancel the pending connect request at lower layer*/
  1145. gatt_disconnect(p_tcb);
  1146. }
  1147. }
  1148. list_node_t *p_node_clcb = NULL;
  1149. list_node_t *p_node_next = NULL;
  1150. for(p_node_clcb = list_begin(gatt_cb.p_clcb_list); p_node_clcb; p_node_clcb = p_node_next) {
  1151. p_clcb = list_node(p_node_clcb);
  1152. p_node_next = list_next(p_node_clcb);
  1153. if (p_clcb->in_use &&
  1154. (p_clcb->p_reg->gatt_if == gatt_if) &&
  1155. (p_clcb->p_tcb->tcb_idx == p_tcb->tcb_idx)) {
  1156. btu_stop_timer(&p_clcb->rsp_timer_ent);
  1157. gatt_clcb_dealloc (p_clcb);
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. gatt_deregister_bgdev_list(gatt_if);
  1164. /* update the listen mode */
  1165. #if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
  1166. GATT_Listen(gatt_if, FALSE, NULL);
  1167. #endif
  1168. memset (p_reg, 0, sizeof(tGATT_REG));
  1169. }
  1170. /*******************************************************************************
  1171. **
  1172. ** Function GATT_StartIf
  1173. **
  1174. ** Description This function is called after registration to start receiving
  1175. ** callbacks for registered interface. Function may call back
  1176. ** with connection status and queued notifications
  1177. **
  1178. ** Parameter gatt_if: application interface.
  1179. **
  1180. ** Returns None.
  1181. **
  1182. *******************************************************************************/
  1183. void GATT_StartIf (tGATT_IF gatt_if)
  1184. {
  1185. tGATT_REG *p_reg;
  1186. tGATT_TCB *p_tcb;
  1187. BD_ADDR bda;
  1188. UINT8 start_idx, found_idx;
  1189. UINT16 conn_id;
  1190. tGATT_TRANSPORT transport ;
  1191. GATT_TRACE_API ("GATT_StartIf gatt_if=%d", gatt_if);
  1192. if ((p_reg = gatt_get_regcb(gatt_if)) != NULL) {
  1193. start_idx = 0;
  1194. while (gatt_find_the_connected_bda(start_idx, bda, &found_idx, &transport)) {
  1195. p_tcb = gatt_find_tcb_by_addr(bda, transport);
  1196. if (p_reg->app_cb.p_conn_cb && p_tcb) {
  1197. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
  1198. (*p_reg->app_cb.p_conn_cb)(gatt_if, bda, conn_id, TRUE, 0, transport);
  1199. }
  1200. start_idx = ++found_idx;
  1201. }
  1202. }
  1203. }
  1204. /*******************************************************************************
  1205. **
  1206. ** Function GATT_Connect
  1207. **
  1208. ** Description This function initiate a connection to a remote device on GATT
  1209. ** channel.
  1210. **
  1211. ** Parameters gatt_if: application interface
  1212. ** bd_addr: peer device address.
  1213. ** bd_addr_type: peer device address type.
  1214. ** is_direct: is a direct connection or a background auto connection
  1215. **
  1216. ** Returns TRUE if connection started; FALSE if connection start failure.
  1217. **
  1218. *******************************************************************************/
  1219. BOOLEAN GATT_Connect (tGATT_IF gatt_if, BD_ADDR bd_addr, tBLE_ADDR_TYPE bd_addr_type,
  1220. BOOLEAN is_direct, tBT_TRANSPORT transport, BOOLEAN is_aux)
  1221. {
  1222. tGATT_REG *p_reg;
  1223. BOOLEAN status = FALSE;
  1224. GATT_TRACE_API ("GATT_Connect gatt_if=%d", gatt_if);
  1225. /* Make sure app is registered */
  1226. if ((p_reg = gatt_get_regcb(gatt_if)) == NULL) {
  1227. GATT_TRACE_ERROR("GATT_Connect - gatt_if =%d is not registered", gatt_if);
  1228. return (FALSE);
  1229. }
  1230. if (is_direct) {
  1231. status = gatt_act_connect (p_reg, bd_addr, bd_addr_type, transport, is_aux);
  1232. } else {
  1233. if (transport == BT_TRANSPORT_LE) {
  1234. status = gatt_update_auto_connect_dev(gatt_if, TRUE, bd_addr, TRUE);
  1235. } else {
  1236. GATT_TRACE_ERROR("Unsupported transport for background connection");
  1237. }
  1238. }
  1239. return status;
  1240. }
  1241. /*******************************************************************************
  1242. **
  1243. ** Function GATT_CancelConnect
  1244. **
  1245. ** Description This function terminate the connection initaition to a remote
  1246. ** device on GATT channel.
  1247. **
  1248. ** Parameters gatt_if: client interface. If 0 used as unconditionally disconnect,
  1249. ** typically used for direct connection cancellation.
  1250. ** bd_addr: peer device address.
  1251. **
  1252. ** Returns TRUE if connection started; FALSE if connection start failure.
  1253. **
  1254. *******************************************************************************/
  1255. BOOLEAN GATT_CancelConnect (tGATT_IF gatt_if, BD_ADDR bd_addr, BOOLEAN is_direct)
  1256. {
  1257. tGATT_REG *p_reg;
  1258. tGATT_TCB *p_tcb;
  1259. BOOLEAN status = TRUE;
  1260. tGATT_IF temp_gatt_if;
  1261. UINT8 start_idx, found_idx;
  1262. GATT_TRACE_API ("GATT_CancelConnect gatt_if=%d", gatt_if);
  1263. if ((gatt_if != 0) && ((p_reg = gatt_get_regcb(gatt_if)) == NULL)) {
  1264. GATT_TRACE_ERROR("GATT_CancelConnect - gatt_if =%d is not registered", gatt_if);
  1265. return (FALSE);
  1266. }
  1267. if (is_direct) {
  1268. if (!gatt_if) {
  1269. GATT_TRACE_DEBUG("GATT_CancelConnect - unconditional");
  1270. start_idx = 0;
  1271. /* only LE connection can be cancelled */
  1272. p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
  1273. if (p_tcb && gatt_num_apps_hold_link(p_tcb)) {
  1274. while (status && gatt_find_app_hold_link(p_tcb, start_idx, &found_idx, &temp_gatt_if)) {
  1275. status = gatt_cancel_open(temp_gatt_if, bd_addr);
  1276. start_idx = ++found_idx;
  1277. }
  1278. } else {
  1279. GATT_TRACE_ERROR("GATT_CancelConnect - no app found");
  1280. status = FALSE;
  1281. }
  1282. } else {
  1283. status = gatt_cancel_open(gatt_if, bd_addr);
  1284. }
  1285. } else {
  1286. if (!gatt_if) {
  1287. if (gatt_get_num_apps_for_bg_dev(bd_addr)) {
  1288. while (gatt_find_app_for_bg_dev(bd_addr, &temp_gatt_if)) {
  1289. gatt_remove_bg_dev_for_app(temp_gatt_if, bd_addr);
  1290. }
  1291. } else {
  1292. GATT_TRACE_ERROR("GATT_CancelConnect -no app associated with the bg device for unconditional removal");
  1293. status = FALSE;
  1294. }
  1295. } else {
  1296. status = gatt_remove_bg_dev_for_app(gatt_if, bd_addr);
  1297. }
  1298. }
  1299. return status;
  1300. }
  1301. /*******************************************************************************
  1302. **
  1303. ** Function GATT_Disconnect
  1304. **
  1305. ** Description This function disconnect the GATT channel for this registered
  1306. ** application.
  1307. **
  1308. ** Parameters conn_id: connection identifier.
  1309. **
  1310. ** Returns GATT_SUCCESS if disconnected.
  1311. **
  1312. *******************************************************************************/
  1313. tGATT_STATUS GATT_Disconnect (UINT16 conn_id)
  1314. {
  1315. tGATT_STATUS ret = GATT_ILLEGAL_PARAMETER;
  1316. tGATT_TCB *p_tcb = NULL;
  1317. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  1318. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  1319. GATT_TRACE_API ("GATT_Disconnect conn_id=%d ", conn_id);
  1320. p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  1321. if (p_tcb) {
  1322. gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
  1323. if (!gatt_num_apps_hold_link(p_tcb)) {
  1324. gatt_disconnect(p_tcb);
  1325. }
  1326. ret = GATT_SUCCESS;
  1327. }
  1328. return ret;
  1329. }
  1330. /*******************************************************************************
  1331. **
  1332. ** Function GATT_SendServiceChangeIndication
  1333. **
  1334. ** Description This function is to send a service change indication
  1335. **
  1336. ** Parameters bd_addr: peer device address.
  1337. **
  1338. ** Returns None.
  1339. **
  1340. *******************************************************************************/
  1341. tGATT_STATUS GATT_SendServiceChangeIndication (BD_ADDR bd_addr)
  1342. {
  1343. UINT8 start_idx, found_idx;
  1344. BOOLEAN srv_chg_ind_pending = FALSE;
  1345. tGATT_TCB *p_tcb;
  1346. tBT_TRANSPORT transport;
  1347. tGATT_STATUS status = GATT_NOT_FOUND;
  1348. if (GATTS_SEND_SERVICE_CHANGE_MODE == GATTS_SEND_SERVICE_CHANGE_AUTO) {
  1349. status = GATT_WRONG_STATE;
  1350. GATT_TRACE_ERROR ("%s can't send service change indication manually, please configure the option through menuconfig", __func__);
  1351. return status;
  1352. }
  1353. if(bd_addr) {
  1354. status = gatt_send_srv_chg_ind(bd_addr);
  1355. } else {
  1356. start_idx = 0;
  1357. BD_ADDR addr;
  1358. while (gatt_find_the_connected_bda(start_idx, addr, &found_idx, &transport)) {
  1359. p_tcb = gatt_get_tcb_by_idx(found_idx);
  1360. srv_chg_ind_pending = gatt_is_srv_chg_ind_pending(p_tcb);
  1361. if (!srv_chg_ind_pending) {
  1362. status = gatt_send_srv_chg_ind(addr);
  1363. } else {
  1364. status = GATT_BUSY;
  1365. GATT_TRACE_DEBUG("discard srv chg - already has one in the queue");
  1366. }
  1367. start_idx = ++found_idx;
  1368. }
  1369. }
  1370. return status;
  1371. }
  1372. /*******************************************************************************
  1373. **
  1374. ** Function GATT_GetConnectionInfor
  1375. **
  1376. ** Description This function use conn_id to find its associated BD address and applciation
  1377. ** interface
  1378. **
  1379. ** Parameters conn_id: connection id (input)
  1380. ** p_gatt_if: application interface (output)
  1381. ** bd_addr: peer device address. (output)
  1382. **
  1383. ** Returns TRUE the logical link information is found for conn_id
  1384. **
  1385. *******************************************************************************/
  1386. BOOLEAN GATT_GetConnectionInfor(UINT16 conn_id, tGATT_IF *p_gatt_if, BD_ADDR bd_addr,
  1387. tBT_TRANSPORT *p_transport)
  1388. {
  1389. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  1390. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1391. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  1392. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  1393. BOOLEAN status = FALSE;
  1394. GATT_TRACE_API ("GATT_GetConnectionInfor conn_id=%d", conn_id);
  1395. if (p_tcb && p_reg ) {
  1396. memcpy(bd_addr, p_tcb->peer_bda, BD_ADDR_LEN);
  1397. *p_gatt_if = gatt_if;
  1398. *p_transport = p_tcb->transport;
  1399. status = TRUE;
  1400. }
  1401. return status;
  1402. }
  1403. /*******************************************************************************
  1404. **
  1405. ** Function GATT_GetConnIdIfConnected
  1406. **
  1407. ** Description This function find the conn_id if the logical link for BD address
  1408. ** and applciation interface is connected
  1409. **
  1410. ** Parameters gatt_if: application interface (input)
  1411. ** bd_addr: peer device address. (input)
  1412. ** p_conn_id: connection id (output)
  1413. ** transport: transport option
  1414. **
  1415. ** Returns TRUE the logical link is connected
  1416. **
  1417. *******************************************************************************/
  1418. BOOLEAN GATT_GetConnIdIfConnected(tGATT_IF gatt_if, BD_ADDR bd_addr, UINT16 *p_conn_id,
  1419. tBT_TRANSPORT transport)
  1420. {
  1421. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1422. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
  1423. BOOLEAN status = FALSE;
  1424. if (p_reg && p_tcb && (gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) ) {
  1425. *p_conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
  1426. status = TRUE;
  1427. }
  1428. GATT_TRACE_API ("GATT_GetConnIdIfConnected status=%d\n", status);
  1429. return status;
  1430. }
  1431. /*******************************************************************************
  1432. **
  1433. ** Function GATT_Listen
  1434. **
  1435. ** Description This function start or stop LE advertisement and listen for
  1436. ** connection.
  1437. **
  1438. ** Parameters gatt_if: application interface
  1439. ** p_bd_addr: listen for specific address connection, or NULL for
  1440. ** listen to all device connection.
  1441. ** start: start or stop listening.
  1442. **
  1443. ** Returns TRUE if advertisement is started; FALSE if adv start failure.
  1444. **
  1445. *******************************************************************************/
  1446. BOOLEAN GATT_Listen (tGATT_IF gatt_if, BOOLEAN start, BD_ADDR_PTR bd_addr)
  1447. {
  1448. tGATT_REG *p_reg;
  1449. GATT_TRACE_API ("GATT_Listen gatt_if=%d", gatt_if);
  1450. /* Make sure app is registered */
  1451. if ((p_reg = gatt_get_regcb(gatt_if)) == NULL) {
  1452. GATT_TRACE_ERROR("GATT_Listen - gatt_if =%d is not registered", gatt_if);
  1453. return (FALSE);
  1454. }
  1455. if (bd_addr != NULL) {
  1456. gatt_update_auto_connect_dev(gatt_if, start, bd_addr, FALSE);
  1457. } else {
  1458. p_reg->listening = start ? GATT_LISTEN_TO_ALL : GATT_LISTEN_TO_NONE;
  1459. }
  1460. return gatt_update_listen_mode();
  1461. }
  1462. #endif