config_t *config_new(const char *filename) { assert(filename != NULL); config_t *config = config_new_empty(); if (!config) { return NULL; } esp_err_t err; nvs_handle fp; err = nvs_open(filename, NVS_READWRITE, &fp); if (err != ESP_OK) { if (err == ESP_ERR_NVS_NOT_INITIALIZED) { LOG_ERROR("%s: NVS not initialized. " "Call nvs_flash_init before initializing bluetooth.", __func__); } else { LOG_ERROR("%s unable to open NVS namespace '%s'\n", __func__, filename); } config_free(config); return NULL; } config_parse(fp, config); nvs_close(fp); return config; }
bool btc_config_init(void) { osi_mutex_new(&lock); config = config_new(CONFIG_FILE_PATH); if (!config) { LOG_WARN("%s unable to load config file; starting unconfigured.\n", __func__); config = config_new_empty(); if (!config) { LOG_ERROR("%s unable to allocate a config object.\n", __func__); goto error; } } if (config_save(config, CONFIG_FILE_PATH)) { // unlink(LEGACY_CONFIG_FILE_PATH); } return true; error:; config_free(config); osi_mutex_free(&lock); config = NULL; LOG_ERROR("%s failed\n", __func__); return false; }
int btc_config_clear(void) { assert(config != NULL); osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); config_free(config); config = config_new_empty(); if (config == NULL) { osi_mutex_unlock(&lock); return false; } int ret = config_save(config, CONFIG_FILE_PATH); osi_mutex_unlock(&lock); return ret; }
static future_t *init(void) { pthread_mutex_init(&lock, NULL); config = config_new(CONFIG_FILE_PATH); if (!config) { LOG_WARN(LOG_TAG, "%s unable to load config file; attempting to transcode legacy file.", __func__); config = btif_config_transcode(LEGACY_CONFIG_FILE_PATH); if (!config) { LOG_WARN(LOG_TAG, "%s unable to transcode legacy file, starting unconfigured.", __func__); config = config_new_empty(); if (!config) { LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__); goto error; } } if (config_save(config, CONFIG_FILE_PATH)) unlink(LEGACY_CONFIG_FILE_PATH); } // TODO(sharvil): use a non-wake alarm for this once we have // API support for it. There's no need to wake the system to // write back to disk. alarm_timer = alarm_new(); if (!alarm_timer) { LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__); goto error; } return future_new_immediate(FUTURE_SUCCESS); error:; alarm_free(alarm_timer); config_free(config); pthread_mutex_destroy(&lock); alarm_timer = NULL; config = NULL; return future_new_immediate(FUTURE_FAIL); }
TEST_F(ConfigTest, config_new_empty) { config_t *config = config_new_empty(); EXPECT_TRUE(config != NULL); config_free(config); }