cli_vcp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. furi_thread_flags_set(furi_thread_get_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. UNUSED(context);
  70. bool tx_idle = true;
  71. size_t missed_rx = 0;
  72. uint8_t last_tx_pkt_len = 0;
  73. // Switch USB to VCP mode (if it is not set yet)
  74. vcp->usb_if_prev = furi_hal_usb_get_config();
  75. if((vcp->usb_if_prev != &usb_cdc_single) && (vcp->usb_if_prev != &usb_cdc_dual)) {
  76. furi_hal_usb_set_config(&usb_cdc_single, NULL);
  77. }
  78. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb, NULL);
  79. FURI_LOG_D(TAG, "Start");
  80. vcp->running = true;
  81. while(1) {
  82. uint32_t flags =
  83. furi_thread_flags_wait(VCP_THREAD_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  84. furi_assert((flags & FuriFlagError) == 0);
  85. // VCP session opened
  86. if(flags & VcpEvtConnect) {
  87. #ifdef CLI_VCP_DEBUG
  88. FURI_LOG_D(TAG, "Connect");
  89. #endif
  90. if(vcp->connected == false) {
  91. vcp->connected = true;
  92. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, FuriWaitForever);
  93. }
  94. }
  95. // VCP session closed
  96. if(flags & VcpEvtDisconnect) {
  97. #ifdef CLI_VCP_DEBUG
  98. FURI_LOG_D(TAG, "Disconnect");
  99. #endif
  100. if(vcp->connected == true) {
  101. vcp->connected = false;
  102. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  103. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, FuriWaitForever);
  104. }
  105. }
  106. // Rx buffer was read, maybe there is enough space for new data?
  107. if((flags & VcpEvtStreamRx) && (missed_rx > 0)) {
  108. #ifdef CLI_VCP_DEBUG
  109. FURI_LOG_D(TAG, "StreamRx");
  110. #endif
  111. if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  112. flags |= VcpEvtRx;
  113. missed_rx--;
  114. }
  115. }
  116. // New data received
  117. if(flags & VcpEvtRx) {
  118. if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  119. int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  120. #ifdef CLI_VCP_DEBUG
  121. FURI_LOG_D(TAG, "Rx %d", len);
  122. #endif
  123. if(len > 0) {
  124. furi_check(
  125. xStreamBufferSend(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. xStreamBufferReceive(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. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  176. xStreamBufferSend(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 = xStreamBufferReceive(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. xStreamBufferSend(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. };