2023-12-18 18:17:21 +08:00
|
|
|
|
#include "tran_for_broadcast.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "mymisc.h"
|
|
|
|
|
#include "mystdlib.h"
|
|
|
|
|
#include "board.h"
|
|
|
|
|
#include "dev_flash.h"
|
|
|
|
|
#include "prot_uc.h"
|
|
|
|
|
#include "coder_lib.h"
|
|
|
|
|
#include "JQ_PSDGenerate.h"
|
|
|
|
|
#include "mystring.h"
|
|
|
|
|
#include "coder_judge.h"
|
|
|
|
|
#include "JQ_UIDGenerate.h"
|
|
|
|
|
#include "transmit.h"
|
|
|
|
|
#include "elec_det.h"
|
|
|
|
|
#include "crc.h"
|
|
|
|
|
#include "dev_backup.h"
|
|
|
|
|
#include "elec_task_slot.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
执行任务命令格式
|
2023-12-19 18:21:28 +08:00
|
|
|
|
[0...1]=总字节数,[2]=当前包数(0开始),[3]=当前包字节数
|
|
|
|
|
[4...n]=数据
|
2023-12-18 18:17:21 +08:00
|
|
|
|
|
|
|
|
|
数据域格式
|
2023-12-20 18:27:37 +08:00
|
|
|
|
[0]=检测模式0,只检测,返回原始检测结果;模式1,检测并添加任务插槽,只返回异常代码和任务插槽数据;
|
|
|
|
|
模式2,检测并执行任务插槽,返回原始检测数据和任务插槽数据;
|
|
|
|
|
模式3,只执行任务插槽,返回异常代码和插槽数据
|
|
|
|
|
[1...n]小板数据
|
|
|
|
|
|
|
|
|
|
小板数据格式
|
|
|
|
|
[0]=小板地址(0x1f时为通用数据,每个小板都可以使用),[1]=数据长度,[2...n]=数据
|
2023-12-18 18:17:21 +08:00
|
|
|
|
|
|
|
|
|
单个任务数据格式
|
|
|
|
|
[0]=插槽序号,[1]=任务id,[2]=参数个数(u16)
|
|
|
|
|
[3]=返回值个数(u16),[4]=异常代码,[5]=任务序号
|
|
|
|
|
[6...n]=参数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
返回数据域格式
|
|
|
|
|
[0]=任务序号,[1]=返回数据长度(u16)
|
|
|
|
|
[2...n]=参数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct{
|
|
|
|
|
ucport_def u;
|
|
|
|
|
rt_timer_t timer;
|
|
|
|
|
}slave_def;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-21 18:45:48 +08:00
|
|
|
|
typedef struct{
|
|
|
|
|
array_def *data;
|
|
|
|
|
}self_def;
|
|
|
|
|
static self_def g_self;
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 18:17:21 +08:00
|
|
|
|
|
|
|
|
|
static slave_def *slave_creat(void)
|
|
|
|
|
{
|
|
|
|
|
slave_def *u=calloc(1,sizeof(slave_def));
|
|
|
|
|
return u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void slave_del(ucport_def *u)
|
|
|
|
|
{
|
|
|
|
|
slave_def *s=(slave_def *)u;
|
|
|
|
|
CHECK_DO(s->timer,rt_timer_delete);
|
2023-12-21 18:45:48 +08:00
|
|
|
|
// CHECK_DO(s->data,arr_delete);
|
2023-12-18 18:17:21 +08:00
|
|
|
|
free(u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 找到自己的数据
|
2023-12-21 18:45:48 +08:00
|
|
|
|
static void slave_get_self(self_def *s)
|
2023-12-18 18:17:21 +08:00
|
|
|
|
{
|
|
|
|
|
int len=arr_length(s->data);
|
|
|
|
|
int off=0;
|
|
|
|
|
int addr,slave_len;
|
|
|
|
|
array_def *expat=arr_creat();
|
|
|
|
|
uint8_t *d;
|
|
|
|
|
while(off<len){
|
|
|
|
|
addr=arr_get(s->data,off);
|
|
|
|
|
slave_len=arr_get(s->data,off+1);
|
2023-12-20 18:27:37 +08:00
|
|
|
|
if((addr==elec_local_addr())||(addr==0x1f)){
|
2023-12-18 18:17:21 +08:00
|
|
|
|
d=arr_data(s->data)+off+2;
|
|
|
|
|
arr_appends(expat,d,slave_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
off+=slave_len+2;
|
|
|
|
|
}
|
|
|
|
|
arr_delete(s->data);
|
|
|
|
|
s->data=expat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加任务
|
2023-12-21 18:45:48 +08:00
|
|
|
|
static void slave_adds(self_def *s)
|
2023-12-18 18:17:21 +08:00
|
|
|
|
{
|
|
|
|
|
task_def *t=calloc(1,sizeof(task_def));
|
|
|
|
|
void *slot=0;
|
|
|
|
|
int off=0;
|
|
|
|
|
int len=arr_length(s->data);
|
|
|
|
|
task_slot_delete();
|
|
|
|
|
slot=task_slot_init();
|
|
|
|
|
while(off<len){
|
2023-12-25 18:46:51 +08:00
|
|
|
|
t->slot_index=arr_get(s->data,off);
|
|
|
|
|
t->task_id=arr_get(s->data,off+1);
|
|
|
|
|
t->par_count=arr_get(s->data,off+2);
|
|
|
|
|
t->ret_count=arr_get(s->data,off+3);
|
|
|
|
|
t->errcode=arr_get(s->data,off+4);
|
2023-12-21 18:45:48 +08:00
|
|
|
|
t->exe_flag=0;
|
2023-12-25 18:46:51 +08:00
|
|
|
|
t->task_index=arr_get(s->data,off+5);
|
|
|
|
|
memcpy(t->params,arr_data(s->data)+off+6,t->par_count*2);
|
2023-12-18 18:17:21 +08:00
|
|
|
|
task_slot_add_item(slot,t);
|
2023-12-26 18:08:59 +08:00
|
|
|
|
DBG_LOG("slot_index=%d,task_id=%d,par_count=%d,ret_count=%d",
|
|
|
|
|
t->slot_index,t->task_id,t->par_count,t->ret_count);
|
2023-12-18 18:17:21 +08:00
|
|
|
|
off+=6+t->par_count*2;
|
|
|
|
|
}
|
|
|
|
|
free(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成返回数据
|
2023-12-21 18:45:48 +08:00
|
|
|
|
// 如果ret不为0,则存在未执行的槽任务
|
|
|
|
|
static array_def *slave_creat_retdata(slave_def *s,array_def *r,int *ret)
|
2023-12-18 18:17:21 +08:00
|
|
|
|
{
|
|
|
|
|
void *slot=task_slot_init();
|
|
|
|
|
task_def *t=0;
|
2023-12-21 18:45:48 +08:00
|
|
|
|
int err=0;
|
2023-12-18 18:17:21 +08:00
|
|
|
|
while(t=task_slot_next_item(slot),t!=0)
|
|
|
|
|
{
|
2023-12-21 18:45:48 +08:00
|
|
|
|
if(t->exe_flag==0){
|
|
|
|
|
err|=1;
|
|
|
|
|
}
|
|
|
|
|
if(r){
|
|
|
|
|
arr_append(r,t->task_index);
|
|
|
|
|
arr_append(r,t->ret_count);
|
|
|
|
|
arr_appends(r,t->rets,t->ret_count*2);
|
|
|
|
|
}
|
2023-12-18 18:17:21 +08:00
|
|
|
|
}
|
2023-12-21 18:45:48 +08:00
|
|
|
|
if(ret) *ret=err;
|
2023-12-18 18:17:21 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-21 18:45:48 +08:00
|
|
|
|
// 重新设置异常代码,异常代码9未执行的槽任务
|
|
|
|
|
#define SET_ERRCODE(arr){\
|
|
|
|
|
if(slot_exe_flag){\
|
|
|
|
|
if(arr_get(arr,0)==0){\
|
|
|
|
|
arr_reset(arr,9,0);\
|
|
|
|
|
}\
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 18:17:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行任务
|
|
|
|
|
static int run_task_dolater(ucport_def *u,uint8_t cmd,array_def *data,char *err_str)
|
|
|
|
|
{
|
|
|
|
|
slave_def *s=(slave_def *)u;
|
2023-12-21 18:45:48 +08:00
|
|
|
|
self_def *g=&g_self;
|
2023-12-19 18:21:28 +08:00
|
|
|
|
int pack_curr=0,pack_all=0,pack_size=0;
|
2023-12-18 18:17:21 +08:00
|
|
|
|
array_def *r=0;
|
|
|
|
|
array_def *check=0;
|
|
|
|
|
uint8_t *d;
|
2023-12-20 18:27:37 +08:00
|
|
|
|
uint8_t ret_mode=0;
|
2023-12-21 18:45:48 +08:00
|
|
|
|
int slot_exe_flag=0;
|
2023-12-19 18:21:28 +08:00
|
|
|
|
pack_all=arr_get(data,0)|(arr_get(data,1)<<8);
|
|
|
|
|
pack_curr=arr_get(data,2);
|
|
|
|
|
pack_size=arr_get(data,3);
|
|
|
|
|
if(pack_curr==0){
|
2023-12-18 18:17:21 +08:00
|
|
|
|
// 第一个数据包重置接收
|
2023-12-21 18:45:48 +08:00
|
|
|
|
CHECK_DO(g->data,arr_delete);
|
|
|
|
|
g->data=arr_creat();
|
2023-12-18 18:17:21 +08:00
|
|
|
|
}
|
2023-12-21 18:45:48 +08:00
|
|
|
|
if(arr_length(g->data)<pack_all){
|
2023-12-19 18:21:28 +08:00
|
|
|
|
d=arr_data(data)+4;
|
2023-12-21 18:45:48 +08:00
|
|
|
|
arr_appends(g->data,d,arr_length(data)-4);
|
2023-12-19 18:21:28 +08:00
|
|
|
|
r=arr_creat();
|
|
|
|
|
arr_append(r,0);
|
|
|
|
|
emit tran_reply_signal(u->p,arr_temp(r));
|
|
|
|
|
}
|
2023-12-21 18:45:48 +08:00
|
|
|
|
if(arr_length(g->data)==pack_all){
|
|
|
|
|
// 延时以保证正常回复到上位机
|
|
|
|
|
rt_thread_mdelay(50);
|
2023-12-19 18:21:28 +08:00
|
|
|
|
// 接收完成后执行命令
|
2023-12-21 18:45:48 +08:00
|
|
|
|
ret_mode=arr_get(g->data,0);
|
|
|
|
|
arr_remove(g->data,0,1);
|
|
|
|
|
slave_get_self(g);
|
|
|
|
|
slave_adds(g);
|
2023-12-20 18:27:37 +08:00
|
|
|
|
if(ret_mode!=3){
|
|
|
|
|
check=elec_check_with_scheme(0);
|
|
|
|
|
}else{
|
|
|
|
|
check=elec_check_slot();
|
|
|
|
|
}
|
|
|
|
|
if(ret_mode==0){
|
|
|
|
|
// 模式0,只返回检测数据
|
2023-12-21 18:45:48 +08:00
|
|
|
|
slave_creat_retdata(s,0,&slot_exe_flag);
|
|
|
|
|
SET_ERRCODE(check);
|
2023-12-20 18:27:37 +08:00
|
|
|
|
emit tran_reply_signal(u->p,arr_temp(check));
|
|
|
|
|
}else if((ret_mode==1)||(ret_mode==3)){
|
|
|
|
|
// 模式1,返回异常代码和任务插槽数据
|
|
|
|
|
r=arr_creat();
|
|
|
|
|
arr_append(r,arr_get(check,0));
|
2023-12-21 18:45:48 +08:00
|
|
|
|
slave_creat_retdata(s,r,&slot_exe_flag);
|
|
|
|
|
SET_ERRCODE(r);
|
2023-12-20 18:27:37 +08:00
|
|
|
|
emit tran_reply_signal(u->p,arr_temp(r));
|
|
|
|
|
}else if(ret_mode==2){
|
|
|
|
|
// 模式2,返回检测数据和任务插槽数据
|
2023-12-21 18:45:48 +08:00
|
|
|
|
slave_creat_retdata(s,check,&slot_exe_flag);
|
|
|
|
|
SET_ERRCODE(check);
|
2023-12-20 18:27:37 +08:00
|
|
|
|
emit tran_reply_signal(u->p,arr_temp(check));
|
|
|
|
|
}
|
2023-12-18 18:17:21 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
static ucport_def *run_task(tran_def *t, uint8_t cmd,array_def *data)
|
|
|
|
|
{
|
|
|
|
|
int ret=0;
|
|
|
|
|
slave_def *u=slave_creat();
|
|
|
|
|
u->u.p=t;
|
|
|
|
|
u->u.del=slave_del;
|
|
|
|
|
u->u.dolater=run_task_dolater;
|
|
|
|
|
u->u.dolater(&u->u,cmd,data,"ok");
|
|
|
|
|
return (ucport_def *)u;
|
|
|
|
|
}
|
|
|
|
|
transmit_export(ym_slave,0x20,run_task)
|
2023-12-25 18:46:51 +08:00
|
|
|
|
// 0x21 是上位机协议的命令字
|
|
|
|
|
transmit_export(ym_checker,0x21,run_task)
|
2023-12-18 18:17:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|