|
|
@ -1,22 +1,31 @@ |
|
|
|
#include "device_ctrl_service.h" |
|
|
|
|
|
|
|
#include "../../ify_hrs_protocol/heart_rate_sensor_protocol.h" |
|
|
|
#include "app_event_distribute.h" |
|
|
|
#include "board/board_battery_state.h" |
|
|
|
#include "board/board_beep_ctrl.h" |
|
|
|
#include "board/board_button.h" |
|
|
|
#include "board/board_light_ctrl.h" |
|
|
|
#include "board/board_sdcard_driver.h" |
|
|
|
#include "board/device_exception_manager.h" |
|
|
|
#include "heart_wave_sample_service.h" |
|
|
|
#include "sample_data_manager_service.h" |
|
|
|
#include "zble_module.h" |
|
|
|
#include "zdatachannel_service.h" |
|
|
|
#include "znordic.h" |
|
|
|
// |
|
|
|
APP_TIMER_DEF(m_state_machine_driver_tmr); // 状态机驱动定时器 |
|
|
|
static device_state_t m_device_state = kdevice_state_standby; // 设备状态 |
|
|
|
static uint32_t m_change_to_cur_state_tp = 0; // 切换到当前状态的时间戳 |
|
|
|
static int m_sample_data_fd = -1; // 采集数据文件描述符 |
|
|
|
static int m_sample_duration_ms; |
|
|
|
APP_TIMER_DEF(m_state_machine_driver_tmr); // 状态机驱动定时器 |
|
|
|
static device_state_t m_device_state = kdevice_state_standby; // 设备状态 |
|
|
|
static uint32_t m_change_to_cur_state_tp = 0; // 切换到当前状态的时间戳 |
|
|
|
static int m_sample_data_fd = -1; // 采集数据文件描述符 |
|
|
|
static bool m_has_triggered_connected = false; // 被连接过 |
|
|
|
|
|
|
|
static uint32_t m_settingStorageTimeS; |
|
|
|
static uint32_t m_startStorageTimeTicket; |
|
|
|
static uint32_t m_hasStoragedTimeS; |
|
|
|
static bool m_isStoraging; |
|
|
|
|
|
|
|
static bool m_IsRealtimeReport; |
|
|
|
|
|
|
|
/******************************************************************************* |
|
|
|
* 函数列表 * |
|
|
@ -170,6 +179,7 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { // |
|
|
|
* 状态无关事件处理 * |
|
|
|
*******************************************************************************/ |
|
|
|
if (event->eventType == kevent_ble_connect_event) { |
|
|
|
m_has_triggered_connected = true; |
|
|
|
BoardLight_setGreenLightEffect(kLightEffect_slowFlash); |
|
|
|
} else if (event->eventType == kevent_ble_disconnect_event) { |
|
|
|
BoardLight_setGreenLightEffect(kLightEffect_quickFlash); |
|
|
@ -197,12 +207,11 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { // |
|
|
|
} else if (BoardBattery_get_battery_level() < APP_LOW_BATTERY_LIMIT) { |
|
|
|
ZLOGI("auto sleep because low battery"); |
|
|
|
prvf_change_to_standby_state(); |
|
|
|
} else if (m_has_triggered_connected && !zble_module_is_connected()) { |
|
|
|
prvf_change_to_standby_state(); |
|
|
|
} |
|
|
|
} |
|
|
|
// 如果设备没有采集时,蓝牙断开,设备直接进入休眠状态 |
|
|
|
if (event->eventType == kevent_ble_disconnect_event) { |
|
|
|
prvf_change_to_standby_state(); |
|
|
|
} |
|
|
|
|
|
|
|
if (event->eventType == kevent_start_sample_cmd_event) { |
|
|
|
ZLOGI("start sample"); |
|
|
@ -229,7 +238,11 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { // |
|
|
|
else if (m_device_state == kdevice_state_sampling) { |
|
|
|
// 采集的字节长度超过最大字节长度,停止采集 |
|
|
|
|
|
|
|
if (event->eventType == kevent_capture_data_block_event) { |
|
|
|
if (m_isStoraging) { |
|
|
|
m_hasStoragedTimeS = znordic_getpower_on_s() - m_startStorageTimeTicket; |
|
|
|
} |
|
|
|
|
|
|
|
if (m_isStoraging && event->eventType == kevent_capture_data_block_event) { |
|
|
|
ZLOGI("capture data block %d", event->val.block_sensor_data.len); |
|
|
|
SampleDataMgr_write(m_sample_data_fd, (uint8_t*)event->val.block_sensor_data.data, event->val.block_sensor_data.len); |
|
|
|
} |
|
|
@ -252,8 +265,14 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { // |
|
|
|
} else if (event->eventType == kevent_stop_sample_cmd_event) { |
|
|
|
ZLOGI("stop sample because stop sample event"); |
|
|
|
stopcapture = true; |
|
|
|
} else if (m_sample_duration_ms != 0 && DeviceCtrl_cur_state_haspassed_ms() > m_sample_duration_ms) { |
|
|
|
ZLOGI("stop sample because sample timeout"); |
|
|
|
} else if (m_isStoraging && m_hasStoragedTimeS >= m_settingStorageTimeS) { |
|
|
|
ZLOGI("stop sample because storage time is enough"); |
|
|
|
stopcapture = true; |
|
|
|
} else if (!m_isStoraging && !zble_module_is_connected()) { |
|
|
|
ZLOGI("stop sample because ble disconnected"); |
|
|
|
stopcapture = true; |
|
|
|
} else if (!m_isStoraging && zble_module_is_connected() && zdatachannel_last_rx_data_haspassed_s() >= BLE_UNCONNECTED_OVERTIME_S) { |
|
|
|
ZLOGI("stop sample because ble unconnected"); |
|
|
|
stopcapture = true; |
|
|
|
} else if (BoardBattery_get_battery_level() < APP_LOW_BATTERY_LIMIT) { |
|
|
|
ZLOGI("stop sample because low battery"); |
|
|
@ -282,22 +301,93 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { // |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void DeviceCtrl_startSample(int duration_s) { |
|
|
|
ZLOGI("start sample %d", duration_s); |
|
|
|
m_sample_duration_ms = duration_s * 1000; |
|
|
|
if (duration_s == 0) { |
|
|
|
m_sample_duration_ms = 2 * 60 * 1000; // 两分钟 |
|
|
|
uint8_t DeviceCtrl_startStorage(uint32_t storageTimeS) { |
|
|
|
if (DeviceExceptionMgr_has_error()) { |
|
|
|
return kifyhrs_ecode_device_exception; |
|
|
|
} |
|
|
|
static app_event_t event; |
|
|
|
event.eventType = kevent_start_sample_cmd_event; |
|
|
|
AppEvent_pushEvent(&event); |
|
|
|
|
|
|
|
if (DeviceCtrl_now_state() != kdevice_state_sampling) { |
|
|
|
return kifyhrs_ecode_invalid_state; |
|
|
|
} |
|
|
|
|
|
|
|
if (storageTimeS == 0) { |
|
|
|
return kifyhrs_ecode_illegal_parameter; |
|
|
|
} |
|
|
|
|
|
|
|
if (storageTimeS >= APP_MAX_STORAGE_TIME_S) { |
|
|
|
return kifyhrs_ecode_illegal_parameter; |
|
|
|
} |
|
|
|
|
|
|
|
m_settingStorageTimeS = storageTimeS; |
|
|
|
m_hasStoragedTimeS = 0; |
|
|
|
m_startStorageTimeTicket = znordic_getpower_on_s(); |
|
|
|
m_isStoraging = true; |
|
|
|
|
|
|
|
ZLOGI("start storage %d", storageTimeS); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
void DeviceCtrl_stopSample() { |
|
|
|
ZLOGI("stop sample"); |
|
|
|
static app_event_t event; |
|
|
|
event.eventType = kevent_stop_sample_cmd_event; |
|
|
|
AppEvent_pushEvent(&event); |
|
|
|
uint8_t DeviceCtrl_stopStorage() { |
|
|
|
m_isStoraging = false; |
|
|
|
m_hasStoragedTimeS = 0; |
|
|
|
m_settingStorageTimeS = 0; |
|
|
|
m_startStorageTimeTicket = 0; |
|
|
|
|
|
|
|
if (DeviceCtrl_now_state() == kdevice_state_sampling) { |
|
|
|
static app_event_t event; |
|
|
|
event.eventType = kevent_stop_sample_cmd_event; |
|
|
|
AppEvent_pushEvent(&event); |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
bool DeviceCtrl_isStoraging() { return m_isStoraging; } |
|
|
|
uint32_t DeviceCtrl_getSettingStorageTime() { return m_settingStorageTimeS; } |
|
|
|
uint32_t DeviceCtrl_getHasStoragedTime() { return m_hasStoragedTimeS; } |
|
|
|
|
|
|
|
uint8_t DeviceCtrl_StartPreview() { |
|
|
|
if (DeviceExceptionMgr_has_error()) { |
|
|
|
return kifyhrs_ecode_device_exception; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief |
|
|
|
* 1.如果当前没有在采集中,则开始采集 |
|
|
|
* 2.如果当前正在采集中, 则只设置上报标志位 |
|
|
|
*/ |
|
|
|
|
|
|
|
if (m_device_state != kdevice_state_ready && m_device_state != kdevice_state_sampling) { |
|
|
|
return kifyhrs_ecode_invalid_state; |
|
|
|
} |
|
|
|
|
|
|
|
if (m_device_state == kdevice_state_ready) { |
|
|
|
static app_event_t event; |
|
|
|
event.eventType = kevent_start_sample_cmd_event; |
|
|
|
AppEvent_pushEvent(&event); |
|
|
|
m_IsRealtimeReport = true; |
|
|
|
|
|
|
|
m_isStoraging = false; |
|
|
|
m_hasStoragedTimeS = 0; |
|
|
|
m_settingStorageTimeS = 0; |
|
|
|
m_startStorageTimeTicket = 0; |
|
|
|
|
|
|
|
} else if (m_device_state == kdevice_state_sampling) { |
|
|
|
m_IsRealtimeReport = true; |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
uint8_t DeviceCtrl_StopPreview() { |
|
|
|
m_IsRealtimeReport = false; |
|
|
|
if (m_device_state == kdevice_state_sampling && !m_isStoraging) { |
|
|
|
static app_event_t event; |
|
|
|
event.eventType = kevent_stop_sample_cmd_event; |
|
|
|
AppEvent_pushEvent(&event); |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
bool DeviceCtrl_IsRealtimeReport() { return m_IsRealtimeReport; } |
|
|
|
|
|
|
|
/******************************************************************************* |
|
|
|
* EXTERN * |
|
|
|