cli_vcp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. 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 = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  83. furi_assert((flags & osFlagsError) == 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. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  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. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  102. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  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(xStreamBufferSpacesAvailable(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(xStreamBufferSpacesAvailable(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. xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever) ==
  125. (size_t)len);
  126. }
  127. } else {
  128. #ifdef CLI_VCP_DEBUG
  129. FURI_LOG_D(TAG, "Rx missed");
  130. #endif
  131. missed_rx++;
  132. }
  133. }
  134. // New data in Tx buffer
  135. if(flags & VcpEvtStreamTx) {
  136. #ifdef CLI_VCP_DEBUG
  137. FURI_LOG_D(TAG, "StreamTx");
  138. #endif
  139. if(tx_idle) {
  140. flags |= VcpEvtTx;
  141. }
  142. }
  143. // CDC write transfer done
  144. if(flags & VcpEvtTx) {
  145. size_t len =
  146. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  147. #ifdef CLI_VCP_DEBUG
  148. FURI_LOG_D(TAG, "Tx %d", len);
  149. #endif
  150. if(len > 0) { // Some data left in Tx buffer. Sending it now
  151. tx_idle = false;
  152. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  153. last_tx_pkt_len = len;
  154. } else { // There is nothing to send.
  155. if(last_tx_pkt_len == 64) {
  156. // Send extra zero-length packet if last packet len is 64 to indicate transfer end
  157. furi_hal_cdc_send(VCP_IF_NUM, NULL, 0);
  158. } else {
  159. // Set flag to start next transfer instantly
  160. tx_idle = true;
  161. }
  162. last_tx_pkt_len = 0;
  163. }
  164. }
  165. if(flags & VcpEvtStop) {
  166. vcp->connected = false;
  167. vcp->running = false;
  168. furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL, NULL);
  169. // Restore previous USB mode (if it was set during init)
  170. if((vcp->usb_if_prev != &usb_cdc_single) && (vcp->usb_if_prev != &usb_cdc_dual)) {
  171. furi_hal_usb_set_config(vcp->usb_if_prev, NULL);
  172. }
  173. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  174. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  175. break;
  176. }
  177. }
  178. FURI_LOG_D(TAG, "End");
  179. return 0;
  180. }
  181. static size_t cli_vcp_rx(uint8_t* buffer, size_t size, uint32_t timeout) {
  182. furi_assert(vcp);
  183. furi_assert(buffer);
  184. if(vcp->running == false) {
  185. return 0;
  186. }
  187. #ifdef CLI_VCP_DEBUG
  188. FURI_LOG_D(TAG, "rx %u start", size);
  189. #endif
  190. size_t rx_cnt = 0;
  191. while(size > 0) {
  192. size_t batch_size = size;
  193. if(batch_size > VCP_RX_BUF_SIZE) batch_size = VCP_RX_BUF_SIZE;
  194. size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
  195. #ifdef CLI_VCP_DEBUG
  196. FURI_LOG_D(TAG, "rx %u ", batch_size);
  197. #endif
  198. if(len == 0) break;
  199. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamRx);
  200. size -= len;
  201. buffer += len;
  202. rx_cnt += len;
  203. }
  204. #ifdef CLI_VCP_DEBUG
  205. FURI_LOG_D(TAG, "rx %u end", size);
  206. #endif
  207. return rx_cnt;
  208. }
  209. static void cli_vcp_tx(const uint8_t* buffer, size_t size) {
  210. furi_assert(vcp);
  211. furi_assert(buffer);
  212. if(vcp->running == false) {
  213. return;
  214. }
  215. #ifdef CLI_VCP_DEBUG
  216. FURI_LOG_D(TAG, "tx %u start", size);
  217. #endif
  218. while(size > 0 && vcp->connected) {
  219. size_t batch_size = size;
  220. if(batch_size > USB_CDC_PKT_LEN) batch_size = USB_CDC_PKT_LEN;
  221. xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
  222. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamTx);
  223. #ifdef CLI_VCP_DEBUG
  224. FURI_LOG_D(TAG, "tx %u", batch_size);
  225. #endif
  226. size -= batch_size;
  227. buffer += batch_size;
  228. }
  229. #ifdef CLI_VCP_DEBUG
  230. FURI_LOG_D(TAG, "tx %u end", size);
  231. #endif
  232. }
  233. static void cli_vcp_tx_stdout(void* _cookie, const char* data, size_t size) {
  234. UNUSED(_cookie);
  235. cli_vcp_tx((const uint8_t*)data, size);
  236. }
  237. static void vcp_state_callback(void* context, uint8_t state) {
  238. UNUSED(context);
  239. if(state == 0) {
  240. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  241. }
  242. }
  243. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  244. UNUSED(context);
  245. // bit 0: DTR state, bit 1: RTS state
  246. bool dtr = state & (1 << 0);
  247. if(dtr == true) {
  248. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
  249. } else {
  250. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  251. }
  252. }
  253. static void vcp_on_cdc_rx(void* context) {
  254. UNUSED(context);
  255. uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
  256. furi_check((ret & osFlagsError) == 0);
  257. }
  258. static void vcp_on_cdc_tx_complete(void* context) {
  259. UNUSED(context);
  260. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
  261. }
  262. static bool cli_vcp_is_connected(void) {
  263. furi_assert(vcp);
  264. return vcp->connected;
  265. }
  266. CliSession cli_vcp = {
  267. cli_vcp_init,
  268. cli_vcp_deinit,
  269. cli_vcp_rx,
  270. cli_vcp_tx,
  271. cli_vcp_tx_stdout,
  272. cli_vcp_is_connected,
  273. };