hello_gigadevice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*!
  2. \file hello_gigadevice.c
  3. \brief TCP server demo program
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "lwip/opt.h"
  33. #include "lwip/tcp.h"
  34. #include "lwip/sys.h"
  35. #include "hello_gigadevice.h"
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include "gd32f30x.h"
  39. #include "lwip/api.h"
  40. #define HELLO_TASK_PRIO (tskIDLE_PRIORITY + 5)
  41. #define MAX_NAME_SIZE 32
  42. #define GREETING "\n\r======= HelloGigaDevice =======\
  43. \n\r== GD32 ==\
  44. \n\r== Telnet SUCCESS==\
  45. \n\rHello. What is your name?\r\n"
  46. #define HELLO "\n\rGigaDevice¡¾8000¡¿PORT Hello "
  47. #if ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1))
  48. static err_t hello_gigadevice_recv(struct netconn *conn, void *data, u16_t len);
  49. static err_t hello_gigadevice_accept(struct netconn *conn);
  50. /*!
  51. \brief called when a data is received on the telnet connection
  52. \param[in] conn: the TCP netconn over which to send data
  53. \param[in] data: pointer to the data received
  54. \param[in] len: size of the data
  55. \param[out] none
  56. \retval err_t: error value
  57. */
  58. static err_t hello_gigadevice_recv(struct netconn *conn, void *data, u16_t len)
  59. {
  60. int done;
  61. char *c;
  62. int i;
  63. done = 0;
  64. c = (char *)data;
  65. /* a telnet communication packet is ended with an enter key */
  66. for (i = 0; i < len && !done; i++)
  67. {
  68. done = ((c[i] == '\r') || (c[i] == '\n'));
  69. }
  70. if (done)
  71. {
  72. if (c[len - 2] != '\r' || c[len - 1] != '\n')
  73. {
  74. /* limit the received data length to MAX_NAME_SIZE - 2('\r' and '\n' will be put into the buffer) */
  75. if ((c[len - 1] == '\r' || c[len - 1] == '\n') && (len + 1 <= MAX_NAME_SIZE))
  76. {
  77. /* calculate the buffer size to be sent(including '\r' and '\n') */
  78. len += 1;
  79. }
  80. else if (len + 2 <= MAX_NAME_SIZE)
  81. {
  82. len += 2;
  83. }
  84. else
  85. {
  86. len = MAX_NAME_SIZE;
  87. }
  88. /* save the received data to a new buffer */
  89. c[len - 2] = '\r';
  90. c[len - 1] = '\n';
  91. }
  92. netconn_write(conn, (void *)&HELLO, sizeof(HELLO), NETCONN_COPY);
  93. netconn_write(conn, data, len, NETCONN_COPY);
  94. }
  95. return ERR_OK;
  96. }
  97. /*!
  98. \brief this function when the telnet connection is established
  99. \param[in] conn: the TCP netconn over which to send data
  100. \param[out] none
  101. \retval err_t: error value
  102. */
  103. static err_t hello_gigadevice_accept(struct netconn *conn)
  104. {
  105. u32_t ipaddress;
  106. u8_t iptxt[50];
  107. volatile u8_t iptab[4];
  108. struct tcp_pcb *pcb;
  109. pcb = conn->pcb.tcp;
  110. ipaddress = pcb->remote_ip.addr;
  111. /* read its IP address */
  112. iptab[0] = (u8_t)(ipaddress >> 24);
  113. iptab[1] = (u8_t)(ipaddress >> 16);
  114. iptab[2] = (u8_t)(ipaddress >> 8);
  115. iptab[3] = (u8_t)(ipaddress);
  116. sprintf((char *)iptxt, "Telnet:%d.%d.%d.%d ", iptab[3], iptab[2], iptab[1], iptab[0]);
  117. /* send out the first message */
  118. netconn_write(conn, (void *)&iptxt, sizeof(iptxt), NETCONN_COPY);
  119. netconn_write(conn, (void *)&GREETING, sizeof(GREETING), NETCONN_COPY);
  120. return ERR_OK;
  121. }
  122. /*!
  123. \brief hello task
  124. \param[in] arg: user supplied argument
  125. \param[out] none
  126. \retval none
  127. */
  128. static void hello_task(void *arg)
  129. {
  130. struct netconn *conn, *newconn;
  131. err_t err, accept_err;
  132. struct netbuf *buf;
  133. void *data;
  134. u16_t len;
  135. err_t recv_err;
  136. LWIP_UNUSED_ARG(arg);
  137. conn = netconn_new(NETCONN_TCP);
  138. if (NULL != conn)
  139. {
  140. /* bind connection to well known port number 8000 */
  141. err = netconn_bind(conn, NULL, 8000);
  142. if (ERR_OK == err)
  143. {
  144. netconn_listen(conn);
  145. while (1)
  146. {
  147. /* grab new connection */
  148. accept_err = netconn_accept(conn, &newconn);
  149. /* process the new connection */
  150. if (ERR_OK == accept_err)
  151. {
  152. hello_gigadevice_accept(newconn);
  153. recv_err = netconn_recv(newconn, &buf);
  154. while (ERR_OK == recv_err)
  155. {
  156. do
  157. {
  158. netbuf_data(buf, &data, &len);
  159. } while (netbuf_next(buf) >= 0);
  160. hello_gigadevice_recv(newconn, data, len);
  161. netbuf_delete(buf);
  162. recv_err = netconn_recv(newconn, &buf);
  163. }
  164. /* close connection and discard connection identifier */
  165. netconn_close(newconn);
  166. netconn_delete(newconn);
  167. }
  168. }
  169. }
  170. else
  171. {
  172. netconn_delete(newconn);
  173. printf(" can not bind TCP netconn");
  174. }
  175. }
  176. else
  177. {
  178. printf("can not create TCP netconn");
  179. }
  180. }
  181. #endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */
  182. #if LWIP_SOCKET
  183. #include "lwip/sockets.h"
  184. struct recev_packet
  185. {
  186. int length;
  187. char bytes[MAX_NAME_SIZE];
  188. int done;
  189. } name_recv;
  190. static err_t hello_gigadevice_recv(int fd, void *data, int len);
  191. /*!
  192. \brief called when a data is received on the telnet connection
  193. \param[in] fd: the socket id which to send data
  194. \param[in] data: pointer to the data received
  195. \param[in] len: size of the data
  196. \param[out] none
  197. \retval err_t: error value
  198. */
  199. static err_t hello_gigadevice_recv(int fd, void *data, int len)
  200. {
  201. char *c;
  202. int i;
  203. int done;
  204. done = 0;
  205. c = (char *)data;
  206. /* a telnet communication packet is ended with an enter key,
  207. in socket, here is to check whether the last packet is complete */
  208. for (i = 0; i < len && !done; i++)
  209. {
  210. done = ((c[i] == '\r') || (c[i] == '\n'));
  211. }
  212. /* when the packet length received is no larger than MAX_NAME_SIZE */
  213. if (0 == name_recv.done)
  214. {
  215. /* havn't received the end of the packet, so the received data length
  216. is the configured socket reception limit--MAX_NAME_SIZE */
  217. if (0 == done)
  218. {
  219. memcpy(name_recv.bytes, data, MAX_NAME_SIZE);
  220. name_recv.length = MAX_NAME_SIZE;
  221. name_recv.done = 1;
  222. /* have received the end of the packet */
  223. }
  224. else
  225. {
  226. memcpy(name_recv.bytes, data, len);
  227. name_recv.length = len;
  228. }
  229. }
  230. if (1 == done)
  231. {
  232. if (c[len - 2] != '\r' || c[len - 1] != '\n')
  233. {
  234. /* limit the received data length to MAX_NAME_SIZE - 2('\r' and '\n' will be put into the buffer) */
  235. if ((c[len - 1] == '\r' || c[len - 1] == '\n') && (len + 1 <= MAX_NAME_SIZE))
  236. {
  237. /* calculate the buffer size to be sent(including '\r' and '\n') */
  238. name_recv.length += 1;
  239. }
  240. else if (len + 2 <= MAX_NAME_SIZE)
  241. {
  242. name_recv.length += 2;
  243. }
  244. else
  245. {
  246. name_recv.length = MAX_NAME_SIZE;
  247. }
  248. /* save the received data to name_recv.bytes */
  249. name_recv.bytes[name_recv.length - 2] = '\r';
  250. name_recv.bytes[name_recv.length - 1] = '\n';
  251. }
  252. send(fd, (void *)&HELLO, sizeof(HELLO), 0);
  253. send(fd, name_recv.bytes, name_recv.length, 0);
  254. name_recv.done = 0;
  255. name_recv.length = 0;
  256. }
  257. return ERR_OK;
  258. }
  259. /*!
  260. \brief hello task
  261. \param[in] arg: user supplied argument
  262. \param[out] none
  263. \retval none
  264. */
  265. static void hello_task(void *arg)
  266. {
  267. int ret;
  268. int sockfd = -1, newfd = -1;
  269. uint32_t len;
  270. int tcp_port = 8000;
  271. int recvnum;
  272. struct sockaddr_in svr_addr, clt_addr;
  273. char buf[50];
  274. /* bind to port 8000 at any interface */
  275. svr_addr.sin_family = AF_INET;
  276. svr_addr.sin_port = htons(tcp_port);
  277. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  278. name_recv.length = 0;
  279. name_recv.done = 0;
  280. while (1)
  281. {
  282. /* create a TCP socket */
  283. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  284. if (sockfd < 0)
  285. {
  286. continue;
  287. }
  288. ret = bind(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  289. if (ret < 0)
  290. {
  291. lwip_close(sockfd);
  292. sockfd = -1;
  293. continue;
  294. }
  295. /* listen for incoming connections (TCP listen backlog = 1) */
  296. ret = listen(sockfd, 1);
  297. if (ret < 0)
  298. {
  299. lwip_close(sockfd);
  300. continue;
  301. }
  302. len = sizeof(clt_addr);
  303. /* grab new connection */
  304. newfd = accept(sockfd, (struct sockaddr *)&clt_addr, (socklen_t *)&len);
  305. if (-1 != newfd)
  306. {
  307. send(newfd, (void *)&GREETING, sizeof(GREETING), 0);
  308. }
  309. while (-1 != newfd)
  310. {
  311. /* reveive packets, and limit a reception to MAX_NAME_SIZE bytes */
  312. recvnum = recv(newfd, buf, MAX_NAME_SIZE, 0);
  313. if (recvnum <= 0)
  314. {
  315. lwip_close(newfd);
  316. newfd = -1;
  317. break;
  318. }
  319. hello_gigadevice_recv(newfd, buf, recvnum);
  320. }
  321. lwip_close(sockfd);
  322. sockfd = -1;
  323. }
  324. }
  325. #endif /* LWIP_SOCKET */
  326. /*!
  327. \brief initialize the hello application
  328. \param[in] none
  329. \param[out] none
  330. \retval none
  331. */
  332. void hello_gigadevice_init(void)
  333. {
  334. xTaskCreate(hello_task, "HELLO", DEFAULT_THREAD_STACKSIZE, NULL, HELLO_TASK_PRIO, NULL);
  335. }