123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- #include "lwip/opt.h"
- #include "lwip/tcp.h"
- #include "lwip/sys.h"
- #include "hello_gigadevice.h"
- #include <string.h>
- #include <stdio.h>
- #include "gd32f30x.h"
- #include "lwip/api.h"
- #define HELLO_TASK_PRIO (tskIDLE_PRIORITY + 5)
- #define MAX_NAME_SIZE 32
- #define GREETING "\n\r======= HelloGigaDevice =======\
- \n\r== GD32 ==\
- \n\r== Telnet SUCCESS==\
- \n\rHello. What is your name?\r\n"
- #define HELLO "\n\rGigaDevice¡¾8000¡¿PORT Hello "
- #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))
- static err_t hello_gigadevice_recv(struct netconn *conn, void *data, u16_t len);
- static err_t hello_gigadevice_accept(struct netconn *conn);
- static err_t hello_gigadevice_recv(struct netconn *conn, void *data, u16_t len)
- {
- int done;
- char *c;
- int i;
- done = 0;
- c = (char *)data;
-
- for (i = 0; i < len && !done; i++)
- {
- done = ((c[i] == '\r') || (c[i] == '\n'));
- }
- if (done)
- {
- if (c[len - 2] != '\r' || c[len - 1] != '\n')
- {
-
- if ((c[len - 1] == '\r' || c[len - 1] == '\n') && (len + 1 <= MAX_NAME_SIZE))
- {
-
- len += 1;
- }
- else if (len + 2 <= MAX_NAME_SIZE)
- {
- len += 2;
- }
- else
- {
- len = MAX_NAME_SIZE;
- }
-
- c[len - 2] = '\r';
- c[len - 1] = '\n';
- }
- netconn_write(conn, (void *)&HELLO, sizeof(HELLO), NETCONN_COPY);
- netconn_write(conn, data, len, NETCONN_COPY);
- }
- return ERR_OK;
- }
- static err_t hello_gigadevice_accept(struct netconn *conn)
- {
- u32_t ipaddress;
- u8_t iptxt[50];
- volatile u8_t iptab[4];
- struct tcp_pcb *pcb;
- pcb = conn->pcb.tcp;
- ipaddress = pcb->remote_ip.addr;
-
- iptab[0] = (u8_t)(ipaddress >> 24);
- iptab[1] = (u8_t)(ipaddress >> 16);
- iptab[2] = (u8_t)(ipaddress >> 8);
- iptab[3] = (u8_t)(ipaddress);
- sprintf((char *)iptxt, "Telnet:%d.%d.%d.%d ", iptab[3], iptab[2], iptab[1], iptab[0]);
-
- netconn_write(conn, (void *)&iptxt, sizeof(iptxt), NETCONN_COPY);
- netconn_write(conn, (void *)&GREETING, sizeof(GREETING), NETCONN_COPY);
- return ERR_OK;
- }
- static void hello_task(void *arg)
- {
- struct netconn *conn, *newconn;
- err_t err, accept_err;
- struct netbuf *buf;
- void *data;
- u16_t len;
- err_t recv_err;
- LWIP_UNUSED_ARG(arg);
- conn = netconn_new(NETCONN_TCP);
- if (NULL != conn)
- {
-
- err = netconn_bind(conn, NULL, 8000);
- if (ERR_OK == err)
- {
- netconn_listen(conn);
- while (1)
- {
-
- accept_err = netconn_accept(conn, &newconn);
-
- if (ERR_OK == accept_err)
- {
- hello_gigadevice_accept(newconn);
- recv_err = netconn_recv(newconn, &buf);
- while (ERR_OK == recv_err)
- {
- do
- {
- netbuf_data(buf, &data, &len);
- } while (netbuf_next(buf) >= 0);
- hello_gigadevice_recv(newconn, data, len);
- netbuf_delete(buf);
- recv_err = netconn_recv(newconn, &buf);
- }
-
- netconn_close(newconn);
- netconn_delete(newconn);
- }
- }
- }
- else
- {
- netconn_delete(newconn);
- printf(" can not bind TCP netconn");
- }
- }
- else
- {
- printf("can not create TCP netconn");
- }
- }
- #endif
- #if LWIP_SOCKET
- #include "lwip/sockets.h"
- struct recev_packet
- {
- int length;
- char bytes[MAX_NAME_SIZE];
- int done;
- } name_recv;
- static err_t hello_gigadevice_recv(int fd, void *data, int len);
- static err_t hello_gigadevice_recv(int fd, void *data, int len)
- {
- char *c;
- int i;
- int done;
- done = 0;
- c = (char *)data;
-
- for (i = 0; i < len && !done; i++)
- {
- done = ((c[i] == '\r') || (c[i] == '\n'));
- }
-
- if (0 == name_recv.done)
- {
-
- if (0 == done)
- {
- memcpy(name_recv.bytes, data, MAX_NAME_SIZE);
- name_recv.length = MAX_NAME_SIZE;
- name_recv.done = 1;
-
- }
- else
- {
- memcpy(name_recv.bytes, data, len);
- name_recv.length = len;
- }
- }
- if (1 == done)
- {
- if (c[len - 2] != '\r' || c[len - 1] != '\n')
- {
-
- if ((c[len - 1] == '\r' || c[len - 1] == '\n') && (len + 1 <= MAX_NAME_SIZE))
- {
-
- name_recv.length += 1;
- }
- else if (len + 2 <= MAX_NAME_SIZE)
- {
- name_recv.length += 2;
- }
- else
- {
- name_recv.length = MAX_NAME_SIZE;
- }
-
- name_recv.bytes[name_recv.length - 2] = '\r';
- name_recv.bytes[name_recv.length - 1] = '\n';
- }
- send(fd, (void *)&HELLO, sizeof(HELLO), 0);
- send(fd, name_recv.bytes, name_recv.length, 0);
- name_recv.done = 0;
- name_recv.length = 0;
- }
- return ERR_OK;
- }
- static void hello_task(void *arg)
- {
- int ret;
- int sockfd = -1, newfd = -1;
- uint32_t len;
- int tcp_port = 8000;
- int recvnum;
- struct sockaddr_in svr_addr, clt_addr;
- char buf[50];
-
- svr_addr.sin_family = AF_INET;
- svr_addr.sin_port = htons(tcp_port);
- svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
- name_recv.length = 0;
- name_recv.done = 0;
- while (1)
- {
-
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- {
- continue;
- }
- ret = bind(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
- if (ret < 0)
- {
- lwip_close(sockfd);
- sockfd = -1;
- continue;
- }
-
- ret = listen(sockfd, 1);
- if (ret < 0)
- {
- lwip_close(sockfd);
- continue;
- }
- len = sizeof(clt_addr);
-
- newfd = accept(sockfd, (struct sockaddr *)&clt_addr, (socklen_t *)&len);
- if (-1 != newfd)
- {
- send(newfd, (void *)&GREETING, sizeof(GREETING), 0);
- }
- while (-1 != newfd)
- {
-
- recvnum = recv(newfd, buf, MAX_NAME_SIZE, 0);
- if (recvnum <= 0)
- {
- lwip_close(newfd);
- newfd = -1;
- break;
- }
- hello_gigadevice_recv(newfd, buf, recvnum);
- }
- lwip_close(sockfd);
- sockfd = -1;
- }
- }
- #endif
- void hello_gigadevice_init(void)
- {
- xTaskCreate(hello_task, "HELLO", DEFAULT_THREAD_STACKSIZE, NULL, HELLO_TASK_PRIO, NULL);
- }
|