From 37c9b3b9f6375af79027a656605a280040ed7652 Mon Sep 17 00:00:00 2001 From: armink Date: Wed, 29 Jul 2015 16:42:44 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E3=80=82=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=9C=AA?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=B6=EF=BC=8C=E8=B0=83=E7=94=A8=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=8E=A5=E5=8F=A3=E5=B0=86=E4=B8=8D=E4=BC=9A=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E4=BF=9D=E5=AD=98=E6=93=8D=E4=BD=9C=E3=80=82=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E5=9C=A8=E9=87=8D=E5=A4=8D=E4=BF=AE=E6=94=B9=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E5=9C=BA=E6=99=AF=E4=B8=8B=E7=9A=84?= =?UTF-8?q?Flash=E5=AF=BF=E5=91=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easyflash/inc/easyflash.h | 2 +- easyflash/src/ef_env.c | 69 +++++++++++++++++++++++++-------------- easyflash/src/ef_env_wl.c | 69 +++++++++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 49 deletions(-) diff --git a/easyflash/inc/easyflash.h b/easyflash/inc/easyflash.h index a7fd4aa..47913fc 100644 --- a/easyflash/inc/easyflash.h +++ b/easyflash/inc/easyflash.h @@ -67,7 +67,7 @@ if (!(EXPR)) \ while (1); \ } /* EasyFlash software version number */ -#define EF_SW_VERSION "1.07.14" +#define EF_SW_VERSION "1.07.29" typedef struct _eflash_env{ char *key; diff --git a/easyflash/src/ef_env.c b/easyflash/src/ef_env.c index f544b0c..e6708ee 100644 --- a/easyflash/src/ef_env.c +++ b/easyflash/src/ef_env.c @@ -68,10 +68,12 @@ enum { static ef_env const *default_env_set = NULL; /* default ENV set size, must be initialized by user */ static size_t default_env_set_size = NULL; -/* ENV RAM cache */ +/* ENV ram cache */ static uint32_t env_cache[ENV_USER_SETTING_SIZE / 4] = { 0 }; /* ENV start address in flash */ static uint32_t env_start_addr = NULL; +/* ENV ram cache has changed when ENV created, deleted and changed value. */ +static bool env_cache_changed = false; #ifdef EF_ENV_USING_PFS_MODE /* current load ENV area address */ @@ -85,7 +87,7 @@ static uint32_t get_env_data_addr(void); static uint32_t get_env_end_addr(void); static void set_env_end_addr(uint32_t end_addr); static EfErrCode write_env(const char *key, const char *value); -static uint32_t *find_env(const char *key); +static char *find_env(const char *key); static EfErrCode del_env(const char *key); static size_t get_env_data_size(void); static EfErrCode create_env(const char *key, const char *value); @@ -288,6 +290,8 @@ static EfErrCode write_env(const char *key, const char *value) { /* fill '\0' for word alignment */ memset(env_cache_bak, 0, env_str_len - (ker_len + value_len + 2)); set_env_end_addr(get_env_end_addr() + env_str_len); + /* ENV ram cache has changed */ + env_cache_changed = true; return result; } @@ -297,11 +301,10 @@ static EfErrCode write_env(const char *key, const char *value) { * * @param key ENV name * - * @return index of ENV in ram cache + * @return found ENV in ram cache */ -static uint32_t *find_env(const char *key) { - uint32_t *env_cache_addr = NULL; - char *env_start, *env_end, *env; +static char *find_env(const char *key) { + char *env_start, *env_end, *env, *found_env = NULL; size_t key_len = strlen(key), env_len; EF_ASSERT(env_start_addr); @@ -324,7 +327,7 @@ static uint32_t *find_env(const char *key) { while (env < env_end) { /* the key length must be equal */ if (!strncmp(env, key, key_len) && (env[key_len] == '=')) { - env_cache_addr = (uint32_t *) env; + found_env = env; break; } else { /* calculate ENV length, contain '\0'. */ @@ -337,7 +340,7 @@ static uint32_t *find_env(const char *key) { } } } - return env_cache_addr; + return found_env; } /** @@ -385,7 +388,7 @@ static EfErrCode create_env(const char *key, const char *value) { */ static EfErrCode del_env(const char *key){ EfErrCode result = EF_NO_ERR; - char *del_env_str = NULL; + char *del_env = NULL; size_t del_env_length, remain_env_length; EF_ASSERT(key); @@ -401,13 +404,13 @@ static EfErrCode del_env(const char *key){ } /* find ENV */ - del_env_str = (char *) find_env(key); + del_env = find_env(key); - if (!del_env_str) { + if (!del_env) { EF_INFO("Not find \"%s\" in ENV.\n", key); return EF_ENV_NAME_ERR; } - del_env_length = strlen(del_env_str); + del_env_length = strlen(del_env); /* '\0' also must be as ENV length */ del_env_length ++; /* the address must multiple of 4 */ @@ -416,11 +419,13 @@ static EfErrCode del_env(const char *key){ } /* calculate remain ENV length */ remain_env_length = get_env_data_size() - - (((uint32_t) del_env_str + del_env_length) - ((uint32_t) env_cache + ENV_PARAM_BYTE_SIZE)); + - (((uint32_t) del_env + del_env_length) - ((uint32_t) env_cache + ENV_PARAM_BYTE_SIZE)); /* remain ENV move forward */ - memcpy(del_env_str, del_env_str + del_env_length, remain_env_length); + memcpy(del_env, del_env + del_env_length, remain_env_length); /* reset ENV end address */ set_env_end_addr(get_env_end_addr() - del_env_length); + /* ENV ram cache has changed */ + env_cache_changed = true; return result; } @@ -436,6 +441,7 @@ static EfErrCode del_env(const char *key){ */ EfErrCode ef_set_env(const char *key, const char *value) { EfErrCode result = EF_NO_ERR; + char *old_env, *old_value; /* lock the ENV cache */ ef_port_env_lock(); @@ -444,11 +450,20 @@ EfErrCode ef_set_env(const char *key, const char *value) { if (*value == NULL) { result = del_env(key); } else { - /* if find this ENV, then delete it and recreate it */ - if (find_env(key)) { - result = del_env(key); - } - if (result == EF_NO_ERR) { + old_env = find_env(key); + /* If find this ENV, then compare the new value and old value. */ + if (old_env) { + /* find the old value address */ + old_env = strchr(old_env, '='); + old_value = old_env + 1; + /* If it is changed then delete it and recreate it */ + if (strcmp(old_value, value)) { + result = del_env(key); + if (result == EF_NO_ERR) { + result = create_env(key, value); + } + } + } else { result = create_env(key, value); } } @@ -466,17 +481,16 @@ EfErrCode ef_set_env(const char *key, const char *value) { * @return value */ char *ef_get_env(const char *key) { - uint32_t *env_cache_addr = NULL; - char *value = NULL; + char *env = NULL, *value = NULL; /* find ENV */ - env_cache_addr = find_env(key); + env = find_env(key); - if (env_cache_addr == NULL) { + if (env == NULL) { return NULL; } /* get value address */ - value = strchr((char *) env_cache_addr, '='); + value = strchr(env, '='); if (value != NULL) { /* the equal sign next character is value */ value++; @@ -626,6 +640,11 @@ EfErrCode ef_save_env(void) { EfErrCode result = EF_NO_ERR; uint32_t write_addr, write_size; + /* ENV ram cache has not changed don't need to save */ + if (!env_cache_changed) { + return result; + } + #ifndef EF_ENV_USING_PFS_MODE write_addr = get_env_system_addr(); write_size = get_env_end_addr() - get_env_system_addr(); @@ -672,6 +691,8 @@ EfErrCode ef_save_env(void) { } } + env_cache_changed = false; + return result; } diff --git a/easyflash/src/ef_env_wl.c b/easyflash/src/ef_env_wl.c index 874c07a..a57c846 100644 --- a/easyflash/src/ef_env_wl.c +++ b/easyflash/src/ef_env_wl.c @@ -77,12 +77,14 @@ static ef_env const *default_env_set = NULL; static size_t default_env_set_size = NULL; /* flash ENV data section size */ static size_t env_data_section_size = NULL; -/* ENV RAM cache */ +/* ENV ram cache */ static uint32_t env_cache[ENV_USER_SETTING_SIZE / 4] = { 0 }; /* ENV start address in flash */ static uint32_t env_start_addr = NULL; /* current using data section address */ static uint32_t cur_using_data_addr = NULL; +/* ENV ram cache has changed when ENV created, deleted and changed value. */ +static bool env_cache_changed = false; #ifdef EF_ENV_USING_PFS_MODE /* next save ENV area address */ @@ -96,7 +98,7 @@ static uint32_t get_env_detail_end_addr(void); static void set_cur_using_data_addr(uint32_t using_data_addr); static void set_env_detail_end_addr(uint32_t end_addr); static EfErrCode write_env(const char *key, const char *value); -static uint32_t *find_env(const char *key); +static char *find_env(const char *key); static size_t get_env_detail_size(void); static size_t get_env_user_used_size(void); static EfErrCode create_env(const char *key, const char *value); @@ -325,6 +327,8 @@ static EfErrCode write_env(const char *key, const char *value) { /* fill '\0' for word alignment */ memset(env_cache_bak, 0, env_str_len - (ker_len + value_len + 2)); set_env_detail_end_addr(get_env_detail_end_addr() + env_str_len); + /* ENV ram cache has changed */ + env_cache_changed = true; return result; } @@ -334,11 +338,10 @@ static EfErrCode write_env(const char *key, const char *value) { * * @param key ENV name * - * @return index of ENV in ram cache + * @return found ENV in ram cache */ -static uint32_t *find_env(const char *key) { - uint32_t *env_cache_addr = NULL; - char *env_start, *env_end, *env; +static char *find_env(const char *key) { + char *env_start, *env_end, *env, *found_env = NULL; size_t key_len = strlen(key), env_len; EF_ASSERT(cur_using_data_addr); @@ -361,7 +364,7 @@ static uint32_t *find_env(const char *key) { while (env < env_end) { /* the key length must be equal */ if (!strncmp(env, key, key_len) && (env[key_len] == '=')) { - env_cache_addr = (uint32_t *) env; + found_env = env; break; } else { /* calculate ENV length, contain '\0'. */ @@ -375,7 +378,7 @@ static uint32_t *find_env(const char *key) { } } - return env_cache_addr; + return found_env; } /** @@ -423,7 +426,7 @@ static EfErrCode create_env(const char *key, const char *value) { */ static EfErrCode del_env(const char *key) { EfErrCode result = EF_NO_ERR; - char *del_env_str = NULL; + char *del_env = NULL; size_t del_env_length, remain_env_length; EF_ASSERT(key); @@ -439,13 +442,13 @@ static EfErrCode del_env(const char *key) { } /* find ENV */ - del_env_str = (char *) find_env(key); + del_env = find_env(key); - if (!del_env_str) { + if (!del_env) { EF_INFO("Not find \"%s\" in ENV.\n", key); return EF_ENV_NAME_ERR; } - del_env_length = strlen(del_env_str); + del_env_length = strlen(del_env); /* '\0' also must be as ENV length */ del_env_length ++; /* the address must multiple of 4 */ @@ -454,11 +457,13 @@ static EfErrCode del_env(const char *key) { } /* calculate remain ENV length */ remain_env_length = get_env_detail_size() - - (((uint32_t) del_env_str + del_env_length) - ((uint32_t) env_cache + ENV_PARAM_PART_BYTE_SIZE)); + - (((uint32_t) del_env + del_env_length) - ((uint32_t) env_cache + ENV_PARAM_PART_BYTE_SIZE)); /* remain ENV move forward */ - memcpy(del_env_str, del_env_str + del_env_length, remain_env_length); + memcpy(del_env, del_env + del_env_length, remain_env_length); /* reset ENV end address */ set_env_detail_end_addr(get_env_detail_end_addr() - del_env_length); + /* ENV ram cache has changed */ + env_cache_changed = true; return result; } @@ -474,6 +479,7 @@ static EfErrCode del_env(const char *key) { */ EfErrCode ef_set_env(const char *key, const char *value) { EfErrCode result = EF_NO_ERR; + char *old_env, *old_value; /* lock the ENV cache */ ef_port_env_lock(); @@ -482,11 +488,20 @@ EfErrCode ef_set_env(const char *key, const char *value) { if (*value == NULL) { result = del_env(key); } else { - /* if find this ENV, then delete it and recreate it */ - if (find_env(key)) { - result = del_env(key); - } - if (result == EF_NO_ERR) { + old_env = find_env(key); + /* If find this ENV, then compare the new value and old value. */ + if (old_env) { + /* find the old value address */ + old_env = strchr(old_env, '='); + old_value = old_env + 1; + /* If it is changed then delete it and recreate it */ + if (strcmp(old_value, value)) { + result = del_env(key); + if (result == EF_NO_ERR) { + result = create_env(key, value); + } + } + } else { result = create_env(key, value); } } @@ -504,17 +519,16 @@ EfErrCode ef_set_env(const char *key, const char *value) { * @return value */ char *ef_get_env(const char *key) { - uint32_t *env_cache_addr = NULL; - char *value = NULL; + char *env = NULL, *value = NULL; /* find ENV */ - env_cache_addr = find_env(key); + env = find_env(key); - if (env_cache_addr == NULL) { + if (env == NULL) { return NULL; } /* get value address */ - value = strchr((char *) env_cache_addr, '='); + value = strchr(env, '='); if (value != NULL) { /* the equal sign next character is value */ value++; @@ -710,6 +724,11 @@ EfErrCode ef_save_env(void) { uint32_t cur_using_addr_bak, move_offset_addr; size_t env_used_size = get_env_user_used_size(); + /* ENV ram cache has not changed don't need to save */ + if (!env_cache_changed) { + return result; + } + #ifndef EF_ENV_USING_PFS_MODE cur_using_addr_bak = get_cur_using_data_addr(); #else @@ -787,6 +806,8 @@ EfErrCode ef_save_env(void) { save_cur_using_data_addr(0xFFFFFFFF); } + env_cache_changed = false; + return result; }