console.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_SMART)
  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_DEVIO
  42. {
  43. rt_device_t dev = rt_device_find(argv[2]);
  44. if (dev != RT_NULL)
  45. {
  46. #ifdef RT_USING_SMART
  47. console_set_iodev(dev);
  48. #else
  49. rt_kprintf("TODO not supported\n");
  50. #endif
  51. }
  52. }
  53. #else
  54. finsh_set_device(argv[2]);
  55. #endif /* RT_USING_POSIX_DEVIO */
  56. }
  57. else
  58. {
  59. rt_kprintf("Unknown command. Please enter 'console' for help\n");
  60. result = -RT_ERROR;
  61. }
  62. }
  63. else
  64. {
  65. rt_kprintf("Usage: \n");
  66. rt_kprintf("console set <name> - change console by name\n");
  67. result = -RT_ERROR;
  68. }
  69. return result;
  70. }
  71. MSH_CMD_EXPORT(console, set console name);
  72. #endif /* FINSH_USING_MSH */