Yunhorn_STS_E1/Src/user_data_process.c

205 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stdint.h"
#include "string.h"
#include "stdio.h"
#include "stm32f0xx_ll_gpio.h"
#include "lora.h"
#include "user_tim.h"
#include "user_flash.h"
#include "user_data_send.h"
#include "user_data_process.h"
extern volatile uint8_t LORA_STATE;
extern volatile uint8_t co_threshold;
struct recData_process {
const char *recData;
void (*function) (int argc, char *argv[]);
};
static void lora_state (int argc, char *argv[]);
static void lora_recv(int argc, char *argv[]);
struct recData_process recData_processes[] = {
/*lora status check*/
"\r\n+STATUS:", lora_state,
"\r\n^STATUS:", lora_state,
"^STATUS:", lora_state,
"\r\n^LRRECV:", lora_recv,
};
/*******************************************************************************
**lora_state(int argc, char *argv[])
**功能描述:
**入口参数int argc
char *argv[]
**输出:无
*******************************************************************************/
static void lora_state(int argc, char *argv[])
{
switch(argv[0][strlen(recData_processes[argc].recData)]-0x30)
{
case LORA_RESET:
LORA_STATE = LORA_RESET;
StopTIM(TIM14);
break;
case LORA_P2P:
LORA_STATE = LORA_P2P;
StopTIM(TIM14);
break;
case LORA_NOT_JOIN:
LORA_STATE = LORA_NOT_JOIN;
StopTIM(TIM14);
break;
case LORA_JOINED:
LORA_STATE = LORA_JOINED;
StartTIM(TIM14);
break;
case LORA_JOINED_ABP:
LORA_STATE = LORA_JOINED;
StartTIM(TIM14);
break;
default :
break;
}
}
static void recv_data_process(int len, char *hexdata)
{
char errbuf[3]="PXX", charstr[64]="";
uint8_t err_flag = 0;
hex2str(hexdata,charstr);
#ifdef DEBUG
printf("\r\n recv_data: len=%d data=%s\r\n", len, charstr);
#endif
if(len == 1)
{
switch(charstr[0]-0x30)
{
case LEVEL_0:
save_config(DISABLE);
break;
case LEVEL_1:
save_config(ENABLE);
break;
default :
err_flag = 1;
break;
}
if (!err_flag) SendHbData(2, read_config());
}
else if(len == 3)
{
char rx[32] = "";
uint8_t ih, il;
memset(rx,NULL,sizeof(rx));
hex2str(hexdata,rx);
switch(charstr[0])
{
case 'P':
ih = (uint8_t)rx[1];
il = (uint8_t)rx[2];
if ((ih >= '0') && (ih <='9') && (il >='0') && (il<='9'))
{
co_threshold = ((ih-0x30)*10+(il-0x30))&0xff;
save_co_threshold((uint8_t)co_threshold);
SendCmdReplyData((uint8_t *)rx);
}
else
{
err_flag=1;
}
break;
default:
err_flag =1;
break;
}
} else err_flag=1;
if (err_flag)
SendCmdReplyData((uint8_t*)errbuf);
}
/*******************************************************************************
**函数名称lora_recv(int argc, char *argv[])
**功能描述:
**入口参数int argc
char *argv[]
**输出:无
*******************************************************************************/
static void lora_recv(int argc, char *argv[])
{
int ret,seq,port,rssi,snr,len;
char hexdata[64] = "";
ret = sscanf(argv[0], "\r\n^LRRECV:%d,%d,-%d,%d,%d,<%s", &seq, &port, &rssi, &snr, &len, hexdata);
if(ret != 6)
{
return;
}
else
{
recv_data_process(len, hexdata);
}
}
/*******************************************************************************
**函数名称data_process(char *str)
**功能描述处理lora模块接收的数据
**入口参数char *str 要处理的数据
**输出:无
*******************************************************************************/
void data_process(char *str)
{
int i;
int argc;
char* argv[5]={NULL};
argv[0] = str;
for (i = 0; i < sizeof(recData_processes)/sizeof(struct recData_process); i++)
{
if(strncmp(str, recData_processes[i].recData,8) == 0)
{
argc = i;
recData_processes[i].function(argc, argv);
break;
}
}
}
static int hex2int(char c)
{
if(c >= '0' && c <= '9'){
return (c-'0');
}
else if (c >= 'A' && c<= 'Z'){
return (c-'A');
}
else if (c >= 'a' && c <= 'z'){
return (c-'a');
}
else {
return 0;
}
}
void hex2str(char* hex , char* str )
{
uint8_t i = 0;
size_t j = 0;
uint8_t a,b;
for (j = 0; j < strlen(hex); j++)
{
a = hex2int(hex[i++]);
b = hex2int(hex[i++]);
str[j] = a*16+b;
}
str[j] = '\0';
}