lwp_console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-02-23 Jesven first version
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include "lwp_console.h"
  13. #define DBG_TAG "CONSOLE"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #define CHAR_CTRL_D 0x4
  17. #define CHAR_CTRL_C 0x3
  18. enum
  19. {
  20. CONSOLE_INIT_FLAG_NONE = 0,
  21. CONSOLE_INIT_FLAG_REGED,
  22. CONSOLE_INIT_FLAG_INITED,
  23. };
  24. static struct rt_console_device _console;
  25. rt_inline struct rt_wqueue *wait_queue_get(struct rt_lwp * lwp)
  26. {
  27. if (lwp == RT_PROCESS_KERNEL)
  28. {
  29. return &_console.wait_queue;
  30. }
  31. return &lwp->wait_queue;
  32. }
  33. rt_inline struct rt_wqueue *wait_queue_current_get(void)
  34. {
  35. return wait_queue_get(_console.foreground);
  36. }
  37. static void console_wakeup_check(struct rt_console_device *console)
  38. {
  39. rt_size_t len;
  40. struct rt_wqueue *wq;
  41. len = rt_ringbuffer_data_len(&console->input_rb);
  42. if (len)
  43. {
  44. wq = wait_queue_current_get();
  45. rt_wqueue_wakeup(wq, (void*)POLLIN);
  46. }
  47. }
  48. static void console_rx_notify(struct rt_device *dev)
  49. {
  50. struct rt_console_device *console;
  51. rt_size_t len;
  52. rt_uint8_t ch;
  53. console = (struct rt_console_device *)dev;
  54. RT_ASSERT(console != RT_NULL);
  55. while (1)
  56. {
  57. len = rt_device_read(console->iodev, -1, &ch, 1);
  58. if (len == 0)
  59. {
  60. break;
  61. }
  62. if (ch == CHAR_CTRL_D) /* ctrl-d */
  63. {
  64. console->foreground = RT_PROCESS_KERNEL;
  65. }
  66. else if (ch == CHAR_CTRL_C) /* ctrl-c */
  67. {
  68. lwp_terminate(console->foreground);
  69. }
  70. else
  71. {
  72. rt_ringbuffer_put_force(&console->input_rb, &ch, 1);
  73. }
  74. }
  75. console_wakeup_check(console);
  76. }
  77. void rt_console_set_foreground(struct rt_lwp *lwp)
  78. {
  79. rt_base_t level;
  80. level = rt_hw_interrupt_disable();
  81. if (_console.init_flag != CONSOLE_INIT_FLAG_INITED)
  82. {
  83. goto exit;
  84. }
  85. _console.foreground = lwp;
  86. console_wakeup_check(&_console);
  87. exit:
  88. rt_hw_interrupt_enable(level);
  89. }
  90. struct rt_lwp * rt_console_get_foreground(void)
  91. {
  92. struct rt_lwp *lwp;
  93. rt_base_t level;
  94. level = rt_hw_interrupt_disable();
  95. lwp = _console.foreground;
  96. rt_hw_interrupt_enable(level);
  97. return lwp;
  98. }
  99. static void iodev_close(struct rt_console_device *console)
  100. {
  101. struct rt_device_notify rx_notify;
  102. rx_notify.notify = RT_NULL;
  103. rx_notify.dev = RT_NULL;
  104. /* clear notify */
  105. rt_device_control(console->iodev, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
  106. rt_device_close(console->iodev);
  107. }
  108. static rt_err_t iodev_open(struct rt_console_device *console)
  109. {
  110. rt_err_t ret;
  111. struct rt_device_notify rx_notify;
  112. rt_uint16_t oflags;
  113. rt_device_control(console->iodev, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  114. ret = rt_device_open(console->iodev, oflags);
  115. if (ret != RT_EOK)
  116. {
  117. return RT_ERROR;
  118. }
  119. rx_notify.notify = console_rx_notify;
  120. rx_notify.dev = (struct rt_device *)console;
  121. rt_device_control(console->iodev, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
  122. return RT_EOK;
  123. }
  124. struct rt_device *rt_console_get_iodev(void)
  125. {
  126. rt_base_t level;
  127. struct rt_device *iodev;
  128. level = rt_hw_interrupt_disable();
  129. iodev = _console.iodev;
  130. rt_hw_interrupt_enable(level);
  131. return iodev;
  132. }
  133. struct rt_device *rt_console_set_iodev(struct rt_device *iodev)
  134. {
  135. rt_base_t level;
  136. struct rt_device *io_before;
  137. struct rt_console_device *console;
  138. RT_ASSERT(iodev != RT_NULL);
  139. console = &_console;
  140. level = rt_hw_interrupt_disable();
  141. RT_ASSERT(console->init_flag >= CONSOLE_INIT_FLAG_REGED);
  142. io_before = console->iodev;
  143. if (iodev == io_before)
  144. {
  145. goto exit;
  146. }
  147. if (console->init_flag >= CONSOLE_INIT_FLAG_INITED)
  148. {
  149. /* close old device */
  150. iodev_close(console);
  151. }
  152. console->iodev = iodev;
  153. if (console->init_flag >= CONSOLE_INIT_FLAG_INITED)
  154. {
  155. rt_err_t ret;
  156. /* open new device */
  157. ret = iodev_open(console);
  158. RT_ASSERT(ret == RT_EOK);
  159. }
  160. exit:
  161. rt_hw_interrupt_enable(level);
  162. return io_before;
  163. }
  164. #ifdef RT_USING_POSIX
  165. /* fops for console */
  166. static int console_fops_open(struct dfs_fd *fd)
  167. {
  168. int ret;
  169. struct rt_device * device;
  170. device = (struct rt_device *)fd->data;
  171. RT_ASSERT(device != RT_NULL);
  172. ret = rt_device_open(device, fd->flags);
  173. return ret;
  174. }
  175. static int console_fops_close(struct dfs_fd *fd)
  176. {
  177. int ret;
  178. struct rt_device * device;
  179. device = (struct rt_device *)fd->data;
  180. RT_ASSERT(device != RT_NULL);
  181. ret = rt_device_close(device);
  182. return ret;
  183. }
  184. static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  185. {
  186. rt_base_t level;
  187. int size = 0;
  188. struct rt_console_device *console;
  189. struct rt_lwp *lwp;
  190. struct rt_wqueue *wq;
  191. console = (struct rt_console_device *)fd->data;
  192. RT_ASSERT(console != RT_NULL);
  193. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  194. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  195. wq = wait_queue_get(lwp);
  196. level = rt_hw_interrupt_disable();
  197. while (count)
  198. {
  199. size = rt_device_read((struct rt_device *)console, -1, buf, count);
  200. if (size > 0)
  201. {
  202. break;
  203. }
  204. if (fd->flags & O_NONBLOCK)
  205. {
  206. break;
  207. }
  208. rt_wqueue_wait(wq, 0, RT_WAITING_FOREVER);
  209. }
  210. rt_hw_interrupt_enable(level);
  211. return size;
  212. }
  213. static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  214. {
  215. int size;
  216. struct rt_device * device;
  217. device = (struct rt_device *)fd->data;
  218. RT_ASSERT(device != RT_NULL);
  219. size = rt_device_write(device, -1, buf, count);
  220. return size;
  221. }
  222. static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  223. {
  224. rt_base_t level;
  225. int mask = POLLOUT;
  226. struct rt_device * device;
  227. struct rt_console_device *console;
  228. struct rt_wqueue *wq;
  229. struct rt_lwp *lwp;
  230. device = (struct rt_device *)fd->data;
  231. RT_ASSERT(device != RT_NULL);
  232. console = (struct rt_console_device *)device;
  233. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  234. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  235. wq = wait_queue_get(lwp);
  236. rt_poll_add(wq, req);
  237. level = rt_hw_interrupt_disable();
  238. if (lwp == console->foreground)
  239. {
  240. rt_size_t len;
  241. len = rt_ringbuffer_data_len(&console->input_rb);
  242. if (len)
  243. {
  244. mask |= POLLIN;
  245. }
  246. }
  247. rt_hw_interrupt_enable(level);
  248. return mask;
  249. }
  250. const static struct dfs_file_ops _console_fops =
  251. {
  252. console_fops_open,
  253. console_fops_close,
  254. RT_NULL,
  255. console_fops_read,
  256. console_fops_write,
  257. RT_NULL, /* flush */
  258. RT_NULL, /* lseek */
  259. RT_NULL, /* getdents */
  260. console_fops_poll,
  261. };
  262. #endif
  263. /* RT-Thread Device Interface */
  264. /*
  265. * This function initializes console device.
  266. */
  267. static rt_err_t rt_console_init(struct rt_device *dev)
  268. {
  269. rt_base_t level;
  270. rt_err_t result;
  271. struct rt_console_device *console;
  272. RT_ASSERT(dev != RT_NULL);
  273. console = (struct rt_console_device *)dev;
  274. level = rt_hw_interrupt_disable();
  275. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_REGED);
  276. result = iodev_open(console);
  277. if (result != RT_EOK)
  278. {
  279. goto exit;
  280. }
  281. console->init_flag = CONSOLE_INIT_FLAG_INITED;
  282. exit:
  283. rt_hw_interrupt_enable(level);
  284. return result;
  285. }
  286. static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
  287. {
  288. rt_err_t result = RT_EOK;
  289. struct rt_console_device *console;
  290. RT_ASSERT(dev != RT_NULL);
  291. console = (struct rt_console_device *)dev;
  292. RT_ASSERT(console != RT_NULL);
  293. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  294. return result;
  295. }
  296. static rt_err_t rt_console_close(struct rt_device *dev)
  297. {
  298. rt_err_t result = RT_EOK;
  299. struct rt_console_device *console;
  300. console = (struct rt_console_device *)dev;
  301. RT_ASSERT(console != RT_NULL);
  302. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  303. return result;
  304. }
  305. static rt_size_t rt_console_read(struct rt_device *dev,
  306. rt_off_t pos,
  307. void *buffer,
  308. rt_size_t size)
  309. {
  310. rt_base_t level;
  311. rt_size_t len = 0;
  312. struct rt_lwp *lwp;
  313. struct rt_console_device *console;
  314. console = (struct rt_console_device *)dev;
  315. RT_ASSERT(console != RT_NULL);
  316. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  317. level = rt_hw_interrupt_disable();
  318. if (size)
  319. {
  320. lwp = lwp_self();
  321. if (lwp == console->foreground)
  322. {
  323. len = rt_ringbuffer_data_len(&console->input_rb);
  324. if (len > size)
  325. {
  326. len = size;
  327. }
  328. if (len)
  329. {
  330. len = rt_ringbuffer_get(&console->input_rb, buffer, len);
  331. }
  332. }
  333. }
  334. rt_hw_interrupt_enable(level);
  335. return len;
  336. }
  337. static rt_size_t rt_console_write(struct rt_device *dev,
  338. rt_off_t pos,
  339. const void *buffer,
  340. rt_size_t size)
  341. {
  342. rt_base_t level;
  343. rt_size_t len = 0;
  344. struct rt_console_device *console;
  345. console = (struct rt_console_device *)dev;
  346. RT_ASSERT(console != RT_NULL);
  347. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  348. level = rt_hw_interrupt_disable();
  349. len = rt_device_write((struct rt_device *)console->iodev, -1, buffer, size);
  350. rt_hw_interrupt_enable(level);
  351. return len;
  352. }
  353. #ifdef RT_USING_DEVICE_OPS
  354. const static struct rt_device_ops console_ops =
  355. {
  356. rt_console_init,
  357. rt_console_open,
  358. rt_console_close,
  359. rt_console_read,
  360. rt_console_write,
  361. RT_NULL,
  362. };
  363. #endif
  364. /*
  365. * console register
  366. */
  367. rt_err_t rt_console_register(const char *name, struct rt_device *iodev)
  368. {
  369. rt_base_t level;
  370. rt_err_t ret;
  371. struct rt_device *device;
  372. struct rt_console_device *console = &_console;
  373. level = rt_hw_interrupt_disable();
  374. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_NONE);
  375. RT_ASSERT(iodev != RT_NULL);
  376. device = &(console->parent);
  377. device->type = RT_Device_Class_Char;
  378. #ifdef RT_USING_DEVICE_OPS
  379. device->ops = &console_ops;
  380. #else
  381. device->init = rt_console_init;
  382. device->open = rt_console_open;
  383. device->close = rt_console_close;
  384. device->read = rt_console_read;
  385. device->write = rt_console_write;
  386. device->control = RT_NULL;
  387. #endif
  388. /* register a character device */
  389. ret = rt_device_register(device, name, 0);
  390. if (ret != RT_EOK)
  391. {
  392. goto exit;
  393. }
  394. #ifdef RT_USING_POSIX
  395. /* set fops */
  396. device->fops = &_console_fops;
  397. #endif
  398. console->iodev = iodev;
  399. console->foreground = RT_PROCESS_KERNEL;
  400. rt_wqueue_init(&(console->wait_queue));
  401. RT_ASSERT(LWP_CONSOLE_INPUT_BUFFER_SIZE > 0);
  402. rt_ringbuffer_init(&console->input_rb, console->input_buf, LWP_CONSOLE_INPUT_BUFFER_SIZE);
  403. console->init_flag = CONSOLE_INIT_FLAG_REGED;
  404. exit:
  405. rt_hw_interrupt_enable(level);
  406. return ret;
  407. }