cli_vcp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include <furi_hal_usb_cdc.h>
  2. #include <furi_hal.h>
  3. #include <furi.h>
  4. #include "cli_i.h"
  5. #define TAG "CliVcp"
  6. #define USB_CDC_PKT_LEN CDC_DATA_SZ
  7. #define VCP_RX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  8. #define VCP_TX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  9. #define VCP_IF_NUM 0
  10. #ifdef CLI_VCP_DEBUG
  11. #define VCP_DEBUG(...) FURI_LOG_D(TAG, __VA_ARGS__)
  12. #else
  13. #define VCP_DEBUG(...)
  14. #endif
  15. typedef enum {
  16. VcpEvtStop = (1 << 0),
  17. VcpEvtConnect = (1 << 1),
  18. VcpEvtDisconnect = (1 << 2),
  19. VcpEvtStreamRx = (1 << 3),
  20. VcpEvtRx = (1 << 4),
  21. VcpEvtStreamTx = (1 << 5),
  22. VcpEvtTx = (1 << 6),
  23. } WorkerEvtFlags;
  24. #define VCP_THREAD_FLAG_ALL \
  25. (VcpEvtStop | VcpEvtConnect | VcpEvtDisconnect | VcpEvtRx | VcpEvtTx | VcpEvtStreamRx | \
  26. VcpEvtStreamTx)
  27. typedef struct {
  28. FuriThread* thread;
  29. FuriStreamBuffer* tx_stream;
  30. FuriStreamBuffer* rx_stream;
  31. volatile bool connected;
  32. volatile bool running;
  33. FuriHalUsbInterface* usb_if_prev;
  34. uint8_t data_buffer[USB_CDC_PKT_LEN];
  35. } CliVcp;
  36. static int32_t vcp_worker(void* context);
  37. static void vcp_on_cdc_tx_complete(void* context);
  38. static void vcp_on_cdc_rx(void* context);
  39. static void vcp_state_callback(void* context, uint8_t state);
  40. static void vcp_on_cdc_control_line(void* context, uint8_t state);
  41. static CdcCallbacks cdc_cb = {
  42. vcp_on_cdc_tx_complete,
  43. vcp_on_cdc_rx,
  44. vcp_state_callback,
  45. vcp_on_cdc_control_line,
  46. NULL,
  47. };
  48. static CliVcp* vcp = NULL;
  49. static const uint8_t ascii_soh = 0x01;
  50. static const uint8_t ascii_eot = 0x04;
  51. static void cli_vcp_init() {
  52. if(vcp == NULL) {
  53. vcp = malloc(sizeof(CliVcp));
  54. vcp->tx_stream = furi_stream_buffer_alloc(VCP_TX_BUF_SIZE, 1);
  55. vcp->rx_stream = furi_stream_buffer_alloc(VCP_RX_BUF_SIZE, 1);
  56. }
  57. furi_assert(vcp->thread == NULL);
  58. vcp->connected = false;
  59. vcp->thread = furi_thread_alloc_ex("CliVcpWorker", 1024, vcp_worker, NULL);
  60. furi_thread_start(vcp->thread);
  61. FURI_LOG_I(TAG, "Init OK");
  62. }
  63. static void cli_vcp_deinit() {
  64. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtStop);
  65. furi_thread_join(vcp->thread);
  66. furi_thread_free(vcp->thread);
  67. vcp->thread = NULL;
  68. }
  69. static int32_t vcp_worker(void* context) {
  70. UNUSED(context);
  71. bool tx_idle = true;
  72. size_t missed_rx = 0;
  73. uint8_t last_tx_pkt_len = 0;
  74. // Switch USB to VCP mode (if it is not set yet)
  75. vcp->usb_if_prev = furi_hal_usb_get_config();
  76. if((vcp->usb_if_prev != &usb_cdc_single) && (vcp->usb_if_prev != &usb_cdc_dual)) {
  77. furi_hal_usb_set_config(&usb_cdc_single, NULL);
  78. }
  79. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb, NULL);
  80. FURI_LOG_D(TAG, "Start");
  81. vcp->running = true;
  82. while(1) {
  83. uint32_t flags =
  84. furi_thread_flags_wait(VCP_THREAD_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  85. furi_assert(!(flags & FuriFlagError));
  86. // VCP session opened
  87. if(flags & VcpEvtConnect) {
  88. VCP_DEBUG("Connect");
  89. if(vcp->connected == false) {
  90. vcp->connected = true;
  91. furi_stream_buffer_send(vcp->rx_stream, &ascii_soh, 1, FuriWaitForever);
  92. }
  93. }
  94. // VCP session closed
  95. if(flags & VcpEvtDisconnect) {
  96. VCP_DEBUG("Disconnect");
  97. if(vcp->connected == true) {
  98. vcp->connected = false;
  99. furi_stream_buffer_receive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  100. furi_stream_buffer_send(vcp->rx_stream, &ascii_eot, 1, FuriWaitForever);
  101. }
  102. }
  103. // Rx buffer was read, maybe there is enough space for new data?
  104. if((flags & VcpEvtStreamRx) && (missed_rx > 0)) {
  105. VCP_DEBUG("StreamRx");
  106. if(furi_stream_buffer_spaces_available(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  107. flags |= VcpEvtRx;
  108. missed_rx--;
  109. }
  110. }
  111. // New data received
  112. if(flags & VcpEvtRx) {
  113. if(furi_stream_buffer_spaces_available(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  114. int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  115. VCP_DEBUG("Rx %ld", len);
  116. if(len > 0) {
  117. furi_check(
  118. furi_stream_buffer_send(
  119. vcp->rx_stream, vcp->data_buffer, len, FuriWaitForever) ==
  120. (size_t)len);
  121. }
  122. } else {
  123. VCP_DEBUG("Rx missed");
  124. missed_rx++;
  125. }
  126. }
  127. // New data in Tx buffer
  128. if(flags & VcpEvtStreamTx) {
  129. VCP_DEBUG("StreamTx");
  130. if(tx_idle) {
  131. flags |= VcpEvtTx;
  132. }
  133. }
  134. // CDC write transfer done
  135. if(flags & VcpEvtTx) {
  136. size_t len =
  137. furi_stream_buffer_receive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  138. VCP_DEBUG("Tx %d", len);
  139. if(len > 0) { // Some data left in Tx buffer. Sending it now
  140. tx_idle = false;
  141. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  142. last_tx_pkt_len = len;
  143. } else { // There is nothing to send.
  144. if(last_tx_pkt_len == 64) {
  145. // Send extra zero-length packet if last packet len is 64 to indicate transfer end
  146. furi_hal_cdc_send(VCP_IF_NUM, NULL, 0);
  147. } else {
  148. // Set flag to start next transfer instantly
  149. tx_idle = true;
  150. }
  151. last_tx_pkt_len = 0;
  152. }
  153. }
  154. if(flags & VcpEvtStop) {
  155. vcp->connected = false;
  156. vcp->running = false;
  157. furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL, NULL);
  158. // Restore previous USB mode (if it was set during init)
  159. if((vcp->usb_if_prev != &usb_cdc_single) && (vcp->usb_if_prev != &usb_cdc_dual)) {
  160. furi_hal_usb_unlock();
  161. furi_hal_usb_set_config(vcp->usb_if_prev, NULL);
  162. }
  163. furi_stream_buffer_receive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  164. furi_stream_buffer_send(vcp->rx_stream, &ascii_eot, 1, FuriWaitForever);
  165. break;
  166. }
  167. }
  168. FURI_LOG_D(TAG, "End");
  169. return 0;
  170. }
  171. static size_t cli_vcp_rx(uint8_t* buffer, size_t size, uint32_t timeout) {
  172. furi_assert(vcp);
  173. furi_assert(buffer);
  174. if(vcp->running == false) {
  175. return 0;
  176. }
  177. VCP_DEBUG("rx %u start", size);
  178. size_t rx_cnt = 0;
  179. while(size > 0) {
  180. size_t batch_size = size;
  181. if(batch_size > VCP_RX_BUF_SIZE) batch_size = VCP_RX_BUF_SIZE;
  182. size_t len = furi_stream_buffer_receive(vcp->rx_stream, buffer, batch_size, timeout);
  183. VCP_DEBUG("rx %u ", batch_size);
  184. if(len == 0) break;
  185. if(vcp->running == false) {
  186. // EOT command is received after VCP session close
  187. rx_cnt += len;
  188. break;
  189. }
  190. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtStreamRx);
  191. size -= len;
  192. buffer += len;
  193. rx_cnt += len;
  194. }
  195. VCP_DEBUG("rx %u end", size);
  196. return rx_cnt;
  197. }
  198. static void cli_vcp_tx(const uint8_t* buffer, size_t size) {
  199. furi_assert(vcp);
  200. furi_assert(buffer);
  201. if(vcp->running == false) {
  202. return;
  203. }
  204. VCP_DEBUG("tx %u start", size);
  205. while(size > 0 && vcp->connected) {
  206. size_t batch_size = size;
  207. if(batch_size > USB_CDC_PKT_LEN) batch_size = USB_CDC_PKT_LEN;
  208. furi_stream_buffer_send(vcp->tx_stream, buffer, batch_size, FuriWaitForever);
  209. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtStreamTx);
  210. VCP_DEBUG("tx %u", batch_size);
  211. size -= batch_size;
  212. buffer += batch_size;
  213. }
  214. VCP_DEBUG("tx %u end", size);
  215. }
  216. static void cli_vcp_tx_stdout(const char* data, size_t size) {
  217. cli_vcp_tx((const uint8_t*)data, size);
  218. }
  219. static void vcp_state_callback(void* context, uint8_t state) {
  220. UNUSED(context);
  221. if(state == 0) {
  222. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtDisconnect);
  223. }
  224. }
  225. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  226. UNUSED(context);
  227. // bit 0: DTR state, bit 1: RTS state
  228. bool dtr = state & (1 << 0);
  229. if(dtr == true) {
  230. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtConnect);
  231. } else {
  232. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtDisconnect);
  233. }
  234. }
  235. static void vcp_on_cdc_rx(void* context) {
  236. UNUSED(context);
  237. uint32_t ret = furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtRx);
  238. furi_check(!(ret & FuriFlagError));
  239. }
  240. static void vcp_on_cdc_tx_complete(void* context) {
  241. UNUSED(context);
  242. furi_thread_flags_set(furi_thread_get_id(vcp->thread), VcpEvtTx);
  243. }
  244. static bool cli_vcp_is_connected(void) {
  245. furi_assert(vcp);
  246. return vcp->connected;
  247. }
  248. CliSession cli_vcp = {
  249. cli_vcp_init,
  250. cli_vcp_deinit,
  251. cli_vcp_rx,
  252. cli_vcp_tx,
  253. cli_vcp_tx_stdout,
  254. cli_vcp_is_connected,
  255. };