usbd_adb_shell.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <rtthread.h>
  7. #include <rtdevice.h>
  8. #include "usbd_core.h"
  9. #include "usbd_adb.h"
  10. #ifndef CONFIG_USBDEV_SHELL_RX_BUFSIZE
  11. #define CONFIG_USBDEV_SHELL_RX_BUFSIZE (2048)
  12. #endif
  13. struct usbd_adb_shell {
  14. struct rt_device parent;
  15. usb_osal_sem_t tx_done;
  16. struct rt_ringbuffer rx_rb;
  17. rt_uint8_t rx_rb_buffer[CONFIG_USBDEV_SHELL_RX_BUFSIZE];
  18. } g_usbd_adb_shell;
  19. void usbd_adb_notify_shell_read(uint8_t *data, uint32_t len)
  20. {
  21. rt_ringbuffer_put(&g_usbd_adb_shell.rx_rb, data, len);
  22. if (g_usbd_adb_shell.parent.rx_indicate) {
  23. g_usbd_adb_shell.parent.rx_indicate(&g_usbd_adb_shell.parent, len);
  24. }
  25. }
  26. void usbd_adb_notify_write_done(void)
  27. {
  28. if (g_usbd_adb_shell.tx_done) {
  29. usb_osal_sem_give(g_usbd_adb_shell.tx_done);
  30. }
  31. }
  32. static rt_err_t usbd_adb_shell_open(struct rt_device *dev, rt_uint16_t oflag)
  33. {
  34. while (!usb_device_is_configured(0)) {
  35. rt_thread_mdelay(10);
  36. }
  37. return RT_EOK;
  38. }
  39. static rt_err_t usbd_adb_shell_close(struct rt_device *dev)
  40. {
  41. if (g_usbd_adb_shell.tx_done) {
  42. usb_osal_sem_give(g_usbd_adb_shell.tx_done);
  43. }
  44. return RT_EOK;
  45. }
  46. static rt_ssize_t usbd_adb_shell_read(struct rt_device *dev,
  47. rt_off_t pos,
  48. void *buffer,
  49. rt_size_t size)
  50. {
  51. return rt_ringbuffer_get(&g_usbd_adb_shell.rx_rb, (rt_uint8_t *)buffer, size);
  52. }
  53. static rt_ssize_t usbd_adb_shell_write(struct rt_device *dev,
  54. rt_off_t pos,
  55. const void *buffer,
  56. rt_size_t size)
  57. {
  58. int ret = 0;
  59. RT_ASSERT(dev != RT_NULL);
  60. if (!usb_device_is_configured(0)) {
  61. return size;
  62. }
  63. if (usbd_adb_can_write() && size) {
  64. usb_osal_sem_reset(g_usbd_adb_shell.tx_done);
  65. usbd_abd_write(ADB_SHELL_LOALID, buffer, size);
  66. usb_osal_sem_take(g_usbd_adb_shell.tx_done, 0xffffffff);
  67. }
  68. return size;
  69. }
  70. #ifdef RT_USING_DEVICE_OPS
  71. const static struct rt_device_ops usbd_adb_shell_ops = {
  72. NULL,
  73. usbd_adb_shell_open,
  74. usbd_adb_shell_close,
  75. usbd_adb_shell_read,
  76. usbd_adb_shell_write,
  77. NULL
  78. };
  79. #endif
  80. void usbd_adb_shell_init(uint8_t in_ep, uint8_t out_ep)
  81. {
  82. rt_err_t ret;
  83. struct rt_device *device;
  84. device = &(g_usbd_adb_shell.parent);
  85. device->type = RT_Device_Class_Char;
  86. device->rx_indicate = RT_NULL;
  87. device->tx_complete = RT_NULL;
  88. #ifdef RT_USING_DEVICE_OPS
  89. device->ops = &usbd_adb_shell_ops;
  90. #else
  91. device->init = NULL;
  92. device->open = usbd_adb_shell_open;
  93. device->close = usbd_adb_shell_close;
  94. device->read = usbd_adb_shell_read;
  95. device->write = usbd_adb_shell_write;
  96. device->control = NULL;
  97. #endif
  98. device->user_data = NULL;
  99. /* register a character device */
  100. ret = rt_device_register(device, "adb-sh", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
  101. #ifdef RT_USING_POSIX_DEVIO
  102. /* set fops */
  103. device->fops = NULL;
  104. #endif
  105. g_usbd_adb_shell.tx_done = usb_osal_sem_create(0);
  106. rt_ringbuffer_init(&g_usbd_adb_shell.rx_rb, g_usbd_adb_shell.rx_rb_buffer, sizeof(g_usbd_adb_shell.rx_rb_buffer));
  107. }
  108. static int adb_enter(int argc, char **argv)
  109. {
  110. (void)argc;
  111. (void)argv;
  112. finsh_set_device("adb-sh");
  113. rt_console_set_device("adb-sh");
  114. return 0;
  115. }
  116. MSH_CMD_EXPORT(adb_enter, adb_enter);
  117. static int adb_exit(int argc, char **argv)
  118. {
  119. (void)argc;
  120. (void)argv;
  121. usbd_adb_close(ADB_SHELL_LOALID);
  122. finsh_set_device(RT_CONSOLE_DEVICE_NAME);
  123. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  124. return 0;
  125. }
  126. MSH_CMD_EXPORT(adb_exit, adb_exit);