hello_gigadevice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. done = ((c[i] == '\r') || (c[i] == '\n'));
  68. }
  69. if(done){
  70. if(c[len-2] != '\r' || c[len-1] != '\n'){
  71. /* limit the received data length to MAX_NAME_SIZE - 2('\r' and '\n' will be put into the buffer) */
  72. if((c[len-1] == '\r' || c[len-1] == '\n') && (len+1 <= MAX_NAME_SIZE)){
  73. /* calculate the buffer size to be sent(including '\r' and '\n') */
  74. len += 1;
  75. }else if(len+2 <= MAX_NAME_SIZE){
  76. len += 2;
  77. }else{
  78. len = MAX_NAME_SIZE;
  79. }
  80. /* save the received data to a new buffer */
  81. c[len-2] = '\r';
  82. c[len-1] = '\n';
  83. }
  84. netconn_write(conn, (void *)&HELLO, sizeof(HELLO), NETCONN_COPY);
  85. netconn_write(conn, data, len, NETCONN_COPY);
  86. }
  87. return ERR_OK;
  88. }
  89. /*!
  90. \brief this function when the telnet connection is established
  91. \param[in] conn: the TCP netconn over which to send data
  92. \param[out] none
  93. \retval err_t: error value
  94. */
  95. static err_t hello_gigadevice_accept(struct netconn *conn)
  96. {
  97. u32_t ipaddress;
  98. u8_t iptxt[50];
  99. volatile u8_t iptab[4];
  100. struct tcp_pcb *pcb;
  101. pcb = conn->pcb.tcp;
  102. ipaddress = pcb->remote_ip.addr;
  103. /* read its IP address */
  104. iptab[0] = (u8_t)(ipaddress >> 24);
  105. iptab[1] = (u8_t)(ipaddress >> 16);
  106. iptab[2] = (u8_t)(ipaddress >> 8);
  107. iptab[3] = (u8_t)(ipaddress);
  108. sprintf((char*)iptxt, "Telnet:%d.%d.%d.%d ", iptab[3], iptab[2], iptab[1], iptab[0]);
  109. /* send out the first message */
  110. netconn_write(conn, (void *)&iptxt, sizeof(iptxt), NETCONN_COPY);
  111. netconn_write(conn, (void *)&GREETING, sizeof(GREETING), NETCONN_COPY);
  112. return ERR_OK;
  113. }
  114. /*!
  115. \brief hello task
  116. \param[in] arg: user supplied argument
  117. \param[out] none
  118. \retval none
  119. */
  120. static void hello_task(void *arg)
  121. {
  122. struct netconn *conn, *newconn;
  123. err_t err, accept_err;
  124. struct netbuf *buf;
  125. void *data;
  126. u16_t len;
  127. err_t recv_err;
  128. LWIP_UNUSED_ARG(arg);
  129. conn = netconn_new(NETCONN_TCP);
  130. if(NULL != conn){
  131. /* bind connection to well known port number 8000 */
  132. err = netconn_bind(conn, NULL, 8000);
  133. if(ERR_OK == err){
  134. netconn_listen(conn);
  135. while(1){
  136. /* grab new connection */
  137. accept_err = netconn_accept(conn, &newconn);
  138. /* process the new connection */
  139. if(ERR_OK == accept_err){
  140. hello_gigadevice_accept(newconn);
  141. recv_err = netconn_recv(newconn, &buf);
  142. while (ERR_OK == recv_err){
  143. do{
  144. netbuf_data(buf, &data, &len);
  145. }while (netbuf_next(buf) >= 0);
  146. hello_gigadevice_recv(newconn, data, len);
  147. netbuf_delete(buf);
  148. recv_err = netconn_recv(newconn, &buf);
  149. }
  150. /* close connection and discard connection identifier */
  151. netconn_close(newconn);
  152. netconn_delete(newconn);
  153. }
  154. }
  155. }else{
  156. netconn_delete(newconn);
  157. printf(" can not bind TCP netconn");
  158. }
  159. }else{
  160. printf("can not create TCP netconn");
  161. }
  162. }
  163. #endif /* ((LWIP_SOCKET == 0) && (LWIP_NETCONN == 1)) */
  164. #if LWIP_SOCKET
  165. #include "lwip/sockets.h"
  166. struct recev_packet
  167. {
  168. int length;
  169. char bytes[MAX_NAME_SIZE];
  170. int done;
  171. }name_recv;
  172. static err_t hello_gigadevice_recv(int fd, void *data, int len);
  173. /*!
  174. \brief called when a data is received on the telnet connection
  175. \param[in] fd: the socket id which to send data
  176. \param[in] data: pointer to the data received
  177. \param[in] len: size of the data
  178. \param[out] none
  179. \retval err_t: error value
  180. */
  181. static err_t hello_gigadevice_recv(int fd, void *data, int len)
  182. {
  183. char *c;
  184. int i;
  185. int done;
  186. done = 0;
  187. c = (char *)data;
  188. /* a telnet communication packet is ended with an enter key,
  189. in socket, here is to check whether the last packet is complete */
  190. for(i=0; i<len && !done; i++){
  191. done = ((c[i] == '\r') || (c[i] == '\n'));
  192. }
  193. /* when the packet length received is no larger than MAX_NAME_SIZE */
  194. if(0 == name_recv.done){
  195. /* havn't received the end of the packet, so the received data length
  196. is the configured socket reception limit--MAX_NAME_SIZE */
  197. if(0 == done){
  198. memcpy(name_recv.bytes, data, MAX_NAME_SIZE);
  199. name_recv.length = MAX_NAME_SIZE;
  200. name_recv.done = 1;
  201. /* have received the end of the packet */
  202. }else{
  203. memcpy(name_recv.bytes, data, len);
  204. name_recv.length = len;
  205. }
  206. }
  207. if(1 == done){
  208. if(c[len-2] != '\r' || c[len-1] != '\n'){
  209. /* limit the received data length to MAX_NAME_SIZE - 2('\r' and '\n' will be put into the buffer) */
  210. if((c[len-1] == '\r' || c[len-1] == '\n') && (len+1 <= MAX_NAME_SIZE)){
  211. /* calculate the buffer size to be sent(including '\r' and '\n') */
  212. name_recv.length += 1;
  213. }else if(len+2 <= MAX_NAME_SIZE){
  214. name_recv.length += 2;
  215. }else{
  216. name_recv.length = MAX_NAME_SIZE;
  217. }
  218. /* save the received data to name_recv.bytes */
  219. name_recv.bytes[name_recv.length - 2] = '\r';
  220. name_recv.bytes[name_recv.length - 1] = '\n';
  221. }
  222. send(fd, (void *)&HELLO, sizeof(HELLO), 0);
  223. send(fd, name_recv.bytes, name_recv.length, 0);
  224. name_recv.done = 0;
  225. name_recv.length = 0;
  226. }
  227. return ERR_OK;
  228. }
  229. /*!
  230. \brief hello task
  231. \param[in] arg: user supplied argument
  232. \param[out] none
  233. \retval none
  234. */
  235. static void hello_task(void *arg)
  236. {
  237. int ret;
  238. int sockfd = -1, newfd = -1;
  239. uint32_t len;
  240. int tcp_port = 8000;
  241. int recvnum;
  242. struct sockaddr_in svr_addr, clt_addr;
  243. char buf[50];
  244. /* bind to port 8000 at any interface */
  245. svr_addr.sin_family = AF_INET;
  246. svr_addr.sin_port = htons(tcp_port);
  247. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  248. name_recv.length = 0;
  249. name_recv.done = 0;
  250. while(1){
  251. /* create a TCP socket */
  252. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  253. if (sockfd < 0){
  254. continue;
  255. }
  256. ret = bind(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  257. if (ret < 0){
  258. lwip_close(sockfd);
  259. sockfd = -1;
  260. continue;
  261. }
  262. /* listen for incoming connections (TCP listen backlog = 1) */
  263. ret = listen( sockfd, 1 );
  264. if (ret < 0){
  265. lwip_close(sockfd);
  266. continue;
  267. }
  268. len = sizeof(clt_addr);
  269. /* grab new connection */
  270. newfd = accept(sockfd, (struct sockaddr *)&clt_addr, (socklen_t *)&len);
  271. if (-1 != newfd){
  272. send(newfd, (void *)&GREETING, sizeof(GREETING), 0);
  273. }
  274. while(-1 != newfd){
  275. /* reveive packets, and limit a reception to MAX_NAME_SIZE bytes */
  276. recvnum = recv(newfd, buf, MAX_NAME_SIZE, 0);
  277. if (recvnum <= 0){
  278. lwip_close(newfd);
  279. newfd = -1;
  280. break;
  281. }
  282. hello_gigadevice_recv(newfd, buf, recvnum);
  283. }
  284. lwip_close(sockfd);
  285. sockfd = -1;
  286. }
  287. }
  288. #endif /* LWIP_SOCKET */
  289. /*!
  290. \brief initialize the hello application
  291. \param[in] none
  292. \param[out] none
  293. \retval none
  294. */
  295. void hello_gigadevice_init(void)
  296. {
  297. xTaskCreate(hello_task, "HELLO", DEFAULT_THREAD_STACKSIZE, NULL, HELLO_TASK_PRIO, NULL);
  298. }