stdinout.c 5.1 KB

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