1
0

main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * main.c
  3. */
  4. #include "main.h"
  5. #include "util.h"
  6. #if !BSPCFG_ENABLE_IO_SUBSYSTEM
  7. #error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined \
  8. non-zero in user_config.h. Please recompile BSP with this option.
  9. #endif
  10. #ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
  11. #error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. \
  12. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in \
  13. user_config.h and recompile BSP with this option.
  14. #endif
  15. #if defined BSP_SDCARD_ESDHC_CHANNEL
  16. #if ! BSPCFG_ENABLE_ESDHC
  17. #error This application requires BSPCFG_ENABLE_ESDHC defined non-zero in \
  18. user_config.h. Please recompile libraries with this option.
  19. #endif
  20. #elif defined BSP_SDCARD_SDHC_CHANNEL
  21. #if ! BSPCFG_ENABLE_SDHC
  22. #error This application requires BSPCFG_ENABLE_SDHC defined non-zero in \
  23. user_config.h. Please recompile libraries with this option.
  24. #endif
  25. #endif
  26. #if defined (BSP_SDCARD_SPI_CHANNEL)
  27. #define SDCARD_COM_CHANNEL BSP_SDCARD_SPI_CHANNEL
  28. #elif defined (BSP_SDCARD_ESDHC_CHANNEL)
  29. #define SDCARD_COM_CHANNEL BSP_SDCARD_ESDHC_CHANNEL
  30. #elif defined (BSP_SDCARD_SDHC_CHANNEL)
  31. #define SDCARD_COM_CHANNEL BSP_SDCARD_SDHC_CHANNEL
  32. #else
  33. #error "SDCARD low level communication device not defined!"
  34. #endif
  35. TASK_TEMPLATE_STRUCT MQX_template_list[] =
  36. {
  37. /* Task number, Entry point, Stack, Pri, String, Auto? */
  38. {MAIN_TASK, Main_task, 20000, 9, "main", MQX_AUTO_START_TASK},
  39. {0, 0, 0, 0, 0, 0, }
  40. };
  41. /*TASK*-----------------------------------------------------
  42. *
  43. * Task Name : Main_task
  44. * Comments :
  45. * This task sets up the SD card and Ethernet devices,
  46. * then starts the example wolfSSL client. The example
  47. * wolfSSL client connects to a server over TLS and sends
  48. * a simple HTTP GET message, then prints out the reply
  49. * from the server.
  50. *
  51. * To change the IP address and port of the server,
  52. * change the wolfsslIP and wolfsslPort variables in
  53. * client_test(). Note that wolfsslIP needs to be given
  54. * in hexadecimal.
  55. *
  56. *END*-----------------------------------------------------*/
  57. void Main_task(uint32_t initial_data)
  58. {
  59. int ret = 0;
  60. char filesystem_name[] = "a:";
  61. char partman_name[] = "pm:";
  62. MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partman_handle;
  63. printf("Starting client example... \n");
  64. ret = sdcard_open(&com_handle, &sdcard_handle, &partman_handle,
  65. &filesystem_handle, partman_name, filesystem_name);
  66. if (ret != 0) {
  67. printf("error: sdcard_open(), ret = %d\n", ret);
  68. _mqx_exit(1);
  69. }
  70. printf("SD card installed to %s\n", filesystem_name);
  71. setup_ethernet();
  72. setup_clock();
  73. client_test();
  74. ret = sdcard_close(&sdcard_handle, &partman_handle, &filesystem_handle,
  75. partman_name, filesystem_name);
  76. if (ret != 0) {
  77. printf("error: sdcard_close(), ret = %d\n", ret);
  78. _mqx_exit(1);
  79. }
  80. printf("SD card uninstalled.\n");
  81. _mqx_exit(0);
  82. }
  83. void setup_ethernet(void)
  84. {
  85. int error;
  86. _enet_handle ehandle; /* for Ethernet driver */
  87. _rtcs_if_handle ihandle;
  88. _enet_address address;
  89. error = RTCS_create();
  90. if (error) {
  91. err_sys("failed to create RTCS");
  92. }
  93. ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, ENET_IPADDR, address);
  94. /* Set up the Ethernet driver */
  95. error = ENET_initialize(BSP_DEFAULT_ENET_DEVICE, address, 0, &ehandle);
  96. if (error)
  97. err_sys("failed to initialize Ethernet driver");
  98. error = RTCS_if_add(ehandle, RTCS_IF_ENET, &ihandle);
  99. if (error)
  100. err_sys("failed to add interface for Ethernet");
  101. error = RTCS_if_bind(ihandle, ENET_IPADDR, ENET_IPMASK);
  102. if (error)
  103. err_sys("failed to bind interface for Ethernet");
  104. #ifdef GATE_IPADDR
  105. RTCS_gate_add(GATE_IPADDR, INADDR_ANY, INADDR_ANY);
  106. #endif
  107. printf("Ethernet device %d bound to %X\n", BSP_DEFAULT_ENET_DEVICE,
  108. ENET_IPADDR);
  109. }
  110. void setup_clock(void)
  111. {
  112. uint32_t ret = 0, i = 0;
  113. uint32_t sntp_connected = 0;
  114. uint32_t sntp_max_tries = 3;
  115. TIME_STRUCT time_s;
  116. DATE_STRUCT date_s;
  117. /* NTP server: nist1-lnk.binary.net */
  118. _ip_address ipaddr = IPADDR(216,229,0,179);
  119. for (i = 0; i < sntp_max_tries; i++) {
  120. printf("Getting time from NTP server [ attempt %u of %u ]...\n",
  121. i+1, sntp_max_tries);
  122. /* update time from NTP server */
  123. ret = SNTP_oneshot(ipaddr, 5000);
  124. if (ret == RTCS_OK) {
  125. sntp_connected = 1;
  126. printf("SNTP successfully updated device time\n");
  127. break;
  128. } else if (ret == RTCSERR_TIMEOUT) {
  129. printf("SNTP attempt timed out.\n");
  130. }
  131. _time_delay(1000);
  132. }
  133. if (sntp_connected == 0) {
  134. err_sys("SNTP failed to update device time");
  135. }
  136. /* print device time, for debug purposes */
  137. _time_get(&time_s);
  138. _time_to_date(&time_s, &date_s);
  139. printf("Current time: %02d/%02d/%02d %02d:%02d:%02d\n",
  140. date_s.YEAR, date_s.MONTH, date_s.DAY, date_s.HOUR, date_s.MINUTE,
  141. date_s.SECOND);
  142. return;
  143. }
  144. int myVerify(int preverify, WOLFSSL_X509_STORE_CTX* store)
  145. {
  146. (void)preverify;
  147. char buffer[80];
  148. printf("In verification callback, error = %d, %s\n",
  149. store->error, wolfSSL_ERR_error_string(store->error, buffer));
  150. return 0;
  151. }
  152. void client_test(void)
  153. {
  154. char msg[64];
  155. char reply[1024];
  156. int sockfd, input;
  157. int ret = 0, msgSz = 0;
  158. struct sockaddr_in servaddr;
  159. WOLFSSL_CTX* ctx;
  160. WOLFSSL* ssl;
  161. long wolfsslIP = IPADDR(192,168,1,125);
  162. long wolfsslPort = 11111;
  163. /* for debug, compile wolfSSL with DEBUG_WOLFSSL defined */
  164. wolfSSL_Debugging_ON();
  165. wolfSSL_Init();
  166. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  167. if (ctx == 0)
  168. err_sys("setting up ctx");
  169. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  170. ret = wolfSSL_CTX_use_certificate_file(ctx, clientCert, SSL_FILETYPE_PEM);
  171. if (ret != SSL_SUCCESS)
  172. err_sys("can't load client cert file, check file");
  173. ret = wolfSSL_CTX_use_PrivateKey_file(ctx, clientKey, SSL_FILETYPE_PEM);
  174. if (ret != SSL_SUCCESS)
  175. err_sys("can't load client key file, check file");
  176. ret = wolfSSL_CTX_load_verify_locations(ctx, caCert, 0);
  177. if (ret != SSL_SUCCESS)
  178. err_sys("can't load CA cert file, check file");
  179. /* create socket descriptor */
  180. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  181. if (sockfd == RTCS_SOCKET_ERROR) {
  182. err_sys("socket creation failed");
  183. } else {
  184. printf("socket created successfully\n");
  185. }
  186. /*
  187. * Unlike most TCP/IP stacks, RTCS requires that sin_port and
  188. * sin_addr needs to be in Host Byte Order, not Network Byte Order.
  189. * This means we shouldn't use htons() when setting these values.
  190. */
  191. memset((char*)&servaddr, 0, sizeof(servaddr));
  192. servaddr.sin_family = AF_INET;
  193. servaddr.sin_port = wolfsslPort;
  194. servaddr.sin_addr.s_addr = wolfsslIP;
  195. ret = connect(sockfd, &servaddr, sizeof(servaddr));
  196. if (ret != RTCS_OK) {
  197. err_sys("connect() failed");
  198. } else {
  199. printf("Connected to %lx, port %d.\n", servaddr.sin_addr.s_addr,
  200. servaddr.sin_port);
  201. }
  202. if ( (ssl = wolfSSL_new(ctx)) == NULL)
  203. err_sys("wolfSSL_new failed");
  204. ret = wolfSSL_set_fd(ssl, sockfd);
  205. if (ret != SSL_SUCCESS)
  206. err_sys("wolfSSL_set_fd failed");
  207. ret = wolfSSL_connect(ssl);
  208. if (ret != SSL_SUCCESS)
  209. err_sys("wolfSSL_connect failed");
  210. printf("wolfSSL_connect() ok, sending GET...\n");
  211. msgSz = 28;
  212. strncpy(msg, "GET /index.html HTTP/1.0\r\n\r\n", msgSz);
  213. if (wolfSSL_write(ssl, msg, msgSz) != msgSz)
  214. err_sys("wolfSSL_write() failed");
  215. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  216. if (input > 0) {
  217. reply[input] = 0;
  218. printf("Server response: %s\n", reply);
  219. while (1) {
  220. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  221. if (input > 0) {
  222. reply[input] = 0;
  223. printf("%s\n", reply);
  224. } else {
  225. break;
  226. }
  227. }
  228. }
  229. wolfSSL_shutdown(ssl);
  230. wolfSSL_free(ssl);
  231. wolfSSL_CTX_free(ctx);
  232. wolfSSL_Cleanup();
  233. }
  234. /* EOF */