xref: /nrf52832-nimble/rt-thread/components/libc/compilers/dlib/stdio.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2017/10/15     bernard      implement stdio for IAR dlib.
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <rtthread.h>
16 #include "libc.h"
17 
18 #if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
19 #include <dfs_posix.h>
20 
21 #define STDIO_DEVICE_NAME_MAX   32
22 
23 static int std_fd = -1;
24 int libc_stdio_set_console(const char* device_name, int mode)
25 {
26     int fd;
27     char name[STDIO_DEVICE_NAME_MAX];
28 
29     snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
30     name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
31 
32     fd = open(name, mode, 0);
33     if (fd >= 0)
34     {
35         if (std_fd >= 0)
36         {
37             close(std_fd);
38         }
39         std_fd = fd;
40     }
41 
42     return std_fd;
43 }
44 
45 int libc_stdio_get_console(void) {
46     return std_fd;
47 }
48 
49 int libc_stdio_read(void *buffer, size_t size)
50 {
51     return read(std_fd, buffer, size);
52 }
53 
54 int libc_stdio_write(const void *buffer, size_t size)
55 {
56     return write(std_fd, buffer, size);
57 }
58 #endif
59