提交 5c7b16d0 编写于 作者: B bernard

[libc] Update libc.

1. Add POSIX termios implementation;
2. Add POSIX signals implementation;
3. Add stdio for each libc.
上级 b27c7e48
...@@ -16,12 +16,12 @@ source "$RTT_DIR/components/finsh/KConfig" ...@@ -16,12 +16,12 @@ source "$RTT_DIR/components/finsh/KConfig"
source "$RTT_DIR/components/dfs/KConfig" source "$RTT_DIR/components/dfs/KConfig"
source "$RTT_DIR/components/net/KConfig"
source "$RTT_DIR/components/drivers/KConfig" source "$RTT_DIR/components/drivers/KConfig"
source "$RTT_DIR/components/gui/KConfig"
source "$RTT_DIR/components/libc/KConfig" source "$RTT_DIR/components/libc/KConfig"
source "$RTT_DIR/components/net/KConfig"
source "$RTT_DIR/components/gui/Kconfig"
endmenu endmenu
/*
* File : libc.c
* Brief : armcc libc initialization
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include "libc.h"
#ifdef RT_USING_PTHREADS
#include <pthread.h>
#endif
int libc_system_init(void)
{
#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS)
rt_device_t dev_console;
dev_console = rt_console_get_device();
if (dev_console)
{
#if defined(RT_USING_POSIX_STDIN)
libc_stdio_set_console(dev_console->parent.name, O_RDWR);
#else
libc_stdio_set_console(dev_console->parent.name, O_WRONLY);
#endif
}
#endif
#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT
pthread_system_init();
#endif
return 0;
}
INIT_COMPONENT_EXPORT(libc_system_init);
/*
* File : libc.h
* Brief : armcc libc header file
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#ifndef __RTT_LIBC_H__
#define __RTT_LIBC_H__
#include <stddef.h>
int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_read (void *buffer, size_t size);
int libc_stdio_write(void *buffer, size_t size);
#endif
...@@ -32,7 +32,7 @@ RTM_EXPORT(realloc); ...@@ -32,7 +32,7 @@ RTM_EXPORT(realloc);
void *calloc(rt_size_t nelem, rt_size_t elsize) void *calloc(rt_size_t nelem, rt_size_t elsize)
{ {
return rt_calloc(nelem, elsize); return rt_calloc(nelem, elsize);
} }
RTM_EXPORT(calloc); RTM_EXPORT(calloc);
......
/*
* File : stdio.c
* Brief : stdio/console
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard implement stdio for armcc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtthread.h>
#include "libc.h"
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
#include <dfs_posix.h>
#define STDIO_DEVICE_NAME_MAX 32
static int std_fd = -1;
int libc_stdio_set_console(const char* device_name, int mode)
{
int fd;
char name[STDIO_DEVICE_NAME_MAX];
snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
fd = open(name, mode, 0);
if (fd >= 0)
{
if (std_fd >= 0)
{
close(std_fd);
}
std_fd = fd;
}
return std_fd;
}
int libc_stdio_read(void *buffer, size_t size)
{
return read(std_fd, buffer, size);
}
int libc_stdio_write(void *buffer, size_t size)
{
return write(std_fd, buffer, size);
}
#endif
...@@ -2,12 +2,22 @@ ...@@ -2,12 +2,22 @@
* File : stubs.c * File : stubs.c
* Brief : reimplement some basic functions of arm standard c library * Brief : reimplement some basic functions of arm standard c library
* *
* This file is part of Device File System in RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
...@@ -28,10 +38,10 @@ ...@@ -28,10 +38,10 @@
#pragma import(__use_no_semihosting_swi) #pragma import(__use_no_semihosting_swi)
/* TODO: Standard IO device handles. */ /* Standard IO device handles. */
#define STDIN 1 #define STDIN 0
#define STDOUT 2 #define STDOUT 1
#define STDERR 3 #define STDERR 2
/* Standard IO device name defines. */ /* Standard IO device name defines. */
const char __stdin_name[] = "STDIN"; const char __stdin_name[] = "STDIN";
...@@ -94,7 +104,7 @@ FILEHANDLE _sys_open(const char *name, int openmode) ...@@ -94,7 +104,7 @@ FILEHANDLE _sys_open(const char *name, int openmode)
if (fd < 0) if (fd < 0)
return -1; return -1;
else else
return fd + STDERR + 1; return fd;
#endif #endif
} }
...@@ -103,21 +113,36 @@ int _sys_close(FILEHANDLE fh) ...@@ -103,21 +113,36 @@ int _sys_close(FILEHANDLE fh)
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
if (fh < STDERR) if (fh <= STDERR) return 0;
return 0;
return close(fh - STDERR - 1); return close(fh);
#endif #endif
} }
/** /*
* read data * Read from a file. Can return:
* - zero if the read was completely successful
* - the number of bytes _not_ read, if the read was partially successful
* - the number of bytes not read, plus the top bit set (0x80000000), if
* the read was partially successful due to end of file
* - -1 if some error other than EOF occurred
* *
* @param fh - file handle * It is also legal to signal EOF by returning no data but
* @param buf - buffer to save read data * signalling no error (i.e. the top-bit-set mechanism need never
* @param len - max length of data buffer * be used).
* @param mode - useless, for historical reasons *
* @return The number of bytes not read. * So if (for example) the user is trying to read 8 bytes at a time
* from a file in which only 5 remain, this routine can do three
* equally valid things:
*
* - it can return 0x80000003 (3 bytes not read due to EOF)
* - OR it can return 3 (3 bytes not read), and then return
* 0x80000008 (8 bytes not read due to EOF) on the next attempt
* - OR it can return 3 (3 bytes not read), and then return
* 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
* attempt
*
* `mode' exists for historical reasons and must be ignored.
*/ */
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{ {
...@@ -127,8 +152,13 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) ...@@ -127,8 +152,13 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
if (fh == STDIN) if (fh == STDIN)
{ {
/* TODO */ #ifdef RT_USING_POSIX_STDIN
return 0; size = libc_stdio_read(buf, len);
return len - size;
#else
/* no stdin */
return -1;
#endif
} }
if ((fh == STDOUT) || (fh == STDERR)) if ((fh == STDOUT) || (fh == STDERR))
...@@ -137,7 +167,7 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) ...@@ -137,7 +167,7 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
size = read(fh - STDERR - 1, buf, len); size = read(fh, buf, len);
if (size >= 0) if (size >= 0)
return len - size; return len - size;
else else
...@@ -145,14 +175,10 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) ...@@ -145,14 +175,10 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
#endif #endif
} }
/** /*
* write data * Write to a file. Returns 0 on success, negative on error, and
* * the number of characters _not_ written on partial success.
* @param fh - file handle * `mode' exists for historical reasons and must be ignored.
* @param buf - data buffer
* @param len - buffer length
* @param mode - useless, for historical reasons
* @return a positive number representing the number of characters not written.
*/ */
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{ {
...@@ -165,22 +191,27 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) ...@@ -165,22 +191,27 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
#ifndef RT_USING_CONSOLE #ifndef RT_USING_CONSOLE
return 0; return 0;
#else #else
rt_device_t console_device; #ifdef RT_USING_POSIX_STDIN
size = libc_stdio_write(buf, len);
console_device = rt_console_get_device(); return len - size;
if (console_device != 0) rt_device_write(console_device, 0, buf, len); #else
if (rt_console_get_device())
{
rt_device_write(rt_console_get_device(), -1, buf, len);
return 0;
}
return 0; return -1;
#endif
#endif #endif
} }
if (fh == STDIN) if (fh == STDIN) return -1;
return -1;
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
size = write(fh - STDERR - 1, buf, len); size = write(fh, buf, len);
if (size >= 0) if (size >= 0)
return len - size; return len - size;
else else
...@@ -188,11 +219,9 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) ...@@ -188,11 +219,9 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
#endif #endif
} }
/** /*
* put he file pointer at offset pos from the beginning of the file. * Move the file position to a given offset from the file start.
* * Returns >=0 on success, <0 on failure.
* @param pos - offset
* @return the current file position, or -1 on failed
*/ */
int _sys_seek(FILEHANDLE fh, long pos) int _sys_seek(FILEHANDLE fh, long pos)
{ {
...@@ -204,7 +233,7 @@ int _sys_seek(FILEHANDLE fh, long pos) ...@@ -204,7 +233,7 @@ int _sys_seek(FILEHANDLE fh, long pos)
#else #else
/* position is relative to the start of file fh */ /* position is relative to the start of file fh */
return lseek(fh - STDERR - 1, pos, 0); return lseek(fh, pos, 0);
#endif #endif
} }
...@@ -285,7 +314,15 @@ int fputc(int c, FILE *f) ...@@ -285,7 +314,15 @@ int fputc(int c, FILE *f)
return 1; return 1;
} }
int fgetc(FILE *f) { int fgetc(FILE *f)
return -1; {
char ch;
#ifdef RT_USING_POSIX_STDIN
if (libc_stdio_read(&ch, 1) == 1)
return ch;
#endif
return -1;
} }
#endif #endif
#ifndef SYS_ERRNO_H__ #ifndef SYS_ERRNO_H__
#define SYS_ERRNO_H__ #define SYS_ERRNO_H__
#ifdef RT_USING_DFS #include <rtthread.h>
#include <dfs_def.h>
/* using device error codes */
#define ENOENT DFS_STATUS_ENOENT
#define EIO DFS_STATUS_EIO
#define ENXIO DFS_STATUS_ENXIO
#define EBADF DFS_STATUS_EBADF
#define EAGAIN DFS_STATUS_EAGAIN
#define ENOMEM DFS_STATUS_ENOMEM
#define EBUSY DFS_STATUS_EBUSY
#define EEXIST DFS_STATUS_EEXIST
#define EXDEV DFS_STATUS_EXDEV
#define ENODEV DFS_STATUS_ENODEV
#define ENOTDIR DFS_STATUS_ENOTDIR
#define EISDIR DFS_STATUS_EISDIR
#define EINVAL DFS_STATUS_EINVAL
#define ENOSPC DFS_STATUS_ENOSPC
#define EROFS DFS_STATUS_EROFS
#define ENOSYS DFS_STATUS_ENOSYS
#define ENOTEMPTY DFS_STATUS_ENOTEMPTY
#else
/* error codes */
#define ENOENT 2 /* No such file or directory */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define EBADF 9 /* Bad file number */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* no memory */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENOSPC 28 /* No space left on device */
#define EROFS 30 /* Read-only file system */
#define ENOSYS 38 /* Function not implemented */
#define ENOTEMPTY 39 /* Directory not empty */
#endif
#define EPERM 1 /* Not super-user */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENFILE 23 /* Too many open files in system */
#define ERANGE 34 /* Math result not representable */
#define EDEADLK 45 /* Deadlock condition */
#define EBADMSG 77 /* Trying to read unreadable message */
#define EMSGSIZE 90 /* Message too long */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define EINPROGRESS 115 /* Operation now in progress */
#define ETIMEDOUT 116 /* Connection timed out */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define ENOTSUP 134 /* Not supported */
#define ENSRNOTFOUND 163 /* Domain name not found */
#define EWOULDBLOCK EAGAIN /* Operation would block */
#endif #endif
...@@ -6,31 +6,31 @@ ...@@ -6,31 +6,31 @@
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
#include <dfs_posix.h> #include <dfs_posix.h>
#else #else
#define _FREAD 0x0001 /* read enabled */ #define _FREAD 0x0001 /* read enabled */
#define _FWRITE 0x0002 /* write enabled */ #define _FWRITE 0x0002 /* write enabled */
#define _FAPPEND 0x0008 /* append (writes guaranteed at the end) */ #define _FAPPEND 0x0008 /* append (writes guaranteed at the end) */
#define _FMARK 0x0010 /* internal; mark during gc() */ #define _FMARK 0x0010 /* internal; mark during gc() */
#define _FDEFER 0x0020 /* internal; defer for next gc pass */ #define _FDEFER 0x0020 /* internal; defer for next gc pass */
#define _FASYNC 0x0040 /* signal pgrp when data ready */ #define _FASYNC 0x0040 /* signal pgrp when data ready */
#define _FSHLOCK 0x0080 /* BSD flock() shared lock present */ #define _FSHLOCK 0x0080 /* BSD flock() shared lock present */
#define _FEXLOCK 0x0100 /* BSD flock() exclusive lock present */ #define _FEXLOCK 0x0100 /* BSD flock() exclusive lock present */
#define _FCREAT 0x0200 /* open with file create */ #define _FCREAT 0x0200 /* open with file create */
#define _FTRUNC 0x0400 /* open with truncation */ #define _FTRUNC 0x0400 /* open with truncation */
#define _FEXCL 0x0800 /* error on open if file exists */ #define _FEXCL 0x0800 /* error on open if file exists */
#define _FNBIO 0x1000 /* non blocking I/O (sys5 style) */ #define _FNBIO 0x1000 /* non blocking I/O (sys5 style) */
#define _FSYNC 0x2000 /* do all writes synchronously */ #define _FSYNC 0x2000 /* do all writes synchronously */
#define _FNONBLOCK 0x4000 /* non blocking I/O (POSIX style) */ #define _FNONBLOCK 0x4000 /* non blocking I/O (POSIX style) */
#define _FNDELAY _FNONBLOCK /* non blocking I/O (4.2 style) */ #define _FNDELAY _FNONBLOCK /* non blocking I/O (4.2 style) */
#define _FNOCTTY 0x8000 /* don't assign a ctty on this open */ #define _FNOCTTY 0x8000 /* don't assign a ctty on this open */
#define O_RDONLY 0 /* +1 == FREAD */ #define O_RDONLY 0 /* +1 == FREAD */
#define O_WRONLY 1 /* +1 == FWRITE */ #define O_WRONLY 1 /* +1 == FWRITE */
#define O_RDWR 2 /* +1 == FREAD|FWRITE */ #define O_RDWR 2 /* +1 == FREAD|FWRITE */
#define O_APPEND _FAPPEND #define O_APPEND _FAPPEND
#define O_CREAT _FCREAT #define O_CREAT _FCREAT
#define O_TRUNC _FTRUNC #define O_TRUNC _FTRUNC
#define O_EXCL _FEXCL #define O_EXCL _FEXCL
#define O_SYNC _FSYNC #define O_SYNC _FSYNC
#endif #endif
#endif /* _SYS_UNISTD_H */ #endif /* _SYS_UNISTD_H */
/*
* File : libc.c
* Brief : iar dlib initialization
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include "libc.h"
#ifdef RT_USING_PTHREADS
#include <pthread.h>
#endif
int libc_system_init(void)
{
#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS)
rt_device_t dev_console;
dev_console = rt_console_get_device();
if (dev_console)
{
#if defined(RT_USING_POSIX_STDIN)
libc_stdio_set_console(dev_console->parent.name, O_RDWR);
#else
libc_stdio_set_console(dev_console->parent.name, O_WRONLY);
#endif
}
#endif
#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT
pthread_system_init();
#endif
return 0;
}
INIT_COMPONENT_EXPORT(libc_system_init);
/*
* File : libc.h
* Brief : iar dlib header file
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#ifndef __RTT_LIBC_H__
#define __RTT_LIBC_H__
#include <stddef.h>
int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_read (void *buffer, size_t size);
int libc_stdio_write(const void *buffer, size_t size);
#endif
/*
* File : stdio.c
* Brief : stdio/console
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard implement stdio for IAR dlib.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtthread.h>
#include "libc.h"
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
#include <dfs_posix.h>
#define STDIO_DEVICE_NAME_MAX 32
static int std_fd = -1;
int libc_stdio_set_console(const char* device_name, int mode)
{
int fd;
char name[STDIO_DEVICE_NAME_MAX];
snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
fd = open(name, mode, 0);
if (fd >= 0)
{
if (std_fd >= 0)
{
close(std_fd);
}
std_fd = fd;
}
return std_fd;
}
int libc_stdio_read(void *buffer, size_t size)
{
return read(std_fd, buffer, size);
}
int libc_stdio_write(const void *buffer, size_t size)
{
return write(std_fd, buffer, size);
}
#endif
#ifndef SYS_ERRNO_H__ #ifndef SYS_ERRNO_H__
#define SYS_ERRNO_H__ #define SYS_ERRNO_H__
#ifdef RT_USING_DFS #include <rtthread.h>
#include <dfs_def.h>
/* using device error codes */
#define ENOENT DFS_STATUS_ENOENT
#define EIO DFS_STATUS_EIO
#define ENXIO DFS_STATUS_ENXIO
#define EBADF DFS_STATUS_EBADF
#define EAGAIN DFS_STATUS_EAGAIN
#define ENOMEM DFS_STATUS_ENOMEM
#define EBUSY DFS_STATUS_EBUSY
#define EEXIST DFS_STATUS_EEXIST
#define EXDEV DFS_STATUS_EXDEV
#define ENODEV DFS_STATUS_ENODEV
#define ENOTDIR DFS_STATUS_ENOTDIR
#define EISDIR DFS_STATUS_EISDIR
#define EINVAL DFS_STATUS_EINVAL
#define ENOSPC DFS_STATUS_ENOSPC
#define EROFS DFS_STATUS_EROFS
#define ENOSYS DFS_STATUS_ENOSYS
#define ENOTEMPTY DFS_STATUS_ENOTEMPTY
#else
/* error codes */
#define ENOENT 2 /* No such file or directory */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define EBADF 9 /* Bad file number */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* no memory */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENOSPC 28 /* No space left on device */
#define EROFS 30 /* Read-only file system */
#define ENOSYS 38 /* Function not implemented */
#define ENOTEMPTY 39 /* Directory not empty */
#endif
#define EPERM 1 /* Not super-user */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENFILE 23 /* Too many open files in system */
#define ERANGE 34 /* Math result not representable */
#define EDEADLK 45 /* Deadlock condition */
#define EBADMSG 77 /* Trying to read unreadable message */
#define EMSGSIZE 90 /* Message too long */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define EINPROGRESS 115 /* Operation now in progress */
#define ETIMEDOUT 116 /* Connection timed out */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define ENOTSUP 134 /* Not supported */
#define ENSRNOTFOUND 163 /* Domain name not found */
#define EWOULDBLOCK EAGAIN /* Operation would block */
#endif #endif
...@@ -36,7 +36,7 @@ int __close(int handle) ...@@ -36,7 +36,7 @@ int __close(int handle)
return _LLIO_ERROR; return _LLIO_ERROR;
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
return close(handle - _LLIO_STDERR - 1); return close(handle);
#else #else
return 0; return 0;
#endif #endif
......
...@@ -36,7 +36,7 @@ long __lseek(int handle, long offset, int whence) ...@@ -36,7 +36,7 @@ long __lseek(int handle, long offset, int whence)
return _LLIO_ERROR; return _LLIO_ERROR;
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
return lseek(handle - _LLIO_STDERR - 1, offset, whence); return lseek(handle, offset, whence);
#else #else
return _LLIO_ERROR; return _LLIO_ERROR;
#endif #endif
......
...@@ -35,7 +35,7 @@ void *realloc(void *rmem, rt_size_t newsize) ...@@ -35,7 +35,7 @@ void *realloc(void *rmem, rt_size_t newsize)
void *calloc(rt_size_t nelem, rt_size_t elsize) void *calloc(rt_size_t nelem, rt_size_t elsize)
{ {
return rt_calloc(nelem, elsize); return rt_calloc(nelem, elsize);
} }
void free(void *rmem) void free(void *rmem)
......
...@@ -33,56 +33,56 @@ ...@@ -33,56 +33,56 @@
int __open(const char *filename, int mode) int __open(const char *filename, int mode)
{ {
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return -1; return _LLIO_ERROR;
#else #else
int handle; int handle;
int open_mode = O_RDONLY; int open_mode = O_RDONLY;
if (mode & _LLIO_CREAT) if (mode & _LLIO_CREAT)
{ {
open_mode |= O_CREAT; open_mode |= O_CREAT;
/* Check what we should do with it if it exists. */ /* Check what we should do with it if it exists. */
if (mode & _LLIO_APPEND) if (mode & _LLIO_APPEND)
{ {
/* Append to the existing file. */ /* Append to the existing file. */
open_mode |= O_APPEND; open_mode |= O_APPEND;
} }
if (mode & _LLIO_TRUNC) if (mode & _LLIO_TRUNC)
{ {
/* Truncate the existsing file. */ /* Truncate the existsing file. */
open_mode |= O_TRUNC; open_mode |= O_TRUNC;
} }
} }
if (mode & _LLIO_TEXT) if (mode & _LLIO_TEXT)
{ {
/* we didn't support text mode */ /* we didn't support text mode */
} }
switch (mode & _LLIO_RDWRMASK) switch (mode & _LLIO_RDWRMASK)
{ {
case _LLIO_RDONLY: case _LLIO_RDONLY:
break; break;
case _LLIO_WRONLY: case _LLIO_WRONLY:
open_mode |= O_WRONLY; open_mode |= O_WRONLY;
break; break;
case _LLIO_RDWR: case _LLIO_RDWR:
/* The file should be opened for both reads and writes. */ /* The file should be opened for both reads and writes. */
open_mode |= O_RDWR; open_mode |= O_RDWR;
break; break;
default: default:
return -1; return _LLIO_ERROR;
} }
handle = open(filename, open_mode, 0); handle = open(filename, open_mode, 0);
if (handle < 0) if (handle < 0)
return -1; return _LLIO_ERROR;
return handle + _LLIO_STDERR + 1; return handle;
#endif #endif
} }
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <dfs_posix.h> #include <dfs_posix.h>
#endif #endif
#include <yfuns.h> #include <yfuns.h>
#include "libc.h"
#pragma module_name = "?__read" #pragma module_name = "?__read"
size_t __read(int handle, unsigned char *buf, size_t len) size_t __read(int handle, unsigned char *buf, size_t len)
...@@ -37,8 +38,11 @@ size_t __read(int handle, unsigned char *buf, size_t len) ...@@ -37,8 +38,11 @@ size_t __read(int handle, unsigned char *buf, size_t len)
if (handle == _LLIO_STDIN) if (handle == _LLIO_STDIN)
{ {
/* TODO */ #ifdef RT_USING_POSIX_STDIN
return 0; return libc_stdio_read(buf, len);
#else
return _LLIO_ERROR;
#endif
} }
if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
...@@ -47,7 +51,7 @@ size_t __read(int handle, unsigned char *buf, size_t len) ...@@ -47,7 +51,7 @@ size_t __read(int handle, unsigned char *buf, size_t len)
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return _LLIO_ERROR; return _LLIO_ERROR;
#else #else
size = read(handle - _LLIO_STDERR - 1, buf, len); size = read(handle, buf, len);
return size; return size;
#endif #endif
} }
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <dfs_posix.h> #include <dfs_posix.h>
#endif #endif
#include <yfuns.h> #include <yfuns.h>
#include "libc.h"
#pragma module_name = "?__write" #pragma module_name = "?__write"
...@@ -41,21 +42,26 @@ size_t __write(int handle, const unsigned char *buf, size_t len) ...@@ -41,21 +42,26 @@ size_t __write(int handle, const unsigned char *buf, size_t len)
#ifndef RT_USING_CONSOLE #ifndef RT_USING_CONSOLE
return _LLIO_ERROR; return _LLIO_ERROR;
#else #else
#ifdef RT_USING_POSIX_STDIN
return libc_stdio_write((void*)buf, len);
#else
rt_device_t console_device; rt_device_t console_device;
console_device = rt_console_get_device(); console_device = rt_console_get_device();
if (console_device != 0) rt_device_write(console_device, 0, buf, len); if (console_device != 0) rt_device_write(console_device, 0, buf, len);
return len; return len;
#endif
#endif #endif
} }
if (handle == _LLIO_STDIN) return -1; if (handle == _LLIO_STDIN) return _LLIO_ERROR;
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return _LLIO_ERROR; return _LLIO_ERROR;
#else #else
size = write(handle - _LLIO_STDERR - 1, buf, len); size = write(handle, buf, len);
return size; return size;
#endif #endif
} }
......
#ifndef _UNISTD_H_
#define _UNISTD_H_
# include "sys/unistd.h"
#endif /* _UNISTD_H_ */
/*
* File : libc.c
* Brief : gcc libc header file
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#include <rtthread.h> #include <rtthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/time.h> #include <sys/time.h>
#include "libc.h" #include "libc.h"
#ifdef RT_USING_PTHREADS #ifdef RT_USING_PTHREADS
#include <pthread.h> #include <pthread.h>
#endif #endif
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#ifdef RT_USING_DFS_DEVFS
#include <devfs.h>
#endif
#endif
int libc_system_init(void) int libc_system_init(void)
{ {
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
int fd; rt_device_t dev_console;
struct rt_device *console_dev;
#ifndef RT_USING_DFS_DEVFS
#error Please enable devfs by defining RT_USING_DFS_DEVFS in rtconfig.h
#endif
console_dev = rt_console_get_device(); dev_console = rt_console_get_device();
if (console_dev) if (dev_console)
{ {
/* initialize console device */ #if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX_STDIN)
rt_console_init(console_dev->parent.name); libc_stdio_set_console(dev_console->parent.name, O_RDWR);
#else
/* open console as stdin/stdout/stderr */ libc_stdio_set_console(dev_console->parent.name, O_WRONLY);
fd = open("/dev/console", O_RDONLY, 0); /* for stdin */ #endif
fd = open("/dev/console", O_WRONLY, 0); /* for stdout */
fd = open("/dev/console", O_WRONLY, 0); /* for stderr */
/* skip warning */
fd = fd;
} }
#endif #endif
/* set PATH and HOME */ /* set PATH and HOME */
putenv("PATH=/bin"); putenv("PATH=/bin");
putenv("HOME=/home"); putenv("HOME=/home");
#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT #if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT
pthread_system_init(); pthread_system_init();
#endif #endif
......
/*
* File : libc.h
* Brief : gcc libc header file
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#ifndef __RTT_LIBC_H__ #ifndef __RTT_LIBC_H__
#define __RTT_LIBC_H__ #define __RTT_LIBC_H__
...@@ -12,6 +37,7 @@ ...@@ -12,6 +37,7 @@
#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND) #define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
int libc_system_init(void); int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode);
/* some time related function */ /* some time related function */
int libc_set_time(const struct timespec *time); int libc_set_time(const struct timespec *time);
......
/*
* File : libc_syms.c
* Brief : exported symbols for libc.
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#include <rtthread.h> #include <rtthread.h>
#include <rtm.h> #include <rtm.h>
......
/*
* File : stdio.c
* Brief : stdio for newlib
*
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include "libc.h"
#define STDIO_DEVICE_NAME_MAX 32
static FILE* std_console = NULL;
int libc_stdio_set_console(const char* device_name, int mode)
{
FILE *fp;
char name[STDIO_DEVICE_NAME_MAX];
char *file_mode;
snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
if (mode == O_RDWR) file_mode = "r+";
else if (mode == O_WRONLY) file_mode = "wb";
else if (mode == O_RDONLY) file_mode = "rb";
fp = fopen(name, file_mode);
if (fp)
{
setvbuf(fp, NULL, _IONBF, 0);
if (std_console)
{
fclose(std_console);
std_console = NULL;
}
std_console = fp;
if (mode == O_RDWR)
{
_GLOBAL_REENT->_stdin = std_console;
}
else
{
_GLOBAL_REENT->_stdin = NULL;
}
if (mode == O_RDONLY)
{
_GLOBAL_REENT->_stdout = NULL;
_GLOBAL_REENT->_stderr = NULL;
}
else
{
_GLOBAL_REENT->_stdout = std_console;
_GLOBAL_REENT->_stderr = std_console;
}
_GLOBAL_REENT->__sdidinit = 1;
}
return fileno(std_console);
}
...@@ -13,6 +13,4 @@ ...@@ -13,6 +13,4 @@
#define O_DIRECTORY 0x0200000 #define O_DIRECTORY 0x0200000
#define O_BINARY 0x0008000 #define O_BINARY 0x0008000
#include <unistd.h>
#endif #endif
...@@ -205,30 +205,23 @@ _wait_r(struct _reent *ptr, int *status) ...@@ -205,30 +205,23 @@ _wait_r(struct _reent *ptr, int *status)
_ssize_t _ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{ {
if (fd < 3) #ifndef RT_USING_DFS
if (fd == 0)
{ {
#ifdef RT_USING_CONSOLE rt_device_t console;
rt_device_t console_device;
extern rt_device_t rt_console_get_device(void);
console_device = rt_console_get_device(); console = rt_console_get_device();
if (console_device != 0) rt_device_write(console_device, 0, buf, nbytes); if (console) return rt_device_write(console, -1, buf, nbytes);
return nbytes;
#else
return 0;
#endif
} }
else
{
#ifdef RT_USING_DFS
_ssize_t rc;
rc = write(fd, buf, nbytes); return 0;
return rc;
#else #else
return 0; _ssize_t rc;
rc = write(fd, buf, nbytes);
return rc;
#endif #endif
}
} }
#endif #endif
......
...@@ -194,7 +194,7 @@ int pthread_detach(pthread_t thread) ...@@ -194,7 +194,7 @@ int pthread_detach(pthread_t thread)
ptd = _pthread_get_data(thread); ptd = _pthread_get_data(thread);
if (thread->stat == RT_THREAD_CLOSE) if ((thread->stat & RT_THREAD_STAT_MASK)== RT_THREAD_CLOSE)
{ {
/* delete joinable semaphore */ /* delete joinable semaphore */
if (ptd->joinable_sem != RT_NULL) if (ptd->joinable_sem != RT_NULL)
...@@ -354,10 +354,21 @@ RTM_EXPORT(pthread_atfork); ...@@ -354,10 +354,21 @@ RTM_EXPORT(pthread_atfork);
int pthread_kill(pthread_t thread, int sig) int pthread_kill(pthread_t thread, int sig)
{ {
return EOPNOTSUPP; #ifdef RT_USING_SIGNALS
return rt_thread_kill(thread, sig);
#else
return ENOSYS;
#endif
} }
RTM_EXPORT(pthread_kill); RTM_EXPORT(pthread_kill);
#ifdef RT_USING_SIGNALS
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
return sigprocmask(how, set, oset);
}
#endif
void pthread_cleanup_pop(int execute) void pthread_cleanup_pop(int execute)
{ {
_pthread_data_t *ptd; _pthread_data_t *ptd;
......
...@@ -268,11 +268,13 @@ int pthread_barrier_wait(pthread_barrier_t *barrier); ...@@ -268,11 +268,13 @@ int pthread_barrier_wait(pthread_barrier_t *barrier);
/* Signal Generation and Delivery, P1003.1b-1993, p. 63 /* Signal Generation and Delivery, P1003.1b-1993, p. 63
NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
sigev_notify_attributes to the sigevent structure. */ sigev_notify_attributes to the sigevent structure. */
#if 0
union sigval union sigval
{ {
int sival_int; /* Integer signal value */ int sival_int; /* Integer signal value */
void *sival_ptr; /* Pointer signal value */ void *sival_ptr; /* Pointer signal value */
}; };
#endif
struct sigevent struct sigevent
{ {
......
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
depend = ['RT_USING_SIGNALS', 'RT_USING_PTHREADS', 'RT_USING_LIBC'],
CPPPATH = CPPPATH)
Return('group')
/*
* File : signal.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/1 Bernard The first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <time.h>
#include "posix_signal.h"
#define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
void (*signal(int sig, void (*func)(int))) (int)
{
return rt_signal_install(sig, func);
}
int sigprocmask (int how, const sigset_t *set, sigset_t *oset)
{
rt_base_t level;
rt_thread_t tid;
tid = rt_thread_self();
level = rt_hw_interrupt_disable();
if (oset) *oset = tid->sig_mask;
if (set)
{
switch(how)
{
case SIG_BLOCK:
tid->sig_mask |= *set;
break;
case SIG_UNBLOCK:
tid->sig_mask &= ~*set;
break;
case SIG_SETMASK:
tid->sig_mask = *set;
break;
default:
break;
}
}
rt_hw_interrupt_enable(level);
return 0;
}
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
{
rt_sighandler_t old = RT_NULL;
if (!sig_valid(signum)) return -RT_ERROR;
if (act)
old = rt_signal_install(signum, act->sa_handler);
else
{
old = rt_signal_install(signum, RT_NULL);
rt_signal_install(signum, old);
}
if (oldact)
oldact->sa_handler = old;
return 0;
}
int sigtimedwait(const sigset_t *set, siginfo_t *info,
const struct timespec *timeout)
{
return 0;
}
int sigwait(const sigset_t *set, int *sig)
{
siginfo_t si;
if (sigtimedwait(set, &si, 0) < 0)
return -1;
*sig = si.si_signo;
return 0;
}
int sigwaitinfo(const sigset_t *set, siginfo_t *info)
{
return sigtimedwait(set, info, NULL);
}
int raise(int sig)
{
rt_thread_kill(rt_thread_self(), sig);
return 0;
}
/*
* File : signals.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/10/1 Bernard The first version
*/
#ifndef POSIX_SIGNAL_H__
#define POSIX_SIGNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <rtthread.h>
enum rt_signal_value{
SIG1 = SIGHUP,
SIG2 = SIGINT,
SIG3 = SIGQUIT,
SIG4 = SIGILL,
SIG5 = SIGTRAP,
SIG6 = SIGABRT,
SIG7 = SIGEMT,
SIG8 = SIGFPE,
SIG9 = SIGKILL,
SIG10 = SIGBUS,
SIG11 = SIGSEGV,
SIG12 = SIGSYS,
SIG13 = SIGPIPE,
SIG14 = SIGALRM,
SIG15 = SIGTERM,
SIG16 = SIGURG,
SIG17 = SIGSTOP,
SIG18 = SIGTSTP,
SIG19 = SIGCONT,
SIG20 = SIGCHLD,
SIG21 = SIGTTIN,
SIG22 = SIGTTOU,
SIG23 = SIGPOLL,
SIG24 = 24, // SIGXCPU,
SIG25 = 25, // SIGXFSZ,
SIG26 = 26, // SIGVTALRM,
SIG27 = 27, // SIGPROF,
SIG28 = SIGWINCH,
SIG29 = 29, // SIGLOST,
SIG30 = SIGUSR1,
SIG31 = SIGUSR2,
SIGRT_MIN = 27, // SIGRTMIN,
SIGRT_MAX = 31, // SIGRTMAX,
SIGMAX = NSIG,
};
/*
The structure definitions on newlib:
typedef void (*_sig_func_ptr)(int);
struct sigaction
{
_sig_func_ptr sa_handler;
sigset_t sa_mask;
int sa_flags;
};
typedef int sig_atomic_t;
typedef _sig_func_ptr sig_t;
typedef _sig_func_ptr sighandler_t;
When enable POSIX_REALTIME_SIGNALS/POSIX_THREADS:
union sigval {
int sival_int;
void *sival_ptr;
};
struct sigevent {
int sigev_notify;
int sigev_signo;
union sigval sigev_value;
void (*sigev_notify_function)( union sigval );
pthread_attr_t *sigev_notify_attributes;
};
typedef struct {
int si_signo;
int si_code;
union sigval si_value;
} siginfo_t;
*/
rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler);
void rt_signal_mask(int signo);
void rt_signal_unmask(int signo);
int rt_thread_kill(rt_thread_t tid, int sig);
int rt_system_signal_init(void);
#ifdef __cplusplus
}
#endif
#endif
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
depend = ['RT_USING_LIBC', 'RT_USING_POSIX_TERMIOS'],
CPPPATH = CPPPATH)
Return('group')
/*
* File : termios.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/08/30 Bernard The first version
*/
#include <stdlib.h>
#include <string.h>
#include <rtthread.h>
#include <dfs_posix.h>
#include <termios.h>
#include "posix_termios.h"
int tcgetattr(int fd, struct termios *tio)
{
/* Get the current serial port settings. */
if (ioctl(fd, TCGETA, tio))
return -1;
return 0;
}
int tcsetattr(int fd, int act, const struct termios *tio)
{
switch (act)
{
case TCSANOW:
/* make the change immediately */
return (ioctl(fd, TCSETA, (void*)tio));
case TCSADRAIN:
/*
* Don't make the change until all currently written data
* has been transmitted.
*/
return (ioctl(fd, TCSETAW, (void*)tio));
case TCSAFLUSH:
/* Don't make the change until all currently written data
* has been transmitted, at which point any received but
* unread data is also discarded.
*/
return (ioctl(fd, TCSETAF, (void*)tio));
default:
errno = EINVAL;
return (-1);
}
}
/**
* this function gets process group ID for session leader for controlling
* terminal
*
* @return always 0
*/
pid_t tcgetsid(int fd)
{
return 0;
}
speed_t cfgetospeed(const struct termios *tio)
{
return tio->c_cflag & CBAUD;
}
speed_t cfgetispeed(const struct termios *tio)
{
return cfgetospeed(tio);
}
int cfsetospeed(struct termios *tio, speed_t speed)
{
if (speed & ~CBAUD)
{
errno = EINVAL;
return -1;
}
tio->c_cflag &= ~CBAUD;
tio->c_cflag |= speed;
return 0;
}
int cfsetispeed(struct termios *tio, speed_t speed)
{
return speed ? cfsetospeed(tio, speed) : 0;
}
int tcsendbreak(int fd, int dur)
{
/* nonzero duration is implementation-defined, so ignore it */
return 0;
}
int tcflush(int fd, int queue)
{
return ioctl(fd, TCFLSH, (void*)queue);
}
int tcflow(int fd, int action)
{
return ioctl(fd, TCXONC, (void*)action);
}
/**
* this function waits for transmission of output
*/
int tcdrain(int fd)
{
return 0;
}
int isatty (int fd)
{
struct termios term;
return tcgetattr (fd, &term) == 0;
}
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
void cfmakeraw(struct termios *t)
{
t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
t->c_oflag &= ~OPOST;
t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
t->c_cflag &= ~(CSIZE|PARENB);
t->c_cflag |= CS8;
t->c_cc[VMIN] = 1;
t->c_cc[VTIME] = 0;
}
int cfsetspeed(struct termios *tio, speed_t speed)
{
return cfsetospeed(tio, speed);
}
#endif
/*
* File : termios.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017/08/30 Bernard The first version
*/
#ifndef TERMIOS_H__
#define TERMIOS_H__
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned char cc_t;
typedef unsigned int speed_t;
typedef unsigned int tcflag_t;
#define NCCS 32
struct termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_line;
cc_t c_cc[NCCS];
speed_t __c_ispeed;
speed_t __c_ospeed;
};
#define VINTR 0
#define VQUIT 1
#define VERASE 2
#define VKILL 3
#define VEOF 4
#define VTIME 5
#define VMIN 6
#define VSWTC 7
#define VSTART 8
#define VSTOP 9
#define VSUSP 10
#define VEOL 11
#define VREPRINT 12
#define VDISCARD 13
#define VWERASE 14
#define VLNEXT 15
#define VEOL2 16
#define IGNBRK 0000001
#define BRKINT 0000002
#define IGNPAR 0000004
#define PARMRK 0000010
#define INPCK 0000020
#define ISTRIP 0000040
#define INLCR 0000100
#define IGNCR 0000200
#define ICRNL 0000400
#define IUCLC 0001000
#define IXON 0002000
#define IXANY 0004000
#define IXOFF 0010000
#define IMAXBEL 0020000
#define IUTF8 0040000
#define OPOST 0000001
#define OLCUC 0000002
#define ONLCR 0000004
#define OCRNL 0000010
#define ONOCR 0000020
#define ONLRET 0000040
#define OFILL 0000100
#define OFDEL 0000200
#define NLDLY 0000400
#define NL0 0000000
#define NL1 0000400
#define CRDLY 0003000
#define CR0 0000000
#define CR1 0001000
#define CR2 0002000
#define CR3 0003000
#define TABDLY 0014000
#define TAB0 0000000
#define TAB1 0004000
#define TAB2 0010000
#define TAB3 0014000
#define BSDLY 0020000
#define BS0 0000000
#define BS1 0020000
#define FFDLY 0100000
#define FF0 0000000
#define FF1 0100000
#define VTDLY 0040000
#define VT0 0000000
#define VT1 0040000
#define B0 0000000
#define B50 0000001
#define B75 0000002
#define B110 0000003
#define B134 0000004
#define B150 0000005
#define B200 0000006
#define B300 0000007
#define B600 0000010
#define B1200 0000011
#define B1800 0000012
#define B2400 0000013
#define B4800 0000014
#define B9600 0000015
#define B19200 0000016
#define B38400 0000017
#define B57600 0010001
#define B115200 0010002
#define B230400 0010003
#define B460800 0010004
#define B500000 0010005
#define B576000 0010006
#define B921600 0010007
#define B1000000 0010010
#define B1152000 0010011
#define B1500000 0010012
#define B2000000 0010013
#define B2500000 0010014
#define B3000000 0010015
#define B3500000 0010016
#define B4000000 0010017
#define CSIZE 0000060
#define CS5 0000000
#define CS6 0000020
#define CS7 0000040
#define CS8 0000060
#define CSTOPB 0000100
#define CREAD 0000200
#define PARENB 0000400
#define PARODD 0001000
#define HUPCL 0002000
#define CLOCAL 0004000
#define ISIG 0000001
#define ICANON 0000002
#define ECHO 0000010
#define ECHOE 0000020
#define ECHOK 0000040
#define ECHONL 0000100
#define NOFLSH 0000200
#define TOSTOP 0000400
#define IEXTEN 0100000
#define TCOOFF 0
#define TCOON 1
#define TCIOFF 2
#define TCION 3
#define TCIFLUSH 0
#define TCOFLUSH 1
#define TCIOFLUSH 2
#define TCSANOW 0
#define TCSADRAIN 1
#define TCSAFLUSH 2
#define EXTA 0000016
#define EXTB 0000017
#define CBAUD 0010017
#define CBAUDEX 0010000
#define CIBAUD 002003600000
#define CMSPAR 010000000000
#define CRTSCTS 020000000000
#define XCASE 0000004
#define ECHOCTL 0001000
#define ECHOPRT 0002000
#define ECHOKE 0004000
#define FLUSHO 0010000
#define PENDIN 0040000
#define EXTPROC 0200000
#define XTABS 0014000
speed_t cfgetospeed (const struct termios *);
speed_t cfgetispeed (const struct termios *);
int cfsetospeed (struct termios *, speed_t);
int cfsetispeed (struct termios *, speed_t);
int tcgetattr (int, struct termios *);
int tcsetattr (int, int, const struct termios *);
int tcsendbreak (int, int);
int tcdrain (int);
int tcflush (int, int);
int tcflow (int, int);
pid_t tcgetsid (int);
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
void cfmakeraw(struct termios *);
int cfsetspeed(struct termios *, speed_t);
#endif
#ifdef __cplusplus
}
#endif
#endif
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
depend = ['RT_USING_LIBC', 'RT_USING_POSIX_TIMER'], CPPPATH = CPPPATH)
Return('group')
#include <stdlib.h>
#include <rtthread.h>
#include <unistd.h>
unsigned int sleep(unsigned int seconds)
{
rt_tick_t delta_tick;
delta_tick = rt_tick_get();
rt_thread_delay(seconds * RT_TICK_PER_SECOND);
delta_tick = rt_tick_get() - delta_tick;
return seconds - delta_tick/RT_TICK_PER_SECOND;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册