Files
BLE-USB_Dongle/apps/usr_le_code/usr_le_recieve.c
T

574 lines
23 KiB
C

#include "system/includes.h"
#include "app_config.h"
#include "asm/pwm_led.h"
#include "tone_player.h"
#include "ui_manage.h"
#include "app_main.h"
#include "app_task.h"
#include "asm/charge.h"
#include "app_power_manage.h"
#include "app_charge.h"
#include "user_cfg.h"
#include "key_event_deal.h"
#include "audio.h"
#include "update.h"
#include "vm.h"
#include "bleproto.h"
#include "bleproto_api.h"
#if TCFG_USB_SLAVE_CDC_ENABLE
#include "usb/device/cdc.h"
#endif
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"
#define FIRM_PACK_LEN 2048//128
static u8 start_data[10]={0x55,0xAA,0xFE,0x61,0x06,0x00,0x00,0x00,0x00,0x00};
static u8 end_data[2]={0xaa,0x55};
static int encode_len=0x00;
static u8 txbuf[384]={0};
static u8 rxbuf[FIRM_PACK_LEN+1024]={0};
static u8 ota_source_data[FIRM_PACK_LEN*2]={0};
extern u16 usr_get_conn_service();
extern void ble_multi_trans_disconnect(void);
extern void usr_ble_adv_updata_OTA();
extern void usr_ble_send_data(u8* data,u16 len);
extern void usr_write_pair_info();
extern void usr_clear_pair_info();
extern void usr_set_device_default();
extern int dual_ota_app_data_deal(u32 msg, u8 *buf, u32 len);
extern int uart_tr_send_data(u8 *data, u32 len);
void usr_ota_deal_task();
void usr_data_ana(bleproto_appdata_desc_t *desc);
void usr_do_action_code(int flag);
void usr_app_ota_init();
void usr_test_protocol(void);
bleproto_authsetup_req_t usr_auth_data;
usr_module_cfg _usr_module_cfg;
usr_module_cfg_cache _usr_module_cfg_cache;
void usr_read_device_sta()
{
int ret = syscfg_read(CFG_USER_DEVICE_STA, (u8 *)&_usr_module_cfg, sizeof(_usr_module_cfg));
if (ret <= 0) {
memset(&_usr_module_cfg, 0, sizeof(_usr_module_cfg));
_usr_module_cfg.power_on_flag = 1;
syscfg_write(CFG_USER_DEVICE_STA, (u8 *)&_usr_module_cfg, sizeof(_usr_module_cfg));
}
_usr_module_cfg.power_on_flag = 1;
memset(&_usr_module_cfg_cache, 0xff, sizeof(_usr_module_cfg_cache));
_usr_module_cfg_cache.power_on_flag = 0x01;
}
void usr_reset_device_sta()
{
memset(&_usr_module_cfg, 0, sizeof(_usr_module_cfg));
_usr_module_cfg.power_on_flag = 1;
syscfg_write(CFG_USER_DEVICE_STA, (u8 *)&_usr_module_cfg, sizeof(_usr_module_cfg));
}
void usr_write_device_sta_vm()
{
syscfg_write(CFG_USER_DEVICE_STA, (u8 *)&_usr_module_cfg, sizeof(_usr_module_cfg));
memcpy(&_usr_module_cfg_cache, &_usr_module_cfg, sizeof(_usr_module_cfg));
}
void usr_write_device_sta()
{
usr_write_device_sta_vm();
}
void usr_le_data_recieve(u8* data,u16 len)///BLE数据接收
{
log_info("le recieve: %s",&data[2]);
printf_buf(data, len);
static u16 usr_len=0;
bleproto_appdata_desc_t desc;
int rc_result=0;
memcpy(&rxbuf[usr_len],data,len);
usr_len+=len;
rc_result = bleproto_appdata_decode(rxbuf, usr_len, &desc);
if(rc_result < 0)
{
usr_len=0;
}
else if(rc_result == 0)
{
/* incomplete frame */
}
else
{
usr_len=0;
usr_data_ana(&desc);
}
}
#if 1
void usr_send_ble_sta(u8 flag)
{
(void)flag;
}
void usr_set_device_default()
{
}
void usr_data_ana(bleproto_appdata_desc_t *desc)
{
bleproto_appdata_desc_t encode_desc = { 0 };
if(desc->header.msgtype/*desc.header.msgtype*/ == BLEPROTO_APPDATA_MSGTYPE_REQ) {
switch(desc->header.serviceid){
case E_BLEPROTO_SERVICE_ID_DEVICEINFO:
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid;//这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_DEVICEINFO;
/* deviceinfo 时间字段忽略:桥接板无 RTC/计划业务 */
(void)desc->datast.deviceinfo_req.t;
(void)desc->datast.deviceinfo_req.gmtoff;
//deviceinfo rsp序列化
bleproto_deviceinfo_rsp_t *rsp = &encode_desc.datast.deviceinfo_rsp;
{
// 填写设备相关信息
rsp->hbcycle=1000;
strcpy(rsp->swv, "V1.0.0.19");
strcpy(rsp->hwv, "V1.0.0.01");
rsp->longcon=1;
rsp->prototype=4;
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
printf("encode_len = %d\n",encode_len);
// 调用蓝牙协议栈发送数据接口,将数据一包一包的发送出去
// 第一包:packetlen
// 第二包:packetlen
// ......
// 第N包: totallen - (packetlen * (N -1))
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len = %d\n",encode_len);
}
//printf("E_BLEPROTO_SERVICE_ID_DEVICEINFO\n");
break;
case E_BLEPROTO_SERVICE_ID_AUTHSETUP:
// 设备处理,回复响应
printf("E_BLEPROTO_SERVICE_ID_AUTHSETUP\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid;//这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_AUTHSETUP;
bleproto_authsetup_rsp_t *au_rsp = &encode_desc.datast.authsetup_rsp;
au_rsp->errcode=0;
memcpy(&usr_auth_data,&desc->datast.authsetup_req,sizeof(usr_auth_data));
{
// 保存三元组
if(usr_auth_data.paired_flag==0)
{
usr_write_pair_info();
printf("usr_auth_data %s %s ",usr_auth_data.authcode,usr_auth_data.devid);
printf_buf(usr_auth_data.gwmac,6);
}
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
printf("encode_len = %d\n",encode_len);
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len = %d\n",encode_len);
}
break;
case E_BLEPROTO_SERVICE_ID_AUTHDELETE:
// 设备处理,回复响应
printf("E_BLEPROTO_SERVICE_ID_AUTHDELETE\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid;//这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_AUTHSETUP;
bleproto_authdelete_rsp_t *aude_rsp = &encode_desc.datast.authdelete_rsp;
memcpy(aude_rsp->gwmac,usr_auth_data.gwmac,6);
if(!memcmp(usr_auth_data.authcode,desc->datast.authdelete_req.authcode,32))
{
aude_rsp->errcode=0;
usr_set_device_default();
usr_clear_pair_info();
}
else
{
aude_rsp->errcode=501;
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
printf("encode_len = %d\n",encode_len);
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len = %d\n",encode_len);
}
break;
case E_BLEPROTO_SERVICE_ID_DOACTION:
printf("E_BLEPROTO_SERVICE_ID_DOACTION\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_DOACTION;
encode_desc.datast.doaction_rsp.errcode = 0;
{
bleproto_doaction_rsp_t *action_rsp = &encode_desc.datast.doaction_rsp;
action_rsp->errcode = 0;
printf("action_run %d \n", desc->datast.doaction_req.action);
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if (encode_len > 0) {
usr_ble_send_data(txbuf, encode_len);
usr_do_action_code(desc->datast.doaction_req.action);
}
break;
case E_BLEPROTO_SERVICE_ID_OTANOTIFY:
printf("E_BLEPROTO_SERVICE_ID_OTANOTIFY\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid;//这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTANOTIFY;
encode_desc.datast.doaction_rsp.errcode = 0;
bleproto_otanotify_rsp_t *in_ota_rsp = &encode_desc.datast.otanotify_rsp;
bleproto_otanotify_req_t *in_ota_data2= &desc->datast.otanotify_req;
printf("in_ota_data id = %d\n",in_ota_data2->id);
printf("in_ota_data ver = %s\n",in_ota_data2->version);
printf("in_ota_data size= %d\n",in_ota_data2->size);
printf("in_ota_data md5 = %s\n",in_ota_data2->md5);
{
in_ota_rsp->errcode=0;
// 执行命令
}
usr_var.usr_ota_data_id = in_ota_data2->id;//int
usr_var.usr_ota_data_size = in_ota_data2->size;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
printf("encode_len = %d\n",encode_len);
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len = %d\n",encode_len);
}
usr_var.usr_allow_to_flash=0;
usr_var.usr_data_index=0;
start_data[2]=usr_var.usr_ota_data_size&0x000000ff;
start_data[3]=(usr_var.usr_ota_data_size>>8)&0x000000ff;
start_data[4]=(usr_var.usr_ota_data_size>>16)&0x000000ff;
start_data[5]=(usr_var.usr_ota_data_size>>24)&0x000000ff;
dual_ota_app_data_deal(0,start_data,10);
printf("#### buf total size = %d\n",usr_var.usr_ota_data_size);
printf_buf(start_data,10);
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0;//desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
encode_desc.datast.doaction_rsp.errcode = 0;
encode_desc.datast.otadata_req.id=usr_var.usr_ota_data_id;
encode_desc.datast.otadata_req.offset=usr_var.usr_data_index;
encode_desc.datast.otadata_req.maxsize=FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len err = %d\n",encode_len);
}
if(usr_var.usr_double_ota_timer_id==0)
{
clk_set("sys", 48 * 1000000L);
usr_var.usr_double_ota_timer_id=1;
//clk_set("sys", 120 * 1000000L);
os_task_create(usr_ota_deal_task, NULL, 31, FIRM_PACK_LEN, 0, "usr_ota");
}
break;
case E_BLEPROTO_SERVICE_ID_OTADATA:
printf("E_BLEPROTO_SERVICE_ID_OTADATA\n");
break;
case E_BLEPROTO_SERVICE_ID_RUNCMD_V2:
{
ble_proto_packet_t *pkt = &desc->datast.runcmd_v2_req;
printf("E_BLEPROTO_SERVICE_ID_RUNCMD_V2 cmd=%d bytes_len=%d\n",
pkt->cmd, pkt->device_bytes_len);
#if TCFG_USB_SLAVE_CDC_ENABLE
/* 透传 device bytes 到 USB CDC(串口助手可见) */
if (pkt->bytes_data && pkt->device_bytes_len > 0) {
u32 wlen = cdc_write_data(0, (u8 *)pkt->bytes_data, pkt->device_bytes_len);
printf("cdc_write_data %u/%u\n", wlen, (u32)pkt->device_bytes_len);
if (wlen == 0) {
printf("cdc write fail: USB not ready(DTR) or CDC not started\n");
}
} else {
printf("runcmd_v2 no bytes payload\n");
}
#else
printf("TCFG_USB_SLAVE_CDC_ENABLE not enable\n");
#endif
break;
}
default:
break;
}
}else if(desc->header.msgtype == BLEPROTO_APPDATA_MSGTYPE_RSP) {
switch(desc->header.serviceid) {
case E_BLEPROTO_SERVICE_ID_REPORTCMD:
// 主控回复的响应,设备处理
printf("E_BLEPROTO_SERVICE_ID_REPORTCMD\n");
printf("cmd id = %d",desc->datast.reportcmd_rsp.errcode);
break;
case E_BLEPROTO_SERVICE_ID_OTADATA:
bleproto_otadata_rsp_t *ota_data_rsp = &desc->datast.otadata_rsp;
//printf("errcode = %d offset = %d len = %d\n",ota_data_rsp->errcode,ota_data_rsp->offset,ota_data_rsp->len);
uart_tr_send_data(ota_data_rsp->data,ota_data_rsp->len);
memcpy(&ota_source_data[usr_var.usr_allow_to_flash],ota_data_rsp->data,ota_data_rsp->len);
usr_var.usr_allow_to_flash+=ota_data_rsp->len;
usr_var.usr_start_write_flash=0;
if(usr_var.usr_allow_to_flash>=(FIRM_PACK_LEN*2))//0 - 1024
//1024 - 2048
//2048 - 3072
//3072 - 4096
{
usr_var.usr_start_write_flash=1;
}
else
{
usr_var.usr_data_index+=FIRM_PACK_LEN;
if(usr_var.usr_data_index>=usr_var.usr_ota_data_size)
{
usr_var.usr_start_write_flash=2;
break;
}
//bleproto_appdata_desc_t encode_desc = { 0 };
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0;//desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
encode_desc.datast.doaction_rsp.errcode = 0;
encode_desc.datast.otadata_req.id=usr_var.usr_ota_data_id;
encode_desc.datast.otadata_req.offset=usr_var.usr_data_index;
encode_desc.datast.otadata_req.maxsize=FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len err = %d\n",encode_len);
}
}
break;
}
}
}
extern void nvram_set_boot_state(u32 state);
extern void hw_mmu_disable(void);
extern void ram_protect_close(void);
AT(.volatile_ram_code)
void usr_update_init()
{
//local_irq_disable();
ram_protect_close();
//hw_mmu_disable();
//nvram_set_boot_state(2);
//JL_CLOCK->PWR_CON |= (1 << 4);
/* chip_reset(); */
/* cpu_reset(); */
//while (1);
}
void usr_ota_deal_task()
{
usr_update_init();
os_time_dly(5);
bleproto_appdata_desc_t encode_desc = { 0 };
while(1)
{
if(usr_var.usr_ota_succ_flag==0)
{
if(usr_var.usr_start_write_flash==1)
{
//OS_ENTER_CRITICAL();
usr_var.usr_data_index+=FIRM_PACK_LEN;
usr_var.usr_start_write_flash=0;
dual_ota_app_data_deal(0,ota_source_data,usr_var.usr_allow_to_flash);
os_time_dly(1);
//OS_EXIT_CRITICAL();
//printf("usr_var.usr_allow_to_flash = %d\n",usr_var.usr_allow_to_flash);
usr_var.usr_allow_to_flash=0;
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0;//desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
encode_desc.datast.doaction_rsp.errcode = 0;
encode_desc.datast.otadata_req.id=usr_var.usr_ota_data_id;
encode_desc.datast.otadata_req.offset=usr_var.usr_data_index;
encode_desc.datast.otadata_req.maxsize=FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
if(encode_len > 0) {
usr_ble_send_data(txbuf,encode_len);
}
else
{
printf("2encode_len err = %d\n",encode_len);
}
}
else if(usr_var.usr_start_write_flash==2)
{
printf("usr_start_write_flash = %d\n",usr_var.usr_start_write_flash);
usr_var.usr_start_write_flash=0;
//OS_ENTER_CRITICAL();
dual_ota_app_data_deal(0,ota_source_data,usr_var.usr_allow_to_flash);
//OS_EXIT_CRITICAL();
printf("usr_var.usr_allow_to_flash = %d\n",usr_var.usr_allow_to_flash);
usr_var.usr_allow_to_flash=0;
os_time_dly(30);
dual_ota_app_data_deal(0,end_data,2);
}
}
os_time_dly(10);
// putchar('6');
}
}
#endif // 0
void usr_ota_process()
{
static u8 cnt = 0;
if (cnt == 0) {
ble_multi_trans_disconnect();
}
if (usr_get_conn_service() == 0) {
cnt++;
if (cnt == 2) {
usr_ble_adv_updata_OTA();
}
if (cnt >= 60) {
cnt = 0;
cpu_reset();
}
} else {
cnt = 1;
}
}
void usr_do_action_code(int flag)
{
switch (flag) {
case 0: /* reboot */
cpu_reset();
break;
case 2: /* factory reset, clear pair */
usr_set_device_default();
usr_clear_pair_info();
break;
case 3: /* clear cfg, keep pair */
usr_set_device_default();
break;
case 100: /* enter OTA */
usr_app_ota_init();
break;
default:
break;
}
}
void usr_app_ota_init()
{
usr_var.usr_goto_updata = 1;
if (usr_var.ota_timer == 0) {
usr_var.ota_timer = sys_timer_add(NULL, usr_ota_process, 1000);
}
}
void usr_test_protocol(void)
{
bleproto_appdata_desc_t encode_desc = {0};
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 1;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_REPORTCMD_V2;
ble_proto_packet_t *pkt = &encode_desc.datast.runcmd_v2_req;
uint8_t test_data[9] = {0x55, 0xAA, 0x00, 0x05, 0x07, 0x00, 0x01, 0x01, 0x0E};
pkt->cmd = 100;
pkt->reserved = 0;
pkt->gateway_int_cnt = 0;
pkt->device_int_len = 0;
pkt->device_str_len = 0;
pkt->device_bytes_len = 9;
pkt->bytes_data = (uint8_t *)test_data;
int elen = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
printf("encode_len = %d\n", elen);
if (elen > 0) {
usr_ble_send_data(txbuf, elen);
printf_buf(txbuf, elen);
}
}