main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* main.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, USA
  20. */
  21. /* IoT-safe example
  22. * main for STM32L4
  23. */
  24. #include <stdio.h>
  25. #include "devices.h"
  26. #include <wolfssl/wolfcrypt/settings.h>
  27. #include <wolfssl/ssl.h>
  28. #include <wolfssl/wolfcrypt/random.h>
  29. #include <wolfssl/wolfcrypt/port/iotsafe/iotsafe.h>
  30. #define STDIO USART2_BASE
  31. #define MODEM USART1_BASE
  32. #define RX_TIMEOUT 100
  33. extern volatile unsigned long jiffies;
  34. int usart1_read(char *buf, int len)
  35. {
  36. int i = 0;
  37. char c;
  38. int ret;
  39. volatile unsigned long start = jiffies;
  40. memset(buf, 0, len);
  41. do {
  42. ret = usart_rx(MODEM, &c);
  43. if (ret > 0) {
  44. buf[i++] = c;
  45. if (c == '\n') {
  46. return i;
  47. }
  48. } else {
  49. __asm__ volatile("wfi");
  50. }
  51. } while(((jiffies - start) < RX_TIMEOUT) && (i < len));
  52. if (i == 0) {
  53. return 0;
  54. }
  55. return i;
  56. }
  57. int usart1_write(const char *buf, int len)
  58. {
  59. int i = 0;
  60. do {
  61. usart_tx(MODEM, buf[i++]);
  62. } while(i < len);
  63. return len;
  64. }
  65. extern int memory_tls_test(void);
  66. void main(void)
  67. {
  68. uint32_t last_mark = 0;
  69. int i;
  70. char randombytes[16];
  71. int ret;
  72. char c;
  73. WC_RNG rng;
  74. clock_pll_on();
  75. systick_enable();
  76. if (usart_init(STDIO, 115200, 8, 'N', 1) < 0)
  77. while(1)
  78. ;
  79. printf("wolfSSL IoT-SAFE demo\n");
  80. printf("Press a key to continue...\n");
  81. do {
  82. ret = usart_rx(STDIO, &c);
  83. } while (ret <= 0);
  84. printf("%c\n", c);
  85. printf("Initializing modem...\n");
  86. stmod_modem_enable();
  87. sleep_ms(1000);
  88. printf("System up and running\r\n");
  89. sleep_ms(1000);
  90. printf("Initializing wolfSSL...\n");
  91. wolfSSL_Init();
  92. wolfSSL_Debugging_ON();
  93. printf("Initializing modem port\n");
  94. if(usart_init(MODEM, 115200, 8, 'N', 1) < 0)
  95. while(1)
  96. ;
  97. printf("Initializing IoTSafe I/O...\n");
  98. wolfIoTSafe_SetCSIM_read_cb(usart1_read);
  99. wolfIoTSafe_SetCSIM_write_cb(usart1_write);
  100. /* IoT-Safe system up and running. */
  101. printf("Initializing RNG...\n");
  102. wc_InitRng(&rng);
  103. printf("Getting RND...\n");
  104. wc_RNG_GenerateBlock(&rng, randombytes, 16);
  105. wc_FreeRng(&rng);
  106. printf("Random bytes: ");
  107. for (i = 0; i < 16; i++) {
  108. printf("%02X", randombytes[i]);
  109. }
  110. printf("\n");
  111. memory_tls_test();
  112. while(1) {
  113. if (usart_rx(STDIO, &c)) {
  114. usart_tx(MODEM, c);
  115. usart_tx(STDIO, c);
  116. }
  117. if (usart_rx(MODEM, &c)) {
  118. usart_tx(STDIO, c);
  119. }
  120. __asm__ volatile("wfi");
  121. }
  122. }