197 lines
6.1 KiB
C
197 lines
6.1 KiB
C
/******************************************************************************
|
|
* @file usr_le_adv.c
|
|
* @brief BLE broadcast / pair / reconnect (USB-BLE bridge, cat-toy logic removed)
|
|
* @author cyWu <1917507415@qq.com>
|
|
* @date 2026-07-24
|
|
* @version V1.0.0
|
|
* @history
|
|
* - V1.0.0, 2026-07-24, cyWu, trim cat-toy deps, keep bind/reconn adv
|
|
******************************************************************************/
|
|
#include "system/includes.h"
|
|
#include "app_config.h"
|
|
#include "app_main.h"
|
|
#include "app_task.h"
|
|
#include "user_cfg.h"
|
|
#include "vm.h"
|
|
#include "bleproto.h"
|
|
#include "bleproto_api.h"
|
|
#include "usr_usb_jb.h"
|
|
|
|
#define LOG_TAG_CONST APP
|
|
#define LOG_TAG "[APP]"
|
|
#define LOG_ERROR_ENABLE
|
|
#define LOG_DEBUG_ENABLE
|
|
#define LOG_INFO_ENABLE
|
|
#define LOG_CLI_ENABLE
|
|
#include "debug.h"
|
|
|
|
extern u8 muti_adv_data[31];
|
|
extern u8 muti_adv_data_len;
|
|
extern u16 usr_get_conn_service();
|
|
extern int muti_make_set_adv_data(u8 *data, u8 data_len);
|
|
extern void usr_reset_device_sta();
|
|
extern void usr_set_adv_interval(u16 val);
|
|
extern u16 usr_get_adv_interval();
|
|
extern void ble_multi_trans_disconnect(void);
|
|
extern void ble_trans_module_enable(u8 flag);
|
|
extern u8 usr_get_ota_status();
|
|
extern void usr_ota_exit();
|
|
extern int muti_make_set_adv_data_updata();
|
|
|
|
void usr_ble_adv_updata();
|
|
void usr_ble_adv_init();
|
|
|
|
static int le_timer_handle = 0;
|
|
|
|
void usr_get_pair_info()
|
|
{
|
|
int ret = syscfg_read(CFG_USER_PAIR_INFO, (u8 *)&usr_auth_data, sizeof(usr_auth_data));
|
|
if (ret <= 0) {
|
|
memset(&usr_auth_data, 0x00, sizeof(usr_auth_data));
|
|
usr_auth_data.paired_flag = 0;
|
|
syscfg_write(CFG_USER_PAIR_INFO, (u8 *)&usr_auth_data, sizeof(usr_auth_data));
|
|
}
|
|
}
|
|
|
|
void usr_write_pair_info()
|
|
{
|
|
usr_auth_data.paired_flag = 1;
|
|
syscfg_write(CFG_USER_PAIR_INFO, (u8 *)&usr_auth_data, sizeof(usr_auth_data));
|
|
}
|
|
|
|
void usr_clear_pair_info()
|
|
{
|
|
printf("usr_clear_pair_info\n");
|
|
memset(&usr_auth_data, 0x00, sizeof(usr_auth_data));
|
|
usr_auth_data.paired_flag = 0;
|
|
syscfg_write(CFG_USER_PAIR_INFO, (u8 *)&usr_auth_data, sizeof(usr_auth_data));
|
|
if (_usr_module_cfg.power_on_flag) {
|
|
sys_timeout_add(NULL, cpu_reset, 600);
|
|
} else {
|
|
sys_enter_soft_poweroff(NULL);
|
|
}
|
|
}
|
|
|
|
void usr_clear_pair_info_noreset()
|
|
{
|
|
printf("usr_clear_pair_info2\n");
|
|
memset(&usr_auth_data, 0x00, sizeof(usr_auth_data));
|
|
usr_auth_data.paired_flag = 0;
|
|
syscfg_write(CFG_USER_PAIR_INFO, (u8 *)&usr_auth_data, sizeof(usr_auth_data));
|
|
usr_reset_device_sta();
|
|
}
|
|
|
|
void usr_goto_pair_mode()
|
|
{
|
|
ble_multi_trans_disconnect();
|
|
usr_clear_pair_info_noreset();
|
|
usr_var.usr_goto_pair = 1;
|
|
os_time_dly(5);
|
|
usr_ble_adv_init();
|
|
}
|
|
|
|
void usr_ble_adv_init()
|
|
{
|
|
int8_t txpower = 0;
|
|
uint8_t manucode[BLEPROTO_ADV_TLV_LENGTH_MANUCODE] = {0};
|
|
uint8_t prodcode[BLEPROTO_ADV_TLV_LENGTH_PRODCODE] = {0};
|
|
uint8_t gmac[6] = {0};
|
|
uint8_t pcode[BLE_PROTO_ADV_LENGTH_PCODE] = {0};
|
|
|
|
/* flash 优先,无有效数据则用模块内默认值 */
|
|
usr_prodcode_get(prodcode, pcode);
|
|
|
|
usr_set_adv_interval(80);
|
|
if (usr_var.usr_ota_succ_flag) {
|
|
return;
|
|
}
|
|
|
|
if (usr_var.usr_goto_pair) {
|
|
usr_clear_pair_info_noreset();
|
|
printf("power on force to pair flag= %d\n", usr_auth_data.paired_flag);
|
|
printf("adv prodcode=%c%c%c%c pcode=%c%c%c%c%c%c\n",
|
|
prodcode[0], prodcode[1], prodcode[2], prodcode[3],
|
|
pcode[0], pcode[1], pcode[2], pcode[3], pcode[4], pcode[5]);
|
|
muti_adv_data_len = bleproto_advdata_encode4bind(muti_adv_data, txpower, manucode, pcode, prodcode);
|
|
usr_var.usr_adv_flag = 1;
|
|
} else {
|
|
usr_get_pair_info();
|
|
if (usr_auth_data.paired_flag == 0) {
|
|
printf("adv prodcode=%c%c%c%c pcode=%c%c%c%c%c%c\n",
|
|
prodcode[0], prodcode[1], prodcode[2], prodcode[3],
|
|
pcode[0], pcode[1], pcode[2], pcode[3], pcode[4], pcode[5]);
|
|
muti_adv_data_len = bleproto_advdata_encode4bind(muti_adv_data, txpower, manucode, pcode, prodcode);
|
|
usr_var.usr_adv_flag = 0;
|
|
} else {
|
|
memcpy(gmac, usr_auth_data.gwmac, 6);
|
|
printf("adv prodcode=%c%c%c%c pcode=%c%c%c%c%c%c\n",
|
|
prodcode[0], prodcode[1], prodcode[2], prodcode[3],
|
|
pcode[0], pcode[1], pcode[2], pcode[3], pcode[4], pcode[5]);
|
|
muti_adv_data_len = bleproto_advdata_encode4reconn(muti_adv_data, txpower, gmac, manucode, pcode, prodcode);
|
|
usr_var.usr_adv_flag = 1;
|
|
}
|
|
}
|
|
|
|
usr_ble_adv_updata();
|
|
if (le_timer_handle == 0) {
|
|
le_timer_handle = sys_timer_add(NULL, usr_ble_adv_updata, 500);
|
|
}
|
|
}
|
|
|
|
void usr_ble_adv_updata()
|
|
{
|
|
static u16 usr_goto_slow_adv = 0;
|
|
|
|
if (usr_var.usr_adv_flag) {
|
|
ble_trans_module_enable(0);
|
|
usr_var.usr_adv_flag = 0;
|
|
muti_make_set_adv_data(muti_adv_data, muti_adv_data_len);
|
|
printf("###muti_adv_data = ");
|
|
printf_buf(muti_adv_data, muti_adv_data_len);
|
|
ble_trans_module_enable(1);
|
|
}
|
|
|
|
if (usr_get_conn_service() == 0) {
|
|
if (usr_get_adv_interval() == 80) {
|
|
if ((usr_var.usr_goto_pair == 0) && (usr_auth_data.paired_flag)) {
|
|
if (usr_var.usr_ota_succ_flag == 0) {
|
|
usr_goto_slow_adv++;
|
|
}
|
|
if (usr_goto_slow_adv >= 120) {
|
|
usr_goto_slow_adv = 0;
|
|
usr_set_adv_interval(80 * 10);
|
|
usr_var.usr_adv_flag = 1;
|
|
}
|
|
}
|
|
} else {
|
|
usr_goto_slow_adv = 0;
|
|
}
|
|
} else {
|
|
usr_goto_slow_adv = 0;
|
|
}
|
|
|
|
if (usr_get_ota_status() == 0) {
|
|
if (usr_get_conn_service() == 0) {
|
|
if (usr_var.usr_ota_succ_flag == 0) {
|
|
usr_ota_exit();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void usr_ble_adv_updata_OTA()
|
|
{
|
|
ble_trans_module_enable(0);
|
|
usr_var.usr_adv_flag = 0;
|
|
|
|
extern u8 usr_mac_addr[6];
|
|
extern int le_controller_set_mac(void *addr);
|
|
usr_mac_addr[0] = usr_mac_addr[0] + 1;
|
|
le_controller_set_mac(usr_mac_addr);
|
|
|
|
muti_make_set_adv_data_updata();
|
|
printf("###muti_adv_data = ");
|
|
printf_buf(muti_adv_data, muti_adv_data_len);
|
|
ble_trans_module_enable(1);
|
|
}
|