1、【增加】环境变量缓冲区锁,提高大并发情况下的稳定性。

Signed-off-by: armink <armink.ztl@gmail.com>
pull/1/head
armink 11 years ago
parent f4cf203def
commit bf0926bf4d

@ -222,7 +222,19 @@ FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size)
|buf |源数据的缓冲区| |buf |源数据的缓冲区|
|size |写入数据的大小(字节)| |size |写入数据的大小(字节)|
### 2.4 打印调试日志信息 ### 2.4 对环境变量缓冲区加锁
```C
void flash_env_lock(void)
```
### 2.5 对环境变量缓冲区解锁
```C
void flash_env_unlock(void)
```
### 2.6 打印调试日志信息
在定义 `FLASH_PRINT_DEBUG` 宏后,打印调试日志信息 在定义 `FLASH_PRINT_DEBUG` 宏后,打印调试日志信息
@ -237,7 +249,7 @@ void flash_log_debug(const char *file, const long line, const char *format, ...)
|format |打印格式| |format |打印格式|
|... |不定参| |... |不定参|
### 2.5 打印普通日志信息 ### 2.7 打印普通日志信息
```C ```C
void flash_log_info(const char *format, ...) void flash_log_info(const char *format, ...)
@ -248,7 +260,7 @@ void flash_log_info(const char *format, ...)
|format |打印格式| |format |打印格式|
|... |不定参| |... |不定参|
### 2.6 无格式打印信息 ### 2.8 无格式打印信息
该方法输出无固定格式的打印信息,为 `flash_print_env` 方法所用。而 `flash_log_debug``flash_log_info` 可以输出带指定前缀及格式的打印日志信息。 该方法输出无固定格式的打印信息,为 `flash_print_env` 方法所用。而 `flash_log_debug``flash_log_info` 可以输出带指定前缀及格式的打印日志信息。

@ -56,7 +56,7 @@ if (!(EXPR)) \
while (1); \ while (1); \
} }
/* EasyFlash software version number */ /* EasyFlash software version number */
#define FLASH_SW_VERSION "1.05.25" #define FLASH_SW_VERSION "1.05.30"
typedef struct _flash_env{ typedef struct _flash_env{
char *key; char *key;
@ -103,6 +103,8 @@ FlashErrCode flash_copy_bl_from_bak(uint32_t bl_addr, size_t bl_size);
FlashErrCode flash_read(uint32_t addr, uint32_t *buf, size_t size); FlashErrCode flash_read(uint32_t addr, uint32_t *buf, size_t size);
FlashErrCode flash_erase(uint32_t addr, size_t size); FlashErrCode flash_erase(uint32_t addr, size_t size);
FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size); FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size);
void flash_env_lock(void);
void flash_env_unlock(void);
void flash_log_debug(const char *file, const long line, const char *format, ...); void flash_log_debug(const char *file, const long line, const char *format, ...);
void flash_log_info(const char *format, ...); void flash_log_info(const char *format, ...);
void flash_print(const char *format, ...); void flash_print(const char *format, ...);

