From c10b209f62a52d2a782d4d8c97734b6c142f2567 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Sat, 5 Mar 2016 17:57:51 -0800 Subject: [PATCH] GATT cache storage refactoring Currently when storing GATT cache into file, we save it piece after piece, max 10 elements at once. This is not necessary, we have enough space to just save all at once. Also the logic doesn't need to be so complicated - no need to state machine for such simple task. Bug: 27455533 Change-Id: I82923dbedfb317835e45e13e0ea28151eb524d37 --- bta/Android.mk | 1 - bta/BUILD.gn | 1 - bta/gatt/bta_gattc_act.c | 151 ++---------------------- bta/gatt/bta_gattc_cache.c | 282 +++++++++++++++++++++++++++++++++------------ bta/gatt/bta_gattc_ci.c | 132 --------------------- bta/gatt/bta_gattc_int.h | 23 +--- bta/gatt/bta_gattc_main.c | 37 ------ bta/include/bta_gattc_ci.h | 119 ------------------- bta/include/bta_gattc_co.h | 114 ------------------ btif/Android.mk | 1 - btif/BUILD.gn | 1 - btif/co/bta_gattc_co.c | 211 --------------------------------- 12 files changed, 229 insertions(+), 844 deletions(-) delete mode 100644 bta/gatt/bta_gattc_ci.c delete mode 100644 bta/include/bta_gattc_ci.h delete mode 100644 bta/include/bta_gattc_co.h delete mode 100644 btif/co/bta_gattc_co.c diff --git a/bta/Android.mk b/bta/Android.mk index 5e6b38197..6dc6a3e8f 100644 --- a/bta/Android.mk +++ b/bta/Android.mk @@ -16,7 +16,6 @@ LOCAL_SRC_FILES:= \ ./gatt/bta_gatts_act.c \ ./gatt/bta_gatts_main.c \ ./gatt/bta_gattc_utils.c \ - ./gatt/bta_gattc_ci.c \ ./gatt/bta_gatts_api.c \ ./gatt/bta_gattc_main.c \ ./gatt/bta_gattc_act.c \ diff --git a/bta/BUILD.gn b/bta/BUILD.gn index 802645c05..64c62614d 100644 --- a/bta/BUILD.gn +++ b/bta/BUILD.gn @@ -45,7 +45,6 @@ static_library("bta") { "gatt/bta_gattc_act.c", "gatt/bta_gattc_api.c", "gatt/bta_gattc_cache.c", - "gatt/bta_gattc_ci.c", "gatt/bta_gattc_main.c", "gatt/bta_gattc_utils.c", "gatt/bta_gatts_act.c", diff --git a/bta/gatt/bta_gattc_act.c b/bta/gatt/bta_gattc_act.c index 64ea90ea0..5b63f754c 100644 --- a/bta/gatt/bta_gattc_act.c +++ b/bta/gatt/bta_gattc_act.c @@ -97,6 +97,9 @@ static const char *bta_gattc_op_code_name[] = ** Action Functions *****************************************************************************/ + +void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status); + /******************************************************************************* ** ** Function bta_gattc_enable @@ -711,7 +714,13 @@ void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) { p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD; - bta_gattc_sm_execute(p_clcb, BTA_GATTC_START_CACHE_EVT, NULL); + if (bta_gattc_cache_load(p_clcb)) { + bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK); + } else { + p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC; + /* cache load failure, start discovery */ + bta_gattc_start_discover(p_clcb, NULL); + } } else /* cache is building */ p_clcb->state = BTA_GATTC_DISCOVER_ST; @@ -1035,7 +1044,7 @@ void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) } /* used to reset cache in application */ - bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda); + bta_gattc_cache_reset(p_clcb->p_srcb->server_bda); } if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_list) { /* release pending attribute list buffer */ @@ -1582,143 +1591,7 @@ void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) { bta_gattc_enqueue(p_clcb, p_data); } -/******************************************************************************* -** -** Function bta_gattc_cache_open -** -** Description open a NV cache for loading -** -** Returns void -** -*******************************************************************************/ -void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) -{ - UNUSED(p_data); - - bta_gattc_set_discover_st(p_clcb->p_srcb); - - APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id); - bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT, - p_clcb->bta_conn_id, FALSE); -} -/******************************************************************************* -** -** Function bta_gattc_start_load -** -** Description start cache loading by sending callout open cache -** -** Returns None. -** -*******************************************************************************/ -void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) -{ - APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" , - p_clcb->bta_conn_id, p_clcb->p_srcb->state); - if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD) - { - if (p_data->ci_open.status == BTA_GATT_OK) - { - p_clcb->p_srcb->attr_index = 0; - bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda, - BTA_GATTC_CI_CACHE_LOAD_EVT, - p_clcb->p_srcb->attr_index, - p_clcb->bta_conn_id); - } - else - { - p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC; - /* cache open failure, start discovery */ - bta_gattc_start_discover(p_clcb, NULL); - } - } - if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE) - { - if (p_data->ci_open.status == BTA_GATT_OK) - { - if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id)) - { - p_data->ci_open.status = BTA_GATT_ERROR; - } - } - if (p_data->ci_open.status != BTA_GATT_OK) - { - p_clcb->p_srcb->attr_index = 0; - bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id); - bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status); - } - } -} -/******************************************************************************* -** -** Function bta_gattc_ci_load -** -** Description cache loading received. -** -** Returns None. -** -*******************************************************************************/ -void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) -{ - - APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d", - p_clcb->bta_conn_id, p_data->ci_load.status); - - if (p_data->ci_load.status == BTA_GATT_OK || - p_data->ci_load.status == BTA_GATT_MORE) - { - if (p_data->ci_load.num_attr != 0) - bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr, - p_data->ci_load.attr, p_clcb->p_srcb->attr_index); - - if (p_data->ci_load.status == BTA_GATT_OK) - { - p_clcb->p_srcb->attr_index = 0; - bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK); - bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0); - } - else /* load more */ - { - p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr; - - bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda, - BTA_GATTC_CI_CACHE_LOAD_EVT, - p_clcb->p_srcb->attr_index, - p_clcb->bta_conn_id); - } - } - else - { - bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0); - p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC; - p_clcb->p_srcb->attr_index = 0; - /* cache load failure, start discovery */ - bta_gattc_start_discover(p_clcb, NULL); - } -} -/******************************************************************************* -** -** Function bta_gattc_ci_save -** -** Description cache loading received. -** -** Returns None. -** -*******************************************************************************/ -void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) -{ - UNUSED(p_data); - - APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " , - p_clcb->bta_conn_id ); - - if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id)) - { - p_clcb->p_srcb->attr_index = 0; - bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0); - bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status); - } -} /******************************************************************************* ** ** Function bta_gattc_fail @@ -1893,7 +1766,7 @@ void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg) } } /* used to reset cache in application */ - bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda); + bta_gattc_cache_reset(p_msg->api_conn.remote_bda); } /******************************************************************************* diff --git a/bta/gatt/bta_gattc_cache.c b/bta/gatt/bta_gattc_cache.c index 6cfcdf472..1ef850103 100644 --- a/bta/gatt/bta_gattc_cache.c +++ b/bta/gatt/bta_gattc_cache.c @@ -29,24 +29,37 @@ #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) +#include +#include #include #include "bta_gattc_int.h" #include "bta_sys.h" #include "btm_api.h" #include "btm_ble_api.h" +#include "btm_int.h" #include "bt_common.h" #include "osi/include/log.h" #include "sdp_api.h" #include "sdpdefs.h" #include "utl.h" +static void bta_gattc_cache_write(BD_ADDR server_bda, UINT16 num_attr, tBTA_GATTC_NV_ATTR *attr); static void bta_gattc_char_dscpt_disc_cmpl(UINT16 conn_id, tBTA_GATTC_SERV *p_srvc_cb); static tBTA_GATT_STATUS bta_gattc_sdp_service_disc(UINT16 conn_id, tBTA_GATTC_SERV *p_server_cb); extern void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src); #define BTA_GATT_SDP_DB_SIZE 4096 +#define GATT_CACHE_PREFIX "/data/misc/bluetooth/gatt_cache_" +#define GATT_CACHE_VERSION 1 + +static void bta_gattc_generate_cache_file_name(char *buffer, BD_ADDR bda) +{ + sprintf(buffer, "%s%02x%02x%02x%02x%02x%02x", GATT_CACHE_PREFIX, + bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); +} + /***************************************************************************** ** Constants and data types *****************************************************************************/ @@ -524,8 +537,12 @@ static void bta_gattc_explore_srvc(UINT16 conn_id, tBTA_GATTC_SERV *p_srvc_cb) #endif /* save cache to NV */ p_clcb->p_srcb->state = BTA_GATTC_SERV_SAVE; - bta_gattc_co_cache_open(p_srvc_cb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT, - conn_id, TRUE); + + if (btm_sec_is_a_bonded_dev(p_srvc_cb->server_bda)) { + bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id); + } + + bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK); } /******************************************************************************* ** @@ -1392,6 +1409,34 @@ void bta_gattc_fill_gatt_db_el(btgatt_db_element_t *p_attr, } /******************************************************************************* +** Returns number of elements inside db from start_handle to end_handle +*******************************************************************************/ +static size_t bta_gattc_get_db_size(list_t *services, + UINT16 start_handle, UINT16 end_handle) { + if (!services || list_is_empty(services)) + return 0; + + size_t db_size = 0; + + for (list_node_t *sn = list_begin(services); + sn != list_end(services); sn = list_next(sn)) { + tBTA_GATTC_CACHE *p_cur_srvc = list_node(sn); + + if (p_cur_srvc->s_handle < start_handle) + continue; + + if (p_cur_srvc->e_handle > end_handle) + break; + + db_size++; + if (p_cur_srvc->p_attr) + db_size += list_length(p_cur_srvc->p_attr); + } + + return db_size; +} + +/******************************************************************************* ** ** Function bta_gattc_get_gatt_db_impl ** @@ -1413,28 +1458,13 @@ static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV *p_srvc_cb, { APPL_TRACE_DEBUG(LOG_TAG, "%s", __func__); - int db_size = 0; - if (!p_srvc_cb->p_srvc_cache || list_is_empty(p_srvc_cb->p_srvc_cache)) { *count = 0; *db = NULL; return; } - for (list_node_t *sn = list_begin(p_srvc_cb->p_srvc_cache); - sn != list_end(p_srvc_cb->p_srvc_cache); sn = list_next(sn)) { - tBTA_GATTC_CACHE *p_cur_srvc = list_node(sn); - - if (p_cur_srvc->s_handle < start_handle) - continue; - - if (p_cur_srvc->e_handle > end_handle) - break; - - db_size++; - if (p_cur_srvc->p_attr) - db_size += list_length(p_cur_srvc->p_attr); - } + size_t db_size = bta_gattc_get_db_size(p_srvc_cb->p_srvc_cache, start_handle, end_handle); void* buffer = osi_malloc(db_size * sizeof(btgatt_db_element_t)); btgatt_db_element_t *curr_db_attr = buffer; @@ -1555,17 +1585,14 @@ void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handl ** *******************************************************************************/ void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srvc_cb, UINT16 num_attr, - tBTA_GATTC_NV_ATTR *p_attr, UINT16 attr_index) + tBTA_GATTC_NV_ATTR *p_attr) { /* first attribute loading, initialize buffer */ APPL_TRACE_ERROR("%s: bta_gattc_rebuild_cache", __func__); - if (attr_index == 0) - { - if (p_srvc_cb->p_srvc_cache) - list_free(p_srvc_cb->p_srvc_cache); - p_srvc_cb->p_srvc_cache = NULL; - p_srvc_cb->p_cur_srvc = NULL; - } + + list_free(p_srvc_cb->p_srvc_cache); + p_srvc_cb->p_srvc_cache = NULL; + p_srvc_cb->p_cur_srvc = NULL; while (num_attr > 0 && p_attr != NULL) { @@ -1617,6 +1644,7 @@ void bta_gattc_fill_nv_attr(tBTA_GATTC_NV_ATTR *p_attr, UINT8 type, UINT16 s_han memcpy(&p_attr->uuid, &uuid, sizeof(tBT_UUID)); } + /******************************************************************************* ** ** Function bta_gattc_cache_save @@ -1626,64 +1654,176 @@ void bta_gattc_fill_nv_attr(tBTA_GATTC_NV_ATTR *p_attr, UINT8 type, UINT16 s_han ** Returns None. ** *******************************************************************************/ -BOOLEAN bta_gattc_cache_save(tBTA_GATTC_SERV *p_srvc_cb, UINT16 conn_id) +void bta_gattc_cache_save(tBTA_GATTC_SERV *p_srvc_cb, UINT16 conn_id) { - UINT8 i = 0; - UINT16 offset = 0; - tBTA_GATTC_NV_ATTR nv_attr[BTA_GATTC_NV_LOAD_MAX]; - - if (p_srvc_cb->p_srvc_cache && !list_is_empty(p_srvc_cb->p_srvc_cache)) { - for (list_node_t *sn = list_begin(p_srvc_cb->p_srvc_cache); - sn != list_end(p_srvc_cb->p_srvc_cache); sn = list_next(sn)) { - tBTA_GATTC_CACHE *p_cur_srvc = list_node(sn); + if (!p_srvc_cb->p_srvc_cache || list_is_empty(p_srvc_cb->p_srvc_cache)) + return; - if (i >= BTA_GATTC_NV_LOAD_MAX) - break; + int i = 0; + size_t db_size = bta_gattc_get_db_size(p_srvc_cb->p_srvc_cache, 0x0000, 0xFFFF); + tBTA_GATTC_NV_ATTR *nv_attr = osi_malloc(db_size * sizeof(tBTA_GATTC_NV_ATTR)); - if (offset ++ >= p_srvc_cb->attr_index) - bta_gattc_fill_nv_attr(&nv_attr[i++], - BTA_GATTC_ATTR_TYPE_SRVC, - p_cur_srvc->s_handle, - p_cur_srvc->e_handle, - p_cur_srvc->service_uuid.id.inst_id, - p_cur_srvc->service_uuid.id.uuid, - 0, - p_cur_srvc->service_uuid.is_primary); + for (list_node_t *sn = list_begin(p_srvc_cb->p_srvc_cache); + sn != list_end(p_srvc_cb->p_srvc_cache); sn = list_next(sn)) { + tBTA_GATTC_CACHE *p_cur_srvc = list_node(sn); - if (!p_cur_srvc->p_attr || list_is_empty(p_cur_srvc->p_attr)) - continue; + bta_gattc_fill_nv_attr(&nv_attr[i++], + BTA_GATTC_ATTR_TYPE_SRVC, + p_cur_srvc->s_handle, + p_cur_srvc->e_handle, + p_cur_srvc->service_uuid.id.inst_id, + p_cur_srvc->service_uuid.id.uuid, + 0, + p_cur_srvc->service_uuid.is_primary); - for (list_node_t *an = list_begin(p_cur_srvc->p_attr); - an != list_end(p_cur_srvc->p_attr); an = list_next(an)) { - tBTA_GATTC_CACHE_ATTR *p_attr = list_node(an); + if (!p_cur_srvc->p_attr || list_is_empty(p_cur_srvc->p_attr)) + continue; - if (i >= BTA_GATTC_NV_LOAD_MAX) - break; + for (list_node_t *an = list_begin(p_cur_srvc->p_attr); + an != list_end(p_cur_srvc->p_attr); an = list_next(an)) { + tBTA_GATTC_CACHE_ATTR *p_attr = list_node(an); - if (offset++ >= p_srvc_cb->attr_index) { - bta_gattc_fill_nv_attr(&nv_attr[i++], - p_attr->attr_type, - p_attr->attr_handle, - 0, - p_attr->inst_id, - p_attr->uuid, - p_attr->property, - FALSE); - } - } + bta_gattc_fill_nv_attr(&nv_attr[i++], + p_attr->attr_type, + p_attr->attr_handle, + 0, + p_attr->inst_id, + p_attr->uuid, + p_attr->property, + FALSE); } } - if (i > 0) { - bta_gattc_co_cache_save(p_srvc_cb->server_bda, BTA_GATTC_CI_CACHE_SAVE_EVT, i, - nv_attr, p_srvc_cb->attr_index, conn_id); + bta_gattc_cache_write(p_srvc_cb->server_bda, db_size, nv_attr); + osi_free(nv_attr); +} - p_srvc_cb->attr_index += i; +/******************************************************************************* +** +** Function bta_gattc_cache_load +** +** Description Load GATT cache from storage for server. +** +** Parameter p_clcb: pointer to server clcb, that will +** be filled from storage +** Returns true on success, false otherwise +** +*******************************************************************************/ +bool bta_gattc_cache_load(tBTA_GATTC_CLCB *p_clcb) +{ + char fname[255] = {0}; + bta_gattc_generate_cache_file_name(fname, p_clcb->p_srcb->server_bda); + + FILE *fd = fopen(fname, "rb"); + if (!fd) { + APPL_TRACE_ERROR("%s: can't open GATT cache file %s for reading, error: %s", + __func__, fname, strerror(errno)); + return false; + } - return TRUE; - } else { - return FALSE; + UINT16 cache_ver = 0; + tBTA_GATTC_NV_ATTR *attr = NULL; + bool success = false; + + if (fread(&cache_ver, sizeof(UINT16), 1, fd) != 1) { + APPL_TRACE_ERROR("%s: can't read GATT cache version from: %s", __func__, fname); + goto done; } + + if (cache_ver != GATT_CACHE_VERSION) { + APPL_TRACE_ERROR("%s: wrong GATT cache version: %s", __func__, fname); + goto done; + } + + UINT16 num_attr = 0; + + if (fread(&num_attr, sizeof(UINT16), 1, fd) != 1) { + APPL_TRACE_ERROR("%s: can't read number of GATT attributes: %s", __func__, fname); + goto done; + } + + attr = osi_malloc(sizeof(tBTA_GATTC_NV_ATTR) * num_attr); + + if (fread(attr, sizeof(tBTA_GATTC_NV_ATTR), 0xFF, fd) != num_attr) { + APPL_TRACE_ERROR("%s: can't read GATT attributes: %s", __func__, fname); + goto done; + } + + bta_gattc_rebuild_cache(p_clcb->p_srcb, num_attr, attr); + + success = true; + +done: + osi_free(attr); + fclose(fd); + return success; +} + +/******************************************************************************* +** +** Function bta_gattc_cache_write +** +** Description This callout function is executed by GATT when a server cache +** is available to save. +** +** Parameter server_bda: server bd address of this cache belongs to +** num_attr: number of attribute to be save. +** attr: pointer to the list of attributes to save. +** Returns +** +*******************************************************************************/ +static void bta_gattc_cache_write(BD_ADDR server_bda, UINT16 num_attr, + tBTA_GATTC_NV_ATTR *attr) +{ + char fname[255] = {0}; + bta_gattc_generate_cache_file_name(fname, server_bda); + + FILE *fd = fopen(fname, "wb"); + if (!fd) { + APPL_TRACE_ERROR("%s: can't open GATT cache file for writing: %s", __func__, fname); + return; + } + + UINT16 cache_ver = GATT_CACHE_VERSION; + if (fwrite(&cache_ver, sizeof(UINT16), 1, fd) != 1) { + APPL_TRACE_ERROR("%s: can't write GATT cache version: %s", __func__, fname); + fclose(fd); + return; + } + + if (fwrite(&num_attr, sizeof(UINT16), 1, fd) != 1) { + APPL_TRACE_ERROR("%s: can't write GATT cache attribute count: %s", __func__, fname); + fclose(fd); + return; + } + + if (fwrite(attr, sizeof(tBTA_GATTC_NV_ATTR), num_attr, fd) != num_attr) { + APPL_TRACE_ERROR("%s: can't write GATT cache attributes: %s", __func__, fname); + fclose(fd); + return; + } + + fclose(fd); +} + +/******************************************************************************* +** +** Function bta_gattc_cache_reset +** +** Description This callout function is executed by GATTC to reset cache in +** application +** +** Parameter server_bda: server bd address of this cache belongs to +** +** Returns void. +** +*******************************************************************************/ +void bta_gattc_cache_reset(BD_ADDR server_bda) +{ + BTIF_TRACE_DEBUG("%s", __func__); + char fname[255] = {0}; + bta_gattc_generate_cache_file_name(fname, server_bda); + unlink(fname); } #endif /* BTA_GATT_INCLUDED */ diff --git a/bta/gatt/bta_gattc_ci.c b/bta/gatt/bta_gattc_ci.c deleted file mode 100644 index 5997f16b8..000000000 --- a/bta/gatt/bta_gattc_ci.c +++ /dev/null @@ -1,132 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2010-2012 Broadcom Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -/****************************************************************************** - * - * This is the implementation file for the GATT call-in functions. - * - ******************************************************************************/ - -#include "bt_target.h" - -#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) - -#include - -#include "bta_api.h" -#include "bta_sys.h" -#include "bta_gattc_ci.h" -#include "bt_common.h" -#include "utl.h" - -/******************************************************************************* -** -** Function bta_gattc_ci_cache_open -** -** Description This function sends an event to indicate server cache open -** completed. -** -** Parameters server_bda - server BDA of this cache. -** status - BTA_GATT_OK if full buffer of data, -** BTA_GATT_FAIL if an error has occurred. -** -** Returns void -** -*******************************************************************************/ -void bta_gattc_ci_cache_open(BD_ADDR server_bda, UINT16 evt, tBTA_GATT_STATUS status, - UINT16 conn_id) -{ - tBTA_GATTC_CI_EVT *p_evt = - (tBTA_GATTC_CI_EVT *) osi_malloc(sizeof(tBTA_GATTC_CI_EVT)); - - UNUSED(server_bda); - - p_evt->hdr.event = evt; - p_evt->hdr.layer_specific = conn_id; - p_evt->status = status; - - bta_sys_sendmsg(p_evt); -} - -/******************************************************************************* -** -** Function bta_gattc_ci_cache_load -** -** Description This function sends an event to BTA indicating the phone has -** load the servere cache and ready to send it to the stack. -** -** Parameters server_bda - server BDA of this cache. -** num_bytes_read - number of bytes read into the buffer -** specified in the read callout-function. -** status - BTA_GATT_OK if full buffer of data, -** BTA_GATT_FAIL if an error has occurred. -** -** Returns void -** -*******************************************************************************/ -void bta_gattc_ci_cache_load(BD_ADDR server_bda, UINT16 evt, UINT16 num_attr, - tBTA_GATTC_NV_ATTR *p_attr, tBTA_GATT_STATUS status, - UINT16 conn_id) -{ - tBTA_GATTC_CI_LOAD *p_evt = - (tBTA_GATTC_CI_LOAD *)osi_calloc(sizeof(tBTA_GATTC_CI_LOAD)); - UNUSED(server_bda); - - p_evt->hdr.event = evt; - p_evt->hdr.layer_specific = conn_id; - p_evt->status = status; - p_evt->num_attr = (num_attr > BTA_GATTC_NV_LOAD_MAX) ? BTA_GATTC_NV_LOAD_MAX : num_attr; - - if (p_evt->num_attr > 0 && p_attr != NULL) { - memcpy(p_evt->attr, p_attr, p_evt->num_attr * sizeof(tBTA_GATTC_NV_ATTR)); - } - - bta_sys_sendmsg(p_evt); -} - -/******************************************************************************* -** -** Function bta_gattc_ci_cache_save -** -** Description This function sends an event to BTA indicating the phone has -** save the servere cache. -** -** Parameters server_bda - server BDA of this cache. -** evt - callin event code. -** status - BTA_GATT_OK if full buffer of data, -** BTA_GATT_ERROR if an error has occurred. -*8 conn_id - for this NV operation for. -** -** Returns void -** -*******************************************************************************/ -void bta_gattc_ci_cache_save(BD_ADDR server_bda, UINT16 evt, tBTA_GATT_STATUS status, - UINT16 conn_id) -{ - tBTA_GATTC_CI_EVT *p_evt = - (tBTA_GATTC_CI_EVT *)osi_malloc(sizeof(tBTA_GATTC_CI_EVT)); - - UNUSED(server_bda); - - p_evt->hdr.event = evt; - p_evt->hdr.layer_specific = conn_id; - p_evt->status = status; - - bta_sys_sendmsg(p_evt); -} -#endif /* BTA_GATT_INCLUDED */ diff --git a/bta/gatt/bta_gattc_int.h b/bta/gatt/bta_gattc_int.h index cdb83a68c..0a4482c03 100644 --- a/bta/gatt/bta_gattc_int.h +++ b/bta/gatt/bta_gattc_int.h @@ -29,8 +29,6 @@ #include "osi/include/fixed_queue.h" #include "bta_sys.h" #include "bta_gatt_api.h" -#include "bta_gattc_ci.h" -#include "bta_gattc_co.h" #include "bt_common.h" @@ -62,12 +60,6 @@ enum BTA_GATTC_OP_CMPL_EVT, BTA_GATTC_INT_DISCONN_EVT, - /* for cache loading/saving */ - BTA_GATTC_START_CACHE_EVT, - BTA_GATTC_CI_CACHE_OPEN_EVT, - BTA_GATTC_CI_CACHE_LOAD_EVT, - BTA_GATTC_CI_CACHE_SAVE_EVT, - BTA_GATTC_INT_START_IF_EVT, BTA_GATTC_API_REG_EVT, BTA_GATTC_API_DEREG_EVT, @@ -233,9 +225,6 @@ typedef union tBTA_GATTC_API_READ_MULTI api_read_multi; tBTA_GATTC_API_CFG_MTU api_mtu; tBTA_GATTC_OP_CMPL op_cmpl; - tBTA_GATTC_CI_EVT ci_open; - tBTA_GATTC_CI_EVT ci_save; - tBTA_GATTC_CI_LOAD ci_load; tBTA_GATTC_INT_CONN int_conn; tBTA_GATTC_ENC_CMPL enc_cmpl; @@ -470,10 +459,7 @@ extern void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); -extern void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_ci_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); -extern void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); -extern void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA * p_msg); extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg); @@ -526,13 +512,16 @@ extern tBTA_GATT_STATUS bta_gattc_query_cache(UINT16 conn_id, UINT8 query_type, tBTA_GATT_ID *p_output, void *p_param); extern void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, int *count); extern tBTA_GATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV *p_srvc_cb); -extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srcv, UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_attr, UINT16 attr_index); -extern BOOLEAN bta_gattc_cache_save(tBTA_GATTC_SERV *p_srvc_cb, UINT16 conn_id); - +extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srcv, UINT16 num_attr, tBTA_GATTC_NV_ATTR *attr); +extern void bta_gattc_cache_save(tBTA_GATTC_SERV *p_srvc_cb, UINT16 conn_id); +extern void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status); extern tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda); extern tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda); extern tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda); extern BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda); +extern bool bta_gattc_cache_load(tBTA_GATTC_CLCB *p_clcb); +extern void bta_gattc_cache_reset(BD_ADDR server_bda); + #endif /* BTA_GATTC_INT_H */ diff --git a/bta/gatt/bta_gattc_main.c b/bta/gatt/bta_gattc_main.c index 5f3d4f321..86d853635 100644 --- a/bta/gatt/bta_gattc_main.c +++ b/bta/gatt/bta_gattc_main.c @@ -62,10 +62,6 @@ enum BTA_GATTC_CONFIRM, BTA_GATTC_EXEC, BTA_GATTC_READ_MULTI, - BTA_GATTC_CI_OPEN, - BTA_GATTC_CI_LOAD, - BTA_GATTC_CI_SAVE, - BTA_GATTC_CACHE_OPEN, BTA_GATTC_IGNORE_OP_CMPL, BTA_GATTC_DISC_CLOSE, BTA_GATTC_RESTART_DISCOVER, @@ -101,10 +97,6 @@ const tBTA_GATTC_ACTION bta_gattc_action[] = bta_gattc_confirm, bta_gattc_execute, bta_gattc_read_multi, - bta_gattc_ci_open, - bta_gattc_ci_load, - bta_gattc_ci_save, - bta_gattc_cache_open, bta_gattc_ignore_op_cmpl, bta_gattc_disc_close, bta_gattc_restart_discover, @@ -144,12 +136,6 @@ static const UINT8 bta_gattc_st_idle[][BTA_GATTC_NUM_COLS] = /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, - -/* ===> for cache loading, saving */ -/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, -/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, -/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, -/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST} }; /* state table for wait for open state */ @@ -179,11 +165,6 @@ static const UINT8 bta_gattc_st_w4_conn[][BTA_GATTC_NUM_COLS] = /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST}, /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_OPEN_FAIL, BTA_GATTC_IDLE_ST}, -/* ===> for cache loading, saving */ -/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST}, -/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST}, -/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST}, -/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST} }; /* state table for open state */ @@ -214,11 +195,6 @@ static const UINT8 bta_gattc_st_connected[][BTA_GATTC_NUM_COLS] = /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, -/* ===> for cache loading, saving */ -/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_CACHE_OPEN, BTA_GATTC_DISCOVER_ST}, -/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST}, -/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST}, -/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST} }; /* state table for discover state */ @@ -248,11 +224,6 @@ static const UINT8 bta_gattc_st_discover[][BTA_GATTC_NUM_COLS] = /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE_OP_CMPL, BTA_GATTC_DISCOVER_ST}, /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, -/* ===> for cache loading, saving */ -/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_DISCOVER_ST}, -/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_CI_OPEN, BTA_GATTC_DISCOVER_ST}, -/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_CI_LOAD, BTA_GATTC_DISCOVER_ST}, -/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_CI_SAVE, BTA_GATTC_DISCOVER_ST} }; /* type for state table */ @@ -485,14 +456,6 @@ static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) return "BTA_GATTC_OP_CMPL_EVT"; case BTA_GATTC_INT_DISCONN_EVT: return "BTA_GATTC_INT_DISCONN_EVT"; - case BTA_GATTC_START_CACHE_EVT: - return "BTA_GATTC_START_CACHE_EVT"; - case BTA_GATTC_CI_CACHE_OPEN_EVT: - return "BTA_GATTC_CI_CACHE_OPEN_EVT"; - case BTA_GATTC_CI_CACHE_LOAD_EVT: - return "BTA_GATTC_CI_CACHE_LOAD_EVT"; - case BTA_GATTC_CI_CACHE_SAVE_EVT: - return "BTA_GATTC_CI_CACHE_SAVE_EVT"; case BTA_GATTC_INT_START_IF_EVT: return "BTA_GATTC_INT_START_IF_EVT"; case BTA_GATTC_API_REG_EVT: diff --git a/bta/include/bta_gattc_ci.h b/bta/include/bta_gattc_ci.h deleted file mode 100644 index a64bf89b0..000000000 --- a/bta/include/bta_gattc_ci.h +++ /dev/null @@ -1,119 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2003-2012 Broadcom Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -/****************************************************************************** - * - * This is the interface file for GATT call-in functions. - * - ******************************************************************************/ -#ifndef BTA_GATTC_CI_H -#define BTA_GATTC_CI_H - -#include "bta_gatt_api.h" - -/***************************************************************************** -** Constants and data types -*****************************************************************************/ - -/* Open Complete Event */ -typedef struct -{ - BT_HDR hdr; - tBTA_GATT_STATUS status; -} tBTA_GATTC_CI_EVT; - -#define BTA_GATTC_NV_LOAD_MAX 10 - -/* Read Ready Event */ -typedef struct -{ - BT_HDR hdr; - tBTA_GATT_STATUS status; - UINT16 num_attr; - tBTA_GATTC_NV_ATTR attr[BTA_GATTC_NV_LOAD_MAX]; -} tBTA_GATTC_CI_LOAD; - - -/***************************************************************************** -** Function Declarations -*****************************************************************************/ -#ifdef __cplusplus -extern "C" -{ -#endif - -/******************************************************************************* -** -** Function bta_gattc_ci_cache_open -** -** Description This function sends an event to indicate server cache open -** completed. -** -** Parameters server_bda - server BDA of this cache. -** status - BTA_GATT_OK if full buffer of data, -** BTA_GATT_FAIL if an error has occurred. -** -** Returns void -** -*******************************************************************************/ -extern void bta_gattc_ci_cache_open(BD_ADDR server_bda, UINT16 evt, - tBTA_GATT_STATUS status, UINT16 conn_id); - -/******************************************************************************* -** -** Function bta_gattc_ci_cache_load -** -** Description This function sends an event to BTA indicating the phone has -** load the servere cache and ready to send it to the stack. -** -** Parameters server_bda - server BDA of this cache. -** num_bytes_read - number of bytes read into the buffer -** specified in the read callout-function. -** status - BTA_GATT_OK if full buffer of data, -** BTA_GATT_FAIL if an error has occurred. -** -** Returns void -** -*******************************************************************************/ -extern void bta_gattc_ci_cache_load(BD_ADDR server_bda, UINT16 evt, - UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_atrr, - tBTA_GATT_STATUS status, UINT16 conn_id); - -/******************************************************************************* -** -** Function bta_gattc_ci_save -** -** Description This function sends an event to BTA indicating the phone has -** save the server cache. -** -** Parameters server_bda - server BDA of this cache. -** status - BTA_GATT_OK if full buffer of data, -** BTA_GATT_FAIL if an error has occurred. -** -** Returns void -** -*******************************************************************************/ -extern void bta_gattc_ci_cache_save(BD_ADDR server_bda, UINT16 evt, - tBTA_GATT_STATUS status, UINT16 conn_id); - - -#ifdef __cplusplus -} -#endif - -#endif /* BTA_GATTC_CI_H */ diff --git a/bta/include/bta_gattc_co.h b/bta/include/bta_gattc_co.h deleted file mode 100644 index b96ebf3b5..000000000 --- a/bta/include/bta_gattc_co.h +++ /dev/null @@ -1,114 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2009-2013 Broadcom Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -/****************************************************************************** - * - * This is the interface file for BTA GATT client call-out functions. - * - ******************************************************************************/ -#ifndef BTA_GATTC_CO_H -#define BTA_GATTC_CO_H - -#include "bta_gatt_api.h" - -/******************************************************************************* -** -** Function bta_gattc_co_cache_open -** -** Description This callout function is executed by GATTC when a GATT server -** cache is ready to be sent. -** -** Parameter server_bda: server bd address of this cache belongs to -** evt: call in event to be passed in when cache open is done. -** conn_id: connection ID of this cache operation attach to. -** to_save: open cache to save or to load. -** -** Returns void. -** -*******************************************************************************/ -extern void bta_gattc_co_cache_open(BD_ADDR server_bda, UINT16 evt, - UINT16 conn_id, BOOLEAN to_save); - -/******************************************************************************* -** -** Function bta_gattc_co_cache_close -** -** Description This callout function is executed by GATTC when a GATT server -** cache is written completely. -** -** Parameter server_bda: server bd address of this cache belongs to -** conn_id: connection ID of this cache operation attach to. -** -** Returns void. -** -*******************************************************************************/ -extern void bta_gattc_co_cache_close(BD_ADDR server_bda, UINT16 conn_id); - -/******************************************************************************* -** -** Function bta_gattc_co_cache_save -** -** Description This callout function is executed by GATT when a server cache -** is available to save. -** -** Parameter server_bda: server bd address of this cache belongs to -** evt: call in event to be passed in when cache save is done. -** num_attr: number of attribute to be save. -** p_attr: pointer to the list of attributes to save. -** attr_index: starting attribute index of the save operation. -** conn_id: connection ID of this cache operation attach to. -** Returns -** -*******************************************************************************/ -extern void bta_gattc_co_cache_save(BD_ADDR server_bda, UINT16 evt, - UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_attr, - UINT16 attr_index, UINT16 conn_id); - -/******************************************************************************* -** -** Function bta_gattc_co_cache_load -** -** Description This callout function is executed by GATT when server cache -** is required to load. -** -** Parameter server_bda: server bd address of this cache belongs to -** evt: call in event to be passed in when cache save is done. -** num_attr: number of attribute to be save. -** attr_index: starting attribute index of the save operation. -** conn_id: connection ID of this cache operation attach to. -** Returns -** -*******************************************************************************/ -extern void bta_gattc_co_cache_load(BD_ADDR server_bda, UINT16 evt, - UINT16 start_index, UINT16 conn_id); - -/******************************************************************************* -** -** Function bta_gattc_co_cache_reset -** -** Description This callout function is executed by GATTC to reset cache in -** application -** -** Parameter server_bda: server bd address of this cache belongs to -** -** Returns void. -** -*******************************************************************************/ -extern void bta_gattc_co_cache_reset(BD_ADDR server_bda); - -#endif /* BTA_GATT_CO_H */ diff --git a/btif/Android.mk b/btif/Android.mk index 5b5c3d17e..6628766b9 100644 --- a/btif/Android.mk +++ b/btif/Android.mk @@ -72,7 +72,6 @@ btifCommonSrc += \ co/bta_hh_co.c \ co/bta_hl_co.c \ co/bta_pan_co.c \ - co/bta_gattc_co.c \ co/bta_gatts_co.c # Tests diff --git a/btif/BUILD.gn b/btif/BUILD.gn index 7285c9aa1..d7171b0dd 100644 --- a/btif/BUILD.gn +++ b/btif/BUILD.gn @@ -65,7 +65,6 @@ static_library("btif") { "co/bta_hh_co.c", "co/bta_hl_co.c", "co/bta_pan_co.c", - "co/bta_gattc_co.c", "co/bta_gatts_co.c", ] diff --git a/btif/co/bta_gattc_co.c b/btif/co/bta_gattc_co.c deleted file mode 100644 index 90b92ed31..000000000 --- a/btif/co/bta_gattc_co.c +++ /dev/null @@ -1,211 +0,0 @@ -/****************************************************************************** - * - * Copyright (C) 2009-2013 Broadcom Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#include -#include - -#include "bt_common.h" -#include "bta_gattc_co.h" -#include "bta_gattc_ci.h" -#include "btif_util.h" -#include "btm_int.h" - -#if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE) -#if( defined BTA_GATT_INCLUDED ) && (BTA_GATT_INCLUDED == TRUE) - -#define GATT_CACHE_PREFIX "/data/misc/bluedroid/gatt_cache_" - -static FILE* sCacheFD = 0; - -static void getFilename(char *buffer, BD_ADDR bda) -{ - sprintf(buffer, "%s%02x%02x%02x%02x%02x%02x", GATT_CACHE_PREFIX - , bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); -} - -static void cacheClose() -{ - if (sCacheFD != 0) - { - fclose(sCacheFD); - sCacheFD = 0; - } -} - -static bool cacheOpen(BD_ADDR bda, bool to_save) -{ - char fname[255] = {0}; - getFilename(fname, bda); - - cacheClose(); - sCacheFD = fopen(fname, to_save ? "w" : "r"); - - return (sCacheFD != 0); -} - -static void cacheReset(BD_ADDR bda) -{ - char fname[255] = {0}; - getFilename(fname, bda); - unlink(fname); -} - - -/***************************************************************************** -** Function Declarations -*****************************************************************************/ - -/******************************************************************************* -** -** Function bta_gattc_co_cache_open -** -** Description This callout function is executed by GATTC when a GATT server -** cache is ready to be sent. -** -** Parameter server_bda: server bd address of this cache belongs to -** evt: call in event to be passed in when cache open is done. -** conn_id: connection ID of this cache operation attach to. -** to_save: open cache to save or to load. -** -** Returns void. -** -*******************************************************************************/ -void bta_gattc_co_cache_open(BD_ADDR server_bda, UINT16 evt, UINT16 conn_id, BOOLEAN to_save) -{ - /* open NV cache and send call in */ - tBTA_GATT_STATUS status = BTA_GATT_OK; - if (!btm_sec_is_a_bonded_dev(server_bda) || !cacheOpen(server_bda, to_save)) - status = BTA_GATT_ERROR; - - BTIF_TRACE_DEBUG("%s() - status=%d", __FUNCTION__, status); - bta_gattc_ci_cache_open(server_bda, evt, status, conn_id); -} - -/******************************************************************************* -** -** Function bta_gattc_co_cache_load -** -** Description This callout function is executed by GATT when server cache -** is required to load. -** -** Parameter server_bda: server bd address of this cache belongs to -** evt: call in event to be passed in when cache save is done. -** num_attr: number of attribute to be save. -** attr_index: starting attribute index of the save operation. -** conn_id: connection ID of this cache operation attach to. -** Returns -** -*******************************************************************************/ -void bta_gattc_co_cache_load(BD_ADDR server_bda, UINT16 evt, UINT16 start_index, UINT16 conn_id) -{ - UINT16 num_attr = 0; - tBTA_GATTC_NV_ATTR attr[BTA_GATTC_NV_LOAD_MAX]; - tBTA_GATT_STATUS status = BTA_GATT_ERROR; - - if (sCacheFD && (0 == fseek(sCacheFD, start_index * sizeof(tBTA_GATTC_NV_ATTR), SEEK_SET))) - { - num_attr = fread(attr, sizeof(tBTA_GATTC_NV_ATTR), BTA_GATTC_NV_LOAD_MAX, sCacheFD); - if (start_index == 0 && num_attr < 1) - status = BTA_GATT_ERROR; // Cache truncated - else - status = (num_attr < BTA_GATTC_NV_LOAD_MAX ? BTA_GATT_OK : BTA_GATT_MORE); - } - - BTIF_TRACE_DEBUG("%s() - sCacheFD=%p, start_index=%d, read=%d, status=%d", - __FUNCTION__, sCacheFD, start_index, num_attr, status); - bta_gattc_ci_cache_load(server_bda, evt, num_attr, attr, status, conn_id); -} - -/******************************************************************************* -** -** Function bta_gattc_co_cache_save -** -** Description This callout function is executed by GATT when a server cache -** is available to save. -** -** Parameter server_bda: server bd address of this cache belongs to -** evt: call in event to be passed in when cache save is done. -** num_attr: number of attribute to be save. -** p_attr: pointer to the list of attributes to save. -** attr_index: starting attribute index of the save operation. -** conn_id: connection ID of this cache operation attach to. -** Returns -** -*******************************************************************************/ -void 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) -{ - tBTA_GATT_STATUS status = BTA_GATT_OK; - UNUSED(attr_index); - - if (sCacheFD != 0) - { - int num = fwrite(p_attr_list, sizeof(tBTA_GATTC_NV_ATTR), num_attr, sCacheFD); - BTIF_TRACE_DEBUG("%s() wrote %d", __FUNCTION__, num); - } - - bta_gattc_ci_cache_save(server_bda, evt, status, conn_id); -} - -/******************************************************************************* -** -** Function bta_gattc_co_cache_close -** -** Description This callout function is executed by GATTC when a GATT server -** cache is written completely. -** -** Parameter server_bda: server bd address of this cache belongs to -** conn_id: connection ID of this cache operation attach to. -** -** Returns void. -** -*******************************************************************************/ -void bta_gattc_co_cache_close(BD_ADDR server_bda, UINT16 conn_id) -{ - UNUSED(server_bda); - UNUSED(conn_id); - - cacheClose(); - - /* close NV when server cache is done saving or loading, - does not need to do anything for now on Insight */ - - BTIF_TRACE_DEBUG("%s()", __FUNCTION__); -} - -/******************************************************************************* -** -** Function bta_gattc_co_cache_reset -** -** Description This callout function is executed by GATTC to reset cache in -** application -** -** Parameter server_bda: server bd address of this cache belongs to -** -** Returns void. -** -*******************************************************************************/ -void bta_gattc_co_cache_reset(BD_ADDR server_bda) -{ - BTIF_TRACE_DEBUG("%s()", __FUNCTION__); - cacheReset(server_bda); -} - -#endif -#endif - -- 2.11.0