diff options
Diffstat (limited to 'lib')
36 files changed, 6375 insertions, 0 deletions
diff --git a/lib/FreeRTOS b/lib/FreeRTOS new file mode 160000 +Subproject 08af68ef9049279b265c3d00e9c48fb9594129a diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX.h new file mode 100755 index 0000000..d938709 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX.h @@ -0,0 +1,50 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX.h + * @brief FreeRTOS+POSIX header. + * + * This file must be included before all other FreeRTOS+POSIX includes. + */ + +#ifndef _FREERTOS_POSIX_H_ +#define _FREERTOS_POSIX_H_ + +/* FreeRTOS+POSIX platform-specific configuration headers. */ +#include "FreeRTOS_POSIX_portable.h" +#include "FreeRTOS_POSIX_portable_default.h" + +/* FreeRTOS includes. */ +#include "FreeRTOS.h" +#include "event_groups.h" +#include "semphr.h" +#include "task.h" + +/* FreeRTOS+POSIX data types and internal structs. */ +#include "FreeRTOS_POSIX/sys/types.h" +#include "FreeRTOS_POSIX_internal.h" + +#endif /* _FREERTOS_POSIX_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_internal.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_internal.h new file mode 100755 index 0000000..16f8036 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_internal.h @@ -0,0 +1,129 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef _FREERTOS_POSIX_INTERNAL_H_ +#define _FREERTOS_POSIX_INTERNAL_H_ + +/** + * @file FreeRTOS_POSIX_internal.h + * @brief Internal structs and initializers for FreeRTOS+POSIX. + */ + +/* Amazon FreeRTOS includes. */ +#include "iot_doubly_linked_list.h" + +/** + * @brief Mutex attribute object. + */ +#if posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1 + typedef struct pthread_mutexattr_internal + { + int iType; /**< Mutex type. */ + } pthread_mutexattr_internal_t; +#endif + +#if posixconfigENABLE_PTHREAD_MUTEX_T == 1 + +/** + * @brief Mutex. + */ + typedef struct pthread_mutex_internal + { + BaseType_t xIsInitialized; /**< Set to pdTRUE if this mutex is initialized, pdFALSE otherwise. */ + StaticSemaphore_t xMutex; /**< FreeRTOS mutex. */ + TaskHandle_t xTaskOwner; /**< Owner; used for deadlock detection and permission checks. */ + pthread_mutexattr_internal_t xAttr; /**< Mutex attributes. */ + } pthread_mutex_internal_t; + +/** + * @brief Compile-time initializer of pthread_mutex_internal_t. + */ + #define FREERTOS_POSIX_MUTEX_INITIALIZER \ + ( ( ( pthread_mutex_internal_t ) \ + { \ + .xIsInitialized = pdFALSE, \ + .xMutex = { { 0 } }, \ + .xTaskOwner = NULL, \ + .xAttr = { .iType = 0 } \ + } \ + ) \ + ) +#endif /* if posixconfigENABLE_PTHREAD_MUTEX_T == 1 */ + +#if posixconfigENABLE_PTHREAD_COND_T == 1 + +/** + * @brief Condition variable. + */ + typedef struct pthread_cond_internal + { + BaseType_t xIsInitialized; /**< Set to pdTRUE if this condition variable is initialized, pdFALSE otherwise. */ + StaticSemaphore_t xCondWaitSemaphore; /**< Threads block on this semaphore in pthread_cond_wait. */ + unsigned iWaitingThreads; /**< The number of threads currently waiting on this condition variable. */ + } pthread_cond_internal_t; + +/** + * @brief Compile-time initializer of pthread_cond_internal_t. + */ + + #define FREERTOS_POSIX_COND_INITIALIZER \ + ( ( ( pthread_cond_internal_t ) \ + { \ + .xIsInitialized = pdFALSE, \ + .xCondWaitSemaphore = { { 0 } }, \ + .iWaitingThreads = 0 \ + } \ + ) \ + ) + +#endif /* if posixconfigENABLE_PTHREAD_COND_T == 1 */ + +#if posixconfigENABLE_SEM_T == 1 + +/** + * @brief Semaphore type. + */ + typedef struct + { + StaticSemaphore_t xSemaphore; /**< FreeRTOS semaphore. */ + int value; /**< POSIX semaphore count. */ + } sem_internal_t; +#endif /* if posixconfigENABLE_SEM_T == 1 */ + +#if posixconfigENABLE_PTHREAD_BARRIER_T == 1 + +/** + * @brief Barrier object. + */ + typedef struct pthread_barrier_internal + { + unsigned uThreadCount; /**< Current number of threads that have entered barrier. */ + unsigned uThreshold; /**< The count argument of pthread_barrier_init. */ + StaticSemaphore_t xThreadCountSemaphore; /**< Prevents more than uThreshold threads from exiting pthread_barrier_wait at once. */ + StaticEventGroup_t xBarrierEventGroup; /**< FreeRTOS event group that blocks to wait on threads entering barrier. */ + } pthread_barrier_internal_t; +#endif /* if posixconfigENABLE_PTHREAD_BARRIER_T == 1 */ + +#endif /* _FREERTOS_POSIX_INTERNAL_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_types.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_types.h new file mode 100755 index 0000000..3b45b8d --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/FreeRTOS_POSIX_types.h @@ -0,0 +1,81 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +#ifndef _FREERTOS_POSIX_INTERNAL_TYPES_H_ +#define _FREERTOS_POSIX_INTERNAL_TYPES_H_ + +#include "FreeRTOS_POSIX_internal.h" + +/* + * sys/types.h defines a POSIX type when posixconfigENABLE_PTHREAD_<TYPE>_T + * is not defined AND when posixconfigENABLE_PTHREAD_<TYPE>_T is set to 1. + * FreeRTOS_POSIX_internal.h defines internal type ONLY when + * posixconfigENABLE_PTHREAD_<TYPE>_T is set to 1. + * #else part below is to have a type defined, so the code compiles, when + * posixconfigENABLE_PTHREAD_<TYPE>_T is not defined. + */ +#if posixconfigENABLE_PTHREAD_MUTEX_T == 1 + typedef pthread_mutex_internal_t PthreadMutexType_t; +#else + typedef void * PthreadMutexType_t; +#endif + +#if posixconfigENABLE_PTHREAD_COND_T == 1 + typedef pthread_cond_internal_t PthreadCondType_t; +#else + typedef void * PthreadCondType_t; +#endif + +#if posixconfigENABLE_SEM_T == 1 + typedef sem_internal_t PosixSemType_t; +#else + typedef void * PosixSemType_t; +#endif + +#if posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1 + typedef struct pthread_mutexattr + { + uint32_t ulpthreadMutexAttrStorage; + } PthreadMutexAttrType_t; +#else + typedef void * PthreadMutexAttrType_t; +#endif + +#if posixconfigENABLE_PTHREAD_ATTR_T == 1 + typedef struct pthread_attr + { + uint32_t ulpthreadAttrStorage; + } PthreadAttrType_t; +#else + typedef void * PthreadAttrType_t; +#endif + +#if posixconfigENABLE_PTHREAD_BARRIER_T == 1 + typedef pthread_barrier_internal_t PthreadBarrierType_t; +#else + typedef void * PthreadBarrierType_t; +#endif + +#endif /* _FREERTOS_POSIX_INTERNAL_TYPES_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/FreeRTOS_POSIX_portable_default.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/FreeRTOS_POSIX_portable_default.h new file mode 100755 index 0000000..763b901 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/FreeRTOS_POSIX_portable_default.h @@ -0,0 +1,145 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable_default.h + * @brief Defaults for port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_DEFAULT_H_ +#define _FREERTOS_POSIX_PORTABLE_DEFAULT_H_ + +/** + * @name The FreeRTOS task name given to pthreads. + */ +/**@{ */ +#ifndef posixconfigPTHREAD_TASK_NAME + #define posixconfigPTHREAD_TASK_NAME "pthread" /**< Task name. */ +#endif +/**@} */ + +/** + * @name the FreeRTOS timer name given to POSIX timers. + */ +/**@{ */ +#ifndef posixconfigTIMER_NAME + #define posixconfigTIMER_NAME "timer" /**< Timer name. */ +#endif +/**@} */ + +/** + * @name Defaults for POSIX message queue implementation. + */ +/**@{ */ +#ifndef posixconfigMQ_MAX_MESSAGES + #define posixconfigMQ_MAX_MESSAGES 10 /**< Maximum number of messages in an mq at one time. */ +#endif + +#ifndef posixconfigMQ_MAX_SIZE + #define posixconfigMQ_MAX_SIZE 128 /**< Maximum size (in bytes) of each message. */ +#endif +/**@} */ + +/** + * @name POSIX implementation-dependent constants usually defined in limits.h. + * + * They are defined here to provide portability between platforms. + */ +/**@{ */ +#ifndef PTHREAD_STACK_MIN + #define PTHREAD_STACK_MIN configMINIMAL_STACK_SIZE * sizeof( StackType_t ) /**< Minimum size in bytes of thread stack storage. */ +#endif +#ifndef NAME_MAX + #define NAME_MAX 64 /**< Maximum number of bytes in a filename (not including terminating null). */ +#endif +#ifndef SEM_VALUE_MAX + #define SEM_VALUE_MAX 0x7FFFU /**< Maximum value of a sem_t. */ +#endif +/**@} */ + +/** + * @name Enable typedefs of POSIX types. + * + * Set these values to 1 or 0 to enable or disable the typedefs, respectively. + * These typedefs should only be disabled if they conflict with system typedefs. + */ +/**@{ */ +#ifndef posixconfigENABLE_CLOCK_T + #define posixconfigENABLE_CLOCK_T 1 /**< clock_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_CLOCKID_T + #define posixconfigENABLE_CLOCKID_T 1 /**< clockid_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_MODE_T + #define posixconfigENABLE_MODE_T 1 /**< mode_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PID_T + #define posixconfigENABLE_PID_T 1 /**< pid_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_ATTR_T + #define posixconfigENABLE_PTHREAD_ATTR_T 1 /**< pthread_attr_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_COND_T + #define posixconfigENABLE_PTHREAD_COND_T 1 /**< pthread_cond_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_CONDATTR_T + #define posixconfigENABLE_PTHREAD_CONDATTR_T 1 /**< pthread_condattr_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_MUTEX_T + #define posixconfigENABLE_PTHREAD_MUTEX_T 1 /**< pthread_mutex_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_MUTEXATTR_T + #define posixconfigENABLE_PTHREAD_MUTEXATTR_T 1 /**< pthread_mutexattr_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_T + #define posixconfigENABLE_PTHREAD_T 1 /**< pthread_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_SSIZE_T + #define posixconfigENABLE_SSIZE_T 1 /**< ssize_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_TIME_T + #define posixconfigENABLE_TIME_T 1 /**< time_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_TIMER_T + #define posixconfigENABLE_TIMER_T 1 /**< timer_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_USECONDS_T + #define posixconfigENABLE_USECONDS_T 1 /**< useconds_t in sys/types.h */ +#endif +#ifndef posixconfigENABLE_TIMESPEC + #define posixconfigENABLE_TIMESPEC 1 /**< struct timespec in time.h */ +#endif +#ifndef posixconfigENABLE_ITIMERSPEC + #define posixconfigENABLE_ITIMERSPEC 1 /**< struct itimerspec in time.h */ +#endif +#ifndef posixconfigENABLE_SEM_T + #define posixconfigENABLE_SEM_T 1 /**< struct sem_t in semaphore.h */ +#endif +#ifndef posixconfigENABLE_PTHREAD_BARRIER_T + #define posixconfigENABLE_PTHREAD_BARRIER_T 1 /**< pthread_barrier_t in sys/types.h */ +#endif +/**@} */ + +#endif /* ifndef _FREERTOS_POSIX_PORTABLE_DEFAULT_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/espressif/esp32_devkitc_esp_wrover_kit/FreeRTOS_POSIX_portable.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/espressif/esp32_devkitc_esp_wrover_kit/FreeRTOS_POSIX_portable.h new file mode 100755 index 0000000..c187043 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/espressif/esp32_devkitc_esp_wrover_kit/FreeRTOS_POSIX_portable.h @@ -0,0 +1,60 @@ +/* + * Amazon FreeRTOS+POSIX V1.0.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable.h + * @brief Port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_H_ +#define _FREERTOS_POSIX_PORTABLE_H_ + +/* ESP-IDF already defines the following types. */ +#define posixconfigENABLE_CLOCK_T 0 +#define posixconfigENABLE_CLOCKID_T 0 +#define posixconfigENABLE_MODE_T 0 +#define posixconfigENABLE_PTHREAD_ATTR_T 0 +#define posixconfigENABLE_PTHREAD_COND_T 0 +#define posixconfigENABLE_PTHREAD_CONDATTR_T 0 +#define posixconfigENABLE_PTHREAD_MUTEX_T 0 +#define posixconfigENABLE_PTHREAD_MUTEXATTR_T 0 +#define posixconfigENABLE_PTHREAD_T 0 +#define posixconfigENABLE_TIME_T 0 +#define posixconfigENABLE_TIMESPEC 0 +#define posixconfigENABLE_ITIMERSPEC 0 + +/* ESP-IDF already provides the header sched.h. Exclude FreeRTOS+POSIX sched.h by + * defining its double inclusion guard. */ +#define _FREERTOS_POSIX_SCHED_H_ + +/* Use the FreeRTOS+POSIX time.h header instead of the ESP-IDF time.h. Disable + * ESP-IDF time.h by defining its double inclusion guard. */ +#define _TIME_H_ + +/* Disable the timer_t type defined by ESP-IDF. */ +#define __timer_t_defined +#include <sys/types.h> + +#endif /* _FREERTOS_POSIX_PORTABLE_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/microchip/curiosity_pic32mzef/FreeRTOS_POSIX_portable.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/microchip/curiosity_pic32mzef/FreeRTOS_POSIX_portable.h new file mode 100755 index 0000000..d4d2db7 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/microchip/curiosity_pic32mzef/FreeRTOS_POSIX_portable.h @@ -0,0 +1,50 @@ +/* + * Amazon FreeRTOS+POSIX V1.0.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable.h + * @brief Port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_H_ +#define _FREERTOS_POSIX_PORTABLE_H_ + +/* Microchip includes. */ +#include <sys/types.h> + +/* Microchip already typedefs the following types. */ +#define posixconfigENABLE_MODE_T 0 +#define posixconfigENABLE_PID_T 0 +#define posixconfigENABLE_SSIZE_T 0 +#define posixconfigENABLE_USECONDS_T 0 +/* Microchip -mnewlib compiler option supports these types. */ +#define posixconfigENABLE_TIMESPEC 0 +#define posixconfigENABLE_ITIMERSPEC 0 +#define posixconfigENABLE_CLOCKID_T 0 +#define posixconfigENABLE_TIME_T 0 +#define posixconfigENABLE_TIMER_T 0 + + +#endif /* _FREERTOS_POSIX_PORTABLE_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/nxp/lpc54018iotmodule/FreeRTOS_POSIX_portable.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/nxp/lpc54018iotmodule/FreeRTOS_POSIX_portable.h new file mode 100755 index 0000000..1022f4b --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/nxp/lpc54018iotmodule/FreeRTOS_POSIX_portable.h @@ -0,0 +1,37 @@ +/* + * Amazon FreeRTOS+POSIX V1.0.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable.h + * @brief Port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_H_ +#define _FREERTOS_POSIX_PORTABLE_H_ + +/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this + * file is empty. */ + +#endif /* _FREERTOS_POSIX_PORTABLE_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/pc/windows/FreeRTOS_POSIX_portable.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/pc/windows/FreeRTOS_POSIX_portable.h new file mode 100755 index 0000000..1022f4b --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/pc/windows/FreeRTOS_POSIX_portable.h @@ -0,0 +1,37 @@ +/* + * Amazon FreeRTOS+POSIX V1.0.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable.h + * @brief Port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_H_ +#define _FREERTOS_POSIX_PORTABLE_H_ + +/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this + * file is empty. */ + +#endif /* _FREERTOS_POSIX_PORTABLE_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/st/stm32l475_discovery/FreeRTOS_POSIX_portable.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/st/stm32l475_discovery/FreeRTOS_POSIX_portable.h new file mode 100755 index 0000000..1022f4b --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/st/stm32l475_discovery/FreeRTOS_POSIX_portable.h @@ -0,0 +1,37 @@ +/* + * Amazon FreeRTOS+POSIX V1.0.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable.h + * @brief Port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_H_ +#define _FREERTOS_POSIX_PORTABLE_H_ + +/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this + * file is empty. */ + +#endif /* _FREERTOS_POSIX_PORTABLE_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/ti/cc3220_launchpad/FreeRTOS_POSIX_portable.h b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/ti/cc3220_launchpad/FreeRTOS_POSIX_portable.h new file mode 100755 index 0000000..1022f4b --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/include/portable/ti/cc3220_launchpad/FreeRTOS_POSIX_portable.h @@ -0,0 +1,37 @@ +/* + * Amazon FreeRTOS+POSIX V1.0.4 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_portable.h + * @brief Port-specific configuration of FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_PORTABLE_H_ +#define _FREERTOS_POSIX_PORTABLE_H_ + +/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this + * file is empty. */ + +#endif /* _FREERTOS_POSIX_PORTABLE_H_ */ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_clock.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_clock.c new file mode 100755 index 0000000..51e592a --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_clock.c @@ -0,0 +1,240 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_clock.c + * @brief Implementation of clock functions in time.h + */ + +/* C standard library includes. */ +#include <stddef.h> +#include <string.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/time.h" +#include "FreeRTOS_POSIX/utils.h" + +/* Declaration of snprintf. The header stdio.h is not included because it + * includes conflicting symbols on some platforms. */ +extern int snprintf( char * s, + size_t n, + const char * format, + ... ); + +/*-----------------------------------------------------------*/ + +clock_t clock( void ) +{ + /* This function is currently unsupported. It will always return -1. */ + + return ( clock_t ) -1; +} + +/*-----------------------------------------------------------*/ + +int clock_getcpuclockid( pid_t pid, + clockid_t * clock_id ) +{ + /* Silence warnings about unused parameters. */ + ( void ) pid; + ( void ) clock_id; + + /* This function is currently unsupported. It will always return EPERM. */ + return EPERM; +} + +/*-----------------------------------------------------------*/ + +int clock_getres( clockid_t clock_id, + struct timespec * res ) +{ + /* Silence warnings about unused parameters. */ + ( void ) clock_id; + + /* Convert FreeRTOS tick resolution as timespec. */ + if( res != NULL ) + { + res->tv_sec = 0; + res->tv_nsec = NANOSECONDS_PER_TICK; + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int clock_gettime( clockid_t clock_id, + struct timespec * tp ) +{ + TimeOut_t xCurrentTime = { 0 }; + + /* Intermediate variable used to convert TimeOut_t to struct timespec. + * Also used to detect overflow issues. It must be unsigned because the + * behavior of signed integer overflow is undefined. */ + uint64_t ullTickCount = 0ULL; + + /* Silence warnings about unused parameters. */ + ( void ) clock_id; + + /* Get the current tick count and overflow count. vTaskSetTimeOutState() + * is used to get these values because they are both static in tasks.c. */ + vTaskSetTimeOutState( &xCurrentTime ); + + /* Adjust the tick count for the number of times a TickType_t has overflowed. + * portMAX_DELAY should be the maximum value of a TickType_t. */ + ullTickCount = ( uint64_t ) ( xCurrentTime.xOverflowCount ) << ( sizeof( TickType_t ) * 8 ); + + /* Add the current tick count. */ + ullTickCount += xCurrentTime.xTimeOnEntering; + + /* Convert ullTickCount to timespec. */ + UTILS_NanosecondsToTimespec( ( int64_t ) ullTickCount * NANOSECONDS_PER_TICK, tp ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int clock_nanosleep( clockid_t clock_id, + int flags, + const struct timespec * rqtp, + struct timespec * rmtp ) +{ + int iStatus = 0; + TickType_t xSleepTime = 0; + struct timespec xCurrentTime = { 0 }; + + /* Silence warnings about unused parameters. */ + ( void ) clock_id; + ( void ) rmtp; + ( void ) flags; /* This is only ignored if INCLUDE_vTaskDelayUntil is 0. */ + + /* Check rqtp. */ + if( UTILS_ValidateTimespec( rqtp ) == false ) + { + iStatus = EINVAL; + } + + /* Get current time */ + if( ( iStatus == 0 ) && ( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) ) + { + iStatus = EINVAL; + } + + if( iStatus == 0 ) + { + /* Check for absolute time sleep. */ + if( ( flags & TIMER_ABSTIME ) == TIMER_ABSTIME ) + { + /* Get current time */ + if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) + { + iStatus = EINVAL; + } + + /* Get number of ticks until absolute time. */ + if( ( iStatus == 0 ) && ( UTILS_AbsoluteTimespecToDeltaTicks( rqtp, &xCurrentTime, &xSleepTime ) == 0 ) ) + { + /* Delay until absolute time if vTaskDelayUntil is available. */ + #if ( INCLUDE_vTaskDelayUntil == 1 ) + + /* Get the current tick count. This variable isn't declared + * at the top of the function because it's only used and needed + * if vTaskDelayUntil is available. */ + TickType_t xCurrentTicks = xTaskGetTickCount(); + + /* Delay until absolute time. */ + vTaskDelayUntil( &xCurrentTicks, xSleepTime ); + #else + + /* If vTaskDelayUntil isn't available, ignore the TIMER_ABSTIME flag + * and sleep for a relative time. */ + vTaskDelay( xSleepTime ); + #endif + } + } + else + { + /* If TIMER_ABSTIME isn't specified, convert rqtp to ticks and + * sleep for a relative time. */ + if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 ) + { + vTaskDelay( xSleepTime ); + } + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int clock_settime( clockid_t clock_id, + const struct timespec * tp ) +{ + /* Silence warnings about unused parameters. */ + ( void ) clock_id; + ( void ) tp; + + /* This function is currently unsupported. It will always return -1 and + * set errno to EPERM. */ + errno = EPERM; + + return -1; +} + +/*-----------------------------------------------------------*/ + +int nanosleep( const struct timespec * rqtp, + struct timespec * rmtp ) +{ + int iStatus = 0; + TickType_t xSleepTime = 0; + + /* Silence warnings about unused parameters. */ + ( void ) rmtp; + + /* Check rqtp. */ + if( UTILS_ValidateTimespec( rqtp ) == false ) + { + errno = EINVAL; + iStatus = -1; + } + + if( iStatus == 0 ) + { + /* Convert rqtp to ticks and delay. */ + if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 ) + { + vTaskDelay( xSleepTime ); + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_mqueue.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_mqueue.c new file mode 100755 index 0000000..9f9079b --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_mqueue.c @@ -0,0 +1,893 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_mqueue.c + * @brief Implementation of message queue functions in mqueue.h + */ + +/* C standard library includes. */ +#include <string.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/fcntl.h" +#include "FreeRTOS_POSIX/mqueue.h" +#include "FreeRTOS_POSIX/utils.h" + +/** + * @brief Element of the FreeRTOS queues that store mq data. + */ +typedef struct QueueElement +{ + char * pcData; /**< Data in queue. Type char* to match msg_ptr. */ + size_t xDataSize; /**< Size of data pointed by pcData. */ +} QueueElement_t; + +/** + * @brief Data structure of an mq. + * + * FreeRTOS isn't guaranteed to have a file-like abstraction, so message + * queues in this implementation are stored as a linked list (in RAM). + */ +typedef struct QueueListElement +{ + Link_t xLink; /**< Pointer to the next element in the list. */ + QueueHandle_t xQueue; /**< FreeRTOS queue handle. */ + size_t xOpenDescriptors; /**< Number of threads that have opened this queue. */ + char * pcName; /**< Null-terminated queue name. */ + struct mq_attr xAttr; /**< Queue attibutes. */ + BaseType_t xPendingUnlink; /**< If pdTRUE, this queue will be unlinked once all descriptors close. */ +} QueueListElement_t; + +/*-----------------------------------------------------------*/ + +/** + * @brief Convert an absolute timespec into a tick timeout, taking into account + * queue flags. + * + * @param[in] lMessageQueueFlags Message queue flags to consider. + * @param[in] pxAbsoluteTimeout The absolute timespec to convert. + * @param[out] pxTimeoutTicks Output parameter of the timeout in ticks. + * + * @return 0 if successful; EINVAL if pxAbsoluteTimeout is invalid, or ETIMEDOUT + * if pxAbsoluteTimeout is in the past. + */ +static int prvCalculateTickTimeout( long lMessageQueueFlags, + const struct timespec * const pxAbsoluteTimeout, + TickType_t * pxTimeoutTicks ); + +/** + * @brief Add a new queue to the queue list. + * + * @param[out] ppxMessageQueue Pointer to new queue. + * @param[in] pxAttr mq_attr of the new queue. + * @param[in] pcName Name of new queue. + * @param[in] xNameLength Length of pcName. + * + * @return pdTRUE if the queue is found; pdFALSE otherwise. + */ +static BaseType_t prvCreateNewMessageQueue( QueueListElement_t ** ppxMessageQueue, + const struct mq_attr * const pxAttr, + const char * const pcName, + size_t xNameLength ); + +/** + * @brief Free all the resources used by a message queue. + * + * @param[out] pxMessageQueue Pointer to queue to free. + * + * @return nothing + */ +static void prvDeleteMessageQueue( const QueueListElement_t * const pxMessageQueue ); + +/** + * @brief Attempt to find the queue identified by pcName or xMqId in the queue list. + * + * Matches queues by pcName first; if pcName is NULL, matches by xMqId. + * @param[out] ppxQueueListElement Output parameter set when queue is found. + * @param[in] pcName A queue name to match. + * @param[in] xMessageQueueDescriptor A queue descriptor to match. + * + * @return pdTRUE if the queue is found; pdFALSE otherwise. + */ +static BaseType_t prvFindQueueInList( QueueListElement_t ** const ppxQueueListElement, + const char * const pcName, + mqd_t xMessageQueueDescriptor ); + +/** + * @brief Initialize the queue list. + * + * Performs initialization of the queue list mutex and queue list head. + * + * @return nothing + */ +static void prvInitializeQueueList( void ); + +/** + * @brief Checks that pcName is a valid name for a message queue. + * + * Also outputs the length of pcName. + * @param[in] pcName The name to check. + * @param[out] pxNameLength Output parameter for name length. + * + * @return pdTRUE if the name is valid; pdFALSE otherwise. + */ +static BaseType_t prvValidateQueueName( const char * const pcName, + size_t * pxNameLength ); + +/** + * @brief Guards access to the list of message queues. + */ +static StaticSemaphore_t xQueueListMutex = { { 0 }, .u = { 0 } }; + +/** + * @brief Head of the linked list of queues. + */ +static Link_t xQueueListHead = { 0 }; + +/*-----------------------------------------------------------*/ + +static int prvCalculateTickTimeout( long lMessageQueueFlags, + const struct timespec * const pxAbsoluteTimeout, + TickType_t * pxTimeoutTicks ) +{ + int iStatus = 0; + + /* Check for nonblocking queue. */ + if( lMessageQueueFlags & O_NONBLOCK ) + { + /* No additional checks are done for nonblocking queues. Timeout is 0. */ + *pxTimeoutTicks = 0; + } + else + { + /* No absolute timeout given. Block forever. */ + if( pxAbsoluteTimeout == NULL ) + { + *pxTimeoutTicks = portMAX_DELAY; + } + else + { + struct timespec xCurrentTime = { 0 }; + + /* Check that the given timespec is valid. */ + if( UTILS_ValidateTimespec( pxAbsoluteTimeout ) == false ) + { + iStatus = EINVAL; + } + + /* Get current time */ + if( ( iStatus == 0 ) && ( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) ) + { + iStatus = EINVAL; + } + + /* Convert absolute timespec to ticks. */ + if( ( iStatus == 0 ) && + ( UTILS_AbsoluteTimespecToDeltaTicks( pxAbsoluteTimeout, &xCurrentTime, pxTimeoutTicks ) != 0 ) ) + { + iStatus = ETIMEDOUT; + } + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +static BaseType_t prvCreateNewMessageQueue( QueueListElement_t ** ppxMessageQueue, + const struct mq_attr * const pxAttr, + const char * const pcName, + size_t xNameLength ) +{ + BaseType_t xStatus = pdTRUE; + + /* Allocate space for a new queue element. */ + *ppxMessageQueue = pvPortMalloc( sizeof( QueueListElement_t ) ); + + /* Check that memory allocation succeeded. */ + if( *ppxMessageQueue == NULL ) + { + xStatus = pdFALSE; + } + + /* Create the FreeRTOS queue. */ + if( xStatus == pdTRUE ) + { + ( *ppxMessageQueue )->xQueue = + xQueueCreate( pxAttr->mq_maxmsg, sizeof( QueueElement_t ) ); + + /* Check that queue creation succeeded. */ + if( ( *ppxMessageQueue )->xQueue == NULL ) + { + vPortFree( *ppxMessageQueue ); + xStatus = pdFALSE; + } + } + + if( xStatus == pdTRUE ) + { + /* Allocate space for the queue name plus null-terminator. */ + ( *ppxMessageQueue )->pcName = pvPortMalloc( xNameLength + 1 ); + + /* Check that memory was successfully allocated for queue name. */ + if( ( *ppxMessageQueue )->pcName == NULL ) + { + vQueueDelete( ( *ppxMessageQueue )->xQueue ); + vPortFree( *ppxMessageQueue ); + xStatus = pdFALSE; + } + else + { + /* Copy queue name. Copying xNameLength+1 will cause strncpy to add + * the null-terminator. */ + ( void ) strncpy( ( *ppxMessageQueue )->pcName, pcName, xNameLength + 1 ); + } + } + + if( xStatus == pdTRUE ) + { + /* Copy attributes. */ + ( *ppxMessageQueue )->xAttr = *pxAttr; + + /* A newly-created queue will have 1 open descriptor for it. */ + ( *ppxMessageQueue )->xOpenDescriptors = 1; + + /* A newly-created queue will not be pending unlink. */ + ( *ppxMessageQueue )->xPendingUnlink = pdFALSE; + + /* Add the new queue to the list. */ + listADD( &xQueueListHead, &( *ppxMessageQueue )->xLink ); + } + + return xStatus; +} + +/*-----------------------------------------------------------*/ + +static void prvDeleteMessageQueue( const QueueListElement_t * const pxMessageQueue ) +{ + QueueElement_t xQueueElement = { 0 }; + + /* Free all data in the queue. It's assumed that no more data will be added + * to the queue, so xQueueReceive does not block. */ + while( xQueueReceive( pxMessageQueue->xQueue, + ( void * ) &xQueueElement, + 0 ) == pdTRUE ) + { + vPortFree( xQueueElement.pcData ); + } + + /* Free memory used by this message queue. */ + vQueueDelete( pxMessageQueue->xQueue ); + vPortFree( ( void * ) pxMessageQueue->pcName ); + vPortFree( ( void * ) pxMessageQueue ); +} + +/*-----------------------------------------------------------*/ + +static BaseType_t prvFindQueueInList( QueueListElement_t ** const ppxQueueListElement, + const char * const pcName, + mqd_t xMessageQueueDescriptor ) +{ + Link_t * pxQueueListLink = NULL; + QueueListElement_t * pxMessageQueue = NULL; + BaseType_t xQueueFound = pdFALSE; + + /* Iterate through the list of queues. */ + listFOR_EACH( pxQueueListLink, &xQueueListHead ) + { + pxMessageQueue = listCONTAINER( pxQueueListLink, QueueListElement_t, xLink ); + + /* Match by name first if provided. */ + if( ( pcName != NULL ) && ( strcmp( pxMessageQueue->pcName, pcName ) == 0 ) ) + { + xQueueFound = pdTRUE; + break; + } + /* If name doesn't match, match by descriptor. */ + else + { + if( ( mqd_t ) pxMessageQueue == xMessageQueueDescriptor ) + { + xQueueFound = pdTRUE; + break; + } + } + } + + /* If the queue was found, set the output parameter. */ + if( ( xQueueFound == pdTRUE ) && ( ppxQueueListElement != NULL ) ) + { + *ppxQueueListElement = pxMessageQueue; + } + + return xQueueFound; +} + +/*-----------------------------------------------------------*/ + +static void prvInitializeQueueList( void ) +{ + /* Keep track of whether the queue list has been initialized. */ + static BaseType_t xQueueListInitialized = pdFALSE; + + /* Check if queue list needs to be initialized. */ + if( xQueueListInitialized == pdFALSE ) + { + /* Initialization must be in a critical section to prevent two threads + * from initializing at the same time. */ + taskENTER_CRITICAL(); + + /* Check again that queue list is still uninitialized, i.e. it wasn't + * initialized while this function was waiting to enter the critical + * section. */ + if( xQueueListInitialized == pdFALSE ) + { + /* Initialize the queue list mutex and list head. */ + ( void ) xSemaphoreCreateMutexStatic( &xQueueListMutex ); + listINIT_HEAD( &xQueueListHead ); + xQueueListInitialized = pdTRUE; + } + + /* Exit the critical section. */ + taskEXIT_CRITICAL(); + } +} + +/*-----------------------------------------------------------*/ + +static BaseType_t prvValidateQueueName( const char * const pcName, + size_t * pxNameLength ) +{ + BaseType_t xStatus = pdTRUE; + size_t xNameLength = 0; + + /* All message queue names must start with '/'. */ + if( pcName[ 0 ] != '/' ) + { + xStatus = pdFALSE; + } + else + { + /* Get the length of pcName, excluding the first '/' and null-terminator. */ + xNameLength = UTILS_strnlen( pcName, NAME_MAX + 2 ); + + if( xNameLength == NAME_MAX + 2 ) + { + /* Name too long. */ + xStatus = pdFALSE; + } + else + { + /* Name length passes, set output parameter. */ + *pxNameLength = xNameLength; + } + } + + return xStatus; +} + +/*-----------------------------------------------------------*/ + +int mq_close( mqd_t mqdes ) +{ + int iStatus = 0; + QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes; + BaseType_t xQueueRemoved = pdFALSE; + + /* Initialize the queue list, if needed. */ + prvInitializeQueueList(); + + /* Lock the mutex that guards access to the queue list. This call will + * never fail because it blocks forever. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY ); + + /* Attempt to find the message queue based on the given descriptor. */ + if( prvFindQueueInList( NULL, NULL, mqdes ) == pdTRUE ) + { + /* Decrement the number of open descriptors. */ + if( pxMessageQueue->xOpenDescriptors > 0 ) + { + pxMessageQueue->xOpenDescriptors--; + } + + /* Check if the queue has any more open descriptors. */ + if( pxMessageQueue->xOpenDescriptors == 0 ) + { + /* If no open descriptors remain and mq_unlink has already been called, + * remove the queue. */ + if( pxMessageQueue->xPendingUnlink == pdTRUE ) + { + listREMOVE( &pxMessageQueue->xLink ); + + /* Set the flag to delete the queue. Deleting the queue is deferred + * until xQueueListMutex is released. */ + xQueueRemoved = pdTRUE; + } + /* Otherwise, wait for the call to mq_unlink. */ + else + { + pxMessageQueue->xPendingUnlink = pdTRUE; + } + } + } + else + { + /* Queue not found; bad descriptor. */ + errno = EBADF; + iStatus = -1; + } + + /* Release the mutex protecting the queue list. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex ); + + /* Delete all resources used by the queue if needed. */ + if( xQueueRemoved == pdTRUE ) + { + prvDeleteMessageQueue( pxMessageQueue ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int mq_getattr( mqd_t mqdes, + struct mq_attr * mqstat ) +{ + int iStatus = 0; + QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes; + + /* Lock the mutex that guards access to the queue list. This call will + * never fail because it blocks forever. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY ); + + /* Find the mq referenced by mqdes. */ + if( prvFindQueueInList( NULL, NULL, mqdes ) == pdTRUE ) + { + /* Update the number of messages in the queue and copy the attributes + * into mqstat. */ + pxMessageQueue->xAttr.mq_curmsgs = ( long ) uxQueueMessagesWaiting( pxMessageQueue->xQueue ); + *mqstat = pxMessageQueue->xAttr; + } + else + { + /* Queue not found; bad descriptor. */ + errno = EBADF; + iStatus = -1; + } + + /* Release the mutex protecting the queue list. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex ); + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +mqd_t mq_open( const char * name, + int oflag, + mode_t mode, + struct mq_attr * attr ) +{ + mqd_t xMessageQueue = NULL; + size_t xNameLength = 0; + + /* Default mq_attr. */ + struct mq_attr xQueueCreationAttr = + { + .mq_flags = 0, + .mq_maxmsg = posixconfigMQ_MAX_MESSAGES, + .mq_msgsize = posixconfigMQ_MAX_SIZE, + .mq_curmsgs = 0 + }; + + /* Silence warnings about unused parameters. */ + ( void ) mode; + + /* Initialize the queue list, if needed. */ + prvInitializeQueueList(); + + /* Check queue name. */ + if( prvValidateQueueName( name, &xNameLength ) == pdFALSE ) + { + /* Invalid name. */ + errno = EINVAL; + xMessageQueue = ( mqd_t ) -1; + } + + /* Check attributes, if given. */ + if( xMessageQueue == NULL ) + { + if( ( oflag & O_CREAT ) && ( attr != NULL ) && ( ( attr->mq_maxmsg <= 0 ) || ( attr->mq_msgsize <= 0 ) ) ) + { + /* Invalid mq_attr.mq_maxmsg or mq_attr.mq_msgsize. */ + errno = EINVAL; + xMessageQueue = ( mqd_t ) -1; + } + } + + if( xMessageQueue == NULL ) + { + /* Lock the mutex that guards access to the queue list. This call will + * never fail because it blocks forever. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY ); + + /* Search the queue list to check if the queue exists. */ + if( prvFindQueueInList( ( QueueListElement_t ** ) &xMessageQueue, + name, + ( mqd_t ) NULL ) == pdTRUE ) + { + /* If the mq exists, check that this function wasn't called with + * O_CREAT and O_EXCL. */ + if( ( oflag & O_EXCL ) && ( oflag & O_CREAT ) ) + { + errno = EEXIST; + xMessageQueue = ( mqd_t ) -1; + } + else + { + /* Check if the mq has been unlinked and is pending removal. */ + if( ( ( QueueListElement_t * ) xMessageQueue )->xPendingUnlink == pdTRUE ) + { + /* Queue pending deletion. Don't allow it to be re-opened. */ + errno = EINVAL; + xMessageQueue = ( mqd_t ) -1; + } + else + { + /* Increase count of open file descriptors for queue. */ + ( ( QueueListElement_t * ) xMessageQueue )->xOpenDescriptors++; + } + } + } + /* Queue does not exist. */ + else + { + /* Only create the new queue if O_CREAT was specified. */ + if( oflag & O_CREAT ) + { + /* Copy attributes if provided. */ + if( attr != NULL ) + { + xQueueCreationAttr = *attr; + } + + /* Copy oflags. */ + xQueueCreationAttr.mq_flags = ( long ) oflag; + + /* Create the new message queue. */ + if( prvCreateNewMessageQueue( ( QueueListElement_t ** ) &xMessageQueue, + &xQueueCreationAttr, + name, + xNameLength ) == pdFALSE ) + { + errno = ENOSPC; + xMessageQueue = ( mqd_t ) -1; + } + } + else + { + errno = ENOENT; + xMessageQueue = ( mqd_t ) -1; + } + } + + /* Release the mutex protecting the queue list. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex ); + } + + return xMessageQueue; +} + +/*-----------------------------------------------------------*/ + +ssize_t mq_receive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned int * msg_prio ) +{ + return mq_timedreceive( mqdes, msg_ptr, msg_len, msg_prio, NULL ); +} + +/*-----------------------------------------------------------*/ + +int mq_send( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio ) +{ + return mq_timedsend( mqdes, msg_ptr, msg_len, msg_prio, NULL ); +} + +/*-----------------------------------------------------------*/ + +ssize_t mq_timedreceive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned * msg_prio, + const struct timespec * abstime ) +{ + ssize_t xStatus = 0; + int iCalculateTimeoutReturn = 0; + TickType_t xTimeoutTicks = 0; + QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes; + QueueElement_t xReceiveData = { 0 }; + + /* Silence warnings about unused parameters. */ + ( void ) msg_prio; + + /* Lock the mutex that guards access to the queue list. This call will + * never fail because it blocks forever. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY ); + + /* Find the mq referenced by mqdes. */ + if( prvFindQueueInList( NULL, NULL, mqdes ) == pdFALSE ) + { + /* Queue not found; bad descriptor. */ + errno = EBADF; + xStatus = -1; + } + + /* Verify that msg_len is large enough. */ + if( xStatus == 0 ) + { + if( msg_len < ( size_t ) pxMessageQueue->xAttr.mq_msgsize ) + { + /* msg_len too small. */ + errno = EMSGSIZE; + xStatus = -1; + } + } + + if( xStatus == 0 ) + { + /* Convert abstime to a tick timeout. */ + iCalculateTimeoutReturn = prvCalculateTickTimeout( pxMessageQueue->xAttr.mq_flags, + abstime, + &xTimeoutTicks ); + + if( iCalculateTimeoutReturn != 0 ) + { + errno = iCalculateTimeoutReturn; + xStatus = -1; + } + } + + /* Release the mutex protecting the queue list. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex ); + + if( xStatus == 0 ) + { + /* Receive data from the FreeRTOS queue. */ + if( xQueueReceive( pxMessageQueue->xQueue, + &xReceiveData, + xTimeoutTicks ) == pdFALSE ) + { + /* If queue receive fails, set the appropriate errno. */ + if( pxMessageQueue->xAttr.mq_flags & O_NONBLOCK ) + { + /* Set errno to EAGAIN for nonblocking mq. */ + errno = EAGAIN; + } + else + { + /* Otherwise, set errno to ETIMEDOUT. */ + errno = ETIMEDOUT; + } + + xStatus = -1; + } + } + + if( xStatus == 0 ) + { + /* Get the length of data for return value. */ + xStatus = ( ssize_t ) xReceiveData.xDataSize; + + /* Copy received data into given buffer, then free it. */ + ( void ) memcpy( msg_ptr, xReceiveData.pcData, xReceiveData.xDataSize ); + vPortFree( xReceiveData.pcData ); + } + + return xStatus; +} + +/*-----------------------------------------------------------*/ + +int mq_timedsend( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned int msg_prio, + const struct timespec * abstime ) +{ + int iStatus = 0, iCalculateTimeoutReturn = 0; + TickType_t xTimeoutTicks = 0; + QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes; + QueueElement_t xSendData = { 0 }; + + /* Silence warnings about unused parameters. */ + ( void ) msg_prio; + + /* Lock the mutex that guards access to the queue list. This call will + * never fail because it blocks forever. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY ); + + /* Find the mq referenced by mqdes. */ + if( prvFindQueueInList( NULL, NULL, mqdes ) == pdFALSE ) + { + /* Queue not found; bad descriptor. */ + errno = EBADF; + iStatus = -1; + } + + /* Verify that mq_msgsize is large enough. */ + if( iStatus == 0 ) + { + if( msg_len > ( size_t ) pxMessageQueue->xAttr.mq_msgsize ) + { + /* msg_len too large. */ + errno = EMSGSIZE; + iStatus = -1; + } + } + + if( iStatus == 0 ) + { + /* Convert abstime to a tick timeout. */ + iCalculateTimeoutReturn = prvCalculateTickTimeout( pxMessageQueue->xAttr.mq_flags, + abstime, + &xTimeoutTicks ); + + if( iCalculateTimeoutReturn != 0 ) + { + errno = iCalculateTimeoutReturn; + iStatus = -1; + } + } + + /* Release the mutex protecting the queue list. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex ); + + /* Allocate memory for the message. */ + if( iStatus == 0 ) + { + xSendData.xDataSize = msg_len; + xSendData.pcData = pvPortMalloc( msg_len ); + + /* Check that memory allocation succeeded. */ + if( xSendData.pcData == NULL ) + { + /* msg_len too large. */ + errno = EMSGSIZE; + iStatus = -1; + } + else + { + /* Copy the data to send. */ + ( void ) memcpy( xSendData.pcData, msg_ptr, msg_len ); + } + } + + if( iStatus == 0 ) + { + /* Send data to the FreeRTOS queue. */ + if( xQueueSend( pxMessageQueue->xQueue, + &xSendData, + xTimeoutTicks ) == pdFALSE ) + { + /* If queue send fails, set the appropriate errno. */ + if( pxMessageQueue->xAttr.mq_flags & O_NONBLOCK ) + { + /* Set errno to EAGAIN for nonblocking mq. */ + errno = EAGAIN; + } + else + { + /* Otherwise, set errno to ETIMEDOUT. */ + errno = ETIMEDOUT; + } + + /* Free the allocated queue data. */ + vPortFree( xSendData.pcData ); + + iStatus = -1; + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int mq_unlink( const char * name ) +{ + int iStatus = 0; + size_t xNameSize = 0; + BaseType_t xQueueRemoved = pdFALSE; + QueueListElement_t * pxMessageQueue = NULL; + + /* Initialize the queue list, if needed. */ + prvInitializeQueueList(); + + /* Check queue name. */ + if( prvValidateQueueName( name, &xNameSize ) == pdFALSE ) + { + /* Error with mq name. */ + errno = EINVAL; + iStatus = -1; + } + + if( iStatus == 0 ) + { + /* Lock the mutex that guards access to the queue list. This call will + * never fail because it blocks forever. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY ); + + /* Check if the named queue exists. */ + if( prvFindQueueInList( &pxMessageQueue, name, ( mqd_t ) NULL ) == pdTRUE ) + { + /* If the queue exists and there are no open descriptors to it, + * remove it from the list. */ + if( pxMessageQueue->xOpenDescriptors == 0 ) + { + listREMOVE( &pxMessageQueue->xLink ); + + /* Set the flag to delete the queue. Deleting the queue is deferred + * until xQueueListMutex is released. */ + xQueueRemoved = pdTRUE; + } + else + { + /* If the queue has open descriptors, set the pending unlink flag + * so that mq_close will free its resources. */ + pxMessageQueue->xPendingUnlink = pdTRUE; + } + } + else + { + /* The named message queue doesn't exist. */ + errno = ENOENT; + iStatus = -1; + } + + /* Release the mutex protecting the queue list. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex ); + } + + /* Delete all resources used by the queue if needed. */ + if( xQueueRemoved == pdTRUE ) + { + prvDeleteMessageQueue( pxMessageQueue ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c new file mode 100755 index 0000000..f34e9ec --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c @@ -0,0 +1,510 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_pthread.c + * @brief Implementation of thread functions in pthread.h + */ + +/* C standard library includes. */ +#include <stddef.h> +#include <string.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/pthread.h" + +/** + * @brief Thread attribute object. + */ +typedef struct pthread_attr_internal +{ + uint16_t usStackSize; /**< Stack size. */ + uint16_t usSchedPriorityDetachState; /**< Schedule priority 15 bits (LSB) Detach state: 1 bits (MSB) */ +} pthread_attr_internal_t; + +#define pthreadDETACH_STATE_MASK 0x8000 +#define pthreadSCHED_PRIORITY_MASK 0x7FFF +#define pthreadDETACH_STATE_SHIFT 15 +#define pthreadGET_SCHED_PRIORITY( var ) ( ( var ) & ( pthreadSCHED_PRIORITY_MASK ) ) +#define pthreadIS_JOINABLE( var ) ( ( ( var ) & ( pthreadDETACH_STATE_MASK ) ) == pthreadDETACH_STATE_MASK ) + +/** + * @brief Thread object. + */ +typedef struct pthread_internal +{ + pthread_attr_internal_t xAttr; /**< Thread attributes. */ + void * ( *pvStartRoutine )( void * ); /**< Application thread function. */ + void * xTaskArg; /**< Arguments for application thread function. */ + TaskHandle_t xTaskHandle; /**< FreeRTOS task handle. */ + StaticSemaphore_t xJoinBarrier; /**< Synchronizes the two callers of pthread_join. */ + StaticSemaphore_t xJoinMutex; /**< Ensures that only one other thread may join this thread. */ + void * xReturn; /**< Return value of pvStartRoutine. */ +} pthread_internal_t; + +/** + * @brief Terminates the calling thread. + * + * For joinable threads, this function waits for pthread_join. Otherwise, + * it deletes the thread and frees up resources used by the thread. + * + * @return This function does not return. + */ +static void prvExitThread( void ); + +/** + * @brief Wrapper function for the user's thread routine. + * + * This function is executed as a FreeRTOS task function. + * @param[in] pxArg A pointer to a pthread_internal_t. + * + * @return nothing + */ +static void prvRunThread( void * pxArg ); + +/** + * @brief Default pthread_attr_t. + */ +static const pthread_attr_internal_t xDefaultThreadAttributes = +{ + .usStackSize = PTHREAD_STACK_MIN, + .usSchedPriorityDetachState = ( ( uint16_t ) tskIDLE_PRIORITY & pthreadSCHED_PRIORITY_MASK ) | ( PTHREAD_CREATE_JOINABLE << pthreadDETACH_STATE_SHIFT ), +}; + +/*-----------------------------------------------------------*/ + +static void prvExitThread( void ) +{ + pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread_self(); + + /* If this thread is joinable, wait for a call to pthread_join. */ + if( pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) ) + { + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier ); + + /* Suspend until the call to pthread_join. The caller of pthread_join + * will perform cleanup. */ + vTaskSuspend( NULL ); + } + else + { + /* For a detached thread, perform cleanup of thread object. */ + vPortFree( pxThread ); + vTaskDelete( NULL ); + } +} + +/*-----------------------------------------------------------*/ + +static void prvRunThread( void * pxArg ) +{ + pthread_internal_t * pxThread = ( pthread_internal_t * ) pxArg; + + /* Run the thread routine. */ + pxThread->xReturn = pxThread->pvStartRoutine( ( void * ) pxThread->xTaskArg ); + + /* Exit once finished. This function does not return. */ + prvExitThread(); +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_destroy( pthread_attr_t * attr ) +{ + ( void ) attr; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_getdetachstate( const pthread_attr_t * attr, + int * detachstate ) +{ + pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr ); + + if( pthreadIS_JOINABLE( pxAttr->usSchedPriorityDetachState ) ) + { + *detachstate = PTHREAD_CREATE_JOINABLE; + } + else + { + *detachstate = PTHREAD_CREATE_DETACHED; + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_getschedparam( const pthread_attr_t * attr, + struct sched_param * param ) +{ + pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr ); + + param->sched_priority = ( int ) ( pthreadGET_SCHED_PRIORITY( pxAttr->usSchedPriorityDetachState ) ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_getstacksize( const pthread_attr_t * attr, + size_t * stacksize ) +{ + pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr ); + + *stacksize = ( size_t ) pxAttr->usStackSize; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_init( pthread_attr_t * attr ) +{ + /* Copy the default values into the new thread attributes object. */ + *( ( pthread_attr_internal_t * ) ( attr ) ) = xDefaultThreadAttributes; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_setdetachstate( pthread_attr_t * attr, + int detachstate ) +{ + int iStatus = 0; + pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr ); + + if( ( detachstate != PTHREAD_CREATE_DETACHED ) && ( detachstate != PTHREAD_CREATE_JOINABLE ) ) + { + iStatus = EINVAL; + } + else + { + /* clear and then set msb bit to detachstate) */ + pxAttr->usSchedPriorityDetachState &= ~pthreadDETACH_STATE_MASK; + pxAttr->usSchedPriorityDetachState |= ( ( uint16_t ) detachstate << pthreadDETACH_STATE_SHIFT ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_setschedparam( pthread_attr_t * attr, + const struct sched_param * param ) +{ + int iStatus = 0; + pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr ); + + /* Check for NULL param. */ + if( param == NULL ) + { + iStatus = EINVAL; + } + + /* Ensure that param.sched_priority is valid. */ + if( ( iStatus == 0 ) && + ( ( param->sched_priority > sched_get_priority_max( SCHED_OTHER ) ) || + ( param->sched_priority < 0 ) ) ) + { + iStatus = ENOTSUP; + } + + /* Set the sched_param. */ + if( iStatus == 0 ) + { + /* clear and then set 15 LSB to schedule priority) */ + pxAttr->usSchedPriorityDetachState &= ~pthreadSCHED_PRIORITY_MASK; + pxAttr->usSchedPriorityDetachState |= ( ( uint16_t ) param->sched_priority ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_setschedpolicy( pthread_attr_t * attr, + int policy ) +{ + /* Silence warnings about unused parameters. */ + ( void ) attr; + ( void ) policy; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_attr_setstacksize( pthread_attr_t * attr, + size_t stacksize ) +{ + int iStatus = 0; + pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr ); + + if( stacksize < PTHREAD_STACK_MIN ) + { + iStatus = EINVAL; + } + else + { + pxAttr->usStackSize = ( uint16_t ) stacksize; + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_create( pthread_t * thread, + const pthread_attr_t * attr, + void *( *startroutine )( void * ), + void * arg ) +{ + int iStatus = 0; + pthread_internal_t * pxThread = NULL; + struct sched_param xSchedParam = { .sched_priority = tskIDLE_PRIORITY }; + + /* Allocate memory for new thread object. */ + pxThread = ( pthread_internal_t * ) pvPortMalloc( sizeof( pthread_internal_t ) ); + + if( pxThread == NULL ) + { + /* No memory. */ + iStatus = EAGAIN; + } + + if( iStatus == 0 ) + { + /* No attributes given, use default attributes. */ + if( attr == NULL ) + { + pxThread->xAttr = xDefaultThreadAttributes; + } + /* Otherwise, use provided attributes. */ + else + { + pxThread->xAttr = *( ( pthread_attr_internal_t * ) ( attr ) ); + } + + /* Get priority from attributes */ + xSchedParam.sched_priority = ( int ) pthreadGET_SCHED_PRIORITY( pxThread->xAttr.usSchedPriorityDetachState ); + + /* Set argument and start routine. */ + pxThread->xTaskArg = arg; + pxThread->pvStartRoutine = startroutine; + + /* If this thread is joinable, create the synchronization mechanisms for + * pthread_join. */ + + if( pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) ) + { + /* These calls will not fail when their arguments aren't NULL. */ + ( void ) xSemaphoreCreateMutexStatic( &pxThread->xJoinMutex ); + ( void ) xSemaphoreCreateBinaryStatic( &pxThread->xJoinBarrier ); + } + } + + if( iStatus == 0 ) + { + /* Suspend all tasks to create a critical section. This ensures that + * the new thread doesn't exit before a tag is assigned. */ + vTaskSuspendAll(); + + /* Create the FreeRTOS task that will run the pthread. */ + if( xTaskCreate( prvRunThread, + posixconfigPTHREAD_TASK_NAME, + ( uint16_t ) ( pxThread->xAttr.usStackSize / sizeof( StackType_t ) ), + ( void * ) pxThread, + xSchedParam.sched_priority, + &pxThread->xTaskHandle ) != pdPASS ) + { + /* Task creation failed, no memory. */ + vPortFree( pxThread ); + iStatus = EAGAIN; + } + else + { + /* Store the pointer to the thread object in the task tag. */ + vTaskSetApplicationTaskTag( pxThread->xTaskHandle, ( TaskHookFunction_t ) pxThread ); + + /* Set the thread object for the user. */ + *thread = ( pthread_t ) pxThread; + } + + /* End the critical section. */ + xTaskResumeAll(); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_getschedparam( pthread_t thread, + int * policy, + struct sched_param * param ) +{ + int iStatus = 0; + pthread_internal_t * pxThread = ( pthread_internal_t * ) thread; + + *policy = SCHED_OTHER; + param->sched_priority = ( int ) pthreadGET_SCHED_PRIORITY( pxThread->xAttr.usSchedPriorityDetachState ); + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_equal( pthread_t t1, + pthread_t t2 ) +{ + return t1 == t2; +} + +/*-----------------------------------------------------------*/ + +void pthread_exit( void * value_ptr ) +{ + pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread_self(); + + /* Set the return value. */ + pxThread->xReturn = value_ptr; + + /* Exit this thread. */ + prvExitThread(); +} + +/*-----------------------------------------------------------*/ + +int pthread_join( pthread_t pthread, + void ** retval ) +{ + int iStatus = 0; + pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread; + + /* Make sure pthread is joinable. Otherwise, this function would block + * forever waiting for an unjoinable thread. */ + if( !pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) ) + { + iStatus = EDEADLK; + } + + /* Only one thread may attempt to join another. Lock the join mutex + * to prevent other threads from calling pthread_join on the same thread. */ + if( iStatus == 0 ) + { + if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxThread->xJoinMutex, 0 ) != pdPASS ) + { + /* Another thread has already joined the requested thread, which would + * cause this thread to wait forever. */ + iStatus = EDEADLK; + } + } + + /* Attempting to join the calling thread would cause a deadlock. */ + if( iStatus == 0 ) + { + if( pthread_equal( pthread_self(), pthread ) != 0 ) + { + iStatus = EDEADLK; + } + } + + if( iStatus == 0 ) + { + /* Wait for the joining thread to finish. Because this call waits forever, + * it should never fail. */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier, portMAX_DELAY ); + + /* Create a critical section to clean up the joined thread. */ + vTaskSuspendAll(); + + /* Release xJoinBarrier and delete it. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier ); + vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier ); + + /* Release xJoinMutex and delete it. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinMutex ); + vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinMutex ); + + /* Delete the FreeRTOS task that ran the thread. */ + vTaskDelete( pxThread->xTaskHandle ); + + /* Set the return value. */ + if( retval != NULL ) + { + *retval = pxThread->xReturn; + } + + /* Free the thread object. */ + vPortFree( pxThread ); + + /* End the critical section. */ + xTaskResumeAll(); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +pthread_t pthread_self( void ) +{ + /* Return a reference to this pthread object, which is stored in the + * FreeRTOS task tag. */ + return ( pthread_t ) xTaskGetApplicationTaskTag( NULL ); +} + +/*-----------------------------------------------------------*/ + +int pthread_setschedparam( pthread_t thread, + int policy, + const struct sched_param * param ) +{ + int iStatus = 0; + + pthread_internal_t * pxThread = ( pthread_internal_t * ) thread; + + /* Silence compiler warnings about unused parameters. */ + ( void ) policy; + + /* Copy the given sched_param. */ + iStatus = pthread_attr_setschedparam( ( pthread_attr_t * ) &pxThread->xAttr, param ); + + if( iStatus == 0 ) + { + /* Change the priority of the FreeRTOS task. */ + vTaskPrioritySet( pxThread->xTaskHandle, param->sched_priority ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_barrier.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_barrier.c new file mode 100755 index 0000000..2b621cb --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_barrier.c @@ -0,0 +1,167 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_pthread_barrier.c + * @brief Implementation of barrier functions in pthread.h + */ + +/* C standard library includes. */ +#include <stddef.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/pthread.h" + +#include "atomic.h" + +/* + * @brief barrier max count + * + * Barriers are implemented on FreeRTOS event groups, of which 8 bits are usable + * when configUSE_16_BIT_TICKS is 1. Otherwise, 24 bits are usable. + */ +/**@{ */ +#if ( configUSE_16_BIT_TICKS == 1 ) + #define posixPTHREAD_BARRIER_MAX_COUNT ( 8 ) +#else + #define posixPTHREAD_BARRIER_MAX_COUNT ( 24 ) +#endif +/**@} */ + +/*-----------------------------------------------------------*/ + +int pthread_barrier_destroy( pthread_barrier_t * barrier ) +{ + pthread_barrier_internal_t * pxBarrier = ( pthread_barrier_internal_t * ) ( barrier ); + + /* Free all resources used by the barrier. */ + ( void ) vEventGroupDelete( ( EventGroupHandle_t ) &pxBarrier->xBarrierEventGroup ); + ( void ) vSemaphoreDelete( ( SemaphoreHandle_t ) &pxBarrier->xThreadCountSemaphore ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_barrier_init( pthread_barrier_t * barrier, + const pthread_barrierattr_t * attr, + unsigned count ) +{ + int iStatus = 0; + pthread_barrier_internal_t * pxNewBarrier = ( pthread_barrier_internal_t * ) ( barrier ); + + /* Silence warnings about unused parameters. */ + ( void ) attr; + + /* Ensure count is greater than 0. */ + if( count == 0 ) + { + iStatus = EINVAL; + } + + /* Ensure that count will fit in a FreeRTOS event group. */ + if( iStatus == 0 ) + { + if( count > posixPTHREAD_BARRIER_MAX_COUNT ) + { + /* No memory exists in the event group for more than + * posixPTHREAD_BARRIER_MAX_COUNT threads. */ + iStatus = ENOMEM; + } + } + + if( iStatus == 0 ) + { + /* Set the current thread count and threshold. */ + pxNewBarrier->uThreadCount = 0; + pxNewBarrier->uThreshold = count; + + /* Create the FreeRTOS event group. This call will not fail when its + * argument isn't NULL. */ + ( void ) xEventGroupCreateStatic( &pxNewBarrier->xBarrierEventGroup ); + + /* Create the semaphore that prevents more than count threads from being + * unblocked by a single successful pthread_barrier_wait. This semaphore + * counts down from count and cannot decrement below 0. */ + ( void ) xSemaphoreCreateCountingStatic( ( UBaseType_t ) count, /* Max count. */ + ( UBaseType_t ) count, /* Initial count. */ + &pxNewBarrier->xThreadCountSemaphore ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_barrier_wait( pthread_barrier_t * barrier ) +{ + int iStatus = 0; + unsigned i = 0; /* Loop iterator. */ + pthread_barrier_internal_t * pxBarrier = ( pthread_barrier_internal_t * ) ( barrier ); + unsigned uThreadNumber = 0; + + /* Decrement the number of threads waiting on this barrier. This will prevent more + * than pxBarrier->uThreshold threads from being unblocked by a single successful + * pthread_barrier_wait call. + * + * This call will never fail because it blocks forever. + */ + ( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &pxBarrier->xThreadCountSemaphore, portMAX_DELAY ); + + uThreadNumber = Atomic_Increment_u32( ( uint32_t * ) &pxBarrier->uThreadCount ); + + /* Set the bit in the event group representing this thread, then wait for the other + * threads to set their bit. This call should wait forever until all threads have set + * their bit, so the return value is ignored. */ + ( void ) xEventGroupSync( ( EventGroupHandle_t ) &pxBarrier->xBarrierEventGroup, + 1 << uThreadNumber, /* Which bit in the event group to set. */ + ( 1 << pxBarrier->uThreshold ) - 1, /* Wait for all threads to set their bits. */ + portMAX_DELAY ); + + /* The first thread to enter the barrier gets PTHREAD_BARRIER_SERIAL_THREAD as its + * return value and resets xThreadCountSemaphore. */ + + if( uThreadNumber == 0 ) + { + iStatus = PTHREAD_BARRIER_SERIAL_THREAD; + + /* uThreadCount can be safely changed without locking xThreadCountMutex + * because xThreadCountSemaphore is currently 0. */ + pxBarrier->uThreadCount = 0; + + /* Reset xThreadCountSemaphore. This allows more threads to enter the + * barrier, starting a new cycle. */ + for( i = 0; i < pxBarrier->uThreshold; i++ ) + { + xSemaphoreGive( ( SemaphoreHandle_t ) &pxBarrier->xThreadCountSemaphore ); + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_cond.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_cond.c new file mode 100755 index 0000000..b972634 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_cond.c @@ -0,0 +1,296 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_pthread_cond.c + * @brief Implementation of condition variable functions in pthread.h + */ + +/* C standard library includes. */ +#include <limits.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/pthread.h" +#include "FreeRTOS_POSIX/utils.h" + +#include "atomic.h" + +/** + * @brief Initialize a PTHREAD_COND_INITIALIZER cond. + * + * PTHREAD_COND_INITIALIZER sets a flag for a cond to be initialized later. + * This function performs the initialization. + * @param[in] pxCond The cond to initialize. + * + * @return nothing + */ +static void prvInitializeStaticCond( pthread_cond_internal_t * pxCond ); + +/*-----------------------------------------------------------*/ + +static void prvInitializeStaticCond( pthread_cond_internal_t * pxCond ) +{ + /* Check if the condition variable needs to be initialized. */ + if( pxCond->xIsInitialized == pdFALSE ) + { + /* Cond initialization must be in a critical section to prevent two threads + * from initializing it at the same time. */ + taskENTER_CRITICAL(); + + /* Check again that the cond is still uninitialized, i.e. it wasn't + * initialized while this function was waiting to enter the critical + * section. */ + if( pxCond->xIsInitialized == pdFALSE ) + { + /* Set the members of the cond. The semaphore create calls will never fail + * when their arguments aren't NULL. */ + pxCond->xIsInitialized = pdTRUE; + ( void ) xSemaphoreCreateCountingStatic( INT_MAX, 0U, &pxCond->xCondWaitSemaphore ); + pxCond->iWaitingThreads = 0; + } + + /* Exit the critical section. */ + taskEXIT_CRITICAL(); + } +} + +/** + * @brief Check "atomically" if iLocalWaitingThreads == pxCond->iWaitingThreads and decrement. + */ +static void prvTestAndDecrement( pthread_cond_t * pxCond, + unsigned iLocalWaitingThreads ) +{ + /* Test local copy of threads waiting is larger than zero. */ + while( iLocalWaitingThreads > 0 ) + { + /* Test-and-set. Atomically check whether the copy in memory has changed. + * And, if not decrease the copy of threads waiting in memory. */ + if( ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32( ( uint32_t * ) &pxCond->iWaitingThreads, ( uint32_t ) iLocalWaitingThreads - 1, ( uint32_t ) iLocalWaitingThreads ) ) + { + /* Signal one succeeded. Break. */ + break; + } + + /* Local copy may be out dated. Reload, and retry. */ + iLocalWaitingThreads = pxCond->iWaitingThreads; + } +} + +/*-----------------------------------------------------------*/ + +int pthread_cond_broadcast( pthread_cond_t * cond ) +{ + unsigned i = 0; + pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond ); + + /* If the cond is uninitialized, perform initialization. */ + prvInitializeStaticCond( pxCond ); + + /* Local copy of number of threads waiting. */ + unsigned iLocalWaitingThreads = pxCond->iWaitingThreads; + + /* Test local copy of threads waiting is larger than zero. */ + while( iLocalWaitingThreads > 0 ) + { + /* Test-and-set. Atomically check whether the copy in memory has changed. + * And, if not set the copy of threads waiting in memory to zero. */ + if( ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32( ( uint32_t * ) &pxCond->iWaitingThreads, 0, ( uint32_t ) iLocalWaitingThreads ) ) + { + /* Unblock all. */ + for( i = 0; i < iLocalWaitingThreads; i++ ) + { + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore ); + } + + break; + } + + /* Local copy is out dated. Reload, and retry. */ + iLocalWaitingThreads = pxCond->iWaitingThreads; + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_cond_destroy( pthread_cond_t * cond ) +{ + pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond ); + + /* Free all resources in use by the cond. */ + vSemaphoreDelete( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_cond_init( pthread_cond_t * cond, + const pthread_condattr_t * attr ) +{ + int iStatus = 0; + pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) cond; + + /* Silence warnings about unused parameters. */ + ( void ) attr; + + if( pxCond == NULL ) + { + iStatus = ENOMEM; + } + + if( iStatus == 0 ) + { + /* Set the members of the cond. The semaphore create calls will never fail + * when their arguments aren't NULL. */ + pxCond->xIsInitialized = pdTRUE; + + ( void ) xSemaphoreCreateCountingStatic( INT_MAX, 0U, &pxCond->xCondWaitSemaphore ); + pxCond->iWaitingThreads = 0; + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_cond_signal( pthread_cond_t * cond ) +{ + pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond ); + + /* If the cond is uninitialized, perform initialization. */ + prvInitializeStaticCond( pxCond ); + + /* Local copy of number of threads waiting. */ + unsigned iLocalWaitingThreads = pxCond->iWaitingThreads; + + /* Test local copy of threads waiting is larger than zero. */ + while( iLocalWaitingThreads > 0 ) + { + /* Test-and-set. Atomically check whether the copy in memory has changed. + * And, if not decrease the copy of threads waiting in memory. */ + if( ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32( ( uint32_t * ) &pxCond->iWaitingThreads, ( uint32_t ) iLocalWaitingThreads - 1, ( uint32_t ) iLocalWaitingThreads ) ) + { + /* Unblock one. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore ); + + /* Signal one succeeded. Break. */ + break; + } + + /* Local copy may be out dated. Reload, and retry. */ + iLocalWaitingThreads = pxCond->iWaitingThreads; + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_cond_timedwait( pthread_cond_t * cond, + pthread_mutex_t * mutex, + const struct timespec * abstime ) +{ + unsigned iLocalWaitingThreads; + int iStatus = 0; + pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond ); + TickType_t xDelay = portMAX_DELAY; + + /* If the cond is uninitialized, perform initialization. */ + prvInitializeStaticCond( pxCond ); + + /* Convert abstime to a delay in TickType_t if provided. */ + if( abstime != NULL ) + { + struct timespec xCurrentTime = { 0 }; + + /* Get current time */ + if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) + { + iStatus = EINVAL; + } + else + { + iStatus = UTILS_AbsoluteTimespecToDeltaTicks( abstime, &xCurrentTime, &xDelay ); + } + } + + /* Increase the counter of threads blocking on condition variable, then + * unlock mutex. */ + if( iStatus == 0 ) + { + /* Atomically increments thread waiting by 1, and + * stores number of threads waiting before increment. */ + iLocalWaitingThreads = Atomic_Increment_u32( ( uint32_t * ) &pxCond->iWaitingThreads ); + + iStatus = pthread_mutex_unlock( mutex ); + } + + /* Wait on the condition variable. */ + if( iStatus == 0 ) + { + if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore, + xDelay ) == pdPASS ) + { + /* When successful, relock mutex. */ + iStatus = pthread_mutex_lock( mutex ); + } + else + { + /* Timeout. Relock mutex and decrement number of waiting threads. */ + iStatus = ETIMEDOUT; + ( void ) pthread_mutex_lock( mutex ); + + /* Atomically decrements thread waiting by 1. + * If iLocalWaitingThreads is updated by other thread(s) in between, + * this implementation guarantees to decrement by 1 based on the + * value currently in pxCond->iWaitingThreads. */ + prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 ); + } + } + else + { + /* Atomically decrements thread waiting by 1. + * If iLocalWaitingThreads is updated by other thread(s) in between, + * this implementation guarantees to decrement by 1 based on the + * value currently in pxCond->iWaitingThreads. */ + prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_cond_wait( pthread_cond_t * cond, + pthread_mutex_t * mutex ) +{ + return pthread_cond_timedwait( cond, mutex, NULL ); +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_mutex.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_mutex.c new file mode 100755 index 0000000..0ea3aeb --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread_mutex.c @@ -0,0 +1,373 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_pthread_mutex.c + * @brief Implementation of mutex functions in pthread.h + */ + +/* C standard library includes. */ +#include <stddef.h> +#include <string.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/pthread.h" +#include "FreeRTOS_POSIX/utils.h" + +/** + * @brief Initialize a PTHREAD_MUTEX_INITIALIZER mutex. + * + * PTHREAD_MUTEX_INITIALIZER sets a flag for a mutex to be initialized later. + * This function performs the initialization. + * @param[in] pxMutex The mutex to initialize. + * + * @return nothing + */ +static void prvInitializeStaticMutex( pthread_mutex_internal_t * pxMutex ); + +/** + * @brief Default pthread_mutexattr_t. + */ +static const pthread_mutexattr_internal_t xDefaultMutexAttributes = +{ + .iType = PTHREAD_MUTEX_DEFAULT, +}; + +/*-----------------------------------------------------------*/ + +static void prvInitializeStaticMutex( pthread_mutex_internal_t * pxMutex ) +{ + /* Check if the mutex needs to be initialized. */ + if( pxMutex->xIsInitialized == pdFALSE ) + { + /* Mutex initialization must be in a critical section to prevent two threads + * from initializing it at the same time. */ + taskENTER_CRITICAL(); + + /* Check again that the mutex is still uninitialized, i.e. it wasn't + * initialized while this function was waiting to enter the critical + * section. */ + if( pxMutex->xIsInitialized == pdFALSE ) + { + /* Set the mutex as the default type. */ + pxMutex->xAttr.iType = PTHREAD_MUTEX_DEFAULT; + + /* Call the correct FreeRTOS mutex initialization function based on + * the mutex type. */ + #if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_RECURSIVE + ( void ) xSemaphoreCreateRecursiveMutexStatic( &pxMutex->xMutex ); + #else + ( void ) xSemaphoreCreateMutexStatic( &pxMutex->xMutex ); + #endif + + pxMutex->xIsInitialized = pdTRUE; + } + + /* Exit the critical section. */ + taskEXIT_CRITICAL(); + } +} + +/*-----------------------------------------------------------*/ + +int pthread_mutex_destroy( pthread_mutex_t * mutex ) +{ + pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) ( mutex ); + + /* Free resources in use by the mutex. */ + if( pxMutex->xTaskOwner == NULL ) + { + vSemaphoreDelete( ( SemaphoreHandle_t ) &pxMutex->xMutex ); + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutex_init( pthread_mutex_t * mutex, + const pthread_mutexattr_t * attr ) +{ + int iStatus = 0; + pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) mutex; + + if( pxMutex == NULL ) + { + /* No memory. */ + iStatus = ENOMEM; + } + + if( iStatus == 0 ) + { + *pxMutex = FREERTOS_POSIX_MUTEX_INITIALIZER; + + /* No attributes given, use default attributes. */ + if( attr == NULL ) + { + pxMutex->xAttr = xDefaultMutexAttributes; + } + /* Otherwise, use provided attributes. */ + else + { + pxMutex->xAttr = *( ( pthread_mutexattr_internal_t * ) ( attr ) ); + } + + /* Call the correct FreeRTOS mutex creation function based on mutex type. */ + if( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE ) + { + /* Recursive mutex. */ + ( void ) xSemaphoreCreateRecursiveMutexStatic( &pxMutex->xMutex ); + } + else + { + /* All other mutex types. */ + ( void ) xSemaphoreCreateMutexStatic( &pxMutex->xMutex ); + } + + /* Ensure that the FreeRTOS mutex was successfully created. */ + if( ( SemaphoreHandle_t ) &pxMutex->xMutex == NULL ) + { + /* Failed to create mutex. Set error EAGAIN and free mutex object. */ + iStatus = EAGAIN; + vPortFree( pxMutex ); + } + else + { + /* Mutex successfully created. */ + pxMutex->xIsInitialized = pdTRUE; + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutex_lock( pthread_mutex_t * mutex ) +{ + return pthread_mutex_timedlock( mutex, NULL ); +} + +/*-----------------------------------------------------------*/ + +int pthread_mutex_timedlock( pthread_mutex_t * mutex, + const struct timespec * abstime ) +{ + int iStatus = 0; + pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) ( mutex ); + TickType_t xDelay = portMAX_DELAY; + BaseType_t xFreeRTOSMutexTakeStatus = pdFALSE; + + /* If mutex in uninitialized, perform initialization. */ + prvInitializeStaticMutex( pxMutex ); + + /* At this point, the mutex should be initialized. */ + configASSERT( pxMutex->xIsInitialized == pdTRUE ); + + /* Convert abstime to a delay in TickType_t if provided. */ + if( abstime != NULL ) + { + struct timespec xCurrentTime = { 0 }; + + /* Get current time */ + if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) + { + iStatus = EINVAL; + } + else + { + iStatus = UTILS_AbsoluteTimespecToDeltaTicks( abstime, &xCurrentTime, &xDelay ); + } + + /* If abstime was in the past, still attempt to lock the mutex without + * blocking, per POSIX spec. */ + if( iStatus == ETIMEDOUT ) + { + xDelay = 0; + iStatus = 0; + } + } + + /* Check if trying to lock a currently owned mutex. */ + if( ( iStatus == 0 ) && + ( pxMutex->xAttr.iType == PTHREAD_MUTEX_ERRORCHECK ) && /* Only PTHREAD_MUTEX_ERRORCHECK type detects deadlock. */ + ( pxMutex->xTaskOwner == xTaskGetCurrentTaskHandle() ) ) /* Check if locking a currently owned mutex. */ + { + iStatus = EDEADLK; + } + + if( iStatus == 0 ) + { + /* Call the correct FreeRTOS mutex take function based on mutex type. */ + if( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE ) + { + xFreeRTOSMutexTakeStatus = xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) &pxMutex->xMutex, xDelay ); + } + else + { + xFreeRTOSMutexTakeStatus = xSemaphoreTake( ( SemaphoreHandle_t ) &pxMutex->xMutex, xDelay ); + } + + /* If the mutex was successfully taken, set its owner. */ + if( xFreeRTOSMutexTakeStatus == pdPASS ) + { + pxMutex->xTaskOwner = xTaskGetCurrentTaskHandle(); + } + /* Otherwise, the mutex take timed out. */ + else + { + iStatus = ETIMEDOUT; + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutex_trylock( pthread_mutex_t * mutex ) +{ + int iStatus = 0; + struct timespec xTimeout = + { + .tv_sec = 0, + .tv_nsec = 0 + }; + + /* Attempt to lock with no timeout. */ + iStatus = pthread_mutex_timedlock( mutex, &xTimeout ); + + /* POSIX specifies that this function should return EBUSY instead of + * ETIMEDOUT for attempting to lock a locked mutex. */ + if( iStatus == ETIMEDOUT ) + { + iStatus = EBUSY; + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutex_unlock( pthread_mutex_t * mutex ) +{ + int iStatus = 0; + pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) ( mutex ); + + /* If mutex in uninitialized, perform initialization. */ + prvInitializeStaticMutex( pxMutex ); + + /* Check if trying to unlock an unowned mutex. */ + if( ( ( pxMutex->xAttr.iType == PTHREAD_MUTEX_ERRORCHECK ) || + ( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE ) ) && + ( pxMutex->xTaskOwner != xTaskGetCurrentTaskHandle() ) ) + { + iStatus = EPERM; + } + + if( iStatus == 0 ) + { + /* Suspend the scheduler so that + * mutex is unlocked AND owner is updated atomically */ + vTaskSuspendAll(); + + /* Call the correct FreeRTOS mutex unlock function based on mutex type. */ + if( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE ) + { + ( void ) xSemaphoreGiveRecursive( ( SemaphoreHandle_t ) &pxMutex->xMutex ); + } + else + { + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxMutex->xMutex ); + } + + /* Update the owner of the mutex. A recursive mutex may still have an + * owner, so it should be updated with xSemaphoreGetMutexHolder. */ + pxMutex->xTaskOwner = xSemaphoreGetMutexHolder( ( SemaphoreHandle_t ) &pxMutex->xMutex ); + + /* Resume the scheduler */ + ( void ) xTaskResumeAll(); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutexattr_destroy( pthread_mutexattr_t * attr ) +{ + ( void ) attr; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutexattr_gettype( const pthread_mutexattr_t * attr, + int * type ) +{ + pthread_mutexattr_internal_t * pxAttr = ( pthread_mutexattr_internal_t * ) ( attr ); + + *type = pxAttr->iType; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutexattr_init( pthread_mutexattr_t * attr ) +{ + *( ( pthread_mutexattr_internal_t * ) ( attr ) ) = xDefaultMutexAttributes; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int pthread_mutexattr_settype( pthread_mutexattr_t * attr, + int type ) +{ + int iStatus = 0; + pthread_mutexattr_internal_t * pxAttr = ( pthread_mutexattr_internal_t * ) ( attr ); + + switch( type ) + { + case PTHREAD_MUTEX_NORMAL: + case PTHREAD_MUTEX_RECURSIVE: + case PTHREAD_MUTEX_ERRORCHECK: + pxAttr->iType = type; + break; + + default: + iStatus = EINVAL; + break; + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_sched.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_sched.c new file mode 100755 index 0000000..7916a99 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_sched.c @@ -0,0 +1,64 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_sched.c + * @brief Implementation of scheduler functions in sched.h + */ + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/sched.h" + +/*-----------------------------------------------------------*/ + +int sched_get_priority_max( int policy ) +{ + /* Silence warnings about unused parameters. */ + ( void ) policy; + + return configMAX_PRIORITIES - 1; +} + +/*-----------------------------------------------------------*/ + +int sched_get_priority_min( int policy ) +{ + /* Silence warnings about unused parameters. */ + ( void ) policy; + + return tskIDLE_PRIORITY; +} + +/*-----------------------------------------------------------*/ + +int sched_yield( void ) +{ + taskYIELD(); + + return 0; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_semaphore.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_semaphore.c new file mode 100755 index 0000000..6a83150 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_semaphore.c @@ -0,0 +1,232 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_semaphore.c + * @brief Implementation of functions in semaphore.h + */ + +/* C standard library includes. */ +#include <stddef.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/semaphore.h" +#include "FreeRTOS_POSIX/utils.h" + +#include "atomic.h" + + +/*-----------------------------------------------------------*/ + +int sem_destroy( sem_t * sem ) +{ + sem_internal_t * pxSem = ( sem_internal_t * ) ( sem ); + + /* Free the resources in use by the semaphore. */ + vSemaphoreDelete( ( SemaphoreHandle_t ) &pxSem->xSemaphore ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int sem_getvalue( sem_t * sem, + int * sval ) +{ + sem_internal_t * pxSem = ( sem_internal_t * ) ( sem ); + + /* Get value does not need atomic operation, since -- Open Group + * states "the updated value represents an actual semaphore value that + * occurred at some unspecified time during the call, but it need not be the + * actual value of the semaphore when it is returned to the calling process." + */ + *sval = pxSem->value; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int sem_init( sem_t * sem, + int pshared, + unsigned value ) +{ + int iStatus = 0; + sem_internal_t * pxSem = ( sem_internal_t * ) ( sem ); + + /* Silence warnings about unused parameters. */ + ( void ) pshared; + + /* Check value parameter. */ + if( value > SEM_VALUE_MAX ) + { + errno = EINVAL; + iStatus = -1; + } + + /* value is guaranteed to not exceed INT32_MAX, which is the default value of SEM_VALUE_MAX (0x7FFFU). */ + pxSem->value = ( int ) value; + + /* Create the FreeRTOS semaphore. + * This is only used to queue threads when no semaphore is available. + * Initializing with semaphore initial count zero. + * This call will not fail because the memory for the semaphore has already been allocated. + */ + if( iStatus == 0 ) + { + ( void ) xSemaphoreCreateCountingStatic( SEM_VALUE_MAX, 0, &pxSem->xSemaphore ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int sem_post( sem_t * sem ) +{ + sem_internal_t * pxSem = ( sem_internal_t * ) ( sem ); + + int iPreviouValue = Atomic_Increment_u32( ( uint32_t * ) &pxSem->value ); + + /* If previous semaphore value is equal or larger than zero, there is no + * thread waiting for this semaphore. Otherwise (<0), call FreeRTOS interface + * to wake up a thread. */ + if( iPreviouValue < 0 ) + { + /* Give the semaphore using the FreeRTOS API. */ + ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxSem->xSemaphore ); + } + + return 0; +} + +/*-----------------------------------------------------------*/ + +int sem_timedwait( sem_t * sem, + const struct timespec * abstime ) +{ + int iStatus = 0; + sem_internal_t * pxSem = ( sem_internal_t * ) ( sem ); + TickType_t xDelay = portMAX_DELAY; + int iPreviousValue = Atomic_Decrement_u32( ( uint32_t * ) &pxSem->value ); + + if( abstime != NULL ) + { + /* If the provided timespec is invalid, still attempt to take the + * semaphore without blocking, per POSIX spec. */ + if( UTILS_ValidateTimespec( abstime ) == false ) + { + xDelay = 0; + iStatus = EINVAL; + } + else + { + struct timespec xCurrentTime = { 0 }; + + /* Get current time */ + if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) + { + iStatus = EINVAL; + } + else + { + iStatus = UTILS_AbsoluteTimespecToDeltaTicks( abstime, &xCurrentTime, &xDelay ); + } + + /* If abstime was in the past, still attempt to take the semaphore without + * blocking, per POSIX spec. */ + if( iStatus == ETIMEDOUT ) + { + xDelay = 0; + } + } + } + + /* If previous semaphore value is larger than zero, the thread entering this function call + * can take the semaphore without yielding. Else (<=0), calling into FreeRTOS API to yield. + */ + if( iPreviousValue > 0 ) + { + /* Under no circumstance shall the function fail with a timeout if the semaphore can be locked immediately. */ + iStatus = 0; + } + else + { + /* Take the semaphore using the FreeRTOS API. */ + if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxSem->xSemaphore, + xDelay ) != pdTRUE ) + { + if( iStatus == 0 ) + { + errno = ETIMEDOUT; + } + else + { + errno = iStatus; + } + + iStatus = -1; + } + else + { + iStatus = 0; + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int sem_trywait( sem_t * sem ) +{ + int iStatus = 0; + + /* Setting an absolute timeout of 0 (i.e. in the past) will cause sem_timedwait + * to not block. */ + struct timespec xTimeout = { 0 }; + + iStatus = sem_timedwait( sem, &xTimeout ); + + /* POSIX specifies that this function should set errno to EAGAIN and not + * ETIMEDOUT. */ + if( ( iStatus == -1 ) && ( errno == ETIMEDOUT ) ) + { + errno = EAGAIN; + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int sem_wait( sem_t * sem ) +{ + return sem_timedwait( sem, NULL ); +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_timer.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_timer.c new file mode 100755 index 0000000..f790e83 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_timer.c @@ -0,0 +1,330 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_timer.c + * @brief Implementation of timer functions in time.h + */ + +/* C standard library includes. */ +#include <stddef.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/pthread.h" +#include "FreeRTOS_POSIX/signal.h" +#include "FreeRTOS_POSIX/time.h" +#include "FreeRTOS_POSIX/utils.h" + +/* FreeRTOS timer include. */ +#include "timers.h" + +/* Timespec zero check macros. */ +#define TIMESPEC_IS_ZERO( xTimespec ) ( xTimespec.tv_sec == 0 && xTimespec.tv_nsec == 0 ) /**< Check for 0. */ +#define TIMESPEC_IS_NOT_ZERO( xTimespec ) ( !( TIMESPEC_IS_ZERO( xTimespec ) ) ) /**< Check for not 0. */ + +/** + * @brief Internal timer structure. + */ +typedef struct timer_internal +{ + StaticTimer_t xTimerBuffer; /**< Memory that holds the FreeRTOS timer. */ + struct sigevent xTimerEvent; /**< What to do when this timer expires. */ + TickType_t xTimerPeriod; /**< Period of this timer. */ +} timer_internal_t; + +/*-----------------------------------------------------------*/ + +void prvTimerCallback( TimerHandle_t xTimerHandle ) +{ + timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle ); + pthread_t xTimerNotificationThread; + + /* The value of the timer ID, set in timer_create, should not be NULL. */ + configASSERT( pxTimer != NULL ); + + /* A value of SIGEV_SIGNAL isn't supported and should not have been successfully + * set. */ + configASSERT( pxTimer->xTimerEvent.sigev_notify != SIGEV_SIGNAL ); + + /* Update the timer period, which may need to be set to an it_interval + * argument. This call should not block. */ + if( pxTimer->xTimerPeriod > 0 ) + { + xTimerChangePeriod( xTimerHandle, pxTimer->xTimerPeriod, 0 ); + } + + /* Create the timer notification thread if requested. */ + if( pxTimer->xTimerEvent.sigev_notify == SIGEV_THREAD ) + { + /* if the user has provided thread attributes, create a thread + * with the provided attributes. Otherwise dispatch callback directly */ + if( pxTimer->xTimerEvent.sigev_notify_attributes == NULL ) + { + ( *pxTimer->xTimerEvent.sigev_notify_function )( pxTimer->xTimerEvent.sigev_value ); + } + else + { + ( void ) pthread_create( &xTimerNotificationThread, + pxTimer->xTimerEvent.sigev_notify_attributes, + ( void * ( * )( void * ) )pxTimer->xTimerEvent.sigev_notify_function, + pxTimer->xTimerEvent.sigev_value.sival_ptr ); + } + } +} + +/*-----------------------------------------------------------*/ + +int timer_create( clockid_t clockid, + struct sigevent * evp, + timer_t * timerid ) +{ + int iStatus = 0; + timer_internal_t * pxTimer = NULL; + + /* Silence warnings about unused parameters. */ + ( void ) clockid; + + /* POSIX specifies that when evp is NULL, the behavior shall be as is + * sigev_notify is SIGEV_SIGNAL. SIGEV_SIGNAL is currently not supported. */ + if( ( evp == NULL ) || ( evp->sigev_notify == SIGEV_SIGNAL ) ) + { + errno = ENOTSUP; + iStatus = -1; + } + + /* Allocate memory for a new timer object. */ + if( iStatus == 0 ) + { + pxTimer = pvPortMalloc( sizeof( timer_internal_t ) ); + + if( pxTimer == NULL ) + { + errno = EAGAIN; + iStatus = -1; + } + } + + if( iStatus == 0 ) + { + /* Copy the event notification structure and set the current timer period. */ + pxTimer->xTimerEvent = *evp; + pxTimer->xTimerPeriod = 0; + + /* Create a new FreeRTOS timer. This call will not fail because the + * memory for it has already been allocated, so the output parameter is + * also set. */ + *timerid = ( timer_t ) xTimerCreateStatic( posixconfigTIMER_NAME, /* Timer name. */ + portMAX_DELAY, /* Initial timer period. Timers are created disarmed. */ + pdFALSE, /* Don't auto-reload timer. */ + ( void * ) pxTimer, /* Timer id. */ + prvTimerCallback, /* Timer expiration callback. */ + &pxTimer->xTimerBuffer ); /* Pre-allocated memory for timer. */ + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int timer_delete( timer_t timerid ) +{ + TimerHandle_t xTimerHandle = timerid; + timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle ); + + /* The value of the timer ID, set in timer_create, should not be NULL. */ + configASSERT( pxTimer != NULL ); + + /* Stop the FreeRTOS timer. Because the timer is statically allocated, no call + * to xTimerDelete is necessary. The timer is stopped so that it's not referenced + * anywhere. xTimerStop will not fail when it has unlimited block time. */ + ( void ) xTimerStop( xTimerHandle, portMAX_DELAY ); + + /* Wait until the timer stop command is processed. */ + while( xTimerIsTimerActive( xTimerHandle ) == pdTRUE ) + { + vTaskDelay( 1 ); + } + + /* Free the memory in use by the timer. */ + vPortFree( pxTimer ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int timer_getoverrun( timer_t timerid ) +{ + /* Silence warnings about unused parameters. */ + ( void ) timerid; + + return 0; +} + +/*-----------------------------------------------------------*/ + +int timer_settime( timer_t timerid, + int flags, + const struct itimerspec * value, + struct itimerspec * ovalue ) +{ + int iStatus = 0; + TimerHandle_t xTimerHandle = timerid; + timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle ); + TickType_t xNextTimerExpiration = 0, xTimerExpirationPeriod = 0; + + /* Validate the value argument, but only if the timer isn't being disarmed. */ + if( TIMESPEC_IS_NOT_ZERO( value->it_value ) ) + { + if( ( UTILS_ValidateTimespec( &value->it_interval ) == false ) || + ( UTILS_ValidateTimespec( &value->it_value ) == false ) ) + { + errno = EINVAL; + iStatus = -1; + } + } + + /* Set ovalue, if given. */ + if( ovalue != NULL ) + { + ( void ) timer_gettime( timerid, ovalue ); + } + + /* Stop the timer if it's currently active. */ + if( ( iStatus == 0 ) && xTimerIsTimerActive( xTimerHandle ) ) + { + ( void ) xTimerStop( xTimerHandle, portMAX_DELAY ); + } + + /* Only restart the timer if it_value is not zero. */ + if( ( iStatus == 0 ) && TIMESPEC_IS_NOT_ZERO( value->it_value ) ) + { + /* Convert it_interval to ticks, but only if it_interval is not 0. If + * it_interval is 0, then the timer is not periodic. */ + if( TIMESPEC_IS_NOT_ZERO( value->it_interval ) ) + { + ( void ) UTILS_TimespecToTicks( &value->it_interval, &xTimerExpirationPeriod ); + } + + /* Set the new timer period. A non-periodic timer will have its period set + * to portMAX_DELAY. */ + pxTimer->xTimerPeriod = xTimerExpirationPeriod; + + /* Convert it_value to ticks, but only if it_value is not 0. If it_value + * is 0, then the timer will remain disarmed. */ + if( TIMESPEC_IS_NOT_ZERO( value->it_value ) ) + { + /* Absolute timeout. */ + if( ( flags & TIMER_ABSTIME ) == TIMER_ABSTIME ) + { + struct timespec xCurrentTime = { 0 }; + + /* Get current time */ + if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) + { + iStatus = EINVAL; + } + else + { + iStatus = UTILS_AbsoluteTimespecToDeltaTicks( &value->it_value, &xCurrentTime, &xNextTimerExpiration ); + } + + /* Make sure xNextTimerExpiration is zero in case we got negative time difference */ + if( iStatus != 0 ) + { + xNextTimerExpiration = 0; + + if( iStatus == ETIMEDOUT ) + { + /* Set Status to 0 as absolute time is past is treated as expiry but not an error */ + iStatus = 0; + } + } + } + /* Relative timeout. */ + else + { + ( void ) UTILS_TimespecToTicks( &value->it_value, &xNextTimerExpiration ); + } + } + + /* If xNextTimerExpiration is still 0, that means that it_value specified + * an absolute timeout in the past. Per POSIX spec, a notification should be + * triggered immediately. */ + if( xNextTimerExpiration == 0 ) + { + prvTimerCallback( xTimerHandle ); + } + else + { + /* Set the timer to expire at the it_value, then start it. */ + ( void ) xTimerChangePeriod( xTimerHandle, xNextTimerExpiration, portMAX_DELAY ); + ( void ) xTimerStart( xTimerHandle, xNextTimerExpiration ); + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int timer_gettime( timer_t timerid, + struct itimerspec * value ) +{ + TimerHandle_t xTimerHandle = timerid; + timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle ); + TickType_t xNextExpirationTime = xTimerGetExpiryTime( xTimerHandle ) - xTaskGetTickCount(), + xTimerExpirationPeriod = pxTimer->xTimerPeriod; + + /* Set it_value only if the timer is armed. Otherwise, set it to 0. */ + if( xTimerIsTimerActive( xTimerHandle ) != pdFALSE ) + { + value->it_value.tv_sec = ( time_t ) ( xNextExpirationTime / configTICK_RATE_HZ ); + value->it_value.tv_nsec = ( long ) ( ( xNextExpirationTime % configTICK_RATE_HZ ) * NANOSECONDS_PER_TICK ); + } + else + { + value->it_value.tv_sec = 0; + value->it_value.tv_nsec = 0; + } + + /* Set it_interval only if the timer is periodic. Otherwise, set it to 0. */ + if( xTimerExpirationPeriod != portMAX_DELAY ) + { + value->it_interval.tv_sec = ( time_t ) ( xTimerExpirationPeriod / configTICK_RATE_HZ ); + value->it_interval.tv_nsec = ( long ) ( ( xTimerExpirationPeriod % configTICK_RATE_HZ ) * NANOSECONDS_PER_TICK ); + } + else + { + value->it_interval.tv_sec = 0; + value->it_interval.tv_nsec = 0; + } + + return 0; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_unistd.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_unistd.c new file mode 100755 index 0000000..0d3d914 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_unistd.c @@ -0,0 +1,54 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_unistd.c + * @brief Implementation of functions in unistd.h + */ + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/unistd.h" + +/*-----------------------------------------------------------*/ + +unsigned sleep( unsigned seconds ) +{ + vTaskDelay( pdMS_TO_TICKS( seconds * 1000 ) ); + + return 0; +} + +/*-----------------------------------------------------------*/ + +int usleep( useconds_t usec ) +{ + /* To avoid delaying for less than usec, always round up. */ + vTaskDelay( pdMS_TO_TICKS( usec / 1000 + ( usec % 1000 != 0 ) ) ); + + return 0; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_utils.c b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_utils.c new file mode 100755 index 0000000..c121587 --- /dev/null +++ b/lib/freertos-posix/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_utils.c @@ -0,0 +1,388 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file FreeRTOS_POSIX_utils.c + * @brief Implementation of utility functions in utils.h + */ + +/* C standard library includes. */ +#include <stddef.h> +#include <limits.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX.h" +#include "FreeRTOS_POSIX/errno.h" +#include "FreeRTOS_POSIX/utils.h" + +/*-----------------------------------------------------------*/ + +size_t UTILS_strnlen( const char * const pcString, + size_t xMaxLength ) +{ + const char * pcCharPointer = pcString; + size_t xLength = 0; + + if( pcString != NULL ) + { + while( ( *pcCharPointer != '\0' ) && ( xLength < xMaxLength ) ) + { + xLength++; + pcCharPointer++; + } + } + + return xLength; +} + +/*-----------------------------------------------------------*/ + +int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime, + const struct timespec * const pxCurrentTime, + TickType_t * const pxResult ) +{ + int iStatus = 0; + struct timespec xDifference = { 0 }; + + /* Check parameters. */ + if( ( pxAbsoluteTime == NULL ) || ( pxCurrentTime == NULL ) || ( pxResult == NULL ) ) + { + iStatus = EINVAL; + } + + /* Calculate the difference between the current time and absolute time. */ + if( iStatus == 0 ) + { + iStatus = UTILS_TimespecSubtract( pxAbsoluteTime, pxCurrentTime, &xDifference ); + + if( iStatus == 1 ) + { + /* pxAbsoluteTime was in the past. */ + iStatus = ETIMEDOUT; + } + else if( iStatus == -1 ) + { + /* error */ + iStatus = EINVAL; + } + } + + /* Convert the time difference to ticks. */ + if( iStatus == 0 ) + { + iStatus = UTILS_TimespecToTicks( &xDifference, pxResult ); + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, + TickType_t * const pxResult ) +{ + int iStatus = 0; + int64_t llTotalTicks = 0; + long lNanoseconds = 0; + + /* Check parameters. */ + if( ( pxTimespec == NULL ) || ( pxResult == NULL ) ) + { + iStatus = EINVAL; + } + else if( ( iStatus == 0 ) && ( UTILS_ValidateTimespec( pxTimespec ) == false ) ) + { + iStatus = EINVAL; + } + + if( iStatus == 0 ) + { + /* Convert timespec.tv_sec to ticks. */ + llTotalTicks = ( int64_t ) configTICK_RATE_HZ * ( pxTimespec->tv_sec ); + + /* Convert timespec.tv_nsec to ticks. This value does not have to be checked + * for overflow because a valid timespec has 0 <= tv_nsec < 1000000000 and + * NANOSECONDS_PER_TICK > 1. */ + lNanoseconds = pxTimespec->tv_nsec / ( long ) NANOSECONDS_PER_TICK + /* Whole nanoseconds. */ + ( long ) ( pxTimespec->tv_nsec % ( long ) NANOSECONDS_PER_TICK != 0 ); /* Add 1 to round up if needed. */ + + /* Add the nanoseconds to the total ticks. */ + llTotalTicks += ( int64_t ) lNanoseconds; + + /* Check for overflow */ + if( llTotalTicks < 0 ) + { + iStatus = EINVAL; + } + else + { + /* check if TickType_t is 32 bit or 64 bit */ + uint32_t ulTickTypeSize = sizeof( TickType_t ); + + /* check for downcast overflow */ + if( ulTickTypeSize == sizeof( uint32_t ) ) + { + if( llTotalTicks > UINT_MAX ) + { + iStatus = EINVAL; + } + } + } + + /* Write result. */ + *pxResult = ( TickType_t ) llTotalTicks; + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +void UTILS_NanosecondsToTimespec( int64_t llSource, + struct timespec * const pxDestination ) +{ + long lCarrySec = 0; + + /* Convert to timespec. */ + pxDestination->tv_sec = ( time_t ) ( llSource / NANOSECONDS_PER_SECOND ); + pxDestination->tv_nsec = ( long ) ( llSource % NANOSECONDS_PER_SECOND ); + + /* Subtract from tv_sec if tv_nsec < 0. */ + if( pxDestination->tv_nsec < 0L ) + { + /* Compute the number of seconds to carry. */ + lCarrySec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L; + + pxDestination->tv_sec -= ( time_t ) ( lCarrySec ); + pxDestination->tv_nsec += lCarrySec * ( long ) NANOSECONDS_PER_SECOND; + } +} + +/*-----------------------------------------------------------*/ + +int UTILS_TimespecAdd( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ) +{ + int64_t llPartialSec = 0; + int iStatus = 0; + + /* Check parameters. */ + if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) ) + { + iStatus = -1; + } + + if( iStatus == 0 ) + { + /* Perform addition. */ + pxResult->tv_nsec = x->tv_nsec + y->tv_nsec; + + /* check for overflow in case nsec value was invalid */ + if( pxResult->tv_nsec < 0 ) + { + iStatus = 1; + } + else + { + llPartialSec = ( pxResult->tv_nsec ) / NANOSECONDS_PER_SECOND; + pxResult->tv_nsec = ( pxResult->tv_nsec ) % NANOSECONDS_PER_SECOND; + pxResult->tv_sec = x->tv_sec + y->tv_sec + llPartialSec; + + /* check for overflow */ + if( pxResult->tv_sec < 0 ) + { + iStatus = 1; + } + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int UTILS_TimespecAddNanoseconds( const struct timespec * const x, + int64_t llNanoseconds, + struct timespec * const pxResult ) +{ + int64_t llTotalNSec = 0; + int iStatus = 0; + + /* Check parameters. */ + if( ( pxResult == NULL ) || ( x == NULL ) ) + { + iStatus = -1; + } + + if( iStatus == 0 ) + { + /* add nano seconds */ + llTotalNSec = x->tv_nsec + llNanoseconds; + + /* check for nano seconds overflow */ + if( llTotalNSec < 0 ) + { + iStatus = 1; + } + else + { + pxResult->tv_nsec = llTotalNSec % NANOSECONDS_PER_SECOND; + pxResult->tv_sec = x->tv_sec + ( llTotalNSec / NANOSECONDS_PER_SECOND ); + + /* check for seconds overflow */ + if( pxResult->tv_sec < 0 ) + { + iStatus = 1; + } + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int UTILS_TimespecSubtract( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ) +{ + int iCompareResult = 0; + int iStatus = 0; + + /* Check parameters. */ + if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) ) + { + iStatus = -1; + } + + if( iStatus == 0 ) + { + iCompareResult = UTILS_TimespecCompare( x, y ); + + /* if x < y then result would be negative, return 1 */ + if( iCompareResult == -1 ) + { + iStatus = 1; + } + else if( iCompareResult == 0 ) + { + /* if times are the same return zero */ + pxResult->tv_sec = 0; + pxResult->tv_nsec = 0; + } + else + { + /* If x > y Perform subtraction. */ + pxResult->tv_sec = x->tv_sec - y->tv_sec; + pxResult->tv_nsec = x->tv_nsec - y->tv_nsec; + + /* check if nano seconds value needs to borrow */ + if( pxResult->tv_nsec < 0 ) + { + /* Based on comparison, tv_sec > 0 */ + pxResult->tv_sec--; + pxResult->tv_nsec += ( long ) NANOSECONDS_PER_SECOND; + } + + /* if nano second is negative after borrow, it is an overflow error */ + if( pxResult->tv_nsec < 0 ) + { + iStatus = -1; + } + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +int UTILS_TimespecCompare( const struct timespec * const x, + const struct timespec * const y ) +{ + int iStatus = 0; + + /* Check parameters */ + if( ( x == NULL ) && ( y == NULL ) ) + { + iStatus = 0; + } + else if( y == NULL ) + { + iStatus = 1; + } + else if( x == NULL ) + { + iStatus = -1; + } + else if( x->tv_sec > y->tv_sec ) + { + iStatus = 1; + } + else if( x->tv_sec < y->tv_sec ) + { + iStatus = -1; + } + else + { + /* seconds are equal compare nano seconds */ + if( x->tv_nsec > y->tv_nsec ) + { + iStatus = 1; + } + else if( x->tv_nsec < y->tv_nsec ) + { + iStatus = -1; + } + else + { + iStatus = 0; + } + } + + return iStatus; +} + +/*-----------------------------------------------------------*/ + +bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) +{ + bool xReturn = false; + + if( pxTimespec != NULL ) + { + /* Verify 0 <= tv_nsec < 1000000000. */ + if( ( pxTimespec->tv_nsec >= 0 ) && + ( pxTimespec->tv_nsec < NANOSECONDS_PER_SECOND ) ) + { + xReturn = true; + } + } + + return xReturn; +} + +/*-----------------------------------------------------------*/ diff --git a/lib/freertos-posix/LICENSE b/lib/freertos-posix/LICENSE new file mode 100644 index 0000000..90228db --- /dev/null +++ b/lib/freertos-posix/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 FreeRTOS + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/freertos-posix/README.md b/lib/freertos-posix/README.md new file mode 100644 index 0000000..d01fca3 --- /dev/null +++ b/lib/freertos-posix/README.md @@ -0,0 +1,15 @@ +## FreeRTOS+POSIX: Portable Operating System Interface (POSIX threading wrapper) for FreeRTOS + +The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. FreeRTOS+POSIX implements a subset of the [POSIX threading](https://pubs.opengroup.org/onlinepubs/7908799/xsh/threads.html) API. + +With FreeRTOS+POSIX, an existing POSIX threading compliant application can be ported to run on the FreeRTOS kernel, and therefor leverage all the [Amazon FreeRTOS](https://www.freertos.org/FAQ_Amazon.html) functionality. Additionally, a library designed for use with POSIX threading compatible operating systems can be ported to FreeRTOS kernel based applications. + +For more on FreeRTOS+POSIX, please visit [freertos.org](https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_POSIX/index.html) + +This repository only contains source code. For demo applications, please visit https://github.com/FreeRTOS/FreeRTOS-Labs. + +## To consume FreeRTOS+POSIX +It is recommended to use this repository as a submodule. Please refer to [Git Tools — Submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules). + +## Notes +This project is undergoing optimizations or refactorization to improve memory usage, modularity, documentation, demo usability, or test coverage. diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/errno.h b/lib/freertos-posix/include/FreeRTOS_POSIX/errno.h new file mode 100755 index 0000000..136e04b --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/errno.h @@ -0,0 +1,95 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file errno.h + * @brief System error numbers. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html + * + * The values defined in this file may not be compatible with the strerror + * function provided by this system. + */ + +#ifndef _FREERTOS_POSIX_ERRNO_H_ +#define _FREERTOS_POSIX_ERRNO_H_ + +/* Undefine all errnos to avoid redefinition errors with system errnos. */ +#undef EPERM +#undef ENOENT +#undef EBADF +#undef EAGAIN +#undef ENOMEM +#undef EEXIST +#undef EBUSY +#undef EINVAL +#undef ENOSPC +#undef ERANGE +#undef ENAMETOOLONG +#undef EDEADLK +#undef EOVERFLOW +#undef ENOSYS +#undef EMSGSIZE +#undef ENOTSUP +#undef ETIMEDOUT + +/** + * @name Definition of POSIX errnos. + */ +/**@{ */ +#define EPERM 1 /**< Operation not permitted. */ +#define ENOENT 2 /**< No such file or directory. */ +#define EBADF 9 /**< Bad file descriptor. */ +#define EAGAIN 11 /**< Resource unavailable, try again. */ +#define ENOMEM 12 /**< Not enough space. */ +#define EEXIST 17 /**< File exists. */ +#define EBUSY 16 /**< Device or resource busy. */ +#define EINVAL 22 /**< Invalid argument. */ +#define ENOSPC 28 /**< No space left on device. */ +#define ERANGE 34 /**< Result too large. */ +#define ENAMETOOLONG 36 /**< File name too long. */ +#define EDEADLK 45 /**< Resource deadlock would occur. */ +#define EOVERFLOW 75 /**< Value too large to be stored in data type. */ +#define ENOSYS 88 /**< Function not supported. */ +#define EMSGSIZE 90 /**< Message too long. */ +#define ENOTSUP 95 /**< Operation not supported. */ +#define ETIMEDOUT 116 /**< Connection timed out. */ +/**@} */ + +/** + * @name System Variable + * + * @brief Define FreeRTOS+POSIX errno, if enabled. + * Set configUSE_POSIX_ERRNO to enable, and clear to disable. See FreeRTOS.h. + * + * @{ + */ +#if ( configUSE_POSIX_ERRNO == 1 ) + extern int FreeRTOS_errno; + #define errno FreeRTOS_errno +#endif +/**@} */ + +#endif /* ifndef _FREERTOS_POSIX_ERRNO_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/fcntl.h b/lib/freertos-posix/include/FreeRTOS_POSIX/fcntl.h new file mode 100755 index 0000000..1e921ce --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/fcntl.h @@ -0,0 +1,79 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file fcntl.h + * @brief File control options. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html + */ + +#ifndef _FREERTOS_POSIX_FCNTL_H_ +#define _FREERTOS_POSIX_FCNTL_H_ + +/** + * @name File creation flags for use in the oflag value to open() and openat(). + */ +/**@{ */ +#define O_CLOEXEC 0x0001 /**< Close the file descriptor upon exec(). */ +#define O_CREAT 0x0002 /**< Create file if it does not exist. */ +#define O_DIRECTORY 0x0004 /**< Fail if file is a non-directory file. */ +#define O_EXCL 0x0008 /**< Exclusive use flag. */ +#define O_NOCTTY 0x0010 /**< Do not assign controlling terminal. */ +#define O_NOFOLLOW 0x0020 /**< Do not follow symbolic links. */ +#define O_TRUNC 0x0040 /**< Truncate flag. */ +#define O_TTY_INIT 0x0080 /**< termios structure provides conforming behavior. */ +/**@} */ + +/** + * @name File status flags for open(), openat(), and fcntl(). + */ +/**@{ */ +#define O_APPEND 0x0100 /**< Set append mode. */ +#define O_DSYNC 0x0200 /**< Write according to synchronized I/O data integrity completion. */ +#define O_NONBLOCK 0x0400 /**< Non-blocking mode. */ +#define O_RSYNC 0x0800 /**< Synchronized read I/O operations. */ +#define O_SYNC 0x0200 /**< Write according to synchronized I/O file integrity completion. */ +/**@} */ + +/** + * @name Mask for file access modes. + */ +/**@{ */ +#define O_ACCMODE 0xF000 +/**@} */ + +/** + * @name File access modes for open(), openat(), and fcntl(). + */ +/**@{ */ +#define O_EXEC 0x1000 /**< Open for execute only (non-directory files). */ +#define O_RDONLY 0x2000 /**< Open for reading only. */ +#define O_RDWR 0xA000 /**< Open for reading and writing. */ +#define O_SEARCH 0x4000 /**< Open directory for search only. */ +#define O_WRONLY 0x8000 /**< Open for writing only. */ +/**@} */ + +#endif /* ifndef _FREERTOS_POSIX_FCNTL_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/mqueue.h b/lib/freertos-posix/include/FreeRTOS_POSIX/mqueue.h new file mode 100755 index 0000000..7cbf01c --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/mqueue.h @@ -0,0 +1,250 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file mqueue.h + * @brief Message queues. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html + */ + +#ifndef _FREERTOS_POSIX_MQUEUE_H_ +#define _FREERTOS_POSIX_MQUEUE_H_ + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX/time.h" + +/** + * @brief Message queue descriptor. + */ +typedef void * mqd_t; + +/** + * @brief Message queue attributes. + */ +struct mq_attr +{ + long mq_flags; /**< Message queue flags. */ + long mq_maxmsg; /**< Maximum number of messages. */ + long mq_msgsize; /**< Maximum message size. */ + long mq_curmsgs; /**< Number of messages currently queued. */ +}; + +/** + * @brief Close a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_close.html + * + * @retval 0 - Upon successful completion + * @retval -1 - A error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EBADF - The mqdes argument is not a valid message queue descriptor. + */ +int mq_close( mqd_t mqdes ); + +/** + * @brief Get message queue attributes. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_getattr.html + * + * @retval 0 - Upon successful completion + * @retval -1 - A error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * DBADF - The mqdes argument is not a valid message queue descriptor. + */ +int mq_getattr( mqd_t mqdes, + struct mq_attr * mqstat ); + +/** + * @brief Open a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html + * + * @note Supported name pattern: leading <slash> character in name is always required; + * the maximum length (excluding null-terminator) of the name argument can be NAME_MAX. + * The default value of NAME_MAX in FreeRTOS_POSIX_portable_default.h is 64, which can be + * overwritten by user. + * @note mode argument is not supported. + * @note Supported oflags: O_RDWR, O_CREAT, O_EXCL, and O_NONBLOCK. + * + * @retval Message queue descriptor -- Upon successful completion + * @retval (mqd_t) - 1 -- An error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EINVAL - name argument is invalid (not following name pattern), + * OR if O_CREAT is specified in oflag with attr argument not NULL and either mq_maxmsg or mq_msgsize is equal to or less than zero, + * OR either O_CREAT or O_EXCL is not set and a queue with the same name is unlinked but pending to be removed. + * <br> + * EEXIST - O_CREAT and O_EXCL are set and the named message queue already exists. + * <br> + * ENOSPC - There is insufficient space for the creation of the new message queue. + * <br> + * ENOENT - O_CREAT is not set and the named message queue does not exist. + */ +mqd_t mq_open( const char * name, + int oflag, + mode_t mode, + struct mq_attr * attr ); + +/** + * @brief Receive a message from a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_receive.html + * + * @note msg_prio argument is not supported. Messages are not checked for corruption. + * + * @retval The length of the selected message in bytes - Upon successful completion. + * The message is removed from the queue + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EBADF - The mqdes argument is not a valid message queue descriptor open for reading. + * <br> + * EMSGSIZE - The specified message buffer size, msg_len, is less than the message size attribute of the message queue. + * <br> + * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but no message arrived on the queue before the specified timeout expired. + * <br> + * EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty. + */ +ssize_t mq_receive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned int * msg_prio ); + +/** + * @brief Send a message to a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_send.html + * + * @note msg_prio argument is not supported. + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EBADF - The mqdes argument is not a valid message queue descriptor open for writing. + * <br> + * EMSGSIZE - The specified message length, msg_len, exceeds the message size attribute of the message queue, + * OR insufficient memory for the message to be sent. + * <br> + * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but the timeout expired before the message could be added to the queue. + * <br> + * EAGAIN - The O_NONBLOCK flag is set in the message queue description associated with mqdes, + * and the specified message queue is full. + */ +int mq_send( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio ); + +/** + * @brief Receive a message from a message queue with timeout. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_timedreceive.html + * + * @note msg_prio argument is not supported. Messages are not checked for corruption. + * + * @retval The length of the selected message in bytes - Upon successful completion. + * The message is removed from the queue + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EBADF - The mqdes argument is not a valid message queue descriptor open for reading. + * <br> + * EMSGSIZE - The specified message buffer size, msg_len, is less than the message size attribute of the message queue. + * <br> + * EINVAL - The process or thread would have blocked, and the abstime parameter specified a nanoseconds field value + * less than zero or greater than or equal to 1000 million. + * <br> + * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but no message arrived on the queue before the specified timeout expired. + * <br> + * EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty. + */ +ssize_t mq_timedreceive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned * msg_prio, + const struct timespec * abstime ); + +/** + * @brief Send a message to a message queue with timeout. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_timedsend.html + * + * @note msg_prio argument is not supported. + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EBADF - The mqdes argument is not a valid message queue descriptor open for writing. + * <br> + * EMSGSIZE - The specified message length, msg_len, exceeds the message size attribute of the message queue, + * OR insufficient memory for the message to be sent. + * <br> + * EINVAL - The process or thread would have blocked, and the abstime parameter specified a nanoseconds field + * value less than zero or greater than or equal to 1000 million. + * <br> + * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but the timeout expired before the message could be added to the queue. + * <br> + * EAGAIN - The O_NONBLOCK flag is set in the message queue description associated with mqdes, + * and the specified message queue is full. + */ +int mq_timedsend( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio, + const struct timespec * abstime ); + +/** + * @brief Remove a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_unlink.html + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EINVAL - name argument is invalid. Refer to requirements on name argument in mq_open(). + * <br> + * ENOENT - The named message queue does not exist. + */ +int mq_unlink( const char * name ); + +#endif /* ifndef _FREERTOS_POSIX_MQUEUE_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/pthread.h b/lib/freertos-posix/include/FreeRTOS_POSIX/pthread.h new file mode 100755 index 0000000..77f949a --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/pthread.h @@ -0,0 +1,501 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file pthread.h + * @brief Threads. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html + */ + +#ifndef _FREERTOS_POSIX_PTHREAD_H_ +#define _FREERTOS_POSIX_PTHREAD_H_ + +/* FreeRTOS+POSIX includes. POSIX states that this header shall make symbols + * defined in sched.h and time.h visible. */ +#include "FreeRTOS_POSIX/sched.h" +#include "FreeRTOS_POSIX/time.h" + +/** + * @name pthread detach state. + */ +/**@{ */ +#define PTHREAD_CREATE_DETACHED 0 /**< Detached. */ +#define PTHREAD_CREATE_JOINABLE 1 /**< Joinable (default). */ +/**@} */ + +/** + * @name Returned to a single thread after a successful pthread_barrier_wait. + * + * @brief POSIX specifies that "The constant PTHREAD_BARRIER_SERIAL_THREAD is defined in + * <pthread.h> and its value shall be distinct from any other value returned by pthread_barrier_wait()." + * So it's defined as negative to distinguish it from the errnos, which are positive. + */ +#define PTHREAD_BARRIER_SERIAL_THREAD ( -2 ) + +/** + * @name Mutex types. + */ +/**@{ */ +#ifndef PTHREAD_MUTEX_NORMAL + #define PTHREAD_MUTEX_NORMAL 0 /**< Non-robust, deadlock on relock, does not remember owner. */ +#endif +#ifndef PTHREAD_MUTEX_ERRORCHECK + #define PTHREAD_MUTEX_ERRORCHECK 1 /**< Non-robust, error on relock, remembers owner. */ +#endif +#ifndef PTHREAD_MUTEX_RECURSIVE + #define PTHREAD_MUTEX_RECURSIVE 2 /**< Non-robust, recursive relock, remembers owner. */ +#endif +#ifndef PTHREAD_MUTEX_DEFAULT + #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL /**< PTHREAD_MUTEX_NORMAL (default). */ +#endif +/**@} */ + +/** + * @name Compile-time initializers. + * + * @brief To use PTHREAD_COND_INITIALIZER, posixconfigENABLE_PTHREAD_COND_T needs to be set to 1 + * in port specific POSIX config file. + * + * To use PTHREAD_MUTEX_INITIALIZER, posixconfigENABLE_PTHREAD_MUTEX_T needs to be set to 1 in + * port specific POSIX config file. + */ +/**@{ */ +#if posixconfigENABLE_PTHREAD_COND_T == 1 + #define PTHREAD_COND_INITIALIZER FREERTOS_POSIX_COND_INITIALIZER /**< pthread_cond_t. */ +#endif + +#if posixconfigENABLE_PTHREAD_MUTEX_T == 1 + #define PTHREAD_MUTEX_INITIALIZER FREERTOS_POSIX_MUTEX_INITIALIZER /**< pthread_mutex_t. */ +#endif + +/**@} */ + +/** + * @brief Destroy the thread attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_destroy.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_destroy( pthread_attr_t * attr ); + +/** + * @brief Get detachstate attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getdetachstate.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_getdetachstate( const pthread_attr_t * attr, + int * detachstate ); + +/** + * @brief Get schedparam attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getschedparam.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_getschedparam( const pthread_attr_t * attr, + struct sched_param * param ); + +/** + * @brief Get stacksize attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getstacksize.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_getstacksize( const pthread_attr_t * attr, + size_t * stacksize ); + +/** + * @brief Initialize the thread attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_init.html + * + * @retval 0 - Upon successful completion. + * + * @note Currently, only stack size, sched_param, and detach state attributes + * are supported. Also see pthread_attr_get*() and pthread_attr_set*(). + */ +int pthread_attr_init( pthread_attr_t * attr ); + +/** + * @brief Set detachstate attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setdetachstate.html + * + * @retval 0 - Upon successful completion + * @retval EINVAL - The value of detachstate is not valid. Currently, supported detach states are -- + * PTHREAD_CREATE_DETACHED and PTHREAD_CREATE_JOINABLE. + */ +int pthread_attr_setdetachstate( pthread_attr_t * attr, + int detachstate ); + +/** + * @brief Set schedparam attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedparam.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value of param is not valid. + * @retval ENOTSUP - An attempt was made to set the attribute to an unsupported value. + */ +int pthread_attr_setschedparam( pthread_attr_t * attr, + const struct sched_param * param ); + +/** + * @brief Set the schedpolicy attribute. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedpolicy.html + * + * @retval 0 - Upon successful completion. + * + * @warning This function is a stub and always returns 0. + */ +int pthread_attr_setschedpolicy( pthread_attr_t * attr, + int policy ); + +/** + * @brief Set stacksize attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setstacksize.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value of stacksize is less than {PTHREAD_STACK_MIN}. + */ +int pthread_attr_setstacksize( pthread_attr_t * attr, + size_t stacksize ); + +/** + * @brief Destroy a barrier object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_destroy.html + * + * @retval 0 - Upon successful completion. + * + * @note This function does not validate whether there is any thread blocking on the barrier before destroying. + */ +int pthread_barrier_destroy( pthread_barrier_t * barrier ); + +/** + * @brief Initialize a barrier object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_init.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value specified by count is equal to zero. + * @retval ENOMEM - count cannot fit into FreeRTOS event group type OR insufficient memory exists to initialize the barrier. + * + * @note attr is ignored. + * + * @note pthread_barrier_init() is implemented with FreeRTOS event group. + * To ensure count fits in event group, count may be at most 8 when configUSE_16_BIT_TICKS is 1; + * it may be at most 24 otherwise. configUSE_16_BIT_TICKS is configured in application FreeRTOSConfig.h + * file, which defines how many bits tick count type has. See further details and limitation about event + * group and configUSE_16_BIT_TICKS in FreeRTOS site. + */ +int pthread_barrier_init( pthread_barrier_t * barrier, + const pthread_barrierattr_t * attr, + unsigned count ); + +/** + * @brief Synchronize at a barrier. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_wait.html + * + * @retval PTHREAD_BARRIER_SERIAL_THREAD - Upon successful completion, the first thread. + * @retval 0 - Upon successful completion, other thread(s). + */ +int pthread_barrier_wait( pthread_barrier_t * barrier ); + +/** + * @brief Thread creation. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_create.html + * + * @retval 0 - Upon successful completion. + * @retval EAGAIN - Insufficient memory for either thread structure or task creation. + */ +int pthread_create( pthread_t * thread, + const pthread_attr_t * attr, + void *( *startroutine )( void * ), + void * arg ); + +/** + * @brief Broadcast a condition. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_broadcast( pthread_cond_t * cond ); + +/** + * @brief Destroy condition variables. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_destroy.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_destroy( pthread_cond_t * cond ); + +/** + * @brief Initialize condition variables. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_init.html + * + * @retval 0 - Upon successful completion. + * @retval ENOMEM - Insufficient memory exists to initialize the condition variable. + * + * @note attr is ignored and treated as NULL. Default setting is always used. + */ +int pthread_cond_init( pthread_cond_t * cond, + const pthread_condattr_t * attr ); + +/** + * @brief Signal a condition. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_signal( pthread_cond_t * cond ); + +/** + * @brief Wait on a condition with a timeout. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The abstime argument passed in does not refer to an initialized structure OR + * the abstime parameter specified a nanoseconds field value less than zero or + * greater than or equal to 1000 million. + * @retval ETIMEDOUT - The time specified by abstime to pthread_cond_timedwait() has passed. + */ +int pthread_cond_timedwait( pthread_cond_t * cond, + pthread_mutex_t * mutex, + const struct timespec * abstime ); + +/** + * @brief Wait on a condition. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_wait( pthread_cond_t * cond, + pthread_mutex_t * mutex ); + +/** + * @brief Compare thread IDs. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_equal.html + * + * @retval 0 - t1 and t2 are both not NULL && equal. + * @retval non-zero - otherwise. + */ +int pthread_equal( pthread_t t1, + pthread_t t2 ); + +/** + * @brief Thread termination. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_exit.html + * + * @retval void - this function cannot return to its caller. + */ +void pthread_exit( void * value_ptr ); + +/** + * @brief Dynamic thread scheduling parameters access. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_getschedparam.html + * + * @retval 0 - Upon successful completion. + * + * @note policy is always set to SCHED_OTHER by this function. + */ +int pthread_getschedparam( pthread_t thread, + int * policy, + struct sched_param * param ); + +/** + * @brief Wait for thread termination. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html + * + * @retval 0 - Upon successful completion. + * @retval EDEADLK - The value specified by the thread argument to pthread_join() does not refer + * to a joinable thread OR multiple simultaneous calls to pthread_join() + * specifying the same target thread OR the value specified by the thread argument + * to pthread_join() refers to the calling thread. + */ +int pthread_join( pthread_t thread, + void ** retval ); + +/** + * @brief Destroy a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_destroy.html + * + * @retval 0 - Upon successful completion. + * + * @note If there exists a thread holding this mutex, this function returns 0 with mutex not being destroyed. + */ +int pthread_mutex_destroy( pthread_mutex_t * mutex ); + +/** + * @brief Initialize a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html + * + * @retval 0 - Upon successful completion. + * @retval ENOMEM - Insufficient memory exists to initialize the mutex structure. + * @retval EAGAIN - Unable to initialize the mutex structure member(s). + */ +int pthread_mutex_init( pthread_mutex_t * mutex, + const pthread_mutexattr_t * attr ); + +/** + * @brief Lock a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - the abstime parameter specified a nanoseconds field value less than zero + * or greater than or equal to 1000 million. + * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already + * owns the mutex. + */ +int pthread_mutex_lock( pthread_mutex_t * mutex ); + +/** + * @brief Lock a mutex with timeout. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The abstime argument passed in does not refer to an initialized structure OR + * the abstime parameter specified a nanoseconds field value less than zero or + * greater than or equal to 1000 million. + * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already owns the mutex. + * @retval ETIMEDOUT - The mutex could not be locked before the specified timeout expired. + */ +int pthread_mutex_timedlock( pthread_mutex_t * mutex, + const struct timespec * abstime ); + +/** + * @brief Attempt to lock a mutex. Fail immediately if mutex is already locked. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_trylock.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - the abstime parameter specified a nanoseconds field value less than zero + * or greater than or equal to 1000 million. + * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already + * owns the mutex. + * @retval EBUSY - The mutex could not be acquired because it was already locked. + */ +int pthread_mutex_trylock( pthread_mutex_t * mutex ); + +/** + * @brief Unlock a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_unlock.html + * + * @retval 0 - Upon successful completion. + * @retval EPERM - The mutex type is PTHREAD_MUTEX_ERRORCHECK or PTHREAD_MUTEX_RECURSIVE, and + * the current thread does not own the mutex. + */ +int pthread_mutex_unlock( pthread_mutex_t * mutex ); + +/** + * @brief Destroy the mutex attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_destroy.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_mutexattr_destroy( pthread_mutexattr_t * attr ); + +/** + * @brief Get the mutex type attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_gettype.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_mutexattr_gettype( const pthread_mutexattr_t * attr, + int * type ); + +/** + * @brief Initialize the mutex attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_init.html + * + * @retval 0 - Upon successful completion. + * + * @note Currently, only the type attribute is supported. Also see pthread_mutexattr_settype() + * and pthread_mutexattr_gettype(). + */ +int pthread_mutexattr_init( pthread_mutexattr_t * attr ); + +/** + * @brief Set the mutex type attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_settype.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value type is invalid. + */ +int pthread_mutexattr_settype( pthread_mutexattr_t * attr, + int type ); + +/** + * @brief Get the calling thread ID. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_self.html + * + * @retval the thread ID of the calling thread. + */ +pthread_t pthread_self( void ); + +/** + * @brief Dynamic thread scheduling parameters access. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_setschedparam.html + * + * @note policy is ignored; only priority (param.sched_priority) may be changed. + * + * @retval 0 - Upon successful completion. + */ +int pthread_setschedparam( pthread_t thread, + int policy, + const struct sched_param * param ); + +#endif /* _FREERTOS_POSIX_PTHREAD_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/sched.h b/lib/freertos-posix/include/FreeRTOS_POSIX/sched.h new file mode 100755 index 0000000..5a72c44 --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/sched.h @@ -0,0 +1,84 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file sched.h + * @brief Execution scheduling. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sched.h.html + */ + +#ifndef _FREERTOS_POSIX_SCHED_H_ +#define _FREERTOS_POSIX_SCHED_H_ + +/** + * @name Scheduling Policies + */ +/**@{ */ +#define SCHED_OTHER 0 /**< Another scheduling policy. */ +/**@} */ + +/** + * @brief Scheduling parameters required for implementation of each supported + * scheduling policy. + */ +struct sched_param +{ + int sched_priority; /**< Process or thread execution scheduling priority. */ +}; + +/** + * @brief Get priority limit (max). + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_get_priority_max.html + * + * @note policy is ignored. + * + * @return the maximum priority value (0-based) system configuration allows. + * <br> + * e.g. if configMAX_PRIORITIES == 7, this function returns (configMAX_PRIORITIES - 1). + * configMAX_PRIORITIES is configured in application FreeRTOSConfig.h file. + */ +int sched_get_priority_max( int policy ); + +/** + * @brief Get priority limit (min). + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_get_priority_min.html + * + * @note policy is ignored. + */ +int sched_get_priority_min( int policy ); + +/** + * @brief Yield the processor. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_yield.html + * + * @retval 0 - Upon successful completion + */ +int sched_yield( void ); + +#endif /* ifndef _FREERTOS_POSIX_SCHED_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/semaphore.h b/lib/freertos-posix/include/FreeRTOS_POSIX/semaphore.h new file mode 100755 index 0000000..fa1da91 --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/semaphore.h @@ -0,0 +1,143 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file semaphore.h + * @brief Semaphores. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/semaphore.h.html + */ + +#ifndef _FREERTOS_POSIX_SEMAPHORE_H_ +#define _FREERTOS_POSIX_SEMAPHORE_H_ + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX/time.h" +#include "FreeRTOS_POSIX_types.h" + +/** + * @brief Semaphore type. + */ +typedef PosixSemType_t sem_t; + +/** + * @brief Destroy an unnamed semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_destroy.html + * + * @retval 0 - upon successful completion + * + * @note Semaphore is destroyed regardless of whether there is any thread currently blocked on this semaphore. + */ +int sem_destroy( sem_t * sem ); + +/** + * @brief Get the value of a semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_getvalue.html + * + * @retval 0 - Upon successful completion + * + * @note If sem is locked, then the object to which sval points is set to zero. + */ +int sem_getvalue( sem_t * sem, + int * sval ); + +/** + * @brief Initialize an unnamed semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_init.html + * + * @note pshared is ignored. Semaphores will always be considered "shared". + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @sideeffect Possible errno values + * <br> + * EINVAL - The value argument exceeds {SEM_VALUE_MAX}. + * <br> + * ENOSPC - A resource required to initialize the semaphore has been exhausted. + */ +int sem_init( sem_t * sem, + int pshared, + unsigned value ); + +/** + * @brief Unlock a semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_post.html + * + * @retval 0 - upon successful completion + */ +int sem_post( sem_t * sem ); + +/** + * @brief Lock a semaphore with timeout. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_timedwait.html + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @sideeffect Possible errno values + * <br> + * EINVAL - parameter specified a nanoseconds field value less than zero or greater + * than or equal to 1000 million + * <br> + * ETIMEDOUT - The semaphore could not be locked before the specified timeout expired. + * + * @note Deadlock detection is not implemented. + */ +int sem_timedwait( sem_t * sem, + const struct timespec * abstime ); + +/** + * @brief Lock a semaphore if available. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_trywait.html + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @sideeffect Possible errno values + * <br> + * EAGAIN - The semaphore was already locked, so it cannot be immediately locked by the sem_trywait() operation. + */ +int sem_trywait( sem_t * sem ); + +/** + * @brief Lock a semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_wait.html + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @note Deadlock detection is not implemented. + */ +int sem_wait( sem_t * sem ); + +#endif /* ifndef _FREERTOS_POSIX_SEMAPHORE_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/signal.h b/lib/freertos-posix/include/FreeRTOS_POSIX/signal.h new file mode 100755 index 0000000..8f9cc6d --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/signal.h @@ -0,0 +1,70 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file signal.h + * @brief Signals. + * + * Signals are currently not implemented in FreeRTOS+POSIX. This header only + * defines the signal data structures used elsewhere. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html + */ + + +#ifndef _FREERTOS_POSIX_SIGNAL_H_ +#define _FREERTOS_POSIX_SIGNAL_H_ + +/** + * @name Values of sigev_notify. + */ +/**@{ */ +#define SIGEV_NONE 0 /**< No asynchronous notification is delivered when the event of interest occurs. */ +#define SIGEV_SIGNAL 1 /**< A queued signal, with an application-defined value, is generated when the event of interest occurs. Not supported. */ +#define SIGEV_THREAD 2 /**< A notification function is called to perform notification. */ +/**@} */ + +/** + * @brief Signal value. + */ +union sigval +{ + int sival_int; /**< Integer signal value. */ + void * sival_ptr; /**< Pointer signal value. */ +}; + +/** + * @brief Signal event structure. + */ +struct sigevent +{ + int sigev_notify; /**< Notification type. A value of SIGEV_SIGNAL is not supported. */ + int sigev_signo; /**< Signal number. This member is ignored. */ + union sigval sigev_value; /**< Signal value. Only the sival_ptr member is used. */ + void ( * sigev_notify_function )( union sigval ); /**< Notification function. */ + pthread_attr_t * sigev_notify_attributes; /**< Notification attributes. */ +}; + +#endif /* ifndef _FREERTOS_POSIX_SIGNAL_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/sys/types.h b/lib/freertos-posix/include/FreeRTOS_POSIX/sys/types.h new file mode 100755 index 0000000..9cb3428 --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/sys/types.h @@ -0,0 +1,191 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file sys/types.h + * @brief Data types. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html + */ + +#ifndef _FREERTOS_POSIX_TYPES_H_ +#define _FREERTOS_POSIX_TYPES_H_ + +/* C standard library includes. */ +#include <stdint.h> + +/* FreeRTOS types include */ +#include "FreeRTOS_POSIX_types.h" + +/** + * @brief Used for system times in clock ticks or CLOCKS_PER_SEC. + * + * Enabled/disabled by posixconfigENABLE_CLOCK_T. + */ +#if !defined( posixconfigENABLE_CLOCK_T ) || ( posixconfigENABLE_CLOCK_T == 1 ) + typedef uint32_t clock_t; +#endif + +/** + * @brief Used for clock ID type in the clock and timer functions. + * + * Enabled/disabled by posixconfigENABLE_CLOCKID_T. + */ +#if !defined( posixconfigENABLE_CLOCKID_T ) || ( posixconfigENABLE_CLOCKID_T == 1 ) + typedef int clockid_t; +#endif + +/** + * @brief Used for some file attributes. + * + * Enabled/disabled by posixconfigENABLE_MODE_T. + */ +#if !defined( posixconfigENABLE_MODE_T ) || ( posixconfigENABLE_MODE_T == 1 ) + typedef int mode_t; +#endif + +/** + * @brief Used for process IDs and process group IDs. + * + * Enabled/disabled by posixconfigENABLE_PID_T. + */ +#if !defined( posixconfigENABLE_PID_T ) || ( posixconfigENABLE_PID_T == 1 ) + typedef int pid_t; +#endif + +/** + * @brief Used to identify a thread attribute object. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_ATTR_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_ATTR_T ) || ( posixconfigENABLE_PTHREAD_ATTR_T == 1 ) + typedef PthreadAttrType_t pthread_attr_t; +#endif + +/** + * @brief Used to identify a barrier. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_BARRIER_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_BARRIER_T ) || ( posixconfigENABLE_PTHREAD_BARRIER_T == 1 ) + typedef PthreadBarrierType_t pthread_barrier_t; +#endif + +/** + * @brief Used to define a barrier attributes object. + */ +typedef void * pthread_barrierattr_t; + +/** + * @brief Used for condition variables. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_COND_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_COND_T ) || ( posixconfigENABLE_PTHREAD_COND_T == 1 ) + typedef PthreadCondType_t pthread_cond_t; +#endif + +/** + * @brief Used to identify a condition attribute object. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_CONDATTR_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_CONDATTR_T ) || ( posixconfigENABLE_PTHREAD_CONDATTR_T == 1 ) + typedef void * pthread_condattr_t; +#endif + +/** + * @brief Used for mutexes. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_MUTEX_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_MUTEX_T ) || ( posixconfigENABLE_PTHREAD_MUTEX_T == 1 ) + typedef PthreadMutexType_t pthread_mutex_t; +#endif + +/** + * @brief Used to identify a mutex attribute object. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_MUTEXATTR_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_MUTEXATTR_T ) || ( posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1 ) + typedef PthreadMutexAttrType_t pthread_mutexattr_t; +#endif + +/** + * @brief Used to identify a thread. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_T ) || ( posixconfigENABLE_PTHREAD_T == 1 ) + typedef void * pthread_t; +#endif + +/** + * @brief Used for a count of bytes or an error indication. + * + * Enabled/disabled by posixconfigENABLE_SSIZE_T. + */ +#if !defined( posixconfigENABLE_SSIZE_T ) || ( posixconfigENABLE_SSIZE_T == 1 ) + typedef int ssize_t; +#endif + +/** + * @brief Used for time in seconds. + * + * Enabled/disabled by posixconfigENABLE_TIME_T. + */ +#if !defined( posixconfigENABLE_TIME_T ) || ( posixconfigENABLE_TIME_T == 1 ) + typedef int64_t time_t; +#endif + +/** + * @brief Used for timer ID returned by timer_create(). + * + * Enabled/disabled by posixconfigENABLE_TIMER_T. + */ +#if !defined( posixconfigENABLE_TIMER_T ) || ( posixconfigENABLE_TIMER_T == 1 ) + typedef void * timer_t; +#endif + +/** + * @brief Used for time in microseconds. + * + * Enabled/disabled by posixconfigENABLE_USECONDS_T. + */ +#if !defined( posixconfigENABLE_USECONDS_T ) || ( posixconfigENABLE_USECONDS_T == 1 ) + typedef unsigned long useconds_t; +#endif + +/** + * @brief Used for file sizes. + * + * Enabled/disabled by posixconfigENABLE_OFF_T. + */ +#if !defined( posixconfigENABLE_OFF_T ) || ( posixconfigENABLE_OFF_T == 1 ) + typedef long int off_t; +#endif + +#endif /* ifndef _FREERTOS_POSIX_TYPES_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/time.h b/lib/freertos-posix/include/FreeRTOS_POSIX/time.h new file mode 100755 index 0000000..d04de0f --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/time.h @@ -0,0 +1,258 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file time.h + * @brief Time types. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html + */ + +#ifndef _FREERTOS_POSIX_TIME_H_ +#define _FREERTOS_POSIX_TIME_H_ + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX/sys/types.h" +#include "FreeRTOS_POSIX/signal.h" + +/** + * @name Unit conversion constants. + */ +/**@{ */ +#define MICROSECONDS_PER_SECOND ( 1000000LL ) /**< Microseconds per second. */ +#define NANOSECONDS_PER_SECOND ( 1000000000LL ) /**< Nanoseconds per second. */ +#define NANOSECONDS_PER_TICK ( NANOSECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Nanoseconds per FreeRTOS tick. */ +/**@} */ + +/** + * @name Clock identifiers. + */ +/**@{ */ +#define CLOCK_REALTIME 0 /**< The identifier of the system-wide clock measuring real time. */ +#define CLOCK_MONOTONIC 1 /**< The identifier for the system-wide monotonic clock.*/ +/**@} */ + +/** + * @name A number used to convert the value returned by the clock() function into seconds. + */ +/**@{ */ +#define CLOCKS_PER_SEC ( ( clock_t ) configTICK_RATE_HZ ) +/**@} */ + +/** + * @name Flag indicating time is absolute. + * + * For functions taking timer objects, this refers to the clock associated with the timer. + */ +/**@{ */ +#define TIMER_ABSTIME 0x01 +/**@} */ + +#if !defined( posixconfigENABLE_TIMESPEC ) || ( posixconfigENABLE_TIMESPEC == 1 ) + +/** + * @brief represents an elapsed time + */ + struct timespec + { + time_t tv_sec; /**< Seconds. */ + long tv_nsec; /**< Nanoseconds. */ + }; +#endif + +#if !defined( posixconfigENABLE_ITIMERSPEC ) || ( posixconfigENABLE_ITIMERSPEC == 1 ) + +/** + * @brief timer + */ + struct itimerspec + { + struct timespec it_interval; /**< Timer period. */ + struct timespec it_value; /**< Timer expiration. */ + }; +#endif + +/** + * @brief Report CPU time used. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock.html + * + * @return The number of FreeRTOS ticks since the scheduler + * was started minus the ticks spent in the idle task. + * + * @note This function does NOT report the number of ticks spent by the calling thread. + */ +clock_t clock( void ); + +/** + * @brief Access a process CPU-time clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getcpuclockid.html + * + * @retval EPERM + * + * @note This function is currently unsupported. + * + */ +int clock_getcpuclockid( pid_t pid, + clockid_t * clock_id ); + +/** + * @brief Returns the resolution of a clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html + * + * @note clock_id is ignored + * @note This function stores the resolution of the FreeRTOS tick count in the object res points to. + * + * @retval 0 - Upon successful execution + */ +int clock_getres( clockid_t clock_id, + struct timespec * res ); + +/** + * @brief Returns the current value for the specified clock, clock_id. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html + * + * @note clock_id is ignored + * @note this function does not check for overflows of time_t. + * + * @retval 0 - Upon successful completion. + */ +int clock_gettime( clockid_t clock_id, + struct timespec * tp ); + +/** + * @brief High resolution sleep with specifiable clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_nanosleep.html + * + * @note clock_id is ignored, as this function uses the FreeRTOS tick count as its clock. + * @note flags is ignored, if INCLUDE_vTaskDelayUntil is 0. i.e. the FreeRTOS function vTaskDelayUntil isn't available. + * @note rmtp is also ignored, as signals are not implemented. + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million. + */ +int clock_nanosleep( clockid_t clock_id, + int flags, + const struct timespec * rqtp, + struct timespec * rmtp ); + +/** + * @brief Sets the time for the specified clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_settime.html + * + * @retval -1 with errno set to EPERM. + * + * @note This function is currently unsupported, as FreeRTOS does not provide a function to modify the tick count. + */ +int clock_settime( clockid_t clock_id, + const struct timespec * tp ); + +/** + * @brief High resolution sleep. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html + * + * @note rmtp is ignored, as signals are not implemented. + * + * @retval 0 - Upon successful completion. + * @retval -1 - The rqtp argument is invalid OR the rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million. + * + */ +int nanosleep( const struct timespec * rqtp, + struct timespec * rmtp ); + +/** + * @brief Create a per-process timer. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html + * + * @note clock_id is ignored, as this function used the FreeRTOS tick count as its clock. + * @note evp.sigev_notify must be set to SIGEV_THREAD, since signals are currently not supported. + * + * @retval 0 - Upon successful completion, with location referenced by timerid updated. + * @retval -1 - If an error occurs. errno is also set. + * + * @sideeffect Possible errno values + * <br> + * ENOTSUP - If evp is NULL OR evp->sigen_notify == SIGEV_SIGNAL. + * <br> + * EAGAIN - The system lacks sufficient signal queuing resources to honor the request. + */ +int timer_create( clockid_t clockid, + struct sigevent * evp, + timer_t * timerid ); + +/** + * @brief Delete a per-process timer. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html + * + * @retval 0 - Upon successful completion. + */ +int timer_delete( timer_t timerid ); + +/** + * @brief Get the timer overrun count. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html + * + * @retval 0 - Always return 0, since signals are not supported. + */ +int timer_getoverrun( timer_t timerid ); + +/** + * @brief Get the amount of time until the timer expires. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html + * + * @retval 0 - Upon successful completion. + */ +int timer_gettime( timer_t timerid, + struct itimerspec * value ); + +/** + * @brief Set the time until the next expiration of the timer. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_settime.html + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred, errno is also set. + * + * @sideeffect Possible errno values + * <br> + * EINVAL - A value structure specified a nanosecond value less than zero or greater than or equal to 1000 million, + * AND the it_value member of that structure did not specify zero seconds and nanoseconds. + */ +int timer_settime( timer_t timerid, + int flags, + const struct itimerspec * value, + struct itimerspec * ovalue ); + +#endif /* ifndef _FREERTOS_POSIX_TIME_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/unistd.h b/lib/freertos-posix/include/FreeRTOS_POSIX/unistd.h new file mode 100755 index 0000000..3b8af22 --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/unistd.h @@ -0,0 +1,61 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file unistd.h + * @brief Standard symbolic constants and types + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html + */ + +#ifndef _FREERTOS_POSIX_UNISTD_H_ +#define _FREERTOS_POSIX_UNISTD_H_ + +#include "FreeRTOS_POSIX/sys/types.h" + +/** + * @brief Suspend execution for an interval of time. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html + * + * @param[in] seconds The number of seconds to suspend execution. + * + * @retval 0 - Upon successful completion. + * + * @note Return value of a positive number is not yet supported. + */ +unsigned sleep( unsigned seconds ); + +/** + * @brief Suspend execution for microsecond intervals. + * + * This is a useful, non-POSIX function. + * @param[in] usec The number of microseconds to suspend execution. + * + * @retval 0 - Upon successful completion. + */ +int usleep( useconds_t usec ); + +#endif /* ifndef _FREERTOS_POSIX_UNISTD_H_ */ diff --git a/lib/freertos-posix/include/FreeRTOS_POSIX/utils.h b/lib/freertos-posix/include/FreeRTOS_POSIX/utils.h new file mode 100755 index 0000000..c893a95 --- /dev/null +++ b/lib/freertos-posix/include/FreeRTOS_POSIX/utils.h @@ -0,0 +1,155 @@ +/* + * Amazon FreeRTOS POSIX V1.1.0 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file utils.h + * @brief Utility functions used by FreeRTOS+POSIX. + */ + +#ifndef _FREERTOS_POSIX_UTILS_ +#define _FREERTOS_POSIX_UTILS_ + +/* C standard library includes. */ +#include <stdbool.h> +#include <stdint.h> + +/* FreeRTOS+POSIX includes. */ +#include "FreeRTOS_POSIX/time.h" + +/** + * @brief Calculates the length of pcString, up to xMaxLength. + * + * @param[in] pcString The string to find the length of. + * @param[in] xMaxLength The limit when searching for the end of pcString. + * + * @return 0 if pcString is NULL; otherwise, the length of pcString or xMaxLength, + * whichever is smaller. + */ +size_t UTILS_strnlen( const char * const pcString, + size_t xMaxLength ); + +/** + * @brief Calculates the number of ticks between now and a given timespec. + * + * @param[in] pxAbsoluteTime A time in the future, specified as seconds and + * nanoseconds since CLOCK_REALTIME's 0. + * @param[in] pxCurrentTime current time, specified as seconds and + * nanoseconds. + * @param[out] pxResult Where the result of the conversion is stored. The result + * is rounded up for fractional ticks. + * + * @return 0 on success. Otherwise, ETIMEDOUT if pxAbsoluteTime is in the past, + * or EINVAL for invalid parameters. + */ +int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime, + const struct timespec * const pxCurrentTime, + TickType_t * const pxResult ); + +/** + * @brief Converts a struct timespec to FreeRTOS ticks. + * + * @param[in] pxTimespec The timespec to convert. + * @param[out] Where the result of the conversion is stored. The result is rounded + * up for fractional ticks. + * + * @return 0 on success. Otherwise, EINVAL for invalid parameters. + */ +int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, + TickType_t * const pxResult ); + +/** + * @brief Converts an integer value to a timespec. + * + * @param[in] llSource The value to convert. + * @param[out] pxDestination Where to store the converted value. + * + * @return No return value. + */ +void UTILS_NanosecondsToTimespec( int64_t llSource, + struct timespec * const pxDestination ); + +/** + * @brief Calculates pxResult = x + y. + * + * @param[in] x The first argument for addition. + * @param[in] y The second argument for addition. + * @param[out] pxResult Where the result of the calculation is stored. + * + * @return -1 if any argument was NULL; 1 if result is negative (overflow); otherwise, 0. + */ +int UTILS_TimespecAdd( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ); + +/** + * @brief Calculates pxResult = x + ( struct timespec ) nanosec. + * + * @param[in] x The first argument for addition. + * @param[in] llNanoseconds The second argument for addition. + * @param[out] pxResult Where the result of the calculation is stored. + * + * @return -1 if pxResult or x was NULL; 1 if result is negative; otherwise, 0. + */ +int UTILS_TimespecAddNanoseconds( const struct timespec * const x, + int64_t llNanoseconds, + struct timespec * const pxResult ); + +/** + * @brief Calculates pxResult = x - y. If the result is negative contents of + * pResult are undefined + * + * @param[in] x The first argument for subtraction. + * @param[in] y The second argument for subtraction. + * @param[out] pxResult Where the result of the calculation is stored. + * + * @return -1 if any argument was NULL; 1 if result is negative; otherwise, 0. + */ +int UTILS_TimespecSubtract( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ); + +/** + * @brief Compare x == y. + * + * @param[in] x The first argument for comparison. + * @param[in] y The second argument for comparison. + * + * @return 0 if x == y; 1 if x > y; -1 if x < y or any argument was NULL + */ +int UTILS_TimespecCompare( const struct timespec * const x, + const struct timespec * const y ); + +/** + * @brief Checks that a timespec conforms to POSIX. + * + * A valid timespec must have 0 <= tv_nsec < 1000000000. + * + * @param[in] pxTimespec The timespec to validate. + * + * @return true if the pxTimespec is valid, false otherwise. + */ +bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ); + +#endif /* ifndef _FREERTOS_POSIX_UTILS_ */ diff --git a/lib/freertos-posix/include/private/iot_doubly_linked_list.h b/lib/freertos-posix/include/private/iot_doubly_linked_list.h new file mode 100755 index 0000000..338c539 --- /dev/null +++ b/lib/freertos-posix/include/private/iot_doubly_linked_list.h @@ -0,0 +1,242 @@ +/* + * Amazon FreeRTOS Common V1.0.0 + * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file iot_doubly_linked_list.h + * @brief Doubly Linked List implementation. + * + * A generic implementation of circular Doubly Linked List which consists of a + * list head and some list entries (zero in case of an empty list). + * + * To start with, a structure of type Link_t should be embedded in the structure + * which is to be organized as doubly linked list. + * @code + * typedef struct UserStruct + * { + * uint32_t ulField1; + * uint32_t ulField2; + * Link_t xLink; + * } UserStruct_t; + * @endcode + * + * A List head should then be defined and initialized. + * @code + * Link_t xListHead; + * listINIT_HEAD( &xListHead ); + * @endcode + * + * listADD can then be used to add nodes to the list. + * @code + * listADD( &( xListHead ), &( pxUserStruct->xLink ) ); + * @endcode + * + * listFOR_EACH can be used for traversing the list. + * @code + * Link_t *pxLink; + * UserStruct_t *pxUserStruct; + * listFOR_EACH( pxLink, &( xListHead ) ) + * { + * pxUserStruct = listCONTAINER( pxLink, UserStruct_t, xLink ); + * } + * @endcode + * + * listFOR_EACH_SAFE should be used if you want to perform destructive operations + * (like free) on nodes while traversing the list. + * @code + * Link_t *pxLink, *pxTempLink; + * UserStruct_t *pxUserStruct; + * listFOR_EACH( pxLink, pxTempLink, &( xListHead ) ) + * { + * pxUserStruct = listCONTAINER( pxLink, UserStruct_t, xLink ); + * free( pxUserStruct ); + * } + * @endcode + */ + +#ifndef _AWS_DOUBLY_LINKED_LIST_H_ +#define _AWS_DOUBLY_LINKED_LIST_H_ + +#include <stddef.h> +#include <stdint.h> + +/** + * @brief Struct embedded in any struct to make it a doubly linked + * list. + * + * pxNext in the head points to the first node in the list and pxPrev + * in the head points to the last node in the list. In case of empty + * list, both pxPrev and pxNext in the head point to the head node itself. + */ +typedef struct Link +{ + struct Link * pxPrev; /**< Pointer to the previous node. */ + struct Link * pxNext; /**< Pointer to the next node. */ +} Link_t; + +/** + * @brief Initializes the given list head to an empty list. + * + * @param[in] pxHead The given list head to initialize. + */ +#define listINIT_HEAD( pxHead ) \ + { \ + ( pxHead )->pxPrev = ( pxHead ); \ + ( pxHead )->pxNext = ( pxHead ); \ + } + +/** + * @brief Adds the given new node to the given list. + * + * @param[in] pxHead The head of the given list. + * @param[in] pxLink The given new node to be added to the given + * list. + */ +#define listADD( pxHead, pxLink ) \ + { \ + Link_t * pxPrevLink = ( pxHead ); \ + Link_t * pxNextLink = ( ( pxHead )->pxNext ); \ + \ + ( pxLink )->pxNext = pxNextLink; \ + pxNextLink->pxPrev = ( pxLink ); \ + pxPrevLink->pxNext = ( pxLink ); \ + ( pxLink )->pxPrev = ( pxPrevLink ); \ + } + +/** + * @brief Removes the given node from the list it is part of. + * + * If the given node is not a part of any list (i.e. next and previous + * nodes are NULL), nothing happens. + * + * @param[in] pxLink The given node to remove from the list. + */ +#define listREMOVE( pxLink ) \ + { \ + /* If the link is part of a list, remove it from the list. */ \ + if( ( pxLink )->pxNext != NULL && ( pxLink )->pxPrev != NULL ) \ + { \ + ( pxLink )->pxPrev->pxNext = ( pxLink )->pxNext; \ + ( pxLink )->pxNext->pxPrev = ( pxLink )->pxPrev; \ + } \ + \ + /* Make sure that this link is not part of any list anymore. */ \ + ( pxLink )->pxPrev = NULL; \ + ( pxLink )->pxNext = NULL; \ + } + +/** + * @brief Given the head of a list, checks if the list is empty. + * + * @param[in] pxHead The head of the given list. + */ +#define listIS_EMPTY( pxHead ) ( ( ( pxHead ) == NULL ) || ( ( pxHead )->pxNext == ( pxHead ) ) ) + +/** + * @brief Removes the first node from the given list and returns it. + * + * Removes the first node from the given list and assigns it to the + * pxLink parameter. If the list is empty, it assigns NULL to the + * pxLink. + * + * @param[in] pxHead The head of the list from which to remove the + * first node. + * @param[out] pxLink The output parameter to receive the removed + * node. + */ +#define listPOP( pxHead, pxLink ) \ + { \ + if( listIS_EMPTY( ( pxHead ) ) ) \ + { \ + ( pxLink ) = NULL; \ + } \ + else \ + { \ + ( pxLink ) = ( pxHead )->pxNext; \ + /* If the link is part of a list, remove it from the list. */ \ + if( ( pxLink )->pxNext != NULL && ( pxLink )->pxPrev != NULL ) \ + { \ + ( pxLink )->pxPrev->pxNext = ( pxLink )->pxNext; \ + ( pxLink )->pxNext->pxPrev = ( pxLink )->pxPrev; \ + } \ + \ + /* Make sure that this link is not part of any list anymore. */ \ + ( pxLink )->pxPrev = NULL; \ + ( pxLink )->pxNext = NULL; \ + } \ + } + +/** + * @brief Merges a list into a given list. + * + * @param[in] pxHeadResultList The head of the given list into which the + * other list should be merged. + * @param[in] pxHeadListToMerge The head of the list to be merged into the + * given list. + */ +#define listMERGE( pxHeadResultList, pxHeadListToMerge ) \ + { \ + if( !listIS_EMPTY( ( pxHeadListToMerge ) ) ) \ + { \ + /* Setup links between last node of listToMerge and first node of resultList. */ \ + ( pxHeadListToMerge )->pxPrev->pxNext = ( pxHeadResultList )->pxNext; \ + ( pxHeadResultList )->pxNext->pxPrev = ( pxHeadListToMerge )->pxPrev; \ + \ + /* Setup links between first node of listToMerge and the head of resultList. */ \ + ( pxHeadListToMerge )->pxNext->pxPrev = ( pxHeadResultList ); \ + ( pxHeadResultList )->pxNext = ( pxHeadListToMerge )->pxNext; \ + /* Empty the merged list. */ \ + listINIT_HEAD( ( pxHeadListToMerge ) ); \ + } \ + } + +/** + * @brief Helper macro to iterate over a list. pxLink contains the link node + * in each iteration. + */ +#define listFOR_EACH( pxLink, pxHead ) \ + for( ( pxLink ) = ( pxHead )->pxNext; \ + ( pxLink ) != ( pxHead ); \ + ( pxLink ) = ( pxLink )->pxNext ) + +/** + * @brief Helper macro to iterate over a list. It is safe to destroy/free the + * nodes while iterating. pxLink contains the link node in each iteration. + */ +#define listFOR_EACH_SAFE( pxLink, pxTempLink, pxHead ) \ + for( ( pxLink ) = ( pxHead )->pxNext, ( pxTempLink ) = ( pxLink )->pxNext; \ + ( pxLink ) != ( pxHead ); \ + ( pxLink ) = ( pxTempLink ), ( pxTempLink ) = ( pxLink )->pxNext ) + +/** + * @brief Given the pointer to the link member (of type Link_t) in a struct, + * extracts the pointer to the containing struct. + * + * @param[in] pxLink The pointer to the link member. + * @param[in] type The type of the containing struct. + * @param[in] member Name of the link member in the containing struct. + */ +#define listCONTAINER( pxLink, type, member ) ( ( type * ) ( ( uint8_t * ) ( pxLink ) - ( uint8_t * ) ( &( ( type * ) 0 )->member ) ) ) + +#endif /* _AWS_DOUBLY_LINKED_LIST_H_ */ |
