AWS IoT Device SDK C: Jobs
AWS IoT Jobs library
Return to main page ↑
aws_iot_jobs_internal.h
Go to the documentation of this file.
1 /*
2  * AWS IoT Jobs V1.0.0
3  * Copyright (C) 2019 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 AWS_IOT_JOBS_INTERNAL_H_
30 #define AWS_IOT_JOBS_INTERNAL_H_
31 
32 /* The config header is always included first. */
33 #include "iot_config.h"
34 
35 /* Linear containers (lists and queues) include. */
36 #include "iot_linear_containers.h"
37 
38 /* Jobs include. */
39 #include "aws_iot_jobs.h"
40 
41 /* AWS IoT include. */
42 #include "aws_iot.h"
43 
53 #if AWS_IOT_JOBS_ENABLE_ASSERTS == 1
54  #ifndef AwsIotJobs_Assert
55  #ifdef Iot_DefaultAssert
56  #define AwsIotJobs_Assert( expression ) Iot_DefaultAssert( expression )
57  #else
58  #error "Asserts are enabled for Jobs, but AwsIotJobs_Assert is not defined"
59  #endif
60  #endif
61 #else
62  #define AwsIotJobs_Assert( expression )
63 #endif
64 
65 /* Configure logs for Jobs functions. */
66 #ifdef AWS_IOT_LOG_LEVEL_JOBS
67  #define LIBRARY_LOG_LEVEL AWS_IOT_LOG_LEVEL_JOBS
68 #else
69  #ifdef IOT_LOG_LEVEL_GLOBAL
70  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
71  #else
72  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
73  #endif
74 #endif
75 
76 #define LIBRARY_LOG_NAME ( "Jobs" )
77 #include "iot_logging_setup.h"
78 
79 /*
80  * Provide default values for undefined memory allocation functions based on
81  * the usage of dynamic memory allocation.
82  */
83 #if IOT_STATIC_MEMORY_ONLY == 1
84  #include "iot_static_memory.h"
85 
91  void * AwsIotJobs_MallocOperation( size_t size );
92 
98  void AwsIotJobs_FreeOperation( void * ptr );
99 
105  #define AwsIotJobs_MallocString Iot_MallocMessageBuffer
106 
112  #define AwsIotJobs_FreeString Iot_FreeMessageBuffer
113 
119  void * AwsIotJobs_MallocSubscription( size_t size );
120 
126  void AwsIotJobs_FreeSubscription( void * ptr );
127 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
128  #ifndef AwsIotJobs_MallocOperation
129  #ifdef Iot_DefaultMalloc
130  #define AwsIotJobs_MallocOperation Iot_DefaultMalloc
131  #else
132  #error "No malloc function defined for AwsIotJobs_MallocOperation"
133  #endif
134  #endif
135 
136  #ifndef AwsIotJobs_FreeOperation
137  #ifdef Iot_DefaultFree
138  #define AwsIotJobs_FreeOperation Iot_DefaultFree
139  #else
140  #error "No free function defined for AwsIotJobs_FreeOperation"
141  #endif
142  #endif
143 
144  #ifndef AwsIotJobs_MallocString
145  #ifdef Iot_DefaultMalloc
146  #define AwsIotJobs_MallocString Iot_DefaultMalloc
147  #else
148  #error "No malloc function defined for AwsIotJobs_MallocString"
149  #endif
150  #endif
151 
152  #ifndef AwsIotJobs_FreeString
153  #ifdef Iot_DefaultFree
154  #define AwsIotJobs_FreeString Iot_DefaultFree
155  #else
156  #error "No free function defined for AwsIotJobs_FreeString"
157  #endif
158  #endif
159 
160  #ifndef AwsIotJobs_MallocSubscription
161  #ifdef Iot_DefaultMalloc
162  #define AwsIotJobs_MallocSubscription Iot_DefaultMalloc
163  #else
164  #error "No malloc function defined for AwsIotJobs_MallocSubscription"
165  #endif
166  #endif
167 
168  #ifndef AwsIotJobs_FreeSubscription
169  #ifdef Iot_DefaultFree
170  #define AwsIotJobs_FreeSubscription Iot_DefaultFree
171  #else
172  #error "No free function defined for AwsIotJobs_FreeSubscription"
173  #endif
174  #endif
175 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
176 
183 #ifndef AWS_IOT_JOBS_DEFAULT_MQTT_TIMEOUT_MS
184  #define AWS_IOT_JOBS_DEFAULT_MQTT_TIMEOUT_MS ( 5000 )
185 #endif
186 #ifndef AWS_IOT_JOBS_NOTIFY_CALLBACKS
187  #define AWS_IOT_JOBS_NOTIFY_CALLBACKS ( 1 )
188 #endif
189 
196 #define JOBS_OPERATION_COUNT ( 4 )
197 
204 #define JOBS_CALLBACK_COUNT ( 2 )
205 
209 #define JOBS_GET_PENDING_OPERATION_STRING "/jobs/get"
210 
214 #define JOBS_GET_PENDING_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( JOBS_GET_PENDING_OPERATION_STRING ) - 1 ) )
215 
219 #define JOBS_START_NEXT_OPERATION_STRING "/jobs/start-next"
220 
224 #define JOBS_START_NEXT_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( JOBS_START_NEXT_OPERATION_STRING ) - 1 ) )
225 
231 #define JOBS_DESCRIBE_OPERATION_STRING "/get"
232 
236 #define JOBS_DESCRIBE_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( JOBS_DESCRIBE_OPERATION_STRING ) - 1 ) )
237 
243 #define JOBS_UPDATE_OPERATION_STRING "/update"
244 
250 #define JOBS_UPDATE_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( JOBS_UPDATE_OPERATION_STRING ) - 1 ) )
251 
256 #define JOBS_NOTIFY_PENDING_CALLBACK_STRING "/jobs/notify"
257 
261 #define JOBS_NOTIFY_PENDING_CALLBACK_STRING_LENGTH ( ( uint16_t ) ( sizeof( JOBS_NOTIFY_PENDING_CALLBACK_STRING ) - 1 ) )
262 
267 #define JOBS_NOTIFY_NEXT_CALLBACK_STRING "/jobs/notify-next"
268 
272 #define JOBS_NOTIFY_NEXT_CALLBACK_STRING_LENGTH ( ( uint16_t ) ( sizeof( JOBS_NOTIFY_NEXT_CALLBACK_STRING ) - 1 ) )
273 
279 #define JOBS_MAX_ID_LENGTH ( 64 )
280 
286 #define JOBS_MAX_TIMEOUT ( 10080 )
287 
297 #define JOBS_MAX_STATUS_DETAILS_LENGTH ( 32768 )
298 
305 #define JOBS_LONGEST_SUFFIX_LENGTH ( JOBS_MAX_ID_LENGTH + JOBS_UPDATE_OPERATION_STRING_LENGTH + 6 )
306 
307 /*------------------------ Jobs internal data types -------------------------*/
308 
312 typedef enum _jobsOperationType
313 {
314  /* Jobs operations. */
320  /* Jobs callbacks. */
324 
328 typedef enum _jobsCallbackType
329 {
333 
337 typedef union _jsonRequestContents
338 {
341  struct
342  {
343  int32_t executionNumber;
345  } describe;
347 
353 typedef struct _jobsSubscription
354 {
357  int32_t operationReferences[ JOBS_OPERATION_COUNT ];
361  AwsIotJobsCallbackInfo_t callbacks[ JOBS_CALLBACK_COUNT ][ AWS_IOT_JOBS_NOTIFY_CALLBACKS ];
362 
369  char * pTopicBuffer;
370 
372  char pThingName[];
374 
380 typedef struct _jobsOperation
381 {
384  /* Basic operation information. */
386  uint32_t flags;
392  /* Jobs request information. */
393  const char * pJobsRequest;
396  const char * pClientToken;
399  /* Jobs response information. */
400  const char * pJobsResponse;
408  void * ( *mallocResponse )( size_t );
409 
410  /* How to notify of an operation's completion. */
411  union
412  {
415  } notify;
417  size_t jobIdLength;
418  char pJobId[];
420 
421 /* Declarations of names printed in logs. */
422 #if LIBRARY_LOG_LEVEL > IOT_LOG_NONE
423  extern const char * const _pAwsIotJobsOperationNames[];
424  extern const char * const _pAwsIotJobsCallbackNames[];
425 #endif
426 
427 /* Declarations of variables for internal Jobs files. */
428 extern uint32_t _AwsIotJobsMqttTimeoutMs;
433 
434 /*------------------------ Jobs operation functions -------------------------*/
435 
450  const AwsIotJobsRequestInfo_t * pRequestInfo,
451  const _jsonRequestContents_t * pRequestContents,
452  uint32_t flags,
453  const AwsIotJobsCallbackInfo_t * pCallbackInfo,
454  _jobsOperation_t ** pNewOperation );
455 
464 void _AwsIotJobs_DestroyOperation( void * pData );
465 
485  const AwsIotJobsRequestInfo_t * pRequestInfo,
486  char ** pTopicBuffer,
487  uint16_t * pOperationTopicLength );
488 
499  _jobsOperation_t * pOperation );
500 
501 /*----------------------- Jobs subscription functions -----------------------*/
502 
518 _jobsSubscription_t * _AwsIotJobs_FindSubscription( const char * pThingName,
519  size_t thingNameLength,
520  bool createIfNotFound );
521 
535  _jobsSubscription_t ** pRemovedSubscription );
536 
544 void _AwsIotJobs_DestroySubscription( void * pData );
545 
568  char * pTopicBuffer,
569  uint16_t operationTopicLength,
570  AwsIotMqttCallbackFunction_t callback );
571 
592  char * pTopicBuffer,
593  _jobsSubscription_t ** pRemovedSubscription );
594 
595 /*------------------------ Jobs serializer functions ------------------------*/
596 
610  const AwsIotJobsRequestInfo_t * pRequestInfo,
611  const _jsonRequestContents_t * pRequestContents,
612  _jobsOperation_t * pOperation );
613 
623 void _AwsIotJobs_ParseResponse( AwsIotStatus_t status,
624  const char * pResponse,
625  size_t responseLength,
626  _jobsOperation_t * pOperation );
627 
628 #endif /* ifndef AWS_IOT_JOBS_INTERNAL_H_ */
Internal structure representing a single Jobs operation.
Definition: aws_iot_jobs_internal.h:380
uint32_t flags
Flags passed to operation API function.
Definition: aws_iot_jobs_internal.h:386
_jobsSubscription_t * pSubscription
Jobs subscriptions object associated with this operation.
Definition: aws_iot_jobs_internal.h:390
AwsIotJobsError_t _AwsIotJobs_CreateOperation(_jobsOperationType_t type, const AwsIotJobsRequestInfo_t *pRequestInfo, const _jsonRequestContents_t *pRequestContents, uint32_t flags, const AwsIotJobsCallbackInfo_t *pCallbackInfo, _jobsOperation_t **pNewOperation)
Create a record for a new in-progress Jobs operation.
Definition: aws_iot_jobs_operation.c:524
int32_t callbackReferences
Reference counter for Jobs callbacks.
Definition: aws_iot_jobs_internal.h:358
Definition: aws_iot_jobs_internal.h:317
size_t jobIdLength
Length of _jobsOperation_t.pJobId.
Definition: aws_iot_jobs_internal.h:417
IotLink_t link
List link member.
Definition: aws_iot_jobs_internal.h:382
_jobsOperationType_t type
Operation type.
Definition: aws_iot_jobs_internal.h:385
IotListDouble_t _AwsIotJobsPendingOperations
List of active Jobs operations awaiting a response from the Jobs service.
Definition: aws_iot_jobs_operation.c:176
void _AwsIotJobs_DestroySubscription(void *pData)
Free resources used for a Jobs subscription object.
Definition: aws_iot_jobs_subscription.c:258
size_t jobsRequestLength
Length of _jobsOperation_t.pJobsRequest.
Definition: aws_iot_jobs_internal.h:394
Represents a Jobs subscriptions object.
Definition: aws_iot_jobs_internal.h:353
Definition: aws_iot_jobs_internal.h:331
AwsIotJobsError_t _AwsIotJobs_IncrementReferences(_jobsOperation_t *pOperation, char *pTopicBuffer, uint16_t operationTopicLength, AwsIotMqttCallbackFunction_t callback)
Increment the reference count of a Jobs subscriptions object.
Definition: aws_iot_jobs_subscription.c:274
AwsIotJobsError_t
Return codes of Jobs functions.
Definition: aws_iot_jobs_types.h:87
_jobsCallbackType_t
Enumerations representing each of the Jobs callback functions.
Definition: aws_iot_jobs_internal.h:328
void AwsIotJobs_FreeOperation(void *ptr)
Free a _jobsOperation_t. This function should have the same signature as free.
Definition: aws_iot_jobs_static_memory.c:123
AwsIotJobsError_t _AwsIotJobs_ProcessOperation(const AwsIotJobsRequestInfo_t *pRequestInfo, _jobsOperation_t *pOperation)
Process a Jobs operation by sending the necessary MQTT packets.
Definition: aws_iot_jobs_operation.c:747
Common information provided to Jobs requests.
Definition: aws_iot_jobs_types.h:569
AwsIotJobsCallbackInfo_t callback
User-provided callback function and parameter.
Definition: aws_iot_jobs_internal.h:414
struct _mqttConnection * IotMqttConnection_t
IotListDouble_t _AwsIotJobsSubscriptions
List of active Jobs subscriptions objects.
Definition: aws_iot_jobs_subscription.c:66
IotMutex_t _AwsIotJobsSubscriptionsMutex
Protects _AwsIotJobsSubscriptions from concurrent access.
Definition: aws_iot_jobs_subscription.c:71
_jobsOperationType_t
Enumerations representing each of the Jobs library's API functions.
Definition: aws_iot_jobs_internal.h:312
int32_t executionNumber
Execution number.
Definition: aws_iot_jobs_internal.h:343
Definition: aws_iot_jobs_internal.h:330
size_t thingNameLength
Length of Thing Name.
Definition: aws_iot_jobs_internal.h:371
IotLink_t link
List link member.
Definition: aws_iot_jobs_internal.h:355
Definition: aws_iot_jobs_internal.h:318
void _AwsIotJobs_RemoveSubscription(_jobsSubscription_t *pSubscription, _jobsSubscription_t **pRemovedSubscription)
Remove a Jobs subscription object from the subscription list if unreferenced.
Definition: aws_iot_jobs_subscription.c:166
char * pTopicBuffer
Buffer allocated for removing Jobs topics.
Definition: aws_iot_jobs_internal.h:369
size_t jobsResponseLength
Length of _jobsOperation_t.pJobsResponse.
Definition: aws_iot_jobs_internal.h:401
Parameter to _AwsIotJobs_GenerateJsonRequest.
Definition: aws_iot_jobs_internal.h:337
void AwsIotJobs_FreeSubscription(void *ptr)
Free a _jobsSubscription_t. This function should have the same signature as free. ...
Definition: aws_iot_jobs_static_memory.c:157
Information on a user-provided Jobs callback function.
Definition: aws_iot_jobs_types.h:501
Definition: aws_iot_jobs_internal.h:322
Definition: aws_iot_jobs_internal.h:315
Information on a Job update for AwsIotJobs_StartNextAsync and AwsIotJobs_UpdateAsync. These functions modify a Job's state.
Definition: aws_iot_jobs_types.h:666
size_t clientTokenLength
Length of _jobsOperation_t.pClientToken.
Definition: aws_iot_jobs_internal.h:397
AwsIotJobsError_t _AwsIotJobs_GenerateJsonRequest(_jobsOperationType_t type, const AwsIotJobsRequestInfo_t *pRequestInfo, const _jsonRequestContents_t *pRequestContents, _jobsOperation_t *pOperation)
Generates a Jobs JSON request document from an AwsIotJobsRequestInfo_t and an AwsIotJobsUpdateInfo_t...
Definition: aws_iot_jobs_serialize.c:1112
_IotSystemSemaphore_t IotSemaphore_t
void * AwsIotJobs_MallocSubscription(size_t size)
Allocate a _jobsSubscription_t. This function should have the same signature as malloc.
Definition: aws_iot_jobs_static_memory.c:135
void * AwsIotJobs_MallocOperation(size_t size)
Allocate a _jobsOperation_t. This function should have the same signature as malloc.
Definition: aws_iot_jobs_static_memory.c:100
uint32_t _AwsIotJobsMqttTimeoutMs
Timeout used for MQTT operations.
Definition: aws_iot_jobs_api.c:198
User-facing functions of the Jobs library.
const char * pClientToken
Client token sent with request.
Definition: aws_iot_jobs_internal.h:396
_jobsSubscription_t * _AwsIotJobs_FindSubscription(const char *pThingName, size_t thingNameLength, bool createIfNotFound)
Find a Jobs subscription object. May create a new subscription object and adds it to the subscription...
Definition: aws_iot_jobs_subscription.c:102
_IotSystemMutex_t IotMutex_t
Definition: aws_iot_jobs_internal.h:321
const char * pJobsResponse
Response received from the Jobs service.
Definition: aws_iot_jobs_internal.h:400
bool includeJobDocument
Whether the response should include the Job document.
Definition: aws_iot_jobs_internal.h:344
IotSemaphore_t waitSemaphore
Semaphore to be used with AwsIotJobs_Wait.
Definition: aws_iot_jobs_internal.h:413
Definition: aws_iot_jobs_internal.h:316
#define JOBS_OPERATION_COUNT
The number of currently available Jobs operations.
Definition: aws_iot_jobs_internal.h:196
const char * pJobsRequest
JSON document to send to the Jobs service.
Definition: aws_iot_jobs_internal.h:393
AwsIotJobsError_t status
Status of operation.
Definition: aws_iot_jobs_internal.h:387
IotMutex_t _AwsIotJobsPendingOperationsMutex
Protects _AwsIotJobsPendingOperations from concurrent access.
Definition: aws_iot_jobs_operation.c:181
AwsIotJobsError_t _AwsIotJobs_GenerateJobsTopic(_jobsOperationType_t type, const AwsIotJobsRequestInfo_t *pRequestInfo, char **pTopicBuffer, uint16_t *pOperationTopicLength)
Fill a buffer with a Jobs topic.
Definition: aws_iot_jobs_operation.c:666
void _AwsIotJobs_DecrementReferences(_jobsOperation_t *pOperation, char *pTopicBuffer, _jobsSubscription_t **pRemovedSubscription)
Decrement the reference count of a Jobs subscriptions object.
Definition: aws_iot_jobs_subscription.c:364
void _AwsIotJobs_ParseResponse(AwsIotStatus_t status, const char *pResponse, size_t responseLength, _jobsOperation_t *pOperation)
Parse a response received from the Jobs service.
Definition: aws_iot_jobs_serialize.c:1154
const AwsIotJobsUpdateInfo_t * pUpdateInfo
Valid for JOBS_START_NEXT and JOBS_UPDATE.
Definition: aws_iot_jobs_internal.h:339
void _AwsIotJobs_DestroyOperation(void *pData)
Free resources used to record a Jobs operation. This is called when the operation completes...
Definition: aws_iot_jobs_operation.c:636
IotMqttConnection_t mqttConnection
MQTT connection associated with this operation.
Definition: aws_iot_jobs_internal.h:389
#define JOBS_CALLBACK_COUNT
The number of currently available Jobs callbacks.
Definition: aws_iot_jobs_internal.h:204