pile_com_stm32/main/communication_tcp.c

193 lines
6.0 KiB
C
Raw Normal View History

2024-03-15 17:42:04 +08:00
#include <stdint.h>
#include <string.h>
#include "lwip/ip_addr.h"
#include "lwip/tcp.h"
#include "lwip/err.h"
#include "lwip/opt.h"
#include "lwip/sockets.h"
#include "stm32/ModbusS.h"
#include "esp_log.h"
#define LOG_TAG "[communication_tcp_sr]: "
bool tcp_is_connected = false;
extern bool data_notify_enable; /* 是否允许数据上报 */
extern void gatts_write_data_cb(uint8_t *data, uint32_t len);
static err_t ModBusSlave_recv(void *arg, struct tcp_pcb *newpcb, struct pbuf *p, err_t err) {
if (p != NULL) {
uint8_t *rxbuf;
int rxlen;
tcp_recved(newpcb, p->tot_len);
rxbuf = (uint8_t *)p->payload;
rxlen = p->len;
ESP_LOGI(LOG_TAG, "recive data %d:", rxlen);
esp_log_buffer_hex(LOG_TAG, rxbuf, rxlen);
gatts_write_data_cb(rxbuf, rxlen);
pbuf_free(p);
}
else if (err == ERR_OK)
{
ESP_LOGI(LOG_TAG, "gps1 remote end is closing the connection\n");
for (uint8_t i = 0; i < 1; i++)
{ // 清空所有客户端
gps1_acce_newpacb[i] = 0;
}
tcp_err(newpcb, NULL);
tcp_recv(newpcb, NULL);
tcp_poll(newpcb, NULL, 120);
err_t err_p = tcp_close(newpcb);
tcp_is_connected = false;
if (ERR_OK != err_p)
{
ESP_LOGI(LOG_TAG, "gps1 close error. err:%d\n", err_p);
}
return err_p;
}
return ERR_OK;
}
static void ModBusSlave_conn_err(void *arg, err_t err) {
// log_printf(LERROR, LOG_TAG "connection error, err:%d\n", err);
tcp_is_connected = false;
}
static err_t ModBusSlave_poll(void *arg, struct tcp_pcb *newpcb) {
// if (newpcb) {
// // log_printf(LERROR, LOG_TAG "close pcb\n");
// tcp_close(newpcb);
// newpcb = NULL;
// }
ESP_LOGI(LOG_TAG, "poll!");
return ERR_OK;
}
static err_t ModBusSlave_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
tcp_err(pcb, ModBusSlave_conn_err);
tcp_recv(pcb, ModBusSlave_recv);
tcp_poll(pcb, ModBusSlave_poll, 120); // 60 second timeout
/* Send out the first message */
// tcp_write(pcb, GREETING, strlen(GREETING), 1);
tcp_is_connected = true;
return ERR_OK;
}
static void notify_all_data(void)
{
ESP_LOGI(GATTS_TABLE_TAG, "notify_all_data");
uint8_t buf[1024];
buf[0] = 1; /* tag:深度:速度 时间 深度 桩号 */
buf[1] = 8;
buf[2] = depth_data->speed & 0xff;
buf[3] = (depth_data->speed >> 8) & 0xff;
buf[4] = depth_data->one_pile_work_time & 0xff;
buf[5] = (depth_data->one_pile_work_time >> 8) & 0xff;
buf[6] = depth_data->depth & 0xff;
buf[7] = (depth_data->depth >> 8) & 0xff;
buf[8] = gWordVar[LAST_PILE_ID_ADDR] & 0xff;
buf[9] = (gWordVar[LAST_PILE_ID_ADDR] >> 8) & 0xff;
buf[10] = 2; /* tag:1号通道流量:瞬时每10cm总 */
buf[11] = 6;
buf[12] = pflow[0].flow & 0xff;
buf[13] = (pflow[0].flow >> 8) & 0xff;
buf[14] = depth_data->depth_flow[0] & 0xff;
buf[15] = (depth_data->depth_flow[0] >> 8) & 0xff;
buf[16] = pflow[0].total_flow & 0xff;
buf[17] = (pflow[0].total_flow >> 8) & 0xff;
buf[18] = 3; /* tag:2号通道流量:瞬时每10cm总 */
buf[19] = 6;
buf[20] = pflow[1].flow & 0xff;
buf[21] = (pflow[1].flow >> 8) & 0xff;
buf[22] = depth_data->depth_flow[1] & 0xff;
buf[23] = (depth_data->depth_flow[1] >> 8) & 0xff;
buf[24] = pflow[1].total_flow & 0xff;
buf[25] = (pflow[1].total_flow >> 8) & 0xff;
buf[26] = 4; /* tag:电流:三个*/
buf[27] = 6;
buf[28] = gWordVar[AC_CURRENT_REG_ADDR + 0] & 0xff;
buf[29] = (gWordVar[AC_CURRENT_REG_ADDR + 0] >> 8) & 0xff;
buf[30] = gWordVar[AC_CURRENT_REG_ADDR + 1] & 0xff;
buf[31] = (gWordVar[AC_CURRENT_REG_ADDR + 1] >> 8) & 0xff;
buf[32] = gWordVar[AC_CURRENT_REG_ADDR + 2] & 0xff;
buf[33] = (gWordVar[AC_CURRENT_REG_ADDR + 2] >> 8) & 0xff;
int64_t x = (int64_t)(gpsMessageP->x * 1000);
int64_t y = (int64_t)(gpsMessageP->y * 1000);
uint16_t dir = (uint16_t)(gpsMessageP->dir * 100);
uint16_t pitch = (uint16_t)(gpsMessageP->pitch * 100);
uint8_t gps_status = gpsMessageP->gps_status;
uint32_t utc_time = gpsMessageP->utc;
// ESP_LOGI(GATTS_TABLE_TAG, "x:%f, %f, %lld", gpsMessageP->x, gpsMessageP->x * 1000, (long long int)(gpsMessageP->x * 1000));
// ESP_LOGI(GATTS_TABLE_TAG, "gps message x:%lld, y:%lld, dir:%ud, pitch:%ud, gps_status:%d, utc_time:%ud", (long long)x, (long long)y, (unsigned int)dir, (unsigned int)pitch, (int)gps_status, (unsigned int)utc_time);
buf[34] = 5; /* tag:CPS信息1:x, y(int64)*/
buf[35] = 16;
buf[36] = x & 0xff;
buf[37] = (x >> 8) & 0xff;
buf[38] = (x >> 16) & 0xff;
buf[39] = (x >> 24) & 0xff;
buf[40] = (x >> 32) & 0xff;
buf[41] = (x >> 40) & 0xff;
buf[42] = (x >> 48) & 0xff;
buf[43] = (x >> 56) & 0xff;
buf[44] = y & 0xff;
buf[45] = (y >> 8) & 0xff;
buf[56] = (y >> 16) & 0xff;
buf[47] = (y >> 24) & 0xff;
buf[48] = (y >> 32) & 0xff;
buf[49] = (y >> 40) & 0xff;
buf[50] = (y >> 48) & 0xff;
buf[51] = (y >> 56) & 0xff;
buf[52] = 6; /* tag:CPS信息2:方向,俯仰(uint16) gps定位状态(uint8) UTC时间(uint32)*/
buf[53] = 9;
buf[54] = dir & 0xff;
buf[55] = (dir >> 8) & 0xff;
buf[56] = pitch & 0xff;
buf[57] = (pitch >> 8) & 0xff;
buf[58] = utc_time & 0xff;
buf[59] = (utc_time >> 8) & 0xff;
buf[60] = (utc_time >> 16) & 0xff;
buf[61] = (utc_time >> 24) & 0xff;
buf[62] = gps_status & 0xff;
/* 需要上报什么数据在此处添加 */
// tcp_write(pcb, GREETING, strlen(GREETING), 1);
}
void notify_tcp_task(void *pvParameters)
{
ESP_LOGI(GATTS_TABLE_TAG, "notify_tcp_task");
while (1)
{
if (data_notify_enable && tcp_is_connected)
{
notify_all_data();
}
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelete(NULL);
}
void communication_tcp_init(void) {
struct tcp_pcb *pcb;
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 6061);
pcb = tcp_listen(pcb);
tcp_accept(pcb, ModBusSlave_accept);
xTaskCreate(notify_tcp_task, "notify_tcp_Task", 4096, NULL, 8, NULL);
}