Rename the FreeRTOS_Plus_IoT_SDK directory to FreeRTOS_IoT_Libraries.

This commit is contained in:
Richard Barry 2019-07-16 18:21:42 +00:00
parent 290c8cedfd
commit 3c3b32b8e4
41 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,596 @@
/*
* FreeRTOS Kernel V10.2.1
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
//_RB_ Add link to docs here.
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
/* Standard includes. */
#include <stdio.h>
/* IoT SDK includes. */
#include "iot_taskpool.h"
/* The priority at which that tasks in the task pool (the worker tasks) get
created. */
#define tpTASK_POOL_WORKER_PRIORITY 1
/* The number of jobs created in the example functions that create more than
one job. */
#define tpJOBS_TO_CREATE 5
/*
* Prototypes for the functions that demonstrate the task pool API.
* See the implementation of the prvTaskPoolDemoTask() function within this file
* for a description of the individual functions. A configASSERT() is hit if
* any of the demos encounter any unexpected behaviour.
*/
static void prvExample_BasicSingleJob( void );
static void prvExample_DeferredJobAndCancellingJobs( void );
static void prvExample_BasicRecyclableJob( void );
static void prvExample_ReuseRecyclableJobFromLowPriorityTask( void );
static void prvExample_ReuseRecyclableJobFromHighPriorityTask( void );
/*
* Prototypes of the callback functions used in the examples. The callback
* simply sends a signal (in the form of a direct task notification) to the
* prvTaskPoolDemoTask() task to let the task know that the callback execute.
* The handle of the prvTaskPoolDemoTask() task is not accessed directly, but
* instead passed into the task pool job as the job's context.
*/
static void prvSimpleTaskNotifyCallback( IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext );
/*
* The task used to demonstrate the task pool API. This task just loops through
* each demo in turn.
*/
static void prvTaskPoolDemoTask( void *pvParameters );
/*-----------------------------------------------------------*/
/* Parameters used to create the system task pool - see TBD for more information
as the task pool used in this example is a slimmed down version of the full
library - the slimmed down version being intended specifically for FreeRTOS
kernel use cases. */
static const IotTaskPoolInfo_t xTaskPoolParameters = {
/* Minimum number of threads in a task pool.
Note the slimmed down version of the task
pool used by this library does not autoscale
the number of tasks in the pool so in this
case this sets the number of tasks in the
pool. */
2,
/* Maximum number of threads in a task pool.
Note the slimmed down version of the task
pool used by this library does not autoscale
the number of tasks in the pool so in this
case this parameter is just ignored. */
2,
/* Stack size for every task pool thread - in
bytes, hence multiplying by the number of bytes
in a word as configMINIMAL_STACK_SIZE is
specified in words. */
configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ),
/* Priority for every task pool thread. */
tpTASK_POOL_WORKER_PRIORITY,
};
/*-----------------------------------------------------------*/
void vStartSimpleTaskPoolDemo( void )
{
/* This example uses a single application task, which in turn is used to
create and send jobs to task pool tasks. */
xTaskCreate( prvTaskPoolDemoTask, /* Function that implements the task. */
"PoolDemo", /* Text name for the task - only used for debugging. */
configMINIMAL_STACK_SIZE, /* Size of stack (in words, not bytes) to allocate for the task. */
NULL, /* Task parameter - not used in this case. */
tskIDLE_PRIORITY, /* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */
NULL ); /* Used to pass out a handle to the created tsak - not used in this case. */
}
/*-----------------------------------------------------------*/
static void prvTaskPoolDemoTask( void *pvParameters )
{
IotTaskPoolError_t xResult;
uint32_t ulLoops = 0;
/* Remove compiler warnings about unused parameters. */
( void ) pvParameters;
/* The task pool must be created before it can be used. The system task
pool is the task pool managed by the task pool library itself - the storage
used by the task pool is provided by the library. */
xResult = IotTaskPool_CreateSystemTaskPool( &xTaskPoolParameters );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* Attempting to create the task pool again should then appear to succeed
(in case it is initialised by more than one library), but have no effect. */
xResult = IotTaskPool_CreateSystemTaskPool( &xTaskPoolParameters );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
for( ;; )
{
/* Demonstrate the most basic use case where a non persistent job is
created and scheduled to run immediately. The task pool worker tasks
(in which the job callback function executes) have a priority above the
priority of this task so the job's callback executes as soon as it is
scheduled. */
prvExample_BasicSingleJob();
/* Demonstrate a job being scheduled to run at some time in the
future, and how a job scheduled to run in the future can be cancelled if
it has not yet started executing. */
prvExample_DeferredJobAndCancellingJobs();
/* Demonstrate the most basic use of a recyclable job. This is similar
to prvExample_BasicSingleJob() but using a recyclable job. Creating a
recyclable job will re-use a previously created and now spare job from
the task pool's job cache if one is available, or otherwise dynamically
create a new job if a spare job is not available in the cache but space
remains in the cache. */
prvExample_BasicRecyclableJob();
/* Demonstrate multiple recyclable jobs being created, used, and then
re-used. In this the task pool worker tasks (in which the job callback
functions execute) have a priority above the priority of this task so
the job's callback functions execute as soon as they are scheduled. */
prvExample_ReuseRecyclableJobFromLowPriorityTask();
/* Again demonstrate multiple recyclable jobs being used, but this time
the priority of the task pool worker tasks (in which the job callback
functions execute) are lower than the priority of this task so the job's
callback functions don't execute until this task enteres the blocked
state. */
prvExample_ReuseRecyclableJobFromHighPriorityTask();
ulLoops++;
if( ( ulLoops % 10UL ) == 0 )
{
printf( "prvTaskPoolDemoTask() performed %u iterations without hitting an assert.\r\n", ulLoops );
fflush( stdout );
}
}
}
/*-----------------------------------------------------------*/
static void prvSimpleTaskNotifyCallback( IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext )
{
TaskHandle_t xTaskToNotify = ( TaskHandle_t ) pUserContext;
/* Remove warnings about unused parameters. */
( void ) pTaskPool;
( void ) pJob;
/* Notify the task that created this job. */
xTaskNotifyGive( xTaskToNotify );
}
/*-----------------------------------------------------------*/
static void prvExample_BasicSingleJob( void )
{
IotTaskPoolJobStorage_t xJobStorage;
IotTaskPoolJob_t xJob;
IotTaskPoolError_t xResult;
uint32_t ulReturn;
const uint32_t ulNoFlags = 0UL;
const TickType_t xNoDelay = ( TickType_t ) 0;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
IotTaskPoolJobStatus_t xJobStatus;
/* Don't expect any notifications to be pending yet. */
configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );
/* Create and schedule a job using the handle of this task as the job's
context and the function that sends a notification to the task handle as
the jobs callback function. This is not a recyclable job so the storage
required to hold information about the job is provided by this task - in
this case the storage is on the stack of this task so no memory is allocated
dynamically but the stack frame must remain in scope for the lifetime of
the job. */
xResult = IotTaskPool_CreateJob( prvSimpleTaskNotifyCallback, /* Callback function. */
( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */
&xJobStorage,
&xJob );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* The job has been created but not scheduled so is now ready. */
IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );
/* This is not a persistent (recyclable) job and its storage is on the
stack of this function, so the amount of heap space available should not
have chanced since entering this function. */
configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );
/* In the full task pool implementation the first parameter is used to
pass the handle of the task pool to schedule. The lean task pool
implementation used in this demo only supports a single task pool, which
is created internally within the library, so the first parameter is NULL. */
xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* Look for the notification coming from the job's callback function. The
priority of the task pool worker task that executes the callback is higher
than the priority of this task so a block time is not needed - the task pool
worker task pre-empts this task and sends the notification (from the job's
callback) as soon as the job is scheduled. */
ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );
configASSERT( ulReturn );
/* The job's callback has executed so the job has now completed. */
IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );
}
/*-----------------------------------------------------------*/
static void prvExample_DeferredJobAndCancellingJobs( void )
{
IotTaskPoolJobStorage_t xJobStorage;
IotTaskPoolJob_t xJob;
IotTaskPoolError_t xResult;
uint32_t ulReturn;
const uint32_t ulShortDelay_ms = 100UL;
const TickType_t xNoDelay = ( TickType_t ) 0, xAllowableMargin = ( TickType_t ) 5; /* Large margin for Windows port, which is not real time. */
TickType_t xTimeBefore, xElapsedTime, xShortDelay_ticks;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
IotTaskPoolJobStatus_t xJobStatus;
/* Don't expect any notifications to be pending yet. */
configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );
/* Create a job using the handle of this task as the job's context and the
function that sends a notification to the task handle as the jobs callback
function. The job is created using storage allocated on the stack of this
function - so no memory is allocated. */
xResult = IotTaskPool_CreateJob( prvSimpleTaskNotifyCallback, /* Callback function. */
( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */
&xJobStorage,
&xJob );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* The job has been created but not scheduled so is now ready. */
IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );
/* This is not a persistent (recyclable) job and its storage is on the
stack of this function, so the amount of heap space available should not
have chanced since entering this function. */
configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );
/* Schedule the job to run its callback in xShortDelay_ms milliseconds time.
In the full task pool implementation the first parameter is used to pass the
handle of the task pool to schedule. The lean task pool implementation used
in this demo only supports a single task pool, which is created internally
within the library, so the first parameter is NULL. */
xResult = IotTaskPool_ScheduleDeferred( NULL, xJob, ulShortDelay_ms );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* The scheduled job should not have executed yet, so don't expect any
notifications and expect the job's status to be 'deferred'. */
ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );
configASSERT( ulReturn == 0 );
IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_DEFERRED );
/* As the job has not yet been executed it can be stopped. */
xResult = IotTaskPool_TryCancel( NULL, xJob, &xJobStatus );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_CANCELED );
/* Schedule the job again, and this time wait until its callback is
executed (the callback function sends a notification to this task) to see
that it executes at the right time. */
xTimeBefore = xTaskGetTickCount();
xResult = IotTaskPool_ScheduleDeferred( NULL, xJob, ulShortDelay_ms );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* Wait twice the deferred execution time to ensure the callback is executed
before the call below times out. */
ulReturn = ulTaskNotifyTake( pdTRUE, pdMS_TO_TICKS( ulShortDelay_ms * 2UL ) );
xElapsedTime = xTaskGetTickCount() - xTimeBefore;
/* A single notification should not have been received... */
configASSERT( ulReturn == 1 );
/* ...and the time since scheduling the job should be greater than or
equal to the deferred execution time - which is converted to ticks for
comparison. */
xShortDelay_ticks = pdMS_TO_TICKS( ulShortDelay_ms );
configASSERT( ( xElapsedTime >= xShortDelay_ticks ) && ( xElapsedTime < ( xShortDelay_ticks + xAllowableMargin ) ) );
}
/*-----------------------------------------------------------*/
static void prvExample_BasicRecyclableJob( void )
{
IotTaskPoolJob_t xJob;
IotTaskPoolError_t xResult;
uint32_t ulReturn;
const uint32_t ulNoFlags = 0UL;
const TickType_t xNoDelay = ( TickType_t ) 0;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
/* Don't expect any notifications to be pending yet. */
configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );
/* Create and schedule a job using the handle of this task as the job's
context and the function that sends a notification to the task handle as
the jobs callback function. The job is created as a recyclable job and in
this case the memory used to hold the job status is allocated inside the
create function. As the job is persistent it can be used multiple times,
as demonstrated in other examples within this demo. In the full task pool
implementation the first parameter is used to pass the handle of the task
pool this recyclable job is to be associated with. In the lean
implementation of the task pool used by this demo there is only one task
pool (the system task pool created within the task pool library) so the
first parameter is NULL. */
xResult = IotTaskPool_CreateRecyclableJob( NULL,
prvSimpleTaskNotifyCallback,
(void * ) xTaskGetCurrentTaskHandle(),
&xJob );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* This recyclable job is persistent, and in this case created dynamically,
so expect there to be less heap space then when entering the function. */
configASSERT( xPortGetFreeHeapSize() < xFreeHeapBeforeCreatingJob );
/* In the full task pool implementation the first parameter is used to
pass the handle of the task pool to schedule. The lean task pool
implementation used in this demo only supports a single task pool, which
is created internally within the library, so the first parameter is NULL. */
xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* Look for the notification coming from the job's callback function. The
priority of the task pool worker task that executes the callback is higher
than the priority of this task so a block time is not needed - the task pool
worker task pre-empts this task and sends the notification (from the job's
callback) as soon as the job is scheduled. */
ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );
configASSERT( ulReturn );
/* Clean up recyclable job. In the full implementation of the task pool
the first parameter is used to pass a handle to the task pool the job is
associated with. In the lean implementation of the task pool used by this
demo there is only one task pool (the system task pool created in the
task pool library itself) so the first parameter is NULL. */
IotTaskPool_DestroyRecyclableJob( NULL, xJob );
/* Once the job has been deleted the memory used to hold the job is
returned, so the available heap should be exactly as when entering this
function. */
configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );
}
/*-----------------------------------------------------------*/
static void prvExample_ReuseRecyclableJobFromLowPriorityTask( void )
{
IotTaskPoolError_t xResult;
uint32_t x, xIndex, ulNotificationValue;
const uint32_t ulNoFlags = 0UL;
IotTaskPoolJob_t xJobs[ tpJOBS_TO_CREATE ];
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
IotTaskPoolJobStatus_t xJobStatus;
/* Don't expect any notifications to be pending yet. */
configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );
/* Create tpJOBS_TO_CREATE jobs using the handle of this task as the job's
context and the function that sends a notification to the task handle as
the jobs callback function. The jobs are created as a recyclable job and
in this case the memory to store the job information is allocated within
the create function as at this time there are no recyclable jobs in the
task pool jobs cache. As the jobs are persistent they can be used multiple
times. In the full task pool implementation the first parameter is used to
pass the handle of the task pool this recyclable job is to be associated
with. In the lean implementation of the task pool used by this demo there
is only one task pool (the system task pool created within the task pool
library) so the first parameter is NULL. */
for( x = 0; x < tpJOBS_TO_CREATE; x++ )
{
xResult = IotTaskPool_CreateRecyclableJob( NULL,
prvSimpleTaskNotifyCallback,
(void * ) xTaskGetCurrentTaskHandle(),
&( xJobs[ x ] ) );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* The job has been created but not scheduled so is now ready. */
IotTaskPool_GetStatus( NULL, xJobs[ x ], &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );
}
/* Demonstrate that the jobs can be recycled by performing twice the number
of iterations of scheduling jobs than there actually are created jobs. This
works because the task pool task priorities are above the priority of this
task, so the tasks that run the jobs pre-empt this task as soon as a job is
ready. */
for( x = 0; x < ( tpJOBS_TO_CREATE * 2UL ); x++ )
{
/* Make sure array index does not go out of bounds. */
xIndex = x % tpJOBS_TO_CREATE;
xResult = IotTaskPool_Schedule( NULL, xJobs[ xIndex ], ulNoFlags );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* The priority of the task pool task(s) is higher than the priority
of this task, so the job's callback function should have already
executed, sending a notification to this task, and incrementing this
task's notification value. */
xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
0UL, /* Don't clear any bits on exit. */
&ulNotificationValue, /* Obtain the notification value. */
0UL ); /* No block time, return immediately. */
configASSERT( ulNotificationValue == ( x + 1 ) );
/* The job's callback has executed so the job is now completed. */
IotTaskPool_GetStatus( NULL, xJobs[ xIndex ], &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );
/* To leave the list of jobs empty we can stop re-creating jobs half
way through iterations of this loop. */
if( x < tpJOBS_TO_CREATE )
{
/* Recycle the job so it can be used again. In the full task pool
implementation the first parameter is used to pass the handle of the
task pool this job will be associated with. In this lean task pool
implementation only the system task pool exists (the task pool created
internally to the task pool library) so the first parameter is just
passed as NULL. *//*_RB_ Why not recycle it automatically? */
IotTaskPool_RecycleJob( NULL, xJobs[ xIndex ] );
xResult = IotTaskPool_CreateRecyclableJob( NULL,
prvSimpleTaskNotifyCallback,
(void * ) xTaskGetCurrentTaskHandle(),
&( xJobs[ xIndex ] ) );
}
}
/* Clear all the notification value bits again. */
xTaskNotifyWait( portMAX_DELAY, /* Clear all bits on entry - portMAX_DELAY is used as it is a portable way of having all bits set. */
0UL, /* Don't clear any bits on exit. */
NULL, /* Don't need the notification value this time. */
0UL ); /* No block time, return immediately. */
configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );
/* Clean up all the recyclable job. In the full implementation of the task
pool the first parameter is used to pass a handle to the task pool the job
is associated with. In the lean implementation of the task pool used by
this demo there is only one task pool (the system task pool created in the
task pool library itself) so the first parameter is NULL. */
for( x = 0; x < tpJOBS_TO_CREATE; x++ )
{
xResult = IotTaskPool_DestroyRecyclableJob( NULL, xJobs[ x ] );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
}
/* Once the job has been deleted the memory used to hold the job is
returned, so the available heap should be exactly as when entering this
function. */
configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );
}
/*-----------------------------------------------------------*/
static void prvExample_ReuseRecyclableJobFromHighPriorityTask( void )
{
IotTaskPoolError_t xResult;
uint32_t x, ulNotificationValue;
const uint32_t ulNoFlags = 0UL;
IotTaskPoolJob_t xJobs[ tpJOBS_TO_CREATE ];
IotTaskPoolJobStorage_t xJobStorage[ tpJOBS_TO_CREATE ];
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
TickType_t xShortDelay = pdMS_TO_TICKS( 150 );
IotTaskPoolJobStatus_t xJobStatus;
/* Don't expect any notifications to be pending yet. */
configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );
/* prvExample_ReuseRecyclableJobFromLowPriorityTask() executes in a task
that has a lower [task] priority than the task pool's worker tasks.
Therefore a talk pool worker preempts the task that calls
prvExample_ReuseRecyclableJobFromHighPriorityTask() as soon as the job is
scheduled. prvExample_ReuseRecyclableJobFromHighPriorityTask() reverses the
priorities - prvExample_ReuseRecyclableJobFromHighPriorityTask() raises its
priority to above the task pool's worker tasks, so the worker tasks do not
execute until the calling task enters the blocked state. First raise the
priority - passing NULL means raise the priority of the calling task. */
vTaskPrioritySet( NULL, tpTASK_POOL_WORKER_PRIORITY + 1 );
/* Create tpJOBS_TO_CREATE jobs using the handle of this task as the job's
context and the function that sends a notification to the task handle as
the jobs callback function. */
for( x = 0; x < tpJOBS_TO_CREATE; x++ )
{
xResult = IotTaskPool_CreateJob( prvSimpleTaskNotifyCallback, /* Callback function. */
( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */
&( xJobStorage[ x ] ),
&( xJobs[ x ] ) );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* This is not a persistent (recyclable) job and its storage is on the
stack of this function, so the amount of heap space available should not
have chanced since entering this function. */
configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );
}
for( x = 0; x < tpJOBS_TO_CREATE; x++ )
{
/* Schedule the next job. */
xResult = IotTaskPool_Schedule( NULL, xJobs[ x ], ulNoFlags );
configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
/* Although scheduled, the job's callback has not executed, so the job
reports itself as scheduled. */
IotTaskPool_GetStatus( NULL, xJobs[ x ], &xJobStatus );
configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_SCHEDULED );
/* The priority of the task pool task(s) is lower than the priority
of this task, so the job's callback function should not have executed
yes, so don't expect the notification value for this task to have
changed. */
xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
0UL, /* Don't clear any bits on exit. */
&ulNotificationValue, /* Obtain the notification value. */
0UL ); /* No block time, return immediately. */
configASSERT( ulNotificationValue == 0 );
}
/* At this point there are tpJOBS_TO_CREATE scheduled, but none have executed
their callbacks because the priority of this task is higher than the
priority of the task pool worker threads. When this task blocks to wait for
a notification a worker thread will be able to executes - but as soon as its
callback function sends a notification to this task this task will
preempt it (because it has a higher priority) so this task only expects to
receive one notification at a time. */
for( x = 0; x < tpJOBS_TO_CREATE; x++ )
{
xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
0UL, /* Don't clear any bits on exit. */
&ulNotificationValue, /* Obtain the notification value. */
xShortDelay ); /* Short delay to allow a task pool worker to execute. */
configASSERT( ulNotificationValue == ( x + 1 ) );
}
/* All the scheduled jobs have now executed, so waiting for another
notification should timeout without the notification value changing. */
xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
0UL, /* Don't clear any bits on exit. */
&ulNotificationValue, /* Obtain the notification value. */
xShortDelay ); /* Short delay to allow a task pool worker to execute. */
configASSERT( ulNotificationValue == x );
/* Reset the priority of this task and clear the notifications ready for the
next example. */
vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
xTaskNotifyWait( portMAX_DELAY, /* Clear all bits on entry - portMAX_DELAY is used as it is a portable way of having all bits set. */
0UL, /* Don't clear any bits on exit. */
NULL, /* Don't need the notification value this time. */
0UL ); /* No block time, return immediately. */
}
/*-----------------------------------------------------------*/

