Merge pull request #24 from outely/master

【增加】添加删除环境变量接口
【更新】修改ef_set_env()可存储空字符串
pull/27/head
朱天龙 (Armink) 8 years ago committed by GitHub
commit deffbdb897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -55,10 +55,9 @@ char *ef_get_env(const char *key)
使用此方法可以实现对环境变量的增加、修改及删除功能。(注意:此处的环境变量指代的已加载到内存中的环境变量) 使用此方法可以实现对环境变量的增加、修改及删除功能。(注意:此处的环境变量指代的已加载到内存中的环境变量)
- **增加** :当环境变量表中不存在该名称的环境变量时,则会执行新增操作; - **增加** :当环境变量表中不存在该名称的环境变量时,则会执行新增操作;
- **修改** :入参中的环境变量名称在当前环境变量表中存在,则把该环境变量值修改为入参中的值; - **修改** :入参中的环境变量名称在当前环境变量表中存在,则把该环境变量值修改为入参中的值;
- **删除**当入参中的value为NULL时则会删除入参名对应的环境变量。
- **删除** 当入参中的value为空时则会删除入参名对应的环境变量。
```C ```C
EfErrCode ef_set_env(const char *key, const char *value) EfErrCode ef_set_env(const char *key, const char *value)
@ -69,7 +68,19 @@ EfErrCode ef_set_env(const char *key, const char *value)
|key |环境变量名称| |key |环境变量名称|
|value |环境变量值| |value |环境变量值|
#### 1.2.5 保存环境变量 #### 1.2.5 删除环境变量
使用此方法可以实现对环境变量的删除功能。(注意:此处的环境变量指代的已加载到内存中的环境变量)
```c
EfErrCode ef_del_env(const char *key)
```
| 参数 | 描述 |
| :--- | :----------- |
| key | 环境变量名称 |
#### 1.2.6 保存环境变量
保存内存中的环境变量表到Flash中。 保存内存中的环境变量表到Flash中。
@ -77,20 +88,20 @@ EfErrCode ef_set_env(const char *key, const char *value)
EfErrCode ef_save_env(void) EfErrCode ef_save_env(void)
``` ```
#### 1.2.6 重置环境变量 #### 1.2.7 重置环境变量
将内存中的环境变量表重置为默认值。 将内存中的环境变量表重置为默认值。
```C ```C
EfErrCode ef_env_set_default(void) EfErrCode ef_env_set_default(void)
``` ```
#### 1.2.7 获取当前环境变量写入到Flash的字节大小 #### 1.2.8 获取当前环境变量写入到Flash的字节大小
```C ```C
size_t ef_get_env_write_bytes(void) size_t ef_get_env_write_bytes(void)
``` ```
#### 1.2.8 设置并保存环境变量 #### 1.2.9 设置并保存环境变量
设置环境变量成功后立刻保存。设置功能参考`ef_set_env`方法。 设置环境变量成功后立刻保存。设置功能参考`ef_set_env`方法。
@ -103,6 +114,18 @@ EfErrCode ef_set_and_save_env(const char *key, const char *value)
|key |环境变量名称| |key |环境变量名称|
|value |环境变量值| |value |环境变量值|
#### 1.2.10 删除并保存环境变量
删除环境变量成功后立刻保存。删除功能参考`ef_del_env`方法。
```c
EfErrCode ef_del_and_save_env(const char *key)
```
| 参数 | 描述 |
| :--- | :----------- |
| key | 环境变量名称 |
### 1.3 在线升级 ### 1.3 在线升级
#### 1.3.1 擦除备份区中的应用程序 #### 1.3.1 擦除备份区中的应用程序

