lwp_console.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 = 0;
  40. struct rt_wqueue *wq = NULL;
  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 = NULL;
  51. rt_size_t len = 0;
  52. rt_uint8_t ch = 0;
  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 = 0;
  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 = RT_NULL;
  97. rt_base_t level = 0;
  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 = RT_EOK;
  115. struct rt_device_notify rx_notify;
  116. rt_uint16_t oflags = 0;
  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 = 0;
  131. struct rt_device *iodev = RT_NULL;
  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 = 0;
  140. struct rt_device *io_before = RT_NULL;
  141. struct rt_console_device *console = RT_NULL;
  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 = 0;
  173. struct rt_device *device = RT_NULL;
  174. device = (struct rt_device *)fd->fnode->data;
  175. RT_ASSERT(device != RT_NULL);
  176. if (fd->fnode->ref_count == 1)
  177. {
  178. ret = rt_device_open(device, fd->flags);
  179. }
  180. return ret;
  181. }
  182. static int console_fops_close(struct dfs_fd *fd)
  183. {
  184. int ret = 0;
  185. struct rt_device *device = RT_NULL;
  186. device = (struct rt_device *)fd->fnode->data;
  187. RT_ASSERT(device != RT_NULL);
  188. if (fd->fnode->ref_count == 1)
  189. {
  190. ret = rt_device_close(device);
  191. }
  192. return ret;
  193. }
  194. static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  195. {
  196. rt_base_t level = 0;
  197. int size = 0;
  198. struct rt_console_device *console = RT_NULL;
  199. struct rt_lwp *lwp = RT_NULL;
  200. struct rt_wqueue *wq = RT_NULL;
  201. int wait_ret = 0;
  202. console = (struct rt_console_device *)fd->fnode->data;
  203. RT_ASSERT(console != RT_NULL);
  204. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  205. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  206. wq = wait_queue_get(lwp);
  207. level = rt_hw_interrupt_disable();
  208. while (count)
  209. {
  210. size = rt_device_read((struct rt_device *)console, -1, buf, count);
  211. if (size > 0)
  212. {
  213. break;
  214. }
  215. if (fd->flags & O_NONBLOCK)
  216. {
  217. break;
  218. }
  219. wait_ret = rt_wqueue_wait_interruptible(wq, 0, RT_WAITING_FOREVER);
  220. if (wait_ret != 0)
  221. {
  222. break;
  223. }
  224. }
  225. rt_hw_interrupt_enable(level);
  226. if (size < 0)
  227. {
  228. size = 0;
  229. }
  230. return size;
  231. }
  232. static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  233. {
  234. int size = 0;
  235. struct rt_device *device = RT_NULL;
  236. device = (struct rt_device *)fd->fnode->data;
  237. RT_ASSERT(device != RT_NULL);
  238. size = rt_device_write(device, -1, buf, count);
  239. return size;
  240. }
  241. static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  242. {
  243. rt_base_t level = 0;
  244. int mask = POLLOUT;
  245. struct rt_device *device = RT_NULL;
  246. struct rt_console_device *console = RT_NULL;
  247. struct rt_wqueue *wq = RT_NULL;
  248. struct rt_lwp *lwp = RT_NULL;
  249. device = (struct rt_device *)fd->fnode->data;
  250. RT_ASSERT(device != RT_NULL);
  251. console = (struct rt_console_device *)device;
  252. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  253. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  254. wq = wait_queue_get(lwp);
  255. rt_poll_add(wq, req);
  256. level = rt_hw_interrupt_disable();
  257. if (lwp == console->foreground)
  258. {
  259. rt_size_t len;
  260. len = rt_ringbuffer_data_len(&console->input_rb);
  261. if (len)
  262. {
  263. mask |= POLLIN;
  264. }
  265. }
  266. rt_hw_interrupt_enable(level);
  267. return mask;
  268. }
  269. const static struct dfs_file_ops _console_fops =
  270. {
  271. console_fops_open,
  272. console_fops_close,
  273. RT_NULL,
  274. console_fops_read,
  275. console_fops_write,
  276. RT_NULL, /* flush */
  277. RT_NULL, /* lseek */
  278. RT_NULL, /* getdents */
  279. console_fops_poll,
  280. };
  281. #endif
  282. /* RT-Thread Device Interface */
  283. /*
  284. * This function initializes console device.
  285. */
  286. static rt_err_t rt_console_init(struct rt_device *dev)
  287. {
  288. rt_base_t level = 0;
  289. rt_err_t result = RT_EOK;
  290. struct rt_console_device *console = RT_NULL;
  291. RT_ASSERT(dev != RT_NULL);
  292. console = (struct rt_console_device *)dev;
  293. level = rt_hw_interrupt_disable();
  294. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_REGED);
  295. result = iodev_open(console);
  296. if (result != RT_EOK)
  297. {
  298. goto exit;
  299. }
  300. console->init_flag = CONSOLE_INIT_FLAG_INITED;
  301. exit:
  302. rt_hw_interrupt_enable(level);
  303. return result;
  304. }
  305. static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
  306. {
  307. rt_err_t result = RT_EOK;
  308. struct rt_console_device *console = RT_NULL;
  309. RT_ASSERT(dev != RT_NULL);
  310. console = (struct rt_console_device *)dev;
  311. RT_ASSERT(console != RT_NULL);
  312. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  313. return result;
  314. }
  315. static rt_err_t rt_console_close(struct rt_device *dev)
  316. {
  317. rt_err_t result = RT_EOK;
  318. struct rt_console_device *console = RT_NULL;
  319. console = (struct rt_console_device *)dev;
  320. RT_ASSERT(console != RT_NULL);
  321. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  322. return result;
  323. }
  324. static rt_size_t rt_console_read(struct rt_device *dev,
  325. rt_off_t pos,
  326. void *buffer,
  327. rt_size_t size)
  328. {
  329. rt_base_t level = 0;
  330. rt_size_t len = 0;
  331. struct rt_lwp *lwp = RT_NULL;
  332. struct rt_console_device *console = RT_NULL;
  333. console = (struct rt_console_device *)dev;
  334. RT_ASSERT(console != RT_NULL);
  335. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  336. level = rt_hw_interrupt_disable();
  337. if (size)
  338. {
  339. lwp = lwp_self();
  340. if (lwp == console->foreground)
  341. {
  342. len = rt_ringbuffer_data_len(&console->input_rb);
  343. if (len > size)
  344. {
  345. len = size;
  346. }
  347. if (len)
  348. {
  349. len = rt_ringbuffer_get(&console->input_rb, buffer, len);
  350. }
  351. }
  352. }
  353. rt_hw_interrupt_enable(level);
  354. return len;
  355. }
  356. static rt_size_t rt_console_write(struct rt_device *dev,
  357. rt_off_t pos,
  358. const void *buffer,
  359. rt_size_t size)
  360. {
  361. rt_base_t level = 0;
  362. rt_size_t len = 0;
  363. struct rt_console_device *console = RT_NULL;
  364. console = (struct rt_console_device *)dev;
  365. RT_ASSERT(console != RT_NULL);
  366. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  367. level = rt_hw_interrupt_disable();
  368. len = rt_device_write((struct rt_device *)console->iodev, -1, buffer, size);
  369. rt_hw_interrupt_enable(level);
  370. return len;
  371. }
  372. #ifdef RT_USING_DEVICE_OPS
  373. const static struct rt_device_ops console_ops =
  374. {
  375. rt_console_init,
  376. rt_console_open,
  377. rt_console_close,
  378. rt_console_read,
  379. rt_console_write,
  380. RT_NULL,
  381. };
  382. #endif
  383. /*
  384. * console register
  385. */
  386. rt_err_t rt_console_register(const char *name, struct rt_device *iodev)
  387. {
  388. rt_base_t level = 0;
  389. rt_err_t ret = RT_EOK;
  390. struct rt_device *device = RT_NULL;
  391. struct rt_console_device *console = &_console;
  392. level = rt_hw_interrupt_disable();
  393. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_NONE);
  394. RT_ASSERT(iodev != RT_NULL);
  395. device = &(console->parent);
  396. device->type = RT_Device_Class_Char;
  397. #ifdef RT_USING_DEVICE_OPS
  398. device->ops = &console_ops;
  399. #else
  400. device->init = rt_console_init;
  401. device->open = rt_console_open;
  402. device->close = rt_console_close;
  403. device->read = rt_console_read;
  404. device->write = rt_console_write;
  405. device->control = RT_NULL;
  406. #endif
  407. /* register a character device */
  408. ret = rt_device_register(device, name, 0);
  409. if (ret != RT_EOK)
  410. {
  411. goto exit;
  412. }
  413. #ifdef RT_USING_POSIX
  414. /* set fops */
  415. device->fops = &_console_fops;
  416. #endif
  417. console->iodev = iodev;
  418. console->foreground = RT_PROCESS_KERNEL;
  419. rt_wqueue_init(&(console->wait_queue));
  420. RT_ASSERT(LWP_CONSOLE_INPUT_BUFFER_SIZE > 0);
  421. rt_ringbuffer_init(&console->input_rb, console->input_buf, LWP_CONSOLE_INPUT_BUFFER_SIZE);
  422. console->init_flag = CONSOLE_INIT_FLAG_REGED;
  423. exit:
  424. rt_hw_interrupt_enable(level);
  425. return ret;
  426. }