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

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" #include "rwip_config.h"
#if (BLE_APP_PRESENT) #if (BLE_APP_PRESENT)
#include "app_sec.h" #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 #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) { if (conidx == app_env.slave_conidx) {
printf("app_start_advertising\n"); printf("app_start_advertising\n");
app_env.slave_conidx = INVALID_DATA; app_env.slave_conidx = INVALID_DATA;
app_env.slave_connected = false; app_env.slave_connected = false;
ble_state_disconnect();
app_start_advertising(); app_start_advertising();
} }
@@ -12,7 +12,8 @@
#include "ota_flash_interface.h" #include "ota_flash_interface.h"
#include "ota_protocol.h" #include "ota_protocol.h"
#include "pwm.h" #include "pwm.h"
#include "light_transition.h" #include "light_transition.h"
#include "usr_server.h"
/*********************************************************************************************************************/ /*********************************************************************************************************************/
void rwip_schedule(void); void rwip_schedule(void);
@@ -26,7 +27,8 @@ TASK_COMPONENTS TaskComps[] =
{0,1,1,Set_timing}, {0,1,1,Set_timing},
{0,100,100,scan_433}, {0,100,100,scan_433},
{0, 100,3, Encoder_key}, {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, app_op_flash}, //用户数据持久化 fmc_spi_read9
{0, 311, 1, xc_ota_schedule}, //保存flash数据任务 8 //最后调用的 {0, 311, 1, xc_ota_schedule}, //保存flash数据任务 8 //最后调用的
}; };
@@ -12,6 +12,8 @@
**************************************************************************************** ****************************************************************************************
*/ */
#include "usr_server.h" #include "usr_server.h"
#include "Mode.h"
#include "PWM.h"
#if (BLE_APP_PRESENT) #if (BLE_APP_PRESENT)
uint8_t srv_user_lid = GATT_INVALID_USER_LID; 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; 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 // This function is called when GATT server user has initiated event send to
// peer device or if an error occurs. // peer device or if an error occurs.
__STATIC void custom_svc_cb_event_sent(uint8_t conidx, uint8_t user_lid, __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 (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)) { 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 { } else {
//custom_svc_tx_char_notify = ENABLE; custom_svc_tx_char_notify = ENABLE;
/// uint8_t data[] = {0x12, 0x13, 0x14}; send_flag = 1U;
// slave_send_data(data, sizeof(data), CUSTOM_SVC_TX_CHAR_VAL); 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, srv_get_hdl_from_att_idx(att_idx), len,
data); data);
if (status != GATT_NO_ERROR) { if (status != GATT_NO_ERROR) {
send_flag=1;
LOGI_ERR("gatt_srv_notify error 0x%x", status); 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 custom_svc_add(void);
uint8_t slave_send_data(uint8_t *data, uint8_t len, uint8_t att_idx); 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_ #endif // _USR_SERVER_H_
@@ -20,6 +20,7 @@ LOAD 0x1100E000
*(ram_em) *(ram_em)
} }
ScatterAssert((0x530077b0)+ImageLength(RW2) < 0x53008000) ScatterAssert((0x530077b0)+ImageLength(RW2) < 0x53008000)
ScatterAssert(ImageLength(EXE) <= 0x10000)
} }
@@ -22,6 +22,7 @@ LOAD 0x11011000
*(ram_em) *(ram_em)
} }
ScatterAssert((0x530077b0)+ImageLength(RW2) < 0x53008000) ScatterAssert((0x530077b0)+ImageLength(RW2) < 0x53008000)
ScatterAssert(ImageLength(EXE) <= 0xD000)
} }
File diff suppressed because it is too large Load Diff
@@ -60,6 +60,7 @@ compiling timeslice.c...
..\app\src\pwm.h(138): warning: #1295-D: Deprecated declaration gpio_pullup_input_inter_test - give arg types ..\app\src\pwm.h(138): warning: #1295-D: Deprecated declaration gpio_pullup_input_inter_test - give arg types
void gpio_pullup_input_inter_test(); void gpio_pullup_input_inter_test();
..\app\src\timeslice.c: 15 warnings, 0 errors ..\app\src\timeslice.c: 15 warnings, 0 errors
compiling rgblight.c...
compiling Mode.c... compiling Mode.c...
..\app\src\pwm.h(130): warning: #1295-D: Deprecated declaration _PWM_INIT - give arg types ..\app\src\pwm.h(130): warning: #1295-D: Deprecated declaration _PWM_INIT - give arg types
void _PWM_INIT(); void _PWM_INIT();
@@ -92,18 +93,68 @@ compiling Mode.c...
..\app\src\RF433.h(190): warning: #1295-D: Deprecated declaration Encoder_key - give arg types ..\app\src\RF433.h(190): warning: #1295-D: Deprecated declaration Encoder_key - give arg types
void Encoder_key(); void Encoder_key();
..\app\src\Mode.c: 15 warnings, 0 errors ..\app\src\Mode.c: 15 warnings, 0 errors
compiling usr_server.c...
..\app\src\Mode.h(39): warning: #1295-D: Deprecated declaration choice_mode - give arg types
void choice_mode();
..\app\src\Mode.h(40): warning: #1295-D: Deprecated declaration set_mode - give arg types
void set_mode();
..\app\src\Mode.h(41): warning: #1295-D: Deprecated declaration Start_PWM - give arg types
void Start_PWM();
..\app\src\Mode.h(42): warning: #1295-D: Deprecated declaration Stop_PWM - give arg types
void Stop_PWM();
..\app\src\Mode.h(43): warning: #1295-D: Deprecated declaration Set_timing - give arg types
void Set_timing();
..\app\src\PWM.h(130): warning: #1295-D: Deprecated declaration _PWM_INIT - give arg types
void _PWM_INIT();
..\app\src\PWM.h(136): warning: #1295-D: Deprecated declaration adc_Init - give arg types
void adc_Init();
..\app\src\PWM.h(137): warning: #1295-D: Deprecated declaration My_ADC_Get_Value - give arg types
void My_ADC_Get_Value();
..\app\src\PWM.h(138): warning: #1295-D: Deprecated declaration gpio_pullup_input_inter_test - give arg types
void gpio_pullup_input_inter_test();
..\app\src\usr_server.c(230): warning: #223-D: function "ble_callback" declared implicitly
ble_callback(co_buf_data(p_data)[0],co_buf_data(p_data)[1],co_buf_data(p_data)[2],co_buf_data(p_data)[3]);
..\app\src\usr_server.c: 10 warnings, 0 errors
compiling app_task.c...
..\app\src\app_task.c(450): warning: #223-D: function "ke_timer_set" declared implicitly
ke_timer_set(APP_SECOND_ADV_START, TASK_APP, 400); // start open noconnect adv
..\app\src\app_task.c(463): warning: #61-D: integer operation result is out of range
value &= ~(0xff << 24);
..\app\src\app_task.c(473): warning: #223-D: function "custom_svc_add" declared implicitly
if (!custom_svc_add()) {
..\app\src\app_task.c(496): warning: #223-D: function "ke_timer_set" declared implicitly
ke_timer_set(APP_SCAN_START, TASK_APP, 10);
..\app\src\app_task.c(640): warning: #177-D: variable "p_param" was declared but never referenced
struct gapm_ext_adv_report_ind *p_param = (struct gapm_ext_adv_report_ind *)param;
..\app\src\app_task.c(688): warning: #223-D: function "xc_ble_gatt_cli_mtu_exch" declared implicitly
xc_ble_gatt_cli_mtu_exch(conidx, app_env.user_lid);
..\app\src\app_task.c(804): warning: #223-D: function "ke_timer_set" declared implicitly
ke_timer_set(APP_SCAN_STOP, TASK_APP, 200); // start open noconnect adv
..\app\src\app_task.c(818): warning: #223-D: function "ke_timer_set" declared implicitly
ke_timer_set(APP_SCAN_START, TASK_APP, 900); // start open noconnect adv
..\app\src\app_task.c(835): warning: #223-D: function "app_switch_mode" declared implicitly
app_switch_mode(app_mode);
..\app\src\app_task.c(836): warning: #940-D: missing return statement at end of non-void function "app_mode_switch_handler"
}
..\app\src\app_task.c(837): warning: #159-D: declaration is incompatible with previous "app_switch_mode" (declared at line 835)
void app_switch_mode(uint8_t mode)
..\app\src\app_task.c(845): warning: #223-D: function "ke_timer_set" declared implicitly
ke_timer_set(APP_SECOND_ADV_START, TASK_APP, 400); // start open noconnect adv
..\app\src\app_task.c(306): warning: #177-D: function "app_get_handler" was declared but never referenced
static uint8_t app_get_handler(const struct app_subtask_handlers *handler_list_desc, ke_msg_id_t msgid, void *p_param,
..\app\src\app_task.c: 13 warnings, 0 errors
linking... linking...
Program Size: Code=27008 RO-data=1380 RW-data=2716 ZI-data=3700 Program Size: Code=27664 RO-data=1380 RW-data=2740 ZI-data=3700
FromELF: creating hex file... FromELF: creating hex file...
After Build - User command #1: fromelf --bin --output=app.bin .\Objects\Xinc_ble_sdk.axf After Build - User command #1: fromelf --bin --output=app.bin .\Objects\Xinc_ble_sdk.axf
After Build - User command #2: .\output_tool\ge_ota_scan.bat After Build - User command #2: .\output_tool\ge_ota_scan.bat
Size of file: 28664 bytes. Size of file: 29328 bytes.
all_size=28664 all_size=29328
dual_xip dual_xip
Addr:13000 Addr:13000
Size:0x6ff8 Size:0x7290
BAddr:0x40000 BAddr:0x40000
Acheck:0xf17467e0 Acheck:0x686820c0
SoftVer:0x10 SoftVer:0x10
RomVer:0x20 RomVer:0x20
LoadAddr:0x11013000 LoadAddr:0x11013000
@@ -131,7 +182,7 @@ status2=1
updata file success ! updata file success !
已复制 1 个文件 已复制 1 个文件
已复制 1 个文件 已复制 1 个文件
".\Objects\Xinc_ble_sdk.axf" - 0 Error(s), 30 Warning(s). ".\Objects\Xinc_ble_sdk.axf" - 0 Error(s), 53 Warning(s).
<h2>Software Packages used:</h2> <h2>Software Packages used:</h2>
@@ -144,7 +195,7 @@ Package Vendor: ARM
D:/Program Files/Keil_v5/ARM/Packs/ARM/CMSIS/5.9.0/Device/ARM/ARMCM0/Include D:/Program Files/Keil_v5/ARM/Packs/ARM/CMSIS/5.9.0/Device/ARM/ARMCM0/Include
<h2>Collection of Component Files used:</h2> <h2>Collection of Component Files used:</h2>
Build Time Elapsed: 00:00:09 Build Time Elapsed: 00:00:26
</pre> </pre>
</body> </body>
</html> </html>
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
@@ -103,3 +103,37 @@
.\objects\timeslice.o: ..\app\src\pwm.h .\objects\timeslice.o: ..\app\src\pwm.h
.\objects\timeslice.o: D:\Program Files\Keil_v5\ARM\ARMCC\Bin\..\include\math.h .\objects\timeslice.o: D:\Program Files\Keil_v5\ARM\ARMCC\Bin\..\include\math.h
.\objects\timeslice.o: ..\app\src\light_transition.h .\objects\timeslice.o: ..\app\src\light_transition.h
.\objects\timeslice.o: ..\app\src\usr_server.h
.\objects\timeslice.o: ..\app\api\app_task.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\dbg\api\dbg.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\dbg\api\dbg_swdiag.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\dbg\api\dbg_trc.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\src\gap\gapm\gapm_int.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gapm.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gapm_msg.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\rwip\api\rwip_task.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gap.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\common\api\co_list.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\ke\api\ke_task.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\ke\api\ke_msg.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\src\gatt\gatt_msg_int.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gatt.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gatt_msg.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\l2cap.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\l2cap_msg.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\plf\refip\src\arch\compiler\gnuarm\compiler.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\common\api\co_buf.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\modules\ke\api\ke_mem.h
.\objects\timeslice.o: ..\app\src\ota_server.h
.\objects\timeslice.o: ..\app\api\app_task.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\api\xc_gatt_server_api.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\src\gatt\gatt_db.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\api\xc_gap_api.h
.\objects\timeslice.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gapc_msg.h
@@ -125,3 +125,6 @@
.\objects\usr_server.o: ..\..\..\..\..\component\ble\ip\ble\hl\src\gatt\gatt_db.h .\objects\usr_server.o: ..\..\..\..\..\component\ble\ip\ble\hl\src\gatt\gatt_db.h
.\objects\usr_server.o: ..\..\..\..\..\component\ble\ip\ble\api\xc_gap_api.h .\objects\usr_server.o: ..\..\..\..\..\component\ble\ip\ble\api\xc_gap_api.h
.\objects\usr_server.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gapc_msg.h .\objects\usr_server.o: ..\..\..\..\..\component\ble\ip\ble\hl\api\gapc_msg.h
.\objects\usr_server.o: ..\app\src\Mode.h
.\objects\usr_server.o: ..\app\src\PWM.h
.\objects\usr_server.o: D:\Program Files\Keil_v5\ARM\ARMCC\Bin\..\include\math.h
Binary file not shown.
@@ -1,4 +1,4 @@
;#<FEEDBACK># ARM Linker, 5060750: Last Updated: Sun Jul 26 20:01:49 2026 ;#<FEEDBACK># ARM Linker, 5060750: Last Updated: Sun Jul 26 21:35:40 2026
;VERSION 0.2 ;VERSION 0.2
;FILE aes.o ;FILE aes.o
__asm___5_aes_c____REV16 <= USED 0 __asm___5_aes_c____REV16 <= USED 0
@@ -121,8 +121,6 @@ uart_receive_cb <= USED 0
;FILE usr_server.o ;FILE usr_server.o
__asm___12_usr_server_c_16ab253a____REV16 <= USED 0 __asm___12_usr_server_c_16ab253a____REV16 <= USED 0
__asm___12_usr_server_c_16ab253a____REVSH <= USED 0 __asm___12_usr_server_c_16ab253a____REVSH <= USED 0
slave_send_data <= USED 0
srv_get_hdl_from_att_idx <= USED 0
;FILE xc_drv_adc.o ;FILE xc_drv_adc.o
__asm___12_xc_drv_adc_c_08f6e5db____REV16 <= USED 0 __asm___12_xc_drv_adc_c_08f6e5db____REV16 <= USED 0
__asm___12_xc_drv_adc_c_08f6e5db____REVSH <= USED 0 __asm___12_xc_drv_adc_c_08f6e5db____REVSH <= USED 0
Binary file not shown.
Binary file not shown.