增加蓝牙状态上报和断链同步

This commit is contained in:
2026-07-26 21:38:07 +08:00
parent 56f2c18da4
commit 9321568920
32 changed files with 4150 additions and 3814 deletions
@@ -13,7 +13,9 @@
#include "rwip_config.h"
#if (BLE_APP_PRESENT)
#include "app_sec.h"
#include "app_task.h" // Application Manager Task API
#include "app_task.h" // Application Manager Task API
extern void ble_state_disconnect(void);
#if APP_SCAN_FUNCTION
@@ -702,7 +704,8 @@ static uint8_t gapc_callback(ke_msg_id_t const msgid, void const *param, ke_task
if (conidx == app_env.slave_conidx) {
printf("app_start_advertising\n");
app_env.slave_conidx = INVALID_DATA;
app_env.slave_connected = false;
app_env.slave_connected = false;
ble_state_disconnect();
app_start_advertising();
}
@@ -12,7 +12,8 @@
#include "ota_flash_interface.h"
#include "ota_protocol.h"
#include "pwm.h"
#include "light_transition.h"
#include "light_transition.h"
#include "usr_server.h"
/*********************************************************************************************************************/
void rwip_schedule(void);
@@ -26,7 +27,8 @@ TASK_COMPONENTS TaskComps[] =
{0,1,1,Set_timing},
{0,100,100,scan_433},
{0, 100,3, Encoder_key},
{0,1,1,My_ADC_Get_Value},
{0,1,1,My_ADC_Get_Value},
{0,50,50,ble_state_sync_poll},
{0, 311, 1, app_op_flash}, //用户数据持久化 fmc_spi_read9
{0, 311, 1, xc_ota_schedule}, //保存flash数据任务 8 //最后调用的
};
@@ -12,6 +12,8 @@
****************************************************************************************
*/
#include "usr_server.h"
#include "Mode.h"
#include "PWM.h"
#if (BLE_APP_PRESENT)
uint8_t srv_user_lid = GATT_INVALID_USER_LID;
@@ -83,6 +85,99 @@ uint16_t srv_get_hdl_from_att_idx(uint8_t idx)
}
uint8_t send_flag=1;
#define BLE_STATE_DIRTY_POWER 0x01U
#define BLE_STATE_DIRTY_MODE 0x02U
#define BLE_STATE_DIRTY_BRIGHTNESS 0x04U
#define BLE_STATE_DIRTY_SPEED 0x08U
#define BLE_STATE_DIRTY_TEMPERATURE 0x10U
#define BLE_STATE_DIRTY_ALL 0x1FU
static uint8_t ble_state_dirty = BLE_STATE_DIRTY_ALL;
static uint8_t ble_last_power;
static uint8_t ble_last_mode;
static uint8_t ble_last_brightness;
static uint8_t ble_last_speed;
static uint8_t ble_last_temperature;
static uint8_t ble_temperature_value(void)
{
uint16_t warm = W_PWM > 100U ? 100U : W_PWM;
return (uint8_t)((warm * 255U + 50U) / 100U);
}
void ble_state_disconnect(void)
{
custom_svc_tx_char_notify = DISABLE;
send_flag = 1U;
ble_state_dirty = BLE_STATE_DIRTY_ALL;
}
void ble_state_sync_poll(void)
{
uint8_t data[4] = {0U, 0U, 0U, 0U};
uint8_t value;
uint8_t sent_mask;
struct app_env_tag *app_env = get_app_env();
if(!app_env->slave_connected || !custom_svc_tx_char_notify) return;
if((deviceStatus == POWERON) != ble_last_power)
ble_state_dirty |= BLE_STATE_DIRTY_POWER;
if((uint8_t)(choose_mode_falsg + 1U) != ble_last_mode)
ble_state_dirty |= BLE_STATE_DIRTY_MODE;
if((uint8_t)(Brightness * 10U) != ble_last_brightness)
ble_state_dirty |= BLE_STATE_DIRTY_BRIGHTNESS;
if(Speed != ble_last_speed)
ble_state_dirty |= BLE_STATE_DIRTY_SPEED;
if(Mode == mode0 && ble_temperature_value() != ble_last_temperature)
ble_state_dirty |= BLE_STATE_DIRTY_TEMPERATURE;
if(!send_flag || !ble_state_dirty) return;
if(ble_state_dirty & BLE_STATE_DIRTY_POWER)
{
value = deviceStatus == POWERON;
data[3] = 0x01U;
sent_mask = BLE_STATE_DIRTY_POWER;
}
else if(ble_state_dirty & BLE_STATE_DIRTY_MODE)
{
value = choose_mode_falsg + 1U;
data[3] = 0x02U;
sent_mask = BLE_STATE_DIRTY_MODE;
}
else if(ble_state_dirty & BLE_STATE_DIRTY_BRIGHTNESS)
{
value = Brightness * 10U;
data[3] = 0x04U;
sent_mask = BLE_STATE_DIRTY_BRIGHTNESS;
}
else if(ble_state_dirty & BLE_STATE_DIRTY_SPEED)
{
value = Speed;
data[3] = 0x05U;
sent_mask = BLE_STATE_DIRTY_SPEED;
}
else
{
value = ble_temperature_value();
data[3] = 0x06U;
sent_mask = BLE_STATE_DIRTY_TEMPERATURE;
}
data[0] = value;
if(slave_send_data(data, sizeof(data), CUSTOM_SVC_TX_CHAR_VAL) == GATT_NO_ERROR)
{
if(sent_mask == BLE_STATE_DIRTY_POWER) ble_last_power = value;
else if(sent_mask == BLE_STATE_DIRTY_MODE) ble_last_mode = value;
else if(sent_mask == BLE_STATE_DIRTY_BRIGHTNESS) ble_last_brightness = value;
else if(sent_mask == BLE_STATE_DIRTY_SPEED) ble_last_speed = value;
else ble_last_temperature = value;
ble_state_dirty &= (uint8_t)(~sent_mask);
}
}
// This function is called when GATT server user has initiated event send to
// peer device or if an error occurs.
__STATIC void custom_svc_cb_event_sent(uint8_t conidx, uint8_t user_lid,
@@ -137,11 +232,11 @@ __STATIC __RAM_CODE void custom_svc_cb_att_val_set(uint8_t conidx, uint8_t user_
if (hdl == srv_get_hdl_from_att_idx(CUSTOM_SVC_TX_CHAR_CFG)) {
if ((co_buf_data(p_data)[0] == 0) && (co_buf_data(p_data)[1] == 0)) {
//custom_svc_tx_char_notify = DISABLE;
custom_svc_tx_char_notify = DISABLE;
} else {
//custom_svc_tx_char_notify = ENABLE;
/// uint8_t data[] = {0x12, 0x13, 0x14};
// slave_send_data(data, sizeof(data), CUSTOM_SVC_TX_CHAR_VAL);
custom_svc_tx_char_notify = ENABLE;
send_flag = 1U;
ble_state_dirty = BLE_STATE_DIRTY_ALL;
}
}
/*==========================================*/
@@ -185,6 +280,7 @@ uint8_t slave_send_data(uint8_t *data, uint8_t len, uint8_t att_idx)
srv_get_hdl_from_att_idx(att_idx), len,
data);
if (status != GATT_NO_ERROR) {
send_flag=1;
LOGI_ERR("gatt_srv_notify error 0x%x", status);
}
}
@@ -47,4 +47,6 @@ extern uint8_t custom_svc_tx_char_notify;
uint8_t custom_svc_add(void);
uint8_t slave_send_data(uint8_t *data, uint8_t len, uint8_t att_idx);
void ble_state_sync_poll(void);
void ble_state_disconnect(void);
#endif // _USR_SERVER_H_