新增电脑通过USB发送产品code,然后dongle根据新的code重新发起广播的功能

This commit is contained in:
2026-07-27 15:00:47 +08:00
parent c843fb24e0
commit df37ff1b23
51 changed files with 2326 additions and 1812 deletions
+15 -7
View File
@@ -15,6 +15,7 @@
#include "vm.h"
#include "bleproto.h"
#include "bleproto_api.h"
#include "usr_usb_jb.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP]"
@@ -93,9 +94,12 @@ 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] = {'C', 'B', 'C', '0'};
uint8_t prodcode[BLEPROTO_ADV_TLV_LENGTH_PRODCODE] = {0};
uint8_t gmac[6] = {0};
uint8_t pcode[6] = {'P', 'I', 'C', 'B', 'C', '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) {
@@ -105,15 +109,24 @@ void usr_ble_adv_init()
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;
}
@@ -125,11 +138,6 @@ void usr_ble_adv_init()
}
}
u8 usr_get_pair_flag()
{
return usr_auth_data.paired_flag;
}
void usr_ble_adv_updata()
{
static u16 usr_goto_slow_adv = 0;
+50
View File
@@ -0,0 +1,50 @@
/******************************************************************************
* @file usr_prodcode.h
* @brief 产品码 / 品类码持久化与广播刷新
* @author cyWu <1917507415@qq.com>
* @date 2026.07.24
* @version V1.0.0
* @history
* - V1.0.0, 2026.07.24, cyWu, 首次发布
******************************************************************************/
#ifndef __USR_PRODCODE_H__
#define __USR_PRODCODE_H__
#include "system/includes.h"
#include "bleproto_packer.h"
/*============================================================================*/
/* 宏定义 */
/*============================================================================*/
#define USR_PRODCODE_LEN BLEPROTO_ADV_TLV_LENGTH_PRODCODE /* 4 */
#define USR_PCODE_LEN BLE_PROTO_ADV_LENGTH_PCODE /* 6 */
#define USR_PRODCODE_MAGIC 0xA5
/*============================================================================*/
/* 外部接口 */
/*============================================================================*/
/**
* @brief 上电从 flash 加载产品码,无则使用默认值
*/
void usr_prodcode_init(void);
/**
* @brief 获取当前产品码(RAM 缓存)
* @param prodcode 输出 4 字节品类/产品型号
* @param pcode 输出 6 字节 productCode
*/
void usr_prodcode_get(u8 prodcode[USR_PRODCODE_LEN], u8 pcode[USR_PCODE_LEN]);
/**
* @brief 写入产品码到 flash,并触发重新广播
* @param prodcode 4 字节
* @param pcode 6 字节
* @return 0 成功,其它失败
*/
int usr_prodcode_set_save_and_readv(const u8 prodcode[USR_PRODCODE_LEN],
const u8 pcode[USR_PCODE_LEN]);
#endif /* __USR_PRODCODE_H__ */
+400
View File
@@ -0,0 +1,400 @@
/******************************************************************************
* @file usr_usb_jb.c
* @brief USB CDC 见宝帧解析:校验后写入产品码并重广播
* @author cyWu <1917507415@qq.com>
* @date 2026.07.24
* @version V1.0.1
* @history
* - V1.0.1, 2026.07.24, cyWu, CDC 中断内禁止 syscfg_write,改延时到任务上下文
* - V1.0.0, 2026.07.24, cyWu, 首次发布
******************************************************************************/
#include "system/includes.h"
#include "app_config.h"
#include "vm.h"
#include "usr_usb_jb.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[USB_JB]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
#define LOG_CLI_ENABLE
#include "debug.h"
#if TCFG_USB_SLAVE_CDC_ENABLE
#include "usb/device/cdc.h"
#endif
/*============================================================================*/
/* 协议常量(对齐 jb_protocol / jb_common */
/*============================================================================*/
#define PKT_HEAD_0 0x55
#define PKT_HEAD_1 0xAA
#define MAX_PACKAGE_LEN 128
#define JB_RB_CAPACITY 256
#define USR_PROD_MAGIC 0x5A
#ifndef CFG_USER_PROD_INFO
#define CFG_USER_PROD_INFO 28
#endif
extern void usr_ble_adv_init(void);
/*============================================================================*/
/* 类型 */
/*============================================================================*/
typedef struct {
uint8_t magic;
uint8_t prodcode[USR_PRODCODE_LEN];
uint8_t pcode[USR_PCODE_LEN];
} usr_prod_info_t;
typedef struct {
uint8_t buf[JB_RB_CAPACITY];
uint16_t write;
uint16_t read;
uint16_t count;
} usr_jb_rb_t;
/*============================================================================*/
/* 静态变量 */
/*============================================================================*/
static const uint8_t s_default_prodcode[USR_PRODCODE_LEN] = {'C', 'B', 'C', '0'};
static const uint8_t s_default_pcode[USR_PCODE_LEN] = {'P', 'I', 'C', 'B', 'C', '0'};
static uint8_t s_prodcode[USR_PRODCODE_LEN];
static uint8_t s_pcode[USR_PCODE_LEN];
static usr_jb_rb_t s_rb;
/* 待写 flash / 回 ACK 的缓存(ISR 只填,定时器里执行) */
static uint8_t s_pending_save;
static uint8_t s_pending_cmd;
static uint8_t s_pending_sn;
static uint8_t s_pending_prod[USR_PRODCODE_LEN];
static uint8_t s_pending_pcode[USR_PCODE_LEN];
static int s_process_timer;
/*============================================================================*/
/* 环形缓冲 / 校验 */
/*============================================================================*/
static void usr_jb_rb_init(usr_jb_rb_t *rb)
{
memset(rb, 0, sizeof(*rb));
}
static void usr_jb_rb_push(usr_jb_rb_t *rb, uint8_t byte)
{
if (rb->count >= JB_RB_CAPACITY) {
rb->read = (rb->read + 1) % JB_RB_CAPACITY;
rb->count--;
}
rb->buf[rb->write] = byte;
rb->write = (rb->write + 1) % JB_RB_CAPACITY;
rb->count++;
}
static uint8_t usr_jb_rb_pop(usr_jb_rb_t *rb)
{
uint8_t b = rb->buf[rb->read];
rb->read = (rb->read + 1) % JB_RB_CAPACITY;
rb->count--;
return b;
}
static uint16_t usr_jb_rb_available(const usr_jb_rb_t *rb)
{
return rb->count;
}
static uint16_t usr_jb_read_u16_be(const uint8_t *buf)
{
return ((uint16_t)buf[0] << 8) | buf[1];
}
/**
* @brief 校验和:sum(LEN_H .. payload_last) & 0xFF
*/
static uint8_t usr_jb_checksum(const uint8_t *frame, uint16_t total_len)
{
uint16_t sum = 0;
uint16_t i;
if (!frame || total_len < 4) {
return 0;
}
for (i = 2; i < total_len - 1; i++) {
sum += frame[i];
}
return (uint8_t)(sum & 0xFF);
}
/**
* @brief 从环形缓冲提取完整帧(逻辑对齐 jbGetOnePacket
* @return 0 成功, 1 需要更多数据, -2 校验失败
*/
static int8_t usr_jb_get_one_packet(uint8_t *out, uint16_t *out_len)
{
static uint8_t state = 0;
static uint16_t expect = 0;
static uint16_t idx = 0;
static uint8_t prev = 0;
while (usr_jb_rb_available(&s_rb)) {
uint8_t b = usr_jb_rb_pop(&s_rb);
if (state == 0) {
if (prev == PKT_HEAD_0 && b == PKT_HEAD_1) {
out[0] = PKT_HEAD_0;
out[1] = PKT_HEAD_1;
idx = 2;
state = 1;
}
} else if (state == 1) {
out[idx++] = b;
if (idx == 6) {
expect = 4 + usr_jb_read_u16_be(&out[2]);
if (expect > MAX_PACKAGE_LEN || expect < 7) {
state = 0;
} else {
state = 2;
}
}
} else {
out[idx++] = b;
if (idx >= expect) {
state = 0;
if (out[expect - 1] == usr_jb_checksum(out, expect)) {
*out_len = expect;
return 0;
}
printf("usr_jb checksum fail expect=0x%02x calc=0x%02x\n",
out[expect - 1], usr_jb_checksum(out, expect));
return -2;
}
}
prev = b;
}
return 1;
}
/*============================================================================*/
/* 产品码 flash */
/*============================================================================*/
static void usr_prodcode_set_default(void)
{
memcpy(s_prodcode, s_default_prodcode, USR_PRODCODE_LEN);
memcpy(s_pcode, s_default_pcode, USR_PCODE_LEN);
}
void usr_prodcode_init(void)
{
usr_prod_info_t info;
int ret;
usr_jb_rb_init(&s_rb);
s_pending_save = 0;
s_process_timer = 0;
usr_prodcode_set_default();
memset(&info, 0, sizeof(info));
ret = syscfg_read(CFG_USER_PROD_INFO, (u8 *)&info, sizeof(info));
if (ret > 0 && info.magic == USR_PROD_MAGIC) {
memcpy(s_prodcode, info.prodcode, USR_PRODCODE_LEN);
memcpy(s_pcode, info.pcode, USR_PCODE_LEN);
printf("prodcode from flash: %c%c%c%c / %c%c%c%c%c%c\n",
s_prodcode[0], s_prodcode[1], s_prodcode[2], s_prodcode[3],
s_pcode[0], s_pcode[1], s_pcode[2], s_pcode[3], s_pcode[4], s_pcode[5]);
} else {
printf("prodcode use default: %c%c%c%c / %c%c%c%c%c%c\n",
s_prodcode[0], s_prodcode[1], s_prodcode[2], s_prodcode[3],
s_pcode[0], s_pcode[1], s_pcode[2], s_pcode[3], s_pcode[4], s_pcode[5]);
}
}
void usr_prodcode_get(uint8_t *prodcode, uint8_t *pcode)
{
if (prodcode) {
memcpy(prodcode, s_prodcode, USR_PRODCODE_LEN);
}
if (pcode) {
memcpy(pcode, s_pcode, USR_PCODE_LEN);
}
}
/**
* @brief 写 VM(必须在任务/定时器上下文调用,禁止在 CDC 中断里调用)
*/
static int usr_prodcode_save(const uint8_t *prodcode, const uint8_t *pcode)
{
usr_prod_info_t info;
int ret;
if (!prodcode || !pcode) {
return -1;
}
memset(&info, 0, sizeof(info));
info.magic = USR_PROD_MAGIC;
memcpy(info.prodcode, prodcode, USR_PRODCODE_LEN);
memcpy(info.pcode, pcode, USR_PCODE_LEN);
memcpy(s_prodcode, prodcode, USR_PRODCODE_LEN);
memcpy(s_pcode, pcode, USR_PCODE_LEN);
ret = syscfg_write(CFG_USER_PROD_INFO, (u8 *)&info, sizeof(info));
if (ret <= 0) {
printf("prodcode flash write fail ret=%d\n", ret);
return -1;
}
printf("prodcode saved: %c%c%c%c / %c%c%c%c%c%c ret=%d\n",
s_prodcode[0], s_prodcode[1], s_prodcode[2], s_prodcode[3],
s_pcode[0], s_pcode[1], s_pcode[2], s_pcode[3], s_pcode[4], s_pcode[5],
ret);
return 0;
}
/**
* @brief 解析产品码 payload(仅 TLV
* 格式: [len=4][ProdtCode 4B][len=6][productCode 6B]
* @return 0 成功, -1 失败
*/
static int usr_jb_parse_prod_payload(const uint8_t *payload, uint16_t payload_len,
uint8_t *prodcode, uint8_t *pcode)
{
if (!payload || !prodcode || !pcode) {
return -1;
}
/* TLV: 04 xx xx xx xx 06 yy yy yy yy yy yy */
if (payload_len < (1 + USR_PRODCODE_LEN + 1 + USR_PCODE_LEN)) {
return -1;
}
if (payload[0] != USR_PRODCODE_LEN) {
return -1;
}
if (payload[1 + USR_PRODCODE_LEN] != USR_PCODE_LEN) {
return -1;
}
memcpy(prodcode, &payload[1], USR_PRODCODE_LEN);
memcpy(pcode, &payload[1 + USR_PRODCODE_LEN + 1], USR_PCODE_LEN);
return 0;
}
#if TCFG_USB_SLAVE_CDC_ENABLE
/**
* @brief 回 ACKCMD = 请求 CMD+1payload = result
*/
static void usr_jb_send_ack(uint8_t cmd, uint8_t sn, uint8_t result)
{
uint8_t buf[8];
uint16_t len_field = 4; /* CMD+SN+result+CKSUM */
uint16_t total = len_field + 4;
buf[0] = PKT_HEAD_0;
buf[1] = PKT_HEAD_1;
buf[2] = (uint8_t)(len_field >> 8);
buf[3] = (uint8_t)(len_field & 0xFF);
buf[4] = (uint8_t)(cmd + 1);
buf[5] = sn;
buf[6] = result;
buf[7] = usr_jb_checksum(buf, total);
cdc_write_data(0, buf, total);
}
#endif
/**
* @brief 任务上下文:解析帧 / 写 flash / ACK / 重建广播
*/
static void usr_jb_process_timeout(void *priv)
{
uint8_t pkt[MAX_PACKAGE_LEN];
uint16_t pkt_len;
int8_t ret;
uint8_t cmd;
uint8_t sn;
uint16_t payload_len;
(void)priv;
s_process_timer = 0;
/* 先把环形缓冲里的完整帧抽完 */
do {
pkt_len = 0;
ret = usr_jb_get_one_packet(pkt, &pkt_len);
if (ret != 0) {
break;
}
cmd = pkt[4];
sn = pkt[5];
payload_len = (pkt_len >= 7) ? (pkt_len - 7) : 0;
printf("usr_jb cmd=0x%02x sn=%u plen=%u\n", cmd, sn, payload_len);
printf_buf(pkt, pkt_len);
if (cmd != USR_JB_CMD_SET_PRODCODE) {
printf("usr_jb ignore cmd 0x%02x\n", cmd);
continue;
}
if (usr_jb_parse_prod_payload(&pkt[6], payload_len,
s_pending_prod, s_pending_pcode) != 0) {
printf("usr_jb set prodcode payload invalid\n");
#if TCFG_USB_SLAVE_CDC_ENABLE
usr_jb_send_ack(cmd, sn, 0x01);
#endif
continue;
}
/* 缓存后统一落盘,避免同一周期多次写 flash */
s_pending_cmd = cmd;
s_pending_sn = sn;
s_pending_save = 1;
} while (1);
if (!s_pending_save) {
return;
}
s_pending_save = 0;
if (usr_prodcode_save(s_pending_prod, s_pending_pcode) == 0) {
#if TCFG_USB_SLAVE_CDC_ENABLE
usr_jb_send_ack(s_pending_cmd, s_pending_sn, 0x00);
#endif
extern void usr_goto_pair_mode(void);
usr_goto_pair_mode();
printf("prodcode applied, adv rebuilt\n");
} else {
#if TCFG_USB_SLAVE_CDC_ENABLE
usr_jb_send_ack(s_pending_cmd, s_pending_sn, 0x05);
#endif
}
}
/**
* @brief USB CDC 喂数:仅压入环形缓冲并挂定时器(可在中断调用)
*/
void usr_usb_jb_feed(uint8_t *buf, uint32_t len)
{
uint32_t i;
if (!buf || len == 0) {
return;
}
for (i = 0; i < len; i++) {
usr_jb_rb_push(&s_rb, buf[i]);
}
/* 延时到系统定时器任务再解析/写 flash,避免中断里擦写导致复位 */
if (s_process_timer == 0) {
s_process_timer = sys_timeout_add(NULL, usr_jb_process_timeout, 20);
}
}
+51
View File
@@ -0,0 +1,51 @@
/******************************************************************************
* @file usr_usb_jb.h
* @brief USB CDC 见宝帧解析:设置 ProdtCode / productCode 并落盘重广播
* @author cyWu <1917507415@qq.com>
* @date 2026.07.24
* @version V1.0.0
* @history
* - V1.0.0, 2026.07.24, cyWu, 首次发布
******************************************************************************/
#ifndef __USR_USB_JB_H__
#define __USR_USB_JB_H__
#include <stdint.h>
/*============================================================================*/
/* 宏定义 */
/*============================================================================*/
#define USR_PRODCODE_LEN (4)
#define USR_PCODE_LEN (6)
#define USR_JB_CMD_SET_PRODCODE (0x64) /**< USB 下发产品码命令 */
/*============================================================================*/
/* 外部接口 */
/*============================================================================*/
/**
* @brief 上电加载产品码(flash 优先,否则默认值)
* @return void
*/
void usr_prodcode_init(void);
/**
* @brief 获取当前产品码缓存
* @param prodcode 输出 ProdtCode,长度 USR_PRODCODE_LEN,可为 NULL
* @param pcode 输出 productCode,长度 USR_PCODE_LEN,可为 NULL
* @return void
*/
void usr_prodcode_get(uint8_t *prodcode, uint8_t *pcode);
/**
* @brief USB CDC 喂数:仅入环并挂定时器(可在中断调用)
* @note 帧解析 / syscfg_write / ACK / 重广播在定时器任务中执行
* @param buf 接收缓冲
* @param len 长度
* @return void
*/
void usr_usb_jb_feed(uint8_t *buf, uint32_t len);
#endif /* __USR_USB_JB_H__ */