-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.c
55 lines (46 loc) · 1.05 KB
/
service.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "service.h"
#include "device.h"
#include "stdarg.h"
#include "errno.h"
/*控制台*/
static device_t *console = NULL;
/* 内核打印函数 */
void kprintf( const char *fmt,... )
{
if(console == NULL)
return ;
va_list args;
static char log_buffer[ERTOS_CONSOLE_BUFFER_SIZE];
va_start(args, fmt);
uint16 length = vsnprintf(log_buffer, ERTOS_CONSOLE_BUFFER_SIZE - 1, fmt, args);
if( length > ERTOS_CONSOLE_BUFFER_SIZE - 1 )
{
length = ERTOS_CONSOLE_BUFFER_SIZE - 1;
}
if( console != NULL )
{
uint16 old_flag = console->open_flag;
console->open_flag |= DEVICE_FLAG_STREAM;
device_write(console, 0, log_buffer, length);
console->open_flag |= old_flag;
}
va_end(args);
}
/* 为控制台关联设备 */
err_t console_set_device( const char *name )
{
device_t *dev = NULL;
if( name == NULL )
return -EERR;
dev = device_find(name);
if(dev)
{
if(console)
{/*如果控制台不为空,则需先关闭*/
device_close(console);
}
device_open(dev, DEVICE_OFLAG_RDWR | DEVICE_FLAG_STREAM);
console = dev;
}
return EOK;
}