cli_vcp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <furi_hal_usb_cdc_i.h>
  2. #include <furi_hal.h>
  3. #include <furi.h>
  4. #include <stream_buffer.h>
  5. #include "cli_i.h"
  6. #define TAG "CliVcp"
  7. #define USB_CDC_PKT_LEN CDC_DATA_SZ
  8. #define VCP_RX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  9. #define VCP_TX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  10. #define VCP_IF_NUM 0
  11. typedef enum {
  12. VcpEvtStop = (1 << 0),
  13. VcpEvtConnect = (1 << 1),
  14. VcpEvtDisconnect = (1 << 2),
  15. VcpEvtStreamRx = (1 << 3),
  16. VcpEvtRx = (1 << 4),
  17. VcpEvtStreamTx = (1 << 5),
  18. VcpEvtTx = (1 << 6),
  19. } WorkerEvtFlags;
  20. #define VCP_THREAD_FLAG_ALL \
  21. (VcpEvtStop | VcpEvtConnect | VcpEvtDisconnect | VcpEvtRx | VcpEvtTx | VcpEvtStreamRx | \
  22. VcpEvtStreamTx)
  23. typedef struct {
  24. FuriThread* thread;
  25. StreamBufferHandle_t tx_stream;
  26. StreamBufferHandle_t rx_stream;
  27. volatile bool connected;
  28. volatile bool running;
  29. FuriHalUsbInterface* usb_if_prev;
  30. uint8_t data_buffer[USB_CDC_PKT_LEN];
  31. } CliVcp;
  32. static int32_t vcp_worker(void* context);
  33. static void vcp_on_cdc_tx_complete(void* context);
  34. static void vcp_on_cdc_rx(void* context);
  35. static void vcp_state_callback(void* context, uint8_t state);
  36. static void vcp_on_cdc_control_line(void* context, uint8_t state);
  37. static CdcCallbacks cdc_cb = {
  38. vcp_on_cdc_tx_complete,
  39. vcp_on_cdc_rx,
  40. vcp_state_callback,
  41. vcp_on_cdc_control_line,
  42. NULL,
  43. };
  44. static CliVcp* vcp = NULL;
  45. static const uint8_t ascii_soh = 0x01;
  46. static const uint8_t ascii_eot = 0x04;
  47. static void cli_vcp_init() {
  48. if(vcp == NULL) {
  49. vcp = malloc(sizeof(CliVcp));
  50. vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
  51. vcp->rx_stream = xStreamBufferCreate(VCP_RX_BUF_SIZE, 1);
  52. }
  53. furi_assert(vcp->thread == NULL);
  54. vcp->connected = false;
  55. vcp->thread = furi_thread_alloc();
  56. furi_thread_set_name(vcp->thread, "CliVcpWorker");
  57. furi_thread_set_stack_size(vcp->thread, 1024);
  58. furi_thread_set_callback(vcp->thread, vcp_worker);
  59. furi_thread_start(vcp->thread);
  60. FURI_LOG_I(TAG, "Init OK");
  61. }
  62. static void cli_vcp_deinit() {
  63. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStop);
  64. furi_thread_join(vcp->thread);
  65. furi_thread_free(vcp->thread);
  66. vcp->thread = NULL;
  67. }
  68. static int32_t vcp_worker(void* 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 = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  82. furi_assert((flags & osFlagsError) == 0);
  83. // VCP session opened
  84. if(flags & VcpEvtConnect) {
  85. #ifdef CLI_VCP_DEBUG
  86. FURI_LOG_D(TAG, "Connect");
  87. #endif
  88. if(vcp->connected == false) {
  89. vcp->connected = true;
  90. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  91. }
  92. }
  93. // VCP session closed
  94. if(flags & VcpEvtDisconnect) {
  95. #ifdef CLI_VCP_DEBUG
  96. FURI_LOG_D(TAG, "Disconnect");
  97. #endif
  98. if(vcp->connected == true) {
  99. vcp->connected = false;
  100. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  101. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  102. }
  103. }
  104. // Rx buffer was read, maybe there is enough space for new data?
  105. if((flags & VcpEvtStreamRx) && (missed_rx > 0)) {
  106. #ifdef CLI_VCP_DEBUG
  107. FURI_LOG_D(TAG, "StreamRx");
  108. #endif
  109. if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  110. flags |= VcpEvtRx;
  111. missed_rx--;
  112. }
  113. }
  114. // New data received
  115. if(flags & VcpEvtRx) {
  116. if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  117. int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  118. #ifdef CLI_VCP_DEBUG
  119. FURI_LOG_D(TAG, "Rx %d", len);
  120. #endif
  121. if(len > 0) {
  122. furi_check(
  123. xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever) ==
  124. len);
  125. }
  126. } else {
  127. #ifdef CLI_VCP_DEBUG
  128. FURI_LOG_D(TAG, "Rx missed");
  129. #endif
  130. missed_rx++;
  131. }
  132. }
  133. // New data in Tx buffer
  134. if(flags & VcpEvtStreamTx) {
  135. #ifdef CLI_VCP_DEBUG
  136. FURI_LOG_D(TAG, "StreamTx");
  137. #endif
  138. if(tx_idle) {
  139. flags |= VcpEvtTx;
  140. }
  141. }
  142. // CDC write transfer done
  143. if(flags & VcpEvtTx) {
  144. size_t len =
  145. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  146. #ifdef CLI_VCP_DEBUG
  147. FURI_LOG_D(TAG, "Tx %d", len);
  148. #endif
  149. if(len > 0) { // Some data left in Tx buffer. Sending it now
  150. tx_idle = false;
  151. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  152. last_tx_pkt_len = len;
  153. } else { // There is nothing to send.
  154. if(last_tx_pkt_len == 64) {
  155. // Send extra zero-length packet if last packet len is 64 to indicate transfer end
  156. furi_hal_cdc_send(VCP_IF_NUM, NULL, 0);
  157. } else {
  158. // Set flag to start next transfer instantly
  159. tx_idle = true;
  160. }
  161. last_tx_pkt_len = 0;
  162. }
  163. }
  164. if(flags & VcpEvtStop) {
  165. vcp->connected = false;
  166. vcp->running = false;
  167. furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL, NULL);
  168. // Restore previous USB mode (if it was set during init)
  169. if((vcp->usb_if_prev != &usb_cdc_single) && (vcp->usb_if_prev != &usb_cdc_dual)) {
  170. furi_hal_usb_set_config(vcp->usb_if_prev, NULL);
  171. }
  172. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  173. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  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 = xStreamBufferReceive(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. osThreadFlagsSet(furi_thread_get_thread_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. xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
  221. osThreadFlagsSet(furi_thread_get_thread_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(void* _cookie, 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. if(state == 0) {
  237. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  238. }
  239. }
  240. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  241. // bit 0: DTR state, bit 1: RTS state
  242. bool dtr = state & (1 << 0);
  243. if(dtr == true) {
  244. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
  245. } else {
  246. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  247. }
  248. }
  249. static void vcp_on_cdc_rx(void* context) {
  250. uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
  251. furi_check((ret & osFlagsError) == 0);
  252. }
  253. static void vcp_on_cdc_tx_complete(void* context) {
  254. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
  255. }
  256. static bool cli_vcp_is_connected(void) {
  257. furi_assert(vcp);
  258. return vcp->connected;
  259. }
  260. CliSession cli_vcp = {
  261. cli_vcp_init,
  262. cli_vcp_deinit,
  263. cli_vcp_rx,
  264. cli_vcp_tx,
  265. cli_vcp_tx_stdout,
  266. cli_vcp_is_connected,
  267. };