1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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  *
21  *  This is the private file for the BTA GATT server.
22  *
23  ******************************************************************************/
24 #ifndef BTA_GATTS_INT_H
25 #define BTA_GATTS_INT_H
26 
27 #include "common/bt_target.h"
28 #include "bta/bta_sys.h"
29 #include "bta/bta_gatt_api.h"
30 #include "stack/gatt_api.h"
31 
32 
33 /*****************************************************************************
34 **  Constants and data types
35 *****************************************************************************/
36 enum {
37     BTA_GATTS_API_REG_EVT  = BTA_SYS_EVT_START(BTA_ID_GATTS),
38     BTA_GATTS_INT_START_IF_EVT,
39     BTA_GATTS_API_DEREG_EVT,
40     BTA_GATTS_API_CREATE_SRVC_EVT,
41     BTA_GATTS_API_INDICATION_EVT,
42 
43     BTA_GATTS_API_ADD_INCL_SRVC_EVT,
44     BTA_GATTS_API_ADD_CHAR_EVT,
45     BTA_GATTS_API_ADD_DESCR_EVT,
46     BTA_GATTS_API_DEL_SRVC_EVT,
47     BTA_GATTS_API_START_SRVC_EVT,
48     BTA_GATTS_API_STOP_SRVC_EVT,
49     BTA_GATTS_API_RSP_EVT,
50     BTA_GATTS_API_SET_ATTR_VAL_EVT,
51     BTA_GATTS_API_OPEN_EVT,
52     BTA_GATTS_API_CANCEL_OPEN_EVT,
53     BTA_GATTS_API_CLOSE_EVT,
54     BTA_GATTS_API_LISTEN_EVT,
55     BTA_GATTS_API_DISABLE_EVT,
56     BTA_GATTS_API_SEND_SERVICE_CHANGE_EVT,
57     BTA_GATTS_API_SHOW_LOCAL_DATABASE_EVT
58 };
59 typedef UINT16 tBTA_GATTS_INT_EVT;
60 
61 /* max number of application allowed on device */
62 #define BTA_GATTS_MAX_APP_NUM   GATT_MAX_SR_PROFILES
63 
64 /* max number of services allowed in the device */
65 #define BTA_GATTS_MAX_SRVC_NUM   GATT_MAX_SR_PROFILES
66 
67 /* internal strucutre for GATTC register API  */
68 typedef struct {
69     BT_HDR                  hdr;
70     tBT_UUID                app_uuid;
71     tBTA_GATTS_CBACK        *p_cback;
72 } tBTA_GATTS_API_REG;
73 
74 typedef struct {
75     BT_HDR                  hdr;
76     tBTA_GATTS_IF           server_if;
77 } tBTA_GATTS_INT_START_IF;
78 
79 typedef tBTA_GATTS_INT_START_IF tBTA_GATTS_API_DEREG;
80 
81 typedef struct {
82     BT_HDR                  hdr;
83     tBTA_GATTS_IF           server_if;
84     tBT_UUID                service_uuid;
85     UINT16                  num_handle;
86     UINT8                   inst;
87     BOOLEAN                 is_pri;
88 
89 } tBTA_GATTS_API_CREATE_SRVC;
90 
91 typedef struct {
92     BT_HDR                  hdr;
93     tBT_UUID                char_uuid;
94     tBTA_GATT_PERM          perm;
95     tBTA_GATT_CHAR_PROP     property;
96     tBTA_GATTS_ATTR_CONTROL control;
97     tBTA_GATT_ATTR_VAL      attr_val;
98 } tBTA_GATTS_API_ADD_CHAR;
99 
100 typedef struct {
101     BT_HDR                  hdr;
102     UINT16                  included_service_id;
103 } tBTA_GATTS_API_ADD_INCL_SRVC;
104 
105 typedef struct {
106     BT_HDR                  hdr;
107     tBT_UUID                descr_uuid;
108     tBTA_GATT_PERM          perm;
109     tBTA_GATTS_ATTR_CONTROL control;
110     tBTA_GATT_ATTR_VAL      attr_val;
111 } tBTA_GATTS_API_ADD_DESCR;
112 
113 typedef struct {
114     BT_HDR  hdr;
115     UINT16  attr_id;
116     UINT16  len;
117     BOOLEAN need_confirm;
118     UINT8   value[BTA_GATT_MAX_ATTR_LEN];
119 } tBTA_GATTS_API_INDICATION;
120 
121 typedef struct {
122     BT_HDR              hdr;
123     UINT32              trans_id;
124     tBTA_GATT_STATUS    status;
125     tBTA_GATTS_RSP      *p_rsp;
126 } tBTA_GATTS_API_RSP;
127 
128 typedef struct{
129     BT_HDR              hdr;
130     UINT16 length;
131     UINT8 *value;
132 }tBTA_GATTS_API_SET_ATTR_VAL;
133 
134 typedef struct {
135     BT_HDR                  hdr;
136     tBTA_GATT_TRANSPORT     transport;
137 } tBTA_GATTS_API_START;
138 
139 
140 typedef struct {
141     BT_HDR                  hdr;
142     BD_ADDR                 remote_bda;
143     tBTA_GATTS_IF           server_if;
144     BOOLEAN                 is_direct;
145     tBTA_GATT_TRANSPORT     transport;
146 
147 } tBTA_GATTS_API_OPEN;
148 
149 typedef tBTA_GATTS_API_OPEN tBTA_GATTS_API_CANCEL_OPEN;
150 
151 typedef struct {
152     BT_HDR                  hdr;
153     BD_ADDR_PTR             remote_bda;
154     tBTA_GATTS_IF           server_if;
155     BOOLEAN                 start;
156 } tBTA_GATTS_API_LISTEN;
157 
158 typedef struct {
159     BT_HDR                  hdr;
160     tBTA_GATTS_IF           server_if;
161     BD_ADDR                 remote_bda;
162 } tBTA_GATTS_API_SEND_SERVICE_CHANGE;
163 
164 typedef union {
165     BT_HDR                          hdr;
166     tBTA_GATTS_API_REG              api_reg;
167     tBTA_GATTS_API_DEREG            api_dereg;
168     tBTA_GATTS_API_CREATE_SRVC      api_create_svc;
169     tBTA_GATTS_API_ADD_INCL_SRVC    api_add_incl_srvc;
170     tBTA_GATTS_API_ADD_CHAR         api_add_char;
171     tBTA_GATTS_API_ADD_DESCR        api_add_char_descr;
172     tBTA_GATTS_API_START            api_start;
173     tBTA_GATTS_API_INDICATION       api_indicate;
174     tBTA_GATTS_API_RSP              api_rsp;
175     tBTA_GATTS_API_SET_ATTR_VAL     api_set_val;
176     tBTA_GATTS_API_OPEN             api_open;
177     tBTA_GATTS_API_CANCEL_OPEN      api_cancel_open;
178 
179     tBTA_GATTS_INT_START_IF         int_start_if;
180     /* if peripheral role is supported */
181     tBTA_GATTS_API_LISTEN           api_listen;
182     tBTA_GATTS_API_SEND_SERVICE_CHANGE api_send_service_change;
183 } tBTA_GATTS_DATA;
184 
185 /* application registration control block */
186 typedef struct {
187     BOOLEAN             in_use;
188     tBT_UUID            app_uuid;
189     tBTA_GATTS_CBACK    *p_cback;
190     tBTA_GATTS_IF       gatt_if;
191 } tBTA_GATTS_RCB;
192 
193 /* service registration control block */
194 typedef struct {
195     tBT_UUID    service_uuid;   /* service UUID */
196     UINT16      service_id;     /* service handle */
197     UINT8       inst_num;       /* instance ID */
198     UINT8       rcb_idx;
199     UINT8       idx;            /* self index of serviec CB */
200     BOOLEAN     in_use;
201 
202 } tBTA_GATTS_SRVC_CB;
203 
204 
205 /* GATT server control block */
206 typedef struct {
207     BOOLEAN             enabled;
208     tBTA_GATTS_RCB      rcb[BTA_GATTS_MAX_APP_NUM];
209     tBTA_GATTS_SRVC_CB  srvc_cb[BTA_GATTS_MAX_SRVC_NUM];
210 } tBTA_GATTS_CB;
211 
212 
213 
214 /*****************************************************************************
215 **  Global data
216 *****************************************************************************/
217 
218 /* GATTC control block */
219 #if BTA_DYNAMIC_MEMORY == FALSE
220 extern tBTA_GATTS_CB  bta_gatts_cb;
221 #else
222 extern tBTA_GATTS_CB *bta_gatts_cb_ptr;
223 #define bta_gatts_cb (*bta_gatts_cb_ptr)
224 #endif
225 
226 /*****************************************************************************
227 **  Function prototypes
228 *****************************************************************************/
229 extern BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg);
230 
231 extern void bta_gatts_api_disable(tBTA_GATTS_CB *p_cb);
232 extern void bta_gatts_api_enable(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_data);
233 extern void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
234 extern void bta_gatts_start_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
235 extern void bta_gatts_deregister(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
236 extern void bta_gatts_create_srvc(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
237 extern void bta_gatts_add_include_srvc(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
238 extern void bta_gatts_add_char(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
239 extern void bta_gatts_add_char_descr(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
240 extern void bta_gatts_set_attr_value(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
241 extern tGATT_STATUS bta_gatts_get_attr_value(UINT16 attr_handle, UINT16 *length, UINT8 **value);
242 extern void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
243 extern void bta_gatts_start_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
244 extern void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg);
245 
246 extern void bta_gatts_send_rsp(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
247 extern void bta_gatts_indicate_handle (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
248 
249 
250 extern void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
251 extern void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
252 extern void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
253 extern void bta_gatts_listen(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
254 extern void bta_gatts_send_service_change_indication (tBTA_GATTS_DATA *p_msg);
255 extern void bta_gatts_show_local_database (void);
256 
257 extern BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src);
258 extern tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if);
259 extern UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if);
260 extern UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx);
261 extern tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id);
262 extern tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id);
263 extern void bta_gatts_deinit(void);
264 
265 #endif /* BTA_GATTS_INT_H */
266