Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
examples/jobs_sample/jobs_sample.c@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | /* |
JMF | 0:082731ede69f | 2 | * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
JMF | 0:082731ede69f | 3 | * |
JMF | 0:082731ede69f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
JMF | 0:082731ede69f | 5 | * You may not use this file except in compliance with the License. |
JMF | 0:082731ede69f | 6 | * A copy of the License is located at |
JMF | 0:082731ede69f | 7 | * |
JMF | 0:082731ede69f | 8 | * http://aws.amazon.com/apache2.0 |
JMF | 0:082731ede69f | 9 | * |
JMF | 0:082731ede69f | 10 | * or in the "license" file accompanying this file. This file is distributed |
JMF | 0:082731ede69f | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
JMF | 0:082731ede69f | 12 | * express or implied. See the License for the specific language governing |
JMF | 0:082731ede69f | 13 | * permissions and limitations under the License. |
JMF | 0:082731ede69f | 14 | */ |
JMF | 0:082731ede69f | 15 | |
JMF | 0:082731ede69f | 16 | /** |
JMF | 0:082731ede69f | 17 | * |
JMF | 0:082731ede69f | 18 | * This example takes the parameters from the aws_iot_config.h file and establishes |
JMF | 0:082731ede69f | 19 | * a connection to the AWS IoT MQTT Platform. It performs several operations to |
JMF | 0:082731ede69f | 20 | * demonstrate the basic capabilities of the AWS IoT Jobs platform. |
JMF | 0:082731ede69f | 21 | * |
JMF | 0:082731ede69f | 22 | * If all the certs are correct, you should see the list of pending Job Executions |
JMF | 0:082731ede69f | 23 | * printed out by the iot_get_pending_callback_handler. If there are any existing pending |
JMF | 0:082731ede69f | 24 | * job executions each will be processed one at a time in the iot_next_job_callback_handler. |
JMF | 0:082731ede69f | 25 | * After all of the pending jobs have been processed the program will wait for |
JMF | 0:082731ede69f | 26 | * notifications for new pending jobs and process them one at a time as they come in. |
JMF | 0:082731ede69f | 27 | * |
JMF | 0:082731ede69f | 28 | * In the main body you can see how each callback is registered for each corresponding |
JMF | 0:082731ede69f | 29 | * Jobs topic. |
JMF | 0:082731ede69f | 30 | * |
JMF | 0:082731ede69f | 31 | */ |
JMF | 0:082731ede69f | 32 | #include <stdio.h> |
JMF | 0:082731ede69f | 33 | #include <stdlib.h> |
JMF | 0:082731ede69f | 34 | #include <ctype.h> |
JMF | 0:082731ede69f | 35 | #include <unistd.h> |
JMF | 0:082731ede69f | 36 | #include <limits.h> |
JMF | 0:082731ede69f | 37 | #include <string.h> |
JMF | 0:082731ede69f | 38 | |
JMF | 0:082731ede69f | 39 | #include "aws_iot_config.h" |
JMF | 0:082731ede69f | 40 | #include "aws_iot_json_utils.h" |
JMF | 0:082731ede69f | 41 | #include "aws_iot_log.h" |
JMF | 0:082731ede69f | 42 | #include "aws_iot_version.h" |
JMF | 0:082731ede69f | 43 | #include "aws_iot_mqtt_client_interface.h" |
JMF | 0:082731ede69f | 44 | #include "aws_iot_jobs_interface.h" |
JMF | 0:082731ede69f | 45 | |
JMF | 0:082731ede69f | 46 | /** |
JMF | 0:082731ede69f | 47 | * @brief Default cert location |
JMF | 0:082731ede69f | 48 | */ |
JMF | 0:082731ede69f | 49 | char certDirectory[PATH_MAX + 1] = "../../../certs"; |
JMF | 0:082731ede69f | 50 | |
JMF | 0:082731ede69f | 51 | /** |
JMF | 0:082731ede69f | 52 | * @brief Default MQTT HOST URL is pulled from the aws_iot_config.h |
JMF | 0:082731ede69f | 53 | */ |
JMF | 0:082731ede69f | 54 | char HostAddress[255] = AWS_IOT_MQTT_HOST; |
JMF | 0:082731ede69f | 55 | |
JMF | 0:082731ede69f | 56 | /** |
JMF | 0:082731ede69f | 57 | * @brief Default MQTT port is pulled from the aws_iot_config.h |
JMF | 0:082731ede69f | 58 | */ |
JMF | 0:082731ede69f | 59 | uint32_t port = AWS_IOT_MQTT_PORT; |
JMF | 0:082731ede69f | 60 | |
JMF | 0:082731ede69f | 61 | static jsmn_parser jsonParser; |
JMF | 0:082731ede69f | 62 | static jsmntok_t jsonTokenStruct[MAX_JSON_TOKEN_EXPECTED]; |
JMF | 0:082731ede69f | 63 | static int32_t tokenCount; |
JMF | 0:082731ede69f | 64 | |
JMF | 0:082731ede69f | 65 | void iot_get_pending_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, |
JMF | 0:082731ede69f | 66 | IoT_Publish_Message_Params *params, void *pData) { |
JMF | 0:082731ede69f | 67 | IOT_UNUSED(pData); |
JMF | 0:082731ede69f | 68 | IOT_UNUSED(pClient); |
JMF | 0:082731ede69f | 69 | IOT_INFO("\nJOB_GET_PENDING_TOPIC callback"); |
JMF | 0:082731ede69f | 70 | IOT_INFO("topic: %.*s", topicNameLen, topicName); |
JMF | 0:082731ede69f | 71 | IOT_INFO("payload: %.*s", (int) params->payloadLen, (char *)params->payload); |
JMF | 0:082731ede69f | 72 | |
JMF | 0:082731ede69f | 73 | jsmn_init(&jsonParser); |
JMF | 0:082731ede69f | 74 | |
JMF | 0:082731ede69f | 75 | tokenCount = jsmn_parse(&jsonParser, params->payload, (int) params->payloadLen, jsonTokenStruct, MAX_JSON_TOKEN_EXPECTED); |
JMF | 0:082731ede69f | 76 | |
JMF | 0:082731ede69f | 77 | if(tokenCount < 0) { |
JMF | 0:082731ede69f | 78 | IOT_WARN("Failed to parse JSON: %d", tokenCount); |
JMF | 0:082731ede69f | 79 | return; |
JMF | 0:082731ede69f | 80 | } |
JMF | 0:082731ede69f | 81 | |
JMF | 0:082731ede69f | 82 | /* Assume the top-level element is an object */ |
JMF | 0:082731ede69f | 83 | if(tokenCount < 1 || jsonTokenStruct[0].type != JSMN_OBJECT) { |
JMF | 0:082731ede69f | 84 | IOT_WARN("Top Level is not an object"); |
JMF | 0:082731ede69f | 85 | return; |
JMF | 0:082731ede69f | 86 | } |
JMF | 0:082731ede69f | 87 | |
JMF | 0:082731ede69f | 88 | jsmntok_t *jobs; |
JMF | 0:082731ede69f | 89 | |
JMF | 0:082731ede69f | 90 | jobs = findToken("inProgressJobs", params->payload, jsonTokenStruct); |
JMF | 0:082731ede69f | 91 | |
JMF | 0:082731ede69f | 92 | if (jobs) { |
JMF | 0:082731ede69f | 93 | IOT_INFO("inProgressJobs: %.*s", jobs->end - jobs->start, (char *)params->payload + jobs->start); |
JMF | 0:082731ede69f | 94 | } |
JMF | 0:082731ede69f | 95 | |
JMF | 0:082731ede69f | 96 | jobs = findToken("queuedJobs", params->payload, jsonTokenStruct); |
JMF | 0:082731ede69f | 97 | |
JMF | 0:082731ede69f | 98 | if (jobs) { |
JMF | 0:082731ede69f | 99 | IOT_INFO("queuedJobs: %.*s", jobs->end - jobs->start, (char *)params->payload + jobs->start); |
JMF | 0:082731ede69f | 100 | } |
JMF | 0:082731ede69f | 101 | } |
JMF | 0:082731ede69f | 102 | |
JMF | 0:082731ede69f | 103 | void iot_next_job_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, |
JMF | 0:082731ede69f | 104 | IoT_Publish_Message_Params *params, void *pData) { |
JMF | 0:082731ede69f | 105 | char topicToPublishUpdate[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 106 | char messageBuffer[200]; |
JMF | 0:082731ede69f | 107 | |
JMF | 0:082731ede69f | 108 | IOT_UNUSED(pData); |
JMF | 0:082731ede69f | 109 | IOT_UNUSED(pClient); |
JMF | 0:082731ede69f | 110 | IOT_INFO("\nJOB_NOTIFY_NEXT_TOPIC / JOB_DESCRIBE_TOPIC($next) callback"); |
JMF | 0:082731ede69f | 111 | IOT_INFO("topic: %.*s", topicNameLen, topicName); |
JMF | 0:082731ede69f | 112 | IOT_INFO("payload: %.*s", (int) params->payloadLen, (char *)params->payload); |
JMF | 0:082731ede69f | 113 | |
JMF | 0:082731ede69f | 114 | jsmn_init(&jsonParser); |
JMF | 0:082731ede69f | 115 | |
JMF | 0:082731ede69f | 116 | tokenCount = jsmn_parse(&jsonParser, params->payload, (int) params->payloadLen, jsonTokenStruct, MAX_JSON_TOKEN_EXPECTED); |
JMF | 0:082731ede69f | 117 | |
JMF | 0:082731ede69f | 118 | if(tokenCount < 0) { |
JMF | 0:082731ede69f | 119 | IOT_WARN("Failed to parse JSON: %d", tokenCount); |
JMF | 0:082731ede69f | 120 | return; |
JMF | 0:082731ede69f | 121 | } |
JMF | 0:082731ede69f | 122 | |
JMF | 0:082731ede69f | 123 | /* Assume the top-level element is an object */ |
JMF | 0:082731ede69f | 124 | if(tokenCount < 1 || jsonTokenStruct[0].type != JSMN_OBJECT) { |
JMF | 0:082731ede69f | 125 | IOT_WARN("Top Level is not an object"); |
JMF | 0:082731ede69f | 126 | return; |
JMF | 0:082731ede69f | 127 | } |
JMF | 0:082731ede69f | 128 | |
JMF | 0:082731ede69f | 129 | jsmntok_t *tokExecution; |
JMF | 0:082731ede69f | 130 | |
JMF | 0:082731ede69f | 131 | tokExecution = findToken("execution", params->payload, jsonTokenStruct); |
JMF | 0:082731ede69f | 132 | |
JMF | 0:082731ede69f | 133 | if (tokExecution) { |
JMF | 0:082731ede69f | 134 | IOT_INFO("execution: %.*s", tokExecution->end - tokExecution->start, (char *)params->payload + tokExecution->start); |
JMF | 0:082731ede69f | 135 | |
JMF | 0:082731ede69f | 136 | jsmntok_t *tok; |
JMF | 0:082731ede69f | 137 | |
JMF | 0:082731ede69f | 138 | tok = findToken("jobId", params->payload, tokExecution); |
JMF | 0:082731ede69f | 139 | |
JMF | 0:082731ede69f | 140 | if (tok) { |
JMF | 0:082731ede69f | 141 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 142 | char jobId[MAX_SIZE_OF_JOB_ID + 1]; |
JMF | 0:082731ede69f | 143 | AwsIotJobExecutionUpdateRequest updateRequest; |
JMF | 0:082731ede69f | 144 | |
JMF | 0:082731ede69f | 145 | rc = parseStringValue(jobId, MAX_SIZE_OF_JOB_ID + 1, params->payload, tok); |
JMF | 0:082731ede69f | 146 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 147 | IOT_ERROR("parseStringValue returned error : %d ", rc); |
JMF | 0:082731ede69f | 148 | return; |
JMF | 0:082731ede69f | 149 | } |
JMF | 0:082731ede69f | 150 | |
JMF | 0:082731ede69f | 151 | IOT_INFO("jobId: %s", jobId); |
JMF | 0:082731ede69f | 152 | |
JMF | 0:082731ede69f | 153 | tok = findToken("jobDocument", params->payload, tokExecution); |
JMF | 0:082731ede69f | 154 | |
JMF | 0:082731ede69f | 155 | /* |
JMF | 0:082731ede69f | 156 | * Do your job processing here. |
JMF | 0:082731ede69f | 157 | */ |
JMF | 0:082731ede69f | 158 | |
JMF | 0:082731ede69f | 159 | if (tok) { |
JMF | 0:082731ede69f | 160 | IOT_INFO("jobDocument: %.*s", tok->end - tok->start, (char *)params->payload + tok->start); |
JMF | 0:082731ede69f | 161 | /* Alternatively if the job still has more steps the status can be set to JOB_EXECUTION_IN_PROGRESS instead */ |
JMF | 0:082731ede69f | 162 | updateRequest.status = JOB_EXECUTION_SUCCEEDED; |
JMF | 0:082731ede69f | 163 | updateRequest.statusDetails = "{\"exampleDetail\":\"a value appropriate for your successful job\"}"; |
JMF | 0:082731ede69f | 164 | } else { |
JMF | 0:082731ede69f | 165 | updateRequest.status = JOB_EXECUTION_FAILED; |
JMF | 0:082731ede69f | 166 | updateRequest.statusDetails = "{\"failureDetail\":\"Unable to process job document\"}"; |
JMF | 0:082731ede69f | 167 | } |
JMF | 0:082731ede69f | 168 | |
JMF | 0:082731ede69f | 169 | updateRequest.expectedVersion = 0; |
JMF | 0:082731ede69f | 170 | updateRequest.executionNumber = 0; |
JMF | 0:082731ede69f | 171 | updateRequest.includeJobExecutionState = false; |
JMF | 0:082731ede69f | 172 | updateRequest.includeJobDocument = false; |
JMF | 0:082731ede69f | 173 | updateRequest.clientToken = NULL; |
JMF | 0:082731ede69f | 174 | |
JMF | 0:082731ede69f | 175 | rc = aws_iot_jobs_send_update(pClient, QOS0, AWS_IOT_MY_THING_NAME, jobId, &updateRequest, |
JMF | 0:082731ede69f | 176 | topicToPublishUpdate, sizeof(topicToPublishUpdate), messageBuffer, sizeof(messageBuffer)); |
JMF | 0:082731ede69f | 177 | } |
JMF | 0:082731ede69f | 178 | } else { |
JMF | 0:082731ede69f | 179 | IOT_INFO("execution property not found, nothing to do"); |
JMF | 0:082731ede69f | 180 | } |
JMF | 0:082731ede69f | 181 | } |
JMF | 0:082731ede69f | 182 | |
JMF | 0:082731ede69f | 183 | void iot_update_accepted_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, |
JMF | 0:082731ede69f | 184 | IoT_Publish_Message_Params *params, void *pData) { |
JMF | 0:082731ede69f | 185 | IOT_UNUSED(pData); |
JMF | 0:082731ede69f | 186 | IOT_UNUSED(pClient); |
JMF | 0:082731ede69f | 187 | IOT_INFO("\nJOB_UPDATE_TOPIC / accepted callback"); |
JMF | 0:082731ede69f | 188 | IOT_INFO("topic: %.*s", topicNameLen, topicName); |
JMF | 0:082731ede69f | 189 | IOT_INFO("payload: %.*s", (int) params->payloadLen, (char *)params->payload); |
JMF | 0:082731ede69f | 190 | } |
JMF | 0:082731ede69f | 191 | |
JMF | 0:082731ede69f | 192 | void iot_update_rejected_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, |
JMF | 0:082731ede69f | 193 | IoT_Publish_Message_Params *params, void *pData) { |
JMF | 0:082731ede69f | 194 | IOT_UNUSED(pData); |
JMF | 0:082731ede69f | 195 | IOT_UNUSED(pClient); |
JMF | 0:082731ede69f | 196 | IOT_INFO("\nJOB_UPDATE_TOPIC / rejected callback"); |
JMF | 0:082731ede69f | 197 | IOT_INFO("topic: %.*s", topicNameLen, topicName); |
JMF | 0:082731ede69f | 198 | IOT_INFO("payload: %.*s", (int) params->payloadLen, (char *)params->payload); |
JMF | 0:082731ede69f | 199 | |
JMF | 0:082731ede69f | 200 | /* Do error handling here for when the update was rejected */ |
JMF | 0:082731ede69f | 201 | } |
JMF | 0:082731ede69f | 202 | |
JMF | 0:082731ede69f | 203 | void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) { |
JMF | 0:082731ede69f | 204 | IOT_WARN("MQTT Disconnect"); |
JMF | 0:082731ede69f | 205 | IoT_Error_t rc = FAILURE; |
JMF | 0:082731ede69f | 206 | |
JMF | 0:082731ede69f | 207 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 208 | return; |
JMF | 0:082731ede69f | 209 | } |
JMF | 0:082731ede69f | 210 | |
JMF | 0:082731ede69f | 211 | IOT_UNUSED(data); |
JMF | 0:082731ede69f | 212 | |
JMF | 0:082731ede69f | 213 | if(aws_iot_is_autoreconnect_enabled(pClient)) { |
JMF | 0:082731ede69f | 214 | IOT_INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
JMF | 0:082731ede69f | 215 | } else { |
JMF | 0:082731ede69f | 216 | IOT_WARN("Auto Reconnect not enabled. Starting manual reconnect..."); |
JMF | 0:082731ede69f | 217 | rc = aws_iot_mqtt_attempt_reconnect(pClient); |
JMF | 0:082731ede69f | 218 | if(NETWORK_RECONNECTED == rc) { |
JMF | 0:082731ede69f | 219 | IOT_WARN("Manual Reconnect Successful"); |
JMF | 0:082731ede69f | 220 | } else { |
JMF | 0:082731ede69f | 221 | IOT_WARN("Manual Reconnect Failed - %d", rc); |
JMF | 0:082731ede69f | 222 | } |
JMF | 0:082731ede69f | 223 | } |
JMF | 0:082731ede69f | 224 | } |
JMF | 0:082731ede69f | 225 | |
JMF | 0:082731ede69f | 226 | int main(int argc, char **argv) { |
JMF | 0:082731ede69f | 227 | char rootCA[PATH_MAX + 1]; |
JMF | 0:082731ede69f | 228 | char clientCRT[PATH_MAX + 1]; |
JMF | 0:082731ede69f | 229 | char clientKey[PATH_MAX + 1]; |
JMF | 0:082731ede69f | 230 | char CurrentWD[PATH_MAX + 1]; |
JMF | 0:082731ede69f | 231 | char cPayload[100]; |
JMF | 0:082731ede69f | 232 | |
JMF | 0:082731ede69f | 233 | int32_t i = 0; |
JMF | 0:082731ede69f | 234 | |
JMF | 0:082731ede69f | 235 | IoT_Error_t rc = FAILURE; |
JMF | 0:082731ede69f | 236 | |
JMF | 0:082731ede69f | 237 | AWS_IoT_Client client; |
JMF | 0:082731ede69f | 238 | IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault; |
JMF | 0:082731ede69f | 239 | IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault; |
JMF | 0:082731ede69f | 240 | |
JMF | 0:082731ede69f | 241 | IoT_Publish_Message_Params paramsQOS0; |
JMF | 0:082731ede69f | 242 | |
JMF | 0:082731ede69f | 243 | getcwd(CurrentWD, sizeof(CurrentWD)); |
JMF | 0:082731ede69f | 244 | snprintf(rootCA, PATH_MAX + 1, "%s/%s/%s", CurrentWD, certDirectory, AWS_IOT_ROOT_CA_FILENAME); |
JMF | 0:082731ede69f | 245 | snprintf(clientCRT, PATH_MAX + 1, "%s/%s/%s", CurrentWD, certDirectory, AWS_IOT_CERTIFICATE_FILENAME); |
JMF | 0:082731ede69f | 246 | snprintf(clientKey, PATH_MAX + 1, "%s/%s/%s", CurrentWD, certDirectory, AWS_IOT_PRIVATE_KEY_FILENAME); |
JMF | 0:082731ede69f | 247 | |
JMF | 0:082731ede69f | 248 | IOT_DEBUG("rootCA %s", rootCA); |
JMF | 0:082731ede69f | 249 | IOT_DEBUG("clientCRT %s", clientCRT); |
JMF | 0:082731ede69f | 250 | IOT_DEBUG("clientKey %s", clientKey); |
JMF | 0:082731ede69f | 251 | |
JMF | 0:082731ede69f | 252 | mqttInitParams.enableAutoReconnect = false; // We enable this later below |
JMF | 0:082731ede69f | 253 | mqttInitParams.pHostURL = HostAddress; |
JMF | 0:082731ede69f | 254 | mqttInitParams.port = port; |
JMF | 0:082731ede69f | 255 | mqttInitParams.pRootCALocation = rootCA; |
JMF | 0:082731ede69f | 256 | mqttInitParams.pDeviceCertLocation = clientCRT; |
JMF | 0:082731ede69f | 257 | mqttInitParams.pDevicePrivateKeyLocation = clientKey; |
JMF | 0:082731ede69f | 258 | mqttInitParams.mqttCommandTimeout_ms = 20000; |
JMF | 0:082731ede69f | 259 | mqttInitParams.tlsHandshakeTimeout_ms = 5000; |
JMF | 0:082731ede69f | 260 | mqttInitParams.isSSLHostnameVerify = true; |
JMF | 0:082731ede69f | 261 | mqttInitParams.disconnectHandler = disconnectCallbackHandler; |
JMF | 0:082731ede69f | 262 | mqttInitParams.disconnectHandlerData = NULL; |
JMF | 0:082731ede69f | 263 | |
JMF | 0:082731ede69f | 264 | rc = aws_iot_mqtt_init(&client, &mqttInitParams); |
JMF | 0:082731ede69f | 265 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 266 | IOT_ERROR("aws_iot_mqtt_init returned error : %d ", rc); |
JMF | 0:082731ede69f | 267 | return rc; |
JMF | 0:082731ede69f | 268 | } |
JMF | 0:082731ede69f | 269 | |
JMF | 0:082731ede69f | 270 | connectParams.keepAliveIntervalInSec = 600; |
JMF | 0:082731ede69f | 271 | connectParams.isCleanSession = true; |
JMF | 0:082731ede69f | 272 | connectParams.MQTTVersion = MQTT_3_1_1; |
JMF | 0:082731ede69f | 273 | connectParams.pClientID = AWS_IOT_MQTT_CLIENT_ID; |
JMF | 0:082731ede69f | 274 | connectParams.clientIDLen = (uint16_t) strlen(AWS_IOT_MQTT_CLIENT_ID); |
JMF | 0:082731ede69f | 275 | connectParams.isWillMsgPresent = false; |
JMF | 0:082731ede69f | 276 | |
JMF | 0:082731ede69f | 277 | IOT_INFO("Connecting..."); |
JMF | 0:082731ede69f | 278 | rc = aws_iot_mqtt_connect(&client, &connectParams); |
JMF | 0:082731ede69f | 279 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 280 | IOT_ERROR("Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL, mqttInitParams.port); |
JMF | 0:082731ede69f | 281 | return rc; |
JMF | 0:082731ede69f | 282 | } |
JMF | 0:082731ede69f | 283 | /* |
JMF | 0:082731ede69f | 284 | * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
JMF | 0:082731ede69f | 285 | * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
JMF | 0:082731ede69f | 286 | * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
JMF | 0:082731ede69f | 287 | */ |
JMF | 0:082731ede69f | 288 | rc = aws_iot_mqtt_autoreconnect_set_status(&client, true); |
JMF | 0:082731ede69f | 289 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 290 | IOT_ERROR("Unable to set Auto Reconnect to true - %d", rc); |
JMF | 0:082731ede69f | 291 | return rc; |
JMF | 0:082731ede69f | 292 | } |
JMF | 0:082731ede69f | 293 | |
JMF | 0:082731ede69f | 294 | char topicToSubscribeGetPending[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 295 | char topicToSubscribeNotifyNext[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 296 | char topicToSubscribeGetNext[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 297 | char topicToSubscribeUpdateAccepted[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 298 | char topicToSubscribeUpdateRejected[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 299 | |
JMF | 0:082731ede69f | 300 | char topicToPublishGetPending[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 301 | char topicToPublishGetNext[MAX_JOB_TOPIC_LENGTH_BYTES]; |
JMF | 0:082731ede69f | 302 | |
JMF | 0:082731ede69f | 303 | rc = aws_iot_jobs_subscribe_to_job_messages( |
JMF | 0:082731ede69f | 304 | &client, QOS0, AWS_IOT_MY_THING_NAME, NULL, JOB_GET_PENDING_TOPIC, JOB_WILDCARD_REPLY_TYPE, |
JMF | 0:082731ede69f | 305 | iot_get_pending_callback_handler, NULL, topicToSubscribeGetPending, sizeof(topicToSubscribeGetPending)); |
JMF | 0:082731ede69f | 306 | |
JMF | 0:082731ede69f | 307 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 308 | IOT_ERROR("Error subscribing JOB_GET_PENDING_TOPIC: %d ", rc); |
JMF | 0:082731ede69f | 309 | return rc; |
JMF | 0:082731ede69f | 310 | } |
JMF | 0:082731ede69f | 311 | |
JMF | 0:082731ede69f | 312 | rc = aws_iot_jobs_subscribe_to_job_messages( |
JMF | 0:082731ede69f | 313 | &client, QOS0, AWS_IOT_MY_THING_NAME, NULL, JOB_NOTIFY_NEXT_TOPIC, JOB_REQUEST_TYPE, |
JMF | 0:082731ede69f | 314 | iot_next_job_callback_handler, NULL, topicToSubscribeNotifyNext, sizeof(topicToSubscribeNotifyNext)); |
JMF | 0:082731ede69f | 315 | |
JMF | 0:082731ede69f | 316 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 317 | IOT_ERROR("Error subscribing JOB_NOTIFY_NEXT_TOPIC: %d ", rc); |
JMF | 0:082731ede69f | 318 | return rc; |
JMF | 0:082731ede69f | 319 | } |
JMF | 0:082731ede69f | 320 | |
JMF | 0:082731ede69f | 321 | rc = aws_iot_jobs_subscribe_to_job_messages( |
JMF | 0:082731ede69f | 322 | &client, QOS0, AWS_IOT_MY_THING_NAME, JOB_ID_NEXT, JOB_DESCRIBE_TOPIC, JOB_WILDCARD_REPLY_TYPE, |
JMF | 0:082731ede69f | 323 | iot_next_job_callback_handler, NULL, topicToSubscribeGetNext, sizeof(topicToSubscribeGetNext)); |
JMF | 0:082731ede69f | 324 | |
JMF | 0:082731ede69f | 325 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 326 | IOT_ERROR("Error subscribing JOB_DESCRIBE_TOPIC ($next): %d ", rc); |
JMF | 0:082731ede69f | 327 | return rc; |
JMF | 0:082731ede69f | 328 | } |
JMF | 0:082731ede69f | 329 | |
JMF | 0:082731ede69f | 330 | rc = aws_iot_jobs_subscribe_to_job_messages( |
JMF | 0:082731ede69f | 331 | &client, QOS0, AWS_IOT_MY_THING_NAME, JOB_ID_WILDCARD, JOB_UPDATE_TOPIC, JOB_ACCEPTED_REPLY_TYPE, |
JMF | 0:082731ede69f | 332 | iot_update_accepted_callback_handler, NULL, topicToSubscribeUpdateAccepted, sizeof(topicToSubscribeUpdateAccepted)); |
JMF | 0:082731ede69f | 333 | |
JMF | 0:082731ede69f | 334 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 335 | IOT_ERROR("Error subscribing JOB_UPDATE_TOPIC/accepted: %d ", rc); |
JMF | 0:082731ede69f | 336 | return rc; |
JMF | 0:082731ede69f | 337 | } |
JMF | 0:082731ede69f | 338 | |
JMF | 0:082731ede69f | 339 | rc = aws_iot_jobs_subscribe_to_job_messages( |
JMF | 0:082731ede69f | 340 | &client, QOS0, AWS_IOT_MY_THING_NAME, JOB_ID_WILDCARD, JOB_UPDATE_TOPIC, JOB_REJECTED_REPLY_TYPE, |
JMF | 0:082731ede69f | 341 | iot_update_rejected_callback_handler, NULL, topicToSubscribeUpdateRejected, sizeof(topicToSubscribeUpdateRejected)); |
JMF | 0:082731ede69f | 342 | |
JMF | 0:082731ede69f | 343 | if(SUCCESS != rc) { |
JMF | 0:082731ede69f | 344 | IOT_ERROR("Error subscribing JOB_UPDATE_TOPIC/rejected: %d ", rc); |
JMF | 0:082731ede69f | 345 | return rc; |
JMF | 0:082731ede69f | 346 | } |
JMF | 0:082731ede69f | 347 | |
JMF | 0:082731ede69f | 348 | paramsQOS0.qos = QOS0; |
JMF | 0:082731ede69f | 349 | paramsQOS0.payload = (void *) cPayload; |
JMF | 0:082731ede69f | 350 | paramsQOS0.isRetained = 0; |
JMF | 0:082731ede69f | 351 | paramsQOS0.payloadLen = strlen(cPayload); |
JMF | 0:082731ede69f | 352 | |
JMF | 0:082731ede69f | 353 | rc = aws_iot_jobs_send_query(&client, QOS0, AWS_IOT_MY_THING_NAME, NULL, NULL, topicToPublishGetPending, sizeof(topicToPublishGetPending), NULL, 0, JOB_GET_PENDING_TOPIC); |
JMF | 0:082731ede69f | 354 | |
JMF | 0:082731ede69f | 355 | AwsIotDescribeJobExecutionRequest describeRequest; |
JMF | 0:082731ede69f | 356 | describeRequest.executionNumber = 0; |
JMF | 0:082731ede69f | 357 | describeRequest.includeJobDocument = true; |
JMF | 0:082731ede69f | 358 | describeRequest.clientToken = NULL; |
JMF | 0:082731ede69f | 359 | |
JMF | 0:082731ede69f | 360 | rc = aws_iot_jobs_describe(&client, QOS0, AWS_IOT_MY_THING_NAME, JOB_ID_NEXT, &describeRequest, topicToPublishGetNext, sizeof(topicToPublishGetNext), NULL, 0); |
JMF | 0:082731ede69f | 361 | |
JMF | 0:082731ede69f | 362 | while(SUCCESS == rc) { |
JMF | 0:082731ede69f | 363 | //Max time the yield function will wait for read messages |
JMF | 0:082731ede69f | 364 | rc = aws_iot_mqtt_yield(&client, 50000); |
JMF | 0:082731ede69f | 365 | } |
JMF | 0:082731ede69f | 366 | |
JMF | 0:082731ede69f | 367 | return rc; |
JMF | 0:082731ede69f | 368 | } |