gatt_int.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. #ifndef GATT_INT_H
  19. #define GATT_INT_H
  20. #include "common/bt_target.h"
  21. #include "common/bt_trace.h"
  22. #include "stack/gatt_api.h"
  23. #include "stack/btm_ble_api.h"
  24. #include "stack/btu.h"
  25. #include "osi/fixed_queue.h"
  26. #include <string.h>
  27. #define GATT_CREATE_CONN_ID(tcb_idx, gatt_if) ((UINT16) ((((UINT8)(tcb_idx) ) << 8) | ((UINT8) (gatt_if))))
  28. #define GATT_GET_TCB_IDX(conn_id) ((UINT8) (((UINT16) (conn_id)) >> 8))
  29. #define GATT_GET_GATT_IF(conn_id) ((tGATT_IF)((UINT8) (conn_id)))
  30. #define GATT_GET_SR_REG_PTR(index) (&gatt_cb.sr_reg[(UINT8) (index)]);
  31. #define GATT_TRANS_ID_MAX 0x0fffffff /* 4 MSB is reserved */
  32. #define GATT_RSP_BY_APP 0x00
  33. #define GATT_RSP_BY_STACK 0x01
  34. #define GATT_RSP_DEFAULT GATT_RSP_BY_APP //need to rsp by the app.
  35. /* security action for GATT write and read request */
  36. #define GATT_SEC_NONE 0
  37. #define GATT_SEC_OK 1
  38. #define GATT_SEC_SIGN_DATA 2 /* compute the signature for the write cmd */
  39. #define GATT_SEC_ENCRYPT 3 /* encrypt the link with current key */
  40. #define GATT_SEC_ENCRYPT_NO_MITM 4 /* unauthenticated encryption or better */
  41. #define GATT_SEC_ENCRYPT_MITM 5 /* authenticated encryption */
  42. #define GATT_SEC_ENC_PENDING 6 /* wait for link encryption pending */
  43. typedef UINT8 tGATT_SEC_ACTION;
  44. #define GATT_ATTR_OP_SPT_MTU (0x00000001 << 0)
  45. #define GATT_ATTR_OP_SPT_FIND_INFO (0x00000001 << 1)
  46. #define GATT_ATTR_OP_SPT_FIND_BY_TYPE (0x00000001 << 2)
  47. #define GATT_ATTR_OP_SPT_READ_BY_TYPE (0x00000001 << 3)
  48. #define GATT_ATTR_OP_SPT_READ (0x00000001 << 4)
  49. #define GATT_ATTR_OP_SPT_MULT_READ (0x00000001 << 5)
  50. #define GATT_ATTR_OP_SPT_READ_BLOB (0x00000001 << 6)
  51. #define GATT_ATTR_OP_SPT_READ_BY_GRP_TYPE (0x00000001 << 7)
  52. #define GATT_ATTR_OP_SPT_WRITE (0x00000001 << 8)
  53. #define GATT_ATTR_OP_SPT_WRITE_CMD (0x00000001 << 9)
  54. #define GATT_ATTR_OP_SPT_PREP_WRITE (0x00000001 << 10)
  55. #define GATT_ATTR_OP_SPT_EXE_WRITE (0x00000001 << 11)
  56. #define GATT_ATTR_OP_SPT_HDL_VALUE_CONF (0x00000001 << 12)
  57. #define GATT_ATTR_OP_SP_SIGN_WRITE (0x00000001 << 13)
  58. #define GATT_INDEX_INVALID 0xff
  59. #define GATT_PENDING_REQ_NONE 0
  60. #define GATT_WRITE_CMD_MASK 0xc0 /*0x1100-0000*/
  61. #define GATT_AUTH_SIGN_MASK 0x80 /*0x1000-0000*/
  62. #define GATT_AUTH_SIGN_LEN 12
  63. #define GATT_HDR_SIZE 3 /* 1B opcode + 2B handle */
  64. /* wait for ATT cmd response timeout value */
  65. #define GATT_WAIT_FOR_RSP_TOUT 30
  66. #define GATT_WAIT_FOR_DISC_RSP_TOUT 15
  67. #define GATT_REQ_RETRY_LIMIT 2
  68. #define GATT_WAIT_FOR_IND_ACK_TOUT 5
  69. /* characteristic descriptor type */
  70. #define GATT_DESCR_EXT_DSCPTOR 1 /* Characteristic Extended Properties */
  71. #define GATT_DESCR_USER_DSCPTOR 2 /* Characteristic User Description */
  72. #define GATT_DESCR_CLT_CONFIG 3 /* Client Characteristic Configuration */
  73. #define GATT_DESCR_SVR_CONFIG 4 /* Server Characteristic Configuration */
  74. #define GATT_DESCR_PRES_FORMAT 5 /* Characteristic Presentation Format */
  75. #define GATT_DESCR_AGGR_FORMAT 6 /* Characteristic Aggregate Format */
  76. #define GATT_DESCR_VALID_RANGE 7 /* Characteristic Valid Range */
  77. #define GATT_DESCR_UNKNOWN 0xff
  78. #define GATT_SEC_FLAG_LKEY_UNAUTHED BTM_SEC_FLAG_LKEY_KNOWN
  79. #define GATT_SEC_FLAG_LKEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED
  80. #define GATT_SEC_FLAG_ENCRYPTED BTM_SEC_FLAG_ENCRYPTED
  81. #define GATT_SEC_FLAG_AUTHORIZATION BTM_SEC_FLAG_AUTHORIZED
  82. typedef UINT8 tGATT_SEC_FLAG;
  83. /* Find Information Response Type
  84. */
  85. #define GATT_INFO_TYPE_PAIR_16 0x01
  86. #define GATT_INFO_TYPE_PAIR_128 0x02
  87. #define GATTS_SEND_SERVICE_CHANGE_AUTO 0
  88. #define GATTS_SEND_SERVICE_CHANGE_MANUAL 1
  89. /* GATT client FIND_TYPE_VALUE_Request data */
  90. typedef struct {
  91. tBT_UUID uuid; /* type of attribute to be found */
  92. UINT16 s_handle; /* starting handle */
  93. UINT16 e_handle; /* ending handle */
  94. UINT16 value_len; /* length of the attribute value */
  95. UINT8 value[GATT_MAX_MTU_SIZE]; /* pointer to the attribute value to be found */
  96. } tGATT_FIND_TYPE_VALUE;
  97. /* client request message to ATT protocol
  98. */
  99. typedef union {
  100. tGATT_READ_BY_TYPE browse; /* read by type request */
  101. tGATT_FIND_TYPE_VALUE find_type_value;/* find by type value */
  102. tGATT_READ_MULTI read_multi; /* read multiple request */
  103. tGATT_READ_PARTIAL read_blob; /* read blob */
  104. tGATT_VALUE attr_value; /* write request */
  105. /* prepare write */
  106. /* write blob */
  107. UINT16 handle; /* read, handle value confirmation */
  108. UINT16 mtu;
  109. tGATT_EXEC_FLAG exec_write; /* execute write */
  110. } tGATT_CL_MSG;
  111. /* error response strucutre */
  112. typedef struct {
  113. UINT16 handle;
  114. UINT8 cmd_code;
  115. UINT8 reason;
  116. } tGATT_ERROR;
  117. /* Execute write response structure */
  118. typedef struct {
  119. UINT8 op_code;
  120. }__attribute__((packed)) tGATT_EXEC_WRITE_RSP;
  121. /* Write request response structure */
  122. typedef struct {
  123. UINT8 op_code;
  124. }__attribute__((packed)) tGATT_WRITE_REQ_RSP;
  125. /* server response message to ATT protocol
  126. */
  127. typedef union {
  128. /* data type member event */
  129. tGATT_VALUE attr_value; /* READ, HANDLE_VALUE_IND, PREPARE_WRITE */
  130. /* READ_BLOB, READ_BY_TYPE */
  131. tGATT_ERROR error; /* ERROR_RSP */
  132. UINT16 handle; /* WRITE, WRITE_BLOB */
  133. UINT16 mtu; /* exchange MTU request */
  134. } tGATT_SR_MSG;
  135. /* Characteristic declaration attribute value
  136. */
  137. typedef struct {
  138. tGATT_CHAR_PROP property;
  139. UINT16 char_val_handle;
  140. } tGATT_CHAR_DECL;
  141. /* attribute value maintained in the server database
  142. */
  143. typedef union {
  144. tBT_UUID uuid; /* service declaration */
  145. tGATT_CHAR_DECL char_decl; /* characteristic declaration */
  146. tGATT_INCL_SRVC incl_handle; /* included service */
  147. tGATT_ATTR_VAL attr_val;
  148. } tGATT_ATTR_VALUE;
  149. /* Attribute UUID type
  150. */
  151. #define GATT_ATTR_UUID_TYPE_16 0
  152. #define GATT_ATTR_UUID_TYPE_128 1
  153. #define GATT_ATTR_UUID_TYPE_32 2
  154. typedef UINT8 tGATT_ATTR_UUID_TYPE;
  155. /* 16 bits UUID Attribute in server database
  156. */
  157. typedef struct {
  158. void *p_next; /* pointer to the next attribute, either tGATT_ATTR16 or tGATT_ATTR128 */
  159. tGATT_ATTR_VALUE *p_value;
  160. tGATT_ATTR_UUID_TYPE uuid_type;
  161. tGATT_PERM permission;
  162. tGATTS_ATTR_CONTROL control;
  163. tGATT_ATTR_MASK mask;
  164. UINT16 handle;
  165. UINT16 uuid;
  166. } tGATT_ATTR16;
  167. /* 32 bits UUID Attribute in server database
  168. */
  169. typedef struct {
  170. void *p_next; /* pointer to the next attribute, either tGATT_ATTR16, tGATT_ATTR32 or tGATT_ATTR128 */
  171. tGATT_ATTR_VALUE *p_value;
  172. tGATT_ATTR_UUID_TYPE uuid_type;
  173. tGATT_PERM permission;
  174. tGATTS_ATTR_CONTROL control;
  175. tGATT_ATTR_MASK mask;
  176. UINT16 handle;
  177. UINT32 uuid;
  178. } tGATT_ATTR32;
  179. /* 128 bits UUID Attribute in server database
  180. */
  181. typedef struct {
  182. void *p_next; /* pointer to the next attribute, either tGATT_ATTR16 or tGATT_ATTR128 */
  183. tGATT_ATTR_VALUE *p_value;
  184. tGATT_ATTR_UUID_TYPE uuid_type;
  185. tGATT_PERM permission;
  186. tGATTS_ATTR_CONTROL control;
  187. tGATT_ATTR_MASK mask;
  188. UINT16 handle;
  189. UINT8 uuid[LEN_UUID_128];
  190. } tGATT_ATTR128;
  191. /* Service Database definition
  192. */
  193. typedef struct {
  194. void *p_attr_list; /* pointer to the first attribute, either tGATT_ATTR16 or tGATT_ATTR128 */
  195. UINT8 *p_free_mem; /* Pointer to free memory */
  196. fixed_queue_t *svc_buffer; /* buffer queue used for service database */
  197. UINT32 mem_free; /* Memory still available */
  198. UINT16 end_handle; /* Last handle number */
  199. UINT16 next_handle; /* Next usable handle value */
  200. } tGATT_SVC_DB;
  201. /* Data Structure used for GATT server */
  202. /* A GATT registration record consists of a handle, and 1 or more attributes */
  203. /* A service registration information record consists of beginning and ending */
  204. /* attribute handle, service UUID and a set of GATT server callback. */
  205. typedef struct {
  206. tGATT_SVC_DB *p_db; /* pointer to the service database */
  207. tBT_UUID app_uuid; /* applicatino UUID */
  208. UINT32 sdp_handle; /* primamry service SDP handle */
  209. UINT16 service_instance; /* service instance number */
  210. UINT16 type; /* service type UUID, primary or secondary */
  211. UINT16 s_hdl; /* service starting handle */
  212. UINT16 e_hdl; /* service ending handle */
  213. tGATT_IF gatt_if; /* this service is belong to which application */
  214. BOOLEAN in_use;
  215. } tGATT_SR_REG;
  216. #define GATT_LISTEN_TO_ALL 0xff
  217. #define GATT_LISTEN_TO_NONE 0
  218. /* Data Structure used for GATT server */
  219. /* An GATT registration record consists of a handle, and 1 or more attributes */
  220. /* A service registration information record consists of beginning and ending */
  221. /* attribute handle, service UUID and a set of GATT server callback. */
  222. typedef struct {
  223. tBT_UUID app_uuid128;
  224. tGATT_CBACK app_cb;
  225. tGATT_IF gatt_if; /* one based */
  226. BOOLEAN in_use;
  227. UINT8 listening; /* if adv for all has been enabled */
  228. } tGATT_REG;
  229. /* command queue for each connection */
  230. typedef struct {
  231. BT_HDR *p_cmd;
  232. UINT16 clcb_idx;
  233. UINT8 op_code;
  234. BOOLEAN to_send;
  235. } tGATT_CMD_Q;
  236. #if GATT_MAX_SR_PROFILES <= 8
  237. typedef UINT8 tGATT_APP_MASK;
  238. #elif GATT_MAX_SR_PROFILES <= 16
  239. typedef UINT16 tGATT_APP_MASK;
  240. #elif GATT_MAX_SR_PROFILES <= 32
  241. typedef UINT32 tGATT_APP_MASK;
  242. #endif
  243. /* command details for each connection */
  244. typedef struct {
  245. BT_HDR *p_rsp_msg;
  246. UINT32 trans_id;
  247. tGATT_READ_MULTI multi_req;
  248. fixed_queue_t *multi_rsp_q;
  249. UINT16 handle;
  250. UINT8 op_code;
  251. UINT8 status;
  252. UINT8 cback_cnt[GATT_MAX_APPS];
  253. } tGATT_SR_CMD;
  254. #define GATT_CH_CLOSE 0
  255. #define GATT_CH_CLOSING 1
  256. #define GATT_CH_CONN 2
  257. #define GATT_CH_CFG 3
  258. #define GATT_CH_OPEN 4
  259. typedef UINT8 tGATT_CH_STATE;
  260. #define GATT_GATT_START_HANDLE 1
  261. #define GATT_GAP_START_HANDLE 20
  262. #define GATT_APP_START_HANDLE 40
  263. typedef struct hdl_cfg {
  264. UINT16 gatt_start_hdl;
  265. UINT16 gap_start_hdl;
  266. UINT16 app_start_hdl;
  267. } tGATT_HDL_CFG;
  268. typedef struct hdl_list_elem {
  269. struct hdl_list_elem *p_next;
  270. struct hdl_list_elem *p_prev;
  271. tGATTS_HNDL_RANGE asgn_range; /* assigned handle range */
  272. tGATT_SVC_DB svc_db;
  273. BOOLEAN in_use;
  274. } tGATT_HDL_LIST_ELEM;
  275. typedef struct {
  276. tGATT_HDL_LIST_ELEM *p_first;
  277. tGATT_HDL_LIST_ELEM *p_last;
  278. UINT16 count;
  279. } tGATT_HDL_LIST_INFO;
  280. typedef struct srv_list_elem {
  281. struct srv_list_elem *p_next;
  282. struct srv_list_elem *p_prev;
  283. UINT16 s_hdl;
  284. UINT8 i_sreg;
  285. BOOLEAN in_use;
  286. BOOLEAN is_primary;
  287. } tGATT_SRV_LIST_ELEM;
  288. typedef struct {
  289. tGATT_SRV_LIST_ELEM *p_last_primary;
  290. tGATT_SRV_LIST_ELEM *p_first;
  291. tGATT_SRV_LIST_ELEM *p_last;
  292. UINT16 count;
  293. } tGATT_SRV_LIST_INFO;
  294. /* prepare write queue data */
  295. typedef struct{
  296. //len: length of value
  297. tGATT_ATTR16 *p_attr;
  298. UINT16 len;
  299. UINT8 op_code;
  300. UINT16 handle;
  301. UINT16 offset;
  302. UINT8 value[2];
  303. }__attribute__((packed)) tGATT_PREPARE_WRITE_QUEUE_DATA;
  304. /* structure to store prepare write packts information */
  305. typedef struct{
  306. //only store prepare write packets which need
  307. //to be responded by stack (not by application)
  308. fixed_queue_t *queue;
  309. //store the total number of prepare write packets
  310. //including that should be responded by stack or by application
  311. UINT16 total_num;
  312. //store application error code for prepare write,
  313. //invalid offset && invalid length
  314. UINT8 error_code_app;
  315. }tGATT_PREPARE_WRITE_RECORD;
  316. typedef struct {
  317. fixed_queue_t *pending_enc_clcb; /* pending encryption channel q */
  318. tGATT_SEC_ACTION sec_act;
  319. BD_ADDR peer_bda;
  320. tBT_TRANSPORT transport;
  321. UINT32 trans_id;
  322. UINT16 att_lcid; /* L2CAP channel ID for ATT */
  323. UINT16 payload_size;
  324. tGATT_CH_STATE ch_state;
  325. UINT8 ch_flags;
  326. tGATT_IF app_hold_link[GATT_MAX_APPS];
  327. /* server needs */
  328. /* server response data */
  329. #if (GATTS_INCLUDED == TRUE)
  330. tGATT_SR_CMD sr_cmd;
  331. #endif ///GATTS_INCLUDED == TRUE
  332. UINT16 indicate_handle;
  333. fixed_queue_t *pending_ind_q;
  334. TIMER_LIST_ENT conf_timer_ent; /* peer confirm to indication timer */
  335. UINT8 prep_cnt[GATT_MAX_APPS];
  336. UINT8 ind_count;
  337. tGATT_CMD_Q cl_cmd_q[GATT_CL_MAX_LCB];
  338. TIMER_LIST_ENT ind_ack_timer_ent; /* local app confirm to indication timer */
  339. UINT8 pending_cl_req;
  340. UINT8 next_slot_inq; /* index of next available slot in queue */
  341. /* client supported feature */
  342. UINT8 cl_supp_feat;
  343. /* server supported feature */
  344. UINT8 sr_supp_feat;
  345. /* if false, should handle database out of sync */
  346. BOOLEAN is_robust_cache_change_aware;
  347. BOOLEAN in_use;
  348. UINT8 tcb_idx;
  349. tGATT_PREPARE_WRITE_RECORD prepare_write_record; /* prepare write packets record */
  350. } tGATT_TCB;
  351. /* logic channel */
  352. typedef struct {
  353. UINT16 next_disc_start_hdl; /* starting handle for the next inc srvv discovery */
  354. tGATT_DISC_RES result;
  355. BOOLEAN wait_for_read_rsp;
  356. } tGATT_READ_INC_UUID128;
  357. typedef struct {
  358. tGATT_TCB *p_tcb; /* associated TCB of this CLCB */
  359. tGATT_REG *p_reg; /* owner of this CLCB */
  360. UINT8 sccb_idx;
  361. UINT8 *p_attr_buf; /* attribute buffer for read multiple, prepare write */
  362. tBT_UUID uuid;
  363. UINT16 conn_id; /* connection handle */
  364. UINT16 clcb_idx;
  365. UINT16 s_handle; /* starting handle of the active request */
  366. UINT16 e_handle; /* ending handle of the active request */
  367. UINT16 counter; /* used as offset, attribute length, num of prepare write */
  368. UINT16 start_offset;
  369. tGATT_AUTH_REQ auth_req; /* authentication requirement */
  370. UINT8 operation; /* one logic channel can have one operation active */
  371. UINT8 op_subtype; /* operation subtype */
  372. UINT8 status; /* operation status */
  373. BOOLEAN first_read_blob_after_read;
  374. tGATT_READ_INC_UUID128 read_uuid128;
  375. BOOLEAN in_use;
  376. TIMER_LIST_ENT rsp_timer_ent; /* peer response timer */
  377. UINT8 retry_count;
  378. } tGATT_CLCB;
  379. typedef struct {
  380. tGATT_CLCB *p_clcb;
  381. } tGATT_PENDING_ENC_CLCB;
  382. #define GATT_SIGN_WRITE 1
  383. #define GATT_VERIFY_SIGN_DATA 2
  384. typedef struct {
  385. BT_HDR hdr;
  386. tGATT_CLCB *p_clcb;
  387. } tGATT_SIGN_WRITE_OP;
  388. typedef struct {
  389. BT_HDR hdr;
  390. tGATT_TCB *p_tcb;
  391. BT_HDR *p_data;
  392. } tGATT_VERIFY_SIGN_OP;
  393. typedef struct {
  394. UINT16 clcb_idx;
  395. BOOLEAN in_use;
  396. } tGATT_SCCB;
  397. typedef struct {
  398. UINT16 handle;
  399. UINT16 uuid;
  400. UINT32 service_change;
  401. } tGATT_SVC_CHG;
  402. typedef struct {
  403. tGATT_IF gatt_if[GATT_MAX_APPS];
  404. tGATT_IF listen_gif[GATT_MAX_APPS];
  405. BD_ADDR remote_bda;
  406. BOOLEAN in_use;
  407. } tGATT_BG_CONN_DEV;
  408. #define GATT_SVC_CHANGED_CONNECTING 1 /* wait for connection */
  409. #define GATT_SVC_CHANGED_SERVICE 2 /* GATT service discovery */
  410. #define GATT_SVC_CHANGED_CHARACTERISTIC 3 /* service change char discovery */
  411. #define GATT_SVC_CHANGED_DESCRIPTOR 4 /* service change CCC discoery */
  412. #define GATT_SVC_CHANGED_CONFIGURE_CCCD 5 /* config CCC */
  413. typedef struct {
  414. UINT16 conn_id;
  415. BOOLEAN in_use;
  416. BOOLEAN connected;
  417. BD_ADDR bda;
  418. tBT_TRANSPORT transport;
  419. /* GATT service change CCC related variables */
  420. UINT8 ccc_stage;
  421. UINT8 ccc_result;
  422. UINT16 s_handle;
  423. UINT16 e_handle;
  424. } tGATT_PROFILE_CLCB;
  425. typedef struct {
  426. list_t *p_tcb_list;
  427. fixed_queue_t *sign_op_queue;
  428. tGATT_SR_REG sr_reg[GATT_MAX_SR_PROFILES];
  429. UINT16 next_handle; /* next available handle */
  430. tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */
  431. tGATT_IF gatt_if;
  432. #if (GATTS_INCLUDED == TRUE)
  433. tGATT_HDL_LIST_INFO hdl_list_info;
  434. tGATT_HDL_LIST_ELEM hdl_list[GATT_MAX_SR_PROFILES];
  435. tGATT_SRV_LIST_INFO srv_list_info;
  436. tGATT_SRV_LIST_ELEM srv_list[GATT_MAX_SR_PROFILES];
  437. #endif ///GATTS_INCLUDED == TRUE
  438. fixed_queue_t *srv_chg_clt_q; /* service change clients queue */
  439. fixed_queue_t *pending_new_srv_start_q; /* pending new service start queue */
  440. tGATT_REG cl_rcb[GATT_MAX_APPS];
  441. list_t *p_clcb_list; /* connection link control block*/
  442. tGATT_SCCB sccb[GATT_MAX_SCCB]; /* sign complete callback function GATT_MAX_SCCB <= GATT_CL_MAX_LCB */
  443. UINT8 trace_level;
  444. UINT16 def_mtu_size;
  445. #if GATT_CONFORMANCE_TESTING == TRUE
  446. BOOLEAN enable_err_rsp;
  447. UINT8 req_op_code;
  448. UINT8 err_status;
  449. UINT16 handle;
  450. #endif
  451. #if (GATTS_INCLUDED == TRUE)
  452. tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
  453. #endif ///GATTS_INCLUDED == TRUE
  454. UINT16 handle_of_h_r; /* Handle of the handles reused characteristic value */
  455. UINT16 handle_of_database_hash;
  456. UINT16 handle_of_cl_supported_feat;
  457. UINT16 handle_of_sr_supported_feat;
  458. BT_OCTET16 database_hash;
  459. UINT8 gatt_sr_supported_feat_mask;
  460. UINT8 gatt_cl_supported_feat_mask;
  461. tGATT_APPL_INFO cb_info;
  462. tGATT_HDL_CFG hdl_cfg;
  463. tGATT_BG_CONN_DEV bgconn_dev[GATT_MAX_BG_CONN_DEV];
  464. BOOLEAN auto_disc; /* internal use: true for auto discovering after connected */
  465. UINT8 srv_chg_mode; /* internal use: service change mode */
  466. tGATTS_RSP rsp; /* use to read internal service attribute */
  467. } tGATT_CB;
  468. typedef struct{
  469. UINT16 local_mtu;
  470. } tGATT_DEFAULT;
  471. #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4
  472. #ifdef __cplusplus
  473. extern "C" {
  474. #endif
  475. extern tGATT_DEFAULT gatt_default;
  476. /* Global GATT data */
  477. #if GATT_DYNAMIC_MEMORY == FALSE
  478. extern tGATT_CB gatt_cb;
  479. #else
  480. extern tGATT_CB *gatt_cb_ptr;
  481. #define gatt_cb (*gatt_cb_ptr)
  482. #endif
  483. #if GATT_CONFORMANCE_TESTING == TRUE
  484. extern void gatt_set_err_rsp(BOOLEAN enable, UINT8 req_op_code, UINT8 err_status);
  485. #endif
  486. #ifdef __cplusplus
  487. }
  488. #endif
  489. /* internal functions */
  490. extern void gatt_init (void);
  491. extern void gatt_free(void);
  492. /* from gatt_main.c */
  493. extern BOOLEAN gatt_disconnect (tGATT_TCB *p_tcb);
  494. extern BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr, tBLE_ADDR_TYPE bd_addr_type, tBT_TRANSPORT transport, BOOLEAN is_aux);
  495. extern BOOLEAN gatt_connect (BD_ADDR rem_bda, tBLE_ADDR_TYPE bd_addr_type, tGATT_TCB *p_tcb, tBT_TRANSPORT transport, BOOLEAN is_aux);
  496. extern void gatt_data_process (tGATT_TCB *p_tcb, BT_HDR *p_buf);
  497. extern void gatt_update_app_use_link_flag ( tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add, BOOLEAN check_acl_link);
  498. extern void gatt_profile_db_init(void);
  499. extern void gatt_set_ch_state(tGATT_TCB *p_tcb, tGATT_CH_STATE ch_state);
  500. extern tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB *p_tcb);
  501. extern void gatt_init_srv_chg(void);
  502. extern void gatt_proc_srv_chg (void);
  503. extern tGATT_STATUS gatt_send_srv_chg_ind (BD_ADDR peer_bda);
  504. extern void gatt_chk_srv_chg(tGATTS_SRV_CHG *p_srv_chg_clt);
  505. extern void gatt_add_a_bonded_dev_for_srv_chg (BD_ADDR bda);
  506. /* from gatt_attr.c */
  507. extern UINT16 gatt_profile_find_conn_id_by_bd_addr(BD_ADDR bda);
  508. /* Functions provided by att_protocol.c */
  509. extern tGATT_STATUS attp_send_cl_msg (tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code, tGATT_CL_MSG *p_msg);
  510. extern BT_HDR *attp_build_sr_msg(tGATT_TCB *p_tcb, UINT8 op_code, tGATT_SR_MSG *p_msg);
  511. extern tGATT_STATUS attp_send_sr_msg (tGATT_TCB *p_tcb, BT_HDR *p_msg);
  512. extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB *p_tcb, BT_HDR *p_toL2CAP);
  513. /* utility functions */
  514. extern UINT8 *gatt_dbg_op_name(UINT8 op_code);
  515. #if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
  516. extern UINT32 gatt_add_sdp_record (tBT_UUID *p_uuid, UINT16 start_hdl, UINT16 end_hdl);
  517. #endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE
  518. extern BOOLEAN gatt_parse_uuid_from_cmd(tBT_UUID *p_uuid, UINT16 len, UINT8 **p_data);
  519. extern UINT8 gatt_build_uuid_to_stream(UINT8 **p_dst, tBT_UUID uuid);
  520. extern BOOLEAN gatt_uuid_compare(tBT_UUID src, tBT_UUID tar);
  521. extern void gatt_convert_uuid32_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT32 uuid_32);
  522. extern char *gatt_uuid_to_str(const tBT_UUID *uuid);
  523. extern void gatt_sr_get_sec_info(BD_ADDR rem_bda, tBT_TRANSPORT transport, UINT8 *p_sec_flag, UINT8 *p_key_size);
  524. extern void gatt_start_rsp_timer(UINT16 clcb_idx);
  525. extern void gatt_start_conf_timer(tGATT_TCB *p_tcb);
  526. extern void gatt_rsp_timeout(TIMER_LIST_ENT *p_tle);
  527. extern void gatt_ind_ack_timeout(TIMER_LIST_ENT *p_tle);
  528. extern void gatt_start_ind_ack_timer(tGATT_TCB *p_tcb);
  529. extern tGATT_STATUS gatt_send_error_rsp(tGATT_TCB *p_tcb, UINT8 err_code, UINT8 op_code, UINT16 handle, BOOLEAN deq);
  530. extern void gatt_dbg_display_uuid(tBT_UUID bt_uuid);
  531. extern tGATT_PENDING_ENC_CLCB *gatt_add_pending_enc_channel_clcb(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb );
  532. extern tGATTS_PENDING_NEW_SRV_START *gatt_sr_is_new_srv_chg(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst);
  533. extern BOOLEAN gatt_is_srv_chg_ind_pending (tGATT_TCB *p_tcb);
  534. extern tGATTS_SRV_CHG *gatt_is_bda_in_the_srv_chg_clt_list (BD_ADDR bda);
  535. extern BOOLEAN gatt_find_the_connected_bda(UINT8 start_idx, BD_ADDR bda, UINT8 *p_found_idx, tBT_TRANSPORT *p_transport);
  536. extern void gatt_set_srv_chg(void);
  537. extern void gatt_delete_dev_from_srv_chg_clt_list(BD_ADDR bd_addr);
  538. extern tGATT_VALUE *gatt_add_pending_ind(tGATT_TCB *p_tcb, tGATT_VALUE *p_ind);
  539. extern tGATTS_PENDING_NEW_SRV_START *gatt_add_pending_new_srv_start( tGATTS_HNDL_RANGE *p_new_srv_start);
  540. extern void gatt_free_srvc_db_buffer_app_id(tBT_UUID *p_app_id);
  541. extern BOOLEAN gatt_update_listen_mode(void);
  542. extern BOOLEAN gatt_cl_send_next_cmd_inq(tGATT_TCB *p_tcb);
  543. /* reserved handle list */
  544. extern tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_app_id (tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst);
  545. extern tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_handle(UINT16 handle);
  546. extern tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_attr_handle(UINT16 attr_handle);
  547. extern tGATT_HDL_LIST_ELEM *gatt_alloc_hdl_buffer(void);
  548. extern void gatt_free_hdl_buffer(tGATT_HDL_LIST_ELEM *p);
  549. extern void gatt_free_attr_value_buffer(tGATT_HDL_LIST_ELEM *p);
  550. extern BOOLEAN gatt_is_last_attribute(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_start, tBT_UUID value);
  551. extern void gatt_update_last_pri_srv_info(tGATT_SRV_LIST_INFO *p_list);
  552. extern BOOLEAN gatt_add_a_srv_to_list(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_new);
  553. extern BOOLEAN gatt_remove_a_srv_from_list(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_remove);
  554. extern BOOLEAN gatt_add_an_item_to_list(tGATT_HDL_LIST_INFO *p_list, tGATT_HDL_LIST_ELEM *p_new);
  555. extern BOOLEAN gatt_remove_an_item_from_list(tGATT_HDL_LIST_INFO *p_list, tGATT_HDL_LIST_ELEM *p_remove);
  556. extern tGATTS_SRV_CHG *gatt_add_srv_chg_clt(tGATTS_SRV_CHG *p_srv_chg);
  557. /* for background connection */
  558. extern BOOLEAN gatt_update_auto_connect_dev (tGATT_IF gatt_if, BOOLEAN add, BD_ADDR bd_addr, BOOLEAN is_initiator);
  559. extern BOOLEAN gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV *p_dev, tGATT_IF gatt_if);
  560. extern BOOLEAN gatt_remove_bg_dev_for_app(tGATT_IF gatt_if, BD_ADDR bd_addr);
  561. extern UINT8 gatt_get_num_apps_for_bg_dev(BD_ADDR bd_addr);
  562. extern BOOLEAN gatt_find_app_for_bg_dev(BD_ADDR bd_addr, tGATT_IF *p_gatt_if);
  563. extern tGATT_BG_CONN_DEV *gatt_find_bg_dev(BD_ADDR remote_bda);
  564. extern void gatt_deregister_bgdev_list(tGATT_IF gatt_if);
  565. extern void gatt_reset_bgdev_list(void);
  566. /* server function */
  567. extern UINT8 gatt_sr_find_i_rcb_by_handle(UINT16 handle);
  568. extern UINT8 gatt_sr_find_i_rcb_by_app_id(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst);
  569. extern UINT8 gatt_sr_alloc_rcb(tGATT_HDL_LIST_ELEM *p_list);
  570. extern tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if, UINT32 trans_id, UINT8 op_code, tGATT_STATUS status, tGATTS_RSP *p_msg);
  571. extern void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
  572. UINT16 len, UINT8 *p_data);
  573. extern void gatt_sr_send_req_callback(UINT16 conn_id, UINT32 trans_id,
  574. UINT8 op_code, tGATTS_DATA *p_req_data);
  575. extern UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle);
  576. extern BOOLEAN gatt_cancel_open(tGATT_IF gatt_if, BD_ADDR bda);
  577. /* */
  578. extern tGATT_REG *gatt_get_regcb (tGATT_IF gatt_if);
  579. extern BOOLEAN gatt_is_clcb_allocated (UINT16 conn_id);
  580. extern tGATT_CLCB *gatt_clcb_alloc (UINT16 conn_id);
  581. extern void gatt_clcb_dealloc (tGATT_CLCB *p_clcb);
  582. extern tGATT_CLCB *gatt_clcb_find_by_conn_id(UINT16 conn_id);
  583. extern tGATT_CLCB *gatt_clcb_find_by_idx(UINT16 cclcb_idx);
  584. extern void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB *p_tcb );
  585. extern BOOLEAN gatt_sr_is_cback_cnt_zero(tGATT_TCB *p_tcb );
  586. extern BOOLEAN gatt_sr_is_prep_cnt_zero(tGATT_TCB *p_tcb );
  587. extern void gatt_sr_reset_cback_cnt(tGATT_TCB *p_tcb );
  588. extern void gatt_sr_reset_prep_cnt(tGATT_TCB *p_tcb );
  589. extern void gatt_sr_update_cback_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first);
  590. extern void gatt_sr_update_prep_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first);
  591. extern BOOLEAN gatt_find_app_hold_link(tGATT_TCB *p_tcb, UINT8 start_idx, UINT8 *p_found_idx, tGATT_IF *p_gatt_if);
  592. extern BOOLEAN gatt_find_specific_app_in_hold_link(tGATT_TCB *p_tcb, tGATT_IF p_gatt_if);
  593. extern UINT8 gatt_num_apps_hold_link(tGATT_TCB *p_tcb);
  594. extern UINT8 gatt_num_clcb_by_bd_addr(BD_ADDR bda);
  595. extern tGATT_TCB *gatt_find_tcb_by_cid(UINT16 lcid);
  596. extern tGATT_TCB *gatt_allocate_tcb_by_bdaddr(BD_ADDR bda, tBT_TRANSPORT transport);
  597. extern tGATT_TCB *gatt_get_tcb_by_idx(UINT8 tcb_idx);
  598. extern tGATT_TCB *gatt_find_tcb_by_addr(BD_ADDR bda, tBT_TRANSPORT transport);
  599. extern BOOLEAN gatt_send_ble_burst_data (BD_ADDR remote_bda, BT_HDR *p_buf);
  600. extern void gatt_tcb_free( tGATT_TCB *p_tcb);
  601. /* GATT client functions */
  602. extern void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb);
  603. extern UINT8 gatt_send_write_msg(tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code, UINT16 handle,
  604. UINT16 len, UINT16 offset, UINT8 *p_data);
  605. extern void gatt_cleanup_upon_disc(BD_ADDR bda, UINT16 reason, tBT_TRANSPORT transport);
  606. extern void gatt_end_operation(tGATT_CLCB *p_clcb, tGATT_STATUS status, void *p_data);
  607. extern void gatt_act_discovery(tGATT_CLCB *p_clcb);
  608. extern void gatt_act_read(tGATT_CLCB *p_clcb, UINT16 offset);
  609. extern void gatt_act_write(tGATT_CLCB *p_clcb, UINT8 sec_act);
  610. extern UINT8 gatt_act_send_browse(tGATT_TCB *p_tcb, UINT16 index, UINT8 op, UINT16 s_handle, UINT16 e_handle,
  611. tBT_UUID uuid);
  612. extern tGATT_CLCB *gatt_cmd_dequeue(tGATT_TCB *p_tcb, UINT8 *p_opcode);
  613. extern BOOLEAN gatt_cmd_enq(tGATT_TCB *p_tcb, UINT16 clcb_idx, BOOLEAN to_send, UINT8 op_code, BT_HDR *p_buf);
  614. extern void gatt_client_handle_server_rsp (tGATT_TCB *p_tcb, UINT8 op_code,
  615. UINT16 len, UINT8 *p_data);
  616. extern void gatt_send_queue_write_cancel (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, tGATT_EXEC_FLAG flag);
  617. /* gatt_auth.c */
  618. extern BOOLEAN gatt_security_check_start(tGATT_CLCB *p_clcb);
  619. extern void gatt_verify_signature(tGATT_TCB *p_tcb, BT_HDR *p_buf);
  620. extern tGATT_SEC_ACTION gatt_determine_sec_act(tGATT_CLCB *p_clcb );
  621. extern tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB *p_tcb);
  622. extern tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB *p_tcb);
  623. extern void gatt_set_sec_act(tGATT_TCB *p_tcb, tGATT_SEC_ACTION sec_act);
  624. /* gatt_db.c */
  625. extern BOOLEAN gatts_init_service_db (tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri, UINT16 s_hdl, UINT16 num_handle);
  626. extern UINT16 gatts_add_included_service (tGATT_SVC_DB *p_db, UINT16 s_handle, UINT16 e_handle, tBT_UUID service);
  627. extern UINT16 gatts_add_characteristic (tGATT_SVC_DB *p_db, tGATT_PERM perm,
  628. tGATT_CHAR_PROP property,
  629. tBT_UUID *p_char_uuid, tGATT_ATTR_VAL *attr_val,
  630. tGATTS_ATTR_CONTROL *control);
  631. extern UINT16 gatts_add_char_descr (tGATT_SVC_DB *p_db, tGATT_PERM perm,
  632. tBT_UUID *p_dscp_uuid, tGATT_ATTR_VAL *attr_val,
  633. tGATTS_ATTR_CONTROL *control);
  634. extern tGATT_STATUS gatts_set_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle,
  635. UINT16 length, UINT8 *value);
  636. extern tGATT_STATUS gatts_get_attr_value_internal(UINT16 attr_handle, UINT16 *length, UINT8 **value);
  637. extern tGATT_STATUS gatts_get_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle,
  638. UINT16 *length, UINT8 **value);
  639. extern BOOLEAN gatts_is_auto_response(UINT16 attr_handle);
  640. extern tGATT_STATUS gatts_db_read_attr_value_by_type (tGATT_TCB *p_tcb, tGATT_SVC_DB *p_db, UINT8 op_code, BT_HDR *p_rsp, UINT16 s_handle,
  641. UINT16 e_handle, tBT_UUID type, UINT16 *p_len, tGATT_SEC_FLAG sec_flag, UINT8 key_size, UINT32 trans_id, UINT16 *p_cur_handle);
  642. extern tGATT_STATUS gatts_read_attr_value_by_handle(tGATT_TCB *p_tcb, tGATT_SVC_DB *p_db, UINT8 op_code, UINT16 handle, UINT16 offset,
  643. UINT8 *p_value, UINT16 *p_len, UINT16 mtu, tGATT_SEC_FLAG sec_flag, UINT8 key_size, UINT32 trans_id);
  644. extern tGATT_STATUS gatts_write_attr_value_by_handle(tGATT_SVC_DB *p_db,
  645. UINT16 handle, UINT16 offset,
  646. UINT8 *p_value, UINT16 len);
  647. extern tGATT_STATUS gatts_write_attr_perm_check (tGATT_SVC_DB *p_db, UINT8 op_code, UINT16 handle, UINT16 offset, UINT8 *p_data,
  648. UINT16 len, tGATT_SEC_FLAG sec_flag, UINT8 key_size);
  649. extern tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB *p_db, BOOLEAN is_long, UINT16 handle, tGATT_SEC_FLAG sec_flag, UINT8 key_size);
  650. extern void gatts_update_srv_list_elem(UINT8 i_sreg, UINT16 handle, BOOLEAN is_primary);
  651. extern tBT_UUID *gatts_get_service_uuid (tGATT_SVC_DB *p_db);
  652. extern BOOLEAN gatt_check_connection_state_by_tcb(tGATT_TCB *p_tcb);
  653. extern void gatt_reset_bgdev_list(void);
  654. extern uint16_t gatt_get_local_mtu(void);
  655. extern void gatt_set_local_mtu(uint16_t mtu);
  656. extern tGATT_STATUS gatts_calculate_datebase_hash(BT_OCTET16 hash);
  657. extern void gatts_show_local_database(void);
  658. extern BOOLEAN gatt_sr_is_cl_change_aware(tGATT_TCB *p_tcb);
  659. extern void gatt_sr_init_cl_status(tGATT_TCB *p_tcb);
  660. extern void gatt_sr_update_cl_status(tGATT_TCB *tcb, BOOLEAN chg_aware);
  661. #endif