Skip to content

Commit

Permalink
Merge pull request #32 from pvyawaha/ota
Browse files Browse the repository at this point in the history
Ota
  • Loading branch information
pvyawaha authored Jul 20, 2020
2 parents 938985a + 597e9d7 commit 410c301
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
42 changes: 21 additions & 21 deletions libraries/aws/ota/src/aws_iot_ota_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@
* http://www.FreeRTOS.org
*/

/* The config header is always included first. */
#include "iot_config.h"

/* Standard library includes. */
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "timers.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/* OTA agent includes. */
#include "aws_iot_ota_agent.h"
#include "aws_ota_agent_config.h"
Expand Down Expand Up @@ -293,7 +289,7 @@ static OTA_AgentContext_t xOTA_Agent =
.xPALCallbacks = OTA_JOB_CALLBACK_DEFAULT_INITIALIZER,
.ulNumOfBlocksToReceive = 1,
.xStatistics = { 0 },
.xOTA_ThreadSafetyMutex = NULL,
.otaBufferSem = { 0 },
.ulRequestMomentum = 0
};

Expand Down Expand Up @@ -1258,8 +1254,8 @@ static OTA_Err_t prvShutdownHandler( OTA_EventData_t * pxEventData )

xOTA_Agent.eState = eOTA_AgentState_Stopped;

/* Delete the OTA agent task. */
vTaskDelete( NULL );
/* Terminate the OTA Agent Thread. */
pthread_exit( NULL );

return kOTA_Err_None;
}
Expand Down Expand Up @@ -1360,10 +1356,10 @@ void prvOTAEventBufferFree( OTA_EventData_t * const pxBuffer )
{
DEFINE_OTA_METHOD_NAME( "prvOTAEventBufferFree" );

if( xSemaphoreTake( xOTA_Agent.xOTA_ThreadSafetyMutex, portMAX_DELAY ) == pdPASS )
if( sem_wait( &xOTA_Agent.otaBufferSem ) == pdPASS )
{
pxBuffer->bBufferUsed = false;
( void ) xSemaphoreGive( xOTA_Agent.xOTA_ThreadSafetyMutex );
( void ) sem_post(&xOTA_Agent.otaBufferSem);
}
else
{
Expand All @@ -1379,7 +1375,7 @@ OTA_EventData_t * prvOTAEventBufferGet( void )
OTA_EventData_t * pxOTAFreeMsg = NULL;

/* Wait at most 1 task switch for a buffer so as not to block the callback. */
if( xSemaphoreTake( xOTA_Agent.xOTA_ThreadSafetyMutex, 1 ) == pdPASS )
if(sem_wait( &xOTA_Agent.otaBufferSem, 1 ) == pdPASS )
{
for( ulIndex = 0; ulIndex < otaconfigMAX_NUM_OTA_DATA_BUFFERS; ulIndex++ )
{
Expand All @@ -1391,7 +1387,7 @@ OTA_EventData_t * prvOTAEventBufferGet( void )
}
}

( void ) xSemaphoreGive( xOTA_Agent.xOTA_ThreadSafetyMutex );
( void )sem_post( &xOTA_Agent.otaBufferSem );
}
else
{
Expand Down Expand Up @@ -2691,7 +2687,7 @@ static void prvAgentShutdownCleanup( void )
/* Delete the semaphore.*/
if( xOTA_Agent.xOTA_ThreadSafetyMutex != NULL )
{
vSemaphoreDelete( xOTA_Agent.xOTA_ThreadSafetyMutex );
sem_destroy(&xOTA_Agent.otaBufferSem);
}
}

Expand Down Expand Up @@ -2831,11 +2827,12 @@ static BaseType_t prvStartOTAAgentTask( void * pvConnectionContext,
{
BaseType_t xReturn = 0;
uint32_t ulIndex = 0;
int ret = 0;

/*
* The actual OTA Task and queue control structure. Only created once.
*/
static TaskHandle_t pxOTA_TaskHandle;
pthread_t xOTAThreadHandle;
static StaticQueue_t xStaticQueue;

portENTER_CRITICAL();
Expand All @@ -2859,8 +2856,8 @@ static BaseType_t prvStartOTAAgentTask( void * pvConnectionContext,
/*
* Create the queue used to pass event messages to the OTA task.
*/
xOTA_Agent.xOTA_ThreadSafetyMutex = xSemaphoreCreateMutex();
configASSERT( xOTA_Agent.xOTA_ThreadSafetyMutex != NULL );
ret = sem_init( &xOTA_Agent.otaBufferSem, 0, 1 );
configASSERT(ret != -1 );

/*
* Initialize all file paths to NULL.
Expand All @@ -2877,16 +2874,19 @@ static BaseType_t prvStartOTAAgentTask( void * pvConnectionContext,
{
xEventBuffer[ ulIndex ].bBufferUsed = false;
}

xReturn = xTaskCreate( prvOTAAgentTask, "OTA Agent Task", otaconfigSTACK_SIZE, NULL, otaconfigAGENT_PRIORITY, &pxOTA_TaskHandle );

/*
* Create the OTA Agent thread.
*/
ret = pthread_create( &xOTAThreadHandle, NULL, prvOTAAgentTask, NULL);

portEXIT_CRITICAL(); /* Protected elements are initialized. It's now safe to context switch. */

/*
* If task creation succeed, wait for the OTA agent to be ready before proceeding. Otherwise,
* let it fall through to exit.
*/
if( xReturn == pdPASS )
if( ret == 0 )
{
while( ( xTicksToWait-- > 0U ) && ( xOTA_Agent.eState != eOTA_AgentState_Ready ) )
{
Expand Down
11 changes: 10 additions & 1 deletion libraries/aws/ota/src/aws_iot_ota_agent_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
#ifndef _AWS_IOT_OTA_AGENT_INTERNAL_H_
#define _AWS_IOT_OTA_AGENT_INTERNAL_H_

/* FreeRTOS+POSIX includes. */
#include "FreeRTOS_POSIX.h"
#include "FreeRTOS_POSIX/errno.h"
#include "FreeRTOS_POSIX/pthread.h"
#include "FreeRTOS_POSIX/signal.h"
#include "FreeRTOS_POSIX/time.h"
#include "FreeRTOS_POSIX/utils.h"
#include "FreeRTOS_POSIX/semaphore.h"

#include "aws_ota_agent_config.h"
#include "jsmn.h"

Expand Down Expand Up @@ -258,7 +267,7 @@ typedef struct ota_agent_context
OTA_PAL_Callbacks_t xPALCallbacks; /* Variable to store PAL callbacks */
uint32_t ulNumOfBlocksToReceive; /* Number of data blocks to receive per data request. */
OTA_AgentStatistics_t xStatistics; /* The OTA agent statistics block. */
SemaphoreHandle_t xOTA_ThreadSafetyMutex; /* Mutex used to ensure thread safety while managing data buffers. */
sem_t otaBufferSem; /* Mutex used to ensure thread safety while managing data buffers. */
uint32_t ulRequestMomentum; /* The number of requests sent before a response was received. */
} OTA_AgentContext_t;

Expand Down

0 comments on commit 410c301

Please sign in to comment.