├── .gitignore ├── Quick_Start_Guide.url ├── FreeRTOS+TCP.url ├── Upgrading to FreeRTOS V10.3.0.url ├── FreeRTOS ├── links_to_doc_pages_for_the_demo_projects.url └── readme.txt ├── FreeRTOS-Plus └── Demo │ └── FreeRTOS_Plus_PKCS11_Windows_Simulator │ ├── READ_ME_INSTRUCTIONS.url │ ├── pkcs11_demos.sln │ ├── pkcs11_demo_config.h │ ├── examples │ ├── pkcs11_demos.h │ ├── demo_helpers.h │ ├── management_and_rng.c │ ├── mechanisms_and_digests.c │ └── demo_helpers.c │ ├── logging │ ├── logging_levels.h │ └── logging_stack.h │ ├── iot_pkcs11_config.h │ ├── FreeRTOSConfig.h │ └── main.c ├── FreeRTOS-Labs ├── Demo │ ├── FreeRTOS_Plus_FAT_Windows_Simulator │ │ ├── WIN32.vcxproj.user │ │ ├── FreeRTOS_Plus_FAT.sln │ │ ├── Run-time-stats-utils.c │ │ ├── FreeRTOSConfig.h │ │ ├── WIN32.vcxproj.filters │ │ ├── main.c │ │ ├── WIN32.vcxproj │ │ ├── FreeRTOSFATConfig.h │ │ └── printf-stdarg.c │ └── FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator │ │ ├── readme.txt │ │ ├── FreeRTOS_Plus_POSIX_with_actor.sln │ │ ├── posix_demo.h │ │ ├── FreeRTOSConfig.h │ │ ├── main.c │ │ ├── WIN32.vcxproj.filters │ │ ├── WIN32.vcxproj │ │ └── posix_demo.c └── readme.txt ├── .gitmodules ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Visual studio related. 2 | **/Debug 3 | **/.vs -------------------------------------------------------------------------------- /Quick_Start_Guide.url: -------------------------------------------------------------------------------- 1 | [InternetShortcut] 2 | URL=http://www.freertos.org/FreeRTOS-quick-start-guide.html 3 | IDList= 4 | [{000214A0-0000-0000-C000-000000000046}] 5 | Prop3=19,2 6 | -------------------------------------------------------------------------------- /FreeRTOS+TCP.url: -------------------------------------------------------------------------------- 1 | [{000214A0-0000-0000-C000-000000000046}] 2 | Prop3=19,2 3 | [InternetShortcut] 4 | URL=http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/index.html 5 | IDList= 6 | -------------------------------------------------------------------------------- /Upgrading to FreeRTOS V10.3.0.url: -------------------------------------------------------------------------------- 1 | [{000214A0-0000-0000-C000-000000000046}] 2 | Prop3=19,11 3 | [InternetShortcut] 4 | IDList= 5 | URL=https://www.freertos.org/FreeRTOS-V10.3.x.html 6 | -------------------------------------------------------------------------------- /FreeRTOS/links_to_doc_pages_for_the_demo_projects.url: -------------------------------------------------------------------------------- 1 | [{000214A0-0000-0000-C000-000000000046}] 2 | Prop3=19,2 3 | [InternetShortcut] 4 | URL=http://www.freertos.org/a00090.html 5 | IDList= 6 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/READ_ME_INSTRUCTIONS.url: -------------------------------------------------------------------------------- 1 | [{000214A0-0000-0000-C000-000000000046}] 2 | Prop3=19,11 3 | [InternetShortcut] 4 | IDList= 5 | URL=https://www.freertos.org/pkcs11/ 6 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/WIN32.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/readme.txt: -------------------------------------------------------------------------------- 1 | Directories: 2 | 3 | + FreeRTOS-Plus\Demo\FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator 4 | contains a FreeRTOS windows simulator demo project for FreeRTOS+POSIX. 5 | See http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_POSIX/ for information about this project. 6 | 7 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "FreeRTOS-Labs/Source/FreeRTOS-Plus-FAT"] 2 | path = FreeRTOS-Labs/Source/FreeRTOS-Plus-FAT 3 | url = https://github.com/FreeRTOS/Lab-Project-FreeRTOS-FAT.git 4 | [submodule "FreeRTOS-Labs/Source/FreeRTOS-Plus-POSIX"] 5 | path = FreeRTOS-Labs/Source/FreeRTOS-Plus-POSIX 6 | url = https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX.git 7 | [submodule "FreeRTOS/Source"] 8 | path = FreeRTOS/Source 9 | url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git 10 | [submodule "FreeRTOS-Plus/Source/FreeRTOS-Plus-PKCS/3rdparty/mbedtls"] 11 | path = FreeRTOS-Plus/Source/FreeRTOS-Plus-PKCS/3rdparty/mbedtls 12 | url = https://github.com/ARMmbed/mbedtls.git 13 | [submodule "FreeRTOS-Plus/Source/FreeRTOS-Plus-PKCS/3rdparty/pkcs11"] 14 | path = FreeRTOS-Plus/Source/FreeRTOS-Plus-PKCS/3rdparty/pkcs11 15 | url = https://github.com/amazon-freertos/pkcs11.git 16 | [submodule "FreeRTOS-Plus/Source/FreeRTOS-PKCS"] 17 | path = FreeRTOS-Plus/Source/FreeRTOS-PKCS 18 | url = https://github.com/FreeRTOS/FreeRTOS-PKCS 19 | -------------------------------------------------------------------------------- /FreeRTOS/readme.txt: -------------------------------------------------------------------------------- 1 | Directories: 2 | 3 | + The FreeRTOS/Source directory contains the FreeRTOS source code, and contains 4 | its own readme file. 5 | 6 | + The FreeRTOS/Demo directory contains a demo application for every official 7 | FreeRTOS port, and contains its own readme file. 8 | 9 | + See http://www.freertos.org/a00017.html for full details of the directory 10 | structure and information on locating the files you require. 11 | 12 | The easiest way to use FreeRTOS is to start with one of the pre-configured demo 13 | application projects (found in the FreeRTOS/Demo directory). That way you will 14 | have the correct FreeRTOS source files included, and the correct include paths 15 | configured. Once a demo application is building and executing you can remove 16 | the demo application file, and start to add in your own application source 17 | files. 18 | 19 | See also - 20 | https://freertos.org/Documentation/01-FreeRTOS-quick-start/01-Beginners-guide/02-Quick-start-guide 21 | https://www.freertos.org/Why-FreeRTOS/FAQs 22 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/FreeRTOS_Plus_FAT.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RTOSDemo", "WIN32.vcxproj", "{C686325E-3261-42F7-AEB1-DDE5280E1CEB}" 5 | EndProject 6 | Global 7 | GlobalSection(TestCaseManagementSettings) = postSolution 8 | CategoryFile = FreeRTOS_Plus_TCP_with_CLI.vsmdi 9 | EndGlobalSection 10 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 11 | Debug|Win32 = Debug|Win32 12 | Release|Win32 = Release|Win32 13 | EndGlobalSection 14 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 15 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|Win32.ActiveCfg = Debug|Win32 16 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|Win32.Build.0 = Debug|Win32 17 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Release|Win32.ActiveCfg = Release|Win32 18 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Release|Win32.Build.0 = Release|Win32 19 | EndGlobalSection 20 | GlobalSection(SolutionProperties) = preSolution 21 | HideSolutionNode = FALSE 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 FreeRTOS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/pkcs11_demos.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29215.179 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RTOSDemo", "WIN32.vcxproj", "{C686325E-3261-42F7-AEB1-DDE5280E1CEB}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | EndGlobalSection 12 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 13 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|Win32.ActiveCfg = Debug|Win32 14 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|Win32.Build.0 = Debug|Win32 15 | EndGlobalSection 16 | GlobalSection(SolutionProperties) = preSolution 17 | HideSolutionNode = FALSE 18 | EndGlobalSection 19 | GlobalSection(ExtensibilityGlobals) = postSolution 20 | SolutionGuid = {150F08BF-9D61-4CC2-8DBF-1335172A1EA4} 21 | EndGlobalSection 22 | GlobalSection(TestCaseManagementSettings) = postSolution 23 | CategoryFile = FreeRTOS_Plus_TCP_Minimal.vsmdi 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/FreeRTOS_Plus_POSIX_with_actor.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28010.2050 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RTOS_POSIX_Demo", "WIN32.vcxproj", "{C686325E-3261-42F7-AEB1-DDE5280E1CEB}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|Win32.Build.0 = Debug|Win32 16 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Release|Win32.ActiveCfg = Release|Win32 17 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {18112148-F9F0-43E0-B713-B146289BDEF3} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/posix_demo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.0.1 3 | * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef _POSIX_DEMO_H_ 29 | #define _POSIX_DEMO_H_ 30 | void vStartPOSIXDemo( void * pvParameters ); 31 | 32 | #endif /* _POSIX_DEMO_H_ */ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## FreeRTOS-Labs 2 | This repository contains libraries and demos that are fully functional, but may be experimental or undergoing optimizations and refactorization to improve memory usage, modularity, documentation, demo usability, or test coverage. 3 | 4 | In this repository, we provide two demo projects: 5 | 6 | - FAT file system demo: 7 | 8 | ``` 9 | FreeRTOS-Labs\Demo\FreeRTOS_Plus_FAT_Windows_Simulator\FreeRTOS_Plus_FAT.sln 10 | ``` 11 | 12 | - POSIX threading API demo: 13 | 14 | ``` 15 | FreeRTOS-Labs\Demo\FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator\FreeRTOS_Plus_POSIX_with_actor.sln 16 | ``` 17 | 18 | 19 | ## Cloning this repository 20 | This repo uses [Git Submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to bring in dependent components. 21 | 22 | Note: If you download the ZIP file provided by GitHub UI, you will not get the contents of the submodules. (The ZIP file is also not a valid git repository) 23 | 24 | To clone using HTTPS: 25 | ``` 26 | git clone https://github.com/FreeRTOS/FreeRTOS-Labs.git --recurse-submodules 27 | ``` 28 | Using SSH: 29 | ``` 30 | git clone git@github.com:FreeRTOS/FreeRTOS-Labs.git --recurse-submodules 31 | ``` 32 | 33 | If you have downloaded the repo without using the `--recurse-submodules` argument, you need to run: 34 | ``` 35 | git submodule update --init --recursive 36 | ``` 37 | 38 | 39 | ## For official FreeRTOS releases 40 | https://github.com/FreeRTOS/FreeRTOS 41 | 42 | 43 | ## Getting help 44 | If you have any questions or need assistance troubleshooting your FreeRTOS project, we have an active community that can help on the [FreeRTOS Community Support Forum](https://forums.freertos.org). 45 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/readme.txt: -------------------------------------------------------------------------------- 1 | *** IMPORTANT NOTE *** 2 | 3 | FreeRTOS-Labs contains libraries and demos that are fully functional, but 4 | undergoing optimizations or refactoring to improve memory usage, modularity, 5 | documentation, demo usability, or test coverage. At this time the projects ARE 6 | A WORK IN PROGRESS and will be released in the main FreeRTOS directories of the 7 | download following full review and completion of the documentation. 8 | 9 | Please note, the FreeRTOS IoT libraries (MQTT, HTTPS, AWS IoT Device Shadow, 10 | and AWS IoT Jobs have been moved to a different location. They can be found 11 | at https://github.com/freertos/freertos/tree/lts-development. 12 | 13 | *** INTRODUCTION *** 14 | 15 | This distribution currently contains demos from the FreeRTOS Plus Posix 16 | and FreeRTOS Plus FAT libraries. 17 | 18 | The pre-configured projects use the FreeRTOS kernel Windows port (often 19 | called the Windows simulator) to enable their evaluation using the free Visual 20 | Studio tools and without needing specific microcontroller hardware. 21 | 22 | 23 | 24 | *** INSTRUCTIONS *** 25 | 26 | Instructions for configuring and using the FreeRTOS-Labs Libraries 27 | following links: 28 | 29 | 30 | + https://www.freertos.org/mqtt/preconfiguredexamplesLW.html 31 | + https://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_POSIX/index.html 32 | + /FreeRTOS-Plus/FreeRTOS_Plus_FAT/index.html 33 | 34 | 35 | *** LOCATING THE EXAMPLE PROJECTS *** 36 | 37 | The Visual Studio projects for each of the FreeRTOS-Labs examples are 38 | located in sub-directories of the following top-level directories: 39 | 40 | + https://www.FreeRTOS.org/mqtt/preconfiguredexamplesLW.html 41 | + https://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_POSIX/demo/posix_demo.html 42 | + https://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_FAT_demo_projects.html 43 | 44 | 45 | 46 | *** ADDITIONAL INFORMATION *** 47 | 48 | See http://www.freertos.org/a00017.html for full details of the FreeRTOS 49 | directory structure 50 | 51 | See also - 52 | https://freertos.org/Documentation/01-FreeRTOS-quick-start/01-Beginners-guide/02-Quick-start-guide 53 | https://www.freertos.org/Why-FreeRTOS/FAQs 54 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/pkcs11_demo_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS PKCS #11 V1.0.3 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | 26 | #ifndef _PKCS11_DEMO_CONFIG_ 27 | #define _PKCS11_DEMO_CONFIG_ 28 | 29 | /* 30 | * @brief this macro defines the stack size for the PKCS #11 demo task. 31 | */ 32 | #define configPKCS11_DEMO_STACK_SIZE 200 33 | 34 | /* 35 | * @brief set this macro to "1" in order to run the PKCS #11 management and 36 | * random number generator demo. 37 | */ 38 | #define configPKCS11_MANAGEMENT_AND_RNG_DEMO 1 39 | 40 | /* 41 | * @brief set this macro to "1" in order to run the PKCS #11 mechanisms and 42 | * digest demo. 43 | */ 44 | #define configPKCS11_MECHANISMS_AND_DIGESTS_DEMO 0 45 | 46 | /* 47 | * @brief set this macro to "1" in order to run the PKCS #11 object demo. 48 | */ 49 | #define configPKCS11_OBJECT_DEMO 0 50 | 51 | /* 52 | * @brief set this macro to "1" in order to run the PKCS #11 sign and verify 53 | * demo. 54 | * 55 | * @warning This demo relies on the objects created in the object demo. 56 | */ 57 | #define configPKCS11_SIGN_AND_VERIFY_DEMO 0 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/examples/pkcs11_demos.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS PKCS #11 V1.0.3 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | #ifndef _PKCS11_DEMOS_h_ 26 | #define _PKCS11_DEMOS_h_ 27 | 28 | /* Prototype for the PKCS #11 "Management" demo. This demo covers the various 29 | * functions used to manage the internal state of the PKCS #11 stack, and then 30 | * demonstrates how to generate random numbers using PKCS #11. 31 | */ 32 | void vPKCS11ManagementAndRNGDemo( void ); 33 | 34 | /* Prototype for the PKCS #11 "Digests" demo. This demo covers how to query 35 | * slots for supported capabilities, and creating a message digest if the 36 | * slot supports it. 37 | */ 38 | void vPKCS11MechanismsAndDigestDemo( void ); 39 | 40 | /* Prototype for the PKCS #11 "Object" demo. This demo covers objects and how 41 | * they are defined and used within PKCS #11. 42 | */ 43 | void vPKCS11ObjectDemo( void ); 44 | 45 | /* Prototype for the PKCS #11 "Sign and Verify" demo. This demo covers how 46 | * PKCS #11 can be used to sign a message, and verify the integrity of a message 47 | * using private and public keys. 48 | * 49 | * This demo will also cover the "iot_pkcs11.h" functions, and how they can be 50 | * used to make the PKCS #11 flow easier to use. 51 | * 52 | * Warning: This demo depends on the objects created in the objects demo. 53 | */ 54 | void vPKCS11SignVerifyDemo( void ); 55 | 56 | #endif /* _PKCS11_DEMOS_h_ */ 57 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/logging/logging_levels.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Logging Level Macros 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | /** 24 | * @file logging_levels.h 25 | * @brief Defines the logging level macros. 26 | */ 27 | 28 | #ifndef LOGGING_LEVELS_H_ 29 | #define LOGGING_LEVELS_H_ 30 | 31 | /** 32 | * @constantspage{logging,logging library} 33 | * 34 | * @section logging_constants_levels Log levels 35 | * @brief Log levels for the libraries in this SDK. 36 | * 37 | * Each library should specify a log level by setting @ref LIBRARY_LOG_LEVEL. 38 | * All log messages with a level at or below the specified level will be printed 39 | * for that library. 40 | * 41 | * Currently, there are 4 log levels. In the order of lowest to highest, they are: 42 | * - #LOG_NONE
43 | * @copybrief LOG_NONE 44 | * - #LOG_ERROR
45 | * @copybrief LOG_ERROR 46 | * - #LOG_WARN
47 | * @copybrief LOG_WARN 48 | * - #LOG_INFO
49 | * @copybrief LOG_INFO 50 | * - #LOG_DEBUG
51 | * @copybrief LOG_DEBUG 52 | */ 53 | 54 | /** 55 | * @brief No log messages. 56 | * 57 | * When @ref LIBRARY_LOG_LEVEL is #LOG_NONE, logging is disabled and no 58 | * logging messages are printed. 59 | */ 60 | #define LOG_NONE 0 61 | 62 | /** 63 | * @brief Represents erroneous application state or event. 64 | * 65 | * These messages describe the situations when a library encounters an error from 66 | * which it cannot recover. 67 | * 68 | * These messages are printed when @ref LIBRARY_LOG_LEVEL is defined as either 69 | * of #LOG_ERROR, #LOG_WARN, #LOG_INFO or #LOG_DEBUG. 70 | */ 71 | #define LOG_ERROR 1 72 | 73 | /** 74 | * @brief Message about an abnormal event. 75 | * 76 | * These messages describe the situations when a library encounters 77 | * abnormal event that may be indicative of an error. Libraries continue 78 | * execution after logging a warning. 79 | * 80 | * These messages are printed when @ref LIBRARY_LOG_LEVEL is defined as either 81 | * of #LOG_WARN, #LOG_INFO or #LOG_DEBUG. 82 | */ 83 | #define LOG_WARN 2 84 | 85 | /** 86 | * @brief A helpful, informational message. 87 | * 88 | * These messages describe normal execution of a library. They provide 89 | * the progress of the program at a coarse-grained level. 90 | * 91 | * These messages are printed when @ref LIBRARY_LOG_LEVEL is defined as either 92 | * of #LOG_INFO or #LOG_DEBUG. 93 | */ 94 | #define LOG_INFO 3 95 | 96 | /** 97 | * @brief Detailed and excessive debug information. 98 | * 99 | * Debug log messages are used to provide the 100 | * progress of the program at a fine-grained level. These are mostly used 101 | * for debugging and may contain excessive information such as internal 102 | * variables, buffers, or other specific information. 103 | * 104 | * These messages are only printed when @ref LIBRARY_LOG_LEVEL is defined as 105 | * #LOG_DEBUG. 106 | */ 107 | #define LOG_DEBUG 4 108 | 109 | #endif /* ifndef LOGGING_LEVELS_H_ */ 110 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/examples/demo_helpers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS PKCS #11 V1.0.3 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | #ifndef _DEMO_HELPER_FUNCTIONS_ 26 | #define _DEMO_HELPER_FUNCTIONS_ 27 | 28 | #include "iot_pkcs11.h" 29 | #include "threading_alt.h" 30 | #include "mbedtls/pk.h" 31 | 32 | /* This function contains standard setup code for PKCS #11. See the 33 | * "management_and_rng.c" file for the demo code explaining this section 34 | * of cryptoki. 35 | */ 36 | void vStart( CK_SESSION_HANDLE * pxSession, 37 | CK_SLOT_ID ** ppxSlotId ); 38 | /*-----------------------------------------------------------*/ 39 | 40 | /* This function contains standard tear down code for PKCS #11. See the 41 | * "management_and_rng.c" file for the demo code explaining this section 42 | * of cryptoki. 43 | */ 44 | void vEnd( CK_SESSION_HANDLE xSession, 45 | CK_SLOT_ID * pxSlotId ); 46 | /*-----------------------------------------------------------*/ 47 | 48 | /* This function is simply a helper function to print the raw hex values 49 | * of an EC public key. It's explanation is not within the scope of the demos 50 | * and is sparsely commented. */ 51 | void vWriteHexBytesToConsole( char * pcDescription, 52 | CK_BYTE * pucData, 53 | CK_ULONG ulDataLength ); 54 | /*-----------------------------------------------------------*/ 55 | 56 | /* This function is simply a helper function to export the raw hex values 57 | * of an EC public key into a buffer. It's explanation is not within the 58 | * scope of the demos and is sparsely commented. */ 59 | CK_RV vExportPublicKey( CK_SESSION_HANDLE xSession, 60 | CK_OBJECT_HANDLE xPublicKeyHandle, 61 | CK_BYTE ** ppucDerPublicKey, 62 | CK_ULONG * pulDerPublicKeyLength ); 63 | /*-----------------------------------------------------------*/ 64 | 65 | /** 66 | * @brief Implements libc calloc semantics using the FreeRTOS heap 67 | */ 68 | void * pvCalloc( size_t xNumElements, 69 | size_t xSize ); 70 | /*-----------------------------------------------------------*/ 71 | 72 | /** 73 | * @brief Implementation of mbedtls_mutex_init for thread-safety. 74 | * 75 | */ 76 | void aws_mbedtls_mutex_init( mbedtls_threading_mutex_t * mutex ); 77 | /*-----------------------------------------------------------*/ 78 | 79 | /** 80 | * @brief Implementation of mbedtls_mutex_free for thread-safety. 81 | * 82 | */ 83 | void aws_mbedtls_mutex_free( mbedtls_threading_mutex_t * mutex ); 84 | /*-----------------------------------------------------------*/ 85 | 86 | /** 87 | * @brief Implementation of mbedtls_mutex_lock for thread-safety. 88 | * 89 | * @return 0 if successful, MBEDTLS_ERR_THREADING_MUTEX_ERROR if timeout, 90 | * MBEDTLS_ERR_THREADING_BAD_INPUT_DATA if the mutex is not valid. 91 | */ 92 | int aws_mbedtls_mutex_lock( mbedtls_threading_mutex_t * mutex ); 93 | /*-----------------------------------------------------------*/ 94 | 95 | /** 96 | * @brief Implementation of mbedtls_mutex_unlock for thread-safety. 97 | * 98 | * @return 0 if successful, MBEDTLS_ERR_THREADING_MUTEX_ERROR if timeout, 99 | * MBEDTLS_ERR_THREADING_BAD_INPUT_DATA if the mutex is not valid. 100 | */ 101 | int aws_mbedtls_mutex_unlock( mbedtls_threading_mutex_t * mutex ); 102 | 103 | #endif /* _DEMO_HELPER_FUNCTIONS_ */ 104 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/FreeRTOSConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.0.1 3 | * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | 29 | #ifndef FREERTOS_CONFIG_H 30 | #define FREERTOS_CONFIG_H 31 | 32 | #include 33 | 34 | /*----------------------------------------------------------- 35 | * Application specific definitions. 36 | * 37 | * These definitions should be adjusted for your particular hardware and 38 | * application requirements. 39 | * 40 | * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE 41 | * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. 42 | * http://www.freertos.org/a00110.html 43 | *----------------------------------------------------------*/ 44 | 45 | #define configUSE_PREEMPTION 1 46 | #define configTICK_RATE_HZ ( 1000 ) /* In this non-real time simulated environment the tick frequency has to be at least a multiple of the Win32 tick frequency, and therefore very slow. */ 47 | #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 60 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the Win32 thread. */ 48 | #define configMAX_TASK_NAME_LEN ( 7 ) 49 | #define configUSE_TRACE_FACILITY 0 50 | #define configUSE_16_BIT_TICKS 0 51 | #define configIDLE_SHOULD_YIELD 1 52 | #define configUSE_CO_ROUTINES 0 53 | #define configUSE_MUTEXES 1 54 | #define configCHECK_FOR_STACK_OVERFLOW 0 /* Not applicable to the Win32 port. */ 55 | #define configUSE_RECURSIVE_MUTEXES 1 56 | #define configQUEUE_REGISTRY_SIZE 0 57 | #define configUSE_APPLICATION_TASK_TAG 1 58 | #define configUSE_COUNTING_SEMAPHORES 1 59 | #define configUSE_ALTERNATIVE_API 0 60 | 61 | #define configUSE_TIMERS 1 62 | #define configTIMER_TASK_PRIORITY 2 63 | #define configTIMER_QUEUE_LENGTH 20 64 | #define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 ) 65 | 66 | #define configMAX_PRIORITIES ( 7 ) 67 | #define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) 68 | 69 | /* Hook functions */ 70 | #define configUSE_IDLE_HOOK 0 71 | #define configUSE_TICK_HOOK 0 72 | #define configUSE_MALLOC_FAILED_HOOK 1 73 | 74 | /* Co-routine definitions. */ 75 | #define configUSE_CO_ROUTINES 0 76 | 77 | /* Set the following definitions to 1 to include the API function, or zero 78 | to exclude the API function. */ 79 | #define INCLUDE_vTaskPrioritySet 1 80 | #define INCLUDE_uxTaskPriorityGet 1 81 | #define INCLUDE_vTaskDelete 1 82 | #define INCLUDE_vTaskCleanUpResources 0 83 | #define INCLUDE_vTaskSuspend 1 84 | #define INCLUDE_vTaskDelayUntil 1 85 | #define INCLUDE_vTaskDelay 1 86 | #define INCLUDE_uxTaskGetStackHighWaterMark 1 87 | #define INCLUDE_xTaskGetSchedulerState 1 88 | #define INCLUDE_xTimerGetTimerTaskHandle 0 89 | #define INCLUDE_xTaskGetIdleTaskHandle 0 90 | #define INCLUDE_xQueueGetMutexHolder 1 91 | #define INCLUDE_eTaskGetState 1 92 | #define INCLUDE_xEventGroupSetBitsFromISR 1 93 | #define INCLUDE_xTimerPendFunctionCall 1 94 | #define INCLUDE_xTaskGetCurrentTaskHandle 1 95 | #define INCLUDE_xTaskAbortDelay 1 96 | 97 | /* heap_4.c, */ 98 | #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 2048U * 1024U ) ) 99 | 100 | /* Allow RTOS objects to be created using RAM provided by the application writer. */ 101 | #define configSUPPORT_STATIC_ALLOCATION 1 102 | 103 | /* Create RTOS objects using dynamically allocated RAM */ 104 | #define configSUPPORT_DYNAMIC_ALLOCATION 1 105 | 106 | /* This demo makes use of one or more example stats formatting functions. These 107 | format the raw data provided by the uxTaskGetSystemState() function in to human 108 | readable ASCII form. See the notes in the implementation of vTaskList() within 109 | FreeRTOS/Source/tasks.c for limitations. */ 110 | #define configUSE_STATS_FORMATTING_FUNCTIONS 1 111 | 112 | /* Run time stats gathering definitions. */ 113 | #define configGENERATE_RUN_TIME_STATS 0 114 | 115 | /* Assert call defined for debug builds. */ 116 | extern void vAssertCalled(const char * pcFile, uint32_t ulLine); 117 | #define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ ) 118 | 119 | #endif /* FREERTOS_CONFIG_H */ 120 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/logging/logging_stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Logging Stack V1.0.0 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | /** 24 | * @file logging_stack.h 25 | * @brief Reference implementation of Logging stack as a header-only library. 26 | */ 27 | 28 | #ifndef LOGGING_STACK_H_ 29 | #define LOGGING_STACK_H_ 30 | 31 | /* Include header for logging level macros. */ 32 | #include "logging_levels.h" 33 | 34 | /* FreeRTOS Include. */ 35 | #include "FreeRTOS.h" 36 | 37 | /* Standard Include. */ 38 | #include 39 | #include 40 | 41 | /* Metadata information to prepend to every log message. */ 42 | 43 | /* Macro to extract only the file name from file path to use for metadata in 44 | * log messages. */ 45 | #define FILENAME ( strrchr( __FILE__, '/' ) ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ) 46 | 47 | #define LOG_METADATA_FORMAT "[%s:%d] " 48 | #define LOG_METADATA_ARGS FILENAME, __LINE__ 49 | 50 | /* Common macro for all logging interface macros. */ 51 | #if !defined( DISABLE_LOGGING ) 52 | #define SdkLog( string ) printf string 53 | #else 54 | #define SdkLog( string ) 55 | #endif 56 | 57 | /* Check that LIBRARY_LOG_LEVEL is defined and has a valid value. */ 58 | #if !defined( LIBRARY_LOG_LEVEL ) || \ 59 | ( ( LIBRARY_LOG_LEVEL != LOG_NONE ) && \ 60 | ( LIBRARY_LOG_LEVEL != LOG_ERROR ) && \ 61 | ( LIBRARY_LOG_LEVEL != LOG_WARN ) && \ 62 | ( LIBRARY_LOG_LEVEL != LOG_INFO ) && \ 63 | ( LIBRARY_LOG_LEVEL != LOG_DEBUG ) ) 64 | #error "Please define LIBRARY_LOG_LEVEL as either LOG_NONE, LOG_ERROR, LOG_WARN, LOG_INFO, or LOG_DEBUG." 65 | #elif !defined( LIBRARY_LOG_NAME ) 66 | #error "Please define LIBRARY_LOG_NAME for the library." 67 | #else 68 | #if LIBRARY_LOG_LEVEL == LOG_DEBUG 69 | /* All log level messages will logged. */ 70 | #define LogError( message ) SdkLog( ( "[ERROR] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 71 | #define LogWarn( message ) SdkLog( ( "[WARN] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 72 | #define LogInfo( message ) SdkLog( ( "[INFO] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 73 | #define LogDebug( message ) SdkLog( ( "[DEBUG] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 74 | 75 | #elif LIBRARY_LOG_LEVEL == LOG_INFO 76 | /* Only INFO, WARNING and ERROR messages will be logged. */ 77 | #define LogError( message ) SdkLog( ( "[ERROR] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 78 | #define LogWarn( message ) SdkLog( ( "[WARN] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 79 | #define LogInfo( message ) SdkLog( ( "[INFO] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 80 | #define LogDebug( message ) 81 | 82 | #elif LIBRARY_LOG_LEVEL == LOG_WARN 83 | /* Only WARNING and ERROR messages will be logged.*/ 84 | #define LogError( message ) SdkLog( ( "[ERROR] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 85 | #define LogWarn( message ) SdkLog( ( "[WARN] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 86 | #define LogInfo( message ) 87 | #define LogDebug( message ) 88 | 89 | #elif LIBRARY_LOG_LEVEL == LOG_ERROR 90 | /* Only ERROR messages will be logged. */ 91 | #define LogError( message ) SdkLog( ( "[ERROR] [%s] "LOG_METADATA_FORMAT, LIBRARY_LOG_NAME, LOG_METADATA_ARGS ) ); SdkLog( message ); SdkLog( ( "\r\n" ) ) 92 | #define LogWarn( message ) 93 | #define LogInfo( message ) 94 | #define LogDebug( message ) 95 | 96 | #else /* if LIBRARY_LOG_LEVEL == LOG_ERROR */ 97 | 98 | #define LogError( message ) 99 | #define LogWarn( message ) 100 | #define LogInfo( message ) 101 | #define LogDebug( message ) 102 | 103 | #endif /* if LIBRARY_LOG_LEVEL == LOG_ERROR */ 104 | #endif /* if !defined( LIBRARY_LOG_LEVEL ) || ( ( LIBRARY_LOG_LEVEL != LOG_NONE ) && ( LIBRARY_LOG_LEVEL != LOG_ERROR ) && ( LIBRARY_LOG_LEVEL != LOG_WARN ) && ( LIBRARY_LOG_LEVEL != LOG_INFO ) && ( LIBRARY_LOG_LEVEL != LOG_DEBUG ) ) */ 105 | 106 | #endif /* ifndef LOGGING_STACK_H_ */ 107 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/iot_pkcs11_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS V1.4.8 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | 26 | /** 27 | * @file iot_pkcs11_config.h 28 | * @brief PCKS#11 config options. 29 | */ 30 | 31 | 32 | #ifndef _IOT_PKCS11_CONFIG_H_ 33 | #define _IOT_PKCS11_CONFIG_H_ 34 | 35 | #include "FreeRTOS.h" 36 | 37 | /**************************************************/ 38 | /******* DO NOT CHANGE the following order ********/ 39 | /**************************************************/ 40 | 41 | /* Include logging header files and define logging macros in the following order: 42 | * 1. Include the header file "logging_levels.h". 43 | * 2. Define the LIBRARY_LOG_NAME and LIBRARY_LOG_LEVEL macros depending on 44 | * the logging configuration for PKCS #11. 45 | * 3. Include the header file "logging_stack.h", if logging is enabled for PKCS #11. 46 | */ 47 | 48 | #include "logging_levels.h" 49 | 50 | /* Logging configuration for the PKCS #11 library. */ 51 | #ifndef LIBRARY_LOG_NAME 52 | #define LIBRARY_LOG_NAME "PKCS11" 53 | #endif 54 | 55 | #ifndef LIBRARY_LOG_LEVEL 56 | #define LIBRARY_LOG_LEVEL LOG_ERROR 57 | #endif 58 | 59 | #include "logging_stack.h" 60 | 61 | /** 62 | * @brief Malloc API used by iot_pkcs11.h 63 | */ 64 | #define PKCS11_MALLOC pvPortMalloc 65 | 66 | /** 67 | * @brief Free API used by iot_pkcs11.h 68 | */ 69 | #define PKCS11_FREE vPortFree 70 | 71 | /** 72 | * @brief PKCS #11 default user PIN. 73 | * 74 | * The PKCS #11 standard specifies the presence of a user PIN. That feature is 75 | * sensible for applications that have an interactive user interface and memory 76 | * protections. However, since typical microcontroller applications lack one or 77 | * both of those, the user PIN is assumed to be used herein for interoperability 78 | * purposes only, and not as a security feature. 79 | * 80 | * Note: Do not cast this to a pointer! The library calls sizeof to get the length 81 | * of this string. 82 | */ 83 | #define configPKCS11_DEFAULT_USER_PIN "0000" 84 | 85 | /** 86 | * @brief Maximum length (in characters) for a PKCS #11 CKA_LABEL 87 | * attribute. 88 | */ 89 | #define pkcs11configMAX_LABEL_LENGTH 32 90 | 91 | /** 92 | * @brief Maximum number of token objects that can be stored 93 | * by the PKCS #11 module. 94 | */ 95 | #define pkcs11configMAX_NUM_OBJECTS 6 96 | 97 | /** 98 | * @brief Maximum number of sessions that can be stored 99 | * by the PKCS #11 module. 100 | */ 101 | #define pkcs11configMAX_SESSIONS 10 102 | 103 | /** 104 | * @brief Set to 1 if a PAL destroy object is implemented. 105 | * 106 | * If set to 0, no PAL destroy object is implemented, and this functionality 107 | * is implemented in the common PKCS #11 layer. 108 | */ 109 | #define pkcs11configPAL_DESTROY_SUPPORTED 0 110 | 111 | /** 112 | * @brief Set to 1 if OTA image verification via PKCS #11 module is supported. 113 | * 114 | * If set to 0, OTA code signing certificate is built in via 115 | * aws_ota_codesigner_certificate.h. 116 | */ 117 | #define pkcs11configOTA_SUPPORTED 0 118 | 119 | /** 120 | * @brief Set to 1 if PAL supports storage for JITP certificate, 121 | * code verify certificate, and trusted server root certificate. 122 | * 123 | * If set to 0, PAL does not support storage mechanism for these, and 124 | * they are accessed via headers compiled into the code. 125 | */ 126 | #define pkcs11configJITP_CODEVERIFY_ROOT_CERT_SUPPORTED 0 127 | 128 | /** 129 | * @brief The PKCS #11 label for device private key. 130 | * 131 | * Private key for connection to AWS IoT endpoint. The corresponding 132 | * public key should be registered with the AWS IoT endpoint. 133 | */ 134 | #define pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS "Device Priv TLS Key" 135 | 136 | /** 137 | * @brief The PKCS #11 label for device public key. 138 | * 139 | * The public key corresponding to pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS. 140 | */ 141 | #define pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS "Device Pub TLS Key" 142 | 143 | /** 144 | * @brief The PKCS #11 label for the device certificate. 145 | * 146 | * Device certificate corresponding to pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS. 147 | */ 148 | #define pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS "Device Cert" 149 | 150 | /** 151 | * @brief The PKCS #11 label for the object to be used for code verification. 152 | * 153 | * Used by over-the-air update code to verify an incoming signed image. 154 | */ 155 | #define pkcs11configLABEL_CODE_VERIFICATION_KEY "Code Verify Key" 156 | 157 | /** 158 | * @brief The PKCS #11 label for Just-In-Time-Provisioning. 159 | * 160 | * The certificate corresponding to the issuer of the device certificate 161 | * (pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS) when using the JITR or 162 | * JITP flow. 163 | */ 164 | #define pkcs11configLABEL_JITP_CERTIFICATE "JITP Cert" 165 | 166 | /** 167 | * @brief The PKCS #11 label for the AWS Trusted Root Certificate. 168 | * 169 | * @see aws_default_root_certificates.h 170 | */ 171 | #define pkcs11configLABEL_ROOT_CERTIFICATE "Root Cert" 172 | 173 | #endif /* _AWS_PKCS11_CONFIG_H_ include guard. */ 174 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/FreeRTOSConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.3.0 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef FREERTOS_CONFIG_H 29 | #define FREERTOS_CONFIG_H 30 | 31 | /*----------------------------------------------------------- 32 | * Application specific definitions. 33 | * 34 | * These definitions should be adjusted for your particular hardware and 35 | * application requirements. 36 | * 37 | * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE 38 | * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. 39 | * http://www.freertos.org/a00110.html 40 | * 41 | * The bottom of this file contains some constants specific to running the UDP 42 | * stack in this demo. Constants specific to FreeRTOS+TCP itself (rather than 43 | * the demo) are contained in FreeRTOSIPConfig.h. 44 | *----------------------------------------------------------*/ 45 | #define configUSE_PREEMPTION 1 46 | #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 47 | #define configMAX_PRIORITIES ( 7 ) 48 | #define configTICK_RATE_HZ ( 1000 ) /* In this non-real time simulated environment the tick frequency has to be at least a multiple of the Win32 tick frequency, and therefore very slow. */ 49 | #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 60 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the Win32 thread. */ 50 | #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 2048U * 1024U ) ) 51 | #define configMAX_TASK_NAME_LEN ( 15 ) 52 | #define configUSE_TRACE_FACILITY 0 53 | #define configUSE_16_BIT_TICKS 0 54 | #define configIDLE_SHOULD_YIELD 1 55 | #define configUSE_CO_ROUTINES 0 56 | #define configUSE_MUTEXES 1 57 | #define configUSE_RECURSIVE_MUTEXES 1 58 | #define configQUEUE_REGISTRY_SIZE 0 59 | #define configUSE_APPLICATION_TASK_TAG 0 60 | #define configUSE_COUNTING_SEMAPHORES 1 61 | #define configUSE_ALTERNATIVE_API 0 62 | #define configNUM_THREAD_LOCAL_STORAGE_POINTERS 0 63 | #define configENABLE_BACKWARD_COMPATIBILITY 1 64 | #define configSUPPORT_STATIC_ALLOCATION 1 65 | 66 | /* Hook function related definitions. */ 67 | #define configUSE_TICK_HOOK 0 68 | #define configUSE_IDLE_HOOK 0 69 | #define configUSE_MALLOC_FAILED_HOOK 0 70 | #define configCHECK_FOR_STACK_OVERFLOW 0 /* Not applicable to the Win32 port. */ 71 | 72 | /* Software timer related definitions. */ 73 | #define configUSE_TIMERS 1 74 | #define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) 75 | #define configTIMER_QUEUE_LENGTH 5 76 | #define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 ) 77 | 78 | /* Event group related definitions. */ 79 | #define configUSE_EVENT_GROUPS 1 80 | 81 | /* Co-routine definitions. */ 82 | #define configUSE_CO_ROUTINES 0 83 | #define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) 84 | 85 | /* Set the following definitions to 1 to include the API function, or zero 86 | to exclude the API function. */ 87 | #define INCLUDE_vTaskPrioritySet 1 88 | #define INCLUDE_uxTaskPriorityGet 1 89 | #define INCLUDE_vTaskDelete 1 90 | #define INCLUDE_vTaskCleanUpResources 0 91 | #define INCLUDE_vTaskSuspend 1 92 | #define INCLUDE_vTaskDelayUntil 1 93 | #define INCLUDE_vTaskDelay 1 94 | #define INCLUDE_uxTaskGetStackHighWaterMark 1 95 | #define INCLUDE_xTaskGetSchedulerState 1 96 | #define INCLUDE_xTimerGetTimerTaskHandle 0 97 | #define INCLUDE_xTaskGetIdleTaskHandle 0 98 | #define INCLUDE_xQueueGetMutexHolder 1 99 | #define INCLUDE_eTaskGetState 1 100 | #define INCLUDE_xEventGroupSetBitsFromISR 1 101 | #define INCLUDE_xTimerPendFunctionCall 1 102 | #define INCLUDE_pcTaskGetTaskName 1 103 | 104 | /* This demo makes use of one or more example stats formatting functions. These 105 | format the raw data provided by the uxTaskGetSystemState() function in to human 106 | readable ASCII form. See the notes in the implementation of vTaskList() within 107 | FreeRTOS/Source/tasks.c for limitations. configUSE_STATS_FORMATTING_FUNCTIONS 108 | is set to 2 so the formatting functions are included without the stdio.h being 109 | included in tasks.c. That is because this project defines its own sprintf() 110 | functions. */ 111 | #define configUSE_STATS_FORMATTING_FUNCTIONS 1 112 | 113 | /* Assert call defined for debug builds. */ 114 | #ifdef _DEBUG 115 | extern void vAssertCalled( const char *pcFile, uint32_t ulLine ); 116 | #define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ ) 117 | #endif /* _DEBUG */ 118 | 119 | 120 | 121 | /* Application specific definitions follow. **********************************/ 122 | 123 | /* Only used when running in the FreeRTOS Windows simulator. Defines the 124 | priority of the task used to simulate Ethernet interrupts. */ 125 | 126 | #if( defined( _MSC_VER ) && ( _MSC_VER <= 1600 ) && !defined( snprintf ) ) 127 | /* Map to Windows names. */ 128 | #define snprintf _snprintf 129 | #define vsnprintf _vsnprintf 130 | #endif 131 | 132 | /* Visual studio does not have an implementation of strcasecmp(). */ 133 | #define strcasecmp _stricmp 134 | #define strncasecmp _strnicmp 135 | #define strcmpi _strcmpi 136 | 137 | #define configPRINTF( X ) printf X 138 | 139 | #endif /* FREERTOS_CONFIG_H */ 140 | 141 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/Run-time-stats-utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. 3 | All rights reserved 4 | 5 | VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. 6 | 7 | This file is part of the FreeRTOS distribution. 8 | 9 | FreeRTOS is free software; you can redistribute it and/or modify it under 10 | the terms of the GNU General Public License (version 2) as published by the 11 | Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. 12 | 13 | *************************************************************************** 14 | >>! NOTE: The modification to the GPL is included to allow you to !<< 15 | >>! distribute a combined work that includes FreeRTOS without being !<< 16 | >>! obliged to provide the source code for proprietary components !<< 17 | >>! outside of the FreeRTOS kernel. !<< 18 | *************************************************************************** 19 | 20 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY 21 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 22 | FOR A PARTICULAR PURPOSE. Full license text is available on the following 23 | link: http://www.freertos.org/a00114.html 24 | 25 | *************************************************************************** 26 | * * 27 | * FreeRTOS provides completely free yet professionally developed, * 28 | * robust, strictly quality controlled, supported, and cross * 29 | * platform software that is more than just the market leader, it * 30 | * is the industry's de facto standard. * 31 | * * 32 | * Help yourself get started quickly while simultaneously helping * 33 | * to support the FreeRTOS project by purchasing a FreeRTOS * 34 | * tutorial book, reference manual, or both: * 35 | * http://www.FreeRTOS.org/Documentation * 36 | * * 37 | *************************************************************************** 38 | 39 | http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading 40 | the FAQ page "My application does not run, what could be wrong?". Have you 41 | defined configASSERT()? 42 | 43 | http://www.FreeRTOS.org/support - In return for receiving this top quality 44 | embedded software for free we request you assist our global community by 45 | participating in the support forum. 46 | 47 | http://www.FreeRTOS.org/training - Investing in training allows your team to 48 | be as productive as possible as early as possible. Now you can receive 49 | FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers 50 | Ltd, and the world's leading authority on the world's leading RTOS. 51 | 52 | http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, 53 | including FreeRTOS+Trace - an indispensable productivity tool, a DOS 54 | compatible FAT file system, and our tiny thread aware UDP/IP stack. 55 | 56 | http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. 57 | Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. 58 | 59 | http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High 60 | Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS 61 | licenses offer ticketed support, indemnification and commercial middleware. 62 | 63 | http://www.SafeRTOS.com - High Integrity Systems also provide a safety 64 | engineered and independently SIL3 certified version for use in safety and 65 | mission critical applications that require provable dependability. 66 | 67 | 1 tab == 4 spaces! 68 | */ 69 | 70 | /* 71 | * Utility functions required to gather run time statistics. See: 72 | * http://www.freertos.org/rtos-run-time-stats.html 73 | * 74 | * Note that this is a simulated port, where simulated time is a lot slower than 75 | * real time, therefore the run time counter values have no real meaningful 76 | * units. 77 | * 78 | * Also note that it is assumed this demo is going to be used for short periods 79 | * of time only, and therefore timer overflows are not handled. 80 | */ 81 | 82 | /* FreeRTOS includes. */ 83 | #include 84 | 85 | /* Variables used in the creation of the run time stats time base. Run time 86 | stats record how much time each task spends in the Running state. */ 87 | static long long llInitialRunTimeCounterValue = 0LL, llTicksPerHundedthMillisecond = 0LL; 88 | 89 | /*-----------------------------------------------------------*/ 90 | 91 | void vConfigureTimerForRunTimeStats( void ) 92 | { 93 | LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue; 94 | 95 | /* Initialise the variables used to create the run time stats time base. 96 | Run time stats record how much time each task spends in the Running 97 | state. */ 98 | 99 | if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 ) 100 | { 101 | llTicksPerHundedthMillisecond = 1; 102 | } 103 | else 104 | { 105 | /* How many times does the performance counter increment in 1/100th 106 | millisecond. */ 107 | llTicksPerHundedthMillisecond = liPerformanceCounterFrequency.QuadPart / 100000LL; 108 | 109 | /* What is the performance counter value now, this will be subtracted 110 | from readings taken at run time. */ 111 | QueryPerformanceCounter( &liInitialRunTimeValue ); 112 | llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart; 113 | } 114 | } 115 | /*-----------------------------------------------------------*/ 116 | 117 | uint32_t ulGetRunTimeCounterValue( void ) 118 | { 119 | LARGE_INTEGER liCurrentCount; 120 | uint32_t ulReturn; 121 | 122 | /* What is the performance counter value now? */ 123 | QueryPerformanceCounter( &liCurrentCount ); 124 | 125 | /* Subtract the performance counter value reading taken when the 126 | application started to get a count from that reference point, then 127 | scale to (simulated) 1/100ths of a millisecond. */ 128 | if( llTicksPerHundedthMillisecond == 0 ) 129 | { 130 | /* The trace macros can call this function before the kernel has been 131 | started, in which case llTicksPerHundedthMillisecond will not have been 132 | initialised. */ 133 | ulReturn = 0; 134 | } 135 | else 136 | { 137 | ulReturn = ( uint32_t ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llTicksPerHundedthMillisecond ); 138 | } 139 | 140 | return ulReturn; 141 | } 142 | /*-----------------------------------------------------------*/ 143 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS POSIX Demo V1.0.0 3 | * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | /* 29 | ****************************************************************************** 30 | * -NOTE- The Win32 port is a simulation (or is that emulation?) only! Do not 31 | * expect to get real time behaviour from the Win32 port or this demo 32 | * application. It is provided as a convenient development and demonstration 33 | * test bed only. This was tested using Windows 10 with Intel Core i7-6700 CPU. 34 | * 35 | * Windows will not be running the FreeRTOS simulator threads continuously, so 36 | * the timing information has no meaningful units. See the documentation page for 37 | * the Windows simulator for an explanation of the slow timing: 38 | * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html 39 | * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT - 40 | * 41 | * Documentation for this demo can be found on: 42 | * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_POSIX/demo/posix_demo.html 43 | ****************************************************************************** 44 | * 45 | * 46 | * /* FreeRTOS includes. */ 47 | #include "FreeRTOS.h" 48 | #include "task.h" 49 | 50 | /* System headers */ 51 | #include 52 | 53 | /* demo include */ 54 | #include "posix_demo.h" 55 | 56 | /* Demo task priority */ 57 | #define mainPOSIX_DEMO_PRIORITY ( tskIDLE_PRIORITY + 4 ) 58 | 59 | /*-----------------------------------------------------------*/ 60 | 61 | int main( void ) 62 | { 63 | configASSERT( ( mainPOSIX_DEMO_PRIORITY < configMAX_PRIORITIES ) ); 64 | 65 | const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL ); 66 | 67 | printf( "FreeRTOS POSIX demo\n" ); 68 | 69 | /* Start the task to run POSIX demo */ 70 | xTaskCreate( vStartPOSIXDemo, 71 | "posix", 72 | configMINIMAL_STACK_SIZE, 73 | NULL, 74 | mainPOSIX_DEMO_PRIORITY, 75 | NULL ); 76 | 77 | vTaskStartScheduler(); 78 | 79 | /* If all is well, the scheduler will now be running, and the following 80 | * line will never be reached. If the following line does execute, then 81 | * there was insufficient FreeRTOS heap memory available for the idle and/or 82 | * timer tasks to be created. See the memory management section on the 83 | * FreeRTOS web site for more details (this is standard text that is not 84 | * really applicable to the Win32 simulator port). */ 85 | for( ; ; ) 86 | { 87 | Sleep( ulLongTime_ms ); 88 | } 89 | 90 | return 0; 91 | } 92 | 93 | 94 | /*-----------------------------------------------------------*/ 95 | 96 | void vAssertCalled( const char * pcFile, 97 | uint32_t ulLine ) 98 | { 99 | const uint32_t ulLongSleep = 1000UL; 100 | volatile uint32_t ulBlockVariable = 0UL; 101 | volatile char * pcFileName = ( volatile char * ) pcFile; 102 | volatile uint32_t ulLineNumber = ulLine; 103 | 104 | ( void ) pcFileName; 105 | ( void ) ulLineNumber; 106 | 107 | printf( "vAssertCalled %s, %ld\n", pcFile, ( long ) ulLine ); 108 | fflush( stdout ); 109 | 110 | /* Setting ulBlockVariable to a non-zero value in the debugger will allow 111 | * this function to be exited. */ 112 | taskDISABLE_INTERRUPTS(); 113 | { 114 | while( ulBlockVariable == 0UL ) 115 | { 116 | Sleep( ulLongSleep ); 117 | } 118 | } 119 | taskENABLE_INTERRUPTS(); 120 | } 121 | 122 | /*-----------------------------------------------------------*/ 123 | 124 | /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an 125 | * implementation of vApplicationGetIdleTaskMemory() to provide the memory that is 126 | * used by the Idle task. */ 127 | void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer, 128 | StackType_t ** ppxIdleTaskStackBuffer, 129 | uint32_t * pulIdleTaskStackSize ) 130 | { 131 | /* If the buffers to be provided to the Idle task are declared inside this 132 | * function then they must be declared static - otherwise they will be allocated on 133 | * the stack and so not exists after this function exits. */ 134 | static StaticTask_t xIdleTaskTCB; 135 | static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ]; 136 | 137 | /* Pass out a pointer to the StaticTask_t structure in which the Idle 138 | * task's state will be stored. */ 139 | *ppxIdleTaskTCBBuffer = &xIdleTaskTCB; 140 | 141 | /* Pass out the array that will be used as the Idle task's stack. */ 142 | *ppxIdleTaskStackBuffer = uxIdleTaskStack; 143 | 144 | /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. 145 | * Note that, as the array is necessarily of type StackType_t, 146 | * configMINIMAL_STACK_SIZE is specified in words, not bytes. */ 147 | *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; 148 | } 149 | 150 | /*-----------------------------------------------------------*/ 151 | 152 | /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the 153 | * application must provide an implementation of vApplicationGetTimerTaskMemory() 154 | * to provide the memory that is used by the Timer service task. */ 155 | void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer, 156 | StackType_t ** ppxTimerTaskStackBuffer, 157 | uint32_t * pulTimerTaskStackSize ) 158 | { 159 | /* If the buffers to be provided to the Timer task are declared inside this 160 | * function then they must be declared static - otherwise they will be allocated on 161 | * the stack and so not exists after this function exits. */ 162 | static StaticTask_t xTimerTaskTCB; 163 | static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ]; 164 | 165 | /* Pass out a pointer to the StaticTask_t structure in which the Timer 166 | * task's state will be stored. */ 167 | *ppxTimerTaskTCBBuffer = &xTimerTaskTCB; 168 | 169 | /* Pass out the array that will be used as the Timer task's stack. */ 170 | *ppxTimerTaskStackBuffer = uxTimerTaskStack; 171 | 172 | /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer. 173 | * Note that, as the array is necessarily of type StackType_t, 174 | * configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */ 175 | *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; 176 | } 177 | 178 | /*-----------------------------------------------------------*/ 179 | 180 | /** 181 | * @brief Warn user if pvPortMalloc fails. 182 | * 183 | * Called if a call to pvPortMalloc() fails because there is insufficient 184 | * free memory available in the FreeRTOS heap. pvPortMalloc() is called 185 | * internally by FreeRTOS API functions that create tasks, queues, software 186 | * timers, and semaphores. The size of the FreeRTOS heap is set by the 187 | * configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. 188 | * 189 | */ 190 | void vApplicationMallocFailedHook() 191 | { 192 | taskDISABLE_INTERRUPTS(); 193 | 194 | for( ; ; ) 195 | { 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.3.0 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | /*** 29 | * See https://www.FreeRTOS.org/pkcs11/index.html for configuration and usage instructions. 30 | ***/ 31 | 32 | /* Standard includes. */ 33 | #include 34 | 35 | /* Visual studio intrinsics used so the __debugbreak() function is available 36 | * should an assert get hit. */ 37 | #include 38 | 39 | /* FreeRTOS includes. */ 40 | #include 41 | #include "task.h" 42 | 43 | /* Windows Crypto includes. */ 44 | #include 45 | #include 46 | #include "mbedtls/entropy.h" 47 | 48 | /* PKCS #11 Demo includes. */ 49 | #include "demo_helpers.h" 50 | #include "pkcs11_demo_config.h" 51 | #include "pkcs11_demos.h" 52 | 53 | /* 54 | * Private function for starting the various PKCS #11 demos. 55 | * 56 | */ 57 | static void prvStartPKCS11Demo( void ) 58 | { 59 | configPRINTF( ( "---------STARTING DEMO---------\r\n" ) ); 60 | #if ( configPKCS11_MANAGEMENT_AND_RNG_DEMO == 1 ) 61 | vPKCS11ManagementAndRNGDemo(); 62 | #endif 63 | #if ( configPKCS11_MECHANISMS_AND_DIGESTS_DEMO == 1 ) 64 | vPKCS11MechanismsAndDigestDemo(); 65 | #endif 66 | #if ( configPKCS11_OBJECT_DEMO == 1 ) 67 | vPKCS11ObjectDemo(); 68 | #endif 69 | #if ( configPKCS11_SIGN_AND_VERIFY_DEMO == 1 ) 70 | vPKCS11SignVerifyDemo(); 71 | #endif 72 | configPRINTF( ( "---------Finished DEMO---------\r\n" ) ); 73 | } 74 | 75 | /*-----------------------------------------------------------*/ 76 | 77 | int main( void ) 78 | { 79 | configPRINTF( ( "Creating PKCS #11 Demo Task.\r\n" ) ); 80 | BaseType_t xReturned; 81 | TaskHandle_t xHandle = NULL; 82 | 83 | mbedtls_threading_set_alt( aws_mbedtls_mutex_init, 84 | aws_mbedtls_mutex_free, 85 | aws_mbedtls_mutex_lock, 86 | aws_mbedtls_mutex_unlock ); 87 | 88 | /* Create the PKCS #11 demo task. */ 89 | xReturned = xTaskCreate( 90 | ( TaskFunction_t ) prvStartPKCS11Demo, 91 | "PKCS11 Demo", 92 | configPKCS11_DEMO_STACK_SIZE, 93 | NULL, 94 | tskIDLE_PRIORITY + 1, 95 | &xHandle ); 96 | configASSERT( xReturned == pdPASS ); 97 | 98 | /* Start the RTOS scheduler. */ 99 | vTaskStartScheduler(); 100 | 101 | /* If all is well, the scheduler will now be running, and the following 102 | * line will never be reached. If the following line does execute, then 103 | * there was insufficient FreeRTOS heap memory available for the idle and/or 104 | * timer tasks to be created. See the memory management section on the 105 | * FreeRTOS web site for more details (this is standard text that is not 106 | * really applicable to the Win32 simulator port). */ 107 | for( ; ; ) 108 | { 109 | __debugbreak(); 110 | } 111 | } 112 | /*-----------------------------------------------------------*/ 113 | 114 | void vAssertCalled( const char * pcFile, 115 | uint32_t ulLine ) 116 | { 117 | volatile uint32_t ulBlockVariable = 0UL; 118 | volatile char * pcFileName = ( volatile char * ) pcFile; 119 | volatile uint32_t ulLineNumber = ulLine; 120 | 121 | ( void ) pcFileName; 122 | ( void ) ulLineNumber; 123 | 124 | printf( "vAssertCalled( %s, %u\n", pcFile, ulLine ); 125 | 126 | /* Setting ulBlockVariable to a non-zero value in the debugger will allow 127 | * this function to be exited. */ 128 | taskDISABLE_INTERRUPTS(); 129 | { 130 | while( ulBlockVariable == 0UL ) 131 | { 132 | __debugbreak(); 133 | } 134 | } 135 | taskENABLE_INTERRUPTS(); 136 | } 137 | /*-----------------------------------------------------------*/ 138 | 139 | int mbedtls_hardware_poll( void * data, 140 | unsigned char * output, 141 | size_t len, 142 | size_t * olen ) 143 | { 144 | int lStatus = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; 145 | HCRYPTPROV hProv = 0; 146 | 147 | /* Unferenced parameter. */ 148 | ( void ) data; 149 | 150 | /* 151 | * This is port-specific for the Windows simulator, so just use Crypto API. 152 | */ 153 | 154 | if( TRUE == CryptAcquireContextA( 155 | &hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) ) 156 | { 157 | if( TRUE == CryptGenRandom( hProv, len, output ) ) 158 | { 159 | lStatus = 0; 160 | *olen = len; 161 | } 162 | 163 | CryptReleaseContext( hProv, 0 ); 164 | } 165 | 166 | return lStatus; 167 | } 168 | 169 | /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an 170 | * implementation of vApplicationGetIdleTaskMemory() to provide the memory that is 171 | * used by the Idle task. */ 172 | void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer, 173 | StackType_t ** ppxIdleTaskStackBuffer, 174 | uint32_t * pulIdleTaskStackSize ) 175 | { 176 | /* If the buffers to be provided to the Idle task are declared inside this 177 | * function then they must be declared static - otherwise they will be allocated on 178 | * the stack and so not exists after this function exits. */ 179 | static StaticTask_t xIdleTaskTCB; 180 | static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ]; 181 | 182 | /* Pass out a pointer to the StaticTask_t structure in which the Idle task's 183 | * state will be stored. */ 184 | *ppxIdleTaskTCBBuffer = &xIdleTaskTCB; 185 | 186 | /* Pass out the array that will be used as the Idle task's stack. */ 187 | *ppxIdleTaskStackBuffer = uxIdleTaskStack; 188 | 189 | /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. 190 | * Note that, as the array is necessarily of type StackType_t, 191 | * configMINIMAL_STACK_SIZE is specified in words, not bytes. */ 192 | *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; 193 | } 194 | /*-----------------------------------------------------------*/ 195 | 196 | /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the 197 | * application must provide an implementation of vApplicationGetTimerTaskMemory() 198 | * to provide the memory that is used by the Timer service task. */ 199 | void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer, 200 | StackType_t ** ppxTimerTaskStackBuffer, 201 | uint32_t * pulTimerTaskStackSize ) 202 | { 203 | /* If the buffers to be provided to the Timer task are declared inside this 204 | * function then they must be declared static - otherwise they will be allocated on 205 | * the stack and so not exists after this function exits. */ 206 | static StaticTask_t xTimerTaskTCB; 207 | static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ]; 208 | 209 | /* Pass out a pointer to the StaticTask_t structure in which the Timer 210 | * task's state will be stored. */ 211 | *ppxTimerTaskTCBBuffer = &xTimerTaskTCB; 212 | 213 | /* Pass out the array that will be used as the Timer task's stack. */ 214 | *ppxTimerTaskStackBuffer = uxTimerTaskStack; 215 | 216 | /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer. 217 | * Note that, as the array is necessarily of type StackType_t, 218 | * configMINIMAL_STACK_SIZE is specified in words, not bytes. */ 219 | *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; 220 | } 221 | /*-----------------------------------------------------------*/ 222 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/FreeRTOSConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. 3 | All rights reserved 4 | 5 | VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. 6 | 7 | This file is part of the FreeRTOS distribution. 8 | 9 | FreeRTOS is free software; you can redistribute it and/or modify it under 10 | the terms of the GNU General Public License (version 2) as published by the 11 | Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. 12 | 13 | *************************************************************************** 14 | >>! NOTE: The modification to the GPL is included to allow you to !<< 15 | >>! distribute a combined work that includes FreeRTOS without being !<< 16 | >>! obliged to provide the source code for proprietary components !<< 17 | >>! outside of the FreeRTOS kernel. !<< 18 | *************************************************************************** 19 | 20 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY 21 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 22 | FOR A PARTICULAR PURPOSE. Full license text is available on the following 23 | link: http://www.freertos.org/a00114.html 24 | 25 | *************************************************************************** 26 | * * 27 | * FreeRTOS provides completely free yet professionally developed, * 28 | * robust, strictly quality controlled, supported, and cross * 29 | * platform software that is more than just the market leader, it * 30 | * is the industry's de facto standard. * 31 | * * 32 | * Help yourself get started quickly while simultaneously helping * 33 | * to support the FreeRTOS project by purchasing a FreeRTOS * 34 | * tutorial book, reference manual, or both: * 35 | * http://www.FreeRTOS.org/Documentation * 36 | * * 37 | *************************************************************************** 38 | 39 | http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading 40 | the FAQ page "My application does not run, what could be wrong?". Have you 41 | defined configASSERT()? 42 | 43 | http://www.FreeRTOS.org/support - In return for receiving this top quality 44 | embedded software for free we request you assist our global community by 45 | participating in the support forum. 46 | 47 | http://www.FreeRTOS.org/training - Investing in training allows your team to 48 | be as productive as possible as early as possible. Now you can receive 49 | FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers 50 | Ltd, and the world's leading authority on the world's leading RTOS. 51 | 52 | http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, 53 | including FreeRTOS+Trace - an indispensable productivity tool, a DOS 54 | compatible FAT file system, and our tiny thread aware UDP/IP stack. 55 | 56 | http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. 57 | Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. 58 | 59 | http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High 60 | Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS 61 | licenses offer ticketed support, indemnification and commercial middleware. 62 | 63 | http://www.SafeRTOS.com - High Integrity Systems also provide a safety 64 | engineered and independently SIL3 certified version for use in safety and 65 | mission critical applications that require provable dependability. 66 | 67 | 1 tab == 4 spaces! 68 | */ 69 | 70 | #ifndef FREERTOS_CONFIG_H 71 | #define FREERTOS_CONFIG_H 72 | 73 | /*----------------------------------------------------------- 74 | * Application specific definitions. 75 | * 76 | * These definitions should be adjusted for your particular hardware and 77 | * application requirements. 78 | * 79 | * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE 80 | * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. 81 | * http://www.freertos.org/a00110.html 82 | * 83 | * The bottom of this file contains some constants specific to running the UDP 84 | * stack in this demo. Constants specific to FreeRTOS+TCP itself (rather than 85 | * the demo) are contained in FreeRTOSIPConfig.h. 86 | *----------------------------------------------------------*/ 87 | #define configENABLE_BACKWARD_COMPATIBILITY 0 88 | #define configUSE_PREEMPTION 1 89 | #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 90 | #define configMAX_PRIORITIES ( 7 ) 91 | #define configTICK_RATE_HZ ( 1000 ) /* In this non-real time simulated environment the tick frequency has to be at least a multiple of the Win32 tick frequency, and therefore very slow. */ 92 | #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 60 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the Win32 thread. */ 93 | #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 2048U * 1024U ) ) 94 | #define configMAX_TASK_NAME_LEN ( 15 ) 95 | #define configUSE_TRACE_FACILITY 1 96 | #define configUSE_16_BIT_TICKS 0 97 | #define configIDLE_SHOULD_YIELD 1 98 | #define configUSE_CO_ROUTINES 0 99 | #define configUSE_MUTEXES 1 100 | #define configUSE_RECURSIVE_MUTEXES 1 101 | #define configQUEUE_REGISTRY_SIZE 0 102 | #define configUSE_APPLICATION_TASK_TAG 0 103 | #define configUSE_COUNTING_SEMAPHORES 1 104 | #define configUSE_ALTERNATIVE_API 0 105 | #define configNUM_THREAD_LOCAL_STORAGE_POINTERS 3 /* FreeRTOS+FAT requires 2 pointers if a CWD is supported. */ 106 | 107 | /* Hook function related definitions. */ 108 | #define configUSE_TICK_HOOK 0 109 | #define configUSE_IDLE_HOOK 0 110 | #define configUSE_MALLOC_FAILED_HOOK 1 111 | #define configCHECK_FOR_STACK_OVERFLOW 0 /* Not applicable to the Win32 port. */ 112 | 113 | /* Software timer related definitions. */ 114 | #define configUSE_TIMERS 1 115 | #define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) 116 | #define configTIMER_QUEUE_LENGTH 5 117 | #define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 ) 118 | 119 | /* Event group related definitions. */ 120 | #define configUSE_EVENT_GROUPS 1 121 | 122 | /* Run time stats gathering definitions. */ 123 | uint32_t ulGetRunTimeCounterValue( void ); 124 | void vConfigureTimerForRunTimeStats( void ); 125 | #define configGENERATE_RUN_TIME_STATS 1 126 | #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats() 127 | #define portGET_RUN_TIME_COUNTER_VALUE() ulGetRunTimeCounterValue() 128 | 129 | /* Co-routine definitions. */ 130 | #define configUSE_CO_ROUTINES 0 131 | #define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) 132 | 133 | /* Set the following definitions to 1 to include the API function, or zero 134 | to exclude the API function. */ 135 | #define INCLUDE_vTaskPrioritySet 1 136 | #define INCLUDE_uxTaskPriorityGet 1 137 | #define INCLUDE_vTaskDelete 1 138 | #define INCLUDE_vTaskCleanUpResources 0 139 | #define INCLUDE_vTaskSuspend 1 140 | #define INCLUDE_vTaskDelayUntil 1 141 | #define INCLUDE_vTaskDelay 1 142 | #define INCLUDE_uxTaskGetStackHighWaterMark 1 143 | #define INCLUDE_xTaskGetSchedulerState 1 144 | #define INCLUDE_xTimerGetTimerTaskHandle 0 145 | #define INCLUDE_xTaskGetIdleTaskHandle 0 146 | #define INCLUDE_xQueueGetMutexHolder 1 147 | #define INCLUDE_eTaskGetState 1 148 | #define INCLUDE_xEventGroupSetBitsFromISR 1 149 | #define INCLUDE_xTimerPendFunctionCall 1 150 | #define INCLUDE_pcTaskGetTaskName 1 151 | 152 | /* This demo makes use of one or more example stats formatting functions. These 153 | format the raw data provided by the uxTaskGetSystemState() function in to human 154 | readable ASCII form. See the notes in the implementation of vTaskList() within 155 | FreeRTOS/Source/tasks.c for limitations. configUSE_STATS_FORMATTING_FUNCTIONS 156 | is set to 2 so the formatting functions are included without the stdio.h being 157 | included in tasks.c. That is because this project defines its own sprintf() 158 | functions. */ 159 | #define configUSE_STATS_FORMATTING_FUNCTIONS 1 160 | 161 | /* Assert call defined for debug builds. */ 162 | #ifdef _DEBUG 163 | extern void vAssertCalled( const char *pcFile, uint32_t ulLine ); 164 | #define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ ) 165 | #endif /* _DEBUG */ 166 | 167 | 168 | 169 | /* Application specific definitions follow. **********************************/ 170 | 171 | #if( defined( _MSC_VER ) && ( _MSC_VER <= 1600 ) && !defined( snprintf ) ) 172 | /* Map to Windows names. */ 173 | #define snprintf _snprintf 174 | #define vsnprintf _vsnprintf 175 | #endif 176 | 177 | /* Visual studio does not have an implementation of strcasecmp(). */ 178 | #define strcasecmp _stricmp 179 | #define strncasecmp _strnicmp 180 | #define strcmpi _strcmpi 181 | 182 | #endif /* FREERTOS_CONFIG_H */ 183 | 184 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/WIN32.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {34567deb-d5ab-4a56-8640-0aaec609521a} 6 | cpp;c;cxx;rc;def;r;odl;idl;hpj;bat 7 | 8 | 9 | {e3396fdf-de1f-4cb9-975c-673cb9315287} 10 | 11 | 12 | {af3445a1-4908-4170-89ed-39345d90d30c} 13 | 14 | 15 | {19ff1a34-36de-4c48-9d10-3fb1fa0d1fa4} 16 | 17 | 18 | 19 | 20 | {88f409e6-d396-4ac5-94bd-7a99c914be46} 21 | 22 | 23 | {a4b373f5-dc97-40ff-9b67-118253049621} 24 | 25 | 26 | {0447c18c-e805-43d8-8b36-744f7c7fb88f} 27 | 28 | 29 | {69f0d201-6431-464d-b056-e2f65f6026ec} 30 | 31 | 32 | {97f083ca-35db-4ba8-b9f2-359fc8b271f3} 33 | 34 | 35 | {4c51c94f-0c16-4bb9-8f1e-cd8d562dd3a7} 36 | 37 | 38 | {22654623-f802-4736-8cf7-7942b8a6147a} 39 | 40 | 41 | {f999220f-5e23-4340-9f52-e20e6a192ea4} 42 | 43 | 44 | {bda389c4-cd84-42de-a420-34383152cc4b} 45 | 46 | 47 | {880b2d47-fcd1-4cff-939c-537e55026cf7} 48 | 49 | 50 | 51 | 52 | application_code 53 | 54 | 55 | lib\FreeRTOS\Portable\MemMang 56 | 57 | 58 | lib\FreeRTOS\Portable\MSVC-W 59 | 60 | 61 | lib\FreeRTOS 62 | 63 | 64 | lib\FreeRTOS 65 | 66 | 67 | lib\FreeRTOS 68 | 69 | 70 | lib\FreeRTOS 71 | 72 | 73 | lib\FreeRTOS 74 | 75 | 76 | lib\FreeRTOS 77 | 78 | 79 | application_code 80 | 81 | 82 | lib\FreeRTOS-Plus-POSIX\source 83 | 84 | 85 | lib\FreeRTOS-Plus-POSIX\source 86 | 87 | 88 | lib\FreeRTOS-Plus-POSIX\source 89 | 90 | 91 | lib\FreeRTOS-Plus-POSIX\source 92 | 93 | 94 | lib\FreeRTOS-Plus-POSIX\source 95 | 96 | 97 | lib\FreeRTOS-Plus-POSIX\source 98 | 99 | 100 | lib\FreeRTOS-Plus-POSIX\source 101 | 102 | 103 | lib\FreeRTOS-Plus-POSIX\source 104 | 105 | 106 | lib\FreeRTOS-Plus-POSIX\source 107 | 108 | 109 | lib\FreeRTOS-Plus-POSIX\source 110 | 111 | 112 | lib\FreeRTOS-Plus-POSIX\source 113 | 114 | 115 | 116 | 117 | configuration_files 118 | 119 | 120 | lib\FreeRTOS\Portable\MSVC-W 121 | 122 | 123 | application_code 124 | 125 | 126 | lib\FreeRTOS-Plus-POSIX\include 127 | 128 | 129 | lib\FreeRTOS-Plus-POSIX\include 130 | 131 | 132 | lib\FreeRTOS-Plus-POSIX\include 133 | 134 | 135 | lib\FreeRTOS-Plus-POSIX\include 136 | 137 | 138 | lib\FreeRTOS-Plus-POSIX\include 139 | 140 | 141 | lib\include\FreeRTOS_POSIX 142 | 143 | 144 | lib\include\FreeRTOS_POSIX 145 | 146 | 147 | lib\include\FreeRTOS_POSIX 148 | 149 | 150 | lib\include\FreeRTOS_POSIX 151 | 152 | 153 | lib\include\FreeRTOS_POSIX 154 | 155 | 156 | lib\include\FreeRTOS_POSIX 157 | 158 | 159 | lib\include\FreeRTOS_POSIX 160 | 161 | 162 | lib\include\FreeRTOS_POSIX 163 | 164 | 165 | lib\include\FreeRTOS_POSIX 166 | 167 | 168 | lib\include\FreeRTOS_POSIX 169 | 170 | 171 | lib\include\FreeRTOS_POSIX\sys 172 | 173 | 174 | lib\include\private 175 | 176 | 177 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/WIN32.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {38712199-cebf-4124-bf15-398f7c3419ea} 6 | ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe 7 | 8 | 9 | {e5ad4ec7-23dc-4295-8add-2acaee488f5a} 10 | 11 | 12 | {7ffdff79-2c12-4bd4-af38-521cc93b7d95} 13 | 14 | 15 | {d232bbe4-6782-4451-8758-726ca14ddaeb} 16 | 17 | 18 | {edeb20f4-c0e9-464e-807b-310470f612e2} 19 | 20 | 21 | {af3445a1-4908-4170-89ed-39345d90d30c} 22 | 23 | 24 | {f32be356-4763-4cae-9020-974a2638cb08} 25 | *.c 26 | 27 | 28 | {88f409e6-d396-4ac5-94bd-7a99c914be46} 29 | 30 | 31 | {d2dcd641-8d91-492b-852f-5563ffadaec6} 32 | 33 | 34 | 35 | 36 | FreeRTOS-Source\Source\Portable 37 | 38 | 39 | FreeRTOS-Source\Source 40 | 41 | 42 | FreeRTOS-Source\Source 43 | 44 | 45 | FreeRTOS-Source\Source 46 | 47 | 48 | FreeRTOS-Source\Source 49 | 50 | 51 | FreeRTOS-Source\Source 52 | 53 | 54 | 55 | 56 | FreeRTOS-Labs\FreeRTOS+FAT 57 | 58 | 59 | FreeRTOS-Labs\FreeRTOS+FAT 60 | 61 | 62 | FreeRTOS-Labs\FreeRTOS+FAT 63 | 64 | 65 | FreeRTOS-Labs\FreeRTOS+FAT 66 | 67 | 68 | FreeRTOS-Labs\FreeRTOS+FAT 69 | 70 | 71 | FreeRTOS-Labs\FreeRTOS+FAT 72 | 73 | 74 | FreeRTOS-Labs\FreeRTOS+FAT 75 | 76 | 77 | FreeRTOS-Labs\FreeRTOS+FAT 78 | 79 | 80 | FreeRTOS-Labs\FreeRTOS+FAT 81 | 82 | 83 | FreeRTOS-Labs\FreeRTOS+FAT 84 | 85 | 86 | FreeRTOS-Labs\FreeRTOS+FAT 87 | 88 | 89 | FreeRTOS-Labs\FreeRTOS+FAT 90 | 91 | 92 | FreeRTOS-Labs\FreeRTOS+FAT\portable 93 | 94 | 95 | FreeRTOS-Labs\FreeRTOS+FAT 96 | 97 | 98 | 99 | 100 | FreeRTOS-Source\Source\Portable 101 | 102 | 103 | 104 | 105 | FreeRTOS-Source\Source\include 106 | 107 | 108 | FreeRTOS-Source\Source\include 109 | 110 | 111 | FreeRTOS-Source\Source\include 112 | 113 | 114 | FreeRTOS-Source\Source\include 115 | 116 | 117 | FreeRTOS-Source\Source\include 118 | 119 | 120 | FreeRTOS-Source\Source\include 121 | 122 | 123 | FreeRTOS-Source\Source\include 124 | 125 | 126 | 127 | FreeRTOS-Source\Source\include 128 | 129 | 130 | 131 | FreeRTOS-Labs\FreeRTOS+FAT\include 132 | 133 | 134 | FreeRTOS-Labs\FreeRTOS+FAT\include 135 | 136 | 137 | FreeRTOS-Labs\FreeRTOS+FAT\include 138 | 139 | 140 | FreeRTOS-Labs\FreeRTOS+FAT\include 141 | 142 | 143 | FreeRTOS-Labs\FreeRTOS+FAT\include 144 | 145 | 146 | FreeRTOS-Labs\FreeRTOS+FAT\include 147 | 148 | 149 | FreeRTOS-Labs\FreeRTOS+FAT\include 150 | 151 | 152 | FreeRTOS-Labs\FreeRTOS+FAT\include 153 | 154 | 155 | FreeRTOS-Labs\FreeRTOS+FAT\include 156 | 157 | 158 | FreeRTOS-Labs\FreeRTOS+FAT\include 159 | 160 | 161 | FreeRTOS-Labs\FreeRTOS+FAT\include 162 | 163 | 164 | FreeRTOS-Labs\FreeRTOS+FAT\include 165 | 166 | 167 | FreeRTOS-Labs\FreeRTOS+FAT\include 168 | 169 | 170 | FreeRTOS-Labs\FreeRTOS+FAT\include 171 | 172 | 173 | FreeRTOS-Labs\FreeRTOS+FAT\include 174 | 175 | 176 | FreeRTOS-Labs\FreeRTOS+FAT\include 177 | 178 | 179 | FreeRTOS-Source\Source\include 180 | 181 | 182 | FreeRTOS-Labs\FreeRTOS+FAT\include 183 | 184 | 185 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. 3 | All rights reserved 4 | 5 | VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. 6 | 7 | This file is part of the FreeRTOS distribution. 8 | 9 | FreeRTOS is free software; you can redistribute it and/or modify it under 10 | the terms of the GNU General Public License (version 2) as published by the 11 | Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. 12 | 13 | *************************************************************************** 14 | >>! NOTE: The modification to the GPL is included to allow you to !<< 15 | >>! distribute a combined work that includes FreeRTOS without being !<< 16 | >>! obliged to provide the source code for proprietary components !<< 17 | >>! outside of the FreeRTOS kernel. !<< 18 | *************************************************************************** 19 | 20 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY 21 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 22 | FOR A PARTICULAR PURPOSE. Full license text is available on the following 23 | link: http://www.freertos.org/a00114.html 24 | 25 | *************************************************************************** 26 | * * 27 | * FreeRTOS provides completely free yet professionally developed, * 28 | * robust, strictly quality controlled, supported, and cross * 29 | * platform software that is more than just the market leader, it * 30 | * is the industry's de facto standard. * 31 | * * 32 | * Help yourself get started quickly while simultaneously helping * 33 | * to support the FreeRTOS project by purchasing a FreeRTOS * 34 | * tutorial book, reference manual, or both: * 35 | * http://www.FreeRTOS.org/Documentation * 36 | * * 37 | *************************************************************************** 38 | 39 | http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading 40 | the FAQ page "My application does not run, what could be wrong?". Have you 41 | defined configASSERT()? 42 | 43 | http://www.FreeRTOS.org/support - In return for receiving this top quality 44 | embedded software for free we request you assist our global community by 45 | participating in the support forum. 46 | 47 | http://www.FreeRTOS.org/training - Investing in training allows your team to 48 | be as productive as possible as early as possible. Now you can receive 49 | FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers 50 | Ltd, and the world's leading authority on the world's leading RTOS. 51 | 52 | http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, 53 | including FreeRTOS+Trace - an indispensable productivity tool, a DOS 54 | compatible FAT file system, and our tiny thread aware UDP/IP stack. 55 | 56 | http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. 57 | Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. 58 | 59 | http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High 60 | Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS 61 | licenses offer ticketed support, indemnification and commercial middleware. 62 | 63 | http://www.SafeRTOS.com - High Integrity Systems also provide a safety 64 | engineered and independently SIL3 certified version for use in safety and 65 | mission critical applications that require provable dependability. 66 | 67 | 1 tab == 4 spaces! 68 | */ 69 | 70 | /* 71 | * Instructions for using this project are provided on: 72 | * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html 73 | * 74 | * NOTE: Some versions of Visual Studio will generate erroneous compiler 75 | * warnings about variables being used before they are set. 76 | */ 77 | 78 | /* Standard includes. */ 79 | #include 80 | #include 81 | #include 82 | 83 | /* FreeRTOS includes. */ 84 | #include 85 | #include "task.h" 86 | #include "timers.h" 87 | #include "queue.h" 88 | #include "semphr.h" 89 | 90 | /* FreeRTOS+FAT includes. */ 91 | #include "ff_headers.h" 92 | #include "ff_stdio.h" 93 | #include "ff_ramdisk.h" 94 | 95 | /* The number and size of sectors that will make up the RAM disk. The RAM disk 96 | is huge to allow some verbose FTP tests used in previous demos. */ 97 | #define mainRAM_DISK_SECTOR_SIZE 512UL /* Currently fixed! */ 98 | #define mainRAM_DISK_SECTORS ( ( 5UL * 1024UL * 1024UL ) / mainRAM_DISK_SECTOR_SIZE ) /* 5M bytes. */ 99 | #define mainIO_MANAGER_CACHE_SIZE ( 15UL * mainRAM_DISK_SECTOR_SIZE ) 100 | 101 | /* Where the RAM disk is mounted. */ 102 | #define mainRAM_DISK_NAME "/ram" 103 | 104 | /* Set to 0 to run the STDIO examples once only, or 1 to create multiple tasks 105 | that run the tests continuously. */ 106 | #define mainRUN_STDIO_TESTS_IN_MULTIPLE_TASK 0 107 | 108 | /*-----------------------------------------------------------*/ 109 | 110 | /* 111 | * Creates a RAM disk, then creates files on the RAM disk. 112 | */ 113 | static void prvCreateDiskAndExampleFiles( void * pvParameters ); 114 | 115 | /* 116 | * Functions used to create and then test files on a disk. 117 | */ 118 | extern void vCreateAndVerifyExampleFiles( const char *pcMountPath ); 119 | extern void vStdioWithCWDTest( const char *pcMountPath ); 120 | extern void vMultiTaskStdioWithCWDTest( const char *const pcMountPath, uint16_t usStackSizeWords ); 121 | 122 | 123 | /*-----------------------------------------------------------*/ 124 | 125 | /* 126 | * NOTE: Some versions of Visual Studio will generate erroneous compiler 127 | * warnings about variables being used before they are set. 128 | */ 129 | int main( void ) 130 | { 131 | /* Create the task that demonstrates the file system using a RAM disk. */ 132 | xTaskCreate( prvCreateDiskAndExampleFiles, /* Function that implements the task. */ 133 | "FAT", /* Text name for the task, only used for debugging. */ 134 | configMINIMAL_STACK_SIZE, /* Stack size, can be anything for the Win32 port, but not any other ports. */ 135 | NULL, /* Not passing in any parameters. */ 136 | tskIDLE_PRIORITY, /* Priority at which the task will execute. */ 137 | NULL ); /* Not storing a handle to the task. */ 138 | 139 | /* Start the RTOS scheduler. */ 140 | vTaskStartScheduler(); 141 | 142 | /* If all is well, the scheduler will now be running, and the following 143 | line will never be reached. If the following line does execute, then 144 | there was insufficient FreeRTOS heap memory available for the idle and/or 145 | timer tasks to be created. See the memory management section on the 146 | FreeRTOS web site for more details (this is standard text that is not 147 | really applicable to the Win32 simulator port). */ 148 | for( ;; ) 149 | { 150 | } 151 | } 152 | /*-----------------------------------------------------------*/ 153 | 154 | static void prvCreateDiskAndExampleFiles( void * pvParameters ) 155 | { 156 | static uint8_t ucRAMDisk[ mainRAM_DISK_SECTORS * mainRAM_DISK_SECTOR_SIZE ]; 157 | FF_Disk_t *pxDisk; 158 | 159 | /* Remove compiler warnings about unused parameters. */ 160 | ( void ) pvParameters; 161 | 162 | /* Create the RAM disk. */ 163 | pxDisk = FF_RAMDiskInit( mainRAM_DISK_NAME, ucRAMDisk, mainRAM_DISK_SECTORS, mainIO_MANAGER_CACHE_SIZE ); 164 | configASSERT( pxDisk ); 165 | 166 | /* Print out information on the disk. */ 167 | FF_RAMDiskShowPartition( pxDisk ); 168 | 169 | /* Create a few example files on the disk. These are not deleted again. */ 170 | vCreateAndVerifyExampleFiles( mainRAM_DISK_NAME ); 171 | 172 | /* A few sanity checks only - can only be called after 173 | vCreateAndVerifyExampleFiles(). */ 174 | #if( mainRUN_STDIO_TESTS_IN_MULTIPLE_TASK == 1 ) 175 | { 176 | /* Note the stack size is not actually used in the Windows port. */ 177 | vMultiTaskStdioWithCWDTest( mainRAM_DISK_NAME, configMINIMAL_STACK_SIZE * 2U ); 178 | } 179 | #else 180 | { 181 | vStdioWithCWDTest( mainRAM_DISK_NAME ); 182 | } 183 | #endif 184 | 185 | FF_PRINTF( "Done. Press a key to exit." ); 186 | while( !_kbhit() ) 187 | { 188 | vTaskDelay( pdMS_TO_TICKS( 1000 ) ); 189 | } 190 | 191 | /* FreeRTOS tasks must not be allowed to run off their implementing function. */ 192 | vPortEndScheduler(); 193 | } 194 | /*-----------------------------------------------------------*/ 195 | 196 | void vAssertCalled( const char *pcFile, uint32_t ulLine ) 197 | { 198 | const uint32_t ulLongSleep = 1000UL; 199 | volatile uint32_t ulBlockVariable = 0UL; 200 | volatile char *pcFileName = ( volatile char * ) pcFile; 201 | volatile uint32_t ulLineNumber = ulLine; 202 | 203 | ( void ) pcFileName; 204 | ( void ) ulLineNumber; 205 | 206 | /* Setting ulBlockVariable to a non-zero value in the debugger will allow 207 | this function to be exited. */ 208 | taskDISABLE_INTERRUPTS(); 209 | { 210 | while( ulBlockVariable == 0UL ) 211 | { 212 | Sleep( ulLongSleep ); 213 | } 214 | } 215 | taskENABLE_INTERRUPTS(); 216 | } 217 | /*-----------------------------------------------------------*/ 218 | 219 | void vApplicationMallocFailedHook( void ) 220 | { 221 | /* Called if a call to pvPortMalloc() fails because there is insufficient 222 | free memory available in the FreeRTOS heap. pvPortMalloc() is called 223 | internally by FreeRTOS API functions that create tasks, queues, software 224 | timers, and semaphores. The size of the FreeRTOS heap is set by the 225 | configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */ 226 | vAssertCalled( __FILE__, __LINE__ ); 227 | } 228 | /*-----------------------------------------------------------*/ 229 | 230 | struct tm *gmtime_r( const time_t *pxTime, struct tm *tmStruct ) 231 | { 232 | /* Dummy time functions to keep the file system happy in the absence of 233 | target support. */ 234 | memcpy( tmStruct, gmtime( pxTime ), sizeof( * tmStruct ) ); 235 | return tmStruct; 236 | } 237 | /*-----------------------------------------------------------*/ 238 | 239 | time_t FreeRTOS_time( time_t *pxTime ) 240 | { 241 | time_t xReturn; 242 | 243 | xReturn = time( &xReturn ); 244 | 245 | if( pxTime != NULL ) 246 | { 247 | *pxTime = xReturn; 248 | } 249 | 250 | return xReturn; 251 | } 252 | /*-----------------------------------------------------------*/ 253 | 254 | 255 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/examples/management_and_rng.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS PKCS #11 V1.0.3 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | /* FreeRTOS includes. */ 26 | #include "FreeRTOS.h" 27 | #include "task.h" 28 | 29 | /* Standard includes. */ 30 | #include "stdio.h" 31 | 32 | /* PKCS #11 includes. */ 33 | #include "iot_pkcs11_config.h" 34 | #include "iot_pkcs11.h" 35 | #include "pkcs11.h" 36 | 37 | /* Demo include. */ 38 | #include "pkcs11_demos.h" 39 | 40 | /** 41 | * This function details how to use the PKCS #11 "Management" functions to 42 | * manage the internal state machine of the PKCS #11 implementation. These 43 | * functions are all defined in 44 | * http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html 45 | * please consult the standard for more information regarding these functions. 46 | * 47 | * The standard has grouped the functions presented in this demo as: 48 | * General Purpose Functions 49 | * Slot and Token Management Functions 50 | * Session Management Functions 51 | * Random Number Generation Functions 52 | */ 53 | void vPKCS11ManagementAndRNGDemo( void ) 54 | { 55 | /* We will use the terminology as defined in the standard, Cryptoki is in 56 | * reference to the Cryptographic Token Interface defined in the PKCS #11 57 | * standard. An implementation of Cryptoki is referred to as a 58 | * "Cryptoki library". */ 59 | configPRINTF( ( "\r\nStarting PKCS #11 Management and Random Number Generation" \ 60 | " Demo.\r\n" ) ); 61 | 62 | /* CK_RV is the return type for a Cryptoki function. Generally the underlying 63 | * type is a CK_ULONG, it can also be a CKR_VENDOR_DEFINED type. */ 64 | CK_RV xResult = CKR_OK; 65 | 66 | /* The CK_FUNCTION_LIST is a structure that contains the Cryptoki version 67 | * and a function pointer to each function in the Cryptoki API. If the 68 | * function pointer is NULL it is unimplemented. */ 69 | CK_FUNCTION_LIST_PTR pxFunctionList = NULL; 70 | 71 | /* This Cryptoki library does not implement any initialization arguments. At the time of 72 | * writing this demo, the purpose of these optional arguments is to provide 73 | * function pointers for mutex operations. */ 74 | CK_C_INITIALIZE_ARGS xInitArgs = { 0 }; 75 | 76 | /* A slot ID is an integer that defines a slot. The Cryptoki definition of 77 | * a slot is "A logical reader that potentially contains a token." 78 | * 79 | * Essentially it is an abstraction for accessing the token. The reason for 80 | * this is Some tokens are a physical "card' that needs to be inserted into 81 | * a slot for the device to read. 82 | * 83 | * A concrete example of a slot could be a USB Hardware Security Module (HSM), 84 | * which generally appears as a singular slot, and abstracts it's internal "token". 85 | * 86 | * Some implementations have multiple slots mapped to a single token, or maps 87 | * a slot per token. */ 88 | CK_SLOT_ID * pxSlotId = NULL; 89 | 90 | /* A session is defined to be "The logical connection between an application 91 | * and a token." 92 | * 93 | * The session can either be private or public, and differentiates 94 | * your application from the other users of the token. */ 95 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 96 | 97 | /* Helper variables. */ 98 | CK_BYTE xRandomData[ 10 ] = { 0 }; 99 | uint32_t ulIndex = 0; 100 | CK_ULONG xSlotCount = 0; 101 | 102 | /* We use the function list returned by C_GetFunctionList to see what functions 103 | * the Cryptoki library supports. We use asserts to ensure that all the 104 | * functionality needed in this demo is available. */ 105 | xResult = C_GetFunctionList( &pxFunctionList ); 106 | configASSERT( xResult == CKR_OK ); 107 | configASSERT( pxFunctionList != NULL ); 108 | configASSERT( pxFunctionList->C_Initialize != NULL ); 109 | configASSERT( pxFunctionList->C_GetSlotList != NULL ); 110 | configASSERT( pxFunctionList->C_OpenSession != NULL ); 111 | configASSERT( pxFunctionList->C_Login != NULL ); 112 | configASSERT( pxFunctionList->C_GenerateRandom != NULL ); 113 | configASSERT( pxFunctionList->C_CloseSession != NULL ); 114 | configASSERT( pxFunctionList->C_Finalize != NULL ); 115 | 116 | configPRINTF( ( "Cryptoki Major Version: %lu Minor Version %lu\r\n", 117 | pxFunctionList->version.major, 118 | pxFunctionList->version.minor ) ); 119 | 120 | /* C_Initialize will initialize the Cryptoki library and the hardware it 121 | * abstracts. */ 122 | xResult = pxFunctionList->C_Initialize( &xInitArgs ); 123 | configASSERT( xResult == CKR_OK ); 124 | 125 | /* C_GetSlotList will retrieve an array of CK_SLOT_IDs. 126 | * This Cryptoki library does not implement slots, but it is important to 127 | * highlight how Cryptoki can be used to interface with real hardware. 128 | * 129 | * By setting the first argument "tokenPresent" to true, we only retrieve 130 | * slots that have a token. If the second argument "pSlotList" is NULL, the 131 | * third argument "pulCount" will be modified to contain the total slots. */ 132 | xResult = pxFunctionList->C_GetSlotList( CK_TRUE, 133 | NULL, 134 | &xSlotCount ); 135 | configASSERT( xResult == CKR_OK ); 136 | 137 | /* Since C_GetSlotList does not allocate the memory itself for getting a list 138 | * of CK_SLOT_ID, we allocate one for it to populate with the list of 139 | * slot ids. */ 140 | pxSlotId = pvPortMalloc( sizeof( CK_SLOT_ID ) * ( xSlotCount ) ); 141 | configASSERT( pxSlotId != NULL ); 142 | 143 | /* Now since pSlotList is not NULL, C_GetSlotList will populate it with the 144 | * available slots. */ 145 | xResult = pxFunctionList->C_GetSlotList( CK_TRUE, 146 | pxSlotId, 147 | &xSlotCount ); 148 | configASSERT( xResult == CKR_OK ); 149 | 150 | /* Since this Cryptoki library does not actually implement the concept of slots, 151 | * but we will use the first available slot, so the demo code conforms to 152 | * Cryptoki. 153 | * 154 | * C_OpenSession will establish a session between the application and 155 | * the token and we can then use the returned CK_SESSION_HANDLE for 156 | * cryptographic operations with the token. 157 | * 158 | * For legacy reasons, Cryptoki demands that the CKF_SERIAL_SESSION bit 159 | * is always set. */ 160 | xResult = pxFunctionList->C_OpenSession( pxSlotId[0], 161 | CKF_SERIAL_SESSION | CKF_RW_SESSION, 162 | NULL, /* Application defined pointer. */ 163 | NULL, /* Callback function. */ 164 | &hSession ); 165 | configASSERT( xResult == CKR_OK ); 166 | 167 | 168 | /* C_Login is called to log the user in to the token. The login status is 169 | * shared between sessions, so logging in once is sufficient for all the sessions 170 | * tied to the token. Most of the behavior for C_Login is defined by the token 171 | * so it may be necessary to modify calls to C_Login when switching to a different 172 | * Cryptoki library or token. 173 | * 174 | * This Cryptoki library does not implement C_Login, and only defines the function 175 | * for compatibility reasons. 176 | */ 177 | xResult = pxFunctionList->C_Login( hSession, 178 | CKU_USER, 179 | ( CK_UTF8CHAR_PTR ) configPKCS11_DEFAULT_USER_PIN, 180 | sizeof( configPKCS11_DEFAULT_USER_PIN ) - 1UL ); 181 | configASSERT( xResult == CKR_OK ); 182 | 183 | /* C_GenerateRandom generates random or pseudo random data. As arguments it 184 | * takes the application session, and a pointer to a byte buffer, as well as 185 | * the length of the byte buffer. Then it will fill this buffer with random 186 | * bytes. */ 187 | xResult = pxFunctionList->C_GenerateRandom( hSession, 188 | xRandomData, 189 | sizeof( xRandomData ) ); 190 | configASSERT( xResult == CKR_OK ); 191 | 192 | for( ulIndex = 0; ulIndex < sizeof( xRandomData ); ulIndex++ ) 193 | { 194 | configPRINTF( ( "Generated random number: %x\r\n", xRandomData[ ulIndex ] ) ); 195 | } 196 | 197 | 198 | /* C_CloseSession closes the session that was established between the 199 | * application and the token. This will clean up the resources that maintained 200 | * the link between the application and the token. If the application wishes 201 | * to use the token again, it will need to open a new session. */ 202 | xResult = pxFunctionList->C_CloseSession( hSession ); 203 | configASSERT( xResult == CKR_OK ); 204 | 205 | /* C_Finalize signals to the Cryptoki library that the application is done 206 | * using it. It should always be the last call to the Cryptoki library. 207 | * NULL should always be passed as the argument, as the parameter is currently 208 | * just reserved for future revisions. 209 | * 210 | * Calling this function in a multi threaded environment can lead to undefined 211 | * behavior if other threads are accessing the Cryptoki library. */ 212 | xResult = pxFunctionList->C_Finalize( NULL ); 213 | configASSERT( xResult == CKR_OK ); 214 | 215 | configPRINTF( ( "Finished PKCS #11 Management and Random Number Generation" \ 216 | " Demo.\r\n" ) ); 217 | 218 | vPortFree( pxSlotId ); 219 | } 220 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/examples/mechanisms_and_digests.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS PKCS #11 V1.0.3 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | /* FreeRTOS includes. */ 26 | #include "FreeRTOS.h" 27 | #include "task.h" 28 | 29 | /* Standard includes. */ 30 | #include "stdio.h" 31 | 32 | /* PKCS #11 includes. */ 33 | #include "iot_pkcs11_config.h" 34 | #include "iot_pkcs11.h" 35 | #include "pkcs11.h" 36 | 37 | /* Demo includes. */ 38 | #include "demo_helpers.h" 39 | #include "pkcs11_demos.h" 40 | 41 | /** 42 | * This function details what Cryptoki mechanisms are, how to query a slot's 43 | * support for them, and how to use those mechanisms to generate a hash of a buffer. 44 | * This can then be used as a message digest. 45 | * 46 | * http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html 47 | * please consult the standard for more information. 48 | * 49 | * The standard has grouped the functions presented in this demo as: 50 | * Slot and token management functions. 51 | * Message digesting functions. 52 | * 53 | */ 54 | void vPKCS11MechanismsAndDigestDemo( void ) 55 | { 56 | /* 57 | * This demo builds upon the demo found in "management_and_rng.c". It borrows 58 | * code and terminology defined and explained, and it is recommended to complete 59 | * the "management and rng" demo before this one. 60 | */ 61 | configPRINTF( ( "\r\nStarting PKCS #11 Mechanisms and Digest Demo.\r\n" ) ); 62 | 63 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 64 | CK_SLOT_ID * pxSlotId = 0; 65 | CK_FUNCTION_LIST_PTR pxFunctionList = NULL; 66 | CK_ULONG ulIndex = 0; 67 | CK_RV xResult = CKR_OK; 68 | 69 | /* The PKCS #11 standard defines a mechanism to be a "A process for 70 | * implementing a cryptographic operation." For example the SHA-256 algorithm 71 | * will be the mechanism used in this demo to perform a digest (hash operation). 72 | * 73 | * The mechanism types are defined in "pkcs11t.h", and are prefixed CKM_, to 74 | * provide a portable way to identify mechanisms. 75 | */ 76 | CK_MECHANISM_TYPE xMechanismType = 0; 77 | 78 | /* This variable is not directly used, but is instantiated for demonstration 79 | * purposes. 80 | */ 81 | ( void ) xMechanismType; 82 | 83 | /* The CK_MECHANISM_INFO allows the application to retrieve the minimum and 84 | * maximum key sizes supported by the mechanism (could be in bits or bytes). 85 | * The structure also has a flags field, that is populated with bit flags 86 | * for what features the mechanism supports. 87 | */ 88 | CK_MECHANISM_INFO MechanismInfo = { 0 }; 89 | 90 | /* The CK_MECHANISM type contains the mechanism type, as well as a pointer 91 | * for mechanism parameters and a CK_ULONG indicating the length of the 92 | * parameters. 93 | */ 94 | CK_MECHANISM xDigestMechanism = { 0 }; 95 | 96 | /* The digest will return a hash of the known SHA-256 hash size, 32 bytes. 97 | * Please see this page for further explanation of the SHA-256 hash. 98 | * https://en.wikipedia.org/wiki/SHA-2 99 | */ 100 | CK_BYTE xDigestResult[ pkcs11SHA256_DIGEST_LENGTH ] = { 0 }; 101 | CK_ULONG ulDigestLength = pkcs11SHA256_DIGEST_LENGTH; 102 | 103 | CK_BYTE pxKnownMessage[] = "Hello world!"; 104 | 105 | vStart( &hSession, &pxSlotId ); 106 | xResult = C_GetFunctionList( &pxFunctionList ); 107 | configASSERT( CKR_OK == xResult ); 108 | configASSERT( pxFunctionList->C_GetMechanismInfo != NULL ); 109 | configASSERT( pxFunctionList->C_DigestInit != NULL ); 110 | configASSERT( pxFunctionList->C_DigestUpdate != NULL ); 111 | configASSERT( pxFunctionList->C_DigestFinal != NULL ); 112 | 113 | /*************************** RSA Capabilities ***************************/ 114 | xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], 115 | CKM_RSA_PKCS, 116 | &MechanismInfo ); 117 | configASSERT( CKR_OK == xResult ); 118 | 119 | /* Check to see if the slot supports signing. This capability is important 120 | * because we want to use the Cryptoki API to sign messages, without directly 121 | * accessing the private key. This concept will be explained further in the 122 | * "sign_verify.c" demo, but for now we will just check that the slot has the 123 | * capabilities we need. See https://en.wikipedia.org/wiki/Public-key_cryptography 124 | * for more information regarding private keys and public keys. 125 | */ 126 | if( 0 != ( CKF_SIGN & MechanismInfo.flags ) ) 127 | { 128 | configPRINTF( ( "This Cryptoki library supports signing messages with RSA" \ 129 | " private keys.\r\n" ) ); 130 | } 131 | else 132 | { 133 | configPRINTF( ( "This Cryptoki library does not support signing messages" \ 134 | " with RSA private keys.\r\n" ) ); 135 | } 136 | 137 | /* This Cryptoki library assumes that RSA private keys are 2048 bit . */ 138 | configASSERT( MechanismInfo.ulMaxKeySize >= pkcs11RSA_2048_MODULUS_BITS ); 139 | configASSERT( MechanismInfo.ulMinKeySize <= pkcs11RSA_2048_MODULUS_BITS ); 140 | 141 | /* Check for pre-padded signature verification support, this feature will 142 | * be used in the "sign_verify.c" demo. 143 | */ 144 | xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], 145 | CKM_RSA_X_509, 146 | &MechanismInfo ); 147 | 148 | /* If this fails, the slot is not able to verify the signature using 149 | * a RSA public key. Please see https://en.wikipedia.org/wiki/Public_key_infrastructure 150 | * for more information regarding PKI (Public Key Infrastructure). 151 | */ 152 | if( 0 != ( CKF_VERIFY & MechanismInfo.flags ) ) 153 | { 154 | configPRINTF( ( "This Cryptoki library supports verifying messages with RSA" \ 155 | " public keys.\r\n" ) ); 156 | } 157 | else 158 | { 159 | configPRINTF( ( "This Cryptoki library does not support verifying messages" \ 160 | " with RSA public keys.\r\n" ) ); 161 | } 162 | 163 | /* This Cryptoki library assumes that RSA public keys are 2048 bit . */ 164 | configASSERT( MechanismInfo.ulMaxKeySize >= pkcs11RSA_2048_MODULUS_BITS ); 165 | configASSERT( MechanismInfo.ulMinKeySize <= pkcs11RSA_2048_MODULUS_BITS ); 166 | 167 | /*************************** ECDSA Capabilities ***************************/ 168 | xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], 169 | CKM_ECDSA, 170 | &MechanismInfo ); 171 | configASSERT( CKR_OK == xResult ); 172 | 173 | if( 0 != ( CKF_SIGN & MechanismInfo.flags ) ) 174 | { 175 | configPRINTF( ( "This Cryptoki library supports signing messages with" \ 176 | " ECDSA private keys.\r\n" ) ); 177 | } 178 | else 179 | { 180 | configPRINTF( ( "This Cryptoki library does not support signing messages" \ 181 | " with ECDSA private keys.\r\n" ) ); 182 | } 183 | 184 | if( 0 != ( CKF_VERIFY & MechanismInfo.flags ) ) 185 | { 186 | configPRINTF( ( "This Cryptoki library supports verifying messages with" \ 187 | " ECDSA public keys.\r\n" ) ); 188 | } 189 | else 190 | { 191 | configPRINTF( ( "This Cryptoki library does not support verifying" \ 192 | " messages with ECDSA public keys.\r\n" ) ); 193 | } 194 | 195 | configASSERT( MechanismInfo.ulMaxKeySize >= pkcs11ECDSA_P256_KEY_BITS ); 196 | configASSERT( MechanismInfo.ulMinKeySize <= pkcs11ECDSA_P256_KEY_BITS ); 197 | 198 | /************************** Digest Capabilities **************************/ 199 | xResult = pxFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], 200 | CKM_SHA256, 201 | &MechanismInfo ); 202 | configASSERT( CKR_OK == xResult ); 203 | 204 | if( 0 != ( CKF_DIGEST & MechanismInfo.flags ) ) 205 | { 206 | configPRINTF( ( "The Cryptoki library supports the " \ 207 | "SHA-256 algorithm.\r\n" ) ); 208 | } 209 | else 210 | { 211 | configPRINTF( ( "The Cryptoki library doesn't support the " \ 212 | "SHA-256 algorithm.\r\n" ) ); 213 | } 214 | 215 | /***************************** Buffer Digest *****************************/ 216 | /* Hash with SHA256 mechanism. */ 217 | xDigestMechanism.mechanism = CKM_SHA256; 218 | 219 | /* Initializes the digest operation and sets what mechanism will be used 220 | * for the digest. */ 221 | xResult = pxFunctionList->C_DigestInit( hSession, 222 | &xDigestMechanism ); 223 | configASSERT( CKR_OK == xResult ); 224 | 225 | 226 | /* Pass a pointer to the buffer of bytes to be hashed, and it's size. */ 227 | xResult = pxFunctionList->C_DigestUpdate( hSession, 228 | pxKnownMessage, 229 | /* Strip NULL Terminator. */ 230 | sizeof( pxKnownMessage ) - 1 ); 231 | configASSERT( CKR_OK == xResult ); 232 | 233 | /* Retrieve the digest buffer. Since the mechanism is a SHA-256 algorithm, 234 | * the size will always be 32 bytes. If the size cannot be known ahead of time, 235 | * a NULL value to the second parameter pDigest, will set the third parameter, 236 | * pulDigestLen to the number of required bytes. */ 237 | xResult = pxFunctionList->C_DigestFinal( hSession, 238 | xDigestResult, 239 | &ulDigestLength ); 240 | configASSERT( CKR_OK == xResult ); 241 | 242 | /* This will now print out the digest of the known message. You can compare 243 | * the hash generated by the Cryptoki library in a UNIX shell by using the 244 | * command '$ echo -n "{pxKnownMessage}" | shasum -a 256' 245 | * this command should generate the same hash. */ 246 | configPRINTF( ( "Known message: %s\r\n", ( char * ) pxKnownMessage ) ); 247 | configPRINTF( ( "Hash of known message using SHA256:\r\n" ) ); 248 | 249 | for( ulIndex = 0; ulIndex < ulDigestLength; ulIndex++ ) 250 | { 251 | configPRINTF( ( "%x", xDigestResult[ ulIndex ] ) ); 252 | } 253 | configPRINTF( ( "\r\n" ) ); 254 | 255 | configPRINTF( ( "Finished PKCS #11 Mechanisms and Digest Demo.\r\n" ) ); 256 | vEnd( hSession, pxSlotId ); 257 | } 258 | 259 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/WIN32.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB} 15 | RTOS_POSIX_Demo 16 | 10.0 17 | 18 | 19 | 20 | Application 21 | false 22 | MultiByte 23 | v142 24 | 25 | 26 | Application 27 | false 28 | MultiByte 29 | v142 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | <_ProjectFileVersion>10.0.30319.1 45 | .\Debug\ 46 | .\Debug\ 47 | true 48 | .\Release\ 49 | .\Release\ 50 | false 51 | 52 | 53 | 54 | .\Debug/WIN32.tlb 55 | 56 | 57 | 58 | 59 | Disabled 60 | ..\..\..\FreeRTOS\Source\include;..\..\..\FreeRTOS\Source\portable\MSVC-MingW;..\..\..\FreeRTOS-Labs\Source\FreeRTOS-Plus-POSIX\FreeRTOS-Plus-POSIX\include\portable\pc\windows;..\..\..\FreeRTOS-Labs\Source\FreeRTOS-Plus-POSIX\FreeRTOS-Plus-POSIX\include\portable;..\..\..\FreeRTOS-Labs\Source\FreeRTOS-Plus-POSIX\FreeRTOS-Plus-POSIX\include;..\..\..\FreeRTOS-Labs\Source\FreeRTOS-Plus-POSIX\FreeRTOS-Plus-POSIX\source;..\..\..\FreeRTOS-Labs\Source\FreeRTOS-Plus-POSIX\include\private;..\..\..\FreeRTOS-Labs\Source\FreeRTOS-Plus-POSIX\include;..\..\..\FreeRTOS-Labs\Demo\FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator;%(AdditionalIncludeDirectories) 61 | WIN32;_DEBUG;_CONSOLE;_WIN32_WINNT=0x0500;WINVER=0x400;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 62 | true 63 | EnableFastChecks 64 | MultiThreadedDebug 65 | .\Debug/WIN32.pch 66 | .\Debug/ 67 | .\Debug/ 68 | .\Debug/ 69 | Level3 70 | true 71 | false 72 | EditAndContinue 73 | 74 | 75 | _DEBUG;%(PreprocessorDefinitions) 76 | 0x0c09 77 | 78 | 79 | .\Debug/RTOS_POSIX_demo.exe 80 | true 81 | true 82 | .\Debug/WIN32.pdb 83 | Console 84 | MachineX86 85 | %(AdditionalDependencies) 86 | 87 | 88 | false 89 | 90 | 91 | true 92 | .\Debug/WIN32.bsc 93 | 94 | 95 | 96 | 97 | .\Release/WIN32.tlb 98 | 99 | 100 | 101 | 102 | MaxSpeed 103 | OnlyExplicitInline 104 | _WINSOCKAPI_;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 105 | true 106 | MultiThreaded 107 | true 108 | .\Release/WIN32.pch 109 | .\Release/ 110 | .\Release/ 111 | .\Release/ 112 | Level3 113 | true 114 | ..\Common\Utils;..\Common\ethernet\lwip-1.4.0\ports\win32\WinPCap;..\Common\ethernet\lwip-1.4.0\src\include\ipv4;..\Common\ethernet\lwip-1.4.0\src\include;..\..\Source\include;..\..\Source\portable\MSVC-MingW;..\Common\ethernet\lwip-1.4.0\ports\win32\include;..\Common\Include;.\lwIP_Apps;.;%(AdditionalIncludeDirectories) 115 | 116 | 117 | NDEBUG;%(PreprocessorDefinitions) 118 | 0x0c09 119 | 120 | 121 | .\Release/RTOSDemo.exe 122 | true 123 | .\Release/WIN32.pdb 124 | Console 125 | MachineX86 126 | ..\Common\ethernet\lwip-1.4.0\ports\win32\WinPCap 127 | wpcap.lib;%(AdditionalDependencies) 128 | 129 | 130 | true 131 | .\Release/WIN32.bsc 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | %(AdditionalIncludeDirectories) 156 | %(PreprocessorDefinitions) 157 | %(PreprocessorDefinitions) 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /FreeRTOS-Plus/Demo/FreeRTOS_Plus_PKCS11_Windows_Simulator/examples/demo_helpers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS PKCS #11 V1.0.3 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | 26 | /* FreeRTOS includes. */ 27 | #include "FreeRTOS.h" 28 | #include "task.h" 29 | 30 | /* Standard includes. */ 31 | #include "stdio.h" 32 | 33 | /* PKCS #11 includes. */ 34 | #include "iot_pkcs11_config.h" 35 | #include "iot_pkcs11.h" 36 | #include "pkcs11.h" 37 | 38 | /* mbed TLS includes. */ 39 | #include "mbedtls/pk.h" 40 | #include "mbedtls/oid.h" 41 | 42 | /* Helpers include. */ 43 | #include "demo_helpers.h" 44 | 45 | void vStart( CK_SESSION_HANDLE * pxSession, 46 | CK_SLOT_ID ** ppxSlotId ) 47 | { 48 | CK_RV xResult = CKR_OK; 49 | 50 | CK_FUNCTION_LIST_PTR pxFunctionList = NULL; 51 | CK_C_INITIALIZE_ARGS xInitArgs = { 0 }; 52 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 53 | CK_ULONG xSlotCount = 0; 54 | CK_SLOT_ID * pxSlotId = NULL; 55 | 56 | xResult = C_GetFunctionList( &pxFunctionList ); 57 | configASSERT( xResult == CKR_OK ); 58 | configASSERT( pxFunctionList != NULL ); 59 | configASSERT( pxFunctionList->C_Initialize != NULL ); 60 | configASSERT( pxFunctionList->C_GetSlotList != NULL ); 61 | configASSERT( pxFunctionList->C_OpenSession != NULL ); 62 | configASSERT( pxFunctionList->C_Login != NULL ); 63 | configASSERT( pxFunctionList->C_GenerateRandom != NULL ); 64 | configASSERT( pxFunctionList->C_CloseSession != NULL ); 65 | configASSERT( pxFunctionList->C_Finalize != NULL ); 66 | 67 | xResult = pxFunctionList->C_Initialize( &xInitArgs ); 68 | configASSERT( xResult == CKR_OK ); 69 | 70 | xResult = pxFunctionList->C_GetSlotList( CK_TRUE, 71 | NULL, 72 | &xSlotCount ); 73 | configASSERT( xResult == CKR_OK ); 74 | 75 | pxSlotId = pvPortMalloc( sizeof( CK_SLOT_ID ) * ( xSlotCount ) ); 76 | configASSERT( pxSlotId != NULL ); 77 | 78 | xResult = pxFunctionList->C_GetSlotList( CK_TRUE, 79 | pxSlotId, 80 | &xSlotCount ); 81 | configASSERT( xResult == CKR_OK ); 82 | 83 | xResult = pxFunctionList->C_OpenSession( pxSlotId[ 0 ], 84 | CKF_SERIAL_SESSION | CKF_RW_SESSION, 85 | NULL, /* Application defined pointer. */ 86 | NULL, /* Callback function. */ 87 | &hSession ); 88 | configASSERT( xResult == CKR_OK ); 89 | 90 | 91 | xResult = pxFunctionList->C_Login( hSession, 92 | CKU_USER, 93 | ( CK_UTF8CHAR_PTR ) configPKCS11_DEFAULT_USER_PIN, 94 | sizeof( configPKCS11_DEFAULT_USER_PIN ) - 1UL ); 95 | configASSERT( xResult == CKR_OK ); 96 | 97 | *ppxSlotId = pxSlotId; 98 | *pxSession = hSession; 99 | } 100 | /*-----------------------------------------------------------*/ 101 | 102 | void vEnd( CK_SESSION_HANDLE xSession, 103 | CK_SLOT_ID * pxSlotId ) 104 | { 105 | C_CloseSession( xSession ); 106 | C_Finalize( NULL ); 107 | vPortFree( pxSlotId ); 108 | } 109 | /*-----------------------------------------------------------*/ 110 | 111 | void vWriteHexBytesToConsole( char * pcDescription, 112 | CK_BYTE * pucData, 113 | CK_ULONG ulDataLength ) 114 | { 115 | /* This function is simply a helper function to print the raw hex values 116 | * of an EC public key. It's explanation is not within the scope of the demos 117 | * and is sparsely commented. */ 118 | #define BYTES_TO_DISPLAY_PER_ROW 16 119 | char pcByteRow[ 1 + ( BYTES_TO_DISPLAY_PER_ROW * 2 ) + ( BYTES_TO_DISPLAY_PER_ROW / 2 ) ]; 120 | char * pcNextChar = pcByteRow; 121 | uint32_t ulIndex = 0; 122 | uint8_t ucByteValue = 0; 123 | 124 | /* Write help text to the console. */ 125 | configPRINTF( ( "%s, %d bytes:\r\n", pcDescription, ulDataLength ) ); 126 | 127 | /* Iterate over the bytes of the encoded public key. */ 128 | for( ulIndex = 0; ulIndex < ulDataLength; ulIndex++ ) 129 | { 130 | /* Convert one byte to ASCII hex. */ 131 | ucByteValue = *( pucData + ulIndex ); 132 | snprintf( pcNextChar, 133 | sizeof( pcByteRow ) - ( pcNextChar - pcByteRow ), 134 | "%02x", 135 | ucByteValue ); 136 | pcNextChar += 2; 137 | 138 | /* Check for the end of a two-byte display word. */ 139 | if( 0 == ( ( ulIndex + 1 ) % sizeof( uint16_t ) ) ) 140 | { 141 | *pcNextChar = ' '; 142 | pcNextChar++; 143 | } 144 | 145 | /* Check for the end of a row. */ 146 | if( 0 == ( ( ulIndex + 1 ) % BYTES_TO_DISPLAY_PER_ROW ) ) 147 | { 148 | *pcNextChar = '\0'; 149 | configPRINTF( ( pcByteRow ) ); 150 | configPRINTF( ( "\r\n" ) ); 151 | pcNextChar = pcByteRow; 152 | } 153 | } 154 | 155 | /* Check for a partial line to print. */ 156 | if( pcNextChar > pcByteRow ) 157 | { 158 | *pcNextChar = '\0'; 159 | configPRINTF( ( pcByteRow ) ); 160 | configPRINTF( ( "\r\n" ) ); 161 | } 162 | } 163 | /*-----------------------------------------------------------*/ 164 | 165 | /* Extract ECDSA public key. */ 166 | CK_RV vExportPublicKey( CK_SESSION_HANDLE xSession, 167 | CK_OBJECT_HANDLE xPublicKeyHandle, 168 | CK_BYTE ** ppucDerPublicKey, 169 | CK_ULONG * pulDerPublicKeyLength ) 170 | { 171 | /* This function is simply a helper function to export the raw hex values 172 | * of an EC public key into a buffer. It's explanation is not within the 173 | * scope of the demos and is sparsely commented. */ 174 | CK_RV xResult; 175 | CK_FUNCTION_LIST_PTR pxFunctionList; 176 | CK_KEY_TYPE xKeyType = 0; 177 | CK_ATTRIBUTE xTemplate = { 0 }; 178 | uint8_t pucEcP256AsnAndOid[] = 179 | { 180 | 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 181 | 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 182 | 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 183 | 0x42, 0x00 184 | }; 185 | uint8_t pucUnusedKeyTag[] = { 0x04, 0x41 }; 186 | 187 | /* This variable is used only for its size. This gets rid of compiler warnings. */ 188 | ( void ) pucUnusedKeyTag; 189 | 190 | xResult = C_GetFunctionList( &pxFunctionList ); 191 | 192 | /* Query the key type. */ 193 | if( CKR_OK == xResult ) 194 | { 195 | xTemplate.type = CKA_KEY_TYPE; 196 | xTemplate.pValue = &xKeyType; 197 | xTemplate.ulValueLen = sizeof( xKeyType ); 198 | xResult = pxFunctionList->C_GetAttributeValue( xSession, 199 | xPublicKeyHandle, 200 | &xTemplate, 201 | 1 ); 202 | } 203 | 204 | /* Scope to ECDSA keys only, since there's currently no use case for 205 | * onboard keygen and certificate enrollment for RSA. */ 206 | if( ( CKR_OK == xResult ) && ( CKK_ECDSA == xKeyType ) ) 207 | { 208 | /* Query the size of the public key. */ 209 | xTemplate.type = CKA_EC_POINT; 210 | xTemplate.pValue = NULL; 211 | xTemplate.ulValueLen = 0; 212 | xResult = pxFunctionList->C_GetAttributeValue( xSession, 213 | xPublicKeyHandle, 214 | &xTemplate, 215 | 1 ); 216 | 217 | /* Allocate a buffer large enough for the full, encoded public key. */ 218 | if( CKR_OK == xResult ) 219 | { 220 | /* Add space for the full DER header. */ 221 | xTemplate.ulValueLen += sizeof( pucEcP256AsnAndOid ) - sizeof( pucUnusedKeyTag ); 222 | *pulDerPublicKeyLength = xTemplate.ulValueLen; 223 | 224 | /* Get a heap buffer. */ 225 | *ppucDerPublicKey = pvPortMalloc( xTemplate.ulValueLen ); 226 | 227 | /* Check for resource exhaustion. */ 228 | if( NULL == *ppucDerPublicKey ) 229 | { 230 | xResult = CKR_HOST_MEMORY; 231 | } 232 | } 233 | 234 | /* Export the public key. */ 235 | if( CKR_OK == xResult ) 236 | { 237 | xTemplate.pValue = *ppucDerPublicKey + sizeof( pucEcP256AsnAndOid ) - sizeof( pucUnusedKeyTag ); 238 | xTemplate.ulValueLen -= ( sizeof( pucEcP256AsnAndOid ) - sizeof( pucUnusedKeyTag ) ); 239 | xResult = pxFunctionList->C_GetAttributeValue( xSession, 240 | xPublicKeyHandle, 241 | &xTemplate, 242 | 1 ); 243 | } 244 | 245 | /* Prepend the full DER header. */ 246 | if( CKR_OK == xResult ) 247 | { 248 | memcpy( *ppucDerPublicKey, pucEcP256AsnAndOid, sizeof( pucEcP256AsnAndOid ) ); 249 | } 250 | } 251 | 252 | /* Free memory if there was an error after allocation. */ 253 | if( ( NULL != *ppucDerPublicKey ) && ( CKR_OK != xResult ) ) 254 | { 255 | vPortFree( *ppucDerPublicKey ); 256 | *ppucDerPublicKey = NULL; 257 | } 258 | 259 | return xResult; 260 | } 261 | /*-----------------------------------------------------------*/ 262 | 263 | void * pvCalloc( size_t xNumElements, 264 | size_t xSize ) 265 | { 266 | void * pvNew = pvPortMalloc( xNumElements * xSize ); 267 | 268 | if( NULL != pvNew ) 269 | { 270 | memset( pvNew, 0, xNumElements * xSize ); 271 | } 272 | 273 | return pvNew; 274 | } 275 | /*-----------------------------------------------------------*/ 276 | 277 | void aws_mbedtls_mutex_init( mbedtls_threading_mutex_t * mutex ) 278 | { 279 | mutex->mutex = xSemaphoreCreateMutex(); 280 | 281 | if( mutex->mutex != NULL ) 282 | { 283 | mutex->is_valid = 1; 284 | } 285 | else 286 | { 287 | mutex->is_valid = 0; 288 | } 289 | } 290 | /*-----------------------------------------------------------*/ 291 | 292 | void aws_mbedtls_mutex_free( mbedtls_threading_mutex_t * mutex ) 293 | { 294 | if( mutex->is_valid == 1 ) 295 | { 296 | vSemaphoreDelete( mutex->mutex ); 297 | mutex->is_valid = 0; 298 | } 299 | } 300 | /*-----------------------------------------------------------*/ 301 | 302 | int aws_mbedtls_mutex_lock( mbedtls_threading_mutex_t * mutex ) 303 | { 304 | int ret = MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; 305 | 306 | if( mutex->is_valid == 1 ) 307 | { 308 | if( xSemaphoreTake( mutex->mutex, portMAX_DELAY ) ) 309 | { 310 | ret = 0; 311 | } 312 | else 313 | { 314 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; 315 | } 316 | } 317 | 318 | return ret; 319 | } 320 | /*-----------------------------------------------------------*/ 321 | 322 | int aws_mbedtls_mutex_unlock( mbedtls_threading_mutex_t * mutex ) 323 | { 324 | int ret = MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; 325 | 326 | if( mutex->is_valid == 1 ) 327 | { 328 | if( xSemaphoreGive( mutex->mutex ) ) 329 | { 330 | ret = 0; 331 | } 332 | else 333 | { 334 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; 335 | } 336 | } 337 | 338 | return ret; 339 | } 340 | /*-----------------------------------------------------------*/ 341 | 342 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/WIN32.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {C686325E-3261-42F7-AEB1-DDE5280E1CEB} 15 | RTOSDemo 16 | 17 | 18 | 19 | Application 20 | false 21 | MultiByte 22 | v142 23 | 24 | 25 | Application 26 | false 27 | MultiByte 28 | v142 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | <_ProjectFileVersion>10.0.30319.1 44 | .\Debug\ 45 | .\Debug\ 46 | true 47 | .\Release\ 48 | .\Release\ 49 | false 50 | AllRules.ruleset 51 | 52 | 53 | 54 | .\Debug/WIN32.tlb 55 | 56 | 57 | 58 | 59 | Disabled 60 | ..\Common\FreeRTOS_Plus_CLI_Demos\include;..\Common\FreeRTOS_Plus_TCP_Demos\include;..\Common\Demo_IP_Protocols\include;..\..\Source\FreeRTOS-Plus-FAT\include;..\..\Source\FreeRTOS-Plus-FAT\portable\common;..\..\Source\FreeRTOS-Plus-TCP\protocols\include;..\..\Source\FreeRTOS-Plus-TCP\portable\BufferManagement;.\DemoTasks\include;..\..\Source\FreeRTOS-Plus-TCP\portable\Compiler\MSVC;.\WinPCap;..\..\..\FreeRTOS\Source\include;..\..\..\FreeRTOS\Source\portable\MSVC-MingW;..\..\Source\FreeRTOS-Plus-CLI;.\TraceMacros\Example1;..\..\Source\FreeRTOS-Plus-TCP\include;.;%(AdditionalIncludeDirectories) 61 | WIN32;_DEBUG;_CONSOLE;_WIN32_WINNT=0x0500;WINVER=0x400;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 62 | EnableFastChecks 63 | MultiThreadedDLL 64 | .\Debug/WIN32.pch 65 | .\Debug/ 66 | .\Debug/ 67 | .\Debug/ 68 | Level4 69 | true 70 | false 71 | EditAndContinue 72 | /wd4210 /wd4127 /wd4214 /wd4201 /wd4244 /wd4310 %(AdditionalOptions) 73 | true 74 | NotUsing 75 | false 76 | CompileAsC 77 | 78 | 79 | _DEBUG;%(PreprocessorDefinitions) 80 | 0x0c09 81 | 82 | 83 | .\Debug/RTOSDemo.exe 84 | true 85 | true 86 | .\Debug/WIN32.pdb 87 | Console 88 | MachineX86 89 | %(AdditionalDependencies) 90 | .\WinPCap 91 | false 92 | false 93 | 94 | 95 | true 96 | .\Debug/WIN32.bsc 97 | 98 | 99 | 100 | 101 | .\Release/WIN32.tlb 102 | 103 | 104 | 105 | 106 | MaxSpeed 107 | OnlyExplicitInline 108 | _WINSOCKAPI_;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 109 | true 110 | MultiThreaded 111 | true 112 | .\Release/WIN32.pch 113 | .\Release/ 114 | .\Release/ 115 | .\Release/ 116 | Level3 117 | true 118 | ..\Common\Utils;..\Common\ethernet\lwip-1.4.0\ports\win32\WinPCap;..\Common\ethernet\lwip-1.4.0\src\include\ipv4;..\Common\ethernet\lwip-1.4.0\src\include;..\..\Source\include;..\..\Source\portable\MSVC-MingW;..\Common\ethernet\lwip-1.4.0\ports\win32\include;..\Common\Include;.\lwIP_Apps;.;%(AdditionalIncludeDirectories) 119 | 120 | 121 | NDEBUG;%(PreprocessorDefinitions) 122 | 0x0c09 123 | 124 | 125 | .\Release/RTOSDemo.exe 126 | true 127 | .\Release/WIN32.pdb 128 | Console 129 | MachineX86 130 | ..\Common\ethernet\lwip-1.4.0\ports\win32\WinPCap 131 | wpcap.lib;%(AdditionalDependencies) 132 | 133 | 134 | true 135 | .\Release/WIN32.bsc 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | %(AdditionalIncludeDirectories) 164 | %(PreprocessorDefinitions) 165 | %(PreprocessorDefinitions) 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/FreeRTOSFATConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. 3 | All rights reserved 4 | 5 | VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. 6 | 7 | This file is part of the FreeRTOS distribution. 8 | 9 | FreeRTOS is free software; you can redistribute it and/or modify it under 10 | the terms of the GNU General Public License (version 2) as published by the 11 | Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. 12 | 13 | *************************************************************************** 14 | >>! NOTE: The modification to the GPL is included to allow you to !<< 15 | >>! distribute a combined work that includes FreeRTOS without being !<< 16 | >>! obliged to provide the source code for proprietary components !<< 17 | >>! outside of the FreeRTOS kernel. !<< 18 | *************************************************************************** 19 | 20 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY 21 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 22 | FOR A PARTICULAR PURPOSE. Full license text is available on the following 23 | link: http://www.freertos.org/a00114.html 24 | 25 | *************************************************************************** 26 | * * 27 | * FreeRTOS provides completely free yet professionally developed, * 28 | * robust, strictly quality controlled, supported, and cross * 29 | * platform software that is more than just the market leader, it * 30 | * is the industry's de facto standard. * 31 | * * 32 | * Help yourself get started quickly while simultaneously helping * 33 | * to support the FreeRTOS project by purchasing a FreeRTOS * 34 | * tutorial book, reference manual, or both: * 35 | * http://www.FreeRTOS.org/Documentation * 36 | * * 37 | *************************************************************************** 38 | 39 | http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading 40 | the FAQ page "My application does not run, what could be wrong?". Have you 41 | defined configASSERT()? 42 | 43 | http://www.FreeRTOS.org/support - In return for receiving this top quality 44 | embedded software for free we request you assist our global community by 45 | participating in the support forum. 46 | 47 | http://www.FreeRTOS.org/training - Investing in training allows your team to 48 | be as productive as possible as early as possible. Now you can receive 49 | FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers 50 | Ltd, and the world's leading authority on the world's leading RTOS. 51 | 52 | http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, 53 | including FreeRTOS+Trace - an indispensable productivity tool, a DOS 54 | compatible FAT file system, and our tiny thread aware UDP/IP stack. 55 | 56 | http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. 57 | Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. 58 | 59 | http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High 60 | Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS 61 | licenses offer ticketed support, indemnification and commercial middleware. 62 | 63 | http://www.SafeRTOS.com - High Integrity Systems also provide a safety 64 | engineered and independently SIL3 certified version for use in safety and 65 | mission critical applications that require provable dependability. 66 | 67 | 1 tab == 4 spaces! 68 | */ 69 | 70 | #ifndef _FF_CONFIG_H_ 71 | #define _FF_CONFIG_H_ 72 | 73 | /* Must be set to either pdFREERTOS_LITTLE_ENDIAN or pdFREERTOS_BIG_ENDIAN, 74 | depending on the endian of the architecture on which FreeRTOS is running. */ 75 | #define ffconfigBYTE_ORDER pdFREERTOS_LITTLE_ENDIAN 76 | 77 | /* Set to 1 to maintain a current working directory (CWD) for each task that 78 | accesses the file system, allowing relative paths to be used. 79 | 80 | Set to 0 not to use a CWD, in which case full paths must be used for each 81 | file access. */ 82 | #define ffconfigHAS_CWD 1 83 | 84 | /* Set to an index within FreeRTOS's thread local storage array that is free for 85 | use by FreeRTOS+FAT. FreeRTOS+FAT will use two consecutive indexes from this 86 | that set by ffconfigCWD_THREAD_LOCAL_INDEX. The number of thread local storage 87 | pointers provided by FreeRTOS is set by configNUM_THREAD_LOCAL_STORAGE_POINTERS 88 | in FreeRTOSConfig.h */ 89 | #define ffconfigCWD_THREAD_LOCAL_INDEX 0 90 | 91 | /* Set to 1 to include long file name support. Set to 0 to exclude long 92 | file name support. 93 | 94 | If long file name support is excluded then only 8.3 file names can be used. 95 | Long file names will be recognised but ignored. 96 | 97 | Users should familiarise themselves with any patent issues that may 98 | potentially exist around the use of long file names in FAT file systems 99 | before enabling long file name support. */ 100 | #define ffconfigLFN_SUPPORT 1 101 | 102 | /* Only used when ffconfigLFN_SUPPORT is set to 1. 103 | 104 | Set to 1 to include a file's short name when listing a directory, i.e. when 105 | calling findfirst()/findnext(). The short name will be stored in the 106 | 'pcShortName' field of FF_DirEnt_t. 107 | 108 | Set to 0 to only include a file's long name. */ 109 | #define ffconfigINCLUDE_SHORT_NAME 0 110 | 111 | /* Set to 1 to recognise and apply the case bits used by Windows XP+ when 112 | using short file names - storing file names such as "readme.TXT" or 113 | "SETUP.exe" in a short-name entry. This is the recommended setting for 114 | maximum compatibility. 115 | 116 | Set to 0 to ignore the case bits. */ 117 | #define ffconfigSHORTNAME_CASE 1 118 | 119 | /* Only used when ffconfigLFN_SUPPORT is set to 1. 120 | 121 | Set to 1 to use UTF-16 (wide-characters) for file and directory names. 122 | 123 | Set to 0 to use either 8-bit ASCII or UTF-8 for file and directory names 124 | (see the ffconfigUNICODE_UTF8_SUPPORT). */ 125 | #define ffconfigUNICODE_UTF16_SUPPORT 0 126 | 127 | /* Only used when ffconfigLFN_SUPPORT is set to 1. 128 | 129 | Set to 1 to use UTF-8 encoding for file and directory names. 130 | 131 | Set to 0 to use either 8-bit ASCII or UTF-16 for file and directory 132 | names (see the ffconfig_UTF_16_SUPPORT setting). */ 133 | #define ffconfigUNICODE_UTF8_SUPPORT 0 134 | 135 | /* Set to 1 to include FAT12 support. 136 | 137 | Set to 0 to exclude FAT12 support. 138 | 139 | FAT16 and FAT32 are always enabled. */ 140 | #define ffconfigFAT12_SUPPORT 0 141 | 142 | /* When writing and reading data, i/o becomes less efficient if sizes other 143 | than 512 bytes are being used. When set to 1 each file handle will 144 | allocate a 512-byte character buffer to facilitate "unaligned access". */ 145 | #define ffconfigOPTIMISE_UNALIGNED_ACCESS 1 146 | 147 | /* Input and output to a disk uses buffers that are only flushed at the 148 | following times: 149 | 150 | - When a new buffer is needed and no other buffers are available. 151 | - When opening a buffer in READ mode for a sector that has just been changed. 152 | - After creating, removing or closing a file or a directory. 153 | 154 | Normally this is quick enough and it is efficient. If 155 | ffconfigCACHE_WRITE_THROUGH is set to 1 then buffers will also be flushed each 156 | time a buffer is released - which is less efficient but more secure. */ 157 | #define ffconfigCACHE_WRITE_THROUGH 1 158 | 159 | /* In most cases, the FAT table has two identical copies on the disk, 160 | allowing the second copy to be used in the case of a read error. If 161 | 162 | Set to 1 to use both FATs - this is less efficient but more secure. 163 | 164 | Set to 0 to use only one FAT - the second FAT will never be written to. */ 165 | #define ffconfigWRITE_BOTH_FATS 1 166 | 167 | /* Set to 1 to have the number of free clusters and the first free cluster 168 | to be written to the FS info sector each time one of those values changes. 169 | 170 | Set to 0 not to store these values in the FS info sector, making booting 171 | slower, but making changes faster. */ 172 | #define ffconfigWRITE_FREE_COUNT 1 173 | 174 | /* Set to 1 to maintain file and directory time stamps for creation, modify 175 | and last access. 176 | 177 | Set to 0 to exclude time stamps. 178 | 179 | If time support is used, the following function must be supplied: 180 | 181 | time_t FreeRTOS_time( time_t *pxTime ); 182 | 183 | FreeRTOS_time has the same semantics as the standard time() function. */ 184 | #define ffconfigTIME_SUPPORT 1 185 | 186 | /* Set to 1 if the media is removable (such as a memory card). 187 | 188 | Set to 0 if the media is not removable. 189 | 190 | When set to 1 all file handles will be "invalidated" if the media is 191 | extracted. If set to 0 then file handles will not be invalidated. 192 | In that case the user will have to confirm that the media is still present 193 | before every access. */ 194 | #define ffconfigREMOVABLE_MEDIA 0 195 | 196 | /* Set to 1 to determine the disk's free space and the disk's first free 197 | cluster when a disk is mounted. 198 | 199 | Set to 0 to find these two values when they are first needed. Determining 200 | the values can take some time. */ 201 | #define ffconfigMOUNT_FIND_FREE 1 202 | 203 | /* Set to 1 to 'trust' the contents of the 'ulLastFreeCluster' and 204 | ulFreeClusterCount fields. 205 | 206 | Set to 0 not to 'trust' these fields.*/ 207 | #define ffconfigFSINFO_TRUSTED 1 208 | 209 | /* Set to 1 to store recent paths in a cache, enabling much faster access 210 | when the path is deep within a directory structure at the expense of 211 | additional RAM usage. 212 | 213 | Set to 0 to not use a path cache. */ 214 | #define ffconfigPATH_CACHE 1 215 | 216 | /* Only used if ffconfigPATH_CACHE is 1. 217 | 218 | Sets the maximum number of paths that can exist in the patch cache at any 219 | one time. */ 220 | #define ffconfigPATH_CACHE_DEPTH 8 221 | 222 | /* Set to 1 to calculate a HASH value for each existing short file name. 223 | Use of HASH values can improve performance when working with large 224 | directories, or with files that have a similar name. 225 | 226 | Set to 0 not to calculate a HASH value. */ 227 | #define ffconfigHASH_CACHE 1 228 | 229 | /* Only used if ffconfigHASH_CACHE is set to 1 230 | 231 | Set to CRC8 or CRC16 to use 8-bit or 16-bit HASH values respectively. */ 232 | #define ffconfigHASH_FUNCTION CRC16 233 | 234 | /*_RB_ Not in FreeRTOSFFConfigDefaults.h. */ 235 | #define ffconfigHASH_CACHE_DEPTH 64 236 | 237 | /* Set to 1 to add a parameter to ff_mkdir() that allows an entire directory 238 | tree to be created in one go, rather than having to create one directory in 239 | the tree at a time. For example mkdir( "/etc/settings/network", pdTRUE );. 240 | 241 | Set to 0 to use the normal mkdir() semantics (without the additional 242 | parameter). */ 243 | #define ffconfigMKDIR_RECURSIVE 0 244 | 245 | /* Set to a function that will be used for all dynamic memory allocations. 246 | Setting to pvPortMalloc() will use the same memory allocator as FreeRTOS. */ 247 | #define ffconfigMALLOC( size ) pvPortMalloc( size ) 248 | 249 | /* Set to a function that matches the above allocator defined with 250 | ffconfigMALLOC. Setting to vPortFree() will use the same memory free 251 | function as FreeRTOS. */ 252 | #define ffconfigFREE( ptr ) vPortFree( ptr ) 253 | 254 | /* Set to 1 to calculate the free size and volume size as a 64-bit number. 255 | 256 | Set to 0 to calculate these values as a 32-bit number. */ 257 | #define ffconfig64_NUM_SUPPORT 0 258 | 259 | /* Defines the maximum number of partitions (and also logical partitions) 260 | that can be recognised. */ 261 | #define ffconfigMAX_PARTITIONS 1 262 | 263 | /* Defines how many drives can be combined in total. Should be set to at 264 | least 2. */ 265 | #define ffconfigMAX_FILE_SYS 2 266 | 267 | /* In case the low-level driver returns an error 'FF_ERR_DRIVER_BUSY', 268 | the library will pause for a number of ms, defined in 269 | ffconfigDRIVER_BUSY_SLEEP_MS before re-trying. */ 270 | #define ffconfigDRIVER_BUSY_SLEEP_MS 20 271 | 272 | /* Set to 1 to include the ff_fprintf() function. 273 | 274 | Set to 0 to exclude the ff_fprintf() function. 275 | 276 | ff_fprintf() is quite a heavy function because it allocates RAM and 277 | brings in a lot of string and variable argument handling code. If 278 | ff_fprintf() is not being used then the code size can be reduced by setting 279 | ffconfigFPRINTF_SUPPORT to 0. */ 280 | #define ffconfigFPRINTF_SUPPORT 1 281 | 282 | /* ff_fprintf() will allocate a buffer of this size in which it will create 283 | its formatted string. The buffer will be freed before the function 284 | exits. */ 285 | #define ffconfigFPRINTF_BUFFER_LENGTH 128 286 | 287 | /* Set to 1 to inline some internal memory access functions. 288 | 289 | Set to 0 to not inline the memory access functions. */ 290 | #define ffconfigINLINE_MEMORY_ACCESS 1 291 | 292 | /* Officially the only criteria to determine the FAT type (12, 16, or 32 293 | bits) is the total number of clusters: 294 | if( ulNumberOfClusters < 4085 ) : Volume is FAT12 295 | if( ulNumberOfClusters < 65525 ) : Volume is FAT16 296 | if( ulNumberOfClusters >= 65525 ) : Volume is FAT32 297 | Not every formatted device follows the above rule. 298 | 299 | Set to 1 to perform additional checks over and above inspecting the 300 | number of clusters on a disk to determine the FAT type. 301 | 302 | Set to 0 to only look at the number of clusters on a disk to determine the 303 | FAT type. */ 304 | #define ffconfigFAT_CHECK 1 305 | 306 | /* Sets the maximum length for file names, including the path. 307 | Note that the value of this define is directly related to the maximum stack 308 | use of the +FAT library. In some API's, a character buffer of size 309 | 'ffconfigMAX_FILENAME' will be declared on stack. */ 310 | #define ffconfigMAX_FILENAME 250 311 | 312 | /* Defined in main.c as Visual Studio does not provide its own implementation. */ 313 | struct tm *gmtime_r( const time_t *pxTime, struct tm *tmStruct ); 314 | 315 | /* Prototype for the function used to print out. In this case it prints to the 316 | console before the network is connected then a UDP port after the network has 317 | connected. NOTE!!! using Windows system calls such as printf() can cause the 318 | FreeRTOS Win32 port to be unstable as Windows may suspend or block a thread that 319 | should be under the control of the FreeRTOS kernel. */ 320 | #include 321 | #define FF_PRINTF printf 322 | 323 | /* Visual studio does not have an implementation of strcasecmp(). 324 | _RB_ Cannot use FF_NOSTRCASECMP setting as the internal implementation of 325 | strcasecmp() is in ff_dir, whereas it is used in the http server. Also not 326 | sure of why FF_NOSTRCASECMP is being tested against 0 to define the internal 327 | implementation, so I have to set it to 1 here, so it is not defined. */ 328 | #define FF_NOSTRCASECMP 1 329 | 330 | /* Include the recursive function ff_deltree(). The use of recursion does not 331 | conform with the coding standard, so use this function with care! */ 332 | #define ffconfigUSE_DELTREE 1 333 | 334 | #endif /* _FF_CONFIG_H_ */ 335 | 336 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/posix_demo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS POSIX Demo V1.0.0 3 | * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://aws.amazon.com/freertos 23 | * http://www.FreeRTOS.org 24 | */ 25 | 26 | /** 27 | * @brief Demo intro: job distribution with actor model. 28 | * 29 | * This demo simulates job distribution with actor model. 30 | * https://en.wikipedia.org/wiki/Actor_model 31 | * 32 | * In this demo, vStartPOSIXDemo() first creates all mailboxes 33 | * which will be used by actors to send and receive messages. 34 | * Then it spins up two types of actors -- Dispatcher and Workers. 35 | * 36 | * Dispatcher -- Distributing sub-tasks to workers. 37 | * Distribution is done by putting messages into each worker's inbox, 38 | * which is essentially an mqueue. Dispatcher keeps distributing tasks 39 | * until all intended tasks are distributed. 40 | * 41 | * Workers -- Take sub-tasks and perform predefined routine for each type of tasks. 42 | * 43 | * Upon finishing distributing all tasks, Dispatcher will send a "terminate" message to 44 | * each worker. vStartPOSIXDemo() will then join all actor threads and clean up mailboxes. 45 | * 46 | * @note A few assumptions are made in this demo, which a user might have to alter 47 | * if to adopt this model in a new application: 48 | * 49 | * - The upper limit for MQUEUE_NUMBER_OF_WORKERS is set to 10. 50 | * This is not due to physical constraint (e.g. memory), rather to make queue 51 | * names end with a single digit number. 52 | * 53 | * - Message enum is cast to char/uint8_t directly, with the assumption that 54 | * the system is not going to have more than 254 messages, which is often true 55 | * in practice. Could extend bits used in a message to either have more messages 56 | * or include additional arguments for a message. Proper typecasting is needed 57 | * in that case. 58 | * 59 | * - The philosophy is "failure is expected". It is shown in both the way dispatcher 60 | * delivers messages (i.e. messages can be dropped by worker(s)), and also the 61 | * way workers process messages (i.e. workers do not inform dispatcher success or 62 | * failure). 63 | * 64 | * - Following the philosophy, dispatcher shall never use blocking calls to distribute 65 | * tasks. The only exception made here is that dispatcher needs to make sure the 66 | * successful delivery of "terminate" messages. So that, main thread could join 67 | * all actor threads and finish the demo. 68 | */ 69 | 70 | /* FreeRTOS includes. */ 71 | #include "FreeRTOS_POSIX.h" 72 | 73 | /* System headers. */ 74 | #include 75 | #include 76 | #include 77 | 78 | /* Demo includes. */ 79 | #include "posix_demo.h" 80 | 81 | /* FreeRTOS+POSIX. */ 82 | #include "FreeRTOS_POSIX/pthread.h" 83 | #include "FreeRTOS_POSIX/mqueue.h" 84 | #include "FreeRTOS_POSIX/time.h" 85 | #include "FreeRTOS_POSIX/fcntl.h" 86 | #include "FreeRTOS_POSIX/errno.h" 87 | 88 | /* Constants. */ 89 | #define LINE_BREAK "\r\n" 90 | 91 | /** 92 | * @brief Control messages. 93 | * 94 | * uint8_t is sufficient for this enum, that we are going to cast to char directly. 95 | * If ever needed, implement a function to properly typecast. 96 | */ 97 | /**@{ */ 98 | typedef enum ControlMessage 99 | { 100 | eMSG_LOWER_INAVLID = 0x00, /**< Guard, let's not use 0x00 for messages. */ 101 | eWORKER_CTRL_MSG_CONTINUE = 0x01, /**< Dispatcher to worker, distributing another job. */ 102 | eWORKER_CTRL_MSG_EXIT = 0x02, /**< Dispatcher to worker, all jobs are finished and the worker receiving such can exit. */ 103 | 104 | /* define additional messages here */ 105 | 106 | eMSG_UPPER_INVALID = 0xFF /**< Guard, additional tasks shall be defined above. */ 107 | } eControlMessage; 108 | /**@} */ 109 | 110 | /** 111 | * @defgroup Configuration constants for the dispatcher-worker demo. 112 | */ 113 | /**@{ */ 114 | #define MQUEUE_NUMBER_OF_WORKERS ( 4 ) /**< The number of worker threads, each thread has one queue which is used as income box. */ 115 | 116 | #if ( MQUEUE_NUMBER_OF_WORKERS > 10 ) 117 | #error "Please keep MQUEUE_NUMBER_OF_WORKERS < 10." 118 | #endif 119 | 120 | #define MQUEUE_WORKER_QNAME_BASE "/qNode0" /**< Queue name base. */ 121 | #define MQUEUE_WORKER_QNAME_BASE_LEN ( 6 ) /** Queue name base length. */ 122 | 123 | #define MQUEUE_TIMEOUT_SECONDS ( 1 ) /**< Relative timeout for mqueue functions. */ 124 | #define MQUEUE_MAX_NUMBER_OF_MESSAGES_WORKER ( 1 ) /**< Maximum number of messages in a queue. */ 125 | 126 | #define MQUEUE_MSG_WORKER_CTRL_MSG_SIZE sizeof( uint8_t ) /**< Control message size. */ 127 | #define DEMO_ERROR ( -1 ) /**< Any non-zero value would work. */ 128 | /**@} */ 129 | 130 | /** 131 | * @brief Structure used by Worker thread. 132 | */ 133 | /**@{ */ 134 | typedef struct WorkerThreadResources 135 | { 136 | pthread_t pxID; /**< thread ID. */ 137 | mqd_t xInboxID; /**< mqueue inbox ID. */ 138 | } WorkerThreadResources_t; 139 | /**@} */ 140 | 141 | /** 142 | * @brief Structure used by Dispatcher thread. 143 | */ 144 | /**@{ */ 145 | typedef struct DispatcherThreadResources 146 | { 147 | pthread_t pxID; /**< thread ID. */ 148 | mqd_t * pOutboxID; /**< a list of mqueue outbox ID. */ 149 | } DispatcherThreadResources_t; 150 | /**@} */ 151 | 152 | /*-----------------------------------------------------------*/ 153 | 154 | static void * prvWorkerThread( void * pvArgs ) 155 | { 156 | WorkerThreadResources_t pArgList = *( WorkerThreadResources_t * ) pvArgs; 157 | 158 | printf( "Worker thread #[%d] - start %s", ( int ) pArgList.pxID, LINE_BREAK ); 159 | 160 | struct timespec xReceiveTimeout = { 0 }; 161 | 162 | ssize_t xMessageSize = 0; 163 | char pcReceiveBuffer[ MQUEUE_MSG_WORKER_CTRL_MSG_SIZE ] = { 0 }; 164 | 165 | /* This is a worker thread that reacts based on what is sent to its inbox (mqueue). */ 166 | while( true ) 167 | { 168 | clock_gettime( CLOCK_REALTIME, &xReceiveTimeout ); 169 | xReceiveTimeout.tv_sec += MQUEUE_TIMEOUT_SECONDS; 170 | 171 | xMessageSize = mq_receive( pArgList.xInboxID, 172 | pcReceiveBuffer, 173 | MQUEUE_MSG_WORKER_CTRL_MSG_SIZE, 174 | 0 ); 175 | 176 | /* Parse messages */ 177 | if( xMessageSize == MQUEUE_MSG_WORKER_CTRL_MSG_SIZE ) 178 | { 179 | switch( ( int ) pcReceiveBuffer[ 0 ] ) 180 | { 181 | case eWORKER_CTRL_MSG_CONTINUE: 182 | /* Task branch, currently only prints message to screen. */ 183 | /* Could perform tasks here. Could also notify dispatcher upon completion, if desired. */ 184 | printf( "Worker thread #[%d] -- Received eWORKER_CTRL_MSG_CONTINUE %s", ( int ) pArgList.pxID, LINE_BREAK ); 185 | break; 186 | 187 | case eWORKER_CTRL_MSG_EXIT: 188 | printf( "Worker thread #[%d] -- Finished. Exit now. %s", ( int ) pArgList.pxID, LINE_BREAK ); 189 | 190 | return NULL; 191 | 192 | default: 193 | /* Received a message that we don't care or not defined. */ 194 | break; 195 | } 196 | } 197 | else 198 | { 199 | /* Invalid message. Error handling can be done here, if desired. */ 200 | } 201 | } 202 | 203 | /* You should never hit here. */ 204 | /* return NULL; */ 205 | } 206 | 207 | /*-----------------------------------------------------------*/ 208 | 209 | static void * prvDispatcherThread( void * pvArgs ) 210 | { 211 | DispatcherThreadResources_t pArgList = *( DispatcherThreadResources_t * ) pvArgs; 212 | 213 | printf( "Dispatcher thread - start %s", LINE_BREAK ); 214 | 215 | struct timespec xSendTimeout = { 0 }; 216 | 217 | ssize_t xMessageSize = 0; 218 | char pcSendBuffer[ MQUEUE_MSG_WORKER_CTRL_MSG_SIZE ] = { 0 }; 219 | 220 | /* Just for fun, let threads do a total of 100 independent tasks. */ 221 | int i = 0; 222 | const int totalNumOfJobsPerThread = 100; 223 | 224 | /* Distribute 1000 independent tasks to workers, in round-robin fashion. */ 225 | pcSendBuffer[ 0 ] = ( char ) eWORKER_CTRL_MSG_CONTINUE; 226 | 227 | for( i = 0; i < totalNumOfJobsPerThread; i++ ) 228 | { 229 | clock_gettime( CLOCK_REALTIME, &xSendTimeout ); 230 | xSendTimeout.tv_sec += MQUEUE_TIMEOUT_SECONDS; 231 | 232 | printf( "Dispatcher iteration #[%d] -- Sending msg to worker thread #[%d]. %s", i, ( int ) pArgList.pOutboxID[ i % MQUEUE_NUMBER_OF_WORKERS ], LINE_BREAK ); 233 | 234 | xMessageSize = mq_timedsend( pArgList.pOutboxID[ i % MQUEUE_NUMBER_OF_WORKERS ], 235 | pcSendBuffer, 236 | MQUEUE_MSG_WORKER_CTRL_MSG_SIZE, 237 | 0, 238 | &xSendTimeout ); 239 | 240 | if( xMessageSize != 0 ) 241 | { 242 | /* This error is acceptable in our setup. 243 | * Since inbox for each thread fits only one message. 244 | * In reality, balance inbox size, message arrival rate, and message drop rate. */ 245 | printf( "An acceptable failure -- dispatcher failed to send eWORKER_CTRL_MSG_CONTINUE to outbox ID: %x. errno %d %s", 246 | ( int ) pArgList.pOutboxID[ i % MQUEUE_NUMBER_OF_WORKERS ], errno, LINE_BREAK ); 247 | } 248 | } 249 | 250 | /* Control thread is now done with distributing jobs. Tell workers they are done. */ 251 | pcSendBuffer[ 0 ] = ( char ) eWORKER_CTRL_MSG_EXIT; 252 | 253 | for( i = 0; i < MQUEUE_NUMBER_OF_WORKERS; i++ ) 254 | { 255 | printf( "Dispatcher [%d] -- Sending eWORKER_CTRL_MSG_EXIT to worker thread #[%d]. %s", i, ( int ) pArgList.pOutboxID[ i % MQUEUE_NUMBER_OF_WORKERS ], LINE_BREAK ); 256 | 257 | /* This is a blocking call, to guarantee worker thread exits. */ 258 | xMessageSize = mq_send( pArgList.pOutboxID[ i % MQUEUE_NUMBER_OF_WORKERS ], 259 | pcSendBuffer, 260 | MQUEUE_MSG_WORKER_CTRL_MSG_SIZE, 261 | 0 ); 262 | } 263 | 264 | return NULL; 265 | } 266 | 267 | /*-----------------------------------------------------------*/ 268 | 269 | /** 270 | * @brief Job distribution with actor model. 271 | * 272 | * See the top of this file for detailed description. 273 | */ 274 | void vStartPOSIXDemo( void *pvParameters ) 275 | { 276 | int i = 0; 277 | int iStatus = 0; 278 | 279 | /* Remove warnings about unused parameters. */ 280 | ( void ) pvParameters; 281 | 282 | /* Handles of the threads and related resources. */ 283 | DispatcherThreadResources_t pxDispatcher = { 0 }; 284 | WorkerThreadResources_t pxWorkers[ MQUEUE_NUMBER_OF_WORKERS ] = { { 0 } }; 285 | mqd_t workerMqueues[ MQUEUE_NUMBER_OF_WORKERS ] = { 0 }; 286 | 287 | struct mq_attr xQueueAttributesWorker = 288 | { 289 | .mq_flags = 0, 290 | .mq_maxmsg = MQUEUE_MAX_NUMBER_OF_MESSAGES_WORKER, 291 | .mq_msgsize = MQUEUE_MSG_WORKER_CTRL_MSG_SIZE, 292 | .mq_curmsgs = 0 293 | }; 294 | 295 | pxDispatcher.pOutboxID = workerMqueues; 296 | 297 | /* Create message queues for each worker thread. */ 298 | for( i = 0; i < MQUEUE_NUMBER_OF_WORKERS; i++ ) 299 | { 300 | /* Prepare a unique queue name for each worker. */ 301 | char qName[] = MQUEUE_WORKER_QNAME_BASE; 302 | qName[ MQUEUE_WORKER_QNAME_BASE_LEN - 1 ] = qName[ MQUEUE_WORKER_QNAME_BASE_LEN - 1 ] + i; 303 | 304 | /* Open a queue with -- 305 | * O_CREAT -- create a message queue. 306 | * O_RDWR -- both receiving and sending messages. 307 | */ 308 | pxWorkers[ i ].xInboxID = mq_open( qName, 309 | O_CREAT | O_RDWR, 310 | ( mode_t ) 0, 311 | &xQueueAttributesWorker ); 312 | 313 | if( pxWorkers[ i ].xInboxID == ( mqd_t ) -1 ) 314 | { 315 | printf( "Invalid inbox (mqueue) for worker. %s", LINE_BREAK ); 316 | iStatus = DEMO_ERROR; 317 | break; 318 | } 319 | 320 | /* Outboxes of dispatcher thread is the inboxes of all worker threads. */ 321 | pxDispatcher.pOutboxID[ i ] = pxWorkers[ i ].xInboxID; 322 | } 323 | 324 | /* Create and start Worker threads. */ 325 | if( iStatus == 0 ) 326 | { 327 | for( i = 0; i < MQUEUE_NUMBER_OF_WORKERS; i++ ) 328 | { 329 | ( void ) pthread_create( &( pxWorkers[ i ].pxID ), NULL, prvWorkerThread, &pxWorkers[ i ] ); 330 | } 331 | 332 | /* Create and start dispatcher thread. */ 333 | ( void ) pthread_create( &( pxDispatcher.pxID ), NULL, prvDispatcherThread, &pxDispatcher ); 334 | 335 | /* Actors will do predefined tasks in threads. Current implementation is that 336 | * dispatcher actor notifies worker actors to terminate upon finishing distributing tasks. */ 337 | 338 | /* Wait for worker threads to join. */ 339 | for( i = 0; i < MQUEUE_NUMBER_OF_WORKERS; i++ ) 340 | { 341 | ( void ) pthread_join( pxWorkers[ i ].pxID, NULL ); 342 | } 343 | 344 | /* Wait for dispatcher thread to join. */ 345 | ( void ) pthread_join( pxDispatcher.pxID, NULL ); 346 | } 347 | 348 | /* Close and unlink worker message queues. */ 349 | for( i = 0; i < MQUEUE_NUMBER_OF_WORKERS; i++ ) 350 | { 351 | char qName[] = MQUEUE_WORKER_QNAME_BASE; 352 | qName[ MQUEUE_WORKER_QNAME_BASE_LEN - 1 ] = qName[ MQUEUE_WORKER_QNAME_BASE_LEN - 1 ] + i; 353 | 354 | if( pxWorkers[ i ].xInboxID != NULL ) 355 | { 356 | ( void ) mq_close( pxWorkers[ i ].xInboxID ); 357 | ( void ) mq_unlink( qName ); 358 | } 359 | } 360 | 361 | /* Have something on console. */ 362 | if( iStatus == 0 ) 363 | { 364 | printf( "All threads finished. %s", LINE_BREAK ); 365 | } 366 | else 367 | { 368 | printf( "Queues did not get initialized properly. Did not run demo. %s", LINE_BREAK ); 369 | } 370 | 371 | /* This task was created with the native xTaskCreate() API function, so 372 | must not run off the end of its implementing thread. */ 373 | vTaskDelete( NULL ); 374 | } 375 | -------------------------------------------------------------------------------- /FreeRTOS-Labs/Demo/FreeRTOS_Plus_FAT_Windows_Simulator/printf-stdarg.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2001, 2002 Georges Menie (www.menie.org) 3 | stdarg version contributed by Christian Ettinger 4 | 5 | This program is free software; you can redistribute it and/or modify 6 | it under the terms of the GNU Lesser General Public License as published by 7 | the Free Software Foundation; either version 2 of the License, or 8 | (at your option) any later version. 9 | 10 | Changes for the FreeRTOS ports: 11 | 12 | - The dot in "%-8.8s" 13 | - The specifiers 'l' (long) and 'L' (long long) 14 | - The specifier 'u' for unsigned 15 | - Dot notation for IP addresses: 16 | sprintf("IP = %xip\n", 0xC0A80164); 17 | will produce "IP = 192.168.1.100\n" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "FreeRTOS.h" 26 | 27 | #define PAD_RIGHT 1 28 | #define PAD_ZERO 2 29 | 30 | /* 31 | * Return 1 for readable, 2 for writeable, 3 for both. 32 | * Function must be provided by the application. 33 | */ 34 | extern BaseType_t xApplicationMemoryPermissions( uint32_t aAddress ); 35 | 36 | extern void vOutputChar( const char cChar, const TickType_t xTicksToWait ); 37 | static const TickType_t xTicksToWait = pdMS_TO_TICKS( 20 ); 38 | 39 | struct xPrintFlags 40 | { 41 | int base; 42 | int width; 43 | int printLimit; 44 | unsigned 45 | pad : 8, 46 | letBase : 8, 47 | isSigned : 1, 48 | isNumber : 1, 49 | long32 : 1, 50 | long64 : 1; 51 | }; 52 | 53 | struct SStringBuf 54 | { 55 | char *str; 56 | const char *orgStr; 57 | const char *nulPos; 58 | int curLen; 59 | struct xPrintFlags flags; 60 | }; 61 | 62 | static void strbuf_init( struct SStringBuf *apStr, char *apBuf, const char *apMaxStr ) 63 | { 64 | apStr->str = apBuf; 65 | apStr->orgStr = apBuf; 66 | apStr->nulPos = apMaxStr-1; 67 | apStr->curLen = 0; 68 | 69 | memset( &apStr->flags, '\0', sizeof( apStr->flags ) ); 70 | } 71 | /*-----------------------------------------------------------*/ 72 | 73 | static BaseType_t strbuf_printchar( struct SStringBuf *apStr, int c ) 74 | { 75 | if( apStr->str == NULL ) 76 | { 77 | vOutputChar( ( char ) c, xTicksToWait ); 78 | apStr->curLen++; 79 | return pdTRUE; 80 | } 81 | if( apStr->str < apStr->nulPos ) 82 | { 83 | *( apStr->str++ ) = c; 84 | apStr->curLen++; 85 | return pdTRUE; 86 | } 87 | if( apStr->str == apStr->nulPos ) 88 | { 89 | *( apStr->str++ ) = '\0'; 90 | } 91 | return pdFALSE; 92 | } 93 | /*-----------------------------------------------------------*/ 94 | 95 | static portINLINE BaseType_t strbuf_printchar_inline( struct SStringBuf *apStr, int c ) 96 | { 97 | if( apStr->str == NULL ) 98 | { 99 | vOutputChar( ( char ) c, xTicksToWait ); 100 | if( c == 0 ) 101 | { 102 | return pdFALSE; 103 | } 104 | apStr->curLen++; 105 | return pdTRUE; 106 | } 107 | if( apStr->str < apStr->nulPos ) 108 | { 109 | *(apStr->str++) = c; 110 | if( c == 0 ) 111 | { 112 | return pdFALSE; 113 | } 114 | apStr->curLen++; 115 | return pdTRUE; 116 | } 117 | if( apStr->str == apStr->nulPos ) 118 | { 119 | *( apStr->str++ ) = '\0'; 120 | } 121 | return pdFALSE; 122 | } 123 | /*-----------------------------------------------------------*/ 124 | 125 | static portINLINE int i2hex( int aCh ) 126 | { 127 | int iResult; 128 | 129 | if( aCh < 10 ) 130 | { 131 | iResult = '0' + aCh; 132 | } 133 | else 134 | { 135 | iResult = 'A' + aCh - 10; 136 | } 137 | 138 | return iResult; 139 | } 140 | /*-----------------------------------------------------------*/ 141 | 142 | static BaseType_t prints(struct SStringBuf *apBuf, const char *apString ) 143 | { 144 | register int padchar = ' '; 145 | int i,len; 146 | 147 | if( xApplicationMemoryPermissions( ( uint32_t )apString ) == 0 ) 148 | { 149 | /* The user has probably made a mistake with the parameter 150 | for '%s', the memory is not readbale. */ 151 | apString = "INV_MEM"; 152 | } 153 | 154 | if( apBuf->flags.width > 0 ) 155 | { 156 | register int len = 0; 157 | register const char *ptr; 158 | for( ptr = apString; *ptr; ++ptr ) 159 | { 160 | ++len; 161 | } 162 | 163 | if( len >= apBuf->flags.width ) 164 | { 165 | apBuf->flags.width = 0; 166 | } 167 | else 168 | { 169 | apBuf->flags.width -= len; 170 | } 171 | 172 | if( apBuf->flags.pad & PAD_ZERO ) 173 | { 174 | padchar = '0'; 175 | } 176 | } 177 | if( ( apBuf->flags.pad & PAD_RIGHT ) == 0 ) 178 | { 179 | for( ; apBuf->flags.width > 0; --apBuf->flags.width ) 180 | { 181 | if( strbuf_printchar( apBuf, padchar ) == 0 ) 182 | { 183 | return pdFALSE; 184 | } 185 | } 186 | } 187 | if( ( apBuf->flags.isNumber == pdTRUE ) && ( apBuf->flags.pad == pdTRUE ) ) 188 | { 189 | /* The string to print represents an integer number. 190 | * In this case, printLimit is the min number of digits to print 191 | * If the length of the number to print is less than the min nb of i 192 | * digits to display, we add 0 before printing the number 193 | */ 194 | len = strlen( apString ); 195 | 196 | if( len < apBuf->flags.printLimit ) 197 | { 198 | i = apBuf->flags.printLimit - len; 199 | for( ; i; i-- ) 200 | { 201 | if( strbuf_printchar( apBuf, '0' ) == 0 ) 202 | { 203 | return pdFALSE; 204 | } 205 | } 206 | } 207 | } 208 | /* The string to print is not the result of a number conversion to ascii. 209 | * For a string, printLimit is the max number of characters to display 210 | */ 211 | for( ; apBuf->flags.printLimit && *apString ; ++apString, --apBuf->flags.printLimit ) 212 | { 213 | if( !strbuf_printchar( apBuf, *apString ) ) 214 | { 215 | return pdFALSE; 216 | } 217 | } 218 | 219 | for( ; apBuf->flags.width > 0; --apBuf->flags.width ) 220 | { 221 | if( !strbuf_printchar( apBuf, padchar ) ) 222 | { 223 | return pdFALSE; 224 | } 225 | } 226 | 227 | return pdTRUE; 228 | } 229 | /*-----------------------------------------------------------*/ 230 | 231 | /* the following should be enough for 32 bit int */ 232 | #define PRINT_BUF_LEN 12 /* to print 4294967296 */ 233 | 234 | #if SPRINTF_LONG_LONG 235 | #warning 64-bit libraries will be included as well 236 | static BaseType_t printll( struct SStringBuf *apBuf, long long i ) 237 | { 238 | char print_buf[ 2 * PRINT_BUF_LEN ]; 239 | register char *s; 240 | register int t, neg = 0; 241 | register unsigned long long u = i; 242 | lldiv_t lldiv_result; 243 | 244 | /* typedef struct 245 | * { 246 | * long long int quot; // quotient 247 | * long long int rem; // remainder 248 | * } lldiv_t; 249 | */ 250 | 251 | apBuf->flags.isNumber = pdTRUE; /* Parameter for prints */ 252 | if( i == 0LL ) 253 | { 254 | print_buf[ 0 ] = '0'; 255 | print_buf[ 1 ] = '\0'; 256 | return prints( apBuf, print_buf ); 257 | } 258 | 259 | if( ( apBuf->flags.isSigned == pdTRUE ) && ( apBuf->flags.base == 10 ) && ( i < 0LL ) ) 260 | { 261 | neg = 1; 262 | u = -i; 263 | } 264 | 265 | s = print_buf + sizeof( print_buf ) - 1; 266 | 267 | *s = '\0'; 268 | /* 18446744073709551616 */ 269 | while( u != 0 ) 270 | { 271 | lldiv_result = lldiv( u, ( unsigned long long ) apBuf->flags.base ); 272 | t = lldiv_result.rem; 273 | if( t >= 10 ) 274 | { 275 | t += apBuf->flags.letBase - '0' - 10; 276 | } 277 | *( --s ) = t + '0'; 278 | u = lldiv_result.quot; 279 | } 280 | 281 | if( neg != 0 ) 282 | { 283 | if( ( apBuf->flags.width != 0 ) && ( apBuf->flags.pad & PAD_ZERO ) ) 284 | { 285 | if( !strbuf_printchar( apBuf, '-' ) ) 286 | { 287 | return pdFALSE; 288 | } 289 | --apBuf->flags.width; 290 | } 291 | else 292 | { 293 | *( --s ) = '-'; 294 | } 295 | } 296 | 297 | return prints( apBuf, s ); 298 | } 299 | #endif /* SPRINTF_LONG_LONG */ 300 | /*-----------------------------------------------------------*/ 301 | 302 | static BaseType_t printi( struct SStringBuf *apBuf, int i ) 303 | { 304 | char print_buf[ PRINT_BUF_LEN ]; 305 | register char *s; 306 | register int t, neg = 0; 307 | register unsigned int u = i; 308 | register unsigned base = apBuf->flags.base; 309 | 310 | apBuf->flags.isNumber = pdTRUE; /* Parameter for prints */ 311 | 312 | if( i == 0 ) 313 | { 314 | print_buf[ 0 ] = '0'; 315 | print_buf[ 1 ] = '\0'; 316 | return prints( apBuf, print_buf ); 317 | } 318 | 319 | if( ( apBuf->flags.isSigned == pdTRUE ) && ( base == 10 ) && ( i < 0 ) ) 320 | { 321 | neg = 1; 322 | u = -i; 323 | } 324 | 325 | s = print_buf + sizeof( print_buf ) - 1; 326 | 327 | *s = '\0'; 328 | switch( base ) 329 | { 330 | case 16: 331 | while( u != 0 ) 332 | { 333 | t = u & 0xF; 334 | if( t >= 10 ) 335 | { 336 | t += apBuf->flags.letBase - '0' - 10; 337 | } 338 | *( --s ) = t + '0'; 339 | u >>= 4; 340 | } 341 | break; 342 | 343 | case 8: 344 | case 10: 345 | /* GCC compiles very efficient */ 346 | while( u ) 347 | { 348 | t = u % base; 349 | *( --s ) = t + '0'; 350 | u /= base; 351 | } 352 | break; 353 | /* 354 | // The generic case, not yet in use 355 | default: 356 | while( u ) 357 | { 358 | t = u % base; 359 | if( t >= 10) 360 | { 361 | t += apBuf->flags.letBase - '0' - 10; 362 | } 363 | *( --s ) = t + '0'; 364 | u /= base; 365 | } 366 | break; 367 | */ 368 | } 369 | 370 | if( neg != 0 ) 371 | { 372 | if( apBuf->flags.width && (apBuf->flags.pad & PAD_ZERO ) ) 373 | { 374 | if( strbuf_printchar( apBuf, '-' ) == 0 ) 375 | { 376 | return pdFALSE; 377 | } 378 | --apBuf->flags.width; 379 | } 380 | else 381 | { 382 | *( --s ) = '-'; 383 | } 384 | } 385 | 386 | return prints( apBuf, s ); 387 | } 388 | /*-----------------------------------------------------------*/ 389 | 390 | static BaseType_t printIp(struct SStringBuf *apBuf, unsigned i ) 391 | { 392 | char print_buf[16]; 393 | 394 | sprintf( print_buf, "%u.%u.%u.%u", 395 | i >> 24, 396 | ( i >> 16 ) & 0xff, 397 | ( i >> 8 ) & 0xff, 398 | i & 0xff ); 399 | apBuf->flags.isNumber = pdTRUE; /* Parameter for prints */ 400 | prints( apBuf, print_buf ); 401 | 402 | return pdTRUE; 403 | } 404 | /*-----------------------------------------------------------*/ 405 | 406 | static void tiny_print( struct SStringBuf *apBuf, const char *format, va_list args ) 407 | { 408 | char scr[2]; 409 | 410 | for( ; ; ) 411 | { 412 | int ch = *( format++ ); 413 | 414 | if( ch != '%' ) 415 | { 416 | do 417 | { 418 | /* Put the most like flow in a small loop */ 419 | if( strbuf_printchar_inline( apBuf, ch ) == 0 ) 420 | { 421 | return; 422 | } 423 | ch = *( format++ ); 424 | } while( ch != '%' ); 425 | } 426 | ch = *( format++ ); 427 | /* Now ch has character after '%', format pointing to next */ 428 | 429 | if( ch == '\0' ) 430 | { 431 | break; 432 | } 433 | if( ch == '%' ) 434 | { 435 | if( strbuf_printchar( apBuf, ch ) == 0 ) 436 | { 437 | return; 438 | } 439 | continue; 440 | } 441 | memset( &apBuf->flags, '\0', sizeof( apBuf->flags ) ); 442 | 443 | if( ch == '-' ) 444 | { 445 | ch = *( format++ ); 446 | apBuf->flags.pad = PAD_RIGHT; 447 | } 448 | while( ch == '0' ) 449 | { 450 | ch = *( format++ ); 451 | apBuf->flags.pad |= PAD_ZERO; 452 | } 453 | if( ch == '*' ) 454 | { 455 | ch = *( format++ ); 456 | apBuf->flags.width = va_arg( args, int ); 457 | } 458 | else 459 | { 460 | while( ch >= '0' && ch <= '9' ) 461 | { 462 | apBuf->flags.width *= 10; 463 | apBuf->flags.width += ch - '0'; 464 | ch = *( format++ ); 465 | } 466 | } 467 | if( ch == '.' ) 468 | { 469 | ch = *( format++ ); 470 | if( ch == '*' ) 471 | { 472 | apBuf->flags.printLimit = va_arg( args, int ); 473 | ch = *( format++ ); 474 | } 475 | else 476 | { 477 | while( ch >= '0' && ch <= '9' ) 478 | { 479 | apBuf->flags.printLimit *= 10; 480 | apBuf->flags.printLimit += ch - '0'; 481 | ch = *( format++ ); 482 | } 483 | } 484 | } 485 | if( apBuf->flags.printLimit == 0 ) 486 | { 487 | apBuf->flags.printLimit--; /* -1: make it unlimited */ 488 | } 489 | if( ch == 's' ) 490 | { 491 | register char *s = ( char * )va_arg( args, int ); 492 | if( prints( apBuf, s ? s : "(null)" ) == 0 ) 493 | { 494 | break; 495 | } 496 | continue; 497 | } 498 | if( ch == 'c' ) 499 | { 500 | /* char are converted to int then pushed on the stack */ 501 | scr[0] = ( char ) va_arg( args, int ); 502 | 503 | if( strbuf_printchar( apBuf, scr[0] ) == 0 ) 504 | { 505 | return; 506 | } 507 | 508 | continue; 509 | } 510 | if( ch == 'l' ) 511 | { 512 | ch = *( format++ ); 513 | apBuf->flags.long32 = 1; 514 | /* Makes not difference as u32 == long */ 515 | } 516 | if( ch == 'L' ) 517 | { 518 | ch = *( format++ ); 519 | apBuf->flags.long64 = 1; 520 | /* Does make a difference */ 521 | } 522 | apBuf->flags.base = 10; 523 | apBuf->flags.letBase = 'a'; 524 | 525 | if( ch == 'd' || ch == 'u' ) 526 | { 527 | apBuf->flags.isSigned = ( ch == 'd' ); 528 | #if SPRINTF_LONG_LONG 529 | if( apBuf->flags.long64 != pdFALSE ) 530 | { 531 | if( printll( apBuf, va_arg( args, long long ) ) == 0 ) 532 | { 533 | break; 534 | } 535 | } else 536 | #endif /* SPRINTF_LONG_LONG */ 537 | if( printi( apBuf, va_arg( args, int ) ) == 0 ) 538 | { 539 | break; 540 | } 541 | continue; 542 | } 543 | 544 | apBuf->flags.base = 16; /* From here all hexadecimal */ 545 | 546 | if( ch == 'x' && format[0] == 'i' && format[1] == 'p' ) 547 | { 548 | format += 2; /* eat the "xi" of "xip" */ 549 | /* Will use base 10 again */ 550 | if( printIp( apBuf, va_arg( args, int ) ) == 0 ) 551 | { 552 | break; 553 | } 554 | continue; 555 | } 556 | if( ch == 'x' || ch == 'X' || ch == 'p' || ch == 'o' ) 557 | { 558 | if( ch == 'X' ) 559 | { 560 | apBuf->flags.letBase = 'A'; 561 | } 562 | else if( ch == 'o' ) 563 | { 564 | apBuf->flags.base = 8; 565 | } 566 | #if SPRINTF_LONG_LONG 567 | if( apBuf->flags.long64 != pdFALSE ) 568 | { 569 | if( printll( apBuf, va_arg( args, long long ) ) == 0 ) 570 | { 571 | break; 572 | } 573 | } else 574 | #endif /* SPRINTF_LONG_LONG */ 575 | if( printi( apBuf, va_arg( args, int ) ) == 0 ) 576 | { 577 | break; 578 | } 579 | continue; 580 | } 581 | } 582 | strbuf_printchar( apBuf, '\0' ); 583 | } 584 | /*-----------------------------------------------------------*/ 585 | 586 | int vsnprintf( char *apBuf, size_t aMaxLen, const char *apFmt, va_list args ) 587 | { 588 | struct SStringBuf strBuf; 589 | strbuf_init( &strBuf, apBuf, ( const char* )apBuf + aMaxLen ); 590 | tiny_print( &strBuf, apFmt, args ); 591 | 592 | return strBuf.curLen; 593 | } 594 | /*-----------------------------------------------------------*/ 595 | 596 | int snprintf( char *apBuf, size_t aMaxLen, const char *apFmt, ... ) 597 | { 598 | va_list args; 599 | 600 | va_start( args, apFmt ); 601 | struct SStringBuf strBuf; 602 | strbuf_init( &strBuf, apBuf, ( const char* )apBuf + aMaxLen ); 603 | tiny_print( &strBuf, apFmt, args ); 604 | va_end( args ); 605 | 606 | return strBuf.curLen; 607 | } 608 | /*-----------------------------------------------------------*/ 609 | 610 | int sprintf( char *apBuf, const char *apFmt, ... ) 611 | { 612 | va_list args; 613 | 614 | va_start( args, apFmt ); 615 | struct SStringBuf strBuf; 616 | strbuf_init( &strBuf, apBuf, ( const char * )apBuf + 1024 ); 617 | tiny_print( &strBuf, apFmt, args ); 618 | va_end( args ); 619 | 620 | return strBuf.curLen; 621 | } 622 | /*-----------------------------------------------------------*/ 623 | 624 | int vsprintf( char *apBuf, const char *apFmt, va_list args ) 625 | { 626 | struct SStringBuf strBuf; 627 | strbuf_init( &strBuf, apBuf, ( const char* ) apBuf + 1024 ); 628 | tiny_print( &strBuf, apFmt, args ); 629 | 630 | return strBuf.curLen; 631 | } 632 | /*-----------------------------------------------------------*/ 633 | 634 | const char *mkSize (unsigned long long aSize, char *apBuf, int aLen) 635 | { 636 | static char retString[33]; 637 | size_t gb, mb, kb, sb; 638 | 639 | if (apBuf == NULL) { 640 | apBuf = retString; 641 | aLen = sizeof( retString ); 642 | } 643 | gb = aSize / (1024*1024*1024); 644 | aSize -= gb * (1024*1024*1024); 645 | mb = aSize / (1024*1024); 646 | aSize -= mb * (1024*1024); 647 | kb = aSize / (1024); 648 | aSize -= kb * (1024); 649 | sb = aSize; 650 | if( gb ) 651 | { 652 | snprintf (apBuf, aLen, "%u.%02u GB", ( unsigned ) gb, ( unsigned ) ( ( 100 * mb ) / 1024ul ) ); 653 | } 654 | else if( mb ) 655 | { 656 | snprintf (apBuf, aLen, "%u.%02u MB", ( unsigned ) mb, ( unsigned ) ( ( 100 * kb) / 1024ul ) ); 657 | } 658 | else if( kb != 0ul ) 659 | { 660 | snprintf (apBuf, aLen, "%u.%02u KB", ( unsigned ) kb, ( unsigned ) ( ( 100 * sb) / 1024ul ) ); 661 | } 662 | else 663 | { 664 | snprintf (apBuf, aLen, "%u bytes", ( unsigned ) sb); 665 | } 666 | return apBuf; 667 | } 668 | --------------------------------------------------------------------------------