Skip to content

Commit

Permalink
[kservice] optimize console_device
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterywolf committed Jan 5, 2025
1 parent ebe05a9 commit 6cad23b
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/kservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,15 @@ RTM_EXPORT(rt_console_get_device);
*/
rt_device_t rt_console_set_device(const char *name)
{
rt_device_t new_device, old_device;
rt_device_t old_device = _console_device;
rt_device_t new_device = rt_device_find(name);

/* save old device */
old_device = _console_device;

/* find new console device */
new_device = rt_device_find(name);

/* check whether it's a same device */
if (new_device == old_device) return RT_NULL;

if (new_device != RT_NULL)
if (new_device != RT_NULL && new_device != old_device)
{
if (_console_device != RT_NULL)
if (old_device != RT_NULL)
{
/* close old console device */
rt_device_close(_console_device);
rt_device_close(old_device);
}

/* set new console device */
Expand Down Expand Up @@ -319,20 +311,23 @@ static void _console_release(void)
*/
static void _kputs(const char *str, long len)
{
RT_UNUSED(len);
#ifdef RT_USING_DEVICE
rt_device_t console_device = rt_console_get_device();
#endif /* RT_USING_DEVICE */

CONSOLE_TAKE;

#ifdef RT_USING_DEVICE
if (_console_device == RT_NULL)
if (console_device == RT_NULL)
{
rt_hw_console_output(str);
}
else
{
rt_device_write(_console_device, 0, str, len);
rt_device_write(console_device, 0, str, len);
}
#else
RT_UNUSED(len);
rt_hw_console_output(str);
#endif /* RT_USING_DEVICE */

Expand Down

0 comments on commit 6cad23b

Please sign in to comment.