From a89b186f0fd1d1e69804524e2221d5a576e1be55 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 8 May 2025 09:21:31 +0800 Subject: [PATCH] add lora data tx/rx --- user/user_lora.c | 3 +++ user/user_tcp.c | 27 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/user/user_lora.c b/user/user_lora.c index 951000f..7aede13 100644 --- a/user/user_lora.c +++ b/user/user_lora.c @@ -85,6 +85,8 @@ int32_t lora_init( void ) return 0; } +extern int32_t user_tcp_write_lora_data( uint8_t *data, uint32_t data_sz ); + void user_lora_task(void *argument) { uint8_t buf[256]; @@ -100,6 +102,7 @@ void user_lora_task(void *argument) if( rx_n && rx_n == last_rx_n ){ memcpy( buf, rx_buf, rx_n ); //TODO: tcp tx data to host + user_tcp_write_lora_data( buf, last_rx_n ); last_rx_n = 0; rx_cnt = 0; }else{ diff --git a/user/user_tcp.c b/user/user_tcp.c index 1e59292..6d9f564 100644 --- a/user/user_tcp.c +++ b/user/user_tcp.c @@ -16,7 +16,7 @@ */ static err_t tcp_server_accept(void *arg, struct tcp_pcb *pcb, err_t err); static err_t tcp_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); -int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint16_t data_sz ); +static int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint32_t data_sz ); struct tcp_pcb *tcp_server_pcb; static struct tcp_pcb *tcp_connected_pcb = NULL; @@ -40,25 +40,30 @@ void Tcp_Server_Init(void) tcp_listen( tcp_server_pcb ); } -int32_t user_tcp_write_data( uint8_t *data, uint32_t sz ) +int32_t user_tcp_write_data( uint8_t *data, uint32_t data_sz ) { int32_t ret=-1; if( tcp_connected_pcb ){ - if( tcp_write( tcp_connected_pcb, data, sz, TCP_WRITE_FLAG_COPY ) == ERR_OK ) + if( tcp_write( tcp_connected_pcb, data, data_sz, TCP_WRITE_FLAG_COPY ) == ERR_OK ) ret = 0; } return ret; } -int32_t user_tcp_write_pkt( uint16_t cmd, uint8_t *data, uint16_t data_size ) +int32_t user_tcp_write_pkt( uint16_t cmd, uint8_t *data, uint32_t data_sz ) { uint8_t buf[256]; uint16_t n; tx_sn++; - n = wrap_pkt( buf, tx_sn, cmd, data, data_size ); + n = wrap_pkt( buf, tx_sn, cmd, data, data_sz ); return user_tcp_write_data( buf, n ); } +int32_t user_tcp_write_lora_data( uint8_t *data, uint32_t data_sz ) +{ + return user_tcp_write_pkt( 0x0104, data, data_sz ); +} + static err_t tcp_server_accept(void *arg, struct tcp_pcb *pcb, err_t err) { /* È·ÈϼàÌýÓëÁ¬½Ó */ @@ -95,7 +100,7 @@ CMD: */ static uint16_t rx_pkt_sn; -int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint16_t data_sz ) +int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint32_t data_sz ) { uint8_t *p = buf; uint16_t crc; @@ -119,6 +124,7 @@ int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint16 } extern void user_sensor_info_read( sensor_info_t *info ); +extern int lora_uart_tx( uint8_t *tx_data, uint16_t tx_size ); int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) { @@ -145,7 +151,7 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) pkt_data_sz = (p[6]<<8)|p[7]; //pkt_data = p+8; pkt_crc = (p[8+pkt_data_sz]<<8)|p[8+pkt_data_sz+1]; - if( recv_pkt_sn > rx_pkt_sn /*&& pkt_crc == usMBCRC16(p, 8+pkt_data_sz)*/ ){ + if( (uint16_t)(recv_pkt_sn - rx_pkt_sn) < 0x8FFF && pkt_crc == usMBCRC16(p, 8+pkt_data_sz) ){ if( tcp_connected_pcb != pcb ) tcp_connected_pcb = pcb; @@ -163,7 +169,12 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) memcpy( &resp_data[15], &sensor.pos.lon, 4 ); memset( &resp_data[19], 0, 13 ); resp_sz = wrap_pkt( resp, rx_pkt_sn, 0x0101, resp_data, 32 ); - break; + break; + case 0x0003: //lora transmit + lora_uart_tx( &p[8], pkt_data_sz ); + resp_data[0] = 1; + resp_sz = wrap_pkt( resp, rx_pkt_sn, 0x0103, resp_data, 1 ); + break; } if( resp_sz ){ tcp_write( pcb, resp, resp_sz, TCP_WRITE_FLAG_COPY ); -- 2.34.1