lwp_console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. struct rt_lwp *lwp = console->foreground;
  69. if (lwp)
  70. {
  71. lwp_kill(lwp_to_pid(lwp), SIGINT);
  72. }
  73. }
  74. else
  75. {
  76. rt_ringbuffer_put_force(&console->input_rb, &ch, 1);
  77. }
  78. }
  79. console_wakeup_check(console);
  80. }
  81. void rt_console_set_foreground(struct rt_lwp *lwp)
  82. {
  83. rt_base_t level;
  84. level = rt_hw_interrupt_disable();
  85. if (_console.init_flag != CONSOLE_INIT_FLAG_INITED)
  86. {
  87. goto exit;
  88. }
  89. _console.foreground = lwp;
  90. console_wakeup_check(&_console);
  91. exit:
  92. rt_hw_interrupt_enable(level);
  93. }
  94. struct rt_lwp * rt_console_get_foreground(void)
  95. {
  96. struct rt_lwp *lwp;
  97. rt_base_t level;
  98. level = rt_hw_interrupt_disable();
  99. lwp = _console.foreground;
  100. rt_hw_interrupt_enable(level);
  101. return lwp;
  102. }
  103. static void iodev_close(struct rt_console_device *console)
  104. {
  105. struct rt_device_notify rx_notify;
  106. rx_notify.notify = RT_NULL;
  107. rx_notify.dev = RT_NULL;
  108. /* clear notify */
  109. rt_device_control(console->iodev, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
  110. rt_device_close(console->iodev);
  111. }
  112. static rt_err_t iodev_open(struct rt_console_device *console)
  113. {
  114. rt_err_t ret;
  115. struct rt_device_notify rx_notify;
  116. rt_uint16_t oflags;
  117. rt_device_control(console->iodev, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  118. ret = rt_device_open(console->iodev, oflags);
  119. if (ret != RT_EOK)
  120. {
  121. return RT_ERROR;
  122. }
  123. rx_notify.notify = console_rx_notify;
  124. rx_notify.dev = (struct rt_device *)console;
  125. rt_device_control(console->iodev, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
  126. return RT_EOK;
  127. }
  128. struct rt_device *rt_console_get_iodev(void)
  129. {
  130. rt_base_t level;
  131. struct rt_device *iodev;
  132. level = rt_hw_interrupt_disable();
  133. iodev = _console.iodev;
  134. rt_hw_interrupt_enable(level);
  135. return iodev;
  136. }
  137. struct rt_device *rt_console_set_iodev(struct rt_device *iodev)
  138. {
  139. rt_base_t level;
  140. struct rt_device *io_before;
  141. struct rt_console_device *console;
  142. RT_ASSERT(iodev != RT_NULL);
  143. console = &_console;
  144. level = rt_hw_interrupt_disable();
  145. RT_ASSERT(console->init_flag >= CONSOLE_INIT_FLAG_REGED);
  146. io_before = console->iodev;
  147. if (iodev == io_before)
  148. {
  149. goto exit;
  150. }
  151. if (console->init_flag >= CONSOLE_INIT_FLAG_INITED)
  152. {
  153. /* close old device */
  154. iodev_close(console);
  155. }
  156. console->iodev = iodev;
  157. if (console->init_flag >= CONSOLE_INIT_FLAG_INITED)
  158. {
  159. rt_err_t ret;
  160. /* open new device */
  161. ret = iodev_open(console);
  162. RT_ASSERT(ret == RT_EOK);
  163. }
  164. exit:
  165. rt_hw_interrupt_enable(level);
  166. return io_before;
  167. }
  168. #ifdef RT_USING_POSIX
  169. /* fops for console */
  170. static int console_fops_open(struct dfs_fd *fd)
  171. {
  172. int ret;
  173. struct rt_device * device;
  174. device = (struct rt_device *)fd->data;
  175. RT_ASSERT(device != RT_NULL);
  176. ret = rt_device_open(device, fd->flags);
  177. return ret;
  178. }
  179. static int console_fops_close(struct dfs_fd *fd)
  180. {
  181. int ret;
  182. struct rt_device * device;
  183. device = (struct rt_device *)fd->data;
  184. RT_ASSERT(device != RT_NULL);
  185. ret = rt_device_close(device);
  186. return ret;
  187. }
  188. static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  189. {
  190. rt_base_t level;
  191. int size = 0;
  192. struct rt_console_device *console;
  193. struct rt_lwp *lwp;
  194. struct rt_wqueue *wq;
  195. int wait_ret;
  196. console = (struct rt_console_device *)fd->data;
  197. RT_ASSERT(console != RT_NULL);
  198. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  199. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  200. wq = wait_queue_get(lwp);
  201. level = rt_hw_interrupt_disable();
  202. while (count)
  203. {
  204. size = rt_device_read((struct rt_device *)console, -1, buf, count);
  205. if (size > 0)
  206. {
  207. break;
  208. }
  209. if (fd->flags & O_NONBLOCK)
  210. {
  211. break;
  212. }
  213. wait_ret = rt_wqueue_wait_interruptible(wq, 0, RT_WAITING_FOREVER);
  214. if (wait_ret != 0)
  215. {
  216. break;
  217. }
  218. }
  219. rt_hw_interrupt_enable(level);
  220. if (size < 0)
  221. {
  222. size = 0;
  223. }
  224. return size;
  225. }
  226. static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  227. {
  228. int size;
  229. struct rt_device * device;
  230. device = (struct rt_device *)fd->data;
  231. RT_ASSERT(device != RT_NULL);
  232. size = rt_device_write(device, -1, buf, count);
  233. return size;
  234. }
  235. static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  236. {
  237. rt_base_t level;
  238. int mask = POLLOUT;
  239. struct rt_device * device;
  240. struct rt_console_device *console;
  241. struct rt_wqueue *wq;
  242. struct rt_lwp *lwp;
  243. device = (struct rt_device *)fd->data;
  244. RT_ASSERT(device != RT_NULL);
  245. console = (struct rt_console_device *)device;
  246. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  247. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  248. wq = wait_queue_get(lwp);
  249. rt_poll_add(wq, req);
  250. level = rt_hw_interrupt_disable();
  251. if (lwp == console->foreground)
  252. {
  253. rt_size_t len;
  254. len = rt_ringbuffer_data_len(&console->input_rb);
  255. if (len)
  256. {
  257. mask |= POLLIN;
  258. }
  259. }
  260. rt_hw_interrupt_enable(level);
  261. return mask;
  262. }
  263. const static struct dfs_file_ops _console_fops =
  264. {
  265. console_fops_open,
  266. console_fops_close,
  267. RT_NULL,
  268. console_fops_read,
  269. console_fops_write,
  270. RT_NULL, /* flush */
  271. RT_NULL, /* lseek */
  272. RT_NULL, /* getdents */
  273. console_fops_poll,
  274. };
  275. #endif
  276. /* RT-Thread Device Interface */
  277. /*
  278. * This function initializes console device.
  279. */
  280. static rt_err_t rt_console_init(struct rt_device *dev)
  281. {
  282. rt_base_t level;
  283. rt_err_t result;
  284. struct rt_console_device *console;
  285. RT_ASSERT(dev != RT_NULL);
  286. console = (struct rt_console_device *)dev;
  287. level = rt_hw_interrupt_disable();
  288. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_REGED);
  289. result = iodev_open(console);
  290. if (result != RT_EOK)
  291. {
  292. goto exit;
  293. }
  294. console->init_flag = CONSOLE_INIT_FLAG_INITED;
  295. exit:
  296. rt_hw_interrupt_enable(level);
  297. return result;
  298. }
  299. static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
  300. {
  301. rt_err_t result = RT_EOK;
  302. struct rt_console_device *console;
  303. RT_ASSERT(dev != RT_NULL);
  304. console = (struct rt_console_device *)dev;
  305. RT_ASSERT(console != RT_NULL);
  306. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  307. return result;
  308. }
  309. static rt_err_t rt_console_close(struct rt_device *dev)
  310. {
  311. rt_err_t result = RT_EOK;
  312. struct rt_console_device *console;
  313. console = (struct rt_console_device *)dev;
  314. RT_ASSERT(console != RT_NULL);
  315. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  316. return result;
  317. }
  318. static rt_size_t rt_console_read(struct rt_device *dev,
  319. rt_off_t pos,
  320. void *buffer,
  321. rt_size_t size)
  322. {
  323. rt_base_t level;
  324. rt_size_t len = 0;
  325. struct rt_lwp *lwp;
  326. struct rt_console_device *console;
  327. console = (struct rt_console_device *)dev;
  328. RT_ASSERT(console != RT_NULL);
  329. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  330. level = rt_hw_interrupt_disable();
  331. if (size)
  332. {
  333. lwp = lwp_self();
  334. if (lwp == console->foreground)
  335. {
  336. len = rt_ringbuffer_data_len(&console->input_rb);
  337. if (len > size)
  338. {
  339. len = size;
  340. }
  341. if (len)
  342. {
  343. len = rt_ringbuffer_get(&console->input_rb, buffer, len);
  344. }
  345. }
  346. }
  347. rt_hw_interrupt_enable(level);
  348. return len;
  349. }
  350. static rt_size_t rt_console_write(struct rt_device *dev,
  351. rt_off_t pos,
  352. const void *buffer,
  353. rt_size_t size)
  354. {
  355. rt_base_t level;
  356. rt_size_t len = 0;
  357. struct rt_console_device *console;
  358. console = (struct rt_console_device *)dev;
  359. RT_ASSERT(console != RT_NULL);
  360. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  361. level = rt_hw_interrupt_disable();
  362. len = rt_device_write((struct rt_device *)console->iodev, -1, buffer, size);
  363. rt_hw_interrupt_enable(level);
  364. return len;
  365. }
  366. #ifdef RT_USING_DEVICE_OPS
  367. const static struct rt_device_ops console_ops =
  368. {
  369. rt_console_init,
  370. rt_console_open,
  371. rt_console_close,
  372. rt_console_read,
  373. rt_console_write,
  374. RT_NULL,
  375. };
  376. #endif
  377. /*
  378. * console register
  379. */
  380. rt_err_t rt_console_register(const char *name, struct rt_device *iodev)
  381. {
  382. rt_base_t level;
  383. rt_err_t ret;
  384. struct rt_device *device;
  385. struct rt_console_device *console = &_console;
  386. level = rt_hw_interrupt_disable();
  387. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_NONE);
  388. RT_ASSERT(iodev != RT_NULL);
  389. device = &(console->parent);
  390. device->type = RT_Device_Class_Char;
  391. #ifdef RT_USING_DEVICE_OPS
  392. device->ops = &console_ops;
  393. #else
  394. device->init = rt_console_init;
  395. device->open = rt_console_open;
  396. device->close = rt_console_close;
  397. device->read = rt_console_read;
  398. device->write = rt_console_write;
  399. device->control = RT_NULL;
  400. #endif
  401. /* register a character device */
  402. ret = rt_device_register(device, name, 0);
  403. if (ret != RT_EOK)
  404. {
  405. goto exit;
  406. }
  407. #ifdef RT_USING_POSIX
  408. /* set fops */
  409. device->fops = &_console_fops;
  410. #endif
  411. console->iodev = iodev;
  412. console->foreground = RT_PROCESS_KERNEL;
  413. rt_wqueue_init(&(console->wait_queue));
  414. RT_ASSERT(LWP_CONSOLE_INPUT_BUFFER_SIZE > 0);
  415. rt_ringbuffer_init(&console->input_rb, console->input_buf, LWP_CONSOLE_INPUT_BUFFER_SIZE);
  416. console->init_flag = CONSOLE_INIT_FLAG_REGED;
  417. exit:
  418. rt_hw_interrupt_enable(level);
  419. return ret;
  420. }