diff options
| author | ousugo <[email protected]> | 2021-12-14 10:42:18 +0800 |
|---|---|---|
| committer | ousugo <[email protected]> | 2021-12-14 10:42:18 +0800 |
| commit | 4820dbb0b3a15a62f4cd5f3e20fb4e02e4bfbb20 (patch) | |
| tree | 27698aa9180b0c25dd9c4d9ced3320da10360b1d /components/libc/posix/src | |
| parent | a237f805b940db16ef0d0009e56eb183f5713c07 (diff) | |
| parent | c5208a7d19871573dcb719358d34d3effa0682e9 (diff) | |
Merge branch 'master' into addAnnotation
Diffstat (limited to 'components/libc/posix/src')
| -rw-r--r-- | components/libc/posix/src/SConscript | 16 | ||||
| -rw-r--r-- | components/libc/posix/src/libc.c | 147 | ||||
| -rw-r--r-- | components/libc/posix/src/libc.h | 29 | ||||
| -rw-r--r-- | components/libc/posix/src/mmap.c | 67 | ||||
| -rw-r--r-- | components/libc/posix/src/poll.c | 226 | ||||
| -rw-r--r-- | components/libc/posix/src/select.c | 176 |
6 files changed, 0 insertions, 661 deletions
diff --git a/components/libc/posix/src/SConscript b/components/libc/posix/src/SConscript index 4be5ac83cc..87be6b6a06 100644 --- a/components/libc/posix/src/SConscript +++ b/components/libc/posix/src/SConscript @@ -10,26 +10,10 @@ group = [] flag = False src += ['unistd.c'] #TODO -if GetDepend('RT_USING_POSIX_DEVIO'): - src += ['libc.c'] - flag = True - if GetDepend('RT_USING_POSIX_DELAY'): src += ['delay.c'] flag = True -if GetDepend('RT_USING_POSIX_POLL'): - src += ['poll.c'] - flag = True - -if GetDepend('RT_USING_POSIX_SELECT'): - src += ['select.c'] - flag = True - -if GetDepend('RT_USING_POSIX_MMAP'): - src += ['mmap.c'] - flag = True - if flag == True: group = DefineGroup('POSIX', src, depend = [], CPPPATH = CPPPATH) diff --git a/components/libc/posix/src/libc.c b/components/libc/posix/src/libc.c deleted file mode 100644 index c324d84ef8..0000000000 --- a/components/libc/posix/src/libc.c +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <rtthread.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/time.h> -#include "libc.h" -#include <stdio.h> -#include <stdlib.h> -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int libc_system_init(void) -{ -#ifdef RT_USING_POSIX_DEVIO - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - } -#endif /* RT_USING_POSIX_DEVIO */ - -#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); - -#ifdef RT_USING_POSIX_DEVIO -#if defined(RT_USING_LIBC) && defined(RT_USING_NEWLIB) -#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 - { - 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; - } - - if (std_console) - return fileno(std_console); - - return -1; -} - -int libc_stdio_get_console(void) -{ - if (std_console) - return fileno(std_console); - else - return -1; -} - -#else -#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_get_console(void) { - return std_fd; -} -#endif /* defined(RT_USING_LIBC) && defined(RT_USING_NEWLIB) */ -#endif /* RT_USING_POSIX_DEVIO */ diff --git a/components/libc/posix/src/libc.h b/components/libc/posix/src/libc.h deleted file mode 100644 index b019267926..0000000000 --- a/components/libc/posix/src/libc.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#ifndef __RTT_LIBC_H__ -#define __RTT_LIBC_H__ - -#include <rtconfig.h> - -#ifdef __cplusplus -extern "C" { -#endif - -int libc_system_init(void); -#ifdef RT_USING_POSIX_DEVIO -int libc_stdio_get_console(void); -int libc_stdio_set_console(const char* device_name, int mode); -#endif /* RT_USING_POSIX_DEVIO */ - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/libc/posix/src/mmap.c b/components/libc/posix/src/mmap.c deleted file mode 100644 index e68b82e640..0000000000 --- a/components/libc/posix/src/mmap.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/11/30 Bernard The first version. - */ - -#include <stdint.h> -#include <stdio.h> - -#include <rtthread.h> -#include <dfs_posix.h> - -#include <sys/mman.h> - -void *mmap(void *addr, size_t length, int prot, int flags, - int fd, off_t offset) -{ - uint8_t *mem; - - if (addr) - { - mem = addr; - } - else mem = (uint8_t *)malloc(length); - - if (mem) - { - off_t cur; - size_t read_bytes; - - cur = lseek(fd, 0, SEEK_SET); - - lseek(fd, offset, SEEK_SET); - read_bytes = read(fd, mem, length); - if (read_bytes != length) - { - if (addr == RT_NULL) - { - /* read failed */ - free(mem); - mem = RT_NULL; - } - } - lseek(fd, cur, SEEK_SET); - - return mem; - } - - errno = ENOMEM; - - return MAP_FAILED; -} - -int munmap(void *addr, size_t length) -{ - if (addr) - { - free(addr); - return 0; - } - - return -1; -} diff --git a/components/libc/posix/src/poll.c b/components/libc/posix/src/poll.c deleted file mode 100644 index 0c773b306c..0000000000 --- a/components/libc/posix/src/poll.c +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016-12-28 Bernard first version - * 2018-03-09 Bernard Add protection for pt->triggered. - */ -#include <stdint.h> -#include <rthw.h> -#include <rtthread.h> -#include <dfs_file.h> -#include <poll.h> - -struct rt_poll_node; - -struct rt_poll_table -{ - rt_pollreq_t req; - rt_uint32_t triggered; /* the waited thread whether triggered */ - rt_thread_t polling_thread; - struct rt_poll_node *nodes; -}; - -struct rt_poll_node -{ - struct rt_wqueue_node wqn; - struct rt_poll_table *pt; - struct rt_poll_node *next; -}; - -static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key) -{ - struct rt_poll_node *pn; - - if (key && !((rt_ubase_t)key & wait->key)) - return -1; - - pn = rt_container_of(wait, struct rt_poll_node, wqn); - pn->pt->triggered = 1; - - return __wqueue_default_wake(wait, key); -} - -static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req) -{ - struct rt_poll_table *pt; - struct rt_poll_node *node; - - node = (struct rt_poll_node *)rt_malloc(sizeof(struct rt_poll_node)); - if (node == RT_NULL) - return; - - pt = rt_container_of(req, struct rt_poll_table, req); - - node->wqn.key = req->_key; - rt_list_init(&(node->wqn.list)); - node->wqn.polling_thread = pt->polling_thread; - node->wqn.wakeup = __wqueue_pollwake; - node->next = pt->nodes; - node->pt = pt; - pt->nodes = node; - rt_wqueue_add(wq, &node->wqn); -} - -static void poll_table_init(struct rt_poll_table *pt) -{ - pt->req._proc = _poll_add; - pt->triggered = 0; - pt->nodes = RT_NULL; - pt->polling_thread = rt_thread_self(); -} - -static int poll_wait_timeout(struct rt_poll_table *pt, int msec) -{ - rt_int32_t timeout; - int ret = 0; - struct rt_thread *thread; - rt_base_t level; - - thread = pt->polling_thread; - - timeout = rt_tick_from_millisecond(msec); - - level = rt_hw_interrupt_disable(); - - if (timeout != 0 && !pt->triggered) - { - rt_thread_suspend(thread); - if (timeout > 0) - { - rt_timer_control(&(thread->thread_timer), - RT_TIMER_CTRL_SET_TIME, - &timeout); - rt_timer_start(&(thread->thread_timer)); - } - - rt_hw_interrupt_enable(level); - - rt_schedule(); - - level = rt_hw_interrupt_disable(); - } - - ret = !pt->triggered; - rt_hw_interrupt_enable(level); - - return ret; -} - -static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) -{ - int mask = 0; - int fd; - - fd = pollfd->fd; - - if (fd >= 0) - { - struct dfs_fd *f = fd_get(fd); - mask = POLLNVAL; - - if (f) - { - mask = POLLMASK_DEFAULT; - if (f->fops->poll) - { - req->_key = pollfd->events | POLLERR | POLLHUP; - - mask = f->fops->poll(f, req); - - /* dealwith the device return error -1*/ - if (mask < 0) - { - fd_put(f); - pollfd->revents = 0; - return mask; - } - } - /* Mask out unneeded events. */ - mask &= pollfd->events | POLLERR | POLLHUP; - fd_put(f); - } - } - pollfd->revents = mask; - - return mask; -} - -static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec) -{ - int num; - int istimeout = 0; - int n; - struct pollfd *pf; - int ret = 0; - - if (msec == 0) - { - pt->req._proc = RT_NULL; - istimeout = 1; - } - - while (1) - { - pf = fds; - num = 0; - - for (n = 0; n < nfds; n ++) - { - ret = do_pollfd(pf, &pt->req); - if(ret < 0) - { - /*dealwith the device return error -1 */ - pt->req._proc = RT_NULL; - return ret; - } - else if(ret > 0) - { - num ++; - pt->req._proc = RT_NULL; - } - pf ++; - } - - pt->req._proc = RT_NULL; - - if (num || istimeout) - break; - - if (poll_wait_timeout(pt, msec)) - istimeout = 1; - } - - return num; -} - -static void poll_teardown(struct rt_poll_table *pt) -{ - struct rt_poll_node *node, *next; - - next = pt->nodes; - while (next) - { - node = next; - rt_wqueue_remove(&node->wqn); - next = node->next; - rt_free(node); - } -} - -int poll(struct pollfd *fds, nfds_t nfds, int timeout) -{ - int num; - struct rt_poll_table table; - - poll_table_init(&table); - - num = poll_do(fds, nfds, &table, timeout); - - poll_teardown(&table); - - return num; -} diff --git a/components/libc/posix/src/select.c b/components/libc/posix/src/select.c deleted file mode 100644 index 55d320e921..0000000000 --- a/components/libc/posix/src/select.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016-12-28 Bernard first version - */ -#include <rtthread.h> -#include <poll.h> -#include <sys/select.h> - -static void fdszero(fd_set *set, int nfds) -{ - fd_mask *m; - int n; - - /* - The 'sizeof(fd_set)' of the system space may differ from user space, - so the actual size of the 'fd_set' is determined here with the parameter 'nfds' - */ - m = (fd_mask *)set; - for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8)) - { - rt_memset(m, 0, sizeof(fd_mask)); - m ++; - } -} - -int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) -{ - int fd; - int npfds; - int msec; - int ndx; - int ret; - struct pollfd *pollset = RT_NULL; - - /* How many pollfd structures do we need to allocate? */ - for (fd = 0, npfds = 0; fd < nfds; fd++) - { - /* Check if any monitor operation is requested on this fd */ - if ((readfds && FD_ISSET(fd, readfds)) || - (writefds && FD_ISSET(fd, writefds)) || - (exceptfds && FD_ISSET(fd, exceptfds))) - { - npfds++; - } - } - - /* Allocate the descriptor list for poll() */ - if (npfds > 0) - { - pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd)); - if (!pollset) - { - return -1; - } - } - - /* Initialize the descriptor list for poll() */ - for (fd = 0, ndx = 0; fd < nfds; fd++) - { - int incr = 0; - - /* The readfs set holds the set of FDs that the caller can be assured - * of reading from without blocking. Note that POLLHUP is included as - * a read-able condition. POLLHUP will be reported at the end-of-file - * or when a connection is lost. In either case, the read() can then - * be performed without blocking. - */ - - if (readfds && FD_ISSET(fd, readfds)) - { - pollset[ndx].fd = fd; - pollset[ndx].events |= POLLIN; - incr = 1; - } - - if (writefds && FD_ISSET(fd, writefds)) - { - pollset[ndx].fd = fd; - pollset[ndx].events |= POLLOUT; - incr = 1; - } - - if (exceptfds && FD_ISSET(fd, exceptfds)) - { - pollset[ndx].fd = fd; - incr = 1; - } - - ndx += incr; - } - - RT_ASSERT(ndx == npfds); - - /* Convert the timeout to milliseconds */ - if (timeout) - { - msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; - } - else - { - msec = -1; - } - - /* Then let poll do all of the real work. */ - - ret = poll(pollset, npfds, msec); - - /* Now set up the return values */ - if (readfds) - { - fdszero(readfds, nfds); - } - - if (writefds) - { - fdszero(writefds, nfds); - } - - if (exceptfds) - { - fdszero(exceptfds, nfds); - } - - /* Convert the poll descriptor list back into selects 3 bitsets */ - - if (ret > 0) - { - ret = 0; - for (ndx = 0; ndx < npfds; ndx++) - { - /* Check for read conditions. Note that POLLHUP is included as a - * read condition. POLLHUP will be reported when no more data will - * be available (such as when a connection is lost). In either - * case, the read() can then be performed without blocking. - */ - - if (readfds) - { - if (pollset[ndx].revents & (POLLIN | POLLHUP)) - { - FD_SET(pollset[ndx].fd, readfds); - ret++; - } - } - - /* Check for write conditions */ - if (writefds) - { - if (pollset[ndx].revents & POLLOUT) - { - FD_SET(pollset[ndx].fd, writefds); - ret++; - } - } - - /* Check for exceptions */ - if (exceptfds) - { - if (pollset[ndx].revents & POLLERR) - { - FD_SET(pollset[ndx].fd, exceptfds); - ret++; - } - } - } - } - - if (pollset) rt_free(pollset); - - return ret; -} |
