123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <termios.h>
- #include <signal.h>
- #include <wolfssl/options.h>
- #include <wolfssl/wolfcrypt/settings.h>
- #include <wolfssl/ssl.h>
- #include <wolfssl/wolfcrypt/random.h>
- #include <wolfssl/wolfcrypt/port/iotsafe/iotsafe.h>
- static int serial_fd = -1;
- extern int client_loop(const char *peer_ip, const char *peer_name,
- const char *peer_port, const char *temperature);
- #ifdef DEBUG_UART_IO
- static void print_buffer_hex(const char *buf, int len);
- static void print_buffer_char(const char *buf, int len);
- #endif
- static int usart_init(const char *dev_name, int *fd);
- static int usart_clean(int fd);
- static int usart_read(char *buf, int len);
- static int usart_write(const char *buf, int len);
- static void show_usage(const char *program);
- #ifdef DEBUG_UART_IO
- static void print_buffer_hex(const char *buf, int len)
- {
- for (int i = 0; i < len; i++)
- printf("%02X", (unsigned int)*(buf++));
- }
- static void print_buffer_char(const char *buf, int len)
- {
- for (int i = 0; i < len; i++)
- printf("%c", *(buf++));
- }
- #endif
- static int usart_init(const char *dev_name, int *fd)
- {
- int portfd = open(dev_name, O_RDWR | O_NOCTTY);
- if (portfd < 0)
- {
- *fd = -1;
- return errno;
- }
- struct termios tty;
- tcgetattr(portfd, &tty);
- cfsetospeed(&tty, B115200);
- cfsetispeed(&tty, B115200);
- tty.c_cflag = (tty.c_cflag & ~CSIZE) | (CS8);
- tty.c_iflag &= ~(IGNBRK | IXON | IXOFF | IXANY| INLCR | ICRNL);
- tty.c_oflag &= ~OPOST;
- tty.c_oflag &= ~(ONLCR|OCRNL);
- tty.c_cflag &= ~(PARENB | PARODD | CSTOPB);
- tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- tty.c_iflag &= ~ISTRIP;
- tty.c_cc[VMIN] = 0;
- tty.c_cc[VTIME] = 5;
- tcsetattr(portfd, TCSANOW, &tty);
- *fd = portfd;
- return 0;
- }
- static int usart_clean(int fd)
- {
- return close(fd);
- }
- static int usart_read(char *buf, int len)
- {
- if (!buf || len < 0)
- return -1;
- #ifdef DEBUG_UART_IO
- printf("UART Read Expected : %d bytes\n", len);
- #endif
- int ret = 0;
- int i = 0;
- char c;
- memset(buf, 0, len);
-
- do
- {
- ret = read(serial_fd, &c, 1U);
- if (ret > 0) {
- buf[i++] = c;
- if (c == '\n')
- break;
- }
- } while (i < len && ret > 0);
- #ifdef DEBUG_UART_IO
- printf("UART Read Actual : %d bytes\n", i);
- #ifdef DEBUG_UART_IO_EXTRA_VERBOSE
- printf("[READ in HEX ] <- ");
- print_buffer_hex(buf, i);
- printf("\n");
- #endif
- printf("[READ in CHAR] <- ");
- print_buffer_char(buf, i);
- printf("\n");
- #endif
- return i;
- }
- static int usart_write(const char *buf, int len)
- {
- if (!buf || len < 0)
- return -1;
- #ifdef DEBUG_UART_IO
- printf("UART Write Expected: %d bytes\n", len);
- #endif
- int sent = write(serial_fd, buf, len);
- if (sent < 0)
- sent = 0;
- #ifdef DEBUG_UART_IO
- printf("UART Write Actual : %d bytes\n", sent);
- #ifdef DEBUG_UART_IO_EXTRA_VERBOSE
- printf("[WRITE in HEX ] -> ");
- print_buffer_hex(buf, sent);
- printf("\n");
- #endif
- printf("[WRITE in CHAR] -> ");
- print_buffer_char(buf, sent);
- printf("\n");
- #endif
- return sent;
- }
- static void show_usage(const char *program)
- {
- printf("\nUsage:\n");
- printf("\t%s -ip SERVER_IP_ADDRESS -h SERVER_HOST_NAME -p PORT_NUMBER "
- "-t TEMPERATURE -d DEVICE_FILE_PATH\n", program);
- printf("\n");
- printf("\t-ip <server IPv4 address eg: 127.0.0.1>\n");
- printf("\t-h <server name eg: xxx.amazon.com>\n");
- printf("\t-p <server port eg: 443>\n");
- printf("\t-t <temperature eg: 25 Celsius>\n");
- printf("\t-d <serial device eg: /dev/ttyACM0>\n");
- exit(-1);
- }
- int main(int argc, char** argv)
- {
- char ip[30] = {0};
- char name[50] = {0};
- char port[8] = {0};
- char temperature[10] = {0};
- char device[30] = {0};
- if (argc == 11)
- {
- if (strcmp(argv[1], "-ip") == 0)
- strcpy((char*)&ip, argv[2]);
- else
- show_usage(argv[0]);
- if (strcmp(argv[3], "-h") == 0)
- strcpy((char*)&name, argv[4]);
- else
- show_usage(argv[0]);
- if (strcmp(argv[5], "-p") == 0)
- strcpy((char*)&port, argv[6]);
- else
- show_usage(argv[0]);
- if (strcmp(argv[7], "-t") == 0)
- strcpy((char*)&temperature, argv[8]);
- else
- show_usage(argv[0]);
- if (strcmp(argv[9], "-d") == 0)
- strcpy((char*)&device, argv[10]);
- else
- show_usage(argv[0]);
- }
- else
- {
- show_usage(argv[0]);
- }
- int ret = 0;
- printf("#####################\n");
- printf("wolfSSL IoT-SAFE Demo\n");
- printf("#####################\n");
- printf("---- Initializing serial I/O\n");
- printf("---- Opening serial I/O\n");
- printf("Serial device: %s\n", (const char*)&device);
- if ((ret = usart_init((const char*)&device, &serial_fd)) != 0)
- {
- printf("ERROR: Error opening %s: Error %i (%s)\n",
- (const char*)&device, ret, strerror(ret));
- exit(-1);
- }
- printf("---- Setting serial I/O read callback\n");
- wolfIoTSafe_SetCSIM_read_cb(&usart_read);
- printf("---- Setting serial I/O write callback\n");
- wolfIoTSafe_SetCSIM_write_cb(&usart_write);
- printf("---- Finish initializing serial I/O\n");
- client_loop(ip, name, port, temperature);
- printf("---- Cleaning serial I/O\n");
- printf("---- Closing serial I/O\n");
- if ((ret = usart_clean(serial_fd)) != 0)
- {
- printf("ERROR: Error closing %s: Error %i (%s)\n",
- (const char*)&device, ret, strerror(ret));
- exit(-1);
- }
- printf("---- Finish cleaning serial I/O\n");
- return 0;
- }
|