test_vfs_uart.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "unity.h"
  18. #include "rom/uart.h"
  19. #include "soc/uart_struct.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "sdkconfig.h"
  23. static void fwrite_str_loopback(const char* str, size_t size)
  24. {
  25. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  26. UART0.conf0.loopback = 1;
  27. fwrite(str, 1, size, stdout);
  28. fflush(stdout);
  29. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  30. vTaskDelay(2 / portTICK_PERIOD_MS);
  31. UART0.conf0.loopback = 0;
  32. }
  33. static void flush_stdin_stdout()
  34. {
  35. vTaskDelay(10 / portTICK_PERIOD_MS);
  36. char *bitbucket = (char*) 0x3f000000;
  37. while (fread(bitbucket, 1, 128, stdin) > 0) {
  38. ;
  39. }
  40. fflush(stdout);
  41. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  42. }
  43. TEST_CASE("can read from stdin", "[vfs]")
  44. {
  45. flush_stdin_stdout();
  46. const size_t count = 12;
  47. srand(count);
  48. char* data = (char*) calloc(1, count * 8 + 1);
  49. char* buf = (char*) calloc(1, count * 8 + 1);
  50. char* p = data;
  51. for (size_t i = 0; i < count; ++i) {
  52. p += sprintf(p, "%08x", rand());
  53. }
  54. p += sprintf(p, "\n");
  55. size_t len = p - data;
  56. fwrite_str_loopback(data, len);
  57. size_t cb = fread(buf, 1, len, stdin);
  58. TEST_ASSERT_EQUAL(len, cb);
  59. TEST_ASSERT_EQUAL_UINT8_ARRAY(data, buf, len);
  60. }
  61. #if CONFIG_NEWLIB_STDOUT_ADDCR
  62. TEST_CASE("CRs are removed from the stdin correctly", "[vfs]")
  63. {
  64. flush_stdin_stdout();
  65. const char* send_str = "1234567890\n\r123\r\n4\n";
  66. /* with CONFIG_NEWLIB_STDOUT_ADDCR, the following will be sent on the wire.
  67. * (last character of each part is marked with a hat)
  68. *
  69. * 1234567890\r\n\r123\r\r\n4\r\n
  70. * ^ ^^ ^
  71. */
  72. char buf[128];
  73. char* dst = buf;
  74. fwrite_str_loopback(send_str, 11); // send up to the first \n
  75. size_t rb = fread(dst, 1, 5, stdin); // read first 5
  76. TEST_ASSERT_EQUAL(5, rb);
  77. dst += rb;
  78. rb = fread(dst, 1, 6, stdin); // ask for 6
  79. TEST_ASSERT_EQUAL(6, rb); // get 6
  80. TEST_ASSERT_EQUAL_UINT8_ARRAY("1234567890\n", buf, 11);
  81. dst += rb;
  82. rb = fread(dst, 1, 2, stdin); // any more characters?
  83. TEST_ASSERT_EQUAL(0, rb); // nothing
  84. fwrite_str_loopback(send_str + 11, 1); // send the '\r'
  85. vTaskDelay(10 / portTICK_PERIOD_MS);
  86. rb = fread(dst, 1, 2, stdin); // try to get somthing
  87. TEST_ASSERT_EQUAL(0, rb); // still nothing (\r is buffered)
  88. fwrite_str_loopback(send_str + 12, 1); // Now send the '1'
  89. vTaskDelay(10 / portTICK_PERIOD_MS);
  90. rb = fread(dst, 1, 2, stdin); // try again
  91. TEST_ASSERT_EQUAL(2, rb); // get two characters
  92. TEST_ASSERT_EQUAL_UINT8_ARRAY("\r1", dst, 2);
  93. dst += rb;
  94. fwrite_str_loopback(send_str + 13, 6); // Send the rest
  95. vTaskDelay(10 / portTICK_PERIOD_MS);
  96. rb = fread(dst, 1, 4, stdin); // consume "23\r\n"
  97. TEST_ASSERT_EQUAL(4, rb);
  98. TEST_ASSERT_EQUAL_UINT8_ARRAY("23\r\n", dst, 4);
  99. dst += rb;
  100. rb = fread(dst, 1, 4, stdin); // ask for more than the remainder
  101. TEST_ASSERT_EQUAL(2, rb);
  102. TEST_ASSERT_EQUAL_UINT8_ARRAY("4\n", dst, 2);
  103. }
  104. #endif //CONFIG_NEWLIB_STDOUT_ADDCR