lwp_console.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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_ioctl(struct dfs_fd *fd, int cmd, void *args)
  242. {
  243. int size = 0;
  244. struct rt_device *device = RT_NULL;
  245. device = (struct rt_device *)fd->fnode->data;
  246. RT_ASSERT(device != RT_NULL);
  247. size = rt_device_control(device, cmd, args);
  248. return size;
  249. }
  250. static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  251. {
  252. rt_base_t level = 0;
  253. int mask = POLLOUT;
  254. struct rt_device *device = RT_NULL;
  255. struct rt_console_device *console = RT_NULL;
  256. struct rt_wqueue *wq = RT_NULL;
  257. struct rt_lwp *lwp = RT_NULL;
  258. device = (struct rt_device *)fd->fnode->data;
  259. RT_ASSERT(device != RT_NULL);
  260. console = (struct rt_console_device *)device;
  261. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  262. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  263. wq = wait_queue_get(lwp);
  264. rt_poll_add(wq, req);
  265. level = rt_hw_interrupt_disable();
  266. if (lwp == console->foreground)
  267. {
  268. rt_size_t len;
  269. len = rt_ringbuffer_data_len(&console->input_rb);
  270. if (len)
  271. {
  272. mask |= POLLIN;
  273. }
  274. }
  275. rt_hw_interrupt_enable(level);
  276. return mask;
  277. }
  278. const static struct dfs_file_ops _console_fops =
  279. {
  280. console_fops_open,
  281. console_fops_close,
  282. console_fops_ioctl,
  283. console_fops_read,
  284. console_fops_write,
  285. RT_NULL, /* flush */
  286. RT_NULL, /* lseek */
  287. RT_NULL, /* getdents */
  288. console_fops_poll,
  289. };
  290. #endif
  291. /* RT-Thread Device Interface */
  292. /*
  293. * This function initializes console device.
  294. */
  295. static rt_err_t rt_console_init(struct rt_device *dev)
  296. {
  297. rt_base_t level = 0;
  298. rt_err_t result = RT_EOK;
  299. struct rt_console_device *console = RT_NULL;
  300. RT_ASSERT(dev != RT_NULL);
  301. console = (struct rt_console_device *)dev;
  302. level = rt_hw_interrupt_disable();
  303. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_REGED);
  304. result = iodev_open(console);
  305. if (result != RT_EOK)
  306. {
  307. goto exit;
  308. }
  309. console->init_flag = CONSOLE_INIT_FLAG_INITED;
  310. exit:
  311. rt_hw_interrupt_enable(level);
  312. return result;
  313. }
  314. static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
  315. {
  316. rt_err_t result = RT_EOK;
  317. struct rt_console_device *console = RT_NULL;
  318. RT_ASSERT(dev != 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_err_t rt_console_close(struct rt_device *dev)
  325. {
  326. rt_err_t result = RT_EOK;
  327. struct rt_console_device *console = RT_NULL;
  328. console = (struct rt_console_device *)dev;
  329. RT_ASSERT(console != RT_NULL);
  330. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  331. return result;
  332. }
  333. static rt_size_t rt_console_read(struct rt_device *dev,
  334. rt_off_t pos,
  335. void *buffer,
  336. rt_size_t size)
  337. {
  338. rt_base_t level = 0;
  339. rt_size_t len = 0;
  340. struct rt_lwp *lwp = RT_NULL;
  341. struct rt_console_device *console = RT_NULL;
  342. console = (struct rt_console_device *)dev;
  343. RT_ASSERT(console != RT_NULL);
  344. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  345. level = rt_hw_interrupt_disable();
  346. if (size)
  347. {
  348. lwp = lwp_self();
  349. if (lwp == console->foreground)
  350. {
  351. len = rt_ringbuffer_data_len(&console->input_rb);
  352. if (len > size)
  353. {
  354. len = size;
  355. }
  356. if (len)
  357. {
  358. len = rt_ringbuffer_get(&console->input_rb, buffer, len);
  359. }
  360. }
  361. }
  362. rt_hw_interrupt_enable(level);
  363. return len;
  364. }
  365. static rt_size_t rt_console_write(struct rt_device *dev,
  366. rt_off_t pos,
  367. const void *buffer,
  368. rt_size_t size)
  369. {
  370. rt_base_t level = 0;
  371. rt_size_t len = 0;
  372. struct rt_console_device *console = RT_NULL;
  373. console = (struct rt_console_device *)dev;
  374. RT_ASSERT(console != RT_NULL);
  375. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  376. level = rt_hw_interrupt_disable();
  377. len = rt_device_write((struct rt_device *)console->iodev, -1, buffer, size);
  378. rt_hw_interrupt_enable(level);
  379. return len;
  380. }
  381. static rt_err_t rt_console_control(rt_device_t dev, int cmd, void *args)
  382. {
  383. rt_base_t level = 0;
  384. rt_size_t len = 0;
  385. struct rt_console_device *console = RT_NULL;
  386. console = (struct rt_console_device *)dev;
  387. RT_ASSERT(console != RT_NULL);
  388. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
  389. level = rt_hw_interrupt_disable();
  390. len = rt_device_control((struct rt_device *)console->iodev, cmd, args);
  391. rt_hw_interrupt_enable(level);
  392. return len;
  393. }
  394. #ifdef RT_USING_DEVICE_OPS
  395. const static struct rt_device_ops console_ops =
  396. {
  397. rt_console_init,
  398. rt_console_open,
  399. rt_console_close,
  400. rt_console_read,
  401. rt_console_write,
  402. rt_console_control,
  403. };
  404. #endif
  405. /*
  406. * console register
  407. */
  408. rt_err_t rt_console_register(const char *name, struct rt_device *iodev)
  409. {
  410. rt_base_t level = 0;
  411. rt_err_t ret = RT_EOK;
  412. struct rt_device *device = RT_NULL;
  413. struct rt_console_device *console = &_console;
  414. level = rt_hw_interrupt_disable();
  415. RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_NONE);
  416. RT_ASSERT(iodev != RT_NULL);
  417. device = &(console->parent);
  418. device->type = RT_Device_Class_Char;
  419. #ifdef RT_USING_DEVICE_OPS
  420. device->ops = &console_ops;
  421. #else
  422. device->init = rt_console_init;
  423. device->open = rt_console_open;
  424. device->close = rt_console_close;
  425. device->read = rt_console_read;
  426. device->write = rt_console_write;
  427. device->control = rt_console_control;
  428. #endif
  429. /* register a character device */
  430. ret = rt_device_register(device, name, 0);
  431. if (ret != RT_EOK)
  432. {
  433. goto exit;
  434. }
  435. #ifdef RT_USING_POSIX
  436. /* set fops */
  437. device->fops = &_console_fops;
  438. #endif
  439. console->iodev = iodev;
  440. console->foreground = RT_PROCESS_KERNEL;
  441. rt_wqueue_init(&(console->wait_queue));
  442. RT_ASSERT(LWP_CONSOLE_INPUT_BUFFER_SIZE > 0);
  443. rt_ringbuffer_init(&console->input_rb, console->input_buf, LWP_CONSOLE_INPUT_BUFFER_SIZE);
  444. console->init_flag = CONSOLE_INIT_FLAG_REGED;
  445. exit:
  446. rt_hw_interrupt_enable(level);
  447. return ret;
  448. }