FreeRTOS: OTA
Over the Air Update library
Return to main page ↑
aws_iot_ota_agent.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS OTA V1.2.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://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
31 #ifndef _AWS_IOT_OTA_AGENT_H_
32 #define _AWS_IOT_OTA_AGENT_H_
33 
34 /* Standard includes. */
35 /* For FILE type in OTA_FileContext_t.*/
36 #include <stdio.h>
37 #include <stdbool.h>
38 
39 /* Includes required by the FreeRTOS timers structure. */
40 #include "FreeRTOS.h"
41 #include "timers.h"
42 
43 /* Evaluates to the length of a constant string defined like 'static const char str[]= "xyz"; */
44 #define CONST_STRLEN( s ) ( ( ( uint32_t ) sizeof( s ) ) - 1UL )
45 
46 /* The OTA signature algorithm string is specified by the PAL. */
47 #define OTA_FILE_SIG_KEY_STR_MAX_LENGTH 32
48 extern const char cOTA_JSON_FileSignatureKey[ OTA_FILE_SIG_KEY_STR_MAX_LENGTH ];
49 
53 #define OTA_DEBUG_LOG_LEVEL 1
54 #if OTA_DEBUG_LOG_LEVEL >= 1
55  #define DEFINE_OTA_METHOD_NAME( name ) \
56  static const char OTA_METHOD_NAME[] = name; \
57  ( void ) OTA_METHOD_NAME;
58  #define OTA_LOG_L1 vLoggingPrintf
59 #else
60  #define DEFINE_OTA_METHOD_NAME( name )
61  #define OTA_LOG_L1( ... )
62 #endif
63 #if OTA_DEBUG_LOG_LEVEL >= 2
64  #define DEFINE_OTA_METHOD_NAME_L2( name ) \
65  static const char OTA_METHOD_NAME[] = name; \
66  ( void ) OTA_METHOD_NAME;
67  #define OTA_LOG_L2 vLoggingPrintf
68 #else
69  #define DEFINE_OTA_METHOD_NAME_L2( name )
70  #define OTA_LOG_L2( ... )
71 #endif
72 #if OTA_DEBUG_LOG_LEVEL >= 3
73  #define DEFINE_OTA_METHOD_NAME_L3( name ) \
74  static const char OTA_METHOD_NAME[] = name; \
75  ( void ) OTA_METHOD_NAME;
76  #define OTA_LOG_L3 vLoggingPrintf
77 #else
78  #define DEFINE_OTA_METHOD_NAME_L3( name )
79  #define OTA_LOG_L3( ... )
80 #endif
81 
82 /*-------------------------- OTA enumerated types --------------------------*/
83 
96 typedef enum
97 {
98  eOTA_AgentState_NoTransition = -1,
99  eOTA_AgentState_Init = 0,
100  eOTA_AgentState_Ready,
101  eOTA_AgentState_RequestingJob,
102  eOTA_AgentState_WaitingForJob,
103  eOTA_AgentState_CreatingFile,
104  eOTA_AgentState_RequestingFileBlock,
105  eOTA_AgentState_WaitingForFileBlock,
106  eOTA_AgentState_ClosingFile,
107  eOTA_AgentState_Suspended,
108  eOTA_AgentState_ShuttingDown,
109  eOTA_AgentState_Stopped,
110  eOTA_AgentState_All
111 } OTA_State_t;
112 
119 typedef enum
120 {
121  eOTA_AgentEvent_Start = 0,
122  eOTA_AgentEvent_StartSelfTest,
123  eOTA_AgentEvent_RequestJobDocument,
124  eOTA_AgentEvent_ReceivedJobDocument,
125  eOTA_AgentEvent_CreateFile,
126  eOTA_AgentEvent_RequestFileBlock,
127  eOTA_AgentEvent_ReceivedFileBlock,
128  eOTA_AgentEvent_RequestTimer,
129  eOTA_AgentEvent_CloseFile,
130  eOTA_AgentEvent_Suspend,
131  eOTA_AgentEvent_Resume,
132  eOTA_AgentEvent_UserAbort,
133  eOTA_AgentEvent_Shutdown,
134  eOTA_AgentEvent_Max
135 } OTA_Event_t;
136 
143 typedef enum
144 {
145  eOTA_PAL_ImageState_Unknown = 0,
146  eOTA_PAL_ImageState_PendingCommit,
147  eOTA_PAL_ImageState_Valid,
148  eOTA_PAL_ImageState_Invalid,
150 
155 typedef enum
156 {
157  eOTA_JobParseErr_Unknown = -1, /* The error code has not yet been set by a logic path. */
158  eOTA_JobParseErr_None = 0, /* Signifies no error has occurred. */
159  eOTA_JobParseErr_BusyWithExistingJob, /* We're busy with a job but received a new job document. */
160  eOTA_JobParseErr_NullJob, /* A null job was reported (no job ID). */
161  eOTA_JobParseErr_UpdateCurrentJob, /* We're already busy with the reported job ID. */
162  eOTA_JobParseErr_ZeroFileSize, /* Job document specified a zero sized file. This is not allowed. */
163  eOTA_JobParseErr_NonConformingJobDoc, /* The job document failed to fulfill the model requirements. */
164  eOTA_JobParseErr_BadModelInitParams, /* There was an invalid initialization parameter used in the document model. */
165  eOTA_JobParseErr_NoContextAvailable, /* There wasn't an OTA context available. */
166  eOTA_JobParseErr_NoActiveJobs, /* No active jobs are available in the service. */
168 
169 
187 typedef enum
188 {
192  eOTA_LastJobEvent = eOTA_JobEvent_StartTest
194 
195 
211 typedef enum
212 {
219  eOTA_LastImageState = eOTA_ImageState_Aborted
221 
222 
223 /*------------------------- OTA callbacks --------------------------*/
224 
229 /* Forward delcaration of OTA_FileContext_t. */
230 typedef struct OTA_FileContext OTA_FileContext_t;
231 
235 typedef uint32_t OTA_Err_t;
236 
265 typedef void (* pxOTACompleteCallback_t)( OTA_JobEvent_t eEvent );
266 
277 
288 typedef OTA_Err_t (* pxOTAPALActivateNewImageCallback_t)( uint32_t ulServerFileID );
289 
300 
311 
321 typedef OTA_PAL_ImageState_t (* pxOTAPALGetPlatformImageStateCallback_t)( uint32_t ulServerFileID );
322 
332 typedef OTA_Err_t (* pxOTAPALResetDeviceCallback_t)( uint32_t ulServerFileID );
333 
344 typedef OTA_Err_t (* pxOTAPALSetPlatformImageStateCallback_t)( uint32_t ulServerFileID,
345  OTA_ImageState_t eState );
346 
359 typedef int16_t (* pxOTAPALWriteBlockCallback_t)( OTA_FileContext_t * const C,
360  uint32_t iOffset,
361  uint8_t * const pacData,
362  uint32_t iBlockSize );
363 
374 typedef OTA_JobParseErr_t (* pxOTACustomJobCallback_t)( const char * pcJSON,
375  uint32_t ulMsgLen );
376 
377 
378 /*--------------------------- OTA structs ----------------------------*/
379 
384 /* A composite cryptographic signature structure able to hold our largest supported signature. */
385 
386 #define kOTA_MaxSignatureSize 256 /* Max bytes supported for a file signature (2048 bit RSA is 256 bytes). */
387 
388 typedef struct
389 {
390  uint16_t usSize; /* Size, in bytes, of the signature. */
391  uint8_t ucData[ kOTA_MaxSignatureSize ]; /* The binary signature data. */
392 } Sig256_t;
393 
401 typedef struct OTA_FileContext
402 {
403  uint8_t * pucFilePath;
404  union
405  {
406  int32_t lFileHandle;
408  #if WIN32
409  FILE * pxFile;
410  #endif
411  uint8_t * pucFile;
412  };
413  uint32_t ulFileSize;
414  uint32_t ulBlocksRemaining;
415  uint32_t ulFileAttributes;
416  uint32_t ulServerFileID;
417  uint8_t * pucJobName;
418  uint8_t * pucStreamName;
420  uint8_t * pucRxBlockBitmap;
421  uint8_t * pucCertFilepath;
422  uint8_t * pucUpdateUrlPath;
423  uint8_t * pucAuthScheme;
424  uint32_t ulUpdaterVersion;
426  uint8_t * pucProtocols;
428 
435 typedef struct
436 {
437  void * pvControlClient;
438  const void * pxNetworkInterface;
439  void * pvNetworkCredentials;
441 
446 typedef struct
447 {
448  pxOTAPALAbortCallback_t xAbort; /* OTA Abort callback pointer */
449  pxOTAPALActivateNewImageCallback_t xActivateNewImage; /* OTA Activate New Image callback pointer */
450  pxOTAPALCloseFileCallback_t xCloseFile; /* OTA Close File callback pointer */
451  pxOTAPALCreateFileForRxCallback_t xCreateFileForRx; /* OTA Create File for Receive callback pointer */
452  pxOTAPALGetPlatformImageStateCallback_t xGetPlatformImageState; /* OTA Get Platform Image State callback pointer */
453  pxOTAPALResetDeviceCallback_t xResetDevice; /* OTA Reset Device callback pointer */
454  pxOTAPALSetPlatformImageStateCallback_t xSetPlatformImageState; /* OTA Set Platform Image State callback pointer */
455  pxOTAPALWriteBlockCallback_t xWriteBlock; /* OTA Write Block callback pointer */
456  pxOTACompleteCallback_t xCompleteCallback; /* OTA Job Completed callback pointer */
457  pxOTACustomJobCallback_t xCustomJobCallback; /* OTA Custom Job callback pointer */
459 
460 
461 /*------------------------- OTA defined constants --------------------------*/
462 
483 /* @[define_ota_err_codes] */
484 #define kOTA_Err_Panic 0xfe000000UL
485 #define kOTA_Err_Uninitialized 0xff000000UL
486 #define kOTA_Err_None 0x00000000UL
487 #define kOTA_Err_SignatureCheckFailed 0x01000000UL
488 #define kOTA_Err_BadSignerCert 0x02000000UL
489 #define kOTA_Err_OutOfMemory 0x03000000UL
490 #define kOTA_Err_ActivateFailed 0x04000000UL
491 #define kOTA_Err_CommitFailed 0x05000000UL
492 #define kOTA_Err_RejectFailed 0x06000000UL
493 #define kOTA_Err_AbortFailed 0x07000000UL
494 #define kOTA_Err_PublishFailed 0x08000000UL
495 #define kOTA_Err_BadImageState 0x09000000UL
496 #define kOTA_Err_NoActiveJob 0x0a000000UL
497 #define kOTA_Err_NoFreeContext 0x0b000000UL
498 #define kOTA_Err_HTTPInitFailed 0x0c000000UL
499 #define kOTA_Err_HTTPRequestFailed 0x0d000000UL
500 #define kOTA_Err_FileAbort 0x10000000UL
501 #define kOTA_Err_FileClose 0x11000000UL
502 #define kOTA_Err_RxFileCreateFailed 0x12000000UL
503 #define kOTA_Err_BootInfoCreateFailed 0x13000000UL
504 #define kOTA_Err_RxFileTooLarge 0x14000000UL
505 #define kOTA_Err_NullFilePtr 0x20000000UL
506 #define kOTA_Err_MomentumAbort 0x21000000UL
507 #define kOTA_Err_DowngradeNotAllowed 0x22000000UL
508 #define kOTA_Err_SameFirmwareVersion 0x23000000UL
509 #define kOTA_Err_JobParserError 0x24000000UL
510 #define kOTA_Err_FailedToEncodeCBOR 0x25000000UL
511 #define kOTA_Err_ImageStateMismatch 0x26000000UL
512 #define kOTA_Err_GenericIngestError 0x27000000UL
513 #define kOTA_Err_UserAbort 0x28000000UL
514 #define kOTA_Err_ResetNotSupported 0x29000000UL
515 #define kOTA_Err_TopicTooLarge 0x2a000000UL
516 #define kOTA_Err_SelfTestTimerFailed 0x2b000000UL
517 #define kOTA_Err_EventQueueSendFailed 0x2c000000UL
518 #define kOTA_Err_InvalidDataProtocol 0x2d000000UL
519 #define kOTA_Err_OTAAgentStopped 0x2e000000UL
520 /* @[define_ota_err_codes] */
521 
522 /* @[define_ota_err_code_helpers] */
523 #define kOTA_PAL_ErrMask 0xffffffUL
524 #define kOTA_Main_ErrMask 0xff000000UL
525 #define kOTA_MainErrShiftDownBits 24U
526 /* @[define_ota_err_code_helpers] */
527 
528 
529 /*------------------------- OTA Public API --------------------------*/
530 
583 OTA_State_t OTA_AgentInit( void * pvConnectionContext,
584  const uint8_t * pucThingName,
586  TickType_t xTicksToWait );
587 
607 OTA_State_t OTA_AgentInit_internal( void * pvConnectionContext,
608  const uint8_t * pucThingName,
609  const OTA_PAL_Callbacks_t * pxCallbacks,
610  TickType_t xTicksToWait );
611 
625 OTA_State_t OTA_AgentShutdown( TickType_t xTicksToWait );
626 
633 
646 
660 
670 
678 
686 
695 OTA_Err_t OTA_Resume( void * pxConnection );
696 
697 /*---------------------------------------------------------------------------*/
698 /* Statistics API */
699 /*---------------------------------------------------------------------------*/
700 
709 uint32_t OTA_GetPacketsReceived( void );
710 
720 uint32_t OTA_GetPacketsQueued( void );
721 
730 uint32_t OTA_GetPacketsProcessed( void );
731 
740 uint32_t OTA_GetPacketsDropped( void );
741 
742 #endif /* ifndef _AWS_IOT_OTA_AGENT_H_ */
OTA_FileContext_t::ulFileSize
uint32_t ulFileSize
Definition: aws_iot_ota_agent.h:413
OTA_FileContext_t::pxSignature
Sig256_t * pxSignature
Definition: aws_iot_ota_agent.h:419
OTA_GetAgentState
OTA_State_t OTA_GetAgentState(void)
Get the current state of the OTA agent.
OTA_AgentInit
OTA_State_t OTA_AgentInit(void *pvConnectionContext, const uint8_t *pucThingName, pxOTACompleteCallback_t xFunc, TickType_t xTicksToWait)
OTA Agent initialization function.
OTA_FileContext_t::bIsInSelfTest
bool bIsInSelfTest
Definition: aws_iot_ota_agent.h:425
OTA_GetPacketsProcessed
uint32_t OTA_GetPacketsProcessed(void)
Get the number of OTA message packets processed by the OTA agent.
OTA_FileContext_t::ulFileAttributes
uint32_t ulFileAttributes
Definition: aws_iot_ota_agent.h:415
OTA_SetImageState
OTA_Err_t OTA_SetImageState(OTA_ImageState_t eState)
Set the state of the current MCU image.
OTA_Err_t
uint32_t OTA_Err_t
OTA Error type.
Definition: aws_iot_ota_agent.h:235
eOTA_ImageState_Testing
@ eOTA_ImageState_Testing
Definition: aws_iot_ota_agent.h:214
kOTA_MaxSignatureSize
#define kOTA_MaxSignatureSize
Definition: aws_iot_ota_agent.h:386
eOTA_JobEvent_Activate
@ eOTA_JobEvent_Activate
Definition: aws_iot_ota_agent.h:189
OTA_CheckForUpdate
OTA_Err_t OTA_CheckForUpdate(void)
Request for the next available OTA job from the job service.
OTA_GetImageState
OTA_ImageState_t OTA_GetImageState(void)
Get the state of the currently running MCU image.
pxOTAPALActivateNewImageCallback_t
OTA_Err_t(* pxOTAPALActivateNewImageCallback_t)(uint32_t ulServerFileID)
OTA new image received callback function typedef.
Definition: aws_iot_ota_agent.h:288
pxOTAPALGetPlatformImageStateCallback_t
OTA_PAL_ImageState_t(* pxOTAPALGetPlatformImageStateCallback_t)(uint32_t ulServerFileID)
OTA Get Platform Image State callback function typedef.
Definition: aws_iot_ota_agent.h:321
OTA_FileContext_t::pucProtocols
uint8_t * pucProtocols
Definition: aws_iot_ota_agent.h:426
pxOTAPALAbortCallback_t
OTA_Err_t(* pxOTAPALAbortCallback_t)(OTA_FileContext_t *const C)
OTA abort callback function typedef.
Definition: aws_iot_ota_agent.h:276
OTA_PAL_ImageState_t
OTA_PAL_ImageState_t
OTA Platform Image State.
Definition: aws_iot_ota_agent.h:144
OTA_FileContext_t::lFileHandle
int32_t lFileHandle
Definition: aws_iot_ota_agent.h:406
pxOTACompleteCallback_t
void(* pxOTACompleteCallback_t)(OTA_JobEvent_t eEvent)
OTA update complete callback function typedef.
Definition: aws_iot_ota_agent.h:265
OTA_GetPacketsQueued
uint32_t OTA_GetPacketsQueued(void)
Get the number of OTA message packets queued by the OTA agent.
OTA_JobEvent_t
OTA_JobEvent_t
OTA Job callback events.
Definition: aws_iot_ota_agent.h:188
OTA_ActivateNewImage
OTA_Err_t OTA_ActivateNewImage(void)
Activate the newest MCU image received via OTA.
OTA_State_t
OTA_State_t
OTA Agent states.
Definition: aws_iot_ota_agent.h:97
OTA_FileContext_t::pucFile
uint8_t * pucFile
Definition: aws_iot_ota_agent.h:411
OTA_JobParseErr_t
OTA_JobParseErr_t
OTA job document parser error codes.
Definition: aws_iot_ota_agent.h:156
eOTA_ImageState_Accepted
@ eOTA_ImageState_Accepted
Definition: aws_iot_ota_agent.h:215
OTA_Event_t
OTA_Event_t
OTA Agent Events.
Definition: aws_iot_ota_agent.h:120
eOTA_ImageState_Aborted
@ eOTA_ImageState_Aborted
Definition: aws_iot_ota_agent.h:217
eOTA_JobEvent_Fail
@ eOTA_JobEvent_Fail
Definition: aws_iot_ota_agent.h:190
OTA_PAL_Callbacks_t
OTA PAL callback structure.
Definition: aws_iot_ota_agent.h:447
eOTA_JobEvent_StartTest
@ eOTA_JobEvent_StartTest
Definition: aws_iot_ota_agent.h:191
OTA_FileContext_t::ulServerFileID
uint32_t ulServerFileID
Definition: aws_iot_ota_agent.h:416
pxOTAPALWriteBlockCallback_t
int16_t(* pxOTAPALWriteBlockCallback_t)(OTA_FileContext_t *const C, uint32_t iOffset, uint8_t *const pacData, uint32_t iBlockSize)
OTA Write Block callback function typedef.
Definition: aws_iot_ota_agent.h:359
OTA_AgentInit_internal
OTA_State_t OTA_AgentInit_internal(void *pvConnectionContext, const uint8_t *pucThingName, const OTA_PAL_Callbacks_t *pxCallbacks, TickType_t xTicksToWait)
Internal OTA Agent initialization function.
OTA_FileContext_t::pucJobName
uint8_t * pucJobName
Definition: aws_iot_ota_agent.h:417
OTA_AgentShutdown
OTA_State_t OTA_AgentShutdown(TickType_t xTicksToWait)
Signal to the OTA Agent to shut down.
OTA_GetPacketsReceived
uint32_t OTA_GetPacketsReceived(void)
Get the number of OTA message packets received by the OTA agent.
OTA_GetPacketsDropped
uint32_t OTA_GetPacketsDropped(void)
Get the number of OTA message packets dropped by the OTA agent.
OTA_Resume
OTA_Err_t OTA_Resume(void *pxConnection)
Resume OTA agent operations .
eOTA_ImageState_Rejected
@ eOTA_ImageState_Rejected
Definition: aws_iot_ota_agent.h:216
pxOTACustomJobCallback_t
OTA_JobParseErr_t(* pxOTACustomJobCallback_t)(const char *pcJSON, uint32_t ulMsgLen)
Custom Job callback function typedef.
Definition: aws_iot_ota_agent.h:374
Sig256_t
Definition: aws_iot_ota_agent.h:389
eOTA_ImageState_Unknown
@ eOTA_ImageState_Unknown
Definition: aws_iot_ota_agent.h:213
OTA_FileContext_t
struct OTA_FileContext OTA_FileContext_t
Definition: aws_iot_ota_agent.h:230
pxOTAPALCloseFileCallback_t
OTA_Err_t(* pxOTAPALCloseFileCallback_t)(OTA_FileContext_t *const C)
OTA close file callback function typedef.
Definition: aws_iot_ota_agent.h:299
OTA_FileContext_t
OTA File Context Information.
Definition: aws_iot_ota_agent.h:402
OTA_FileContext_t::pucCertFilepath
uint8_t * pucCertFilepath
Definition: aws_iot_ota_agent.h:421
OTA_ConnectionContext_t
OTA Connection context.
Definition: aws_iot_ota_agent.h:436
OTA_FileContext_t::pucStreamName
uint8_t * pucStreamName
Definition: aws_iot_ota_agent.h:418
OTA_FileContext_t::pucRxBlockBitmap
uint8_t * pucRxBlockBitmap
Definition: aws_iot_ota_agent.h:420
OTA_FileContext_t::pucFilePath
uint8_t * pucFilePath
Definition: aws_iot_ota_agent.h:403
OTA_FileContext_t::pucAuthScheme
uint8_t * pucAuthScheme
Definition: aws_iot_ota_agent.h:423
OTA_Suspend
OTA_Err_t OTA_Suspend(void)
Suspend OTA agent operations .
OTA_FileContext_t::pucUpdateUrlPath
uint8_t * pucUpdateUrlPath
Definition: aws_iot_ota_agent.h:422
pxOTAPALCreateFileForRxCallback_t
OTA_Err_t(* pxOTAPALCreateFileForRxCallback_t)(OTA_FileContext_t *const C)
OTA create file to store received data callback function typedef.
Definition: aws_iot_ota_agent.h:310
OTA_FileContext_t::ulUpdaterVersion
uint32_t ulUpdaterVersion
Definition: aws_iot_ota_agent.h:424
OTA_FileContext_t::ulBlocksRemaining
uint32_t ulBlocksRemaining
Definition: aws_iot_ota_agent.h:414
pxOTAPALSetPlatformImageStateCallback_t
OTA_Err_t(* pxOTAPALSetPlatformImageStateCallback_t)(uint32_t ulServerFileID, OTA_ImageState_t eState)
OTA Set Platform Image State callback function typedef.
Definition: aws_iot_ota_agent.h:344
pxOTAPALResetDeviceCallback_t
OTA_Err_t(* pxOTAPALResetDeviceCallback_t)(uint32_t ulServerFileID)
OTA Reset Device callback function typedef.
Definition: aws_iot_ota_agent.h:332
OTA_ImageState_t
OTA_ImageState_t
OTA Image states.
Definition: aws_iot_ota_agent.h:212