main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* main.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* IoT-SAFE example
  22. * main for POSIX serial communication.
  23. */
  24. /* C Standard Library */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. /* POSIX Library */
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <sys/stat.h>
  33. #include <termios.h>
  34. #include <signal.h>
  35. /* wolfSSL Library */
  36. #include <wolfssl/options.h>
  37. #include <wolfssl/wolfcrypt/settings.h>
  38. #include <wolfssl/ssl.h>
  39. #include <wolfssl/wolfcrypt/random.h>
  40. #include <wolfssl/wolfcrypt/port/iotsafe/iotsafe.h>
  41. /* Variable Declarations */
  42. static int serial_fd = -1;
  43. /* Function Declarations */
  44. extern int client_loop(const char *peer_ip, const char *peer_name,
  45. const char *peer_port, const char *temperature);
  46. #ifdef DEBUG_UART_IO
  47. static void print_buffer_hex(const char *buf, int len);
  48. static void print_buffer_char(const char *buf, int len);
  49. #endif
  50. static int usart_init(const char *dev_name, int *fd);
  51. static int usart_clean(int fd);
  52. static int usart_read(char *buf, int len);
  53. static int usart_write(const char *buf, int len);
  54. static void show_usage(const char *program);
  55. /* Function Definitions */
  56. #ifdef DEBUG_UART_IO
  57. static void print_buffer_hex(const char *buf, int len)
  58. {
  59. for (int i = 0; i < len; i++)
  60. printf("%02X", (unsigned int)*(buf++));
  61. }
  62. static void print_buffer_char(const char *buf, int len)
  63. {
  64. for (int i = 0; i < len; i++)
  65. printf("%c", *(buf++));
  66. }
  67. #endif
  68. static int usart_init(const char *dev_name, int *fd)
  69. {
  70. int portfd = open(dev_name, O_RDWR | O_NOCTTY);
  71. if (portfd < 0)
  72. {
  73. *fd = -1;
  74. return errno;
  75. }
  76. struct termios tty;
  77. tcgetattr(portfd, &tty);
  78. cfsetospeed(&tty, B115200);
  79. cfsetispeed(&tty, B115200);
  80. tty.c_cflag = (tty.c_cflag & ~CSIZE) | (CS8);
  81. tty.c_iflag &= ~(IGNBRK | IXON | IXOFF | IXANY| INLCR | ICRNL);
  82. tty.c_oflag &= ~OPOST;
  83. tty.c_oflag &= ~(ONLCR|OCRNL);
  84. tty.c_cflag &= ~(PARENB | PARODD | CSTOPB);
  85. tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  86. tty.c_iflag &= ~ISTRIP;
  87. tty.c_cc[VMIN] = 0;
  88. tty.c_cc[VTIME] = 5;
  89. tcsetattr(portfd, TCSANOW, &tty);
  90. *fd = portfd;
  91. return 0;
  92. }
  93. static int usart_clean(int fd)
  94. {
  95. return close(fd);
  96. }
  97. static int usart_read(char *buf, int len)
  98. {
  99. if (!buf || len < 0)
  100. return -1;
  101. #ifdef DEBUG_UART_IO
  102. printf("UART Read Expected : %d bytes\n", len);
  103. #endif
  104. int ret = 0;
  105. int i = 0;
  106. char c;
  107. memset(buf, 0, len);
  108. /* Read 1 byte at one time until *buf is full or a POSIX read error like
  109. * timeout occurs. */
  110. do
  111. {
  112. ret = read(serial_fd, &c, 1U);
  113. if (ret > 0) {
  114. buf[i++] = c;
  115. if (c == '\n')
  116. break;
  117. }
  118. } while (i < len && ret > 0);
  119. #ifdef DEBUG_UART_IO
  120. printf("UART Read Actual : %d bytes\n", i);
  121. #ifdef DEBUG_UART_IO_EXTRA_VERBOSE
  122. printf("[READ in HEX ] <- ");
  123. print_buffer_hex(buf, i);
  124. printf("\n");
  125. #endif
  126. printf("[READ in CHAR] <- ");
  127. print_buffer_char(buf, i);
  128. printf("\n");
  129. #endif
  130. return i;
  131. }
  132. static int usart_write(const char *buf, int len)
  133. {
  134. if (!buf || len < 0)
  135. return -1;
  136. #ifdef DEBUG_UART_IO
  137. printf("UART Write Expected: %d bytes\n", len);
  138. #endif
  139. int sent = write(serial_fd, buf, len);
  140. if (sent < 0)
  141. sent = 0;
  142. #ifdef DEBUG_UART_IO
  143. printf("UART Write Actual : %d bytes\n", sent);
  144. #ifdef DEBUG_UART_IO_EXTRA_VERBOSE
  145. printf("[WRITE in HEX ] -> ");
  146. print_buffer_hex(buf, sent);
  147. printf("\n");
  148. #endif
  149. printf("[WRITE in CHAR] -> ");
  150. print_buffer_char(buf, sent);
  151. printf("\n");
  152. #endif
  153. return sent;
  154. }
  155. static void show_usage(const char *program)
  156. {
  157. printf("\nUsage:\n");
  158. printf("\t%s -ip SERVER_IP_ADDRESS -h SERVER_HOST_NAME -p PORT_NUMBER "
  159. "-t TEMPERATURE -d DEVICE_FILE_PATH\n", program);
  160. printf("\n");
  161. printf("\t-ip <server IPv4 address eg: 127.0.0.1>\n");
  162. printf("\t-h <server name eg: xxx.amazon.com>\n");
  163. printf("\t-p <server port eg: 443>\n");
  164. printf("\t-t <temperature eg: 25 Celsius>\n");
  165. printf("\t-d <serial device eg: /dev/ttyACM0>\n");
  166. exit(-1);
  167. }
  168. int main(int argc, char** argv)
  169. {
  170. char ip[30] = {0};
  171. char name[50] = {0};
  172. char port[8] = {0};
  173. char temperature[10] = {0};
  174. char device[30] = {0};
  175. if (argc == 11)
  176. {
  177. if (strcmp(argv[1], "-ip") == 0)
  178. strcpy((char*)&ip, argv[2]);
  179. else
  180. show_usage(argv[0]);
  181. if (strcmp(argv[3], "-h") == 0)
  182. strcpy((char*)&name, argv[4]);
  183. else
  184. show_usage(argv[0]);
  185. if (strcmp(argv[5], "-p") == 0)
  186. strcpy((char*)&port, argv[6]);
  187. else
  188. show_usage(argv[0]);
  189. if (strcmp(argv[7], "-t") == 0)
  190. strcpy((char*)&temperature, argv[8]);
  191. else
  192. show_usage(argv[0]);
  193. if (strcmp(argv[9], "-d") == 0)
  194. strcpy((char*)&device, argv[10]);
  195. else
  196. show_usage(argv[0]);
  197. }
  198. else
  199. {
  200. show_usage(argv[0]);
  201. }
  202. int ret = 0;
  203. printf("#####################\n");
  204. printf("wolfSSL IoT-SAFE Demo\n");
  205. printf("#####################\n");
  206. printf("---- Initializing serial I/O\n");
  207. printf("---- Opening serial I/O\n");
  208. printf("Serial device: %s\n", (const char*)&device);
  209. if ((ret = usart_init((const char*)&device, &serial_fd)) != 0)
  210. {
  211. printf("ERROR: Error opening %s: Error %i (%s)\n",
  212. (const char*)&device, ret, strerror(ret));
  213. exit(-1);
  214. }
  215. printf("---- Setting serial I/O read callback\n");
  216. wolfIoTSafe_SetCSIM_read_cb(&usart_read);
  217. printf("---- Setting serial I/O write callback\n");
  218. wolfIoTSafe_SetCSIM_write_cb(&usart_write);
  219. printf("---- Finish initializing serial I/O\n");
  220. client_loop(ip, name, port, temperature);
  221. printf("---- Cleaning serial I/O\n");
  222. printf("---- Closing serial I/O\n");
  223. if ((ret = usart_clean(serial_fd)) != 0)
  224. {
  225. printf("ERROR: Error closing %s: Error %i (%s)\n",
  226. (const char*)&device, ret, strerror(ret));
  227. exit(-1);
  228. }
  229. printf("---- Finish cleaning serial I/O\n");
  230. return 0;
  231. }