@ -40,19 +40,19 @@ extern "C" {
#endif #endif
#if defined(EF_USING_ENV) && (!defined(ENV_USER_SETTING_SIZE) || !defined(ENV_AREA_SIZE)) #if defined(EF_USING_ENV) && (!defined(ENV_USER_SETTING_SIZE) || !defined(ENV_AREA_SIZE))
#error "Please configure user setting ENV size or ENV area size (in ef_cfg.h)" #error "Please configure user setting ENV size or ENV area size (in ef_cfg.h)"
#endif #endif
#if defined(EF_USING_LOG) && !defined(LOG_AREA_SIZE) #if defined(EF_USING_LOG) && !defined(LOG_AREA_SIZE)
#error "Please configure log area size (in ef_cfg.h)" #error "Please configure log area size (in ef_cfg.h)"
#endif #endif
#if !defined(EF_START_ADDR) #if !defined(EF_START_ADDR)
#error "Please configure backup area start address (in ef_cfg.h)" #error "Please configure backup area start address (in ef_cfg.h)"
#endif #endif
#if !defined(EF_ERASE_MIN_SIZE) #if !defined(EF_ERASE_MIN_SIZE)
#error "Please configure minimum size of flash erasure (in ef_cfg.h)" #error "Please configure minimum size of flash erasure (in ef_cfg.h)"
#endif #endif
/* EasyFlash debug print function. Must be implement by user. */ /* EasyFlash debug print function. Must be implement by user. */
@ -78,10 +78,10 @@ if (!(EXPR)) \
/* EasyFlash software version number */ /* EasyFlash software version number */
#define EF_SW_VERSION "3.1.0" #define EF_SW_VERSION "3.1.0"
typedef struct _ef_env{ typedef struct _ef_env {
char *key; char *key;
char *value; char *value;
}ef_env, *ef_env_t; } ef_env, *ef_env_t;
/* EasyFlash error code */ /* EasyFlash error code */
typedef enum { typedef enum {
@ -100,7 +100,7 @@ typedef enum {
EF_SECTOR_EMPTY, EF_SECTOR_EMPTY,
EF_SECTOR_USING, EF_SECTOR_USING,
EF_SECTOR_FULL, EF_SECTOR_FULL,
}EfSecrorStatus; } EfSecrorStatus;
/* easyflash.c */ /* easyflash.c */
EfErrCode easyflash_init(void); EfErrCode easyflash_init(void);
@ -111,10 +111,12 @@ EfErrCode ef_load_env(void);
void ef_print_env(void); void ef_print_env(void);
char *ef_get_env(const char *key); char *ef_get_env(const char *key);
EfErrCode ef_set_env(const char *key, const char *value); EfErrCode ef_set_env(const char *key, const char *value);
EfErrCode ef_del_env(const char *key);
EfErrCode ef_save_env(void); EfErrCode ef_save_env(void);
EfErrCode ef_env_set_default(void); EfErrCode ef_env_set_default(void);
size_t ef_get_env_write_bytes(void); size_t ef_get_env_write_bytes(void);
EfErrCode ef_set_and_save_env(const char *key, const char *value); EfErrCode ef_set_and_save_env(const char *key, const char *value);
EfErrCode ef_del_and_save_env(const char *key);
#endif #endif
#ifdef EF_USING_IAP #ifdef EF_USING_IAP

@ -130,16 +130,16 @@ EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size) {
if (ENV_USER_SETTING_SIZE % EF_ERASE_MIN_SIZE == 0) { if (ENV_USER_SETTING_SIZE % EF_ERASE_MIN_SIZE == 0) {
EF_ASSERT(ENV_USER_SETTING_SIZE == ENV_AREA_SIZE); EF_ASSERT(ENV_USER_SETTING_SIZE == ENV_AREA_SIZE);
} else { } else {
EF_ASSERT((ENV_USER_SETTING_SIZE/EF_ERASE_MIN_SIZE + 1)*EF_ERASE_MIN_SIZE == ENV_AREA_SIZE); EF_ASSERT((ENV_USER_SETTING_SIZE / EF_ERASE_MIN_SIZE + 1)*EF_ERASE_MIN_SIZE == ENV_AREA_SIZE);
} }
#else #else
/* total_size must be aligned with erase_min_size */ /* total_size must be aligned with erase_min_size */
if (ENV_USER_SETTING_SIZE % EF_ERASE_MIN_SIZE == 0) { if (ENV_USER_SETTING_SIZE % EF_ERASE_MIN_SIZE == 0) {
/* it has double area when used power fail safeguard mode */ /* it has double area when used power fail safeguard mode */
EF_ASSERT(2*ENV_USER_SETTING_SIZE == ENV_AREA_SIZE); EF_ASSERT(2 * ENV_USER_SETTING_SIZE == ENV_AREA_SIZE);
} else { } else {
/* it has double area when used power fail safeguard mode */ /* it has double area when used power fail safeguard mode */
EF_ASSERT(2*(ENV_USER_SETTING_SIZE/EF_ERASE_MIN_SIZE + 1)*EF_ERASE_MIN_SIZE == ENV_AREA_SIZE); EF_ASSERT(2 * (ENV_USER_SETTING_SIZE / EF_ERASE_MIN_SIZE + 1)*EF_ERASE_MIN_SIZE == ENV_AREA_SIZE);
} }
#endif #endif
@ -170,7 +170,7 @@ EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size) {
* *
* @return result * @return result
*/ */
EfErrCode ef_env_set_default(void){ EfErrCode ef_env_set_default(void) {
extern EfErrCode ef_env_ver_num_set_default(void); extern EfErrCode ef_env_ver_num_set_default(void);
EfErrCode result = EF_NO_ERR; EfErrCode result = EF_NO_ERR;
@ -433,7 +433,7 @@ static EfErrCode create_env(const char *key, const char *value) {
* *
* @return result * @return result
*/ */
static EfErrCode del_env(const char *key){ static EfErrCode del_env(const char *key) {
EfErrCode result = EF_NO_ERR; EfErrCode result = EF_NO_ERR;
char *del_env = NULL; char *del_env = NULL;
size_t del_env_length, remain_env_length; size_t del_env_length, remain_env_length;
@ -478,7 +478,7 @@ static EfErrCode del_env(const char *key){
} }
/** /**
* Set an ENV. If it value is empty, delete it. * Set an ENV.If it value is NULL, delete it.
* If not find it in ENV table, then create it. * If not find it in ENV table, then create it.
* *
* @param key ENV name * @param key ENV name
@ -490,7 +490,7 @@ EfErrCode ef_set_env(const char *key, const char *value) {
EfErrCode result = EF_NO_ERR; EfErrCode result = EF_NO_ERR;
char *old_env, *old_value; char *old_env, *old_value;
if(!init_ok) { if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n"); EF_INFO("ENV isn't initialize OK.\n");
return EF_ENV_INIT_FAILED; return EF_ENV_INIT_FAILED;
} }
@ -498,8 +498,8 @@ EfErrCode ef_set_env(const char *key, const char *value) {
/* lock the ENV cache */ /* lock the ENV cache */
ef_port_env_lock(); ef_port_env_lock();
/* if ENV value is empty, delete it */ /* if ENV value is NULL, delete it */
if ((value == NULL) || *value == '\0') { if (value == NULL) {
result = del_env(key); result = del_env(key);
} else { } else {
old_env = find_env(key); old_env = find_env(key);
@ -519,6 +519,33 @@ EfErrCode ef_set_env(const char *key, const char *value) {
result = create_env(key, value); result = create_env(key, value);
} }
} }
/* unlock the ENV cache */
ef_port_env_unlock();
return result;
}
/**
* Del an ENV.
*
* @param key ENV name
*
* @return result
*/
EfErrCode ef_del_env(const char *key) {
EfErrCode result = EF_NO_ERR;
if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n");
return EF_ENV_INIT_FAILED;
}
/* lock the ENV cache */
ef_port_env_lock();
result = del_env(key);
/* unlock the ENV cache */ /* unlock the ENV cache */
ef_port_env_unlock(); ef_port_env_unlock();
@ -535,7 +562,7 @@ EfErrCode ef_set_env(const char *key, const char *value) {
char *ef_get_env(const char *key) { char *ef_get_env(const char *key) {
char *env = NULL, *value = NULL; char *env = NULL, *value = NULL;
if(!init_ok) { if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n"); EF_INFO("ENV isn't initialize OK.\n");
return NULL; return NULL;
} }
@ -564,7 +591,7 @@ void ef_print_env(void) {
uint8_t j; uint8_t j;
char c; char c;
if(!init_ok) { if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n"); EF_INFO("ENV isn't initialize OK.\n");
return; return;
} }
@ -679,7 +706,7 @@ EfErrCode ef_load_env(void) {
ef_port_read(area1_start_address + ENV_PARAM_INDEX_SAVED_COUNT * 4, ef_port_read(area1_start_address + ENV_PARAM_INDEX_SAVED_COUNT * 4,
&area1_saved_count, 4); &area1_saved_count, 4);
/* the bigger saved count area is valid */ /* the bigger saved count area is valid */
if ((area0_saved_count > area1_saved_count)||((area0_saved_count == 0)&&(area1_saved_count == 0xFFFFFFFF))) { if ((area0_saved_count > area1_saved_count) || ((area0_saved_count == 0) && (area1_saved_count == 0xFFFFFFFF))) {
area1_is_valid = false; area1_is_valid = false;
} else { } else {
area0_is_valid = false; area0_is_valid = false;
@ -828,6 +855,25 @@ EfErrCode ef_set_and_save_env(const char *key, const char *value) {
return result; return result;
} }
/**
* Del and save an ENV. If del ENV is success then will save it.
*
* @param key ENV name
*
* @return result
*/
EfErrCode ef_del_and_save_env(const char *key) {
EfErrCode result = EF_NO_ERR;
result = ef_del_env(key);
if (result == EF_NO_ERR) {
result = ef_save_env();
}
return result;
}
#ifdef EF_ENV_AUTO_UPDATE #ifdef EF_ENV_AUTO_UPDATE
/** /**
* Auto update ENV to latest default when current EF_ENV_VER is changed. * Auto update ENV to latest default when current EF_ENV_VER is changed.

@ -173,7 +173,7 @@ EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size) {
* *
* @return result * @return result
*/ */
EfErrCode ef_env_set_default(void){ EfErrCode ef_env_set_default(void) {
EfErrCode result = EF_NO_ERR; EfErrCode result = EF_NO_ERR;
size_t i; size_t i;
@ -293,7 +293,7 @@ static size_t get_env_detail_size(void) {
* *
* @return size * @return size
*/ */
/* must be initialized */ /* must be initialized */
static size_t get_env_user_used_size(void) { static size_t get_env_user_used_size(void) {
if (get_env_detail_end_addr() > get_cur_using_data_addr()) { if (get_env_detail_end_addr() > get_cur_using_data_addr()) {
return get_env_detail_end_addr() - get_cur_using_data_addr(); return get_env_detail_end_addr() - get_cur_using_data_addr();
@ -494,7 +494,7 @@ static EfErrCode del_env(const char *key) {
} }
/** /**
* Set an ENV. If it value is empty, delete it. * Set an ENV.If it value is NULL, delete it.
* If not find it in ENV table, then create it. * If not find it in ENV table, then create it.
* *
* @param key ENV name * @param key ENV name
@ -506,7 +506,7 @@ EfErrCode ef_set_env(const char *key, const char *value) {
EfErrCode result = EF_NO_ERR; EfErrCode result = EF_NO_ERR;
char *old_env, *old_value; char *old_env, *old_value;
if(!init_ok) { if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n"); EF_INFO("ENV isn't initialize OK.\n");
return EF_ENV_INIT_FAILED; return EF_ENV_INIT_FAILED;
} }
@ -514,8 +514,8 @@ EfErrCode ef_set_env(const char *key, const char *value) {
/* lock the ENV cache */ /* lock the ENV cache */
ef_port_env_lock(); ef_port_env_lock();
/* if ENV value is empty, delete it */ /* if ENV value is NULL, delete it */
if ((value == NULL) || (*value == '\0')) { if (value == NULL) {
result = del_env(key); result = del_env(key);
} else { } else {
old_env = find_env(key); old_env = find_env(key);
@ -541,6 +541,32 @@ EfErrCode ef_set_env(const char *key, const char *value) {
return result; return result;
} }
/**
* Del an ENV.
*
* @param key ENV name
*
* @return result
*/
EfErrCode ef_del_env(const char *key) {
EfErrCode result = EF_NO_ERR;
if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n");
return EF_ENV_INIT_FAILED;
}
/* lock the ENV cache */
ef_port_env_lock();
result = del_env(key);
/* unlock the ENV cache */
ef_port_env_unlock();
return result;
}
/** /**
* Get an ENV value by key name. * Get an ENV value by key name.
* *
@ -551,7 +577,7 @@ EfErrCode ef_set_env(const char *key, const char *value) {
char *ef_get_env(const char *key) { char *ef_get_env(const char *key) {
char *env = NULL, *value = NULL; char *env = NULL, *value = NULL;
if(!init_ok) { if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n"); EF_INFO("ENV isn't initialize OK.\n");
return NULL; return NULL;
} }
@ -579,7 +605,7 @@ void ef_print_env(void) {
uint8_t j; uint8_t j;
char c; char c;
if(!init_ok) { if (!init_ok) {
EF_INFO("ENV isn't initialize OK.\n"); EF_INFO("ENV isn't initialize OK.\n");
return; return;
} }
@ -743,7 +769,7 @@ EfErrCode ef_load_env(void) {
ef_port_read(area1_cur_using_addr + ENV_PARAM_PART_INDEX_SAVED_COUNT * 4, ef_port_read(area1_cur_using_addr + ENV_PARAM_PART_INDEX_SAVED_COUNT * 4,
&area1_saved_count, 4); &area1_saved_count, 4);
/* the bigger saved count area is valid */ /* the bigger saved count area is valid */
if ((area0_saved_count > area1_saved_count)||((area0_saved_count == 0)&&(area1_saved_count == 0xFFFFFFFF))) { if ((area0_saved_count > area1_saved_count) || ((area0_saved_count == 0) && (area1_saved_count == 0xFFFFFFFF))) {
area1_is_valid = false; area1_is_valid = false;
} else { } else {
area0_is_valid = false; area0_is_valid = false;
@ -801,7 +827,7 @@ EfErrCode ef_save_env(void) {
/* change the ENV detail end address to next save area address */ /* change the ENV detail end address to next save area address */
set_env_detail_end_addr(get_cur_using_data_addr() + env_used_size); set_env_detail_end_addr(get_cur_using_data_addr() + env_used_size);
/* area0 or area1 */ /* area0 or area1 */
if(get_cur_using_data_addr() < get_env_start_addr() + ENV_AREA_SIZE / 2) { if (get_cur_using_data_addr() < get_env_start_addr() + ENV_AREA_SIZE / 2) {
data_sec_end_addr = get_env_start_addr() + ENV_AREA_SIZE / 2 - 4; data_sec_end_addr = get_env_start_addr() + ENV_AREA_SIZE / 2 - 4;
} else { } else {
data_sec_end_addr = get_env_start_addr() + ENV_AREA_SIZE - 4; data_sec_end_addr = get_env_start_addr() + ENV_AREA_SIZE - 4;
@ -943,7 +969,7 @@ static EfErrCode save_cur_using_data_addr(uint32_t cur_data_addr) {
EfErrCode result = EF_NO_ERR; EfErrCode result = EF_NO_ERR;
uint32_t cur_system_sec_addr; uint32_t cur_system_sec_addr;
if (cur_data_addr < get_env_start_addr() + ENV_AREA_SIZE / 2){ if (cur_data_addr < get_env_start_addr() + ENV_AREA_SIZE / 2) {
/* current using system section is in ENV area0 */ /* current using system section is in ENV area0 */
cur_system_sec_addr = get_env_start_addr(); cur_system_sec_addr = get_env_start_addr();
} else { } else {
@ -989,6 +1015,25 @@ EfErrCode ef_set_and_save_env(const char *key, const char *value) {
return result; return result;
} }
/**
* Del and save an ENV. If del ENV is success then will save it.
*
* @param key ENV name
*
* @return result
*/
EfErrCode ef_del_and_save_env(const char *key) {
EfErrCode result = EF_NO_ERR;
result = ef_del_env(key);
if (result == EF_NO_ERR) {
result = ef_save_env();
}
return result;
}
#ifdef EF_ENV_AUTO_UPDATE #ifdef EF_ENV_AUTO_UPDATE
/** /**
* Auto update ENV to latest default when current EF_ENV_VER is changed. * Auto update ENV to latest default when current EF_ENV_VER is changed.

Loading…
Cancel
Save