g0b1vetx-board/Board/eth-board.c
2023-05-19 18:02:00 +08:00

384 lines
7.4 KiB
C

#include "eth-board.h"
#include "usart.h"
#include "proto-sl651.h"
#include <string.h>
// 尝试创建Socket
const char
void EthMqttTick (void)
{
if (s_tx_timeout > 1)
s_tx_timeout--;
if (s_run_timer > 1)
s_run_timer--;
if (s_lost_timer > 1)
s_lost_timer--;
}
//void EthMqttReboot(void)
//{
// DelayMs(500);
// HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
//}
static enum
{
DeviceReboot,
DeviceConfig,
DeviceGetPara,
DeviceSetPara,
DeviceReset,
DeviceLink,
DeviceIdle,
} DeviceState = DeviceReboot ;
static enum
{
FistIn,
Runing,
End,
JumpUp
} s_run = FistIn;
// NetFunc.Init = EthMqttInit;
// NetFunc.IsConnected = EthMqttIsConnect;
// NetFunc.IsReady = EthMqttIsReady;
// NetFunc.ReadData = EthMqttReadData;
// NetFunc.WriteData = EthMqttWriteData;
// NetFunc.IsIdle = EthMqttIsIdle;
// NetFunc.ReSubTopic = EthMqttReSubTopic;
// NetFunc.Task = EthMqttTask;
void EthMqttInit (void)
{
// char str[128];
// char addr_str[10];
// GPIO_InitTypeDef GPIO_InitStruct;
// GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
// GPIO_InitStruct.Pull = GPIO_PULLUP;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
// GPIO_InitStruct.Pin = GPIO_PIN_1;
// HAL_GPIO_Init (GPIOB, &GPIO_InitStruct);
// HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLUP;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
// // 状态
// GPIO_InitStruct.Pin = GPIO_PIN_4;
// HAL_GPIO_Init (GPIOC, &GPIO_InitStruct);
// memset(addr_str,0,10);
// hexbyte_to_hex_str((char *)SysInfo.Addr,addr_str,4);
// UartInfoInit(uEth); // 初始化以太网接口
// memset(sub_topic,0,64);
// sprintf(sub_topic,"device/sluice/tx/22000073%s",addr_str);
// memset(pub_topic,0,64);
// sprintf(pub_topic,"device/sluice/rx/22000073%s",addr_str);
// s_tx_timeout = 1;
}
// // 上线
// void EthMqttConnectd (void)
// {
// s_isconnect = 1;
// //DisWriteNetSte (1, QUEUE);
// s_lost_timer = 60000;
// dbg_printf("Eth Device Isconnected \r\n");
// }
// 掉线
void EthMqttDisConnectd (void)
{
s_lost_timer = 0;
DisWriteNetSte (2, QUEUE);
DeviceState = DeviceReboot;
dbg_printf ("Eth Device IsDisconnected \r\n");
}
int EthMqttReadData (char * buf, int length)
{
int k = 0;
if (UartInfoRxIsReady (uEth) == 1)
{
k = UartInfoReadData (uEth, (char *) buf, length);
dbg_printf ("<<<<<<<<<<<<%d\r\n", k);
s_lost_timer = 60000;
DisWriteNetSte (1, QUEUE);
}
return k;
}
int EthMqttIsReady (void)
{
if (DeviceState != DeviceIdle)
{
return 0;
}
else
{
return UartInfoRxIsReady (uEth);
}
}
void EthMqttWriteData (char * data, int length)
{
UartInfoWriteData (uEth, data, length);
}
int EthMqttIsIdle (void)
{
if (EthMqttIsConnect() == 0)
{
return 0;
}
if (DeviceState == DeviceIdle)
return 1;
return 0;
}
int EthMqttIsConnect (void)
{
if (HAL_GPIO_ReadPin (GPIOC, GPIO_PIN_4) == GPIO_PIN_SET)
return 1;
else
return 0;
//return s_isconnect;
}
void EthMqttTask (void)
{
int rx_size;
char str[128];
//static pst_Work_Pragma pstWork_Pragma;
switch (DeviceState)
{
case DeviceReboot:
{
if (s_run == FistIn)
{
dbg_printf ("EthDeviceReboot\r\n");
HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
s_run_timer = 500;
s_run = Runing;
}
else if (s_run == Runing)
{
if (s_run_timer == 1)
{
s_run_timer = 0;
s_run = End;
HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
dbg_printf ("Reboot Finsh\r\n");
s_run_timer = 5000;
}
}
else if (s_run == End)
{
if (s_run_timer == 1)
{
//dbg_printf("Reboot Finsh\r\n");
s_run = JumpUp;
s_run_timer = 0;
}
}
else
{
s_run = FistIn ;
DeviceState = DeviceConfig;
}
break;
}
// 这里要等待3s
case DeviceConfig: // 进入配置模式
{
if (s_run == FistIn)
{
dbg_printf ("DeviceConfig\r\n");
UartInfoClearData (uEth) ;
UartInfoWriteData (uEth, "++++++++++", 10);
s_run_timer = 50;
s_run = Runing;
}
else if (s_run == Runing)
{
if (s_run_timer == 1)
{
s_run_timer = 0;
UartInfoWriteData (uEth, "{\"Command\":\"config\"}", strlen ("{\"Command\":\"config\"}"));
dbg_printf ("Command\r\n");
s_run = End;
s_run_timer = 3000;
}
// 获取发送的数据
}
else if (s_run == End)
{
if (s_run_timer == 1)
{
s_run = JumpUp;
s_run_timer = 0;
}
}
else
{
s_run = FistIn;
DeviceState = DeviceGetPara;
}
break;
}
case DeviceGetPara:
{
if (UartInfoRxIsReady (uEth) == 1)
{
dbg_printf ("DeviceGetPara\r\n");
rx_size = UartInfoReadData (uEth, (char *) s_buffer, 1024);
dbg_printf ("(%d)[%d]\r\n", rx_size, s_buffer[0]) ;
DeviceState = DeviceSetPara;
}
break;
}
case DeviceSetPara:
{
if (s_run == FistIn)
{
memset (s_buffer, 0, 1024);
//dbg_printf("subtopic:%s\r\n",(char *)pstWork_Pragma->stExternPragma.stMQTTClient.SubTopic);
sprintf (s_buffer,
"{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\"}",
"Command", "config",
"RemoteHost", SysInfo.remoteAddr,
"RemotePort", SysInfo.remotePort,
"WorkMode", "MQTT",
"UserName", SysInfo.remoteuser,
"Password", SysInfo.remotepass,
"PubTopic", pub_topic,
"SubTopic", sub_topic
);
//SetParametersToData(pstWork_Pragma,(char *)s_buffer,1024);
dbg_printf ("%s\r\n", s_buffer);
UartInfoWriteData (uEth, s_buffer, strlen (s_buffer));
s_run_timer = 1000;
s_run = Runing;
}
else if (s_run == Runing)
{
if (s_run_timer == 1)
{
s_run_timer = 0;
UartInfoWriteData (uEth, "{\"Command\":\"exit\"}", strlen ("{\"Command\":\"exit\"}"));
dbg_printf ("Command\r\n");
s_run = End;
s_run_timer = 50;
}
// 获取发送的数据
}
else if (s_run == End)
{
if (s_run_timer == 1)
{
s_run = JumpUp;
s_run_timer = 0;
}
}
else
{
s_run = FistIn;
DeviceState = DeviceReset;
}
break;
}
case DeviceReset:
{
if (s_run == FistIn)
{
HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
s_run_timer = 500;
s_run = Runing;
}
else if (s_run == Runing)
{
if (s_run_timer == 1)
{
s_run_timer = 0;
s_run = End;
HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
dbg_printf ("Reboot Finsh\r\n");
s_run_timer = 3000;
}
}
else if (s_run == End)
{
if (s_run_timer == 1)
{
dbg_printf ("Reboot Finsh\r\n");
UartInfoClearData (uEth) ;
s_run = JumpUp;
s_run_timer = 0;
}
}
else
{
s_run = FistIn ;
DeviceState = DeviceIdle;
}
break;
}
case DeviceLink:// 检查上线
{
s_isconnect = 1;
DeviceState = DeviceIdle;
break;
}
case DeviceIdle:
{
if (s_lost_timer == 0)
{
if (EthMqttIsConnect())
{
EthMqttConnectd();
s_lost_timer = 60000;
}
// 第一次上线
}
break;
}
}
if (s_lost_timer == 1)
{
EthMqttDisConnectd();
s_run = FistIn ;
}
}
int EthMqttReSubTopic (char * topic)
{
char addr_str[10];
memset (addr_str, 0, 10);
hexbyte_to_hex_str ( (char *) SysInfo.Addr, addr_str, 4);
memset (sub_topic, 0, 64);
sprintf (sub_topic, "device/%s/tx/22000073%s", topic, addr_str);
memset (pub_topic, 0, 64);
sprintf (pub_topic, "device/%s/rx/22000073%s", topic, addr_str);
dbg_printf ("EthMqttReSubTopic\r\n");
// memset(sub_topic,0,64);
// sprintf(sub_topic,"device/%s/tx/22000000%08x",topic,SysInfo.Addr);
// memset(pub_topic,0,64);
// sprintf(pub_topic,"device/%s/rx/22000000%08x",topic,SysInfo.Addr);
EthMqttDisConnectd();
}