FreeRTOS
aws_ota_agent.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS
3  * Copyright (C) 2017 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_OTA_AGENT_H_
32 #define _AWS_OTA_AGENT_H_
33 
34 /* Type definitions for OTA Agent */
35 #include "aws_ota_types.h"
36 
37 /* Includes required by the FreeRTOS timers structure. */
38 #include "FreeRTOS.h"
39 #include "timers.h"
40 
41 /* Include for console serial output. */
42 #include "aws_logging_task.h"
43 
44 /* Evaluates to the length of a constant string defined like 'static const char str[]= "xyz"; */
45 #define CONST_STRLEN( s ) ( ( ( uint32_t ) sizeof( s ) ) - 1UL )
46 
47 /* The OTA signature algorithm string is specified by the PAL. */
48 #define OTA_FILE_SIG_KEY_STR_MAX_LENGTH 32
49 extern const char pcOTA_JSON_FileSignatureKey[ OTA_FILE_SIG_KEY_STR_MAX_LENGTH ];
50 
54 #define OTA_DEBUG_LOG_LEVEL 1
55 #if OTA_DEBUG_LOG_LEVEL >= 1
56 #define DEFINE_OTA_METHOD_NAME(name) static const char OTA_METHOD_NAME[] = name;
57 #define OTA_LOG_L1 vLoggingPrintf
58 #else
59 #define DEFINE_OTA_METHOD_NAME(name)
60 #define OTA_LOG_L1(...)
61 #endif
62 #if OTA_DEBUG_LOG_LEVEL >= 2
63 #define DEFINE_OTA_METHOD_NAME_L2(name) static const char OTA_METHOD_NAME[] = name;
64 #define OTA_LOG_L2 vLoggingPrintf
65 #else
66 #define DEFINE_OTA_METHOD_NAME_L2(name)
67 #define OTA_LOG_L2(...)
68 #endif
69 #if OTA_DEBUG_LOG_LEVEL >= 3
70 #define DEFINE_OTA_METHOD_NAME_L3(name) static const char OTA_METHOD_NAME[] = name;
71 #define OTA_LOG_L3 vLoggingPrintf
72 #else
73 #define DEFINE_OTA_METHOD_NAME_L3(name)
74 #define OTA_LOG_L3(...)
75 #endif
76 
84 typedef enum {
90  eOTA_NumAgentStates = 4
91 } OTA_State_t;
92 
93 /* A composite cryptographic signature structure able to hold our largest supported signature. */
94 
95 #define kOTA_MaxSignatureSize 256 /* Max bytes supported for a file signature (2048 bit RSA is 256 bytes). */
96 
97 typedef struct {
98  uint16_t usSize; /* Size, in bytes, of the signature. */
99  uint8_t ucData[kOTA_MaxSignatureSize]; /* The binary signature data. */
100 } Sig256_t;
101 
105 typedef uint32_t OTA_Err_t;
106 
115 #define kOTA_PAL_ErrMask 0xffffffUL
116 #define kOTA_Main_ErrMask 0xff000000UL
117 #define kOTA_MainErrShiftDownBits 24U
125 #define kOTA_Err_Panic 0xfe000000UL
126 #define kOTA_Err_Uninitialized 0xff000000UL
127 #define kOTA_Err_None 0x00000000UL
128 #define kOTA_Err_SignatureCheckFailed 0x01000000UL
129 #define kOTA_Err_BadSignerCert 0x02000000UL
130 #define kOTA_Err_OutOfMemory 0x03000000UL
131 #define kOTA_Err_ActivateFailed 0x04000000UL
132 #define kOTA_Err_CommitFailed 0x05000000UL
133 #define kOTA_Err_RejectFailed 0x06000000UL
134 #define kOTA_Err_AbortFailed 0x07000000UL
135 #define kOTA_Err_PublishFailed 0x08000000UL
136 #define kOTA_Err_BadImageState 0x09000000UL
137 #define kOTA_Err_NoActiveJob 0x0a000000UL
138 #define kOTA_Err_NoFreeContext 0x0b000000UL
139 #define kOTA_Err_FileAbort 0x10000000UL
140 #define kOTA_Err_FileClose 0x11000000UL
141 #define kOTA_Err_RxFileCreateFailed 0x12000000UL
142 #define kOTA_Err_BootInfoCreateFailed 0x13000000UL
143 #define kOTA_Err_RxFileTooLarge 0x14000000UL
144 #define kOTA_Err_NullFilePtr 0x20000000UL
145 #define kOTA_Err_MomentumAbort 0x21000000UL
146 #define kOTA_Err_DowngradeNotAllowed 0x22000000UL
147 #define kOTA_Err_SameFirmwareVersion 0x23000000UL
148 #define kOTA_Err_JobParserError 0x24000000UL
149 #define kOTA_Err_FailedToEncodeCBOR 0x25000000UL
150 #define kOTA_Err_ImageStateMismatch 0x26000000UL
151 #define kOTA_Err_GenericIngestError 0x27000000UL
152 #define kOTA_Err_UserAbort 0x28000000UL
153 #define kOTA_Err_ResetNotSupported 0x29000000UL
154 #define kOTA_Err_TopicTooLarge 0x2a000000UL
172 typedef enum {
176  eOTA_LastJobEvent = eOTA_JobEvent_StartTest
178 
179 
194 typedef enum {
201  eOTA_LastImageState = eOTA_ImageState_Aborted
203 
204 
211 typedef struct {
212 
213  uint8_t *pacFilepath;
214  union {
215 
216  int32_t iFileHandle;
218 #if WIN32
219  FILE *pstFile;
220 #endif
221  uint8_t *pucFile;
222  };
223  TimerHandle_t pvRequestTimer;
224  uint32_t ulFileSize;
225  uint32_t ulBlocksRemaining;
226  uint32_t ulFileAttributes;
227  uint32_t ulServerFileID;
228  uint32_t ulRequestMomentum;
229  uint8_t *pacJobName;
230  uint8_t *pacStreamName;
232  uint8_t *pacRxBlockBitmap;
233  uint8_t *pacCertFilepath;
234  uint32_t ulUpdaterVersion;
235  bool_t bIsInSelfTest;
238 
239 
269 typedef void (*pxOTACompleteCallback_t)( OTA_JobEvent_t eEvent );
270 
271 /*---------------------------------------------------------------------------*/
272 /* Public API */
273 /*---------------------------------------------------------------------------*/
274 
294 OTA_State_t OTA_AgentInit( void *pvClient, const uint8_t *pcThingName, pxOTACompleteCallback_t xFunc, TickType_t xTicksToWait );
295 
309 OTA_State_t OTA_AgentShutdown( TickType_t xTicksToWait );
310 
317 
330 
344 
354 
355 /* @brief Request for the next available OTA job from the job service via MQTT.
356  *
357  * @return kOTA_Err_None if successful, otherwise an error code prefixed with 'kOTA_Err_' from the
358  * list above.
359  */
360 OTA_Err_t OTA_CheckForUpdate( void );
361 
362 /*---------------------------------------------------------------------------*/
363 /* Statistics API */
364 /*---------------------------------------------------------------------------*/
365 
374 uint32_t OTA_GetPacketsReceived( void );
375 
385 uint32_t OTA_GetPacketsQueued( void );
386 
395 uint32_t OTA_GetPacketsProcessed( void );
396 
405 uint32_t OTA_GetPacketsDropped( void );
406 
407 /* _AWS_OTA_AGENT_H_ */
408 #endif
OTA_State_t
OTA Agent states.
Definition: aws_ota_agent.h:84
uint32_t OTA_Err_t
OTA Error type.
uint8_t * pacRxBlockBitmap
uint32_t OTA_GetPacketsQueued(void)
Get the number of OTA message packets queued by the OTA agent.
OTA_ImageState_t
OTA Image states.
OTA_JobEvent_t
OTA Job callback events.
OTA_ImageState_t OTA_GetImageState(void)
Get the state of the currently running MCU image.
Sig256_t * pxSignature
uint32_t ulServerFileID
uint32_t OTA_GetPacketsProcessed(void)
Get the number of OTA message packets processed by the OTA agent.
OTA File Context Information.
OTA_State_t OTA_AgentShutdown(TickType_t xTicksToWait)
Signal to the OTA Agent to shut down.
uint8_t * pacStreamName
uint8_t * pacCertFilepath
OTA_State_t OTA_GetAgentState(void)
Get the current state of the OTA agent.
uint32_t ulBlocksRemaining
uint8_t * pacJobName
uint32_t ulUpdaterVersion
OTA_Err_t OTA_SetImageState(OTA_ImageState_t eState)
Set the state of the current MCU image.
OTA_Err_t OTA_ActivateNewImage(void)
Activate the newest MCU image received via OTA.
uint8_t * pacFilepath
uint32_t ulRequestMomentum
void(* pxOTACompleteCallback_t)(OTA_JobEvent_t eEvent)
OTA update complete callback function typedef.
OTA_State_t OTA_AgentInit(void *pvClient, const uint8_t *pcThingName, pxOTACompleteCallback_t xFunc, TickType_t xTicksToWait)
OTA Agent initialization function.
uint32_t OTA_GetPacketsDropped(void)
Get the number of OTA message packets dropped by the OTA agent.
uint32_t ulFileAttributes
uint32_t OTA_GetPacketsReceived(void)
Get the number of OTA message packets received by the OTA agent.
TimerHandle_t pvRequestTimer