Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f69e3d15 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by Jakub Pawłowski
Browse files

bta_gattc_cache_write refactor

Bug: 154056389
Test: compilation
Tag: #feature
Change-Id: I46512626311dfdbb480b4d47edb59b21670b2f02
parent 1ed87e94
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -218,7 +218,7 @@ static void bta_gattc_explore_srvc_finished(uint16_t conn_id,


  if (btm_sec_is_a_bonded_dev(p_srvc_cb->server_bda)) {
  if (btm_sec_is_a_bonded_dev(p_srvc_cb->server_bda)) {
    bta_gattc_cache_write(p_clcb->p_srcb->server_bda,
    bta_gattc_cache_write(p_clcb->p_srcb->server_bda,
                          p_clcb->p_srcb->gatt_database.Serialize());
                          p_clcb->p_srcb->gatt_database);
  }
  }


  // After success, reset the count.
  // After success, reset the count.
+31 −14
Original line number Original line Diff line number Diff line
@@ -90,33 +90,30 @@ done:


/*******************************************************************************
/*******************************************************************************
 *
 *
 * Function         bta_gattc_cache_write
 * Function         bta_gattc_store_db
 *
 *
 * Description      This callout function is executed by GATT when a server
 * Description      Storess GATT db.
 *                  cache is available to save.
 *
 *
 * Parameter        server_bda: server bd address of this cache belongs to
 * Parameter        fname: output file name
 *                  attr: attributes to save.
 *                  attr: attributes to save.
 * Returns
 *
 * Returns          true on success, false otherwise
 *
 *
 ******************************************************************************/
 ******************************************************************************/
void bta_gattc_cache_write(const RawAddress& server_bda,
static bool bta_gattc_store_db(
                           const std::vector<StoredAttribute>& attr) {
    const char* fname, const std::vector<StoredAttribute>& attr) {
  char fname[255] = {0};
  bta_gattc_generate_cache_file_name(fname, sizeof(fname), server_bda);

  FILE* fd = fopen(fname, "wb");
  FILE* fd = fopen(fname, "wb");
  if (!fd) {
  if (!fd) {
    LOG(ERROR) << __func__
    LOG(ERROR) << __func__
               << ": can't open GATT cache file for writing: " << fname;
               << ": can't open GATT cache file for writing: " << fname;
    return;
    return false;
  }
  }


  uint16_t cache_ver = GATT_CACHE_VERSION;
  uint16_t cache_ver = GATT_CACHE_VERSION;
  if (fwrite(&cache_ver, sizeof(uint16_t), 1, fd) != 1) {
  if (fwrite(&cache_ver, sizeof(uint16_t), 1, fd) != 1) {
    LOG(ERROR) << __func__ << ": can't write GATT cache version: " << fname;
    LOG(ERROR) << __func__ << ": can't write GATT cache version: " << fname;
    fclose(fd);
    fclose(fd);
    return;
    return false;
  }
  }


  uint16_t num_attr = attr.size();
  uint16_t num_attr = attr.size();
@@ -124,16 +121,36 @@ void bta_gattc_cache_write(const RawAddress& server_bda,
    LOG(ERROR) << __func__
    LOG(ERROR) << __func__
               << ": can't write GATT cache attribute count: " << fname;
               << ": can't write GATT cache attribute count: " << fname;
    fclose(fd);
    fclose(fd);
    return;
    return false;
  }
  }


  if (fwrite(attr.data(), sizeof(StoredAttribute), num_attr, fd) != num_attr) {
  if (fwrite(attr.data(), sizeof(StoredAttribute), num_attr, fd) != num_attr) {
    LOG(ERROR) << __func__ << ": can't write GATT cache attributes: " << fname;
    LOG(ERROR) << __func__ << ": can't write GATT cache attributes: " << fname;
    fclose(fd);
    fclose(fd);
    return;
    return false;
  }
  }


  fclose(fd);
  fclose(fd);
  return true;
}

/*******************************************************************************
 *
 * 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
 *                  database: attributes to save.
 * Returns
 *
 ******************************************************************************/
void bta_gattc_cache_write(const RawAddress& server_bda,
                           const gatt::Database& database) {
  char fname[255] = {0};
  bta_gattc_generate_cache_file_name(fname, sizeof(fname), server_bda);
  bta_gattc_store_db(fname, database.Serialize());
}
}


/*******************************************************************************
/*******************************************************************************
+1 −1
Original line number Original line Diff line number Diff line
@@ -479,7 +479,7 @@ extern bool bta_gattc_conn_dealloc(const RawAddress& remote_bda);
extern bool bta_gattc_cache_load(tBTA_GATTC_SERV* p_srcb);
extern bool bta_gattc_cache_load(tBTA_GATTC_SERV* p_srcb);
extern void bta_gattc_cache_write(
extern void bta_gattc_cache_write(
    const RawAddress& server_bda,
    const RawAddress& server_bda,
    const std::vector<gatt::StoredAttribute>& attr);
    const gatt::Database& database);
extern void bta_gattc_cache_reset(const RawAddress& server_bda);
extern void bta_gattc_cache_reset(const RawAddress& server_bda);


extern bool bta_gattc_read_db_hash(tBTA_GATTC_CLCB* p_clcb);
extern bool bta_gattc_read_db_hash(tBTA_GATTC_CLCB* p_clcb);