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 擦除备份区中的应用程序

@ -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

@ -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
@ -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();
@ -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.

@ -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
@ -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.
* *
@ -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