Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
src/aws_iot_jobs_interface.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 2015-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 | #include "aws_iot_jobs_interface.h" |
JMF | 0:082731ede69f | 17 | #include "aws_iot_log.h" |
JMF | 0:082731ede69f | 18 | #include "aws_iot_jobs_json.h" |
JMF | 0:082731ede69f | 19 | #include <string.h> |
JMF | 0:082731ede69f | 20 | |
JMF | 0:082731ede69f | 21 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 22 | extern "C" { |
JMF | 0:082731ede69f | 23 | #endif |
JMF | 0:082731ede69f | 24 | |
JMF | 0:082731ede69f | 25 | #define CHECK_GENERATE_STRING_RESULT(result, bufferSize) \ |
JMF | 0:082731ede69f | 26 | if (result < 0) { \ |
JMF | 0:082731ede69f | 27 | return FAILURE; \ |
JMF | 0:082731ede69f | 28 | } else if ((unsigned) result >= bufferSize) { \ |
JMF | 0:082731ede69f | 29 | return LIMIT_EXCEEDED_ERROR; \ |
JMF | 0:082731ede69f | 30 | } |
JMF | 0:082731ede69f | 31 | |
JMF | 0:082731ede69f | 32 | |
JMF | 0:082731ede69f | 33 | IoT_Error_t aws_iot_jobs_subscribe_to_job_messages( |
JMF | 0:082731ede69f | 34 | AWS_IoT_Client *pClient, QoS qos, |
JMF | 0:082731ede69f | 35 | const char *thingName, |
JMF | 0:082731ede69f | 36 | const char *jobId, |
JMF | 0:082731ede69f | 37 | AwsIotJobExecutionTopicType topicType, |
JMF | 0:082731ede69f | 38 | AwsIotJobExecutionTopicReplyType replyType, |
JMF | 0:082731ede69f | 39 | pApplicationHandler_t pApplicationHandler, |
JMF | 0:082731ede69f | 40 | void *pApplicationHandlerData, |
JMF | 0:082731ede69f | 41 | char *topicBuffer, |
JMF | 0:082731ede69f | 42 | uint16_t topicBufferSize) |
JMF | 0:082731ede69f | 43 | { |
JMF | 0:082731ede69f | 44 | int requiredSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, topicType, replyType, thingName, jobId); |
JMF | 0:082731ede69f | 45 | CHECK_GENERATE_STRING_RESULT(requiredSize, topicBufferSize); |
JMF | 0:082731ede69f | 46 | |
JMF | 0:082731ede69f | 47 | return aws_iot_mqtt_subscribe(pClient, topicBuffer, (uint16_t)strlen(topicBuffer), qos, pApplicationHandler, pApplicationHandlerData); |
JMF | 0:082731ede69f | 48 | } |
JMF | 0:082731ede69f | 49 | |
JMF | 0:082731ede69f | 50 | IoT_Error_t aws_iot_jobs_subscribe_to_all_job_messages( |
JMF | 0:082731ede69f | 51 | AWS_IoT_Client *pClient, QoS qos, |
JMF | 0:082731ede69f | 52 | const char *thingName, |
JMF | 0:082731ede69f | 53 | pApplicationHandler_t pApplicationHandler, |
JMF | 0:082731ede69f | 54 | void *pApplicationHandlerData, |
JMF | 0:082731ede69f | 55 | char *topicBuffer, |
JMF | 0:082731ede69f | 56 | uint16_t topicBufferSize) |
JMF | 0:082731ede69f | 57 | { |
JMF | 0:082731ede69f | 58 | return aws_iot_jobs_subscribe_to_job_messages(pClient, qos, thingName, NULL, JOB_WILDCARD_TOPIC, JOB_WILDCARD_REPLY_TYPE, |
JMF | 0:082731ede69f | 59 | pApplicationHandler, pApplicationHandlerData, topicBuffer, topicBufferSize); |
JMF | 0:082731ede69f | 60 | } |
JMF | 0:082731ede69f | 61 | |
JMF | 0:082731ede69f | 62 | IoT_Error_t aws_iot_jobs_unsubscribe_from_job_messages( |
JMF | 0:082731ede69f | 63 | AWS_IoT_Client *pClient, |
JMF | 0:082731ede69f | 64 | char *topicBuffer) |
JMF | 0:082731ede69f | 65 | { |
JMF | 0:082731ede69f | 66 | return aws_iot_mqtt_unsubscribe(pClient, topicBuffer, (uint16_t)strlen(topicBuffer)); |
JMF | 0:082731ede69f | 67 | } |
JMF | 0:082731ede69f | 68 | |
JMF | 0:082731ede69f | 69 | IoT_Error_t aws_iot_jobs_send_query( |
JMF | 0:082731ede69f | 70 | AWS_IoT_Client *pClient, QoS qos, |
JMF | 0:082731ede69f | 71 | const char *thingName, |
JMF | 0:082731ede69f | 72 | const char *jobId, |
JMF | 0:082731ede69f | 73 | const char *clientToken, |
JMF | 0:082731ede69f | 74 | char *topicBuffer, |
JMF | 0:082731ede69f | 75 | uint16_t topicBufferSize, |
JMF | 0:082731ede69f | 76 | char *messageBuffer, |
JMF | 0:082731ede69f | 77 | size_t messageBufferSize, |
JMF | 0:082731ede69f | 78 | AwsIotJobExecutionTopicType topicType) |
JMF | 0:082731ede69f | 79 | { |
JMF | 0:082731ede69f | 80 | if (thingName == NULL || topicBuffer == NULL || (clientToken != NULL && messageBuffer == NULL)) { |
JMF | 0:082731ede69f | 81 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 82 | } |
JMF | 0:082731ede69f | 83 | |
JMF | 0:082731ede69f | 84 | int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, topicType, JOB_REQUEST_TYPE, thingName, jobId); |
JMF | 0:082731ede69f | 85 | CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize); |
JMF | 0:082731ede69f | 86 | uint16_t topicSize = (uint16_t) neededSize; |
JMF | 0:082731ede69f | 87 | |
JMF | 0:082731ede69f | 88 | char emptyBuffer[1]; |
JMF | 0:082731ede69f | 89 | size_t messageLength; |
JMF | 0:082731ede69f | 90 | if (clientToken == NULL) { |
JMF | 0:082731ede69f | 91 | messageLength = 0; |
JMF | 0:082731ede69f | 92 | messageBuffer = emptyBuffer; |
JMF | 0:082731ede69f | 93 | } else { |
JMF | 0:082731ede69f | 94 | int serializeResult = aws_iot_jobs_json_serialize_client_token_only_request(messageBuffer, messageBufferSize, clientToken); |
JMF | 0:082731ede69f | 95 | CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize); |
JMF | 0:082731ede69f | 96 | messageLength = (size_t)serializeResult; |
JMF | 0:082731ede69f | 97 | } |
JMF | 0:082731ede69f | 98 | |
JMF | 0:082731ede69f | 99 | IoT_Publish_Message_Params publishParams; |
JMF | 0:082731ede69f | 100 | publishParams.qos = qos; |
JMF | 0:082731ede69f | 101 | publishParams.isRetained = 0; |
JMF | 0:082731ede69f | 102 | publishParams.isDup = 0; |
JMF | 0:082731ede69f | 103 | publishParams.id = 0; |
JMF | 0:082731ede69f | 104 | publishParams.payload = messageBuffer; |
JMF | 0:082731ede69f | 105 | publishParams.payloadLen = messageLength; |
JMF | 0:082731ede69f | 106 | |
JMF | 0:082731ede69f | 107 | return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams); |
JMF | 0:082731ede69f | 108 | } |
JMF | 0:082731ede69f | 109 | |
JMF | 0:082731ede69f | 110 | IoT_Error_t aws_iot_jobs_start_next( |
JMF | 0:082731ede69f | 111 | AWS_IoT_Client *pClient, QoS qos, |
JMF | 0:082731ede69f | 112 | const char *thingName, |
JMF | 0:082731ede69f | 113 | const AwsIotStartNextPendingJobExecutionRequest *startNextRequest, |
JMF | 0:082731ede69f | 114 | char *topicBuffer, |
JMF | 0:082731ede69f | 115 | uint16_t topicBufferSize, |
JMF | 0:082731ede69f | 116 | char *messageBuffer, |
JMF | 0:082731ede69f | 117 | size_t messageBufferSize) |
JMF | 0:082731ede69f | 118 | { |
JMF | 0:082731ede69f | 119 | if (thingName == NULL || topicBuffer == NULL || startNextRequest == NULL) { |
JMF | 0:082731ede69f | 120 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 121 | } |
JMF | 0:082731ede69f | 122 | |
JMF | 0:082731ede69f | 123 | int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, JOB_START_NEXT_TOPIC, JOB_REQUEST_TYPE, thingName, NULL); |
JMF | 0:082731ede69f | 124 | CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize); |
JMF | 0:082731ede69f | 125 | uint16_t topicSize = (uint16_t) neededSize; |
JMF | 0:082731ede69f | 126 | |
JMF | 0:082731ede69f | 127 | int serializeResult = aws_iot_jobs_json_serialize_start_next_job_execution_request(messageBuffer, messageBufferSize, startNextRequest); |
JMF | 0:082731ede69f | 128 | CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize); |
JMF | 0:082731ede69f | 129 | |
JMF | 0:082731ede69f | 130 | IoT_Publish_Message_Params publishParams; |
JMF | 0:082731ede69f | 131 | publishParams.qos = qos; |
JMF | 0:082731ede69f | 132 | publishParams.isRetained = 0; |
JMF | 0:082731ede69f | 133 | publishParams.isDup = 0; |
JMF | 0:082731ede69f | 134 | publishParams.id = 0; |
JMF | 0:082731ede69f | 135 | publishParams.payload = messageBuffer; |
JMF | 0:082731ede69f | 136 | publishParams.payloadLen = (size_t) serializeResult; |
JMF | 0:082731ede69f | 137 | |
JMF | 0:082731ede69f | 138 | return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams); |
JMF | 0:082731ede69f | 139 | } |
JMF | 0:082731ede69f | 140 | |
JMF | 0:082731ede69f | 141 | IoT_Error_t aws_iot_jobs_describe( |
JMF | 0:082731ede69f | 142 | AWS_IoT_Client *pClient, QoS qos, |
JMF | 0:082731ede69f | 143 | const char *thingName, |
JMF | 0:082731ede69f | 144 | const char *jobId, |
JMF | 0:082731ede69f | 145 | const AwsIotDescribeJobExecutionRequest *describeRequest, |
JMF | 0:082731ede69f | 146 | char *topicBuffer, |
JMF | 0:082731ede69f | 147 | uint16_t topicBufferSize, |
JMF | 0:082731ede69f | 148 | char *messageBuffer, |
JMF | 0:082731ede69f | 149 | size_t messageBufferSize) |
JMF | 0:082731ede69f | 150 | { |
JMF | 0:082731ede69f | 151 | if (thingName == NULL || topicBuffer == NULL || describeRequest == NULL) { |
JMF | 0:082731ede69f | 152 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 153 | } |
JMF | 0:082731ede69f | 154 | |
JMF | 0:082731ede69f | 155 | int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, JOB_DESCRIBE_TOPIC, JOB_REQUEST_TYPE, thingName, jobId); |
JMF | 0:082731ede69f | 156 | CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize); |
JMF | 0:082731ede69f | 157 | uint16_t topicSize = (uint16_t) neededSize; |
JMF | 0:082731ede69f | 158 | |
JMF | 0:082731ede69f | 159 | char emptyBuffer[1]; |
JMF | 0:082731ede69f | 160 | size_t messageLength; |
JMF | 0:082731ede69f | 161 | if (messageBuffer == NULL) { |
JMF | 0:082731ede69f | 162 | messageLength = 0; |
JMF | 0:082731ede69f | 163 | messageBuffer = emptyBuffer; |
JMF | 0:082731ede69f | 164 | } else { |
JMF | 0:082731ede69f | 165 | int serializeResult = aws_iot_jobs_json_serialize_describe_job_execution_request(messageBuffer, messageBufferSize, describeRequest); |
JMF | 0:082731ede69f | 166 | CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize); |
JMF | 0:082731ede69f | 167 | messageLength = (size_t) serializeResult; |
JMF | 0:082731ede69f | 168 | } |
JMF | 0:082731ede69f | 169 | |
JMF | 0:082731ede69f | 170 | IoT_Publish_Message_Params publishParams; |
JMF | 0:082731ede69f | 171 | publishParams.qos = qos; |
JMF | 0:082731ede69f | 172 | publishParams.isRetained = 0; |
JMF | 0:082731ede69f | 173 | publishParams.isDup = 0; |
JMF | 0:082731ede69f | 174 | publishParams.id = 0; |
JMF | 0:082731ede69f | 175 | publishParams.payload = messageBuffer; |
JMF | 0:082731ede69f | 176 | publishParams.payloadLen = messageLength; |
JMF | 0:082731ede69f | 177 | |
JMF | 0:082731ede69f | 178 | return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams); |
JMF | 0:082731ede69f | 179 | } |
JMF | 0:082731ede69f | 180 | |
JMF | 0:082731ede69f | 181 | IoT_Error_t aws_iot_jobs_send_update( |
JMF | 0:082731ede69f | 182 | AWS_IoT_Client *pClient, QoS qos, |
JMF | 0:082731ede69f | 183 | const char *thingName, |
JMF | 0:082731ede69f | 184 | const char *jobId, |
JMF | 0:082731ede69f | 185 | const AwsIotJobExecutionUpdateRequest *updateRequest, |
JMF | 0:082731ede69f | 186 | char *topicBuffer, |
JMF | 0:082731ede69f | 187 | uint16_t topicBufferSize, |
JMF | 0:082731ede69f | 188 | char *messageBuffer, |
JMF | 0:082731ede69f | 189 | size_t messageBufferSize) |
JMF | 0:082731ede69f | 190 | { |
JMF | 0:082731ede69f | 191 | if (thingName == NULL || topicBuffer == NULL || jobId == NULL || updateRequest == NULL) { |
JMF | 0:082731ede69f | 192 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 193 | } |
JMF | 0:082731ede69f | 194 | |
JMF | 0:082731ede69f | 195 | int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, JOB_UPDATE_TOPIC, JOB_REQUEST_TYPE, thingName, jobId); |
JMF | 0:082731ede69f | 196 | CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize); |
JMF | 0:082731ede69f | 197 | uint16_t topicSize = (uint16_t) neededSize; |
JMF | 0:082731ede69f | 198 | |
JMF | 0:082731ede69f | 199 | int serializeResult = aws_iot_jobs_json_serialize_update_job_execution_request(messageBuffer, messageBufferSize, updateRequest); |
JMF | 0:082731ede69f | 200 | CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize); |
JMF | 0:082731ede69f | 201 | |
JMF | 0:082731ede69f | 202 | IoT_Publish_Message_Params publishParams; |
JMF | 0:082731ede69f | 203 | publishParams.qos = qos; |
JMF | 0:082731ede69f | 204 | publishParams.isRetained = 0; |
JMF | 0:082731ede69f | 205 | publishParams.isDup = 0; |
JMF | 0:082731ede69f | 206 | publishParams.id = 0; |
JMF | 0:082731ede69f | 207 | publishParams.payload = messageBuffer; |
JMF | 0:082731ede69f | 208 | publishParams.payloadLen = (size_t) serializeResult; |
JMF | 0:082731ede69f | 209 | |
JMF | 0:082731ede69f | 210 | return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams); |
JMF | 0:082731ede69f | 211 | } |
JMF | 0:082731ede69f | 212 | |
JMF | 0:082731ede69f | 213 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 214 | } |
JMF | 0:082731ede69f | 215 | #endif |