123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #include "lwip/opt.h"
- #include "lwip/tcp.h"
- #include "lwip/sys.h"
- #include "udp.h"
- #include <string.h>
- #include <stdio.h>
- #include "gd32f30x.h"
- #include "lwip/memp.h"
- #include "main.h"
- #include "lwip/api.h"
- #define UDP_TASK_PRIO ( tskIDLE_PRIORITY + 5)
- #define MAX_BUF_SIZE 50
- #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))
- struct recev_packet
- {
- int length;
- char bytes[MAX_BUF_SIZE];
- };
- static err_t udp_echo_recv(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port);
-
- static err_t udp_echo_recv(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port)
- {
- struct pbuf *q;
- struct recev_packet recev_packet;
- uint32_t data_len = 0;
- struct netbuf *udpbuf;
-
- if(NULL != buf){
-
- for(q = buf->p; q != NULL; q = q->next){
-
- if(q->len > (MAX_BUF_SIZE - data_len)){
-
- memcpy((recev_packet.bytes + data_len), q->payload, (MAX_BUF_SIZE - data_len));
- data_len = MAX_BUF_SIZE;
- }else{
-
- memcpy((recev_packet.bytes + data_len), q->payload, q->len);
- data_len += q->len;
- }
-
- if(data_len >= MAX_BUF_SIZE){
- break;
- }
- }
-
- recev_packet.length = data_len;
- udpbuf = netbuf_new();
- netbuf_ref(udpbuf, recev_packet.bytes, recev_packet.length);
- netconn_sendto(conn, udpbuf, addr, port);
- netbuf_delete(udpbuf);
- }
-
- return ERR_OK;
- }
- static void udp_task(void *arg)
- {
- struct netconn *conn;
- struct ip_addr ipaddr;
- struct netbuf *buf;
- err_t recv_err;
- IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
-
-
- conn = netconn_new(NETCONN_UDP);
- netconn_bind(conn, IP_ADDR_ANY, 1025);
-
- while(1){
- recv_err = netconn_recv(conn, &buf);
- while (ERR_OK == recv_err){
- udp_echo_recv(conn, buf, &ipaddr, 1025);
- netbuf_delete(buf);
- recv_err = netconn_recv(conn, &buf);
- }
-
-
- netconn_close(conn);
- netconn_delete(conn);
- }
- }
- #endif
- #include "lwip/sockets.h"
- volatile int8_t sockfd=-1;
- #define UDP_LOCAL_PORT 12345
- #define UDP_REMOTE_PORT 54321
- #define SERCER_IP_ADDRESS "192.168.2.22"
- struct sockaddr_in remote_addr;
- int udp_log_start()
- {
- int ret;
- struct sockaddr_in local_addr;
-
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < -1) {
- perror("Error creating socket");
- goto exit;
- }
- local_addr.sin_family=AF_INET;
- local_addr.sin_port=htons(UDP_LOCAL_PORT);
- local_addr.sin_addr.s_addr=INADDR_ANY;
- ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
- if(ret < 0)
- {
- lwip_close(sockfd);
- sockfd = -1;
- goto exit;
- }
-
- remote_addr.sin_family=AF_INET;
- remote_addr.sin_port=htons(UDP_REMOTE_PORT);
- inet_aton(SERCER_IP_ADDRESS,&(remote_addr.sin_addr));
- return 1;
-
- exit:
- return -1;
- }
- void udp_log_close()
- {
- lwip_close(sockfd);
- sockfd=-1;
- }
- int fputc(int ch, FILE *f)
- {
- if(sockfd>=0)
- {
- sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
- return ch;
- }
- }
- int udp_send_printf(char *p)
- {
- if(sockfd>=0)
- {
- sendto(sockfd, p, strlen(p), 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
- }
- }
|