1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2013 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 #include "bta/bta_api.h"
20 
21 #if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE)
22 #if( defined GATTS_INCLUDED ) && (GATTS_INCLUDED == TRUE)
23 
24 #include <stdlib.h>
25 #include <string.h>
26 #include "bta/bta_gatts_co.h"
27 #include "btc/btc_storage.h"
28 #include "btc/btc_ble_storage.h"
29 // #include "btif_util.h"
30 
31 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
32 /*****************************************************************************
33 **  Local type definitions
34 *****************************************************************************/
35 
36 #define BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE 50
37 
38 typedef struct {
39     BOOLEAN             enable;
40     UINT8               num_clients;
41     tBTA_GATTS_SRV_CHG  srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE];
42 } __attribute__((packed)) btif_gatts_srv_chg_cb_t;
43 
44 /*****************************************************************************
45 **  Static variables
46 *****************************************************************************/
47 
48 static btif_gatts_srv_chg_cb_t btif_gatts_srv_chg_cb;
49 
50 /*****************************************************************************
51 **  Static functions
52 *****************************************************************************/
53 
btif_gatts_check_init(void)54 static void btif_gatts_check_init(void)
55 {
56     btif_gatts_srv_chg_cb_t *p_cb = &btif_gatts_srv_chg_cb;
57 
58     if (!p_cb->enable) {
59         memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t));
60         p_cb->enable = TRUE;
61     }
62 }
63 
64 /*****************************************************************************
65 **  Externally called functions
66 *****************************************************************************/
67 
btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda)68 void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda)
69 {
70     btif_gatts_srv_chg_cb_t *p_cb = &btif_gatts_srv_chg_cb;
71     BOOLEAN                 found = FALSE;
72     UINT8                   i;
73 
74     btif_gatts_check_init();
75 
76     for (i = 0; i != p_cb->num_clients; ++i) {
77         if (!memcmp(p_cb->srv_chg[i].bda,  bda, sizeof(BD_ADDR))) {
78             found = TRUE;
79             break;
80         }
81     }
82 
83     if (!found) {
84         if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE) {
85             bdcpy(p_cb->srv_chg[p_cb->num_clients].bda, bda);
86             p_cb->srv_chg[p_cb->num_clients].srv_changed = FALSE;
87             p_cb->num_clients++;
88         }
89     }
90 }
91 
92 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
93 /*****************************************************************************
94 **  Call-out functions
95 *****************************************************************************/
96 
97 /*******************************************************************************
98 **
99 ** Function         bta_gatts_co_update_handle_range
100 **
101 ** Description      This callout function is executed by GATTS when a GATT server
102 **                  handle range ios to be added or removed.
103 **
104 ** Parameter        is_add: true is to add a handle range; otherwise is to delete.
105 **                  p_hndl_range: handle range.
106 **
107 ** Returns          void.
108 **
109 *******************************************************************************/
bta_gatts_co_update_handle_range(BOOLEAN is_add,tBTA_GATTS_HNDL_RANGE * p_hndl_range)110 void bta_gatts_co_update_handle_range(BOOLEAN is_add, tBTA_GATTS_HNDL_RANGE *p_hndl_range)
111 {
112     UNUSED(is_add);
113     UNUSED(p_hndl_range);
114 }
115 
116 /*******************************************************************************
117 **
118 ** Function         bta_gatts_co_srv_chg
119 **
120 ** Description      This call-out is to read/write/remove service change related
121 **                  informaiton. The request consists of the cmd and p_req and the
122 **                  response is returned in p_rsp
123 **
124 ** Parameter        cmd - request command
125 **                  p_req - request paramters
126 **                  p_rsp - response data for the request
127 **
128 ** Returns          TRUE - if the request is processed successfully and
129 **                         the response is returned in p_rsp.
130 **                  FALSE - if the request can not be processed
131 **
132 *******************************************************************************/
bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,tBTA_GATTS_SRV_CHG_REQ * p_req,tBTA_GATTS_SRV_CHG_RSP * p_rsp)133 BOOLEAN bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,
134                              tBTA_GATTS_SRV_CHG_REQ *p_req,
135                              tBTA_GATTS_SRV_CHG_RSP *p_rsp)
136 {
137     UNUSED(cmd);
138     UNUSED(p_req);
139     UNUSED(p_rsp);
140 
141     return FALSE;
142 }
143 
144 /*******************************************************************************
145 **
146 ** Function         bta_gatts_co_load_handle_range
147 **
148 ** Description      This callout function is executed by GATTS when a GATT server
149 **                  handle range is requested to be loaded from NV.
150 **
151 ** Parameter
152 **
153 ** Returns          void.
154 **
155 *******************************************************************************/
bta_gatts_co_load_handle_range(UINT8 index,tBTA_GATTS_HNDL_RANGE * p_handle_range)156 BOOLEAN bta_gatts_co_load_handle_range(UINT8 index,
157                                        tBTA_GATTS_HNDL_RANGE *p_handle_range)
158 {
159     UNUSED(index);
160     UNUSED(p_handle_range);
161 
162     return FALSE;
163 }
164 
165 #if (SMP_INCLUDED == TRUE)
166 /*******************************************************************************
167 **
168 ** Function         bta_gatts_co_cl_feat_save
169 **
170 ** Description      This callout function is executed by GATTS when GATT server
171 **                  client support feature is requested to write to NV.
172 **
173 ** Parameter        remote_addr - remote device address
174 **                  feature - pointer of client support feature
175 **
176 ** Returns          void.
177 **
178 *******************************************************************************/
bta_gatts_co_cl_feat_save(BD_ADDR remote_addr,UINT8 * feature)179 void bta_gatts_co_cl_feat_save(BD_ADDR remote_addr, UINT8 *feature)
180 {
181     bt_bdaddr_t bd_addr;
182 
183     memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
184     btc_storage_set_gatt_cl_supp_feat(&bd_addr, feature, 1);
185 }
186 
187 /*******************************************************************************
188 **
189 ** Function         bta_gatts_co_db_hash_save
190 **
191 ** Description      This callout function is executed by GATTS when GATT server
192 **                  client status is requested to write to NV.
193 **
194 ** Parameter        remote_addr - remote device address
195 **                  db_hash - pointer of GATT service datebase hash
196 **
197 ** Returns          void.
198 **
199 *******************************************************************************/
bta_gatts_co_db_hash_save(BD_ADDR remote_addr,BT_OCTET16 db_hash)200 void bta_gatts_co_db_hash_save(BD_ADDR remote_addr, BT_OCTET16 db_hash)
201 {
202     bt_bdaddr_t bd_addr;
203 
204     memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
205     btc_storage_set_gatt_db_hash(&bd_addr, db_hash, BT_OCTET16_LEN);
206 }
207 
208 /*******************************************************************************
209 **
210 ** Function         bta_gatts_co_cl_feat_load
211 **
212 ** Description      This callout function is executed by GATTS when GATT server
213 **                  client status is requested to load from NV.
214 **
215 ** Parameter        remote_addr - remote device address
216 **                  feature - pointer of GATT service datebase hash
217 **
218 ** Returns          void.
219 **
220 *******************************************************************************/
bta_gatts_co_cl_feat_load(BD_ADDR remote_addr,UINT8 * feature)221 void bta_gatts_co_cl_feat_load(BD_ADDR remote_addr, UINT8 *feature)
222 {
223     bt_bdaddr_t bd_addr;
224 
225     memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
226     btc_storage_get_gatt_cl_supp_feat(&bd_addr, feature, 1);
227 }
228 
229 /*******************************************************************************
230 **
231 ** Function         bta_gatts_co_db_hash_load
232 **
233 ** Description      This callout function is executed by GATTS when GATT server
234 **                  client status is requested to load from NV.
235 **
236 ** Parameter        remote_addr - remote device address
237 **                  db_hash - pointer of GATT service datebase hash
238 **
239 ** Returns          void.
240 **
241 *******************************************************************************/
bta_gatts_co_db_hash_load(BD_ADDR remote_addr,BT_OCTET16 db_hash)242 void bta_gatts_co_db_hash_load(BD_ADDR remote_addr, BT_OCTET16 db_hash)
243 {
244     bt_bdaddr_t bd_addr;
245 
246     memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
247     btc_storage_get_gatt_db_hash(&bd_addr, db_hash, BT_OCTET16_LEN);
248 }
249 #endif // #if (SMP_INCLUDED == TRUE)
250 #endif // #if (GATTS_INCLUDED == TRUE)
251 #endif // #if (BLE_INCLUDED == TRUE)
252