初版
This commit is contained in:
@@ -0,0 +1,280 @@
|
||||
/**
|
||||
* @file data_call_log_storage.c
|
||||
* @brief 通话记录存储实现
|
||||
*/
|
||||
#include "app_config.h"
|
||||
#include "sys_time.h"
|
||||
#include "rtc.h"
|
||||
#include "timestamp.h"
|
||||
#include "data_storage.h"
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[CALL-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".call_log_storage.data.bss")
|
||||
#pragma data_seg(".call_log_storage.data")
|
||||
#pragma const_seg(".call_log_storage.text.const")
|
||||
#pragma code_seg(".call_log_storage.text")
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
|
||||
/**********************
|
||||
* STATIC PROTOTYPES
|
||||
**********************/
|
||||
static small_file_call_log_t curr_call_log;
|
||||
|
||||
|
||||
void small_file_call_log_set_name(char *name, int name_len)
|
||||
{
|
||||
int len;
|
||||
if (!name || !name_len) {
|
||||
return;
|
||||
}
|
||||
|
||||
len = (name_len < CALL_LOG_NAME_LEN) ? name_len : CALL_LOG_NAME_LEN;
|
||||
memcpy(curr_call_log.name, name, len);
|
||||
curr_call_log.name[len - 1] = '\0';
|
||||
}
|
||||
|
||||
void small_file_call_log_set_number(char *number, int number_len)
|
||||
{
|
||||
int len;
|
||||
if (!number || !number_len) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(curr_call_log.number, 0, CALL_LOG_NUMBER_LEN);
|
||||
len = (number_len < CALL_LOG_NUMBER_LEN) ? number_len : CALL_LOG_NUMBER_LEN;
|
||||
memcpy(curr_call_log.number, number, len);
|
||||
curr_call_log.number[len - 1] = '\0';
|
||||
}
|
||||
|
||||
void small_file_call_log_set_date(void)
|
||||
{
|
||||
struct sys_time time;
|
||||
rtc_read_time(&time);
|
||||
curr_call_log.utc_time = timestamp_mytime_2_utc_sec(&time);
|
||||
}
|
||||
|
||||
u32 small_file_call_log_get_date(void)
|
||||
{
|
||||
return curr_call_log.utc_time;
|
||||
}
|
||||
|
||||
void small_file_call_log_set_type(enum CALL_TYPE type)
|
||||
{
|
||||
curr_call_log.type = type;
|
||||
}
|
||||
|
||||
void small_file_call_log_set_sel(enum CALL_SEL call_sel)
|
||||
{
|
||||
curr_call_log.sel = call_sel;
|
||||
}
|
||||
|
||||
int small_file_call_log_save(void)
|
||||
{
|
||||
#if TCFG_DATA_STORAGE_VM_ENABLE
|
||||
/*通话记录使用一个vm id, 这里额外处理*/
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_CALL_LOG, 0);
|
||||
u8 *tmp_buf = NULL;
|
||||
int ret = true;
|
||||
|
||||
if (id != 0) {
|
||||
u32 cur_file_size = small_file_get_size_by_id(F_TYPE_CALL_LOG, id);
|
||||
tmp_buf = zalloc(cur_file_size + SMALL_FILE_CALL_LOG_SIZE);
|
||||
ret = small_file_read(F_TYPE_CALL_LOG, id, 0, tmp_buf, cur_file_size);
|
||||
if (ret != cur_file_size) {
|
||||
log_error("<%s> line:%d", __func__, __LINE__);
|
||||
ret = false;
|
||||
goto __end;
|
||||
}
|
||||
|
||||
memcpy(tmp_buf + cur_file_size, &curr_call_log, SMALL_FILE_CALL_LOG_SIZE);
|
||||
|
||||
ret = small_file_write(F_TYPE_CALL_LOG, &id, 0, tmp_buf, (cur_file_size + SMALL_FILE_CALL_LOG_SIZE), (cur_file_size + SMALL_FILE_CALL_LOG_SIZE));
|
||||
memset(&curr_call_log, 0, SMALL_FILE_CALL_LOG_SIZE);
|
||||
if (ret != (cur_file_size + SMALL_FILE_CALL_LOG_SIZE)) {
|
||||
log_error("<%s> line:%d", __func__, __LINE__);
|
||||
ret = false;
|
||||
goto __end;
|
||||
}
|
||||
} else {
|
||||
ret = small_file_write(F_TYPE_CALL_LOG, &id, 0, &curr_call_log, SMALL_FILE_CALL_LOG_SIZE, SMALL_FILE_CALL_LOG_SIZE);
|
||||
memset(&curr_call_log, 0, SMALL_FILE_CALL_LOG_SIZE);
|
||||
if (ret != SMALL_FILE_CALL_LOG_SIZE) {
|
||||
log_error("<%s> line:%d", __func__, __LINE__);
|
||||
ret = false;
|
||||
goto __end;
|
||||
}
|
||||
}
|
||||
|
||||
__end:
|
||||
if (tmp_buf) {
|
||||
free(tmp_buf);
|
||||
}
|
||||
return ret;
|
||||
#else
|
||||
u32 id = 0;
|
||||
u32 ret = small_file_write(F_TYPE_CALL_LOG, &id, 0, &curr_call_log, SMALL_FILE_CALL_LOG_SIZE, SMALL_FILE_CALL_LOG_SIZE);
|
||||
memset(&curr_call_log, 0, SMALL_FILE_CALL_LOG_SIZE);
|
||||
if (ret != SMALL_FILE_CALL_LOG_SIZE) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int ui_small_file_call_log_read_by_index(small_file_call_log_t *call_log, int index)
|
||||
{
|
||||
#if TCFG_DATA_STORAGE_VM_ENABLE
|
||||
/*通话记录使用一个vm id, 这里额外处理*/
|
||||
int ret;
|
||||
if (!call_log) {
|
||||
return false;
|
||||
}
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_CALL_LOG, 0);
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
u32 size = small_file_get_size_by_id(F_TYPE_CALL_LOG, id);
|
||||
u32 count = size / SMALL_FILE_CALL_LOG_SIZE;
|
||||
|
||||
if (index + 1 > count) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = small_file_read(F_TYPE_CALL_LOG, id, index * SMALL_FILE_CALL_LOG_SIZE, call_log, SMALL_FILE_CALL_LOG_SIZE);
|
||||
|
||||
if (ret == SMALL_FILE_CALL_LOG_SIZE) {
|
||||
return true;
|
||||
}
|
||||
log_error("<%s> line:%d", __func__, __LINE__);
|
||||
return false;
|
||||
|
||||
#else
|
||||
int ret;
|
||||
if (!call_log) {
|
||||
return false;
|
||||
}
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_CALL_LOG, index);
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = small_file_read(F_TYPE_CALL_LOG, id, 0, call_log, sizeof(small_file_call_log_t));
|
||||
if (ret == sizeof(small_file_call_log_t)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int ui_small_file_call_log_get_count(void)
|
||||
{
|
||||
#if TCFG_DATA_STORAGE_VM_ENABLE
|
||||
/*通话记录使用一个vm id, 这里额外处理*/
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_CALL_LOG, 0);
|
||||
if (!id) {
|
||||
return 0;
|
||||
}
|
||||
u32 size = small_file_get_size_by_id(F_TYPE_CALL_LOG, id);
|
||||
return size / SMALL_FILE_CALL_LOG_SIZE;
|
||||
#else
|
||||
return small_file_get_count(F_TYPE_CALL_LOG);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/************************************************
|
||||
* 测试用例
|
||||
***********************************************/
|
||||
#if 0
|
||||
void watch_data_call_log_test(void)
|
||||
{
|
||||
printf("%s %d\n", __FUNCTION__, __LINE__);
|
||||
small_file_call_log_t call_log;
|
||||
int ret;
|
||||
char name[CALL_LOG_DATE_LEN] = "张三李四";
|
||||
char number[CALL_LOG_NUMBER_LEN] = "123456789123";
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
small_file_call_log_set_name(name, sizeof(name));
|
||||
small_file_call_log_set_number(number, sizeof(number));
|
||||
small_file_call_log_set_date();
|
||||
small_file_call_log_set_type(1);
|
||||
small_file_call_log_set_sel(1);
|
||||
small_file_call_log_save();
|
||||
printf("count %d\n", ui_small_file_call_log_get_count());
|
||||
void os_time_dly(int tick);
|
||||
os_time_dly(100);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ret = ui_small_file_call_log_read_by_index(&call_log, i);
|
||||
printf("i:%d ret:%d", i, ret);
|
||||
printf("call_log.date:%d", call_log.utc_time);
|
||||
printf("call_log.name:%s", call_log.name);
|
||||
printf("call_log.number:%s", call_log.number);
|
||||
}
|
||||
|
||||
char name_1[CALL_LOG_DATE_LEN] = {0};
|
||||
char number_1[CALL_LOG_NUMBER_LEN] = "98 654 21";
|
||||
for (int i = 0; i < 5; i++) {
|
||||
small_file_call_log_set_name(name_1, sizeof(name_1));
|
||||
small_file_call_log_set_number(number_1, sizeof(number_1));
|
||||
small_file_call_log_set_date();
|
||||
small_file_call_log_set_type(1);
|
||||
small_file_call_log_set_sel(1);
|
||||
small_file_call_log_save();
|
||||
printf("count %d\n", ui_small_file_call_log_get_count());
|
||||
void os_time_dly(int tick);
|
||||
os_time_dly(100);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ret = ui_small_file_call_log_read_by_index(&call_log, i);
|
||||
printf("i:%d ret:%d", i, ret);
|
||||
printf("call_log.date:%d", call_log.utc_time);
|
||||
printf("call_log.name:%s", call_log.name);
|
||||
printf("call_log.number:%s", call_log.number);
|
||||
}
|
||||
|
||||
printf("%s %d\n", __FUNCTION__, __LINE__);
|
||||
|
||||
ASSERT(ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
#else /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
void small_file_call_log_set_sel(enum CALL_SEL call_sel)
|
||||
{
|
||||
}
|
||||
|
||||
int ui_small_file_call_log_read_by_index(small_file_call_log_t *call_log, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int ui_small_file_call_log_get_count(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void small_file_call_log_set_type(enum CALL_TYPE type)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
#endif /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#ifndef _WATCH_DATA_CALL_LOG_STORAGE_
|
||||
#define _WATCH_DATA_CALL_LOG_STORAGE_
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
#define CALL_LOG_NAME_LEN (20)
|
||||
#define CALL_LOG_NUMBER_LEN (20)
|
||||
#define CALL_LOG_DATE_LEN (20)
|
||||
#define SMALL_FILE_CALL_LOG_SIZE (sizeof(small_file_call_log_t))
|
||||
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
#pragma pack(1)
|
||||
typedef struct call_log {
|
||||
u32 utc_time;
|
||||
char name[CALL_LOG_NAME_LEN];
|
||||
char number[CALL_LOG_NUMBER_LEN];
|
||||
u8 type; /*通话类型*/
|
||||
u8 sel; /*通话设备*/
|
||||
u32 mask;
|
||||
} small_file_call_log_t;
|
||||
#pragma pack()
|
||||
|
||||
enum CALL_SEL {
|
||||
CALL_SEL_AUTO = 0,
|
||||
CALL_SEL_BT, /*蓝牙通话*/
|
||||
CALL_SEL_CAT1, /*CAT1通话*/
|
||||
CALL_SEL_ERA_BLE, /*耳机通话*/
|
||||
|
||||
CALL_SEL_MAX,
|
||||
};
|
||||
|
||||
enum CALL_TYPE {
|
||||
CALL_OUT = 0, /*电话播出*/
|
||||
CALL_INCOME, /*电话打入*/
|
||||
CALL_INCOME_REJECT, /*电话打入拒接*/
|
||||
};
|
||||
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/*通话记录接口*/
|
||||
void small_file_call_log_set_name(char *name, int name_len);
|
||||
void small_file_call_log_set_number(char *number, int number_len);
|
||||
void small_file_call_log_set_date(void);
|
||||
u32 small_file_call_log_get_date(void);
|
||||
void small_file_call_log_set_type(enum CALL_TYPE type);
|
||||
void small_file_call_log_set_sel(enum CALL_SEL call_sel);
|
||||
int small_file_call_log_save(void);
|
||||
|
||||
int ui_small_file_call_log_read_by_index(small_file_call_log_t *call_log, int index);
|
||||
int ui_small_file_call_log_get_count(void);
|
||||
|
||||
|
||||
#endif /*_WATCH_DATA_CALL_LOG_STORAGE_*/
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
/**
|
||||
* @file data_message_storage.c
|
||||
* @brief 消息存储实现
|
||||
*/
|
||||
#include "app_config.h"
|
||||
#include "sys_time.h"
|
||||
#include "flashdb.h"
|
||||
#include "data_storage.h"
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[MSG-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".message_storage.data.bss")
|
||||
#pragma data_seg(".message_storage.data")
|
||||
#pragma const_seg(".message_storage.text.const")
|
||||
#pragma code_seg(".message_storage.text")
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
|
||||
|
||||
#define WRITE_BIG_U32(a,src) {*((u8*)(a)+0) = (u8)((src)>>24); *((u8*)(a)+1) = (u8)(((src)>>16)&0xff);*((u8*)(a)+2) = (u8)(((src)>>8)&0xff);*((u8*)(a)+3) = (u8)((src)&0xff);}
|
||||
|
||||
static u32 ancs_message_uid;
|
||||
|
||||
|
||||
int ui_small_file_message_get_count(void)
|
||||
{
|
||||
return small_file_get_count(F_TYPE_MESSAGE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int small_file_message_read_by_index(small_file_message_t *message, int index)
|
||||
{
|
||||
/* struct fdb_kv_iterator iterator; */
|
||||
/* fdb_kv_t cur_kv; */
|
||||
/* struct fdb_blob blob; */
|
||||
/* u32 data_size; */
|
||||
u8 *data_buf;
|
||||
/* int count = 0; */
|
||||
u8 T;
|
||||
u8 *V;
|
||||
u16 L;
|
||||
u16 offset = 0;
|
||||
u32 copy_len;
|
||||
|
||||
if (!message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_MESSAGE, index);
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
data_buf = zalloc(512);
|
||||
if (!data_buf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int ret = small_file_read(F_TYPE_MESSAGE, id, 0, data_buf, 512);
|
||||
if (!ret) {
|
||||
ASSERT(0);
|
||||
}
|
||||
/* put_buf(data_buf, ret); */
|
||||
|
||||
while (offset < ret) {
|
||||
L = (data_buf[offset] << 8) | data_buf[offset + 1];
|
||||
T = data_buf[offset + 2];
|
||||
V = &data_buf[offset + 3];
|
||||
if (T == MESSAGE_TIMESTAMP_TYPE) {
|
||||
copy_len = (L - 1) > MESSAGE_TIMESTAMP_LEN ? MESSAGE_TIMESTAMP_LEN : (L - 1);
|
||||
memcpy(&message->timestamp, V, copy_len);
|
||||
/* printf("TimeStamp:0x%x",message->timestamp); */
|
||||
} else if (T == MESSAGE_PACKAGENAME_TYPE) {
|
||||
copy_len = (L - 1) > MESSAGE_PACKAGENAME_LEN ? MESSAGE_PACKAGENAME_LEN : (L - 1);
|
||||
memset(message->packagename, 0, MESSAGE_PACKAGENAME_LEN);
|
||||
memcpy(message->packagename, V, copy_len);
|
||||
/* log_info("packagename:%s", temp_message_buf->packagename); */
|
||||
} else if (T == MESSAGE_APP_IDENTIFIER_TYPE) {
|
||||
copy_len = (L - 1) > MESSAGE_APP_IDENTIFIER_LEN ? MESSAGE_APP_IDENTIFIER_LEN : (L - 1);
|
||||
memcpy(&message->app_identifier, V, copy_len);
|
||||
/* log_info("AppIdentifier:%d", temp_message_buf->AppIdentifier); */
|
||||
} else if (T == MESSAGE_TITLE_TYPE) {
|
||||
copy_len = (L - 1) > MESSAGE_TITLE_LEN ? MESSAGE_TITLE_LEN : (L - 1);
|
||||
memset(message->title, 0, MESSAGE_TITLE_LEN);
|
||||
memcpy(message->title, V, copy_len);
|
||||
/* log_info("title:%s", temp_message_buf->title); */
|
||||
} else if (T == MESSAGE_CONTENT_TYPE) {
|
||||
copy_len = (L - 1) > MESSAGE_CONTENT_LEN ? MESSAGE_CONTENT_LEN : (L - 1);
|
||||
memset(message->content, 0, MESSAGE_CONTENT_LEN);
|
||||
memcpy(message->content, V, copy_len);
|
||||
}
|
||||
offset += L + 2;
|
||||
}
|
||||
free(data_buf);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void message_set_info_from_ancs(void *info, void *name, void *data, u16 len)
|
||||
{
|
||||
u32 copy_len;
|
||||
long date_value;
|
||||
char date_string[9];
|
||||
char *end;
|
||||
u32 timestamp = 0;
|
||||
small_file_message_t *p_msg = info;
|
||||
|
||||
ASSERT(p_msg);
|
||||
if (!strcmp((char *)name, "UID")) {
|
||||
ancs_message_uid = *(u32 *)data;
|
||||
} else if (!strcmp((char *)name, "AppIdentifier")) {
|
||||
copy_len = len > MESSAGE_PACKAGENAME_LEN ? MESSAGE_PACKAGENAME_LEN : len;
|
||||
memset(p_msg->packagename, 0, MESSAGE_PACKAGENAME_LEN);
|
||||
memcpy(p_msg->packagename, (u8 *)data, copy_len);
|
||||
log_debug("packagename:%s", p_msg->packagename);
|
||||
if (!strcmp((char *)data, PACKAGE_NAME_SYS_MESSAGE_SEND)) {
|
||||
p_msg->app_identifier = 1;
|
||||
} else if (!strcmp((char *)data, PACKAGE_NAME_SYS_MESSAGE_RECEIVE)) {
|
||||
p_msg->app_identifier = 1;
|
||||
} else if (!strcmp((char *)data, IOS_PACKAGE_NAME_SYS_MESSAGE)) {
|
||||
p_msg->app_identifier = 1;
|
||||
} else if (!strcmp((char *)data, IOS_PACKAGE_NAME_WECHAT)) {
|
||||
p_msg->app_identifier = 2;
|
||||
} else if (!strcmp((char *)data, IOS_PACKAGE_NAME_QQ)) {
|
||||
p_msg->app_identifier = 3;
|
||||
} else if (!strcmp((char *)data, IOS_PACKAGE_NAME_DING_DING)) {
|
||||
p_msg->app_identifier = 4;
|
||||
} else {
|
||||
p_msg->app_identifier = 0;
|
||||
}
|
||||
log_debug("app_identifier:%d", p_msg->app_identifier);
|
||||
} else if (!strcmp((char *)name, "IDTitle")) {
|
||||
copy_len = len > MESSAGE_TITLE_LEN ? MESSAGE_TITLE_LEN : len;
|
||||
memset(p_msg->title, 0, MESSAGE_TITLE_LEN);
|
||||
memcpy(p_msg->title, (u8 *)data, copy_len);
|
||||
log_debug("%s %d %d\n", data, len, __LINE__);
|
||||
} else if (!strcmp((char *)name, "IDMessage")) {
|
||||
copy_len = len > MESSAGE_CONTENT_LEN ? MESSAGE_CONTENT_LEN : len;
|
||||
memset(p_msg->content, 0, MESSAGE_CONTENT_LEN);
|
||||
memcpy(p_msg->content, (u8 *)data, copy_len);
|
||||
log_debug("id_msg:%s %d %d\n", data, len, __LINE__);
|
||||
} else if (!strcmp((char *)name, "IDDate")) {
|
||||
/*格式:20240905T174158*/
|
||||
|
||||
/*秒*/
|
||||
snprintf(date_string, (2 + 1), "%s", (char *)data + 13);
|
||||
date_value = strtol(date_string, &end, 10);
|
||||
log_debug("msc date_string:%s date_value:%d", date_string, date_value);
|
||||
timestamp |= (date_value & 0x3f);
|
||||
|
||||
/*分*/
|
||||
snprintf(date_string, (2 + 1), "%s", (char *)data + 11);
|
||||
date_value = strtol(date_string, &end, 10);
|
||||
log_debug("min date_string:%s date_value:%d", date_string, date_value);
|
||||
timestamp |= (date_value & 0x3f) << 6;
|
||||
|
||||
/*时*/
|
||||
snprintf(date_string, (2 + 1), "%s", (char *)data + 9);
|
||||
date_value = strtol(date_string, &end, 10);
|
||||
log_debug("hour date_string:%s date_value:%d", date_string, date_value);
|
||||
timestamp |= (date_value & 0x1f) << 12;
|
||||
|
||||
/*日*/
|
||||
snprintf(date_string, (2 + 1), "%s", (char *)data + 6);
|
||||
date_value = strtol(date_string, &end, 10);
|
||||
log_debug("day date_string:%s date_value:%d", date_string, date_value);
|
||||
timestamp |= (date_value & 0x1f) << 17;
|
||||
|
||||
/*月*/
|
||||
snprintf(date_string, (2 + 1), "%s", (char *)data + 4);
|
||||
date_value = strtol(date_string, &end, 10);
|
||||
log_debug("month date_string:%s date_value:%d", date_string, date_value);
|
||||
timestamp |= (date_value & 0x0f) << 21;
|
||||
|
||||
/*年*/
|
||||
snprintf(date_string, (4 + 1), "%s", (char *)data);
|
||||
date_value = strtol(date_string, &end, 10) - 2010;
|
||||
log_debug("year date_string:%s date_value:%d", date_string, date_value);
|
||||
timestamp |= (date_value & 0x3f) << 26;
|
||||
|
||||
WRITE_BIG_U32(&p_msg->timestamp, timestamp);
|
||||
log_debug("IDDate:%s timestamp:%x, p_msg->timestamp:%x", data, timestamp, p_msg->timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
void message_add_info_from_ancs(void *info)
|
||||
{
|
||||
u32 offset = 0;
|
||||
u8 *temp_ptr = 0;
|
||||
small_file_message_t *p_msg = info;
|
||||
ASSERT(p_msg);
|
||||
u8 packagename_len = strlen((const char *)p_msg->packagename);
|
||||
u8 title_len = strlen((const char *)p_msg->title);
|
||||
u8 content_len = strlen((const char *)p_msg->content);
|
||||
/* length+type: 3 byte*/
|
||||
u32 mess_data_len = 3 + MESSAGE_TIMESTAMP_LEN + 3 + packagename_len +
|
||||
3 + MESSAGE_APP_IDENTIFIER_LEN + 3 + title_len +
|
||||
3 + content_len;
|
||||
|
||||
temp_ptr = zalloc(mess_data_len);
|
||||
if (!temp_ptr) {
|
||||
log_error("temp_ptr malloc fail");
|
||||
return;
|
||||
}
|
||||
|
||||
temp_ptr[offset] = (MESSAGE_TIMESTAMP_LEN + 1) >> 8;
|
||||
offset++;
|
||||
temp_ptr[offset] = (MESSAGE_TIMESTAMP_LEN + 1) & 0xff;
|
||||
offset++;
|
||||
temp_ptr[offset] = 0;
|
||||
offset++;
|
||||
memcpy(&temp_ptr[offset], &p_msg->timestamp, MESSAGE_TIMESTAMP_LEN);
|
||||
offset += MESSAGE_TIMESTAMP_LEN;
|
||||
|
||||
temp_ptr[offset] = (packagename_len + 1) >> 8;
|
||||
offset++;
|
||||
temp_ptr[offset] = (packagename_len + 1) & 0xff;
|
||||
offset++;
|
||||
temp_ptr[offset] = 1;
|
||||
offset++;
|
||||
memcpy(&temp_ptr[offset], p_msg->packagename, packagename_len);
|
||||
offset += packagename_len;
|
||||
|
||||
temp_ptr[offset] = (MESSAGE_APP_IDENTIFIER_LEN + 1) >> 8;
|
||||
offset++;
|
||||
temp_ptr[offset] = (MESSAGE_APP_IDENTIFIER_LEN + 1) & 0xff;
|
||||
offset++;
|
||||
temp_ptr[offset] = 2;
|
||||
offset++;
|
||||
temp_ptr[offset] = p_msg->app_identifier;
|
||||
offset += MESSAGE_APP_IDENTIFIER_LEN;
|
||||
|
||||
temp_ptr[offset] = (title_len + 1) >> 8;
|
||||
offset++;
|
||||
temp_ptr[offset] = (title_len + 1) & 0xff;
|
||||
offset++;
|
||||
temp_ptr[offset] = 3;
|
||||
offset++;
|
||||
memcpy(&temp_ptr[offset], p_msg->title, title_len);
|
||||
offset += title_len;
|
||||
|
||||
temp_ptr[offset] = (content_len + 1) >> 8;
|
||||
offset++;
|
||||
temp_ptr[offset] = (content_len + 1) & 0xff;
|
||||
offset++;
|
||||
temp_ptr[offset] = 4;
|
||||
offset++;
|
||||
memcpy(&temp_ptr[offset], p_msg->content, content_len);
|
||||
|
||||
u32 file_id = ancs_message_uid;
|
||||
small_file_write(F_TYPE_MESSAGE, &file_id, 0, temp_ptr, mess_data_len, mess_data_len);
|
||||
log_debug("ancs_message_uid:%x, file_id:%x", ancs_message_uid, file_id);
|
||||
ancs_message_uid = 0;
|
||||
|
||||
free(temp_ptr);
|
||||
|
||||
// UI SHOW
|
||||
#ifdef CONFIG_UI_STYLE_JL_CSC_PUBLIC_MODLS_ENABLE
|
||||
#if TCFG_UI_MSG_NOTICE
|
||||
if (ui_small_file_message_get_count() < 10) {
|
||||
UI_MSG_POST("message_status:event=%4", 1);
|
||||
} else {
|
||||
UI_MSG_POST("message_status:event=%4", 3);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#if 0
|
||||
if (flash_message_count() < 10) {
|
||||
UI_MSG_POST("message_status:event=%4", 1);
|
||||
} else {
|
||||
UI_MSG_POST("message_status:event=%4", 3);
|
||||
}
|
||||
#else
|
||||
/* u8 cur_task = app_get_curr_task(); */
|
||||
/* switch (cur_task) { */
|
||||
/* case APP_POWERON_TASK: */
|
||||
/* case APP_POWEROFF_TASK: */
|
||||
/* case APP_WATCH_UPDATE_TASK: */
|
||||
/* case APP_SMARTBOX_ACTION_TASK: */
|
||||
/* break; */
|
||||
/* default: */
|
||||
/* if (UI_WINDOW_PREEMPTION_CHECK()) { */
|
||||
/* break; */
|
||||
/* } */
|
||||
/* if (get_screen_saver_status()) { */
|
||||
/* ui_screen_recover(0); */
|
||||
/* ui_auto_shut_down_enable(); */
|
||||
/* UI_SHOW_WINDOW(ID_WINDOW_MESS); */
|
||||
/* } else { */
|
||||
/* ui_auto_shut_down_re_run(); */
|
||||
/* if (UI_GET_WINDOW_ID() == ID_WINDOW_MESS) { */
|
||||
/* if (flash_message_count() < 10) { */
|
||||
/* UI_MSG_POST("message_status:event=%4", 1); */
|
||||
/* } else { */
|
||||
/* UI_MSG_POST("message_status:event=%4", 3); */
|
||||
/* } */
|
||||
/* } else { */
|
||||
/* UI_HIDE_CURR_WINDOW(); */
|
||||
/* UI_SHOW_WINDOW(ID_WINDOW_MESS); */
|
||||
/* } */
|
||||
/* } */
|
||||
/* break; */
|
||||
/* } */
|
||||
#endif
|
||||
// moto
|
||||
/* UI_MOTO_RUN(2); */
|
||||
}
|
||||
|
||||
|
||||
#else /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
void message_set_info_from_ancs(void *info, void *name, void *data, u16 len)
|
||||
{
|
||||
}
|
||||
void message_add_info_from_ancs(void *info)
|
||||
{
|
||||
}
|
||||
|
||||
int ui_small_file_message_get_count(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int small_file_message_read_by_index(small_file_message_t *message, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef _WATCH_DATA_MESSAGE_STORAGE_
|
||||
#define _WATCH_DATA_MESSAGE_STORAGE_
|
||||
|
||||
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
#define MESSAGE_TIMESTAMP_TYPE (0)
|
||||
#define MESSAGE_PACKAGENAME_TYPE (1)
|
||||
#define MESSAGE_APP_IDENTIFIER_TYPE (2)
|
||||
#define MESSAGE_TITLE_TYPE (3)
|
||||
#define MESSAGE_CONTENT_TYPE (4)
|
||||
|
||||
|
||||
#define MESSAGE_TIMESTAMP_LEN (4)
|
||||
#define MESSAGE_PACKAGENAME_LEN (31+1)
|
||||
#define MESSAGE_APP_IDENTIFIER_LEN (1)
|
||||
#define MESSAGE_TITLE_LEN (36+1)
|
||||
#define MESSAGE_CONTENT_LEN (439+1)
|
||||
|
||||
#define SMALL_FILE_MESSAGE_SIZE (sizeof(small_file_message_t))
|
||||
#define SMALL_FILE_MESSAGE_PARTITION_NAME "message"
|
||||
|
||||
|
||||
#define PACKAGE_NAME_SYS_MESSAGE_RECEIVE "MobileSMS_RECEIVE"
|
||||
#define PACKAGE_NAME_SYS_MESSAGE_SEND "MobileSMS_SEND"
|
||||
|
||||
#define IOS_PACKAGE_NAME_SYS_MESSAGE "com.apple.MobileSMS"
|
||||
#define IOS_PACKAGE_NAME_WECHAT "com.tencent.xin"
|
||||
#define IOS_PACKAGE_NAME_QQ "com.tencent.mqq"
|
||||
#define IOS_PACKAGE_NAME_DING_DING "com.laiwang.DingTalk"
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct message {
|
||||
u32 timestamp;
|
||||
char packagename[MESSAGE_PACKAGENAME_LEN];
|
||||
u8 app_identifier;
|
||||
u8 title[MESSAGE_TITLE_LEN];
|
||||
u8 content[MESSAGE_CONTENT_LEN];
|
||||
} small_file_message_t;
|
||||
#pragma pack()
|
||||
|
||||
|
||||
|
||||
int ui_small_file_message_get_count(void);
|
||||
int small_file_message_read_by_index(small_file_message_t *message, int index);
|
||||
void message_set_info_from_ancs(void *info, void *name, void *data, u16 len);
|
||||
void message_add_info_from_ancs(void *info);
|
||||
|
||||
|
||||
|
||||
#endif /*_WATCH_DATA_MESSAGE_STORAGE_*/
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* @file data_phonebook_storage.c
|
||||
* @brief 电话本存储相关
|
||||
*/
|
||||
#include "app_config.h"
|
||||
#include "flashdb.h"
|
||||
#include "fdb_low_lvl.h"
|
||||
#include "data_storage.h"
|
||||
#include "data_phonebook_storage.h"
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[PBOOK-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".phonebook_storage.bss")
|
||||
#pragma data_seg(".phonebook_storage.data")
|
||||
#pragma const_seg(".phonebook_storage.text.const")
|
||||
#pragma code_seg(".phonebook_storage.text")
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
|
||||
|
||||
int ui_small_file_phonebook_get_count(void)
|
||||
{
|
||||
int phonebook_count;
|
||||
u32 file_len = small_file_get_size_by_index(F_TYPE_PHONEBOOK, 0);
|
||||
phonebook_count = file_len / SMALL_FILE_PHONEBOOK_SIZE;
|
||||
return phonebook_count;
|
||||
}
|
||||
|
||||
|
||||
int ui_small_file_phonebook_read_by_index(small_file_phonebook_t *phonebook, int index)
|
||||
{
|
||||
int ret;
|
||||
if (!phonebook) {
|
||||
return false;
|
||||
}
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_PHONEBOOK, 0);
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
int phonebook_count = ui_small_file_phonebook_get_count();
|
||||
if (index >= phonebook_count) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = small_file_read(F_TYPE_PHONEBOOK, id, index * sizeof(struct small_file_phonebook), phonebook, sizeof(struct small_file_phonebook));
|
||||
|
||||
/*jlui框架显示文字,数字需要用到结束符。避免app传过来,不带结束符。*/
|
||||
phonebook->name[PHONEBOOK_NAME_LEN - 1] = 0;
|
||||
phonebook->number[PHONEBOOK_NUMBER_LEN - 1] = 0 ;
|
||||
|
||||
if (ret == sizeof(struct small_file_phonebook)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*电话号码除去空格*/
|
||||
static int phone_number_neaten(const char *in_number, char *out_number)
|
||||
{
|
||||
int out_len = 0;
|
||||
for (int i = 0; i < PHONEBOOK_NUMBER_LEN - 1; i++) {
|
||||
if (in_number[i] != ' ') {
|
||||
out_number[out_len++] = in_number[i];
|
||||
}
|
||||
}
|
||||
out_number[out_len] = 0;
|
||||
return out_len;
|
||||
}
|
||||
|
||||
int small_file_phonebook_get_name_by_number(char *name, char *number)
|
||||
{
|
||||
int phonebook_count;
|
||||
small_file_phonebook_t phonebook;
|
||||
char temp_number[PHONEBOOK_NUMBER_LEN];
|
||||
|
||||
if (!name || !number) {
|
||||
return false;
|
||||
}
|
||||
|
||||
phonebook_count = ui_small_file_phonebook_get_count();
|
||||
if (!phonebook_count) {
|
||||
memcpy(name, "UNKNOW", strlen("UNKNOW") + 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < phonebook_count; i++) {
|
||||
ui_small_file_phonebook_read_by_index(&phonebook, i);
|
||||
phone_number_neaten(phonebook.number, temp_number);
|
||||
if (!strcmp(temp_number, number)) {
|
||||
memcpy(name, phonebook.name, sizeof(phonebook.name));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(name, "UNKNOW", strlen("UNKNOW") + 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/************************************************
|
||||
* 测试用例
|
||||
***********************************************/
|
||||
#if 0
|
||||
|
||||
small_file_phonebook_t test_phonebook[] = {
|
||||
{"aaaaa", "12345678123"},
|
||||
{"张三", "13452341111"},
|
||||
{"测试号码", "19507567268"},
|
||||
{"你好", "546 2563462"},
|
||||
{"ddddd", "04809234"},
|
||||
{"李四", "049 852 09348"},
|
||||
};
|
||||
|
||||
void watch_data_phonebook_test(void)
|
||||
{
|
||||
/*不带id创建*/
|
||||
/* u32 create_id = 0; */
|
||||
|
||||
/*带id创建*/
|
||||
u32 create_id = 0x2198abcd;
|
||||
|
||||
small_file_write(F_TYPE_PHONEBOOK, &create_id, 0, test_phonebook, sizeof(test_phonebook), sizeof(test_phonebook));
|
||||
printf("<%s> create_id:%x", __func__, create_id);
|
||||
|
||||
u8 *read_phonebook_buf = zalloc(sizeof(test_phonebook));
|
||||
small_file_read(F_TYPE_PHONEBOOK, create_id, 0, read_phonebook_buf, sizeof(test_phonebook));
|
||||
put_buf(read_phonebook_buf, sizeof(test_phonebook));
|
||||
|
||||
char name[PHONEBOOK_NAME_LEN] = {0};
|
||||
if (small_file_phonebook_get_name_by_number(name, "13452341111")) {
|
||||
printf("<%s> find name:%s", __func__, name);
|
||||
}
|
||||
|
||||
small_file_delete_by_id(F_TYPE_PHONEBOOK, create_id);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#else /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
int ui_small_file_phonebook_get_count(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int ui_small_file_phonebook_read_by_index(small_file_phonebook_t *phonebook, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int small_file_phonebook_get_name_by_number(char *name, char *number)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef _WATCH_DATA_PHONEBOOK_STORAGE_
|
||||
#define _WATCH_DATA_PHONEBOOK_STORAGE_
|
||||
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
#define PHONEBOOK_NAME_LEN (20)
|
||||
#define PHONEBOOK_NUMBER_LEN (20)
|
||||
#define SMALL_FILE_PHONEBOOK_SIZE (sizeof(small_file_phonebook_t))
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
typedef struct small_file_phonebook {
|
||||
char name[PHONEBOOK_NAME_LEN];
|
||||
char number[PHONEBOOK_NUMBER_LEN];
|
||||
} small_file_phonebook_t;
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 获取存储的联系人数量
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int ui_small_file_phonebook_get_count(void);
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过index获取对应的联系人信息
|
||||
*
|
||||
* @Params phonebook 存储读取到的联系人信息
|
||||
* @Params index 序号
|
||||
*
|
||||
* @Return true or false
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int ui_small_file_phonebook_read_by_index(small_file_phonebook_t *phonebook, int index);
|
||||
|
||||
int small_file_phonebook_get_name_by_number(char *name, char *number);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /*_WATCH_DATA_PHONEBOOK_STORAGE_*/
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
#include "app_config.h"
|
||||
#include "includes.h"
|
||||
#include "typedef.h"
|
||||
#include "file_simple_transfer.h"
|
||||
#include "rcsp_config.h"
|
||||
#include "data_storage.h"
|
||||
#include "app_config.h"
|
||||
|
||||
#define LOG_TAG_CONST RCSP_ADAPTOR
|
||||
#define LOG_TAG "[RCSP-ADAPTOR]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
/* #define LOG_DUMP_ENABLE */
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
|
||||
|
||||
#if (RCSP_MODE && TCFG_DEV_MANAGER_ENABLE && JL_RCSP_SIMPLE_TRANSFER)
|
||||
|
||||
|
||||
|
||||
//*----------------------------------------------------------------------------*/
|
||||
/**@brief 获取当前类型小文件列表
|
||||
@param 无
|
||||
@return 数量*4
|
||||
@note
|
||||
*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int simple_trans_get_id_table_len(u8 file_type)
|
||||
{
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
return small_file_get_id_table_len(file_type);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int simple_trans_get_id_table(u8 file_type, u8 *table_data, u16 data_len)
|
||||
{
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
return small_file_get_id_table(file_type, table_data, data_len);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 读取小文件数据
|
||||
*
|
||||
* @Return 读取的字节数
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
static int simple_trans_read_file_by_id(u8 file_type, u16 id, u32 data_offset, u8 *data, u16 data_len)
|
||||
{
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
int rlen;
|
||||
rlen = small_file_read(file_type, id, data_offset, data, data_len);
|
||||
log_info_hexdump(data, rlen);
|
||||
log_info("%s file_type:%d id:%d data_offset:%d data_len:%d rlen:%d\n", __FUNCTION__, file_type, id, data_offset, data_len, rlen);
|
||||
return rlen;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 写入小文件数据
|
||||
*
|
||||
* @Return 0:成功 -1:失败
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
static int simple_trans_insert_file_by_id(u8 file_type, u16 *id, u32 data_offset, u8 *data, u16 data_len, u16 file_total_size)
|
||||
{
|
||||
log_info("<%s> file_type:%d, id:%x, data_offset:%x, data_len:%x, file_total_size:%x\n", __func__, file_type, *id, data_offset, data_len, file_total_size);
|
||||
log_info_hexdump(data, data_len);
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
//TODO 判断这个文件剩余空间是否足够
|
||||
//
|
||||
//
|
||||
u32 creat_id;
|
||||
creat_id = *id;
|
||||
|
||||
int ret = small_file_write(file_type, &creat_id, data_offset, data, data_len, file_total_size);
|
||||
|
||||
if (ret != data_len) {
|
||||
*id = 0;
|
||||
} else {
|
||||
*id = (u16)creat_id;
|
||||
}
|
||||
|
||||
log_info("<%s> creat id:%d", __func__, *id);
|
||||
if (ret != data_len) {
|
||||
log_error("<%s> fail:%d %d", __func__, ret, data_len);
|
||||
return -1;
|
||||
}
|
||||
//TODO ui页面显示
|
||||
#if TCFG_UI_ENABLE_SMARTWIN|| TCFG_UI_ENABLE_NOTICE
|
||||
if (file_type == F_TYPE_MESSAGE) {
|
||||
//灵动岛
|
||||
UI_MSG_POST("smartwin_status:message=%4", 1);
|
||||
//消息通知
|
||||
if (ui_small_file_message_get_count() < 10) {
|
||||
UI_MSG_POST("message_status:event=%4", 1);
|
||||
} else {
|
||||
UI_MSG_POST("message_status:event=%4", 3);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_UI_STYLE_JL_CSC_PUBLIC_MODLS_ENABLE
|
||||
#if TCFG_UI_MSG_NOTICE
|
||||
if (ui_small_file_message_get_count() < 10) {
|
||||
UI_MSG_POST("message_status:event=%4", 1);
|
||||
} else {
|
||||
UI_MSG_POST("message_status:event=%4", 3);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 更新小文件数据
|
||||
*
|
||||
* @Return 0:成功 -1:失败
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
static int simple_trans_update_file_by_id(u8 file_type, u16 id, u32 data_offset, u8 *data, u16 data_len, u16 file_total_size)
|
||||
{
|
||||
log_info("<%s> id:%d, data_offset:%d, data_len:%d, file_total_size:%d", __func__, id, data_offset, data_len, file_total_size);
|
||||
log_info_hexdump(data, data_len);
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
//TODO 判断这个文件剩余空间是否足够
|
||||
u32 creat_id;
|
||||
creat_id = id;
|
||||
int ret = small_file_write(file_type, &creat_id, data_offset, data, data_len, file_total_size);
|
||||
if (ret != data_len) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
//TODO ui页面显示
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 删除小文件数据
|
||||
*
|
||||
* @Return 0:成功 -1:失败
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
static int simple_trans_delete_file_by_id(u8 file_type, u16 id)
|
||||
{
|
||||
log_info("<%s> file_type:%d, id:%d", __func__, file_type, id);
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
int ret;
|
||||
ret = small_file_delete_by_id(file_type, id);
|
||||
if (ret == false) {
|
||||
return -1;
|
||||
}
|
||||
#if TCFG_UI_MSG_NOTICE ||TCFG_UI_ENABLE_NOTICE
|
||||
if (file_type == F_TYPE_MESSAGE) {
|
||||
//消息通知
|
||||
UI_MSG_POST("message_status:event=%4", 2);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static const rcsp_simple_trans_opt g_test_trans_opt = {
|
||||
.get_id_table_len = simple_trans_get_id_table_len,
|
||||
.get_id_table = simple_trans_get_id_table,
|
||||
.read_file_by_id = simple_trans_read_file_by_id,
|
||||
.insert_file_by_id = simple_trans_insert_file_by_id,
|
||||
.update_file_by_id = simple_trans_update_file_by_id,
|
||||
.delete_file_by_id = simple_trans_delete_file_by_id,
|
||||
};
|
||||
|
||||
int test_simple_trans(void)
|
||||
{
|
||||
rcsp_register_file_simple_transfer_interface((rcsp_simple_trans_opt *)&g_test_trans_opt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
late_initcall(test_simple_trans);
|
||||
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
#if TRANS_ANCS_EN
|
||||
|
||||
static small_file_message_t *ancs_message_temp;
|
||||
void notice_set_info_from_ancs(void *name, void *data, u16 len)
|
||||
{
|
||||
if (!ancs_message_temp) {
|
||||
ancs_message_temp = zalloc(SMALL_FILE_MESSAGE_SIZE);
|
||||
}
|
||||
|
||||
if (!ancs_message_temp) {
|
||||
log_error("ancs_message_temp is NULL !!!");
|
||||
return;
|
||||
}
|
||||
message_set_info_from_ancs(ancs_message_temp, name, data, len);
|
||||
}
|
||||
|
||||
void notice_add_info_from_ancs()
|
||||
{
|
||||
if (!ancs_message_temp) {
|
||||
log_error("ancs_message_temp must be valid !!!");
|
||||
return;
|
||||
}
|
||||
|
||||
message_add_info_from_ancs(ancs_message_temp);
|
||||
if (ancs_message_temp) {
|
||||
free(ancs_message_temp);
|
||||
ancs_message_temp = NULL;
|
||||
}
|
||||
|
||||
#if TCFG_UI_ENABLE_SMARTWIN|| TCFG_UI_ENABLE_NOTICE
|
||||
//灵动岛
|
||||
UI_MSG_POST("smartwin_status:message=%4", 1);
|
||||
//消息通知
|
||||
if (ui_small_file_message_get_count() < 10) {
|
||||
UI_MSG_POST("message_status:event=%4", 1);
|
||||
} else {
|
||||
UI_MSG_POST("message_status:event=%4", 3);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void notice_remove_info_from_ancs(u32 uid)
|
||||
{
|
||||
if (ui_small_file_message_get_count() == 0) {
|
||||
log_warn("message null!!!");
|
||||
return;
|
||||
}
|
||||
|
||||
log_debug("<%s> uid:%x", __func__, uid);
|
||||
small_file_delete_by_id(F_TYPE_MESSAGE, uid);
|
||||
#if TCFG_UI_ENABLE_NOTICE
|
||||
UI_MSG_POST("message_status:event=%4", 2);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif //#if TRANS_ANCS_EN
|
||||
#endif //#if TCFG_DATA_STORAGE_ENABLE
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
#ifndef _WATCH_DATA_STORAGE_H
|
||||
#define _WATCH_DATA_STORAGE_H
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "data_phonebook_storage.h"
|
||||
#include "data_call_log_storage.h"
|
||||
#include "data_message_storage.h"
|
||||
#include "data_weather_storage.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/* 小文件类型 */
|
||||
#define F_TYPE_BASE (1)
|
||||
|
||||
#define F_TYPE_PHONEBOOK (F_TYPE_BASE)
|
||||
#define F_TYPE_SPORTRECORD (F_TYPE_BASE+1)
|
||||
#define F_TYPE_HEART (F_TYPE_BASE+2)
|
||||
#define F_TYPE_BLOOD_OXYGEN (F_TYPE_BASE+3)
|
||||
#define F_TYPE_SLEEP (F_TYPE_BASE+4)
|
||||
#define F_TYPE_MESSAGE (F_TYPE_BASE+5)
|
||||
#define F_TYPE_WEATHER (F_TYPE_BASE+6)
|
||||
#define F_TYPE_CALL_LOG (F_TYPE_BASE+7)
|
||||
#define F_TYPE_STEP (F_TYPE_BASE+8)
|
||||
#define F_TYPE_HEART_SINGLE (F_TYPE_BASE+9)
|
||||
#define F_TYPE_BLOOD_OXYGEN_SINGLE (F_TYPE_BASE+10)
|
||||
|
||||
#define F_TYPE_MAX (F_TYPE_BLOOD_OXYGEN_SINGLE)
|
||||
#define F_TYPE_COUNT (F_TYPE_MAX +1- F_TYPE_BASE)
|
||||
|
||||
#define ID_TABLE_ITEM_SIZE 4
|
||||
/*ID_TABLE 格式:每一项4个字节 低位两个字节file_id 高位两个字节file_size*/
|
||||
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
|
||||
struct small_file {
|
||||
u8 type;
|
||||
u8 max_item;
|
||||
u8 storage_sel;
|
||||
};
|
||||
|
||||
|
||||
int data_small_file_init(void);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过id,读取对应小文件内容
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
* @Params id 文件id
|
||||
* @Params buf 存储要读取文件数据的buffer
|
||||
* @Params len 要读取的数据长度
|
||||
*
|
||||
* @Return 读取到的字节数
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_read(u8 small_file_type, u32 id, u32 offset, void *buf, u32 len);
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 写入小文件数据
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
* @Params buf_offset 要写入数据的偏移
|
||||
* @Params buf 存储写入文件数据的buffer
|
||||
* @Params len 要写入数据的长度
|
||||
*
|
||||
* @Return 写入成功 返回u16位大小的id; 写入失败 返回id为0
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
u32 small_file_write(u8 small_file_type, u32 *id, u32 buf_offset, void *buf, u32 len, u32 total_len);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过id,更新对应小文件内容(仅原来内容范围内)
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
* @Params id 文件id
|
||||
* @Params buf_offset 要写入数据的偏移
|
||||
* @Params buf 存储写入文件数据的buffer
|
||||
* @Params len 要写入数据的长度
|
||||
*
|
||||
* @Return 实际写入长度
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_update_by_id(u8 small_file_type, u32 id, u32 buf_offset, void *buf, u32 len, u32 total_len);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过id,删除小文件
|
||||
*
|
||||
* @Params file_type 小文件类型
|
||||
* @Params id 文件id
|
||||
*
|
||||
* @Return false or true
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_delete_by_id(u8 small_file_type, u32 id);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 删除所有小文件
|
||||
*
|
||||
* @Return false or true
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_del_all(void);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 获取小文件的id_table的大小
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
*
|
||||
* @Return length of the id_table
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_get_id_table_len(u8 small_file_type);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 获取小文件的id_table
|
||||
*
|
||||
* @Params small_file_type
|
||||
* @Params table_data Pointer to the buffer where the id_table data will be stored.
|
||||
* @Params data_len The length of the table_data buffer.
|
||||
*
|
||||
* @Return false or true
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_get_id_table(u8 small_file_type, u8 *table_data, u16 data_len);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 获取小文件的数量
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
*
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_get_count(u8 small_file_type);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过index获取小文件的id
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
* @Params index 文件存储顺序序号
|
||||
*
|
||||
* @Return 文件id
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
u32 small_file_get_id_by_index(u8 small_file_type, int index);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过index获取小文件的大小
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
* @Params index 文件存储顺序序号
|
||||
*
|
||||
* @Return 文件大小
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
u32 small_file_get_size_by_index(u8 small_file_type, int index);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 通过id获取小文件的大小
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
* @Params index 文件存储顺序序号
|
||||
*
|
||||
* @Return 文件大小
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
u32 small_file_get_size_by_id(u8 small_file_type, int id);
|
||||
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 删除某一小文件类型下的文件
|
||||
*
|
||||
* @Params small_file_type 小文件类型
|
||||
*
|
||||
* @Return false or true
|
||||
*/
|
||||
/* ------------------------------------------------------------------------------------*/
|
||||
int small_file_del_by_file_type(u8 small_file_type);
|
||||
|
||||
#endif /*_WATCH_DATA_STORAGE_H*/
|
||||
|
||||
@@ -0,0 +1,913 @@
|
||||
#include "app_config.h"
|
||||
#include "asm/cpu.h"
|
||||
#include "device/device.h"
|
||||
#include "flashdb.h"
|
||||
#include "data_storage.h"
|
||||
#include "os/os_api.h"
|
||||
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[FDB-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
/* #define LOG_DUMP_ENABLE */
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".storage.data.bss")
|
||||
#pragma data_seg(".storage.data")
|
||||
#pragma const_seg(".storage.text.const")
|
||||
#pragma code_seg(".storage.text")
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE && (TCFG_DATA_STORAGE_FDB_ENABLE||TCFG_DATA_STORAGE_FDB_EXFLASH_ENABLE)
|
||||
|
||||
|
||||
static struct small_file_hd *hd;
|
||||
|
||||
|
||||
struct small_file_info {
|
||||
u8 type; /*文件类型*/
|
||||
u8 max_item; /*文件数量最大值*/
|
||||
u8 cur_item; /*当前文件数量*/
|
||||
u8 storage_sel; /*对应数据库*/
|
||||
u16 *size_table; /*每个文件对应的大小*/
|
||||
u32 *table; /*每个文件对应的id*/
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define SMALL_FILE_DB_NAME "small_file_kvdb"
|
||||
#define SMALL_FILE_PARTITION_NAME "small_file_partition"
|
||||
|
||||
|
||||
#define USED_KVDB_MAX 1
|
||||
#define USED_TSDB_MAX 1
|
||||
|
||||
#define USED_KVDB_0 0
|
||||
#define USED_KVDB_1 1
|
||||
#define USED_KVDB_2 2
|
||||
#define USED_KVDB_3 3
|
||||
|
||||
#define USED_TSDB_0 4
|
||||
#define USED_TSDB_1 5
|
||||
#define USED_TSDB_2 6
|
||||
#define USED_TSDB_3 7
|
||||
|
||||
#define NAME_FILE_LEN 13
|
||||
|
||||
struct small_file_hd {
|
||||
struct small_file_info *child;
|
||||
struct fdb_kvdb *kvdb[USED_KVDB_MAX];
|
||||
struct fdb_tsdb *tsdb[USED_TSDB_MAX];
|
||||
OS_MUTEX db_mutex;
|
||||
OS_MUTEX mutex;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
const struct small_file data_small_file_info[] = {
|
||||
{F_TYPE_PHONEBOOK, 1, USED_KVDB_0},
|
||||
{F_TYPE_MESSAGE, 10, USED_KVDB_0},
|
||||
{F_TYPE_SPORTRECORD, 1, USED_KVDB_0},
|
||||
{F_TYPE_HEART, 1, USED_KVDB_0},
|
||||
{F_TYPE_BLOOD_OXYGEN, 1, USED_KVDB_0},
|
||||
{F_TYPE_SLEEP, 1, USED_KVDB_0},
|
||||
{F_TYPE_WEATHER, 1, USED_KVDB_0},
|
||||
{F_TYPE_CALL_LOG, 10, USED_KVDB_0},
|
||||
{F_TYPE_STEP, 1, USED_KVDB_0},
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
extern int small_file_table_init(u8 small_file_type);
|
||||
|
||||
#define VM_MASK 0x55
|
||||
#define VM_MASK_BIT 40//24
|
||||
#define TYPE_BIT 32//16
|
||||
|
||||
|
||||
static u32 __is_name2mask(char *name)
|
||||
{
|
||||
char *end;
|
||||
char temp[9];
|
||||
snprintf(temp, NAME_FILE_LEN - sizeof(temp) + 1, "%s", name);
|
||||
long intValue = strtol(temp, &end, 16);
|
||||
if (*end == '\0') { // 确保整个字符串都被成功转换
|
||||
} else {
|
||||
/* return false; */
|
||||
}
|
||||
u8 mask = (intValue >> (VM_MASK_BIT - 32)) & 0xff;
|
||||
if (mask == VM_MASK) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
static u32 __type_name2id(char *name)
|
||||
{
|
||||
char *end;
|
||||
char temp[9];
|
||||
snprintf(temp, NAME_FILE_LEN - sizeof(temp) + 1, "%s", name);
|
||||
long intValue = strtol(temp, &end, 16);
|
||||
if (*end == '\0') { // 确保整个字符串都被成功转换
|
||||
/* printf("The integer value of %s is %lx\n", name, intValue); */
|
||||
} else {
|
||||
/* printf("Invalid hexadecimal string\n"); */
|
||||
/* return 0; */
|
||||
}
|
||||
u8 mask = (intValue >> (VM_MASK_BIT - 32)) & 0xff;
|
||||
if (mask == VM_MASK) {
|
||||
snprintf(temp, sizeof(temp), "%s", name + (NAME_FILE_LEN - sizeof(temp)));
|
||||
intValue = strtol(temp, &end, 16);
|
||||
return (intValue) & 0xffffffff;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static u32 __type_name2type(char *name)
|
||||
{
|
||||
char *end;
|
||||
char temp[9];
|
||||
snprintf(temp, NAME_FILE_LEN - sizeof(temp) + 1, "%s", name);
|
||||
long intValue = strtol(temp, &end, 16);
|
||||
if (*end == '\0') { // 确保整个字符串都被成功转换
|
||||
/* printf("The integer value of %s is %lx\n", name, intValue); */
|
||||
} else {
|
||||
/* printf("Invalid hexadecimal string\n"); */
|
||||
/* return 0; */
|
||||
}
|
||||
u8 mask = (intValue >> (VM_MASK_BIT - 32)) & 0xff;
|
||||
if (mask == VM_MASK) {
|
||||
return (intValue >> (TYPE_BIT - 32)) & 0xff;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *__type_id2name(char *name, u8 file_type, u32 id)
|
||||
{
|
||||
sprintf(name, "%02x%02x%08x", (int)VM_MASK, file_type, (u32)id);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static u32 __new_id_create(void *priv, int min, int max)
|
||||
{
|
||||
struct small_file_info *file = (struct small_file_info *)priv;
|
||||
u32 id = min;
|
||||
/*最小不重复id*/
|
||||
__again:
|
||||
for (int index = 0; index < file->cur_item; index++) {
|
||||
if (file->table[index] == id) {
|
||||
id ++;
|
||||
if (!id || id > max) {
|
||||
id = min;
|
||||
}
|
||||
goto __again;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
static u32 __is_new_id(struct small_file_info *info, u32 id)
|
||||
{
|
||||
if (!id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int index = 0; index < info->cur_item; index++) {
|
||||
if (info->table[index] == id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static u32 __find_id_index(struct small_file_info *info, u32 id)
|
||||
{
|
||||
for (int index = 0; index < info->cur_item; index++) {
|
||||
if (info->table[index] == id) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return info->cur_item;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static fdb_time_t get_time(void)
|
||||
{
|
||||
static int counts = 0;
|
||||
/* Using the counts instead of timestamp.
|
||||
* Please change this function to return RTC time.
|
||||
*/
|
||||
return ++counts;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void flashdb_lock(fdb_db_t db)
|
||||
{
|
||||
if (hd) {
|
||||
os_mutex_pend(&hd->db_mutex, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void flashdb_unlock(fdb_db_t db)
|
||||
{
|
||||
if (hd) {
|
||||
os_mutex_post(&hd->db_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int data_small_file_init(void)
|
||||
{
|
||||
|
||||
fdb_err_t result;
|
||||
|
||||
if (!hd) {
|
||||
hd = zalloc(sizeof(struct small_file_hd) \
|
||||
+ F_TYPE_COUNT * sizeof(struct small_file_info));
|
||||
}
|
||||
|
||||
hd->child = (struct small_file_info *)(hd + 1);
|
||||
|
||||
|
||||
os_mutex_create(&hd->mutex);
|
||||
os_mutex_create(&hd->db_mutex);
|
||||
|
||||
|
||||
int flag = 0;
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file *file = (struct small_file *)&data_small_file_info[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
flag |= BIT(file->storage_sel);
|
||||
hd->child[file->type].type = file->type;
|
||||
hd->child[file->type].max_item = file->max_item;
|
||||
hd->child[file->type].storage_sel = file->storage_sel;
|
||||
hd->child[file->type].table = (u32 *)zalloc(sizeof(u32) * file->max_item);
|
||||
hd->child[file->type].size_table = (u16 *)zalloc(sizeof(u16) * file->max_item);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//先支持单个
|
||||
if (flag & 0x0f) {
|
||||
hd->kvdb[0] = (struct fdb_kvdb *)zalloc(sizeof(struct fdb_kvdb) * USED_KVDB_MAX);
|
||||
result = fdb_kvdb_init(hd->kvdb[0], SMALL_FILE_DB_NAME, SMALL_FILE_PARTITION_NAME, NULL, NULL);
|
||||
|
||||
fdb_kvdb_control(hd->kvdb[0], FDB_KVDB_CTRL_SET_LOCK, flashdb_lock);
|
||||
fdb_kvdb_control(hd->kvdb[0], FDB_KVDB_CTRL_SET_UNLOCK, flashdb_unlock);
|
||||
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
log_error("kvdb init fail\n");
|
||||
goto __err;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (flag & 0xf0) {
|
||||
hd->tsdb[0] = (struct fdb_tsdb *)zalloc(sizeof(struct fdb_tsdb) * USED_TSDB_MAX);
|
||||
result = fdb_tsdb_init(hd->tsdb[0], SMALL_FILE_DB_NAME, SMALL_FILE_PARTITION_NAME, get_time, 128, NULL);
|
||||
fdb_tsdb_control(hd->tsdb[0], FDB_TSDB_CTRL_SET_LOCK, flashdb_lock);
|
||||
fdb_tsdb_control(hd->tsdb[0], FDB_TSDB_CTRL_SET_UNLOCK, flashdb_unlock);
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
log_error("tsdb init fail\n");
|
||||
goto __err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file *file = (struct small_file *)&data_small_file_info[i];
|
||||
small_file_table_init(file->type);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
__err:
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
u32 small_file_get_id_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
u32 id;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
if (!file->cur_item || index >= file->cur_item) {
|
||||
return 0;
|
||||
}
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
id = file->table[index];
|
||||
os_mutex_post(&hd->mutex);
|
||||
return id;
|
||||
|
||||
}
|
||||
|
||||
int small_file_info_delete_by_index(struct small_file_info *file, int index)
|
||||
{
|
||||
fdb_err_t result;
|
||||
|
||||
struct fdb_kvdb *kvdb;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
|
||||
if (!file->cur_item || index >= file->cur_item) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
u32 id = file->table[index];
|
||||
|
||||
for (; index < file->cur_item - 1; index++) {
|
||||
file->table[index] = file->table[index + 1];
|
||||
file->size_table[index] = file->size_table[index + 1];
|
||||
}
|
||||
|
||||
--file->cur_item;
|
||||
file->table[file->cur_item] = 0;
|
||||
file->size_table[file->cur_item] = 0;
|
||||
char name[NAME_FILE_LEN];
|
||||
|
||||
__type_id2name(name, file->type, id);
|
||||
|
||||
printf(">>>>>>>>>>>>>>>>> %s %s %d\n", __FUNCTION__, name, id);
|
||||
|
||||
result = fdb_kv_del(kvdb, name);
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
log_info("<%s> id:%d succ!", __func__, id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
int small_file_del_all(void)
|
||||
{
|
||||
int flag = 0;
|
||||
fdb_err_t result;
|
||||
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file *file = (struct small_file *)&data_small_file_info[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
flag |= BIT(file->storage_sel);
|
||||
}
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
//先支持单个
|
||||
if (flag & 0x0f) {
|
||||
result = fdb_kv_set_default(hd->kvdb[0]);
|
||||
if (result != FDB_NO_ERR) {
|
||||
log_error("kvdb deinit fail\n");
|
||||
goto __err;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file *file = (struct small_file *)&data_small_file_info[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
hd->child[file->type].cur_item = 0;
|
||||
memset(hd->child[file->type].table, 0, (sizeof(u32) * file->max_item));
|
||||
memset(hd->child[file->type].size_table, 0, (sizeof(u16) * file->max_item));
|
||||
}
|
||||
|
||||
log_info("<%s> succ!", __func__);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
log_error("<%s> del small_file error", __func__);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int small_file_delete_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
return small_file_info_delete_by_index(file, index);
|
||||
|
||||
}
|
||||
|
||||
|
||||
u32 small_file_write(u8 small_file_type, u32 *id, u32 buf_offset, void *buf, u32 len, u32 total_len)
|
||||
{
|
||||
fdb_err_t result;
|
||||
struct fdb_blob blob;
|
||||
struct small_file_info *file;
|
||||
struct fdb_kv kv;
|
||||
struct fdb_kvdb *kvdb;
|
||||
u8 is_new_id = 1;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
file = &hd->child[small_file_type];
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
|
||||
if (!*id) {
|
||||
if (buf_offset) {
|
||||
ASSERT(file->cur_item);
|
||||
*id = file->table[file->cur_item - 1];
|
||||
//临时特殊处理一下app 不发id号
|
||||
}
|
||||
}
|
||||
|
||||
is_new_id = __is_new_id(file, *id);
|
||||
|
||||
while (is_new_id && file->cur_item >= file->max_item) {
|
||||
small_file_info_delete_by_index(file, 0);
|
||||
}
|
||||
|
||||
if (!*id) {
|
||||
*id = __new_id_create(file, 0x1, 0xffff);
|
||||
}
|
||||
|
||||
char name[NAME_FILE_LEN];
|
||||
__type_id2name(name, file->type, *id);
|
||||
|
||||
__agin:
|
||||
|
||||
if (buf_offset) {
|
||||
if (fdb_kv_get_obj(kvdb, name, &kv)) {
|
||||
/* if (kv.value_len >= buf_offset) { */
|
||||
u8 *temp_data = zalloc(buf_offset + len);
|
||||
fdb_kv_get_blob(kvdb, name, fdb_blob_make(&blob, temp_data, kv.value_len));
|
||||
memcpy(&temp_data[buf_offset], buf, len);
|
||||
result = fdb_kv_set_blob(kvdb, name, fdb_blob_make(&blob, temp_data, buf_offset + len));
|
||||
free(temp_data);
|
||||
/* } else { */
|
||||
/* goto __err; */
|
||||
/* } */
|
||||
} else {
|
||||
goto __err;
|
||||
}
|
||||
} else {
|
||||
result = fdb_kv_set_blob(kvdb, name, fdb_blob_make(&blob, buf, len));
|
||||
}
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
small_file_delete_by_index(small_file_type, 0);
|
||||
goto __agin;
|
||||
}
|
||||
|
||||
if (is_new_id) {
|
||||
file->table[file->cur_item] = *id;
|
||||
file->size_table[file->cur_item] = len;
|
||||
file->cur_item++;
|
||||
} else {
|
||||
u32 cur_item = __find_id_index(file, *id);
|
||||
file->table[cur_item] = *id;
|
||||
file->size_table[cur_item] = buf_offset + len;
|
||||
if (cur_item == file->cur_item) {
|
||||
file->cur_item++;
|
||||
log_info("<%s> id:%d del rwrite", __func__, *id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
log_info("<%s> id:%d succ!", __func__, *id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return len;
|
||||
__err:
|
||||
os_mutex_post(&hd->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_update_by_id(u8 small_file_type, u32 id, u32 buf_offset, void *buf, u32 len, u32 total_len)
|
||||
{
|
||||
fdb_err_t result;
|
||||
struct fdb_blob blob;
|
||||
struct small_file_info *file;
|
||||
struct fdb_kv kv;
|
||||
struct fdb_kvdb *kvdb;
|
||||
u8 is_new_id = 1;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
file = &hd->child[small_file_type];
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
|
||||
if (!id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char name[NAME_FILE_LEN];
|
||||
__type_id2name(name, file->type, id);
|
||||
|
||||
__agin:
|
||||
|
||||
if (fdb_kv_get_obj(kvdb, name, &kv)) {
|
||||
if (kv.value_len < (buf_offset + len)) {
|
||||
goto __err;
|
||||
}
|
||||
u8 *temp_data = zalloc(kv.value_len);
|
||||
fdb_kv_get_blob(kvdb, name, fdb_blob_make(&blob, temp_data, kv.value_len));
|
||||
memcpy(&temp_data[buf_offset], buf, len);
|
||||
result = fdb_kv_set_blob(kvdb, name, fdb_blob_make(&blob, temp_data, kv.value_len));
|
||||
free(temp_data);
|
||||
} else {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
goto __agin;
|
||||
}
|
||||
|
||||
log_info("<%s> id:%d succ!", __func__, id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return len;
|
||||
__err:
|
||||
os_mutex_post(&hd->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int small_file_read(u8 small_file_type, u32 id, u32 offset, void *buf, u32 len)
|
||||
{
|
||||
int ret = len;
|
||||
struct fdb_blob blob;
|
||||
struct fdb_kvdb *kvdb;
|
||||
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
struct small_file_info *file = &hd->child[small_file_type];
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
|
||||
if (!file->cur_item) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
|
||||
u8 *temp_data = zalloc(offset + len);
|
||||
|
||||
char name[NAME_FILE_LEN];
|
||||
__type_id2name(name, file->type, id);
|
||||
/* printf("%s %d %s \n", __FUNCTION__, __LINE__, name); */
|
||||
fdb_kv_get_blob(kvdb, name, fdb_blob_make(&blob, temp_data, offset + len));
|
||||
if (blob.saved.len > offset) {
|
||||
if (blob.saved.len < offset + len) {
|
||||
ret = blob.saved.len - offset;
|
||||
}
|
||||
memcpy(buf, &temp_data[offset], ret);
|
||||
} else {
|
||||
ret = 0;
|
||||
}
|
||||
free(temp_data);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return ret;
|
||||
__err:
|
||||
os_mutex_post(&hd->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int small_file_delete_by_id(u8 small_file_type, u32 id)
|
||||
{
|
||||
fdb_err_t result;
|
||||
struct small_file_info *file;
|
||||
struct fdb_kvdb *kvdb;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
file = &hd->child[small_file_type];
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
if (!file->cur_item) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
|
||||
u32 index = __find_id_index(file, id);
|
||||
if (index == file->cur_item) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
for (; index < file->cur_item - 1; index++) {
|
||||
file->table[index] = file->table[index + 1];
|
||||
file->size_table[index] = file->size_table[index + 1];
|
||||
}
|
||||
|
||||
--file->cur_item;
|
||||
file->table[file->cur_item] = 0;
|
||||
file->size_table[file->cur_item] = 0;
|
||||
|
||||
char name[NAME_FILE_LEN];
|
||||
|
||||
|
||||
__type_id2name(name, file->type, id);
|
||||
|
||||
printf(">>>>>>>>>>>>>>>>> %s %s\n", __FUNCTION__, name);
|
||||
|
||||
result = fdb_kv_del(kvdb, name);
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
log_info("<%s> id:%d succ!", __func__, id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int small_file_get_id_table_len(u8 small_file_type)
|
||||
{
|
||||
return small_file_get_id_table(small_file_type, NULL, 0);
|
||||
}
|
||||
|
||||
|
||||
int small_file_table_init(u8 small_file_type)
|
||||
{
|
||||
int index = 0;
|
||||
struct small_file_info *file;
|
||||
struct fdb_kvdb *kvdb;
|
||||
struct fdb_kv_iterator iterator;
|
||||
fdb_kv_t cur_kv;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
flashdb_lock(NULL);
|
||||
fdb_kv_iterator_init(&iterator);
|
||||
while (fdb_kv_iterate(kvdb, &iterator)) {
|
||||
cur_kv = &(iterator.curr_kv);
|
||||
|
||||
if (!__is_name2mask(cur_kv->name)) {
|
||||
printf("del name %s \n", cur_kv->name);
|
||||
fdb_kv_del(kvdb, cur_kv->name);
|
||||
}
|
||||
|
||||
if (__type_name2type(cur_kv->name) == file->type) {
|
||||
u32 id = __type_name2id(cur_kv->name);
|
||||
printf("name %s %d \n", cur_kv->name, id);
|
||||
if (index < file->max_item) {
|
||||
file->size_table[file->cur_item] = cur_kv->value_len;
|
||||
file->table[file->cur_item] = id;
|
||||
file->cur_item++;
|
||||
index++;
|
||||
} else {
|
||||
printf("file over del name %s \n", cur_kv->name);
|
||||
fdb_kv_del(kvdb, cur_kv->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flashdb_unlock(NULL);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return index ;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int small_file_get_count(u8 small_file_type)
|
||||
{
|
||||
int count;
|
||||
struct small_file_info *file;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
count = file->cur_item;
|
||||
os_mutex_post(&hd->mutex);
|
||||
return count;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int small_file_get_id_table(u8 small_file_type, u8 *table_data, u16 data_len)
|
||||
{
|
||||
|
||||
int count;
|
||||
struct small_file_info *file;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
u16 id;
|
||||
u16 file_len;
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
count = file->cur_item;
|
||||
for (int i = 0; (i < count) && (i * 4 < data_len); i++) {
|
||||
if (table_data) {
|
||||
id = (u16)file->table [i];
|
||||
file_len = (u16)file->size_table [i];
|
||||
memcpy(table_data, &id, sizeof(u16));
|
||||
table_data += 2;
|
||||
memcpy(table_data, &file_len, sizeof(u16));
|
||||
table_data += 2;
|
||||
}
|
||||
}
|
||||
|
||||
os_mutex_post(&hd->mutex);
|
||||
return count * 4;
|
||||
|
||||
}
|
||||
|
||||
|
||||
u32 small_file_get_size_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
u32 size;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
if (!file->cur_item || index >= file->cur_item) {
|
||||
return 0;
|
||||
}
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
size = file->size_table[index];
|
||||
os_mutex_post(&hd->mutex);
|
||||
return size;
|
||||
|
||||
}
|
||||
u32 small_file_get_size_by_id(u8 small_file_type, int id)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
u32 size = 0;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
for (int index = 0; index < file->cur_item; index++) {
|
||||
if (file->table[index] == id) {
|
||||
size = file->size_table[index];
|
||||
}
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return size;
|
||||
|
||||
}
|
||||
|
||||
int small_file_del_by_file_type(u8 small_file_type)
|
||||
{
|
||||
fdb_err_t result;
|
||||
struct small_file_info *file;
|
||||
u32 id;
|
||||
char name[NAME_FILE_LEN];
|
||||
struct fdb_kvdb *kvdb;
|
||||
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
file = &hd->child[small_file_type];
|
||||
kvdb = hd->kvdb[file->storage_sel % USED_KVDB_MAX];
|
||||
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
if (!file->cur_item) {
|
||||
log_error("<%s> file->cur_item is 0", __func__);
|
||||
goto __err;
|
||||
}
|
||||
|
||||
while (file->cur_item) {
|
||||
id = file->table[file->cur_item - 1];
|
||||
__type_id2name(name, file->type, id);
|
||||
|
||||
result = fdb_kv_del(kvdb, name);
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
file->table[file->cur_item - 1] = 0;
|
||||
file->size_table[file->cur_item - 1] = 0;
|
||||
--file->cur_item;
|
||||
}
|
||||
log_info("<%s> succ!", __func__);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
log_error("<%s> del small_file error", __func__);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
/************************************************
|
||||
* 注册flashDB的分区
|
||||
***********************************************/
|
||||
|
||||
REGISTER_FLASHDB_PARTITION(phonebook_partition) = {
|
||||
.name = SMALL_FILE_PARTITION_NAME,
|
||||
.dev_name = TCFG_DATA_DEV_NAME,
|
||||
.offset = 0,
|
||||
.len = TCFG_DATA_FLASH_DEV_PATY_SIZE,
|
||||
};
|
||||
|
||||
#endif /* if TCFG_WATCH_DATA_STORAGE_ENABLE */
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#include "app_config.h"
|
||||
#include "asm/cpu.h"
|
||||
#include "device/device.h"
|
||||
#include "flashdb.h"
|
||||
#include "data_storage.h"
|
||||
#include "os/os_api.h"
|
||||
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[NULL-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
/* #define LOG_DUMP_ENABLE */
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".storage.data.bss")
|
||||
#pragma data_seg(".storage.data")
|
||||
#pragma const_seg(".storage.text.const")
|
||||
#pragma code_seg(".storage.text")
|
||||
#endif
|
||||
|
||||
#if !TCFG_DATA_STORAGE_ENABLE
|
||||
|
||||
|
||||
int small_file_get_id_table_len(u8 small_file_type)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_get_id_table(u8 small_file_type, u8 *table_data, u16 data_len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_delete_by_id(u8 small_file_type, u32 id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_del_all(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int small_file_read(u8 small_file_type, u32 id, u32 offset, void *buf, u32 len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
u32 small_file_write(u8 small_file_type, u32 *id, u32 buf_offset, void *buf, u32 len, u32 total_len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_update_by_id(u8 small_file_type, u32 id, u32 buf_offset, void *buf, u32 len, u32 total_len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 small_file_get_size_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 small_file_get_size_by_id(u8 small_file_type, int id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 small_file_get_id_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_get_count(u8 small_file_type)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int data_small_file_init(void)
|
||||
{
|
||||
return false;;
|
||||
}
|
||||
|
||||
|
||||
#endif /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
@@ -0,0 +1,876 @@
|
||||
#include "app_config.h"
|
||||
#include "asm/cpu.h"
|
||||
#include "device/device.h"
|
||||
#include "flashdb.h"
|
||||
#include "data_storage.h"
|
||||
#include "os/os_api.h"
|
||||
#include "syscfg_id.h"
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[VM-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
/* #define LOG_DUMP_ENABLE */
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".storage.data.bss")
|
||||
#pragma data_seg(".storage.data")
|
||||
#pragma const_seg(".storage.text.const")
|
||||
#pragma code_seg(".storage.text")
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE && TCFG_DATA_STORAGE_VM_ENABLE
|
||||
|
||||
//================================================//
|
||||
// 说明
|
||||
//目前使用vm id数量限制在SMALL_FILE_VM_ITEM_MAX
|
||||
//
|
||||
//每条数据存储格式为 |id|len|用户数据|
|
||||
//
|
||||
//================================================//
|
||||
|
||||
|
||||
|
||||
#define SMALL_FILE_VM_ITEM_MAX (VM_SMALL_FILE_END - VM_SMALL_FILE_START + 1)
|
||||
|
||||
struct small_file_vm {
|
||||
u8 type; /*文件类型*/
|
||||
u8 max_item; /*文件数量最大值*/
|
||||
u8 vm_max_item; /*使用vm id数量最大值*/
|
||||
u16 vm_id_table[SMALL_FILE_VM_ITEM_MAX]; /*对应vm id*/
|
||||
};
|
||||
|
||||
struct small_file_vm_storage_info {
|
||||
u32 id;
|
||||
u32 len;
|
||||
};
|
||||
|
||||
struct small_file_info {
|
||||
u8 type; /*文件类型*/
|
||||
u8 max_item; /*文件数量最大值*/
|
||||
u8 cur_item; /*当前文件数量*/
|
||||
u16 *size_table; /*每个文件对应的大小*/
|
||||
u32 *table; /*每个文件对应的id*/
|
||||
u8 vm_max_item; /*使用vm id数量最大值*/
|
||||
u16 *vm_id_table; /*文件用于存储的id号*/
|
||||
};
|
||||
|
||||
struct small_file_hd {
|
||||
struct small_file_info *child;
|
||||
OS_MUTEX mutex;
|
||||
};
|
||||
|
||||
/*当前max_item未使用*/
|
||||
const struct small_file_vm data_small_file_info[] = {
|
||||
{F_TYPE_PHONEBOOK, 10, 1, {VM_SMALL_FILE_PHONEBOOK}},
|
||||
{F_TYPE_CALL_LOG, 10, 1, {VM_SMALL_FILE_CALL_LOG}},
|
||||
{F_TYPE_WEATHER, 1, 1, {VM_SMALL_FILE_WEATHER}},
|
||||
{F_TYPE_HEART, 1, 1, {VM_SMALL_FILE_HEART}},
|
||||
{
|
||||
F_TYPE_MESSAGE, 5, 5, {
|
||||
VM_SMALL_FILE_MESSAGE_0, VM_SMALL_FILE_MESSAGE_1, \
|
||||
VM_SMALL_FILE_MESSAGE_2, VM_SMALL_FILE_MESSAGE_3, VM_SMALL_FILE_MESSAGE_4
|
||||
}
|
||||
},
|
||||
{F_TYPE_SPORTRECORD, 2, 2, {VM_SMALL_FILE_SPORTRECORD_0, VM_SMALL_FILE_SPORTRECORD_1}},
|
||||
{
|
||||
F_TYPE_BLOOD_OXYGEN, 4, 4, {
|
||||
VM_SMALL_FILE_BLOOD_OXYGEN_0, VM_SMALL_FILE_BLOOD_OXYGEN_1, \
|
||||
VM_SMALL_FILE_BLOOD_OXYGEN_2, VM_SMALL_FILE_BLOOD_OXYGEN_3
|
||||
}
|
||||
},
|
||||
{
|
||||
F_TYPE_SLEEP, 4, 4, {
|
||||
VM_SMALL_FILE_SLEEP_0, VM_SMALL_FILE_SLEEP_1, \
|
||||
VM_SMALL_FILE_SLEEP_2, VM_SMALL_FILE_SLEEP_3
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
static int small_file_info_delete_by_index(struct small_file_info *file, int index);
|
||||
static int small_file_table_init(u8 small_file_type);
|
||||
static u32 __is_new_id(struct small_file_info *info, u32 id);
|
||||
static u32 __new_id_create(void *priv, int min, int max);
|
||||
static u32 __find_id_index(struct small_file_info *info, u32 id);
|
||||
static int __get_vm_saved_info(u16 vm_id, struct small_file_vm_storage_info *storage_info);
|
||||
static int __read_vm_saved_data(u16 vm_id, u8 *buf, u32 len);
|
||||
static int __write_vm_saved_data(u16 vm_id, u32 stored_id, u8 *buf, u32 len);
|
||||
static int __del_vm_saved_item(u16 vm_id);
|
||||
static int __data_small_file_info_init(void);
|
||||
static int __data_small_file_info_update(void);
|
||||
|
||||
static struct small_file_hd *hd;
|
||||
|
||||
int data_small_file_init(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!hd) {
|
||||
hd = zalloc(sizeof(struct small_file_hd) \
|
||||
+ F_TYPE_COUNT * sizeof(struct small_file_info));
|
||||
}
|
||||
|
||||
if (!hd) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
hd->child = (struct small_file_info *)(hd + 1);
|
||||
|
||||
os_mutex_create(&hd->mutex);
|
||||
|
||||
__data_small_file_info_init();
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file_vm *file = (struct small_file_vm *)&data_small_file_info[i];
|
||||
small_file_table_init(file->type);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
__err:
|
||||
return false;
|
||||
}
|
||||
|
||||
int small_file_get_id_table_len(u8 small_file_type)
|
||||
{
|
||||
return small_file_get_id_table(small_file_type, NULL, 0);
|
||||
}
|
||||
|
||||
int small_file_get_id_table(u8 small_file_type, u8 *table_data, u16 data_len)
|
||||
{
|
||||
int count;
|
||||
struct small_file_info *file;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
u16 id;
|
||||
u16 file_len;
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
count = file->cur_item;
|
||||
for (int i = 0; (i < count) && (i * 4 < data_len); i++) {
|
||||
if (table_data) {
|
||||
id = (u16)file->table[i];
|
||||
file_len = (u16)file->size_table[i];
|
||||
memcpy(table_data, &id, sizeof(u16));
|
||||
table_data += 2;
|
||||
memcpy(table_data, &file_len, sizeof(u16));
|
||||
table_data += 2;
|
||||
}
|
||||
}
|
||||
|
||||
os_mutex_post(&hd->mutex);
|
||||
return count * 4;
|
||||
}
|
||||
|
||||
int small_file_delete_by_id(u8 small_file_type, u32 id)
|
||||
{
|
||||
int ret;
|
||||
u16 vm_id = 0;
|
||||
u32 index;
|
||||
struct small_file_info *file;
|
||||
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
file = &hd->child[small_file_type];
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
if (!file->cur_item) {
|
||||
goto __err;
|
||||
}
|
||||
if (id == 0) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
index = __find_id_index(file, id);
|
||||
if (index >= file->cur_item) {
|
||||
log_error("<%s> index:%d id:%d", __func__, index, id);
|
||||
goto __err;
|
||||
}
|
||||
/*检查找到index的对应id是否正确*/
|
||||
if (file->table[index] != id) {
|
||||
log_error("<%s> index:%d id:%d find_id:%d", __func__, index, id, file->table[index]);
|
||||
goto __err;
|
||||
}
|
||||
|
||||
vm_id = file->vm_id_table[index];
|
||||
|
||||
ret = __del_vm_saved_item(vm_id);
|
||||
if (ret == false) {
|
||||
log_error("<%s> line:%d vm write err", __func__, __LINE__);
|
||||
goto __err;
|
||||
}
|
||||
|
||||
for (; index < file->cur_item - 1; index++) {
|
||||
file->table[index] = file->table[index + 1];
|
||||
file->size_table[index] = file->size_table[index + 1];
|
||||
file->vm_id_table[index] = file->vm_id_table[index + 1];
|
||||
}
|
||||
|
||||
--file->cur_item;
|
||||
file->table[file->cur_item] = 0;
|
||||
file->size_table[file->cur_item] = 0;
|
||||
file->vm_id_table[file->cur_item] = vm_id;
|
||||
|
||||
log_info("<%s> file_type:%d id:%d succ!", __func__, file->type, id);
|
||||
|
||||
ret = __data_small_file_info_update();
|
||||
if (ret == false) {
|
||||
log_error("<%s> __data_small_file_info_update error", __func__);
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
log_error("<%s> file_type:%d id:%d error!", __func__, file->type, id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
int small_file_delete_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
return small_file_info_delete_by_index(file, index);
|
||||
|
||||
}
|
||||
|
||||
int small_file_del_all(void)
|
||||
{
|
||||
int ret;
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file_vm *file = (struct small_file_vm *)&data_small_file_info[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = 0; j < hd->child[file->type].cur_item; j++) {
|
||||
ret = __del_vm_saved_item(hd->child[file->type].vm_id_table[j]);
|
||||
if (ret == false) {
|
||||
log_error("<%s> line:%d vm del err", __func__, __LINE__);
|
||||
goto __err;
|
||||
}
|
||||
hd->child[file->type].table[j] = 0;
|
||||
hd->child[file->type].size_table[j] = 0;
|
||||
}
|
||||
hd->child[file->type].cur_item = 0;
|
||||
}
|
||||
|
||||
log_info("<%s> succ!", __func__);
|
||||
ret = __data_small_file_info_update();
|
||||
if (ret == false) {
|
||||
log_error("<%s> __data_small_file_info_update error", __func__);
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
log_error("<%s> del small_file error", __func__);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
int small_file_read(u8 small_file_type, u32 id, u32 offset, void *buf, u32 len)
|
||||
{
|
||||
int read_len = len;
|
||||
u32 vm_id = 0;
|
||||
int saved_len = 0;
|
||||
u32 cur_item = 0;
|
||||
struct small_file_info *file;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
file = &hd->child[small_file_type];
|
||||
if (!file->cur_item) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
cur_item = __find_id_index(file, id);
|
||||
if (cur_item >= file->cur_item) {
|
||||
log_error("<%s> id:%d error", __func__, id);
|
||||
goto __err;
|
||||
}
|
||||
vm_id = file->vm_id_table[cur_item];
|
||||
|
||||
u8 *temp_data = zalloc(offset + len);
|
||||
if (!temp_data) {
|
||||
log_error("%s temp_data is null", __func__);
|
||||
goto __err;
|
||||
}
|
||||
saved_len = __read_vm_saved_data(vm_id, temp_data, offset + len);
|
||||
if (saved_len > offset) {
|
||||
if (saved_len < offset + len) {
|
||||
read_len = saved_len - offset;
|
||||
}
|
||||
memcpy(buf, &temp_data[offset], read_len);
|
||||
} else {
|
||||
read_len = 0;
|
||||
}
|
||||
|
||||
free(temp_data);
|
||||
log_info("<%s> file_type:%d id:%d read succ!", __func__, file->type, id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return read_len;
|
||||
|
||||
__err:
|
||||
log_error("<%s> file_type:%d id:%d read error!", __func__, file->type, id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
u32 small_file_write(u8 small_file_type, u32 *id, u32 buf_offset, void *buf, u32 len, u32 total_len)
|
||||
{
|
||||
int ret;
|
||||
u16 vm_id;
|
||||
u8 *temp_data = NULL;
|
||||
u8 is_new_id = 1;
|
||||
u32 cur_item = 0;
|
||||
struct small_file_info *file;
|
||||
struct small_file_vm_storage_info stored_data_info;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
if (!file->max_item) {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
if (!*id) {
|
||||
if (buf_offset) {
|
||||
ASSERT(file->cur_item);
|
||||
*id = file->table[file->cur_item - 1];
|
||||
//临时特殊处理一下app 不发id号
|
||||
}
|
||||
}
|
||||
is_new_id = __is_new_id(file, *id);
|
||||
|
||||
while (is_new_id && file->cur_item >= file->max_item) {
|
||||
small_file_info_delete_by_index(file, 0);
|
||||
}
|
||||
|
||||
if (!*id) {
|
||||
*id = __new_id_create(file, 0x1, 0xffff);
|
||||
}
|
||||
cur_item = __find_id_index(file, *id);
|
||||
if (cur_item >= file->cur_item) {
|
||||
log_error("<%s> id:%d error", __func__, *id);
|
||||
goto __err;
|
||||
}
|
||||
vm_id = file->vm_id_table[cur_item];
|
||||
|
||||
|
||||
if (buf_offset) {
|
||||
if (__get_vm_saved_info(vm_id, &stored_data_info) == true) {
|
||||
temp_data = zalloc(buf_offset + len);
|
||||
if (!temp_data) {
|
||||
log_error("%s temp_data is null", __func__);
|
||||
goto __err;
|
||||
}
|
||||
ret = __read_vm_saved_data(vm_id, temp_data, stored_data_info.len);
|
||||
if (ret != stored_data_info.len) {
|
||||
goto __err;
|
||||
}
|
||||
memcpy(&temp_data[buf_offset], buf, len);
|
||||
ret = __write_vm_saved_data(vm_id, stored_data_info.id, temp_data, buf_offset + len);
|
||||
if (ret != (buf_offset + len)) {
|
||||
log_error("<%s> line:%d vm write err", __func__, __LINE__);
|
||||
goto __err;
|
||||
}
|
||||
} else {
|
||||
goto __err;
|
||||
}
|
||||
} else {
|
||||
ret = __write_vm_saved_data(vm_id, *id, buf, len);
|
||||
if (ret != len) {
|
||||
goto __err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (is_new_id) {
|
||||
file->table[file->cur_item] = *id;
|
||||
file->size_table[file->cur_item] = len;
|
||||
file->cur_item++;
|
||||
} else {
|
||||
cur_item = __find_id_index(file, *id);
|
||||
file->table[cur_item] = *id;
|
||||
file->size_table[cur_item] = buf_offset + len;
|
||||
if (cur_item == file->cur_item) {
|
||||
file->cur_item++;
|
||||
log_info("<%s> id:%d del rwrite", __func__, *id);
|
||||
}
|
||||
}
|
||||
|
||||
if (temp_data) {
|
||||
free(temp_data);
|
||||
}
|
||||
|
||||
|
||||
log_info("<%s> file_type:%d id:%d succ!", __func__, file->type, *id);
|
||||
|
||||
ret = __data_small_file_info_update();
|
||||
if (ret == false) {
|
||||
log_error("<%s> __data_small_file_info_update error", __func__);
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return len;
|
||||
|
||||
__err:
|
||||
if (temp_data) {
|
||||
free(temp_data);
|
||||
}
|
||||
log_info("<%s> file_type:%d id:%d error!", __func__, file->type, *id);
|
||||
os_mutex_post(&hd->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int small_file_update_by_id(u8 small_file_type, u32 id, u32 buf_offset, void *buf, u32 len, u32 total_len)
|
||||
{
|
||||
int ret;
|
||||
u16 vm_id;
|
||||
u32 cur_item = 0;
|
||||
u8 *temp_data = NULL;
|
||||
struct small_file_info *file;
|
||||
struct small_file_vm_storage_info stored_data_info;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
|
||||
cur_item = __find_id_index(file, id);
|
||||
if (cur_item >= file->cur_item) {
|
||||
log_error("<%s> id:%d error", __func__, id);
|
||||
goto __err;
|
||||
}
|
||||
vm_id = file->vm_id_table[cur_item];
|
||||
|
||||
if (__get_vm_saved_info(vm_id, &stored_data_info) == true) {
|
||||
if (stored_data_info.len < (buf_offset + len)) {
|
||||
goto __err;
|
||||
}
|
||||
temp_data = zalloc(stored_data_info.len);
|
||||
if (!temp_data) {
|
||||
log_error("%s temp_data is null", __func__);
|
||||
goto __err;
|
||||
}
|
||||
ret = __read_vm_saved_data(vm_id, temp_data, stored_data_info.len);
|
||||
if (ret != stored_data_info.len) {
|
||||
goto __err;
|
||||
}
|
||||
memcpy(&temp_data[buf_offset], buf, len);
|
||||
ret = __write_vm_saved_data(vm_id, stored_data_info.id, temp_data, stored_data_info.len);
|
||||
if (ret != stored_data_info.len) {
|
||||
log_error("<%s> line:%d vm write err", __func__, __LINE__);
|
||||
goto __err;
|
||||
}
|
||||
} else {
|
||||
goto __err;
|
||||
}
|
||||
|
||||
free(temp_data);
|
||||
log_info("<%s> file_type:%d id:%d succ!", __func__, file->type, id);
|
||||
ret = __data_small_file_info_update();
|
||||
if (ret == false) {
|
||||
log_error("<%s> __data_small_file_info_update error", __func__);
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return len;
|
||||
|
||||
__err:
|
||||
if (temp_data) {
|
||||
free(temp_data);
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 small_file_get_size_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
u32 size;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
if (!file->cur_item || index >= file->cur_item) {
|
||||
return 0;
|
||||
}
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
size = file->size_table[index];
|
||||
os_mutex_post(&hd->mutex);
|
||||
return size;
|
||||
}
|
||||
|
||||
u32 small_file_get_size_by_id(u8 small_file_type, int id)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
u32 size = 0;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
for (int index = 0; index < file->cur_item; index++) {
|
||||
if (file->table[index] == id) {
|
||||
size = file->size_table[index];
|
||||
}
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return size;
|
||||
}
|
||||
|
||||
u32 small_file_get_id_by_index(u8 small_file_type, int index)
|
||||
{
|
||||
struct small_file_info *file;
|
||||
u32 id;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
file = &hd->child[small_file_type];
|
||||
if (!file->cur_item || index >= file->cur_item) {
|
||||
return 0;
|
||||
}
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
id = file->table[index];
|
||||
os_mutex_post(&hd->mutex);
|
||||
return id;
|
||||
}
|
||||
|
||||
int small_file_get_count(u8 small_file_type)
|
||||
{
|
||||
int count;
|
||||
struct small_file_info *file;
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
count = file->cur_item;
|
||||
os_mutex_post(&hd->mutex);
|
||||
return count;
|
||||
}
|
||||
|
||||
static int small_file_info_delete_by_index(struct small_file_info *file, int index)
|
||||
{
|
||||
u16 vm_id = 0;
|
||||
int ret;
|
||||
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!file->cur_item || index >= file->cur_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
|
||||
vm_id = file->vm_id_table[index];
|
||||
|
||||
for (; index < file->cur_item - 1; index++) {
|
||||
file->table[index] = file->table[index + 1];
|
||||
file->size_table[index] = file->size_table[index + 1];
|
||||
file->vm_id_table[index] = file->vm_id_table[index + 1];
|
||||
}
|
||||
|
||||
--file->cur_item;
|
||||
file->table[file->cur_item] = 0;
|
||||
file->size_table[file->cur_item] = 0;
|
||||
file->vm_id_table[file->cur_item] = vm_id;
|
||||
|
||||
ret = __del_vm_saved_item(vm_id);
|
||||
if (ret == false) {
|
||||
log_error("<%s> line:%d vm write err", __func__, __LINE__);
|
||||
goto __err;
|
||||
}
|
||||
|
||||
log_info("<%s> file_type:%d index:%d succ!", __func__, file->type, index);
|
||||
|
||||
ret = __data_small_file_info_update();
|
||||
if (ret == false) {
|
||||
log_error("<%s> __data_small_file_info_update error", __func__);
|
||||
}
|
||||
os_mutex_post(&hd->mutex);
|
||||
return true;
|
||||
|
||||
__err:
|
||||
os_mutex_post(&hd->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
static int small_file_table_init(u8 small_file_type)
|
||||
{
|
||||
int ret;
|
||||
int count = 0;
|
||||
u32 id;
|
||||
struct small_file_info *file;
|
||||
struct small_file_vm_storage_info file_storage_info;
|
||||
|
||||
if (!hd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT(small_file_type < F_TYPE_COUNT);
|
||||
os_mutex_pend(&hd->mutex, 0);
|
||||
file = &hd->child[small_file_type];
|
||||
|
||||
for (int i = 0; i < file->vm_max_item; i++) {
|
||||
ret = __get_vm_saved_info(file->vm_id_table[i], &file_storage_info);
|
||||
if (ret == false) {
|
||||
continue;
|
||||
}
|
||||
file->size_table[file->cur_item] = file_storage_info.len;
|
||||
file->table[file->cur_item] = file_storage_info.id;
|
||||
file->cur_item++;
|
||||
++count;
|
||||
}
|
||||
|
||||
os_mutex_post(&hd->mutex);
|
||||
return count;
|
||||
}
|
||||
|
||||
static u32 __is_new_id(struct small_file_info *info, u32 id)
|
||||
{
|
||||
if (!id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int index = 0; index < info->cur_item; index++) {
|
||||
if (info->table[index] == id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static u32 __new_id_create(void *priv, int min, int max)
|
||||
{
|
||||
struct small_file_info *file = (struct small_file_info *)priv;
|
||||
u32 id = min;
|
||||
/*最小不重复id*/
|
||||
__again:
|
||||
for (int index = 0; index < file->cur_item; index++) {
|
||||
if (file->table[index] == id) {
|
||||
id ++;
|
||||
if (!id || id > max) {
|
||||
id = min;
|
||||
}
|
||||
goto __again;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
static u32 __find_id_index(struct small_file_info *info, u32 id)
|
||||
{
|
||||
for (int index = 0; index < info->cur_item; index++) {
|
||||
if (info->table[index] == id) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return info->cur_item;
|
||||
}
|
||||
|
||||
static int __get_vm_saved_info(u16 vm_id, struct small_file_vm_storage_info *storage_info)
|
||||
{
|
||||
int ret;
|
||||
ret = syscfg_read(vm_id, storage_info, sizeof(struct small_file_vm_storage_info));
|
||||
if (ret != sizeof(struct small_file_vm_storage_info)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int __read_vm_saved_data(u16 vm_id, u8 *buf, u32 len)
|
||||
{
|
||||
int saved_len = 0;
|
||||
u32 read_len = sizeof(struct small_file_vm_storage_info) + len;
|
||||
u8 *read_buf = zalloc(read_len);
|
||||
if (!read_buf) {
|
||||
log_error("%s read_buf is null", __func__);
|
||||
return 0;
|
||||
}
|
||||
saved_len = syscfg_read(vm_id, read_buf, read_len);
|
||||
if (saved_len <= sizeof(struct small_file_vm_storage_info)) {
|
||||
free(read_buf);
|
||||
return 0;
|
||||
}
|
||||
saved_len -= sizeof(struct small_file_vm_storage_info);
|
||||
memcpy(buf, &read_buf[sizeof(struct small_file_vm_storage_info)], saved_len);
|
||||
free(read_buf);
|
||||
return saved_len;
|
||||
}
|
||||
|
||||
static int __write_vm_saved_data(u16 vm_id, u32 stored_id, u8 *buf, u32 len)
|
||||
{
|
||||
int ret;
|
||||
struct small_file_vm_storage_info *storage_info;
|
||||
u32 saved_len = sizeof(struct small_file_vm_storage_info) + len;
|
||||
u8 *saved_buf = zalloc(saved_len);
|
||||
if (!saved_buf) {
|
||||
log_error("%s saved_buf is null", __func__);
|
||||
return 0;
|
||||
}
|
||||
storage_info = (struct small_file_vm_storage_info *)saved_buf;
|
||||
storage_info->id = stored_id;
|
||||
storage_info->len = len;
|
||||
memcpy(&saved_buf[sizeof(struct small_file_vm_storage_info)], buf, len);
|
||||
|
||||
ret = syscfg_write(vm_id, saved_buf, saved_len);
|
||||
if (ret != saved_len) {
|
||||
free(saved_buf);
|
||||
return 0;
|
||||
}
|
||||
free(saved_buf);
|
||||
return len;
|
||||
}
|
||||
|
||||
static int __del_vm_saved_item(u16 vm_id)
|
||||
{
|
||||
int ret;
|
||||
u8 data = 0;
|
||||
ret = syscfg_write(vm_id, &data, sizeof(data));
|
||||
if (ret != sizeof(data)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int __data_small_file_info_init(void)
|
||||
{
|
||||
int ret;
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct small_file_vm *temp_data = (struct small_file_vm *)zalloc(sizeof(data_small_file_info));
|
||||
if (!temp_data) {
|
||||
log_error("%s temp_data is null", __func__);
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = syscfg_read(VM_SMALL_FILE_MANAGER, temp_data, sizeof(data_small_file_info));
|
||||
if (ret == sizeof(data_small_file_info)) {
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file_vm *file = (struct small_file_vm *)&temp_data[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
hd->child[file->type].type = file->type;
|
||||
hd->child[file->type].max_item = file->max_item;
|
||||
hd->child[file->type].vm_max_item = file->vm_max_item;
|
||||
hd->child[file->type].table = (u32 *)zalloc(sizeof(u32) * file->vm_max_item);
|
||||
hd->child[file->type].size_table = (u16 *)zalloc(sizeof(u16) * file->vm_max_item);
|
||||
hd->child[file->type].vm_id_table = (u16 *)zalloc(sizeof(u16) * file->vm_max_item);
|
||||
ASSERT(hd->child[file->type].table && hd->child[file->type].size_table && hd->child[file->type].vm_id_table);
|
||||
memcpy((char *)hd->child[file->type].vm_id_table, (const char *)file->vm_id_table, sizeof(u16) * file->vm_max_item);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file_vm *file = (struct small_file_vm *)&data_small_file_info[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
hd->child[file->type].type = file->type;
|
||||
hd->child[file->type].max_item = file->max_item;
|
||||
hd->child[file->type].vm_max_item = file->vm_max_item;
|
||||
hd->child[file->type].table = (u32 *)zalloc(sizeof(u32) * file->vm_max_item);
|
||||
hd->child[file->type].size_table = (u16 *)zalloc(sizeof(u16) * file->vm_max_item);
|
||||
hd->child[file->type].vm_id_table = (u16 *)zalloc(sizeof(u16) * file->vm_max_item);
|
||||
ASSERT(hd->child[file->type].table && hd->child[file->type].size_table && hd->child[file->type].vm_id_table);
|
||||
memcpy((char *)hd->child[file->type].vm_id_table, (const char *)file->vm_id_table, sizeof(u16) * file->vm_max_item);
|
||||
}
|
||||
}
|
||||
|
||||
free(temp_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
static int __data_small_file_info_update(void)
|
||||
{
|
||||
int ret;
|
||||
if (!hd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct small_file_vm *temp_file;
|
||||
struct small_file_vm *temp_data = (struct small_file_vm *)zalloc(sizeof(data_small_file_info));
|
||||
if (!temp_data) {
|
||||
log_error("%s temp_data is null", __func__);
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < ARRAY_SIZE(data_small_file_info); i++) {
|
||||
struct small_file_vm *file = (struct small_file_vm *)&data_small_file_info[i];
|
||||
temp_file = &temp_data[i];
|
||||
|
||||
if (!file->type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
temp_file->type = hd->child[file->type].type;
|
||||
temp_file->max_item = hd->child[file->type].max_item;
|
||||
temp_file->vm_max_item = hd->child[file->type].vm_max_item;
|
||||
memcpy((char *)temp_file->vm_id_table, (const char *)hd->child[file->type].vm_id_table, sizeof(u16) * hd->child[file->type].vm_max_item);
|
||||
}
|
||||
|
||||
ret = syscfg_write(VM_SMALL_FILE_MANAGER, temp_data, sizeof(data_small_file_info));
|
||||
free(temp_data);
|
||||
if (ret != sizeof(data_small_file_info)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @file data_weater_storage.c
|
||||
* @brief 电话本存储相关
|
||||
*/
|
||||
#include "app_config.h"
|
||||
#include "flashdb.h"
|
||||
#include "fdb_low_lvl.h"
|
||||
#include "data_storage.h"
|
||||
#include "data_weather_storage.h"
|
||||
|
||||
#define LOG_TAG_CONST DATA_STORAGE
|
||||
#define LOG_TAG "[WEATER-DATA]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
|
||||
#ifdef SUPPORT_MS_EXTENSIONS
|
||||
#pragma bss_seg(".weater_storage.bss")
|
||||
#pragma data_seg(".weater_storage.data")
|
||||
#pragma const_seg(".weater_storage.text.const")
|
||||
#pragma code_seg(".weater_storage.text")
|
||||
#endif
|
||||
|
||||
#if TCFG_DATA_STORAGE_ENABLE
|
||||
int ui_small_file_weather_get_count(void)
|
||||
{
|
||||
return small_file_get_count(F_TYPE_WEATHER);
|
||||
}
|
||||
|
||||
|
||||
int ui_small_file_weather_get_size_by_index(int index)
|
||||
{
|
||||
return small_file_get_size_by_index(F_TYPE_WEATHER, index);
|
||||
}
|
||||
|
||||
int ui_small_file_weather_read_by_index(void *buf, u32 len, int index)
|
||||
{
|
||||
int ret;
|
||||
if (!buf) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_WEATHER, index);
|
||||
if (!id) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ret = small_file_read(F_TYPE_WEATHER, id, 0, buf, len);
|
||||
if (ret == len) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
int ui_small_file_weather_get_singel_info(struct weather_single_info *info, int index)
|
||||
{
|
||||
ASSERT(info);
|
||||
u32 id = small_file_get_id_by_index(F_TYPE_WEATHER, index);
|
||||
if (!id) {
|
||||
return FALSE;
|
||||
}
|
||||
int file_size = small_file_get_size_by_index(F_TYPE_WEATHER, index);
|
||||
int offset = file_size - 9;
|
||||
if (file_size < 9) {
|
||||
return FALSE;
|
||||
}
|
||||
u8 rbuf[2] = {0};
|
||||
int rlen = 2;
|
||||
int ret = small_file_read(F_TYPE_WEATHER, id, offset, rbuf, rlen);
|
||||
if (ret == rlen) {
|
||||
info->weather = rbuf[0];
|
||||
info->temperature = (s8)rbuf[1];
|
||||
printf("%s info->weather:%d tmep:%d", __func__, info->weather, info->temperature);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/************************************************
|
||||
* 测试用例
|
||||
***********************************************/
|
||||
#if 0
|
||||
|
||||
u8 test_weather_info_buf[] = {
|
||||
0x06, 0xE5, 0xB9, 0xBF, 0xE4, 0xB8, 0x9C, 0x06, 0xE7, 0x8F, 0xA0, 0xE6, 0xB5, 0xB7, 0x03, 0x18,
|
||||
0x40, 0x05, 0x06, 0x2D, 0x52, 0xE2, 0x4C,
|
||||
};
|
||||
|
||||
void watch_data_weather_test(void)
|
||||
{
|
||||
u32 create_id = 0;
|
||||
small_file_write(F_TYPE_WEATHER, &create_id, 0, &test_weather_info_buf, sizeof(test_weather_info_buf), sizeof(test_weather_info_buf));
|
||||
printf("<%s> create_id:%d", __func__, create_id);
|
||||
|
||||
create_id = 0;
|
||||
small_file_write(F_TYPE_WEATHER, &create_id, 0, &test_weather_info_buf, sizeof(test_weather_info_buf), sizeof(test_weather_info_buf));
|
||||
printf("<%s> create_id:%d", __func__, create_id);
|
||||
|
||||
int file_count = ui_small_file_weather_get_count();
|
||||
int file_size;
|
||||
u8 *read_weather_info_buf;
|
||||
for (int i = 0; i < file_count; i++) {
|
||||
file_size = ui_small_file_weather_get_size_by_index(i);
|
||||
read_weather_info_buf = zalloc(file_size);
|
||||
printf("i:%d file_size:%d", i, file_size);
|
||||
ui_small_file_weather_read_by_index(read_weather_info_buf, file_size, i);
|
||||
put_buf(read_weather_info_buf, file_size);
|
||||
free(read_weather_info_buf);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#else /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
int ui_small_file_weather_get_count(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int ui_small_file_weather_get_size_by_index(int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int ui_small_file_weather_read_by_index(void *buf, u32 len, int index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* if TCFG_DATA_STORAGE_ENABLE */
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef __WEATHER_H__
|
||||
#define __WEATHER_H__
|
||||
|
||||
#include "generic/typedef.h"
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
struct __WEATHER_INFO {
|
||||
u8 province_name_len;
|
||||
u8 *province;
|
||||
u8 city_name_len;
|
||||
u8 *city;
|
||||
u8 weather;
|
||||
s8 temperature;
|
||||
u8 humidity;
|
||||
u8 wind_direction;
|
||||
u8 wind_power;
|
||||
u32 update_time;
|
||||
};
|
||||
|
||||
struct weather_single_info {
|
||||
u8 weather;
|
||||
s8 temperature;
|
||||
};
|
||||
//获取简单信息的接口,不耗ram
|
||||
int ui_small_file_weather_get_singel_info(struct weather_single_info *info, int index);
|
||||
int ui_small_file_weather_get_count(void);
|
||||
int ui_small_file_weather_get_size_by_index(int index);
|
||||
int ui_small_file_weather_read_by_index(void *buf, u32 len, int index);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user