• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "gki.h"
21 #include "bta_gattc_co.h"
22 #include "bta_gattc_ci.h"
23 #include "btif_util.h"
24 
25 #if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE)
26 #if( defined BTA_GATT_INCLUDED ) && (BTA_GATT_INCLUDED == TRUE)
27 
28 #define GATT_CACHE_PREFIX "/data/misc/bluedroid/gatt_cache_"
29 
30 static FILE* sCacheFD = 0;
31 
getFilename(char * buffer,BD_ADDR bda)32 static void getFilename(char *buffer, BD_ADDR bda)
33 {
34     sprintf(buffer, "%s%02x%02x%02x%02x%02x%02x", GATT_CACHE_PREFIX
35         , bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
36 }
37 
cacheClose()38 static void cacheClose()
39 {
40     if (sCacheFD != 0)
41     {
42         fclose(sCacheFD);
43         sCacheFD = 0;
44     }
45 }
46 
cacheOpen(BD_ADDR bda,bool to_save)47 static bool cacheOpen(BD_ADDR bda, bool to_save)
48 {
49     char fname[255] = {0};
50     getFilename(fname, bda);
51 
52     cacheClose();
53     sCacheFD = fopen(fname, to_save ? "w" : "r");
54 
55     return (sCacheFD != 0);
56 }
57 
cacheReset(BD_ADDR bda)58 static void cacheReset(BD_ADDR bda)
59 {
60     char fname[255] = {0};
61     getFilename(fname, bda);
62     unlink(fname);
63 }
64 
65 
66 /*****************************************************************************
67 **  Function Declarations
68 *****************************************************************************/
69 
70 /*******************************************************************************
71 **
72 ** Function         bta_gattc_co_cache_open
73 **
74 ** Description      This callout function is executed by GATTC when a GATT server
75 **                  cache is ready to be sent.
76 **
77 ** Parameter        server_bda: server bd address of this cache belongs to
78 **                  evt: call in event to be passed in when cache open is done.
79 **                  conn_id: connection ID of this cache operation attach to.
80 **                  to_save: open cache to save or to load.
81 **
82 ** Returns          void.
83 **
84 *******************************************************************************/
bta_gattc_co_cache_open(BD_ADDR server_bda,UINT16 evt,UINT16 conn_id,BOOLEAN to_save)85 void bta_gattc_co_cache_open(BD_ADDR server_bda, UINT16 evt, UINT16 conn_id, BOOLEAN to_save)
86 {
87     /* open NV cache and send call in */
88     tBTA_GATT_STATUS    status = BTA_GATT_OK;
89     if (!cacheOpen(server_bda, to_save))
90         status = BTA_GATT_ERROR;
91 
92     BTIF_TRACE_DEBUG("%s() - status=%d", __FUNCTION__, status);
93     bta_gattc_ci_cache_open(server_bda, evt, status, conn_id);
94 }
95 
96 /*******************************************************************************
97 **
98 ** Function         bta_gattc_co_cache_load
99 **
100 ** Description      This callout function is executed by GATT when server cache
101 **                  is required to load.
102 **
103 ** Parameter        server_bda: server bd address of this cache belongs to
104 **                  evt: call in event to be passed in when cache save is done.
105 **                  num_attr: number of attribute to be save.
106 **                  attr_index: starting attribute index of the save operation.
107 **                  conn_id: connection ID of this cache operation attach to.
108 ** Returns
109 **
110 *******************************************************************************/
bta_gattc_co_cache_load(BD_ADDR server_bda,UINT16 evt,UINT16 start_index,UINT16 conn_id)111 void bta_gattc_co_cache_load(BD_ADDR server_bda, UINT16 evt, UINT16 start_index, UINT16 conn_id)
112 {
113     UINT16              num_attr = 0;
114     tBTA_GATTC_NV_ATTR  attr[BTA_GATTC_NV_LOAD_MAX];
115     tBTA_GATT_STATUS    status = BTA_GATT_ERROR;
116 
117     if (sCacheFD && (0 == fseek(sCacheFD, start_index * sizeof(tBTA_GATTC_NV_ATTR), SEEK_SET)))
118     {
119         num_attr = fread(attr, sizeof(tBTA_GATTC_NV_ATTR), BTA_GATTC_NV_LOAD_MAX, sCacheFD);
120         status = (num_attr < BTA_GATTC_NV_LOAD_MAX ? BTA_GATT_OK : BTA_GATT_MORE);
121     }
122 
123     BTIF_TRACE_DEBUG("%s() - sCacheFD=%p, start_index=%d, read=%d, status=%d",
124         __FUNCTION__, sCacheFD, start_index, num_attr, status);
125     bta_gattc_ci_cache_load(server_bda, evt, num_attr, attr, status, conn_id);
126 }
127 
128 /*******************************************************************************
129 **
130 ** Function         bta_gattc_co_cache_save
131 **
132 ** Description      This callout function is executed by GATT when a server cache
133 **                  is available to save.
134 **
135 ** Parameter        server_bda: server bd address of this cache belongs to
136 **                  evt: call in event to be passed in when cache save is done.
137 **                  num_attr: number of attribute to be save.
138 **                  p_attr: pointer to the list of attributes to save.
139 **                  attr_index: starting attribute index of the save operation.
140 **                  conn_id: connection ID of this cache operation attach to.
141 ** Returns
142 **
143 *******************************************************************************/
bta_gattc_co_cache_save(BD_ADDR server_bda,UINT16 evt,UINT16 num_attr,tBTA_GATTC_NV_ATTR * p_attr_list,UINT16 attr_index,UINT16 conn_id)144 void bta_gattc_co_cache_save (BD_ADDR server_bda, UINT16 evt, UINT16 num_attr,
145                               tBTA_GATTC_NV_ATTR *p_attr_list, UINT16 attr_index, UINT16 conn_id)
146 {
147     tBTA_GATT_STATUS    status = BTA_GATT_OK;
148     UNUSED(attr_index);
149 
150     if (sCacheFD != 0)
151     {
152         int num = fwrite(p_attr_list, sizeof(tBTA_GATTC_NV_ATTR), num_attr, sCacheFD);
153         BTIF_TRACE_DEBUG("%s() wrote %d", __FUNCTION__, num);
154     }
155 
156     bta_gattc_ci_cache_save(server_bda, evt, status, conn_id);
157 }
158 
159 /*******************************************************************************
160 **
161 ** Function         bta_gattc_co_cache_close
162 **
163 ** Description      This callout function is executed by GATTC when a GATT server
164 **                  cache is written completely.
165 **
166 ** Parameter        server_bda: server bd address of this cache belongs to
167 **                  conn_id: connection ID of this cache operation attach to.
168 **
169 ** Returns          void.
170 **
171 *******************************************************************************/
bta_gattc_co_cache_close(BD_ADDR server_bda,UINT16 conn_id)172 void bta_gattc_co_cache_close(BD_ADDR server_bda, UINT16 conn_id)
173 {
174     UNUSED(server_bda);
175     UNUSED(conn_id);
176 
177     cacheClose();
178 
179     /* close NV when server cache is done saving or loading,
180        does not need to do anything for now on Insight */
181 
182     BTIF_TRACE_DEBUG("%s()", __FUNCTION__);
183 }
184 
185 /*******************************************************************************
186 **
187 ** Function         bta_gattc_co_cache_reset
188 **
189 ** Description      This callout function is executed by GATTC to reset cache in
190 **                  application
191 **
192 ** Parameter        server_bda: server bd address of this cache belongs to
193 **
194 ** Returns          void.
195 **
196 *******************************************************************************/
bta_gattc_co_cache_reset(BD_ADDR server_bda)197 void bta_gattc_co_cache_reset(BD_ADDR server_bda)
198 {
199     BTIF_TRACE_DEBUG("%s()", __FUNCTION__);
200     cacheReset(server_bda);
201 }
202 
203 #endif
204 #endif
205 
206