|
|
|
|
/*
|
|
|
|
|
* bsp_UDP.c
|
|
|
|
|
*
|
|
|
|
|
* Created on: Aug 13, 2024
|
|
|
|
|
* Author: akeguo
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "stm32h7xx_hal.h"
|
|
|
|
|
#include "lwip.h"
|
|
|
|
|
#include "udp.h"
|
|
|
|
|
#include "string.h"
|
|
|
|
|
#include "bsp_UDP.h"
|
|
|
|
|
#include "bsp_DLT_Log.h"
|
|
|
|
|
#include <bsp_UpperComputer_Handler.h>
|
|
|
|
|
#include "bsp_GPIO.h"
|
|
|
|
|
void UDP_GV_Dispatch();
|
|
|
|
|
void UDP_IV_Dispatch();
|
|
|
|
|
void udp_cmd_send_GV(char *pData, uint16_t Size);
|
|
|
|
|
/* 定义端口号 */
|
|
|
|
|
#define UDP_DLT_LOCAL_PORT 8000 /* 本地端口 */
|
|
|
|
|
#define UDP_CMD_LOCAL_PORT 8002 /* 本地端口 */
|
|
|
|
|
#define UDP_CMD_LOCAL_Android_Listen_PORT 8006 /* 本地端口 接收安卓 */
|
|
|
|
|
|
|
|
|
|
#define UDP_DLT_Send_LOCAL_PORT 7000 /* 本地端口 */
|
|
|
|
|
#define UDP_CMD_Send_LOCAL_PORT 7002 /* 本地端口 */
|
|
|
|
|
#define UDP_GV_Send_LOCAL_PORT 7004 /* 本地端口 */
|
|
|
|
|
#define UDP_Android_Send_LOCAL_PORT 7006 /* 本地端口 发给安卓*/
|
|
|
|
|
/*32从7006发往安卓9006*/
|
|
|
|
|
/*安卓从9006发往32的8006*/
|
|
|
|
|
#define UDP_REMOTE_DLT_PORT 9000 /* 远端端口 */
|
|
|
|
|
#define UDP_REMOTE_CMD_PORT 9002 /* 远端端口 */
|
|
|
|
|
#define UDP_REMOTE_GV_PORT 9004 /* 远端端口 */
|
|
|
|
|
#define UDP_REMOTE_Android_PORT 9006 /* 远端端口 */
|
|
|
|
|
|
|
|
|
|
int8_t is_udp_GV_update_loop_enalbed = 0;
|
|
|
|
|
unsigned char received_data[1000];
|
|
|
|
|
|
|
|
|
|
/* udp控制块 */
|
|
|
|
|
static struct udp_pcb *upcb_Rx_DLT;
|
|
|
|
|
static struct udp_pcb *upcb_Rx_Cmd;
|
|
|
|
|
static struct udp_pcb *upcb_Rx_From_Android;
|
|
|
|
|
|
|
|
|
|
static struct udp_pcb *upcb_Tx_DLT;
|
|
|
|
|
static struct udp_pcb *upcb_Tx_Cmd;
|
|
|
|
|
static struct udp_pcb *upcb_Tx_GV;
|
|
|
|
|
static struct udp_pcb *upcb_Tx_Android;
|
|
|
|
|
|
|
|
|
|
ip_addr_t serverIP;
|
|
|
|
|
|
|
|
|
|
ip_addr_t Android_ServerIP;
|
|
|
|
|
|
|
|
|
|
DLT_DecodeFuncPtr UDP_DLT_ReceivedCallback;
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* 描述 : 接收回调函数
|
|
|
|
|
* 参数 : -
|
|
|
|
|
* 返回 : 无
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
static void udp_receive_callback(void *arg, struct udp_pcb *upcb,
|
|
|
|
|
struct pbuf *p, const ip_addr_t *addr, u16_t port)
|
|
|
|
|
{
|
|
|
|
|
if (p == NULL) return;
|
|
|
|
|
|
|
|
|
|
memcpy(received_data, p->payload, p->len);
|
|
|
|
|
|
|
|
|
|
if (upcb->local_port == UDP_DLT_LOCAL_PORT)
|
|
|
|
|
{
|
|
|
|
|
UDP_DLT_ReceivedCallback(received_data, p->len);
|
|
|
|
|
//DLT_DataReceiveEndCallback(received_data, p->len);
|
|
|
|
|
}
|
|
|
|
|
else if (upcb->local_port == UDP_CMD_LOCAL_PORT)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (*received_data == 0x55 && *(received_data + 1) == 0x55
|
|
|
|
|
&& p->len >= 3)
|
|
|
|
|
{
|
|
|
|
|
decode_command_and_feedback(received_data + 2, p->len - 2, 2,
|
|
|
|
|
NULL); //0代表UDP
|
|
|
|
|
}
|
|
|
|
|
else if (*received_data == 0x66 && *(received_data + 1) == 0x66
|
|
|
|
|
&& p->len >= 3)
|
|
|
|
|
{
|
|
|
|
|
GF_BSP_GPIO_SetIO(received_data[3], received_data[2]);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_cmd_send_back(received_data, p->len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (upcb->local_port == UDP_CMD_LOCAL_Android_Listen_PORT)
|
|
|
|
|
{
|
|
|
|
|
//对PV数据进行修改
|
|
|
|
|
//if (*buffer == 0x55 && *(buffer + 1) == 0x55 && length >= 4)
|
|
|
|
|
if (received_data[0] != 0x55 || received_data[1] != 0x55 || p->len < 4)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t crc_check = ((received_data[p->len - 1] << 8)
|
|
|
|
|
| received_data[p->len - 2]);
|
|
|
|
|
uint16_t crc_check1 = MB_CRC16(received_data, p->len - 2);
|
|
|
|
|
/* CRC 校验正确 */
|
|
|
|
|
if (crc_check != MB_CRC16(received_data, p->len - 2)) return;
|
|
|
|
|
|
|
|
|
|
if (received_data[2] != 0x01 || received_data[3] != 0x01) return;
|
|
|
|
|
|
|
|
|
|
pb_istream_t i_pv_stream =
|
|
|
|
|
{ 0 };
|
|
|
|
|
|
|
|
|
|
i_pv_stream = pb_istream_from_buffer(&received_data[4], p->len - 4);
|
|
|
|
|
pb_decode(&i_pv_stream, PV_struct_define_fields, &_decoded_PV_temp);
|
|
|
|
|
if (_decoded_PV_temp.TimeStamp > decoded_PV.TimeStamp)
|
|
|
|
|
{
|
|
|
|
|
decoded_PV = _decoded_PV_temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
pbuf_free(p);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//char UDPprintf[100];
|
|
|
|
|
|
|
|
|
|
void udp_dlt_send_back(char *pData, uint16_t Size)
|
|
|
|
|
{
|
|
|
|
|
struct pbuf *p;
|
|
|
|
|
|
|
|
|
|
/* 分配缓冲区空间 */
|
|
|
|
|
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_RAM);
|
|
|
|
|
if (p != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 填充缓冲区数据 */
|
|
|
|
|
pbuf_take(p, pData, Size);
|
|
|
|
|
|
|
|
|
|
/* 发送udp数据 */
|
|
|
|
|
|
|
|
|
|
//udp_send(upcb_DLT, p);
|
|
|
|
|
err_t t = udp_sendto(upcb_Tx_DLT, p, &serverIP,
|
|
|
|
|
UDP_REMOTE_DLT_PORT);
|
|
|
|
|
/* 释放缓冲区空间 */
|
|
|
|
|
pbuf_free(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void udp_cmd_send_back(char *pData, uint16_t Size)
|
|
|
|
|
{
|
|
|
|
|
struct pbuf *p;
|
|
|
|
|
|
|
|
|
|
/* 分配缓冲区空间 */
|
|
|
|
|
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_RAM);
|
|
|
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 填充缓冲区数据 */
|
|
|
|
|
pbuf_take(p, pData, Size);
|
|
|
|
|
|
|
|
|
|
/* 发送udp数据 */
|
|
|
|
|
err_t err = udp_send(upcb_Tx_Cmd, p);
|
|
|
|
|
if (err != ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
// 发送失败
|
|
|
|
|
printf("Failed to send UDP data: %d\n", err);
|
|
|
|
|
}
|
|
|
|
|
/* 释放缓冲区空间 */
|
|
|
|
|
pbuf_free(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void udp_cmd_send_GV(char *pData, uint16_t Size)
|
|
|
|
|
{
|
|
|
|
|
struct pbuf *p;
|
|
|
|
|
|
|
|
|
|
/* 分配缓冲区空间 */
|
|
|
|
|
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_RAM);
|
|
|
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 填充缓冲区数据 */
|
|
|
|
|
pbuf_take(p, pData, Size);
|
|
|
|
|
|
|
|
|
|
/* 发送udp数据 */
|
|
|
|
|
err_t err = udp_send(upcb_Tx_GV, p);
|
|
|
|
|
if (err != ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
// 发送失败
|
|
|
|
|
//printf("Failed to send UDP data: %d\n", err);
|
|
|
|
|
}
|
|
|
|
|
/* 释放缓冲区空间 */
|
|
|
|
|
pbuf_free(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void udp_send_by_pcb(struct udp_pcb *upcb, char *pData, uint16_t Size)
|
|
|
|
|
{
|
|
|
|
|
struct pbuf *p;
|
|
|
|
|
|
|
|
|
|
/* 分配缓冲区空间 */
|
|
|
|
|
p = pbuf_alloc(PBUF_TRANSPORT, Size, PBUF_RAM);
|
|
|
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 填充缓冲区数据 */
|
|
|
|
|
pbuf_take(p, pData, Size);
|
|
|
|
|
|
|
|
|
|
/* 发送udp数据 */
|
|
|
|
|
err_t err = udp_send(upcb, p);
|
|
|
|
|
if (err != ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
// 发送失败
|
|
|
|
|
//printf("Failed to send UDP data: %d\n", err);
|
|
|
|
|
}
|
|
|
|
|
/* 释放缓冲区空间 */
|
|
|
|
|
pbuf_free(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* 描述 : 创建udp客户端
|
|
|
|
|
* 参数 : 无
|
|
|
|
|
* 返回 : 无
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
void udp_client_init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
GF_BSP_Interrupt_Add_CallBack(
|
|
|
|
|
DF_BSP_InterCall_TIM8_2ms_PeriodElapsedCallback, UDP_GV_Dispatch);
|
|
|
|
|
//将UDP上传IV关闭
|
|
|
|
|
GF_BSP_Interrupt_Add_CallBack(
|
|
|
|
|
DF_BSP_InterCall_TIM8_2ms_PeriodElapsedCallback, UDP_IV_Dispatch);
|
|
|
|
|
//Timer intialize();
|
|
|
|
|
err_t err;
|
|
|
|
|
|
|
|
|
|
IP4_ADDR(&serverIP, 192, 168, 144, 255);
|
|
|
|
|
IP4_ADDR(&Android_ServerIP, 192, 168, 144, 20);
|
|
|
|
|
|
|
|
|
|
/* 创建udp控制块 */
|
|
|
|
|
upcb_Rx_DLT = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Rx_DLT != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
//upcb_DLT->local_port = UDP_DLT_LOCAL_PORT;
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_DLT->so_options |= SOF_BROADCAST;
|
|
|
|
|
//err = udp_connect(upcb_DLT, &serverIP, UDP_REMOTE_DLT_PORT);
|
|
|
|
|
err = udp_bind(upcb_Rx_DLT, IP_ADDR_ANY, UDP_DLT_LOCAL_PORT);
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
udp_recv(upcb_Rx_DLT, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Rx_DLT);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upcb_Rx_From_Android = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Rx_From_Android != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
//upcb_DLT->local_port = UDP_DLT_LOCAL_PORT;
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_DLT->so_options |= SOF_BROADCAST;
|
|
|
|
|
//err = udp_connect(upcb_DLT, &serverIP, UDP_REMOTE_DLT_PORT);
|
|
|
|
|
err = udp_bind(upcb_Rx_From_Android, IP_ADDR_ANY,
|
|
|
|
|
UDP_CMD_LOCAL_Android_Listen_PORT);
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
udp_recv(upcb_Rx_From_Android, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Rx_From_Android);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upcb_Rx_Cmd = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Rx_Cmd != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
//upcb_Cmd->local_port = UDP_CMD_LOCAL_PORT;
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
|
|
|
//err = udp_connect(upcb_Cmd, &serverIP, UDP_REMOTE_CMD_PORT);
|
|
|
|
|
err = udp_bind(upcb_Rx_Cmd, IP_ADDR_ANY, UDP_CMD_LOCAL_PORT);
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
udp_recv(upcb_Rx_Cmd, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Rx_Cmd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upcb_Tx_Cmd = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Tx_Cmd != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
upcb_Tx_Cmd->local_port = UDP_CMD_Send_LOCAL_PORT;
|
|
|
|
|
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
|
|
|
err = udp_connect(upcb_Tx_Cmd, &serverIP, UDP_REMOTE_CMD_PORT);
|
|
|
|
|
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
//udp_recv(upcb_Tx_Cmd, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Tx_Cmd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
upcb_Tx_DLT = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Tx_DLT != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
upcb_Tx_Cmd->local_port = UDP_DLT_Send_LOCAL_PORT;
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
|
|
|
err = udp_connect(upcb_Tx_DLT, &serverIP, UDP_REMOTE_DLT_PORT);
|
|
|
|
|
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
//udp_recv(upcb_Tx_Cmd, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Tx_DLT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upcb_Tx_GV = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Tx_GV != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
upcb_Tx_Cmd->local_port = UDP_GV_Send_LOCAL_PORT;
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
|
|
|
err = udp_connect(upcb_Tx_GV, &serverIP, UDP_REMOTE_GV_PORT);
|
|
|
|
|
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
//udp_recv(upcb_Tx_Cmd, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Tx_GV);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upcb_Tx_Android = udp_new();
|
|
|
|
|
|
|
|
|
|
if (upcb_Tx_Android != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* 配置本地端口 */
|
|
|
|
|
upcb_Tx_Android->local_port = UDP_Android_Send_LOCAL_PORT;
|
|
|
|
|
/* 配置服务器IP和端口 */
|
|
|
|
|
//upcb_Cmd->so_options |= SOF_BROADCAST;
|
|
|
|
|
err = udp_connect(upcb_Tx_Android, &Android_ServerIP,
|
|
|
|
|
UDP_REMOTE_Android_PORT);
|
|
|
|
|
|
|
|
|
|
if (err == ERR_OK)
|
|
|
|
|
{
|
|
|
|
|
/* 注册接收回调函数 */
|
|
|
|
|
//udp_recv(upcb_Tx_Cmd, udp_receive_callback, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
udp_remove(upcb_Tx_Android);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
int counter_index = 0;
|
|
|
|
|
int iv_counter_index = 0;
|
|
|
|
|
void UDP_GV_Dispatch()
|
|
|
|
|
{
|
|
|
|
|
counter_index++;
|
|
|
|
|
if (counter_index <= 50) return;
|
|
|
|
|
if (is_udp_GV_update_loop_enalbed == 0) return;
|
|
|
|
|
counter_index = 0;
|
|
|
|
|
pb_ostream_t GV_o_stream =
|
|
|
|
|
{ 0 };
|
|
|
|
|
char buf[1024];
|
|
|
|
|
GV_o_stream = pb_ostream_from_buffer(&buf[2], sizeof(buf));
|
|
|
|
|
pb_encode(&GV_o_stream, GV_struct_define_fields, &GV);
|
|
|
|
|
buf[1] = 0xfe;
|
|
|
|
|
buf[0] = 0xfe;
|
|
|
|
|
//memcpy(&wh_LTE_7S0_Handler->Tx_Buf[2], buf, GV_o_stream.bytes_written);
|
|
|
|
|
udp_cmd_send_GV(buf, GV_o_stream.bytes_written + 2);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//现在是每一百ms执行一次
|
|
|
|
|
void UDP_IV_Dispatch()
|
|
|
|
|
{
|
|
|
|
|
iv_counter_index++;
|
|
|
|
|
if (iv_counter_index <= 50) return;
|
|
|
|
|
iv_counter_index = 0;
|
|
|
|
|
char Tx_Buf[1024];
|
|
|
|
|
pb_ostream_t IV_o_stream = pb_ostream_from_buffer(&Tx_Buf[2],
|
|
|
|
|
sizeof(Tx_Buf) - 2);
|
|
|
|
|
pb_encode(&IV_o_stream, IV_struct_define_fields, &IV);
|
|
|
|
|
|
|
|
|
|
Tx_Buf[0] = 0x55;
|
|
|
|
|
Tx_Buf[1] = 0x55;
|
|
|
|
|
int32_t TxCount = IV_o_stream.bytes_written + 4;
|
|
|
|
|
uint16_t crc = MB_CRC16(Tx_Buf, IV_o_stream.bytes_written + 2);
|
|
|
|
|
|
|
|
|
|
Tx_Buf[IV_o_stream.bytes_written + 2] = (crc >> 8) & 0xff;
|
|
|
|
|
Tx_Buf[IV_o_stream.bytes_written + 3] = crc & 0xff;
|
|
|
|
|
|
|
|
|
|
udp_send_by_pcb(upcb_Tx_Android, Tx_Buf, IV_o_stream.bytes_written + 4);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************** END OF FILE ********************************/
|
|
|
|
|
|