cli_vcp.c 9.3 KB

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