This commit is contained in:
huxi
2025-12-03 11:12:34 +08:00
parent c23ae4f24c
commit bc195654bf
8163 changed files with 3799544 additions and 92 deletions
@@ -0,0 +1,376 @@
#include "app_config.h"
#include "app_task.h"
#include "system/timer.h"
#include "app_main.h"
#include "system/includes.h"
#include "key_event_deal.h"
#include "clock_manager/clock_manager.h"
#include "health_manager/health_manager.h"
#include "sensor_hub.h"
#define LOG_TAG_CONST SPORT_HEALTH_MANAGE
#define LOG_TAG "[SHM-ALGO_GSENSOR]"
#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(".health_manager.data.bss")
#pragma data_seg(".health_manager.data")
#pragma const_seg(".health_manager.text.const")
#pragma code_seg(".health_manager.text")
#endif
#define SPORT_HEALTH_MANAGER_TASK "health_manager"
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_value_get 获取模块数据
*
* @param module 模块
* @param type 获取数据类型
* @param priv
*
* @return
*/
/* ------------------------------------------------------------------------------------*/
int sport_health_manager_value_get(int module, int type, void *priv)
{
int rets;
__asm__ volatile("%0 = rets":"=r"(rets));
int ret = -SHM_ERR_MOD_NOT_FIND;
if (module >= SHM_MOD_END) {
goto __ret;
}
if (type >= SHM_GET_TYPE_MAX) {
ret = -SHM_ERR_TYPE_NOT_DEFINE;
goto __ret;
}
struct sport_health_module *mod;
list_for_each_sport_health_module(mod) {
if (mod->module == module) {
if (mod->get_value) {
ret = mod->get_value(type, priv);
} else {
ret = - SHM_ERR_MOD_CB_NOT_DEFINE;
goto __ret;
}
}
}
__ret:
if (ret) {
log_error("%s err:%d rets:0x%x", __func__, ret, rets);
}
return ret;
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_alloc 申请内存接口
*
* @param size
*
* @return
*/
/* ------------------------------------------------------------------------------------*/
void *sport_health_alloc(u32 size)
{
//debug info
return zalloc(size);
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_free 释放内存
*
* @param p
*/
/* ------------------------------------------------------------------------------------*/
void sport_health_free(void *p)
{
free(p);
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_msg_post_self 发送消息给其他模块执行
*
* @param module
* @param cmd
* @param priv
*
* @return
*/
/* ------------------------------------------------------------------------------------*/
int sport_health_manager_msg_post_self(int module, int cmd, void *priv)
{
int rets;
__asm__ volatile("%0 = rets":"=r"(rets));
int ret = SHM_ERR_OK;
struct sport_health_module *mod;
list_for_each_sport_health_module(mod) {
if (mod->module == module) {
if (mod->io_ctrl) {
mod->io_ctrl(cmd, priv);
}
continue;
}
}
return ret;
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_msg_post post消息给健康模块
*
* @param module
* @param cmd
* @param priv
* @param need_pend 是否阻塞
*
* @return
*/
/* ------------------------------------------------------------------------------------*/
int sport_health_manager_msg_post(int module, int cmd, void *priv, int need_pend)
{
int rets;
__asm__ volatile("%0 = rets":"=r"(rets));
int pnd = need_pend;
if (cpu_in_irq()) {
pnd = 0;
} else {
if (!strcmp(os_current_task(), SPORT_HEALTH_MANAGER_TASK)) {
pnd = 0;
}
}
int ret = SHM_ERR_OK;
static OS_SEM sem;
int msg[4] = {0};
msg[0] = cmd;
msg[1] = (int)priv;
if (pnd) {
os_sem_create(&sem, 0);
msg[2] = (int)&sem;
} else {
msg[2] = 0;
}
log_debug("%s mod:%d cmd:%d pnd:%d", __func__, module, cmd, need_pend);
int err = os_taskq_post_type(SPORT_HEALTH_MANAGER_TASK, module, 3, msg);
if (err) {
log_warn("%s cmd:0x%x os_err:%d rets:0x%x\n", __func__, cmd, err, rets);
ret = -SHM_ERR_OS_ERR;
} else {
if (pnd) {
os_sem_pend(&sem, 0);
}
}
return ret;
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_module_init 初始化所有模块
*/
/* ------------------------------------------------------------------------------------*/
static void sport_health_module_init()
{
log_debug("%s %d 0x%x 0x%x", __func__, __LINE__, sport_health_module_begin, sport_health_module_end);
struct sport_health_module *mod;
list_for_each_sport_health_module(mod) {
log_debug("%s %d", __func__, mod->module);
if (mod->io_ctrl) {
mod->io_ctrl(SHM_CMD_INIT, NULL);
}
}
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_module_release 注销所有模块
*/
/* ------------------------------------------------------------------------------------*/
static void sport_health_module_release()
{
log_debug("%s %d 0x%x 0x%x", __func__, __LINE__, sport_health_module_begin, sport_health_module_end);
struct sport_health_module *mod;
list_for_each_sport_health_module(mod) {
log_debug("%s %d", __func__, mod->module);
if (mod->io_ctrl) {
mod->io_ctrl(SHM_CMD_RELEASE, NULL);
}
}
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_update_sec_all 每秒更新模块
*/
/* ------------------------------------------------------------------------------------*/
static void sport_health_manager_update_sec_all()
{
for (int module = SHM_MOD_BEGIN + 1; module < SHM_MOD_END; module++) {
struct sport_health_module *mod;
list_for_each_sport_health_module(mod) {
if (mod->module == module) {
if (mod->io_ctrl) {
mod->io_ctrl(SHM_CMD_UPDATE_SEC, NULL);
}
break;
}
}
}
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_update_timer_cb timer回调
*/
/* ------------------------------------------------------------------------------------*/
static void sport_health_manager_update_timer_cb()
{
sport_health_manager_msg_post(SHM_MOD_BEGIN, SHM_CMD_UPDATE_ALL, NULL, 0);
}
#if TCFG_ACCELER_SLEEP_ENABLE
void sensor_driver_irq_handle(P33_IO_WKUP_EDGE edge)
{
sport_health_manager_msg_post(SHM_MOD_GSENSOR_ALGO, SHM_CMD_INIT, NULL, 0);
}
//只读配置,用户后续新增io唤醒、ad唤醒注意使用const修饰,节约静态ram
static const struct _p33_io_wakeup_config sensor_irq = {
.pullup_down_mode = PORT_INPUT_PULLUP_10K,
.filter = PORT_FLT_DISABLE,
.edge = RISING_EDGE,
.gpio = IO_PORTB_03,
.callback = sensor_driver_irq_handle,
};
void sensor_irq_init(void)
{
p33_io_wakeup_port_init(&sensor_irq);
p33_io_wakeup_enable(IO_PORTB_03, 1);
}
#endif
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_task 运动健康管理主线程
*/
/* ------------------------------------------------------------------------------------*/
void sport_health_manager_task()
{
log_debug("%s %d", __func__, __LINE__);
//todo
int ret;
int msg[32];
#if TCFG_SENSOR_HUB
sensor_driver_check();
#endif
#if TCFG_ACCELER_SLEEP_ENABLE
sensor_irq_init();
#endif
sport_health_module_init();
sys_s_hi_timer_add(NULL, sport_health_manager_update_timer_cb, 1000);
while (1) {
/* memset(msg, 0, 32); */
ret = os_taskq_pend(NULL, msg, ARRAY_SIZE(msg));
if (ret != OS_TASKQ) {
continue;
}
int module = msg[0];
int module_cmd = msg[1];
int module_priv = msg[2];
int msg_pnd = msg[3];
log_debug("taskloop module:%d cmd:%d", module, module_cmd);
ASSERT(module < SHM_MOD_END);
ASSERT(module_cmd < SHM_CMD_MAX);
if (module_cmd == SHM_CMD_UPDATE_ALL) {
sport_health_manager_update_sec_all();
if (msg_pnd) {
os_sem_post((OS_SEM *)msg_pnd);
}
continue;
}
struct sport_health_module *mod;
list_for_each_sport_health_module(mod) {
if (mod->module == module) {
if (mod->io_ctrl) {
mod->io_ctrl(module_cmd, (void *)module_priv);
}
break;
}
}
if (msg_pnd) {
os_sem_post((OS_SEM *)msg_pnd);
}
}
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_init 运动健康管理初始化
*
* @return
*/
/* ------------------------------------------------------------------------------------*/
int sport_health_manager_init()
{
#if TCFG_SPORT_HEALTH_ENABLE
log_debug("%s %d", __func__, __LINE__);
clock_alloc("health", 8 * MHz);
//init probe todo
int ret = SHM_ERR_OK;
int err = task_create(sport_health_manager_task, NULL, SPORT_HEALTH_MANAGER_TASK);
if (err) {
log_warn("%s os_err:%d \n", __func__, err);
ret = -SHM_ERR_OS_ERR;
}
return ret;
#else
return 0;
#endif
}
/* ------------------------------------------------------------------------------------*/
/**
* @brief sport_health_manager_release
*
* @return
*/
/* ------------------------------------------------------------------------------------*/
int sport_health_manager_release()
{
int ret = SHM_ERR_OK;
#if TCFG_SPORT_HEALTH_ENABLE
sport_health_module_release();
clock_free("health");
/* #if 0 */
/* int err = task_kill(SPORT_HEALTH_MANAGER_TASK); */
/* if (err) { */
/* log_warn("%s os_err:%d \n", __func__, err); */
/* ret = -SHM_ERR_OS_ERR; */
/* } */
/* #endif */
#endif
return ret;
}
@@ -0,0 +1,429 @@
#include "app_config.h"
#include "app_task.h"
#include "system/timer.h"
#include "app_main.h"
#include "system/includes.h"
#include "key_event_deal.h"
#include "health_manager/health_manager.h"
#define LOG_TAG_CONST SPORT_HEALTH_MANAGE
#define LOG_TAG "[SHM-ALGO_GSENSOR]"
#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(".health_manager.data.bss")
#pragma data_seg(".health_manager.data")
#pragma const_seg(".health_manager.text.const")
#pragma code_seg(".health_manager.text")
#endif
#define BIG_LITTLE_SWAP16(x) ( (((*(u16*)x) & 0xff00) >> 8) | \
(((*(u16*)x) & 0x00ff) << 8) )
#define BIG_LITTLE_SWAP32(x) ( (((*(u32*)x) & 0xff000000) >> 24) | \
(((*(u32*)x) & 0x00ff0000) >> 8) | \
(((*(u32*)x) & 0x0000ff00) << 8) | \
(((*(u32*)x) & 0x000000ff) << 24) )
void sport_health_common_swapX(const uint8_t *src, uint8_t *dst, int32_t len)
{
if (len == 2) {
BIG_LITTLE_SWAP16(dst);
} else if (len == 4) {
BIG_LITTLE_SWAP32(dst);
} else {
ASSERT(0);
}
}
int sport_health_get_value_daily_active(struct daily_active *data)
{
ASSERT(data);
return sport_health_manager_value_get(SHM_MOD_DAILY_ACTIVE, SHM_GET_TYPE_INFO, data);
}
int sport_health_get_value_daily_steps()
{
struct daily_active data;
if (!sport_health_get_value_daily_active(&data)) {
return data.steps;
}
return 0;
}
int sport_health_get_value_daily_calories()
{
struct daily_active data;
if (!sport_health_get_value_daily_active(&data)) {
return data.calories;
}
return 0;
}
int sport_health_get_value_daily_distance()
{
struct daily_active data;
if (!sport_health_get_value_daily_active(&data)) {
return data.distance / 100; //转m
}
return 0;
}
int sport_health_get_value_daily_stand_times()
{
/* struct daily_active data; */
/* if(!sport_health_get_value_daily_active(&data)){ */
/* return data.stand_times; */
/* } */
return 0;
}
int sport_health_get_value_daily_active_target(struct daily_active *data)
{
ASSERT(data);
return sport_health_manager_value_get(SHM_MOD_DAILY_ACTIVE, SHM_GET_TYPE_TARGET, data);
}
int sport_health_get_value_daily_target_steps()
{
struct daily_active data;
if (!sport_health_get_value_daily_active_target(&data)) {
return data.steps;
}
return 0;
}
int sport_health_get_value_daily_target_calories()
{
struct daily_active data;
if (!sport_health_get_value_daily_active_target(&data)) {
return data.calories;
}
return 0;
}
int sport_health_get_value_daily_target_distance()
{
struct daily_active data;
if (!sport_health_get_value_daily_active_target(&data)) {
return data.distance;
}
return 0;
}
int sport_health_get_value_daily_target_stand_times()
{
/* struct daily_active data; */
/* if(!sport_health_get_value_daily_active_target(&data)){ */
/* return data.stand_times; */
/* } */
return 0;
}
int sport_health_set_daily_active_target(struct daily_active *data)
{
return sport_health_manager_msg_post(SHM_MOD_DAILY_ACTIVE, SHM_CMD_TARGET_SET, (void *)data, 1);
}
int sport_health_ctrl_sport_start_with_type(int type)
{
//设置运动类型
int ret = sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_INFO_SET, (void *)type, 1);
ASSERT(!ret, "ret:%d", ret);
//开始运动
return sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_START, NULL, 1);
}
int sport_health_ctrl_sport_start()
{
return sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_START, NULL, 1);
}
int sport_health_ctrl_sport_pause()
{
return sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_PAUSE, NULL, 1);
}
int sport_health_ctrl_sport_continue()
{
return sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_CONTINUE, NULL, 1);
}
int sport_health_ctrl_sport_stop()
{
return sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_STOP, NULL, 1);
}
int sport_health_set_sport_type(int type)
{
return sport_health_manager_msg_post(SHM_MOD_SPORT, SHM_CMD_INFO_SET, (void *)type, 1);
}
int sport_health_get_sport_info(struct sport_value *data)
{
ASSERT(data);
return sport_health_manager_value_get(SHM_MOD_SPORT, SHM_GET_TYPE_INFO, (void *)data);
}
int sport_health_get_sport_status()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.status;
}
return 0;
}
int sport_health_get_sport_type()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.type;
}
return 0;
}
int sport_health_get_sport_steps()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.steps_c;
}
return 0;
}
int sport_health_get_sport_calories()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.calories_c;
}
return 0;
}
/*根据rcsp协议,单位统一为0.01公里*/
int sport_health_get_sport_distance()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.distance_c / 1000;
}
return 0;
}
int sport_health_get_sport_freq()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.step_freq_c;
}
return 0;
}
int sport_health_get_sport_time()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.run_sec;
}
return 0;
}
int sport_health_get_sport_file_total()
{
int total = 0;
sport_health_manager_value_get(SHM_MOD_SPORT, SHM_GET_TYPE_STORAGE_TOTAL, &total);
return total;
}
int sport_health_get_sport_file_value(struct sport_value *value)
{
return sport_health_manager_value_get(SHM_MOD_SPORT, SHM_GET_TYPE_INFO_STORAGE, value);
}
int sport_health_get_sport_hr_real()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.heart_real;
}
return 0;
}
int sport_health_get_sport_hr_max()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.heart_max;
}
return 0;
}
int sport_health_get_sport_hr_min()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.heart_min;
}
return 0;
}
int sport_health_get_sport_hr_arg()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.heart_val;
}
return 0;
}
int sport_health_get_sport_speed()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.speed_c * 3.6; //cm/s to 0.01km/h
}
return 0;
}
int sport_health_get_sport_step_stride()
{
struct sport_value info;
int ret = sport_health_get_sport_info(&info);
if (!ret) {
return info.step_stride_c;
}
return 0;
}
int sport_health_get_sport_file_id()
{
u16 file_id = 0;
sport_health_manager_value_get(SHM_MOD_SPORT, SHM_GET_TYPE_STORAGE_ID, &file_id);
return file_id;
}
int sport_health_get_sport_file_size()
{
u16 file_id = 0;
sport_health_manager_value_get(SHM_MOD_SPORT, SHM_GET_TYPE_STORAGE_ID, &file_id);
if (file_id) {
void *fp = sport_health_file_open(F_TYPE_SPORTRECORD, file_id);
int file_len = 0;
if (fp) {
file_len = sport_health_file_get_len(fp);
sport_health_file_close(fp);
}
return file_len;
}
return 0;
}
int sport_health_gsensor_algo_cfg_update()
{
return sport_health_manager_msg_post(SHM_MOD_GSENSOR_ALGO, SHM_CMD_INFO_SET, NULL, 1);
}
int sport_health_heart_rate_module_enable()
{
return sport_health_manager_msg_post(SHM_MOD_HEART_RATE, SHM_CMD_ENABLE, NULL, 1);
}
int sport_health_heart_rate_module_disable()
{
return sport_health_manager_msg_post(SHM_MOD_HEART_RATE, SHM_CMD_DISABLE, NULL, 1);
}
int sport_health_heart_rate_module_clr()
{
return sport_health_manager_msg_post(SHM_MOD_HEART_RATE, SHM_CMD_CLEAR_VALUE, NULL, 1);
}
int sport_health_heart_rate_get_cur()
{
int heart_rate = 0;
sport_health_manager_value_get(SHM_MOD_HEART_RATE, SHM_GET_TYPE_REAL_VALUE, &heart_rate);
return heart_rate;
}
int sport_health_heart_rate_get_min()
{
int heart_rate = 0;
sport_health_manager_value_get(SHM_MOD_HEART_RATE, SHM_GET_TYPE_MIN_VALUE, &heart_rate);
return heart_rate;
}
int sport_health_heart_rate_get_max()
{
int heart_rate = 0;
sport_health_manager_value_get(SHM_MOD_HEART_RATE, SHM_GET_TYPE_MAX_VALUE, &heart_rate);
return heart_rate;
}
int sport_health_heart_rate_get_avg()
{
int heart_rate = 0;
sport_health_manager_value_get(SHM_MOD_HEART_RATE, SHM_GET_TYPE_AVG_VALUE, &heart_rate);
return heart_rate;
}
int sport_health_heart_rate_get_rec(struct health_file_data_info *info)
{
return sport_health_manager_value_get(SHM_MOD_HEART_RATE, SHM_GET_TYPE_REC_VALUE, info);
}
int sport_health_blood_oxygen_module_enable()
{
return sport_health_manager_msg_post(SHM_MOD_BLOOD_OXYGEN, SHM_CMD_ENABLE, NULL, 1);
}
int sport_health_blood_oxygen_module_disable()
{
return sport_health_manager_msg_post(SHM_MOD_BLOOD_OXYGEN, SHM_CMD_DISABLE, NULL, 1);
}
int sport_health_blood_oxygen_module_clr()
{
return sport_health_manager_msg_post(SHM_MOD_BLOOD_OXYGEN, SHM_CMD_CLEAR_VALUE, NULL, 1);
}
int sport_health_blood_oxygen_get_cur()
{
int blood_oxygen = 0;
sport_health_manager_value_get(SHM_MOD_BLOOD_OXYGEN, SHM_GET_TYPE_REAL_VALUE, &blood_oxygen);
return blood_oxygen;
}
int sport_health_blood_oxygen_get_min()
{
int blood_oxygen = 0;
sport_health_manager_value_get(SHM_MOD_BLOOD_OXYGEN, SHM_GET_TYPE_MIN_VALUE, &blood_oxygen);
return blood_oxygen;
}
int sport_health_blood_oxygen_get_max()
{
int blood_oxygen = 0;
sport_health_manager_value_get(SHM_MOD_BLOOD_OXYGEN, SHM_GET_TYPE_MAX_VALUE, &blood_oxygen);
return blood_oxygen;
}
int sport_health_blood_oxygen_get_avg()
{
int blood_oxygen = 0;
sport_health_manager_value_get(SHM_MOD_BLOOD_OXYGEN, SHM_GET_TYPE_AVG_VALUE, &blood_oxygen);
return blood_oxygen;
}
int sport_health_blood_oxygen_get_rec(struct health_file_data_info *info)
{
return sport_health_manager_value_get(SHM_MOD_BLOOD_OXYGEN, SHM_GET_TYPE_REC_VALUE, info);
}
int sport_health_sleep_data_get(struct sleep_data_analysis *info, int analysis_en, struct sys_time *time)
{
if (!time) {
rtc_read_time(&info->file_time);
} else {
memcpy(&info->file_time, time, sizeof(struct sys_time));
}
int ret = sport_health_manager_value_get(SHM_MOD_SLEEP, SHM_GET_TYPE_INFO_STORAGE, (void *)info);
if (ret) {
return ret;
}
if (analysis_en) {
ret = sport_health_manager_value_get(SHM_MOD_SLEEP, SHM_GET_TYPE_DATA_ANALYSIS, (void *)info);
}
return ret;
}
@@ -0,0 +1,224 @@
#include "app_config.h"
#include "app_task.h"
#include "system/timer.h"
#include "app_main.h"
#include "system/includes.h"
#include "key_event_deal.h"
#include "health_manager/health_manager.h"
#include "data_storage/data_storage.h"
#define LOG_TAG_CONST SPORT_HEALTH_MANAGE
#define LOG_TAG "[SHM-FILE]"
#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(".health_manager.data.bss")
#pragma data_seg(".health_manager.data")
#pragma const_seg(".health_manager.text.const")
#pragma code_seg(".health_manager.text")
#endif
#define SHM_FILE_MAGIC 0X5a
struct shm_file_hd {
u8 magic;
u8 type;
u16 id;
};
#if TCFG_DATA_STORAGE_ENABLE
static void *__sport_health_file_open(u8 type, u16 id)
{
struct shm_file_hd *hd = zalloc(sizeof(struct shm_file_hd));
hd->type = type;
hd->id = id;
hd->magic = SHM_FILE_MAGIC;
log_debug("[open] id:%d", id);
return (void *)hd;
}
static int __sport_health_file_write(void *hd, u8 *buf, int offset, int len)
{
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
u32 file_id = fp->id;
log_debug("[write]type:%d id:%d offset:%d len:%d", fp->type, file_id, offset, len);
u32 ret = small_file_write(fp->type, &file_id, offset, (void *)buf, len, 0);
fp->id = file_id;
ret += offset;
return ret;
}
static int __sport_health_file_update(void *hd, u8 *buf, int offset, int len)
{
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
log_debug("[update]type:%d id:%d offset:%d len:%d", fp->type, fp->id, offset, len);
u32 ret = small_file_update_by_id(fp->type, fp->id, offset, (void *)buf, len, 0);
return ret;
}
static int __sport_health_file_read(void *hd, u8 *buf, int offset, int len)
{
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
log_debug("[read]type:%d id:%d offset:%d len:%d", fp->type, fp->id, offset, len);
return small_file_read(fp->type, fp->id, offset, (void *)buf, len);
}
static int __sport_health_file_close(void *hd)
{
if (hd) {
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
log_debug("[close]type:%d id:%d ", fp->type, fp->id);
free(hd);
/* hd = NULL; */
}
return 0;
}
static int __sport_health_file_get_len(void *hd)
{
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
log_debug("[len]type:%d id:%d ", fp->type, fp->id);
return small_file_get_size_by_id(fp->type, fp->id);
}
static int __sport_health_file_get_total(u8 type)
{
log_debug("[total]type:%d ", type);
return small_file_get_count(type);
}
static int __sport_health_file_delete(void *hd)
{
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
log_debug("[delete]type:%d id:%d ", fp->type, fp->id);
return small_file_delete_by_id(fp->type, fp->id);
}
static int __sport_health_file_get_id(void *hd, u8 index)
{
struct shm_file_hd *fp = (struct shm_file_hd *)hd;
ASSERT(fp->magic == SHM_FILE_MAGIC);
fp->id = small_file_get_id_by_index(fp->type, index);
log_debug("[get_id]type:%d id:%d index:%d ", fp->type, fp->id, index);
return fp->id;
}
static void *__sport_health_file_open_by_time(u8 type, u16 year, u8 month, u8 day)
{
log_debug("[open]type:%d [%d-%d-%d]", type, year, month, day);
if ((type == F_TYPE_SLEEP) || (type == F_TYPE_HEART) || (type == F_TYPE_BLOOD_OXYGEN)) {
struct shm_file_hd *fp = (struct shm_file_hd *)__sport_health_file_open(type, 0);
if (!fp) {
return NULL;
}
int total = __sport_health_file_get_total(type);
for (int i = 0; i < total; i++) {
fp->id = __sport_health_file_get_id((void *)fp, i);
struct health_file_total_head total_head;
__sport_health_file_read(fp, (u8 *)&total_head, 0, sizeof(struct health_file_total_head));
sport_health_common_swapX((const u8 *)&total_head.year, (u8 *)&total_head.year, 2);
if ((total_head.year == year) && (total_head.month == month) && (total_head.day == day)) {
log_debug("[open]suc type:%d id:%d", fp->type, fp->id);
return (void *)fp;
}
}
__sport_health_file_close((void *)fp);
}
return NULL;
}
const struct shm_file_ops __shm_file_ops = {
.open = __sport_health_file_open,
.write = __sport_health_file_write,
.open_by_time = __sport_health_file_open_by_time,
.read = __sport_health_file_read,
.update = __sport_health_file_update,
.close = __sport_health_file_close,
.delete = __sport_health_file_delete,
.total = __sport_health_file_get_total,
.len = __sport_health_file_get_len,
.get_id = __sport_health_file_get_id,
};
#else
const struct shm_file_ops __shm_file_ops = {0};
#endif
void *sport_health_file_open_by_time(u8 type, u16 year, u8 month, u8 day)
{
if (__shm_file_ops.open_by_time) {
return __shm_file_ops.open_by_time(type, year, month, day);
}
return NULL;
}
void *sport_health_file_open(u8 type, u16 id)
{
if (__shm_file_ops.open) {
return __shm_file_ops.open(type, id);
}
return NULL;
}
int sport_health_file_write(void *hd, u8 *buf, int offset, int len)
{
if (__shm_file_ops.write) {
return __shm_file_ops.write(hd, buf, offset, len);
}
return 0;
}
int sport_health_file_update(void *hd, u8 *buf, int offset, int len)
{
if (__shm_file_ops.update) {
return __shm_file_ops.update(hd, buf, offset, len);
}
return 0;
}
int sport_health_file_read(void *hd, u8 *buf, int offset, int len)
{
if (__shm_file_ops.read) {
return __shm_file_ops.read(hd, buf, offset, len);
}
return 0;
}
int sport_health_file_close(void *hd)
{
if (__shm_file_ops.close) {
return __shm_file_ops.close(hd);
}
return 0;
}
int sport_health_file_get_len(void *hd)
{
if (__shm_file_ops.len) {
return __shm_file_ops.len(hd);
}
return 0;
}
int sport_health_file_get_total(u8 type)
{
if (__shm_file_ops.total) {
return __shm_file_ops.total(type);
}
return 0;
}
int sport_health_file_delete(void *hd)
{
if (__shm_file_ops.delete) {
return __shm_file_ops.delete(hd);
}
return 0;
}
int sport_health_file_get_id(void *hd, u8 index)
{
if (__shm_file_ops.get_id) {
return __shm_file_ops.get_id(hd, index);
}
return 0;
}
@@ -0,0 +1,142 @@
#include "app_config.h"
#include "sport_info_opt.h"
#include "utils/syscfg_id.h"
#include "health_manager/shm_info_storage.h"
#define LOG_TAG_CONST SPORT_HEALTH_MANAGE
#define LOG_TAG "[SHM-INFO-STORAGE]"
#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(".shm_info_storage.data.bss")
#pragma data_seg(".shm_info_storage.data")
#pragma const_seg(".shm_info_storage.text.const")
#pragma code_seg(".shm_info_storage.text")
#endif
static u32 g_sport_info_switch_record = 0;
static u8 g_sport_info_mode_record[SPORT_INFO_MODE_TYPE_MAX + 1] = {0};
// 读vm
int sport_info_write_vm(int vm_id, u8 *data, u16 data_len)
{
u8 *tmp_var = zalloc(data_len);
if (NULL == tmp_var) {
goto __sport_info_write_vm_end;
}
syscfg_read(vm_id, tmp_var, data_len);
if (0 != memcmp(tmp_var, data, data_len)) {
syscfg_write(vm_id, data, data_len);
}
__sport_info_write_vm_end:
if (tmp_var) {
free(tmp_var);
}
return 0;
}
// 写vm
int sport_info_read_vm(int vm_id, u8 *data, u16 data_len)
{
int ret = 0;
u8 *tmp_var = zalloc(data_len);
if (NULL == tmp_var) {
log_error("no enough ram\n");
goto __sport_info_read_vm_end;
}
if (data_len == syscfg_read(vm_id, tmp_var, data_len)) {
memcpy(data, tmp_var, data_len);
ret = data_len;
}
__sport_info_read_vm_end:
if (tmp_var) {
free(tmp_var);
}
return ret;
}
// 更新开关
void sport_info_switch_record_update(u8 switch_type, u8 switch_state, u8 write_vm)
{
if (switch_state) {
g_sport_info_switch_record |= BIT(switch_type);
} else {
g_sport_info_switch_record &= ~BIT(switch_type);
}
if (write_vm) {
sport_info_write_vm(VM_SPORT_INFO_SWITCH_FLAG, (u8 *)&g_sport_info_switch_record, sizeof(g_sport_info_switch_record));
}
}
// 获取开关信息
u32 sport_info_swtich_record_get(u8 switch_type)
{
u32 sport_info_switch_record = 0;
if (sport_info_read_vm(VM_SPORT_INFO_SWITCH_FLAG, (u8 *)&g_sport_info_switch_record, sizeof(g_sport_info_switch_record))) {
sport_info_switch_record = g_sport_info_switch_record;
}
if (switch_type < SPORT_INFO_SWTICH_TYPE_MAX) {
return (sport_info_switch_record & BIT(switch_type));
}
return sport_info_switch_record;
}
// 更新模式
void sport_info_mode_record_update(u8 mode_type, u8 mode)
{
g_sport_info_mode_record[0] = sizeof(g_sport_info_mode_record) - 1;
g_sport_info_mode_record[mode_type + 1] = mode;
sport_info_write_vm(VM_SPORT_INFO_MODE_FLAG, g_sport_info_mode_record, sizeof(g_sport_info_mode_record));
}
// 获取模式信息
u16 sport_info_record_get(u8 mode_type, u8 *mode_data[])
{
u16 data_len = sport_info_read_vm(VM_SPORT_INFO_MODE_FLAG, g_sport_info_mode_record, sizeof(g_sport_info_mode_record));
*mode_data = g_sport_info_mode_record;
if (mode_type < SPORT_INFO_MODE_TYPE_MAX) {
*mode_data += mode_type + 1;
data_len = 1;
}
return data_len;
}
int sport_exercise_heart_rate_get(e_heart_rate *heart_rate)
{
return sport_info_read_vm(VM_SPORT_INFO_EXERCISE_HEART_RATE, (u8 *)heart_rate, sizeof(e_heart_rate));
}
int sport_fall_detection_get(fall_detection_t *fall_detect)
{
return sport_info_read_vm(VM_SPORT_INFO_FALL_DETECTION, (u8 *)fall_detect, sizeof(fall_detection_t));
}
int sport_personal_info_get(personal_information *info)
{
return sport_info_read_vm(VM_SPORT_INFO_PERSONAL_INFO_FLAG, (u8 *)info, sizeof(personal_information));
}
int sport_raise_wrist_get(raise_wrist_t *raise_wrist)
{
return sport_info_read_vm(VM_SPORT_INFO_RAISE_WRIST, (u8 *)raise_wrist, sizeof(raise_wrist_t));
}
int sport_sedentary_get(sedentary_t *sedentary)
{
return sport_info_read_vm(VM_SPORT_INFO_SEDENTARY, (u8 *)sedentary, sizeof(sedentary_t));
}
int sport_sleep_detection_get(sleep_detection_t *sleep_detection)
{
return sport_info_read_vm(VM_SPORT_INFO_SLEEP_DETECTION, (u8 *)sleep_detection, sizeof(sleep_detection_t));
}