stdinout.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2019 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_netif.h"
  14. #include "esp_log.h"
  15. #include "driver/uart.h"
  16. #include "esp_console.h"
  17. #include "esp_vfs_dev.h"
  18. #include "linenoise/linenoise.h"
  19. //
  20. // Internal functions declaration referenced in io object
  21. //
  22. static esp_err_t netsuite_io_transmit(void *h, void *buffer, size_t len);
  23. static esp_err_t netsuite_io_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf);
  24. static esp_err_t netsuite_io_attach(esp_netif_t * esp_netif, void * args);
  25. /**
  26. * @brief IO object netif related configuration with data-path function callbacks
  27. * and pointer to the IO object instance (unused as this is a singleton)
  28. */
  29. const esp_netif_driver_ifconfig_t c_driver_ifconfig = {
  30. .driver_free_rx_buffer = NULL,
  31. .transmit = netsuite_io_transmit,
  32. .transmit_wrap = netsuite_io_transmit_wrap,
  33. .handle = "netsuite-io-object" // this IO object is a singleton, its handle uses as a name
  34. };
  35. /**
  36. * @brief IO object base structure used to point to internal attach function
  37. */
  38. const esp_netif_driver_base_t s_driver_base = {
  39. .post_attach = netsuite_io_attach
  40. };
  41. /**
  42. * @brief Transmit function called from esp_netif to output network stack data
  43. *
  44. * Note: This API has to conform to esp-netif transmit prototype
  45. *
  46. * @param h Opaque pointer representing the io driver (unused, const string in this case)
  47. * @param data data buffer
  48. * @param length length of data to send
  49. *
  50. * @return ESP_OK on success
  51. */
  52. static esp_err_t netsuite_io_transmit(void *h, void *buffer, size_t len)
  53. {
  54. /* output the packet to stdout */
  55. char *data = buffer;
  56. printf("\nPacketOut:[");
  57. for (size_t i=0; i<len; i++) {
  58. printf("%02x", *data++);
  59. }
  60. printf("]\n");
  61. return ESP_OK;
  62. }
  63. /**
  64. * @brief Transmit wrapper that is typically used for buffer handling and optimization.
  65. * Here just wraps the netsuite_io_transmit().
  66. *
  67. * @note The netstack_buf could be a ref-counted network stack buffer and might be used
  68. * by the lower layers directly if an additional handling is practical.
  69. * See docs on `esp_wifi_internal_tx_by_ref()` in components/esp_wifi/include/esp_private/wifi.h
  70. */
  71. static esp_err_t netsuite_io_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
  72. {
  73. return netsuite_io_transmit(h, buffer, len);
  74. }
  75. /**
  76. * @brief Post attach adapter for netsuite i/o
  77. *
  78. * Used to exchange internal callbacks and context between esp-netif and the I/O object.
  79. * In case of netsuite I/O, it only updates the driver config with internal callbacks and
  80. * its instance pointer (const string in this case)
  81. *
  82. * @param esp_netif handle to esp-netif object
  83. * @param args pointer to netsuite IO
  84. *
  85. * @return ESP_OK on success
  86. */
  87. static esp_err_t netsuite_io_attach(esp_netif_t * esp_netif, void * args)
  88. {
  89. ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &c_driver_ifconfig));
  90. return ESP_OK;
  91. }
  92. /**
  93. * @brief Process line read from serial input, character by character
  94. *
  95. * Converts from hex string to byte stream, so it can be processed
  96. * in test network interface
  97. *
  98. * @param line
  99. * @param packet
  100. *
  101. * @return size of packet
  102. */
  103. static size_t process_line(char* line, char* packet)
  104. {
  105. size_t count = 0;
  106. size_t i;
  107. for (i=0; i< strlen(line); i++) {
  108. char c = line[i];
  109. // accept both separators between bytes
  110. if (c == '-' || c == ' ') {
  111. ++count;
  112. // Processing numeric characters
  113. } else if (c >= '0' && c <= '9') {
  114. packet[count] *= 16;
  115. packet[count] += c - '0';
  116. // Processing alpha-numeric hex characters
  117. } else if (c >= 'a' && c <= 'f') {
  118. packet[count] *= 16;
  119. packet[count] += c - 'a' + 10;
  120. }
  121. }
  122. if (i>0 && strlen(line)>0) {
  123. count++;
  124. }
  125. return count;
  126. }
  127. /**
  128. * Created (initializes) the i/o object and returns handle ready to be attached to the esp-netif
  129. */
  130. void * netsuite_io_new(void)
  131. {
  132. // Initialize VFS & UART so we can use std::cout/cin
  133. setvbuf(stdin, NULL, _IONBF, 0);
  134. setvbuf(stdout, NULL, _IONBF, 0);
  135. /* Install UART driver for interrupt-driven reads and writes */
  136. ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM,
  137. 256, 0, 0, NULL, 0) );
  138. /* Tell VFS to use UART driver */
  139. esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
  140. esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
  141. /* Move the caret to the beginning of the next line on '\n' */
  142. esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
  143. linenoiseSetDumbMode(1);
  144. return (void *)&s_driver_base;
  145. }
  146. /**
  147. * I/O receive function
  148. */
  149. ssize_t netsuite_io_get_packet(char *packet, size_t max_len)
  150. {
  151. size_t size;
  152. /* read packet from stdin */
  153. char* line = linenoise("");
  154. if (!line) {
  155. return -1;
  156. }
  157. /* convert to binary */
  158. size = process_line(line, packet);
  159. if (size > max_len) {
  160. return -1;
  161. }
  162. linenoiseFree(line);
  163. return size;
  164. }