forked from enkiller/vconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vconsole.c
375 lines (322 loc) · 7.95 KB
/
vconsole.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-12-15 tyx first implementation
*/
#include <rtdevice.h>
#include <string.h>
#include <vconsole.h>
#include <finsh.h>
#define PRINT rt_kprintf
#define VC_EVENT_RECV (0x1 << 0)
#define VC_EVENT_EXIT (0x1 << 1)
int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_get_console(void);
static rt_size_t _read(rt_device_t dev, rt_off_t pos, void *buffer,
rt_size_t size);
static rt_size_t _write(rt_device_t dev, rt_off_t pos, const void *buffer,
rt_size_t size);
#ifdef RT_USING_POSIX
#include <dfs_posix.h>
#include <dfs_poll.h>
static rt_err_t fops_rx_ind(rt_device_t dev, rt_size_t size)
{
rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
return RT_EOK;
}
/* fops for vc */
static int _fops_open(struct dfs_fd *fd)
{
rt_err_t ret = 0;
rt_uint16_t flags = 0;
rt_device_t device;
device = (rt_device_t)fd->data;
RT_ASSERT(device != RT_NULL);
switch (fd->flags & O_ACCMODE)
{
case O_RDONLY:
flags = RT_DEVICE_FLAG_RDONLY;
break;
case O_WRONLY:
flags = RT_DEVICE_FLAG_WRONLY;
break;
case O_RDWR:
flags = RT_DEVICE_FLAG_RDWR;
break;
default:
break;
}
if ((fd->flags & O_ACCMODE) != O_WRONLY)
rt_device_set_rx_indicate(device, fops_rx_ind);
ret = rt_device_open(device, flags);
if (ret == RT_EOK) return 0;
return ret;
}
static int _fops_close(struct dfs_fd *fd)
{
rt_device_t device;
device = (rt_device_t)fd->data;
rt_device_set_rx_indicate(device, RT_NULL);
rt_device_close(device);
return 0;
}
static int _fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
{
rt_device_t device;
device = (rt_device_t)fd->data;
switch (cmd)
{
case FIONREAD:
break;
case FIONWRITE:
break;
}
return rt_device_control(device, cmd, args);
}
static int _fops_read(struct dfs_fd *fd, void *buf, size_t count)
{
int size = 0;
rt_device_t device;
device = (rt_device_t)fd->data;
do
{
size = rt_device_read(device, -1, buf, count);
if (size <= 0)
{
if (fd->flags & O_NONBLOCK)
{
size = -EAGAIN;
break;
}
rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
}
}while (size <= 0);
return size;
}
static int _fops_write(struct dfs_fd *fd, const void *buf, size_t count)
{
rt_device_t device;
device = (rt_device_t)fd->data;
return rt_device_write(device, -1, buf, count);
}
static int _fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
{
int mask = 0;
int flags = 0;
rt_device_t device;
vconsole_t vc;
device = (rt_device_t)fd->data;
RT_ASSERT(device != RT_NULL);
vc = (vconsole_t)device;
/* only support POLLIN */
flags = fd->flags & O_ACCMODE;
if (flags == O_RDONLY || flags == O_RDWR)
{
rt_poll_add(&(device->wait_queue), req);
if (rt_ringbuffer_data_len(&vc->ring_buff) > 0)
mask |= POLLIN;
}
return mask;
}
const static struct dfs_file_ops _vc_fops =
{
_fops_open,
_fops_close,
_fops_ioctl,
_fops_read,
_fops_write,
RT_NULL, /* flush */
RT_NULL, /* lseek */
RT_NULL, /* getdents */
_fops_poll,
};
#endif
static rt_size_t _read(rt_device_t dev, rt_off_t pos, void *buffer,
rt_size_t size)
{
vconsole_t vc = (vconsole_t)dev;
rt_size_t len;
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_take(&vc->mutex);
#else
rt_enter_critical();
#endif
len = rt_ringbuffer_get(&vc->ring_buff, buffer, size);
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_release(&vc->mutex);
#else
rt_exit_critical();
#endif
return len;
}
static rt_size_t _write(rt_device_t dev, rt_off_t pos, const void *buffer,
rt_size_t size)
{
vconsole_t vc = (vconsole_t)dev;
rt_size_t len, w_len;
rt_uint8_t *bstr;
const rt_uint8_t *str = buffer;
int index = 0, lf = 0;
int i;
w_len = 0;
bstr = vc->cache;
for (i = 0; i < size && index < vc->cache_size; i++)
{
if(str[i] == '\n')
{
bstr[index++] = '\r';
lf ++;
}
bstr[index++] = str[i];
if (index >= vc->cache_size - 1)
{
len = vc->output(dev, vc->cache, vc->cache_size);
index = 0;
w_len += len;
}
}
if (index > 0)
{
len = vc->output(dev, vc->cache, index);
w_len += len;
}
if (w_len > lf)
w_len -= lf;
return w_len;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops _vc_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_read,
_write,
RT_NULL
};
#endif
rt_device_t vconsole_create(const char *name, vc_output_t out)
{
vconsole_t vc;
int ret;
rt_device_t vc_dev;
if (out == RT_NULL)
{
return RT_NULL;
}
/* create virtual console device */
vc = rt_calloc(1, sizeof(struct vconsole) +
VCONSOLE_CACHE_SIZE +
VCONSOLE_POOL_SIZE);
if (vc == RT_NULL)
{
return RT_NULL;
}
vc->magic = VCONSOLE_MAGIC_NUM;
vc->output = out;
vc->cache_size = VCONSOLE_CACHE_SIZE;
vc->ring_size = VCONSOLE_POOL_SIZE;
rt_ringbuffer_init(&vc->ring_buff, vc->ring_pool, vc->ring_size);
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_init(&vc->mutex, name, RT_IPC_FLAG_FIFO);
#endif
/* init virtual console device */
vc_dev = &vc->parent;
vc_dev->type = RT_Device_Class_Char;
#ifdef RT_USING_DEVICE_OPS
vc_dev->ops = &_vc_ops;
#else
vc_dev->init = RT_NULL;
vc_dev->open = RT_NULL;
vc_dev->close = RT_NULL;
vc_dev->read = _read;
vc_dev->write = _write;
vc_dev->control = RT_NULL;
#endif
vc_dev->user_data = RT_NULL;
/* register virtual console device */
ret = rt_device_register(vc_dev, name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
if (ret != RT_EOK)
{
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_detach(&vc->mutex);
#endif
rt_free(vc);
return RT_NULL;
}
#ifdef RT_USING_POSIX
/* set fops */
vc_dev->fops = &_vc_fops;
#endif
return vc_dev;
}
rt_device_t vconsole_switch(rt_device_t device)
{
rt_device_t old_dev;
if (device == RT_NULL)
return RT_NULL;
old_dev = rt_console_get_device();
rt_console_set_device(device->parent.name);
#ifdef RT_USING_POSIX
{
int fd, flags;
fd = libc_stdio_get_console();
flags = ioctl(fd, F_GETFL, 0);
ioctl(fd, F_SETFL, (void *)(flags | O_NONBLOCK));
libc_stdio_set_console(device->parent.name, O_RDWR);
rt_wqueue_wakeup(&(old_dev->wait_queue), (void*)POLLERR);
rt_thread_mdelay(20);
}
#else
finsh_set_device(device->parent.name);
#endif
return old_dev;
}
rt_err_t vconsole_delete(rt_device_t device)
{
vconsole_t vc = (vconsole_t)device;
if (vc == RT_NULL)
{
return -RT_EINVAL;
}
if (device == rt_console_get_device())
{
return -RT_EBUSY;
}
if (vc->magic != VCONSOLE_MAGIC_NUM)
{
return -RT_ERROR;
}
rt_device_unregister(device);
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_detach(&vc->mutex);
#endif
rt_free(vc);
return RT_EOK;
}
rt_size_t vconsole_input(rt_device_t device, const rt_uint8_t *buff, rt_size_t size)
{
rt_size_t len;
vconsole_t vc = (vconsole_t)device;
RT_ASSERT(vc != RT_NULL);
RT_ASSERT(vc->magic == VCONSOLE_MAGIC_NUM);
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_take(&vc->mutex);
#else
rt_enter_critical();
#endif
len = rt_ringbuffer_put(&vc->ring_buff, buff, size);
#ifdef VCONSOLE_USING_MUTEX
rt_mutex_release(&vc->mutex);
#else
rt_exit_critical();
#endif
if (len > 0 && device->rx_indicate)
{
device->rx_indicate(device, len);
}
return len;
}