cli_vcp.c 9.2 KB

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