@ -128,6 +128,25 @@ FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size) {
return result; return result;
} }
/**
* lock the ENV ram cache
*/
void flash_env_lock(void) {
/* You can add your code under here. */
}
/**
* unlock the ENV ram cache
*/
void flash_env_unlock(void) {
/* You can add your code under here. */
}
/** /**
* This function is print flash debug info. * This function is print flash debug info.
* *

@ -70,6 +70,7 @@ static uint32_t get_env_end_addr(void);
static void set_env_end_addr(uint32_t end_addr); static void set_env_end_addr(uint32_t end_addr);
static FlashErrCode write_env(const char *key, const char *value); static FlashErrCode write_env(const char *key, const char *value);
static uint32_t *find_env(const char *key); static uint32_t *find_env(const char *key);
static FlashErrCode del_env(const char *key);
static size_t get_env_data_size(void); static size_t get_env_data_size(void);
static FlashErrCode create_env(const char *key, const char *value); static FlashErrCode create_env(const char *key, const char *value);
@ -127,6 +128,9 @@ FlashErrCode flash_env_set_default(void){
FLASH_ASSERT(default_env_set); FLASH_ASSERT(default_env_set);
FLASH_ASSERT(default_env_set_size); FLASH_ASSERT(default_env_set_size);
/* lock the ENV cache */
flash_env_lock();
/* set environment end address is at data section start address */ /* set environment end address is at data section start address */
set_env_end_addr(get_env_data_addr()); set_env_end_addr(get_env_data_addr());
@ -135,6 +139,9 @@ FlashErrCode flash_env_set_default(void){
create_env(default_env_set[i].key, default_env_set[i].value); create_env(default_env_set[i].key, default_env_set[i].value);
} }
/* unlock the ENV cache */
flash_env_unlock();
flash_save_env(); flash_save_env();
return result; return result;
@ -196,8 +203,6 @@ static size_t get_env_data_size(void) {
* @return size * @return size
*/ */
size_t flash_get_env_total_size(void) { size_t flash_get_env_total_size(void) {
/* must be initialized */
return FLASH_USER_SETTING_ENV_SIZE; return FLASH_USER_SETTING_ENV_SIZE;
} }
@ -262,7 +267,8 @@ static FlashErrCode write_env(const char *key, const char *value) {
*/ */
static uint32_t *find_env(const char *key) { static uint32_t *find_env(const char *key) {
uint32_t *env_cache_addr = NULL; uint32_t *env_cache_addr = NULL;
char *env_start, *env_end, *env, *env_bak; char *env_start, *env_end, *env;
size_t key_len = strlen(key), env_len;
FLASH_ASSERT(env_start_addr); FLASH_ASSERT(env_start_addr);
@ -282,18 +288,18 @@ static uint32_t *find_env(const char *key) {
env = env_start; env = env_start;
while (env < env_end) { while (env < env_end) {
/* storage model is key=value\0 */ /* the key length must be equal */
env_bak = strstr(env, key); if (!strncmp(env, key, key_len) && (env[key_len] == '=')) {
/* the key name length must be equal */ env_cache_addr = (uint32_t *) env;
if (env_bak && (env_bak[strlen(key)] == '=')) {
env_cache_addr = (uint32_t *) env_bak;
break; break;
} else { } else {
/* calculate ENV length, contain '\0'. */
env_len = strlen(env) + 1;
/* next ENV and word alignment */ /* next ENV and word alignment */
if ((strlen(env) + 1) % 4 == 0) { if (env_len % 4 == 0) {
env += strlen(env) + 1; env += env_len;
} else { } else {
env += ((strlen(env) + 1) / 4 + 1) * 4; env += (env_len / 4 + 1) * 4;
} }
} }
} }
@ -320,7 +326,7 @@ static FlashErrCode create_env(const char *key, const char *value) {
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
if (strstr(key, "=")) { if (strchr(key, '=')) {
FLASH_INFO("Flash ENV name can't contain '='.\n"); FLASH_INFO("Flash ENV name can't contain '='.\n");
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
@ -343,7 +349,7 @@ static FlashErrCode create_env(const char *key, const char *value) {
* *
* @return result * @return result
*/ */
FlashErrCode flash_del_env(const char *key){ static FlashErrCode del_env(const char *key){
FlashErrCode result = FLASH_NO_ERR; FlashErrCode result = FLASH_NO_ERR;
char *del_env_str = NULL; char *del_env_str = NULL;
size_t del_env_length, remain_env_length; size_t del_env_length, remain_env_length;
@ -355,13 +361,14 @@ FlashErrCode flash_del_env(const char *key){
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
if (strstr(key, "=")) { if (strchr(key, '=')) {
FLASH_INFO("Flash ENV name or value can't contain '='.\n"); FLASH_INFO("Flash ENV name or value can't contain '='.\n");
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
/* find ENV */ /* find ENV */
del_env_str = (char *) find_env(key); del_env_str = (char *) find_env(key);
if (!del_env_str) { if (!del_env_str) {
FLASH_INFO("Not find \"%s\" in ENV.\n", key); FLASH_INFO("Not find \"%s\" in ENV.\n", key);
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
@ -396,18 +403,24 @@ FlashErrCode flash_del_env(const char *key){
FlashErrCode flash_set_env(const char *key, const char *value) { FlashErrCode flash_set_env(const char *key, const char *value) {
FlashErrCode result = FLASH_NO_ERR; FlashErrCode result = FLASH_NO_ERR;
/* lock the ENV cache */
flash_env_lock();
/* if ENV value is empty, delete it */ /* if ENV value is empty, delete it */
if (*value == NULL) { if (*value == NULL) {
result = flash_del_env(key); result = del_env(key);
} else { } else {
/* if find this ENV, then delete it and recreate it */ /* if find this ENV, then delete it and recreate it */
if (find_env(key)) { if (find_env(key)) {
result = flash_del_env(key); result = del_env(key);
} }
if (result == FLASH_NO_ERR) { if (result == FLASH_NO_ERR) {
result = create_env(key, value); result = create_env(key, value);
} }
} }
/* unlock the ENV cache */
flash_env_unlock();
return result; return result;
} }
@ -424,11 +437,12 @@ char *flash_get_env(const char *key) {
/* find ENV */ /* find ENV */
env_cache_addr = find_env(key); env_cache_addr = find_env(key);
if (env_cache_addr == NULL) { if (env_cache_addr == NULL) {
return NULL; return NULL;
} }
/* get value address */ /* get value address */
value = strstr((char *) env_cache_addr, "="); value = strchr((char *) env_cache_addr, '=');
if (value != NULL) { if (value != NULL) {
/* the equal sign next character is value */ /* the equal sign next character is value */
value++; value++;
@ -483,7 +497,6 @@ void flash_load_env(void) {
/* read ENV CRC code from flash */ /* read ENV CRC code from flash */
flash_read(get_env_system_addr() + ENV_PARAM_INDEX_DATA_CRC * 4, flash_read(get_env_system_addr() + ENV_PARAM_INDEX_DATA_CRC * 4,
&env_cache[ENV_PARAM_INDEX_DATA_CRC] , 4); &env_cache[ENV_PARAM_INDEX_DATA_CRC] , 4);
/* if ENV CRC32 check is fault, set default for it */ /* if ENV CRC32 check is fault, set default for it */
if (!env_crc_is_ok()) { if (!env_crc_is_ok()) {
FLASH_INFO("Warning: ENV CRC check failed. Set it to default.\n"); FLASH_INFO("Warning: ENV CRC check failed. Set it to default.\n");

@ -88,6 +88,7 @@ static uint32_t *find_env(const char *key);
static size_t get_env_detail_size(void); static size_t get_env_detail_size(void);
static size_t get_env_user_used_size(void); static size_t get_env_user_used_size(void);
static FlashErrCode create_env(const char *key, const char *value); static FlashErrCode create_env(const char *key, const char *value);
static FlashErrCode del_env(const char *key);
static FlashErrCode save_cur_using_data_addr(uint32_t cur_data_addr); static FlashErrCode save_cur_using_data_addr(uint32_t cur_data_addr);
#ifdef FLASH_ENV_USING_CRC_CHECK #ifdef FLASH_ENV_USING_CRC_CHECK
@ -144,6 +145,9 @@ FlashErrCode flash_env_set_default(void){
FLASH_ASSERT(default_env_set); FLASH_ASSERT(default_env_set);
FLASH_ASSERT(default_env_set_size); FLASH_ASSERT(default_env_set_size);
/* lock the ENV cache */
flash_env_lock();
/* set ENV detail part end address is at ENV detail part start address */ /* set ENV detail part end address is at ENV detail part start address */
set_env_detail_end_addr(get_env_detail_addr()); set_env_detail_end_addr(get_env_detail_addr());
@ -152,6 +156,9 @@ FlashErrCode flash_env_set_default(void){
create_env(default_env_set[i].key, default_env_set[i].value); create_env(default_env_set[i].key, default_env_set[i].value);
} }
/* unlock the ENV cache */
flash_env_unlock();
flash_save_env(); flash_save_env();
return result; return result;
@ -313,7 +320,8 @@ static FlashErrCode write_env(const char *key, const char *value) {
*/ */
static uint32_t *find_env(const char *key) { static uint32_t *find_env(const char *key) {
uint32_t *env_cache_addr = NULL; uint32_t *env_cache_addr = NULL;
char *env_start, *env_end, *env, *env_bak; char *env_start, *env_end, *env;
size_t key_len = strlen(key), env_len;
FLASH_ASSERT(cur_using_data_addr); FLASH_ASSERT(cur_using_data_addr);
@ -333,21 +341,22 @@ static uint32_t *find_env(const char *key) {
env = env_start; env = env_start;
while (env < env_end) { while (env < env_end) {
/* storage model is key=value\0 */ /* the key length must be equal */
env_bak = strstr(env, key); if (!strncmp(env, key, key_len) && (env[key_len] == '=')) {
/* the key name length must be equal */ env_cache_addr = (uint32_t *) env;
if (env_bak && (env_bak[strlen(key)] == '=')) {
env_cache_addr = (uint32_t *) env_bak;
break; break;
} else { } else {
/* calculate ENV length, contain '\0'. */
env_len = strlen(env) + 1;
/* next ENV and word alignment */ /* next ENV and word alignment */
if ((strlen(env) + 1) % 4 == 0) { if (env_len % 4 == 0) {
env += strlen(env) + 1; env += env_len;
} else { } else {
env += ((strlen(env) + 1) / 4 + 1) * 4; env += (env_len / 4 + 1) * 4;
} }
} }
} }
return env_cache_addr; return env_cache_addr;
} }
@ -371,7 +380,7 @@ static FlashErrCode create_env(const char *key, const char *value) {
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
if (strstr(key, "=")) { if (strchr(key, '=')) {
FLASH_INFO("Flash ENV name can't contain '='.\n"); FLASH_INFO("Flash ENV name can't contain '='.\n");
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
@ -394,7 +403,7 @@ static FlashErrCode create_env(const char *key, const char *value) {
* *
* @return result * @return result
*/ */
FlashErrCode flash_del_env(const char *key){ static FlashErrCode del_env(const char *key) {
FlashErrCode result = FLASH_NO_ERR; FlashErrCode result = FLASH_NO_ERR;
char *del_env_str = NULL; char *del_env_str = NULL;
size_t del_env_length, remain_env_length; size_t del_env_length, remain_env_length;
@ -406,13 +415,14 @@ FlashErrCode flash_del_env(const char *key){
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
if (strstr(key, "=")) { if (strchr(key, '=')) {
FLASH_INFO("Flash ENV name or value can't contain '='.\n"); FLASH_INFO("Flash ENV name or value can't contain '='.\n");
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
} }
/* find ENV */ /* find ENV */
del_env_str = (char *) find_env(key); del_env_str = (char *) find_env(key);
if (!del_env_str) { if (!del_env_str) {
FLASH_INFO("Not find \"%s\" in ENV.\n", key); FLASH_INFO("Not find \"%s\" in ENV.\n", key);
return FLASH_ENV_NAME_ERR; return FLASH_ENV_NAME_ERR;
@ -447,18 +457,24 @@ FlashErrCode flash_del_env(const char *key){
FlashErrCode flash_set_env(const char *key, const char *value) { FlashErrCode flash_set_env(const char *key, const char *value) {
FlashErrCode result = FLASH_NO_ERR; FlashErrCode result = FLASH_NO_ERR;
/* lock the ENV cache */
flash_env_lock();
/* if ENV value is empty, delete it */ /* if ENV value is empty, delete it */
if (*value == NULL) { if (*value == NULL) {
result = flash_del_env(key); result = del_env(key);
} else { } else {
/* if find this ENV, then delete it and recreate it */ /* if find this ENV, then delete it and recreate it */
if (find_env(key)) { if (find_env(key)) {
result = flash_del_env(key); result = del_env(key);
} }
if (result == FLASH_NO_ERR) { if (result == FLASH_NO_ERR) {
result = create_env(key, value); result = create_env(key, value);
} }
} }
/* unlock the ENV cache */
flash_env_unlock();
return result; return result;
} }
@ -475,11 +491,12 @@ char *flash_get_env(const char *key) {
/* find ENV */ /* find ENV */
env_cache_addr = find_env(key); env_cache_addr = find_env(key);
if (env_cache_addr == NULL) { if (env_cache_addr == NULL) {
return NULL; return NULL;
} }
/* get value address */ /* get value address */
value = strstr((char *) env_cache_addr, "="); value = strchr((char *) env_cache_addr, '=');
if (value != NULL) { if (value != NULL) {
/* the equal sign next character is value */ /* the equal sign next character is value */
value++; value++;
@ -549,15 +566,15 @@ void flash_load_env(void) {
/* read ENV CRC code from flash */ /* read ENV CRC code from flash */
flash_read(get_cur_using_data_addr() + ENV_PARAM_PART_INDEX_DATA_CRC * 4, flash_read(get_cur_using_data_addr() + ENV_PARAM_PART_INDEX_DATA_CRC * 4,
&env_cache[ENV_PARAM_PART_INDEX_DATA_CRC], 4); &env_cache[ENV_PARAM_PART_INDEX_DATA_CRC], 4);
/* if ENV CRC32 check is fault, set default for it */ /* if ENV CRC32 check is fault, set default for it */
if (!env_crc_is_ok()) { if (!env_crc_is_ok()) {
FLASH_INFO("Warning: ENV CRC check failed. Set it to default.\n"); FLASH_INFO("Warning: ENV CRC check failed. Set it to default.\n");
flash_env_set_default(); flash_env_set_default();
} }
}
#endif #endif
}
} }
} }

Loading…
Cancel
Save