1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Copyright (c) 2024 ~ 2026, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "usbd_core.h"
#include "usbd_adb.h"
#ifdef RT_USING_FINSH
#include <shell.h>
#include <finsh.h>
#endif
#ifndef CONFIG_USBDEV_SHELL_RX_BUFSIZE
#define CONFIG_USBDEV_SHELL_RX_BUFSIZE (2048)
#endif
struct usbd_adb_shell {
struct rt_device parent;
struct rt_ringbuffer rx_rb;
rt_uint8_t rx_rb_buffer[CONFIG_USBDEV_SHELL_RX_BUFSIZE];
} g_usbd_adb_shell;
static rt_err_t usbd_adb_shell_open(struct rt_device *dev, rt_uint16_t oflag)
{
while (!usb_device_is_configured(0)) {
rt_thread_mdelay(10);
}
return RT_EOK;
}
static rt_err_t usbd_adb_shell_close(struct rt_device *dev)
{
return RT_EOK;
}
static rt_ssize_t usbd_adb_shell_read(struct rt_device *dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
return rt_ringbuffer_get(&g_usbd_adb_shell.rx_rb, (rt_uint8_t *)buffer, size);
}
static rt_ssize_t usbd_adb_shell_write(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
int ret = 0;
RT_ASSERT(dev != RT_NULL);
usbd_adb_write(ADB_LOCALID_SHELL, buffer, size);
return size;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops usbd_adb_shell_ops = {
NULL,
usbd_adb_shell_open,
usbd_adb_shell_close,
usbd_adb_shell_read,
usbd_adb_shell_write,
NULL
};
#endif
void rt_usbd_adb_shell_init(void)
{
rt_err_t ret;
struct rt_device *device;
device = &(g_usbd_adb_shell.parent);
device->type = RT_Device_Class_Char;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &usbd_adb_shell_ops;
#else
device->init = NULL;
device->open = usbd_adb_shell_open;
device->close = usbd_adb_shell_close;
device->read = usbd_adb_shell_read;
device->write = usbd_adb_shell_write;
device->control = NULL;
#endif
device->user_data = NULL;
/* register a character device */
ret = rt_device_register(device, "adb-sh", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
#ifdef RT_USING_POSIX_DEVIO
/* set fops */
device->fops = NULL;
#endif
rt_ringbuffer_init(&g_usbd_adb_shell.rx_rb, g_usbd_adb_shell.rx_rb_buffer, sizeof(g_usbd_adb_shell.rx_rb_buffer));
}
static void adb_shell_on_open(uint32_t remoteid)
{
USB_LOG_INFO("adb shell open, remoteid:%u\r\n", (unsigned)remoteid);
finsh_set_device("adb-sh");
rt_console_set_device("adb-sh");
}
static void adb_shell_on_close(uint32_t remoteid)
{
USB_LOG_INFO("adb shell close, remoteid:%u\r\n", (unsigned)remoteid);
}
static void adb_shell_on_write(uint32_t remoteid, const uint8_t *data, uint32_t len)
{
rt_size_t put;
put = rt_ringbuffer_put(&g_usbd_adb_shell.rx_rb, data, len);
if (g_usbd_adb_shell.parent.rx_indicate != RT_NULL) {
g_usbd_adb_shell.parent.rx_indicate(&g_usbd_adb_shell.parent, put);
}
}
static const struct adb_service adb_shell_service = {
.name = "shell:",
.localid = ADB_LOCALID_SHELL,
.on_open = adb_shell_on_open,
.on_close = adb_shell_on_close,
.on_write = adb_shell_on_write,
};
void usbd_adb_shell_init(void)
{
if (usbd_adb_service_register(&adb_shell_service) != 0) {
USB_LOG_ERR("adb shell: register failed\r\n");
}
rt_usbd_adb_shell_init();
}
static int adb_exit(int argc, char **argv)
{
(void)argc;
(void)argv;
usbd_adb_close(ADB_LOCALID_SHELL);
if (rt_console_get_device() != rt_device_find(RT_CONSOLE_DEVICE_NAME)) {
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
finsh_set_device(RT_CONSOLE_DEVICE_NAME);
}
return 0;
}
MSH_CMD_EXPORT(adb_exit, adb_exit);
|