AWS IoT Over-the-air Update  v3.0.0
Client library for AWS IoT OTA
ota.h
Go to the documentation of this file.
1 /*
2  * AWS IoT Over-the-air Update v3.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 
28 #ifndef OTA_H
29 #define OTA_H
30 
31 /* Standard includes. */
32 /* For FILE type in OtaFileContext_t.*/
33 #include <stdio.h>
34 #include <stdint.h>
35 
36 /* OTA Library Interface include. */
37 #include "ota_private.h"
38 #include "ota_os_interface.h"
39 #include "ota_mqtt_interface.h"
40 #include "ota_http_interface.h"
41 #include "ota_platform_interface.h"
42 
47 #define CONST_STRLEN( s ) ( ( ( uint32_t ) sizeof( s ) ) - 1UL )
48 
49 
50 #define OTA_FILE_SIG_KEY_STR_MAX_LENGTH 32
59 /* MISRA rule 8.6 requires identifier with external linkage to have exact one external definition.
60  * However, this variable is defined in OTA platform abstraction layer implementation, which is
61  * not in this repository but in C-SDK and amazon-freertos repo, so it's a false positive. */
62 /* coverity[misra_c_2012_rule_8_6_violation] */
64 
65 /*-------------------------- OTA enumerated types --------------------------*/
66 
72 typedef enum OtaErr
73 {
74  OtaErrNone = 0,
98 
107 typedef enum OtaState
108 {
109  OtaAgentStateNoTransition = -1,
110  OtaAgentStateInit = 0,
111  OtaAgentStateReady,
112  OtaAgentStateRequestingJob,
113  OtaAgentStateWaitingForJob,
114  OtaAgentStateCreatingFile,
115  OtaAgentStateRequestingFileBlock,
116  OtaAgentStateWaitingForFileBlock,
117  OtaAgentStateClosingFile,
118  OtaAgentStateSuspended,
119  OtaAgentStateShuttingDown,
120  OtaAgentStateStopped,
121  OtaAgentStateAll
122 } OtaState_t;
123 
128 typedef enum OtaJobParseErr
129 {
130  OtaJobParseErrUnknown = -1, /* @brief The error code has not yet been set by a logic path. */
131  OtaJobParseErrNone = 0, /* @brief Signifies no error has occurred. */
132  OtaJobParseErrNullJob, /* @brief A null job was reported (no job ID). */
133  OtaJobParseErrUpdateCurrentJob, /* @brief We're already busy with the reported job ID. */
134  OtaJobParseErrZeroFileSize, /* @brief Job document specified a zero sized file. This is not allowed. */
135  OtaJobParseErrNonConformingJobDoc, /* @brief The job document failed to fulfill the model requirements. */
136  OtaJobParseErrBadModelInitParams, /* @brief There was an invalid initialization parameter used in the document model. */
137  OtaJobParseErrNoContextAvailable, /* @brief There was not an OTA context available. */
138  OtaJobParseErrNoActiveJobs /* @brief No active jobs are available in the service. */
140 
158 typedef enum OtaJobEvent
159 {
168  OtaLastJobEvent = OtaJobEventStartTest
169 } OtaJobEvent_t;
170 
176 typedef enum
177 {
178  JobStatusInProgress = 0,
179  JobStatusFailed,
180  JobStatusSucceeded,
181  JobStatusRejected, /* Not possible today using the "get next job" feature. FUTURE! */
182  JobStatusFailedWithVal, /* This shows 2 numeric reason codes. */
183  NumJobStatusMappings
185 
194 typedef struct OtaJobDocument
195 {
196  const uint8_t * pJobDocJson;
197  size_t jobDocLength;
198  const uint8_t * pJobId;
199  size_t jobIdLength;
200  uint32_t fileTypeId;
203  int32_t reason;
204  int32_t subReason;
206 
207 /*------------------------- OTA callbacks --------------------------*/
208 
239 typedef void (* OtaAppCallback_t)( OtaJobEvent_t eEvent,
240  const void * pData );
241 
242 /*--------------------------- OTA structs ----------------------------*/
243 
244 
252 typedef struct OtaInterface
253 {
259 
266 typedef struct OtaAppBuffer
267 {
268  uint8_t * pUpdateFilePath;
270  uint8_t * pCertFilePath;
271  uint16_t certFilePathSize;
272  uint8_t * pStreamName;
273  uint16_t streamNameSize;
274  uint8_t * pDecodeMemory;
275  uint32_t decodeMemorySize;
276  uint8_t * pFileBitmap;
277  uint16_t fileBitmapSize;
278  uint8_t * pUrl;
279  uint16_t urlSize;
280  uint8_t * pAuthScheme;
281  uint16_t authSchemeSize;
283 
289 typedef struct OtaAgentContext
290 {
292  uint8_t pThingName[ otaconfigMAX_THINGNAME_LEN + 1U ];
294  uint32_t fileIndex;
295  uint32_t serverFileID;
296  uint8_t pActiveJobName[ OTA_JOB_ID_MAX_SIZE ];
298  uint32_t timestampFromJob;
302  uint32_t requestMomentum;
307 
308 /*------------------------- OTA Public API --------------------------*/
309 
390 /* @[declare_ota_init] */
391 OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer,
392  OtaInterfaces_t * pOtaInterfaces,
393  const uint8_t * pThingName,
394  OtaAppCallback_t OtaAppCallback );
395 /* @[declare_ota_init] */
396 
434 /* @[declare_ota_shutdown] */
435 OtaState_t OTA_Shutdown( uint32_t ticksToWait,
436  uint8_t unsubscribeFlag );
437 /* @[declare_ota_shutdown] */
438 
458 /* @[declare_ota_getstate] */
459 OtaState_t OTA_GetState( void );
460 /* @[declare_ota_getstate] */
461 
496 /* @[declare_ota_activatenewimage] */
498 /* @[declare_ota_activatenewimage] */
499 
534 /* @[declare_ota_setimagestate] */
536 /* @[declare_ota_setimagestate] */
537 
546 /* @[declare_ota_getimagestate] */
548 /* @[declare_ota_getimagestate] */
549 
556 /* @[declare_ota_checkforupdate] */
558 /* @[declare_ota_checkforupdate] */
559 
602 /* @[declare_ota_suspend] */
603 OtaErr_t OTA_Suspend( void );
604 /* @[declare_ota_suspend] */
605 
645 /* @[declare_ota_resume] */
646 OtaErr_t OTA_Resume( void );
647 /* @[declare_ota_resume] */
648 
663 /* @[declare_ota_eventprocessingtask] */
664 void OTA_EventProcessingTask( void * pUnused );
665 /* @[declare_ota_eventprocessingtask] */
666 
667 
709 /* @[declare_ota_signalevent] */
710 bool OTA_SignalEvent( const OtaEventMsg_t * const pEventMsg );
711 /* @[declare_ota_signalevent] */
712 
713 /*---------------------------------------------------------------------------*/
714 /* Statistics API */
715 /*---------------------------------------------------------------------------*/
716 
755 /* @[declare_ota_getstatistics] */
757 /* @[declare_ota_getstatistics] */
758 
766 /* @[declare_ota_err_strerror] */
767 const char * OTA_Err_strerror( OtaErr_t err );
768 /* @[declare_ota_err_strerror] */
769 
777 /* @[declare_ota_jobparse_strerror] */
778 const char * OTA_JobParse_strerror( OtaJobParseErr_t err );
779 /* @[declare_ota_jobparse_strerror] */
780 
788 /* @[declare_ota_palstatus_strerror] */
789 const char * OTA_PalStatus_strerror( OtaPalMainStatus_t status );
790 /* @[declare_ota_palstatus_strerror] */
791 
799 /* @[declare_ota_osstatus_strerror] */
800 const char * OTA_OsStatus_strerror( OtaOsStatus_t status );
801 /* @[declare_ota_osstatus_strerror] */
802 
803 #endif /* ifndef OTA_H */
OtaState_t
OtaState_t
OTA Agent states.
Definition: ota.h:108
OtaAppBuffer_t::pStreamName
uint8_t * pStreamName
Name of stream to download the files.
Definition: ota.h:272
OtaErrFailedToEncodeCbor
@ OtaErrFailedToEncodeCbor
Failed to encode CBOR object for requesting data block from streaming service.
Definition: ota.h:94
OtaAgentContext_t::serverFileID
uint32_t serverFileID
Definition: ota.h:295
OTA_Shutdown
OtaState_t OTA_Shutdown(uint32_t ticksToWait, uint8_t unsubscribeFlag)
Signal to the OTA Agent to shut down.
Definition: ota.c:3157
OtaErrInitFileTransferFailed
@ OtaErrInitFileTransferFailed
Failed to update the OTA job status.
Definition: ota.h:81
ota_os_interface.h
Contains OTA OS Functional Interface statuses, type definitions and structures to store interface rou...
OTA_EventProcessingTask
void OTA_EventProcessingTask(void *pUnused)
OTA agent event processing loop.
Definition: ota.c:2907
OtaJobDocument_t
OTA Job document.
Definition: ota.h:195
OtaErrNoActiveJob
@ OtaErrNoActiveJob
Attempt to set final image state without an active job.
Definition: ota.h:92
OtaInterfaces_t::os
OtaOSInterface_t os
OS interface to store event, timers and memory operations.
Definition: ota.h:254
OtaJobDocument_t::jobDocLength
size_t jobDocLength
Job document length in bytes.
Definition: ota.h:197
OtaErrRequestFileBlockFailed
@ OtaErrRequestFileBlockFailed
Failed to request file block.
Definition: ota.h:82
OtaFileContext_t
OTA File Context Information.
Definition: ota_private.h:380
OtaImageState_t
OtaImageState_t
OTA Image states.
Definition: ota_private.h:312
OtaJobEventStartTest
@ OtaJobEventStartTest
OTA job is now in self test, perform user tests.
Definition: ota.h:162
OTA_SetImageState
OtaErr_t OTA_SetImageState(OtaImageState_t state)
Set the state of the current MCU image.
Definition: ota.c:3301
OtaJobDocument_t::reason
int32_t reason
Job status reason.
Definition: ota.h:203
OtaJobEventSelfTestFailed
@ OtaJobEventSelfTestFailed
OTA self-test failed for current job.
Definition: ota.h:164
OTA_JOB_ID_MAX_SIZE
#define OTA_JOB_ID_MAX_SIZE
Maximum size of the Job ID.
Definition: ota_private.h:107
OTA_GetStatistics
OtaErr_t OTA_GetStatistics(OtaAgentStatistics_t *pStatistics)
Get the statistics of OTA message packets.
Definition: ota.c:3224
OtaAppBuffer_t::certFilePathSize
uint16_t certFilePathSize
Maximum size of the certificate file path.
Definition: ota.h:271
OtaAgentContext_t::OtaAppCallback
OtaAppCallback_t OtaAppCallback
Definition: ota.h:304
OtaAgentContext_t::imageState
OtaImageState_t imageState
Definition: ota.h:299
OtaAgentStatistics_t
This is the OTA statistics structure to hold useful info.
Definition: ota_private.h:289
OtaAppBuffer_t::pUrl
uint8_t * pUrl
Presigned url to download files from S3.
Definition: ota.h:278
OtaErrUserAbort
@ OtaErrUserAbort
User aborted the active OTA.
Definition: ota.h:93
OtaJobEvent_t
OtaJobEvent_t
OTA Job callback events.
Definition: ota.h:159
OtaAppBuffer_t::streamNameSize
uint16_t streamNameSize
Maximum size of the stream name.
Definition: ota.h:273
OtaErr_t
OtaErr_t
The OTA API return status. OTA agent error codes are in the upper 8 bits of the 32 bit OTA error word...
Definition: ota.h:73
OTA_OsStatus_strerror
const char * OTA_OsStatus_strerror(OtaOsStatus_t status)
Status code to string conversion for OTA OS status.
Definition: ota.c:3584
OtaErrCleanupDataFailed
@ OtaErrCleanupDataFailed
Failed to clean up the data plane.
Definition: ota.h:84
OtaAppBuffer_t::pAuthScheme
uint8_t * pAuthScheme
Authentication scheme used to validate download.
Definition: ota.h:280
OtaAppBuffer_t::pCertFilePath
uint8_t * pCertFilePath
Path to certificate file.
Definition: ota.h:270
OtaOSInterface_t
OTA OS Interface.
Definition: ota_os_interface.h:295
OtaErrSameFirmwareVersion
@ OtaErrSameFirmwareVersion
Firmware version is the same as previous. New firmware could have failed to commit.
Definition: ota.h:90
OTA_ActivateNewImage
OtaErr_t OTA_ActivateNewImage(void)
Activate the newest MCU image received via OTA.
Definition: ota.c:3265
OtaAppBuffer_t::pUpdateFilePath
uint8_t * pUpdateFilePath
Path to store the files.
Definition: ota.h:268
OtaJobEventProcessed
@ OtaJobEventProcessed
OTA event queued by OTA_SignalEvent is processed.
Definition: ota.h:163
OtaAppBuffer_t::pDecodeMemory
uint8_t * pDecodeMemory
Place to store the decoded files.
Definition: ota.h:274
OtaErrAgentStopped
@ OtaErrAgentStopped
Returned when operations are performed that requires OTA Agent running & its stopped.
Definition: ota.h:78
OtaErrFailedToDecodeCbor
@ OtaErrFailedToDecodeCbor
Failed to decode CBOR object from streaming service response.
Definition: ota.h:95
OtaErrUninitialized
@ OtaErrUninitialized
The error code has not yet been set by a logic path.
Definition: ota.h:75
OtaInterfaces_t
OTA Interface for referencing different components.
Definition: ota.h:253
OtaAgentContext_t::numOfBlocksToReceive
uint32_t numOfBlocksToReceive
Definition: ota.h:300
OTA_CheckForUpdate
OtaErr_t OTA_CheckForUpdate(void)
Request for the next available OTA job from the job service.
Definition: ota.c:3237
OtaAgentContext_t
The OTA agent is a singleton today. The structure keeps it nice and organized.
Definition: ota.h:290
OtaAgentContext_t::statistics
OtaAgentStatistics_t statistics
Definition: ota.h:301
OtaMqttInterface_t
OTA Event Interface structure.
Definition: ota_mqtt_interface.h:156
OtaJobDocument_t::pJobDocJson
const uint8_t * pJobDocJson
Job document in JSON format.
Definition: ota.h:196
OtaAppBuffer_t::decodeMemorySize
uint32_t decodeMemorySize
Maximum size of the decoded files buffer.
Definition: ota.h:275
OTA_SignalEvent
bool OTA_SignalEvent(const OtaEventMsg_t *const pEventMsg)
Signal event to the OTA Agent task.
Definition: ota.c:2922
ota_private.h
Macros, enums, variables, and definitions internal to the OTA Agent module and shared by other OTA mo...
OtaAppBuffer_t::urlSize
uint16_t urlSize
Maximum size of the URL.
Definition: ota.h:279
OtaJobEventReceivedJob
@ OtaJobEventReceivedJob
OTA event when a new valid AFT-OTA job is received.
Definition: ota.h:166
ota_http_interface.h
Contains OTA HTTP Statuses, function type definitions and http interface structure.
OtaJobEventParseCustomJob
@ OtaJobEventParseCustomJob
OTA event for parsing custom job document.
Definition: ota.h:165
OtaJobStatus_t
OtaJobStatus_t
Gives the status of the job operation.
Definition: ota.h:177
OTA_Err_strerror
const char * OTA_Err_strerror(OtaErr_t err)
Error code to string conversion for OTA errors.
Definition: ota.c:3428
OtaErrNone
@ OtaErrNone
No error occurred during the operation.
Definition: ota.h:74
OtaEventMsg_t
Stores information about the event message.
Definition: ota_private.h:432
OtaErrSignalEventFailed
@ OtaErrSignalEventFailed
Failed to send event to OTA state machine.
Definition: ota.h:79
OTA_Suspend
OtaErr_t OTA_Suspend(void)
Suspend OTA agent operations .
Definition: ota.c:3366
OtaJobDocument_t::jobIdLength
size_t jobIdLength
Length of job ID in bytes.
Definition: ota.h:199
OtaOsStatus_t
OtaOsStatus_t
The OTA OS interface return status.
Definition: ota_os_interface.h:93
OTA_GetState
OtaState_t OTA_GetState(void)
Get the current state of the OTA agent.
Definition: ota.c:3216
OtaJobEventUpdateComplete
@ OtaJobEventUpdateComplete
OTA event when the update is completed.
Definition: ota.h:167
OtaJobDocument_t::pJobId
const uint8_t * pJobId
Job ID associated with the job document.
Definition: ota.h:198
OTA_Resume
OtaErr_t OTA_Resume(void)
Resume OTA agent operations .
Definition: ota.c:3399
OtaErrDowngradeNotAllowed
@ OtaErrDowngradeNotAllowed
Firmware version is older than the previous version.
Definition: ota.h:89
OtaAgentContext_t::requestMomentum
uint32_t requestMomentum
Definition: ota.h:302
OTA_PalStatus_strerror
const char * OTA_PalStatus_strerror(OtaPalMainStatus_t status)
Status code to string conversion for OTA PAL status.
Definition: ota.c:3638
OtaErrPanic
@ OtaErrPanic
Unrecoverable Firmware error. Probably should log error and reboot.
Definition: ota.h:76
OtaAppBuffer_t::updateFilePathsize
uint16_t updateFilePathsize
Maximum size of the file path.
Definition: ota.h:269
OtaAppBuffer_t::pFileBitmap
uint8_t * pFileBitmap
Bitmap of the parameters received.
Definition: ota.h:276
OtaErrJobParserError
@ OtaErrJobParserError
An error occurred during job document parsing. See reason sub-code.
Definition: ota.h:86
OTA_GetImageState
OtaImageState_t OTA_GetImageState(void)
Get the state of the currently running MCU image.
Definition: ota.c:3355
OtaErrActivateFailed
@ OtaErrActivateFailed
Failed to activate the new image.
Definition: ota.h:96
OtaPalMainStatus_t
OtaPalMainStatus_t
The OTA platform interface main status.
Definition: ota_platform_interface.h:73
ota_platform_interface.h
Contains PAL interface statuses, type definitions and structure to store interface routines.
OtaAgentContext_t::timestampFromJob
uint32_t timestampFromJob
Definition: ota.h:298
OtaJobEventFail
@ OtaJobEventFail
OTA receive failed. Unable to use this update.
Definition: ota.h:161
OtaAgentContext_t::pClientTokenFromJob
uint8_t * pClientTokenFromJob
Definition: ota.h:297
OtaHttpInterface_t
OTA Event Interface structure.
Definition: ota_http_interface.h:130
OtaJobDocument_t::parseErr
OtaJobParseErr_t parseErr
Job parsing status.
Definition: ota.h:201
OtaInterfaces_t::mqtt
OtaMqttInterface_t mqtt
MQTT interface that references the publish subscribe methods and callbacks.
Definition: ota.h:255
OtaAgentContext_t::state
OtaState_t state
Definition: ota.h:291
OtaPalInterface_t
OTA pal Interface structure.
Definition: ota_platform_interface.h:292
OtaErrImageStateMismatch
@ OtaErrImageStateMismatch
The OTA job was in Self Test but the platform image state was not. Possible tampering.
Definition: ota.h:91
OtaAgentContext_t::pOtaInterface
OtaInterfaces_t * pOtaInterface
Definition: ota.h:303
OtaAppBuffer_t::fileBitmapSize
uint16_t fileBitmapSize
Maximum size of the bitmap.
Definition: ota.h:277
OtaErrInvalidArg
@ OtaErrInvalidArg
API called with invalid argument.
Definition: ota.h:77
OtaInterfaces_t::http
OtaHttpInterface_t http
HTTP interface to request data.
Definition: ota.h:256
ota_mqtt_interface.h
Contains OTA MQTT Statuses, function type definitions and mqtt interface structure.
OtaErrInvalidDataProtocol
@ OtaErrInvalidDataProtocol
Job does not have a valid protocol for data transfer.
Definition: ota.h:87
OtaJobDocument_t::subReason
int32_t subReason
Job status subreason.
Definition: ota.h:204
OtaAgentContext_t::fileIndex
uint32_t fileIndex
Definition: ota.h:294
otaconfigMAX_THINGNAME_LEN
#define otaconfigMAX_THINGNAME_LEN
The maximum allowed length of the thing name used by the OTA agent.
Definition: ota_config_defaults.h:132
OtaJobDocument_t::status
OtaJobStatus_t status
Job status.
Definition: ota.h:202
OtaJobDocument_t::fileTypeId
uint32_t fileTypeId
File Type ID from the job document.
Definition: ota.h:200
OtaErrUpdateJobStatusFailed
@ OtaErrUpdateJobStatusFailed
Failed to update the OTA job status.
Definition: ota.h:85
OtaErrCleanupControlFailed
@ OtaErrCleanupControlFailed
Failed to clean up the control plane.
Definition: ota.h:83
OtaErrMomentumAbort
@ OtaErrMomentumAbort
Too many OTA stream requests without any response.
Definition: ota.h:88
OtaAppBuffer_t::authSchemeSize
uint16_t authSchemeSize
Maximum size of the auth scheme.
Definition: ota.h:281
OTA_JobParse_strerror
const char * OTA_JobParse_strerror(OtaJobParseErr_t err)
Error code to string conversion for OTA Job Parsing errors.
Definition: ota.c:3534
OtaJobParseErr_t
OtaJobParseErr_t
OTA job document parser error codes.
Definition: ota.h:129
OtaAgentContext_t::fileContext
OtaFileContext_t fileContext
Definition: ota.h:293
OtaAgentContext_t::unsubscribeOnShutdown
uint8_t unsubscribeOnShutdown
Definition: ota.h:305
OTA_JsonFileSignatureKey
const char OTA_JsonFileSignatureKey[OTA_FILE_SIG_KEY_STR_MAX_LENGTH]
The OTA signature algorithm string is specified by the PAL.
OtaErrRequestJobFailed
@ OtaErrRequestJobFailed
Failed to request the job document.
Definition: ota.h:80
OtaAppCallback_t
void(* OtaAppCallback_t)(OtaJobEvent_t eEvent, const void *pData)
OTA update complete callback function typedef.
Definition: ota.h:239
OtaInterfaces_t::pal
OtaPalInterface_t pal
OTA PAL callback structure.
Definition: ota.h:257
OtaJobEventActivate
@ OtaJobEventActivate
OTA receive is authenticated and ready to activate.
Definition: ota.h:160
OtaAppBuffer_t
OTA Application Buffer size information.
Definition: ota.h:267
OTA_FILE_SIG_KEY_STR_MAX_LENGTH
#define OTA_FILE_SIG_KEY_STR_MAX_LENGTH
Definition: ota.h:50
OTA_Init
OtaErr_t OTA_Init(OtaAppBuffer_t *pOtaBuffer, OtaInterfaces_t *pOtaInterfaces, const uint8_t *pThingName, OtaAppCallback_t OtaAppCallback)
OTA Agent initialization function.
Definition: ota.c:3063