console.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-11 GuEe-GUI the first version
  9. */
  10. #include <rtthread.h>
  11. #ifdef RT_USING_POSIX
  12. #include <console.h>
  13. #endif
  14. #include <virtio_console.h>
  15. static int console_init()
  16. {
  17. rt_err_t status = RT_EOK;
  18. rt_device_t device = rt_device_find("virtio-console0");
  19. if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
  20. {
  21. /* Create vport0p1 */
  22. status = rt_device_control(device, VIRTIO_DEVICE_CTRL_CONSOLE_PORT_CREATE, RT_NULL);
  23. }
  24. if (device != RT_NULL)
  25. {
  26. rt_device_close(device);
  27. }
  28. return status;
  29. }
  30. INIT_ENV_EXPORT(console_init);
  31. #ifdef FINSH_USING_MSH
  32. static int console(int argc, char **argv)
  33. {
  34. rt_err_t result = RT_EOK;
  35. if (argc > 1)
  36. {
  37. if (!rt_strcmp(argv[1], "set"))
  38. {
  39. rt_kprintf("console change to %s\n", argv[2]);
  40. rt_console_set_device(argv[2]);
  41. #ifdef RT_USING_POSIX
  42. {
  43. rt_device_t dev = rt_device_find(argv[2]);
  44. if (dev != RT_NULL)
  45. {
  46. console_set_iodev(dev);
  47. }
  48. }
  49. #else
  50. finsh_set_device(argv[2]);
  51. #endif /* RT_USING_POSIX */
  52. }
  53. else
  54. {
  55. rt_kprintf("Unknown command. Please enter 'console' for help\n");
  56. result = -RT_ERROR;
  57. }
  58. }
  59. else
  60. {
  61. rt_kprintf("Usage: \n");
  62. rt_kprintf("console set <name> - change console by name\n");
  63. result = -RT_ERROR;
  64. }
  65. return result;
  66. }
  67. MSH_CMD_EXPORT(console, set console name);
  68. #endif /* FINSH_USING_MSH */