vconsole.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2020-12-15 tyx first implementation
  7. */
  8. #include <rtdevice.h>
  9. #include <string.h>
  10. #include <vconsole.h>
  11. #include <finsh.h>
  12. #define PRINT rt_kprintf
  13. #define VC_EVENT_RECV (0x1 << 0)
  14. #define VC_EVENT_EXIT (0x1 << 1)
  15. int libc_stdio_set_console(const char* device_name, int mode);
  16. int libc_stdio_get_console(void);
  17. static rt_size_t _read(rt_device_t dev, rt_off_t pos, void *buffer,
  18. rt_size_t size);
  19. static rt_size_t _write(rt_device_t dev, rt_off_t pos, const void *buffer,
  20. rt_size_t size);
  21. #ifdef RT_USING_POSIX
  22. #include <dfs_posix.h>
  23. #include <dfs_poll.h>
  24. static rt_err_t fops_rx_ind(rt_device_t dev, rt_size_t size)
  25. {
  26. rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
  27. return RT_EOK;
  28. }
  29. /* fops for vc */
  30. static int _fops_open(struct dfs_fd *fd)
  31. {
  32. rt_err_t ret = 0;
  33. rt_uint16_t flags = 0;
  34. rt_device_t device;
  35. device = (rt_device_t)fd->data;
  36. RT_ASSERT(device != RT_NULL);
  37. switch (fd->flags & O_ACCMODE)
  38. {
  39. case O_RDONLY:
  40. flags = RT_DEVICE_FLAG_RDONLY;
  41. break;
  42. case O_WRONLY:
  43. flags = RT_DEVICE_FLAG_WRONLY;
  44. break;
  45. case O_RDWR:
  46. flags = RT_DEVICE_FLAG_RDWR;
  47. break;
  48. default:
  49. break;
  50. }
  51. if ((fd->flags & O_ACCMODE) != O_WRONLY)
  52. rt_device_set_rx_indicate(device, fops_rx_ind);
  53. ret = rt_device_open(device, flags);
  54. if (ret == RT_EOK) return 0;
  55. return ret;
  56. }
  57. static int _fops_close(struct dfs_fd *fd)
  58. {
  59. rt_device_t device;
  60. device = (rt_device_t)fd->data;
  61. rt_device_set_rx_indicate(device, RT_NULL);
  62. rt_device_close(device);
  63. return 0;
  64. }
  65. static int _fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  66. {
  67. rt_device_t device;
  68. device = (rt_device_t)fd->data;
  69. switch (cmd)
  70. {
  71. case FIONREAD:
  72. break;
  73. case FIONWRITE:
  74. break;
  75. }
  76. return rt_device_control(device, cmd, args);
  77. }
  78. static int _fops_read(struct dfs_fd *fd, void *buf, size_t count)
  79. {
  80. int size = 0;
  81. rt_device_t device;
  82. device = (rt_device_t)fd->data;
  83. do
  84. {
  85. size = rt_device_read(device, -1, buf, count);
  86. if (size <= 0)
  87. {
  88. if (fd->flags & O_NONBLOCK)
  89. {
  90. size = -EAGAIN;
  91. break;
  92. }
  93. rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
  94. }
  95. }while (size <= 0);
  96. return size;
  97. }
  98. static int _fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  99. {
  100. rt_device_t device;
  101. device = (rt_device_t)fd->data;
  102. return rt_device_write(device, -1, buf, count);
  103. }
  104. static int _fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  105. {
  106. int mask = 0;
  107. int flags = 0;
  108. rt_device_t device;
  109. vconsole_t vc;
  110. device = (rt_device_t)fd->data;
  111. RT_ASSERT(device != RT_NULL);
  112. vc = (vconsole_t)device;
  113. /* only support POLLIN */
  114. flags = fd->flags & O_ACCMODE;
  115. if (flags == O_RDONLY || flags == O_RDWR)
  116. {
  117. rt_poll_add(&(device->wait_queue), req);
  118. if (rt_ringbuffer_data_len(&vc->ring_buff) > 0)
  119. mask |= POLLIN;
  120. }
  121. return mask;
  122. }
  123. const static struct dfs_file_ops _vc_fops =
  124. {
  125. _fops_open,
  126. _fops_close,
  127. _fops_ioctl,
  128. _fops_read,
  129. _fops_write,
  130. RT_NULL, /* flush */
  131. RT_NULL, /* lseek */
  132. RT_NULL, /* getdents */
  133. _fops_poll,
  134. };
  135. #endif
  136. static rt_size_t _read(rt_device_t dev, rt_off_t pos, void *buffer,
  137. rt_size_t size)
  138. {
  139. vconsole_t vc = (vconsole_t)dev;
  140. rt_size_t len;
  141. #ifdef VCONSOLE_USING_MUTEX
  142. rt_mutex_take(&vc->mutex);
  143. #else
  144. rt_enter_critical();
  145. #endif
  146. len = rt_ringbuffer_get(&vc->ring_buff, buffer, size);
  147. #ifdef VCONSOLE_USING_MUTEX
  148. rt_mutex_release(&vc->mutex);
  149. #else
  150. rt_exit_critical();
  151. #endif
  152. return len;
  153. }
  154. static rt_size_t _write(rt_device_t dev, rt_off_t pos, const void *buffer,
  155. rt_size_t size)
  156. {
  157. vconsole_t vc = (vconsole_t)dev;
  158. rt_size_t len, w_len;
  159. rt_uint8_t *bstr;
  160. const rt_uint8_t *str = buffer;
  161. int index = 0, lf = 0;
  162. int i;
  163. w_len = 0;
  164. bstr = vc->cache;
  165. for (i = 0; i < size && index < vc->cache_size; i++)
  166. {
  167. if(str[i] == '\n')
  168. {
  169. bstr[index++] = '\r';
  170. lf ++;
  171. }
  172. bstr[index++] = str[i];
  173. if (index >= vc->cache_size - 1)
  174. {
  175. len = vc->output(dev, vc->cache, vc->cache_size);
  176. index = 0;
  177. w_len += len;
  178. }
  179. }
  180. if (index > 0)
  181. {
  182. len = vc->output(dev, vc->cache, index);
  183. w_len += len;
  184. }
  185. if (w_len > lf)
  186. w_len -= lf;
  187. return w_len;
  188. }
  189. #ifdef RT_USING_DEVICE_OPS
  190. const static struct rt_device_ops _vc_ops =
  191. {
  192. RT_NULL,
  193. RT_NULL,
  194. RT_NULL,
  195. _read,
  196. _write,
  197. RT_NULL
  198. };
  199. #endif
  200. rt_device_t vconsole_create(const char *name, vc_output_t out)
  201. {
  202. vconsole_t vc;
  203. int ret;
  204. rt_device_t vc_dev;
  205. if (out == RT_NULL)
  206. {
  207. return RT_NULL;
  208. }
  209. /* create virtual console device */
  210. vc = rt_calloc(1, sizeof(struct vconsole) +
  211. VCONSOLE_CACHE_SIZE +
  212. VCONSOLE_POOL_SIZE);
  213. if (vc == RT_NULL)
  214. {
  215. return RT_NULL;
  216. }
  217. vc->magic = VCONSOLE_MAGIC_NUM;
  218. vc->output = out;
  219. vc->cache_size = VCONSOLE_CACHE_SIZE;
  220. vc->ring_size = VCONSOLE_POOL_SIZE;
  221. rt_ringbuffer_init(&vc->ring_buff, vc->ring_pool, vc->ring_size);
  222. #ifdef VCONSOLE_USING_MUTEX
  223. rt_mutex_init(&vc->mutex, name, RT_IPC_FLAG_FIFO);
  224. #endif
  225. /* init virtual console device */
  226. vc_dev = &vc->parent;
  227. vc_dev->type = RT_Device_Class_Char;
  228. #ifdef RT_USING_DEVICE_OPS
  229. vc_dev->ops = &_vc_ops;
  230. #else
  231. vc_dev->init = RT_NULL;
  232. vc_dev->open = RT_NULL;
  233. vc_dev->close = RT_NULL;
  234. vc_dev->read = _read;
  235. vc_dev->write = _write;
  236. vc_dev->control = RT_NULL;
  237. #endif
  238. vc_dev->user_data = RT_NULL;
  239. /* register virtual console device */
  240. ret = rt_device_register(vc_dev, name,
  241. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  242. if (ret != RT_EOK)
  243. {
  244. #ifdef VCONSOLE_USING_MUTEX
  245. rt_mutex_detach(&vc->mutex);
  246. #endif
  247. rt_free(vc);
  248. return RT_NULL;
  249. }
  250. #ifdef RT_USING_POSIX
  251. /* set fops */
  252. vc_dev->fops = &_vc_fops;
  253. #endif
  254. return vc_dev;
  255. }
  256. rt_device_t vconsole_switch(rt_device_t device)
  257. {
  258. rt_device_t old_dev;
  259. if (device == RT_NULL)
  260. return RT_NULL;
  261. old_dev = rt_console_get_device();
  262. rt_console_set_device(device->parent.name);
  263. #ifdef RT_USING_POSIX
  264. {
  265. int fd, flags;
  266. fd = libc_stdio_get_console();
  267. flags = ioctl(fd, F_GETFL, 0);
  268. ioctl(fd, F_SETFL, (void *)(flags | O_NONBLOCK));
  269. libc_stdio_set_console(device->parent.name, O_RDWR);
  270. rt_wqueue_wakeup(&(old_dev->wait_queue), (void*)POLLERR);
  271. rt_thread_mdelay(20);
  272. }
  273. #else
  274. finsh_set_device(device->parent.name);
  275. #endif
  276. return old_dev;
  277. }
  278. rt_err_t vconsole_delete(rt_device_t device)
  279. {
  280. vconsole_t vc = (vconsole_t)device;
  281. if (vc == RT_NULL)
  282. {
  283. return -RT_EINVAL;
  284. }
  285. if (device == rt_console_get_device())
  286. {
  287. return -RT_EBUSY;
  288. }
  289. if (vc->magic != VCONSOLE_MAGIC_NUM)
  290. {
  291. return -RT_ERROR;
  292. }
  293. rt_device_unregister(device);
  294. #ifdef VCONSOLE_USING_MUTEX
  295. rt_mutex_detach(&vc->mutex);
  296. #endif
  297. rt_free(vc);
  298. return RT_EOK;
  299. }
  300. rt_size_t vconsole_input(rt_device_t device, const rt_uint8_t *buff, rt_size_t size)
  301. {
  302. rt_size_t len;
  303. vconsole_t vc = (vconsole_t)device;
  304. RT_ASSERT(vc != RT_NULL);
  305. RT_ASSERT(vc->magic == VCONSOLE_MAGIC_NUM);
  306. #ifdef VCONSOLE_USING_MUTEX
  307. rt_mutex_take(&vc->mutex);
  308. #else
  309. rt_enter_critical();
  310. #endif
  311. len = rt_ringbuffer_put(&vc->ring_buff, buff, size);
  312. #ifdef VCONSOLE_USING_MUTEX
  313. rt_mutex_release(&vc->mutex);
  314. #else
  315. rt_exit_critical();
  316. #endif
  317. if (len > 0 && device->rx_indicate)
  318. {
  319. device->rx_indicate(device, len);
  320. }
  321. return len;
  322. }