AWS IoT Over-the-air Update v2.0.0 (Release Candidate)
Client library for AWS IoT OTA
ota_private.h
Go to the documentation of this file.
1/*
2 * AWS IoT Over-the-air Update v2.0.0 (Release Candidate)
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
29#ifndef OTA_PRIVATE_H
30#define OTA_PRIVATE_H
31
32/* Standard includes. */
33/* For FILE type in OtaFileContext_t.*/
34#include <stdio.h>
35#include <stdint.h>
36#include <stdbool.h>
37
38/* OTA_DO_NOT_USE_CUSTOM_CONFIG allows building the OTA library
39 * without a custom config. If a custom config is provided, the
40 * OTA_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
41#ifndef OTA_DO_NOT_USE_CUSTOM_CONFIG
42 #include "ota_config.h"
43#endif
44
45/* Include config defaults header to get default values of configs not defined
46 * in ota_config.h file. */
47#include "ota_config_defaults.h"
48
53/* General constants. */
54#define LOG2_BITS_PER_BYTE 3U
55#define BITS_PER_BYTE ( ( uint32_t ) 1U << LOG2_BITS_PER_BYTE )
56#define OTA_FILE_BLOCK_SIZE ( ( uint32_t ) 1U << otaconfigLOG2_FILE_BLOCK_SIZE )
57#define OTA_MAX_FILES 1U
58#define OTA_MAX_BLOCK_BITMAP_SIZE 128U
59#define OTA_REQUEST_MSG_MAX_SIZE ( 3U * OTA_MAX_BLOCK_BITMAP_SIZE )
60#define OTA_REQUEST_URL_MAX_SIZE ( 1500 )
61#define OTA_ERASED_BLOCKS_VAL 0xffU
62#ifdef configOTA_NUM_MSG_Q_ENTRIES
63 #define OTA_NUM_MSG_Q_ENTRIES configOTA_NUM_MSG_Q_ENTRIES
64#else
65 #define OTA_NUM_MSG_Q_ENTRIES 20U
66#endif
73/* Job document parser constants. */
74#define OTA_MAX_JSON_TOKENS 64U
75#define OTA_MAX_JSON_STR_LEN 256U
76#define OTA_DOC_MODEL_MAX_PARAMS 32U
77#define OTA_JOB_PARAM_REQUIRED true
78#define OTA_JOB_PARAM_OPTIONAL false
79#define OTA_DONT_STORE_PARAM 0xffff
80#define OTA_STORE_NESTED_JSON 0x1fffU
81#define OTA_DATA_BLOCK_SIZE ( ( 1U << otaconfigLOG2_FILE_BLOCK_SIZE ) + OTA_REQUEST_URL_MAX_SIZE + 30 )
88/* OTA Agent task event flags. */
89#define OTA_EVT_MASK_JOB_MSG_READY 0x00000001UL
90#define OTA_EVT_MASK_DATA_MSG_READY 0x00000002UL
91#define OTA_EVT_MASK_SHUTDOWN 0x00000004UL
92#define OTA_EVT_MASK_REQ_TIMEOUT 0x00000008UL
93#define OTA_EVT_MASK_USER_ABORT 0x000000016UL
94#define OTA_EVT_MASK_ALL_EVENTS ( OTA_EVT_MASK_JOB_MSG_READY | OTA_EVT_MASK_DATA_MSG_READY | OTA_EVT_MASK_SHUTDOWN | OTA_EVT_MASK_REQ_TIMEOUT | OTA_EVT_MASK_USER_ABORT )
101#define OTA_NUM_JOB_PARAMS ( 21 )
102
107#define OTA_JOB_ID_MAX_SIZE ( 72UL + 1UL )
108
114#define kOTA_MaxSignatureSize 256 /* Max bytes supported for a file signature (2048 bit RSA is 256 bytes). */
115
127#define OTA_JSON_SEPARATOR "."
128#define OTA_JSON_CLIENT_TOKEN_KEY "clientToken"
129#define OTA_JSON_TIMESTAMP_KEY "timestamp"
130#define OTA_JSON_EXECUTION_KEY "execution"
131#define OTA_JSON_JOB_ID_KEY OTA_JSON_EXECUTION_KEY OTA_JSON_SEPARATOR "jobId"
132#define OTA_JSON_STATUS_DETAILS_KEY OTA_JSON_EXECUTION_KEY OTA_JSON_SEPARATOR "statusDetails"
133#define OTA_JSON_SELF_TEST_KEY OTA_JSON_STATUS_DETAILS_KEY OTA_JSON_SEPARATOR "self_test"
134#define OTA_JSON_UPDATED_BY_KEY OTA_JSON_STATUS_DETAILS_KEY OTA_JSON_SEPARATOR "updatedBy"
135#define OTA_JSON_UPDATED_BY_KEY_ONLY "updatedBy"
136#define OTA_JSON_SELF_TEST_KEY_ONLY "self_test"
137#define OTA_JSON_JOB_DOC_KEY OTA_JSON_EXECUTION_KEY OTA_JSON_SEPARATOR "jobDocument"
138#define OTA_JSON_OTA_UNIT_KEY OTA_JSON_JOB_DOC_KEY OTA_JSON_SEPARATOR "afr_ota"
139#define OTA_JSON_PROTOCOLS_KEY OTA_JSON_OTA_UNIT_KEY OTA_JSON_SEPARATOR "protocols"
140#define OTA_JSON_FILE_GROUP_KEY OTA_JSON_OTA_UNIT_KEY OTA_JSON_SEPARATOR "files"
141#define OTA_JSON_STREAM_NAME_KEY OTA_JSON_OTA_UNIT_KEY OTA_JSON_SEPARATOR "streamname"
142#define OTA_JSON_FILE_PATH_KEY "filepath"
143#define OTA_JSON_FILE_SIZE_KEY "filesize"
144#define OTA_JSON_FILE_ID_KEY "fileid"
145#define OTA_JSON_FILE_ATTRIBUTE_KEY "attr"
146#define OTA_JSON_FILE_CERT_NAME_KEY "certfile"
147#define OTA_JSON_UPDATE_DATA_URL_KEY "update_data_url"
148#define OTA_JSON_AUTH_SCHEME_KEY "auth_scheme"
149#define OTA_JSON_FILETYPE_KEY "fileType"
159typedef enum
160{
176
182typedef enum
183{
202
208typedef enum
209{
210 ModelParamTypeStringCopy,
211 ModelParamTypeStringInDoc, /* Only use this type if you can process before freeing the document memory. */
212 ModelParamTypeObject,
213 ModelParamTypeArray,
214 ModelParamTypeUInt32,
215 ModelParamTypeSigBase64,
216 ModelParamTypeIdent,
217 ModelParamTypeArrayCopy
219
225typedef enum
226{
227 JobStatusInProgress = 0,
228 JobStatusFailed,
229 JobStatusSucceeded,
230 JobStatusRejected, /* Not possible today using the "get next job" feature. FUTURE! */
231 JobStatusFailedWithVal, /* This shows 2 numeric reason codes. */
232 NumJobStatusMappings
234
240typedef enum
241{
242 JobReasonReceiving = 0, /* Update progress status. */
243 JobReasonSigCheckPassed, /* Set status details to Self Test Ready. */
244 JobReasonSelfTestActive, /* Set status details to Self Test Active. */
245 JobReasonAccepted, /* Set job state to Succeeded. */
246 JobReasonRejected, /* Set job state to Failed. */
247 JobReasonAborted, /* Set job state to Failed. */
248 NumJobReasons
250
262typedef struct
263{
264 const char * pSrcKey;
265 const bool required;
266 uint16_t pDestOffset;
270
284typedef struct
285{
286 void * contextBase;
287 uint32_t contextSize;
289 uint16_t numModelParams;
293
298typedef struct OtaAgentStatistics
299{
305
321typedef enum OtaImageState
322{
329 OtaLastImageState = OtaImageStateAborted
331
338typedef enum OtaPalImageState
339{
345
352typedef enum OtaEvent
353{
369
376typedef struct
377{
378 uint16_t size;
379 uint8_t data[ kOTA_MaxSignatureSize ];
380} Sig256_t;
381
389typedef struct OtaFileContext
390{
391 uint8_t * pFilePath;
393 #if defined( WIN32 ) || defined( __linux__ )
394 FILE * pFile;
395 #else
396 uint8_t * pFile;
397 #endif
398 uint32_t fileSize;
400 uint32_t fileAttributes;
401 uint32_t serverFileID;
402 uint8_t * pJobName;
403 uint16_t jobNameMaxSize;
404 uint8_t * pStreamName;
406 uint8_t * pRxBlockBitmap;
408 uint8_t * pCertFilepath;
410 uint8_t * pUpdateUrlPath;
412 uint8_t * pAuthScheme;
414 uint32_t updaterVersion;
416 uint8_t * pProtocols;
418 uint8_t * pDecodeMem;
420 uint32_t fileType;
423
429typedef struct OtaEventData
430{
431 uint8_t data[ OTA_DATA_BLOCK_SIZE ];
432 uint32_t dataLength;
435
441typedef struct OtaEventMsg
442{
446
447#endif /* ifndef OTA_PRIVATE_H */
#define OTA_DATA_BLOCK_SIZE
Header is 19 bytes.
Definition: ota_private.h:81
#define kOTA_MaxSignatureSize
A composite cryptographic signature structure able to hold our largest supported signature.
Definition: ota_private.h:114
OtaEvent_t
OTA Agent Events.
Definition: ota_private.h:353
OtaPalImageState_t
OTA Platform Image State.
Definition: ota_private.h:339
OtaImageState_t
OTA Image states.
Definition: ota_private.h:322
@ OtaAgentEventRequestJobDocument
Event for requesting job document.
Definition: ota_private.h:356
@ OtaAgentEventReceivedJobDocument
Event when job document is received.
Definition: ota_private.h:357
@ OtaAgentEventMax
Last event specifier.
Definition: ota_private.h:367
@ OtaAgentEventReceivedFileBlock
Event to trigger when file block is received.
Definition: ota_private.h:360
@ OtaAgentEventUserAbort
Event triggered by user to stop agent.
Definition: ota_private.h:365
@ OtaAgentEventCreateFile
Event to create a file.
Definition: ota_private.h:358
@ OtaAgentEventStart
Start the OTA state machine.
Definition: ota_private.h:354
@ OtaAgentEventCloseFile
Event to trigger closing file.
Definition: ota_private.h:362
@ OtaAgentEventShutdown
Event to trigger ota shutdown.
Definition: ota_private.h:366
@ OtaAgentEventSuspend
Event to suspend ota task.
Definition: ota_private.h:363
@ OtaAgentEventRequestTimer
Event to request event timer.
Definition: ota_private.h:361
@ OtaAgentEventRequestFileBlock
Event to request file blocks.
Definition: ota_private.h:359
@ OtaAgentEventStartSelfTest
Event to trigger self test.
Definition: ota_private.h:355
@ OtaAgentEventResume
Event to resume suspended task.
Definition: ota_private.h:364
@ OtaPalImageStateInvalid
OTA PAL Image is invalid.
Definition: ota_private.h:343
@ OtaPalImageStateUnknown
The initial state of the OTA PAL Image.
Definition: ota_private.h:340
@ OtaPalImageStateValid
OTA PAL Image is valid.
Definition: ota_private.h:342
@ OtaPalImageStatePendingCommit
OTA PAL Image awaiting update.
Definition: ota_private.h:341
@ OtaImageStateRejected
The state of the OTA MCU Image when the job has been rejected.
Definition: ota_private.h:326
@ OtaImageStateTesting
The state of the OTA MCU Image post successful download and reboot.
Definition: ota_private.h:324
@ OtaImageStateAborted
The state of the OTA MCU Image after a timeout publish to the stream request fails....
Definition: ota_private.h:327
@ OtaImageStateAccepted
The state of the OTA MCU Image post successful download and successful self_test.
Definition: ota_private.h:325
@ OtaImageStateUnknown
The initial state of the OTA MCU Image.
Definition: ota_private.h:323
This represents the default values for the configuration macros for the OTA library.
ModelParamType_t
Document model parameter types used by the JSON document parser.
Definition: ota_private.h:209
OtaJobReason_t
Gives the reason to set for job parsing operation.
Definition: ota_private.h:241
IngestResult_t
Data ingest results.
Definition: ota_private.h:160
@ IngestResultNoDecodeMemory
Definition: ota_private.h:171
@ IngestResultFileCloseFail
Definition: ota_private.h:163
@ IngestResultUnexpectedBlock
Definition: ota_private.h:166
@ IngestResultDuplicate_Continue
Definition: ota_private.h:174
@ IngestResultNullContext
Definition: ota_private.h:164
@ IngestResultSigCheckFail
Definition: ota_private.h:162
@ IngestResultBadFileHandle
Definition: ota_private.h:165
@ IngestResultAccepted_Continue
Definition: ota_private.h:173
@ IngestResultWriteBlockFailed
Definition: ota_private.h:169
@ IngestResultBadData
Definition: ota_private.h:168
@ IngestResultNullResultPointer
Definition: ota_private.h:170
@ IngestResultUninitialized
Definition: ota_private.h:172
@ IngestResultBlockOutOfRange
Definition: ota_private.h:167
@ IngestResultFileComplete
Definition: ota_private.h:161
OtaJobStatus_t
Gives the status of the job parsing operation.
Definition: ota_private.h:226
DocParseErr_t
Generic JSON document parser errors.
Definition: ota_private.h:183
@ DocParseErrMalformedDoc
Definition: ota_private.h:192
@ DocParseErrParamKeyNotInModel
Definition: ota_private.h:198
@ DocParseErrNullBodyPointer
Definition: ota_private.h:195
@ DocParseErrInvalidModelParamType
Definition: ota_private.h:199
@ DocParseErrInvalidNumChar
Definition: ota_private.h:190
@ DocParseErrDuplicatesNotAllowed
Definition: ota_private.h:191
@ DocParseErrNullDocPointer
Definition: ota_private.h:196
@ DocParseErrOutOfMemory
Definition: ota_private.h:186
@ DocParseErrUserBufferInsuffcient
Definition: ota_private.h:187
@ DocParseErrNullModelPointer
Definition: ota_private.h:194
@ DocParseErrBase64Decode
Definition: ota_private.h:189
@ DocParseErrUnknown
Definition: ota_private.h:184
@ DocParseErrFieldTypeMismatch
Definition: ota_private.h:188
@ DocParseErrInvalidToken
Definition: ota_private.h:200
@ DocParseErr_InvalidJSONBuffer
Definition: ota_private.h:193
@ DocParseErrNone
Definition: ota_private.h:185
@ DocParseErrTooManyParams
Definition: ota_private.h:197
JSON document model to store the details of parameters expected in the job document.
Definition: ota_private.h:285
uint16_t numModelParams
Definition: ota_private.h:289
uint32_t paramsReceivedBitmap
Definition: ota_private.h:290
void * contextBase
Definition: ota_private.h:286
const JsonDocParam_t * pBodyDef
Definition: ota_private.h:288
uint32_t contextSize
Definition: ota_private.h:287
uint32_t paramsRequiredBitmap
Definition: ota_private.h:291
JSON document parameter to store the details of keys and where to store them.
Definition: ota_private.h:263
uint16_t pDestOffset
Definition: ota_private.h:266
const bool required
Definition: ota_private.h:265
uint16_t pDestSizeOffset
Definition: ota_private.h:267
const char * pSrcKey
Definition: ota_private.h:264
const ModelParamType_t modelParamType
Definition: ota_private.h:268
This is the OTA statistics structure to hold useful info.
Definition: ota_private.h:299
uint32_t otaPacketsReceived
Definition: ota_private.h:300
uint32_t otaPacketsQueued
Definition: ota_private.h:301
uint32_t otaPacketsProcessed
Definition: ota_private.h:302
uint32_t otaPacketsDropped
Definition: ota_private.h:303
The OTA Agent event and data structures.
Definition: ota_private.h:430
uint32_t dataLength
Definition: ota_private.h:432
bool bufferUsed
Definition: ota_private.h:433
Stores information about the event message.
Definition: ota_private.h:442
OtaEvent_t eventId
Definition: ota_private.h:444
OtaEventData_t * pEventData
Definition: ota_private.h:443
OTA File Context Information.
Definition: ota_private.h:390
uint16_t filePathMaxSize
Maximum size of the update file path.
Definition: ota_private.h:392
uint32_t decodeMemMaxSize
Maximum size of the decode memory.
Definition: ota_private.h:419
uint16_t jobNameMaxSize
Maximum size of the job name.
Definition: ota_private.h:403
uint8_t * pRxBlockBitmap
Bitmap of blocks received (for deduplicating and missing block request).
Definition: ota_private.h:406
uint32_t fileAttributes
Flags specific to the file being received (e.g. secure, bundle, archive).
Definition: ota_private.h:400
uint32_t blocksRemaining
How many blocks remain to be received (a code optimization).
Definition: ota_private.h:399
uint32_t fileSize
The size of the file in bytes.
Definition: ota_private.h:398
uint32_t fileType
The file type id set when creating the OTA job.
Definition: ota_private.h:420
uint16_t blockBitmapMaxSize
Maximum size of the block bitmap.
Definition: ota_private.h:407
bool isInSelfTest
True if the job is in self test mode.
Definition: ota_private.h:415
uint8_t * pStreamName
The stream associated with this file from the OTA service.
Definition: ota_private.h:404
uint8_t * pUpdateUrlPath
Url for the file.
Definition: ota_private.h:410
uint16_t authSchemeMaxSize
Maximum size of the auth scheme.
Definition: ota_private.h:413
uint8_t * pFilePath
Update file pathname.
Definition: ota_private.h:391
uint8_t * pFile
File type is RAM/Flash image pointer after file is open for write.
Definition: ota_private.h:396
uint32_t updaterVersion
Used by OTA self-test detection, the version of Firmware that did the update.
Definition: ota_private.h:414
Sig256_t * pSignature
Pointer to the file's signature structure.
Definition: ota_private.h:421
uint8_t * pCertFilepath
Pathname of the certificate file used to validate the receive file.
Definition: ota_private.h:408
uint16_t updateUrlMaxSize
Maximum size of the url.
Definition: ota_private.h:411
uint8_t * pAuthScheme
Authorization scheme.
Definition: ota_private.h:412
uint32_t serverFileID
The file is referenced by this numeric ID in the OTA job.
Definition: ota_private.h:401
uint16_t streamNameMaxSize
Maximum size of the stream name.
Definition: ota_private.h:405
uint8_t * pDecodeMem
Decode memory.
Definition: ota_private.h:418
uint16_t certFilePathMaxSize
Maximum certificate path size.
Definition: ota_private.h:409
uint16_t protocolMaxSize
Maximum size of the supported protocols string.
Definition: ota_private.h:417
uint8_t * pProtocols
Authorization scheme.
Definition: ota_private.h:416
uint8_t * pJobName
The job name associated with this file from the job service.
Definition: ota_private.h:402
OTA File Signature info.
Definition: ota_private.h:377
uint16_t size
Size, in bytes, of the signature.
Definition: ota_private.h:378