View file

@ -0,0 +1,355 @@
/*
* FreeRTOS Kernel V10.2.1
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
/*
* Creates two transmitting tasks and two receiving tasks. The transmitting
* tasks send values that are received by the receiving tasks. One set of tasks
* uses the standard API. The other set of tasks uses the zero copy API.
*
* See the following web page for essential demo usage and configuration
* details:
* http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#define simpTINY_DELAY ( ( TickType_t ) 2 )
/*
* Uses a socket to send data without using the zero copy option.
* prvSimpleServerTask() will receive the data.
*/
static void prvSimpleClientTask( void *pvParameters );
/*
* Uses a socket to receive the data sent by the prvSimpleClientTask() task.
* Does not use the zero copy option.
*/
static void prvSimpleServerTask( void *pvParameters );
/*
* Uses a socket to send data using the zero copy option.
* prvSimpleZeroCopyServerTask() will receive the data.
*/
static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );
/*
* Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()
* task. Uses the zero copy option.
*/
static void prvSimpleZeroCopyServerTask( void *pvParameters );
/*-----------------------------------------------------------*/
void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
{
/* Create the client and server tasks that do not use the zero copy
interface. */
xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
/* Create the client and server tasks that do use the zero copy interface. */
xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
}
/*-----------------------------------------------------------*/
static void prvSimpleClientTask( void *pvParameters )
{
Socket_t xClientSocket;
struct freertos_sockaddr xDestinationAddress;
uint8_t cString[ 65 ];
BaseType_t lReturned;
uint32_t ulCount = 0UL, ulIPAddress;
const uint32_t ulLoopsPerSocket = 10UL;
const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
/* Remove compiler warning about unused parameters. */
( void ) pvParameters;
/* It is assumed that this task is not created until the network is up,
so the IP address can be obtained immediately. store the IP address being
used in ulIPAddress. This is done so the socket can send to a different
port on the same IP address. */
FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
/* This test sends to itself, so data sent from here is received by a server
socket on the same IP address. Setup the freertos_sockaddr structure with
this nodes IP address, and the port number being sent to. The strange
casting is to try and remove compiler warnings on 32 bit machines. */
xDestinationAddress.sin_addr = ulIPAddress;
xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
for( ;; )
{
/* Create the socket. */
xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
/* The count is used to differentiate between different messages sent to
the server, and to break out of the do while loop below. */
ulCount = 0UL;
do
{
/* Create the string that is sent to the server. */
sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
/* Send the string to the socket. ulFlags is set to 0, so the zero
copy option is not selected. That means the data from cString[] is
copied into a network buffer inside FreeRTOS_sendto(), and cString[]
can be reused as soon as FreeRTOS_sendto() has returned. */
lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
ulCount++;
} while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
FreeRTOS_closesocket( xClientSocket );
/* A short delay to prevent the messages printed by the server task
scrolling off the screen too quickly, and to prevent reduce the network
loading. */
vTaskDelay( x150ms );
}
}
/*-----------------------------------------------------------*/
static void prvSimpleServerTask( void *pvParameters )
{
int32_t lBytes;
uint8_t cReceivedString[ 60 ];
struct freertos_sockaddr xClient, xBindAddress;
uint32_t xClientLength = sizeof( xClient );
Socket_t xListeningSocket;
/* Just to prevent compiler warnings. */
( void ) pvParameters;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
/* This test receives data sent from a different port on the same IP
address. Configure the freertos_sockaddr structure with the address being
bound to. The strange casting is to try and remove compiler warnings on 32
bit machines. Note that this task is only created after the network is up,
so the IP address is valid here. */
xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
/* Bind the socket to the port that the client task will send to. */
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
for( ;; )
{
/* Zero out the receive array so there is NULL at the end of the string
when it is printed out. */
memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
/* Receive data on the socket. ulFlags is zero, so the zero copy option
is not set and the received data is copied into the buffer pointed to by
cReceivedString. By default the block time is portMAX_DELAY.
xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
appropriately in case future versions do use it. */
lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
/* Error check. */
configASSERT( lBytes == ( BaseType_t ) strlen( ( const char * ) cReceivedString ) );
}
}
/*-----------------------------------------------------------*/
static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )
{
Socket_t xClientSocket;
uint8_t *pucUDPPayloadBuffer;
struct freertos_sockaddr xDestinationAddress;
BaseType_t lReturned;
uint32_t ulCount = 0UL, ulIPAddress;
const uint32_t ulLoopsPerSocket = 10UL;
const char *pcStringToSend = "Server received (using zero copy): Message number ";
const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
/* 15 is added to ensure the number, \r\n and terminating zero fit. */
const size_t xStringLength = strlen( pcStringToSend ) + 15;
/* Remove compiler warning about unused parameters. */
( void ) pvParameters;
/* It is assumed that this task is not created until the network is up,
so the IP address can be obtained immediately. store the IP address being
used in ulIPAddress. This is done so the socket can send to a different
port on the same IP address. */
FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
/* This test sends to itself, so data sent from here is received by a server
socket on the same IP address. Setup the freertos_sockaddr structure with
this nodes IP address, and the port number being sent to. The strange
casting is to try and remove compiler warnings on 32 bit machines. */
xDestinationAddress.sin_addr = ulIPAddress;
xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
for( ;; )
{
/* Create the socket. */
xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
/* The count is used to differentiate between different messages sent to
the server, and to break out of the do while loop below. */
ulCount = 0UL;
do
{
/* This task is going to send using the zero copy interface. The
data being sent is therefore written directly into a buffer that is
passed into, rather than copied into, the FreeRTOS_sendto()
function.
First obtain a buffer of adequate length from the IP stack into which
the string will be written. Although a max delay is used, the actual
delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence
the do while loop is used to ensure a buffer is obtained. */
do
{
} while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );
/* A buffer was successfully obtained. Create the string that is
sent to the server. First the string is filled with zeros as this will
effectively be the null terminator when the string is received at the other
end. Note that the string is being written directly into the buffer
obtained from the IP stack above. */
memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );
sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );
/* Pass the buffer into the send function. ulFlags has the
FREERTOS_ZERO_COPY bit set so the IP stack will take control of the
buffer rather than copy data out of the buffer. */
lReturned = FreeRTOS_sendto( xClientSocket, /* The socket being sent to. */
( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */
strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */
FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
&xDestinationAddress, /* Where the data is being sent. */
sizeof( xDestinationAddress ) );
if( lReturned == 0 )
{
/* The send operation failed, so this task is still responsible
for the buffer obtained from the IP stack. To ensure the buffer
is not lost it must either be used again, or, as in this case,
returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().
pucUDPPayloadBuffer can be safely re-used after this call. */
FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
}
else
{
/* The send was successful so the IP stack is now managing the
buffer pointed to by pucUDPPayloadBuffer, and the IP stack will
return the buffer once it has been sent. pucUDPPayloadBuffer can
be safely re-used. */
}
ulCount++;
} while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
FreeRTOS_closesocket( xClientSocket );
/* A short delay to prevent the messages scrolling off the screen too
quickly. */
vTaskDelay( x150ms );
}
}
/*-----------------------------------------------------------*/
static void prvSimpleZeroCopyServerTask( void *pvParameters )
{
int32_t lBytes;
uint8_t *pucUDPPayloadBuffer;
struct freertos_sockaddr xClient, xBindAddress;
uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
Socket_t xListeningSocket;
/* Just to prevent compiler warnings. */
( void ) pvParameters;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
/* This test receives data sent from a different port on the same IP address.
Obtain the nodes IP address. Configure the freertos_sockaddr structure with
the address being bound to. The strange casting is to try and remove
compiler warnings on 32 bit machines. Note that this task is only created
after the network is up, so the IP address is valid here. */
FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
xBindAddress.sin_addr = ulIPAddress;
xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
/* Bind the socket to the port that the client task will send to. */
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
for( ;; )
{
/* Receive data on the socket. ulFlags has the zero copy bit set
(FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
received data should be passed out to this task using the second
parameter to the FreeRTOS_recvfrom() call. When this is done the
IP stack is no longer responsible for releasing the buffer, and
the task *must* return the buffer to the stack when it is no longer
needed. By default the block time is portMAX_DELAY. */
lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
/* Print the received characters. */
if( lBytes > 0 )
{
/* It is expected to receive one more byte than the string length as
the NULL terminator is also transmitted. */
configASSERT( lBytes == ( ( BaseType_t ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );
}
if( lBytes >= 0 )
{
/* The buffer *must* be freed once it is no longer needed. */
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
}
}
}