Jim Flynn / Mbed OS aws-iot-device-sdk-mbed-c
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aws_iot_jobs_interface.c Source File

aws_iot_jobs_interface.c

00001 /*
00002 * Copyright 2015-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License").
00005 * You may not use this file except in compliance with the License.
00006 * A copy of the License is located at
00007 *
00008 * http://aws.amazon.com/apache2.0
00009 *
00010 * or in the "license" file accompanying this file. This file is distributed
00011 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
00012 * express or implied. See the License for the specific language governing
00013 * permissions and limitations under the License.
00014 */
00015 
00016 #include "aws_iot_jobs_interface.h"
00017 #include "aws_iot_log.h"
00018 #include "aws_iot_jobs_json.h"
00019 #include <string.h>
00020 
00021 #ifdef __cplusplus
00022 extern "C" {
00023 #endif
00024 
00025 #define CHECK_GENERATE_STRING_RESULT(result, bufferSize) \
00026     if (result < 0) { \
00027         return FAILURE; \
00028     } else if ((unsigned) result >= bufferSize) { \
00029         return LIMIT_EXCEEDED_ERROR; \
00030     }
00031 
00032 
00033 IoT_Error_t aws_iot_jobs_subscribe_to_job_messages(
00034         AWS_IoT_Client *pClient, QoS qos,
00035         const char *thingName,
00036         const char *jobId,
00037         AwsIotJobExecutionTopicType topicType,
00038         AwsIotJobExecutionTopicReplyType replyType,
00039         pApplicationHandler_t pApplicationHandler,
00040         void *pApplicationHandlerData,
00041         char *topicBuffer,
00042         uint16_t topicBufferSize)
00043 {
00044     int requiredSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, topicType, replyType, thingName, jobId);
00045     CHECK_GENERATE_STRING_RESULT(requiredSize, topicBufferSize);
00046 
00047     return aws_iot_mqtt_subscribe(pClient, topicBuffer, (uint16_t)strlen(topicBuffer), qos, pApplicationHandler, pApplicationHandlerData);
00048 }
00049 
00050 IoT_Error_t aws_iot_jobs_subscribe_to_all_job_messages(
00051         AWS_IoT_Client *pClient, QoS qos,
00052         const char *thingName,
00053         pApplicationHandler_t pApplicationHandler,
00054         void *pApplicationHandlerData,
00055         char *topicBuffer,
00056         uint16_t topicBufferSize)
00057 {
00058     return aws_iot_jobs_subscribe_to_job_messages(pClient, qos, thingName, NULL, JOB_WILDCARD_TOPIC, JOB_WILDCARD_REPLY_TYPE,
00059             pApplicationHandler, pApplicationHandlerData, topicBuffer, topicBufferSize);
00060 }
00061 
00062 IoT_Error_t aws_iot_jobs_unsubscribe_from_job_messages(
00063         AWS_IoT_Client *pClient,
00064         char *topicBuffer) 
00065 {
00066     return aws_iot_mqtt_unsubscribe(pClient, topicBuffer, (uint16_t)strlen(topicBuffer));
00067 }
00068 
00069 IoT_Error_t aws_iot_jobs_send_query(
00070         AWS_IoT_Client *pClient, QoS qos,
00071         const char *thingName,
00072         const char *jobId,
00073         const char *clientToken,
00074         char *topicBuffer,
00075         uint16_t topicBufferSize,
00076         char *messageBuffer,
00077         size_t messageBufferSize,
00078         AwsIotJobExecutionTopicType topicType)
00079 {
00080     if (thingName == NULL || topicBuffer == NULL || (clientToken != NULL && messageBuffer == NULL)) {
00081         return NULL_VALUE_ERROR;
00082     }
00083 
00084     int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, topicType, JOB_REQUEST_TYPE, thingName, jobId);
00085     CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize);
00086     uint16_t topicSize = (uint16_t) neededSize;
00087 
00088     char emptyBuffer[1];
00089     size_t messageLength;
00090     if (clientToken == NULL) {
00091         messageLength = 0;
00092         messageBuffer = emptyBuffer;
00093     } else {
00094         int serializeResult = aws_iot_jobs_json_serialize_client_token_only_request(messageBuffer, messageBufferSize, clientToken);
00095         CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize);
00096         messageLength = (size_t)serializeResult;
00097     }
00098 
00099     IoT_Publish_Message_Params publishParams;
00100     publishParams.qos = qos;
00101     publishParams.isRetained = 0;
00102     publishParams.isDup = 0;
00103     publishParams.id = 0;
00104     publishParams.payload = messageBuffer;
00105     publishParams.payloadLen = messageLength;
00106 
00107     return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams);
00108 }
00109 
00110 IoT_Error_t aws_iot_jobs_start_next(
00111         AWS_IoT_Client *pClient, QoS qos,
00112         const char *thingName,
00113         const AwsIotStartNextPendingJobExecutionRequest *startNextRequest,
00114         char *topicBuffer,
00115         uint16_t topicBufferSize,
00116         char *messageBuffer,
00117         size_t messageBufferSize)
00118 {
00119     if (thingName == NULL || topicBuffer == NULL || startNextRequest == NULL) {
00120         return NULL_VALUE_ERROR;
00121     }
00122 
00123     int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, JOB_START_NEXT_TOPIC, JOB_REQUEST_TYPE, thingName, NULL);
00124     CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize);
00125     uint16_t topicSize = (uint16_t) neededSize;
00126 
00127     int serializeResult = aws_iot_jobs_json_serialize_start_next_job_execution_request(messageBuffer, messageBufferSize, startNextRequest);
00128     CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize);
00129 
00130     IoT_Publish_Message_Params publishParams;
00131     publishParams.qos = qos;
00132     publishParams.isRetained = 0;
00133     publishParams.isDup = 0;
00134     publishParams.id = 0;
00135     publishParams.payload = messageBuffer;
00136     publishParams.payloadLen = (size_t) serializeResult;
00137 
00138     return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams);
00139 }
00140 
00141 IoT_Error_t aws_iot_jobs_describe(
00142         AWS_IoT_Client *pClient, QoS qos,
00143         const char *thingName,
00144         const char *jobId,
00145         const AwsIotDescribeJobExecutionRequest *describeRequest,
00146         char *topicBuffer,
00147         uint16_t topicBufferSize,
00148         char *messageBuffer,
00149         size_t messageBufferSize)
00150 {
00151     if (thingName == NULL || topicBuffer == NULL || describeRequest == NULL) {
00152         return NULL_VALUE_ERROR;
00153     }
00154 
00155     int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, JOB_DESCRIBE_TOPIC, JOB_REQUEST_TYPE, thingName, jobId);
00156     CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize);
00157     uint16_t topicSize = (uint16_t) neededSize;
00158 
00159     char emptyBuffer[1];
00160     size_t messageLength;
00161     if (messageBuffer == NULL) {
00162         messageLength = 0;
00163         messageBuffer = emptyBuffer;
00164     } else {
00165         int serializeResult = aws_iot_jobs_json_serialize_describe_job_execution_request(messageBuffer, messageBufferSize, describeRequest);
00166         CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize);
00167         messageLength = (size_t) serializeResult;
00168     }
00169 
00170     IoT_Publish_Message_Params publishParams;
00171     publishParams.qos = qos;
00172     publishParams.isRetained = 0;
00173     publishParams.isDup = 0;
00174     publishParams.id = 0;
00175     publishParams.payload = messageBuffer;
00176     publishParams.payloadLen = messageLength;
00177 
00178     return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams);
00179 }
00180 
00181 IoT_Error_t aws_iot_jobs_send_update(
00182         AWS_IoT_Client *pClient, QoS qos,
00183         const char *thingName,
00184         const char *jobId,
00185         const AwsIotJobExecutionUpdateRequest *updateRequest,
00186         char *topicBuffer,
00187         uint16_t topicBufferSize,
00188         char *messageBuffer,
00189         size_t messageBufferSize)
00190 {
00191     if (thingName == NULL || topicBuffer == NULL || jobId == NULL || updateRequest == NULL) {
00192         return NULL_VALUE_ERROR;
00193     }
00194 
00195     int neededSize = aws_iot_jobs_get_api_topic(topicBuffer, topicBufferSize, JOB_UPDATE_TOPIC, JOB_REQUEST_TYPE, thingName, jobId);
00196     CHECK_GENERATE_STRING_RESULT(neededSize, topicBufferSize);
00197     uint16_t topicSize = (uint16_t) neededSize;
00198 
00199     int serializeResult = aws_iot_jobs_json_serialize_update_job_execution_request(messageBuffer, messageBufferSize, updateRequest);
00200     CHECK_GENERATE_STRING_RESULT(serializeResult, messageBufferSize);
00201 
00202     IoT_Publish_Message_Params publishParams;
00203     publishParams.qos = qos;
00204     publishParams.isRetained = 0;
00205     publishParams.isDup = 0;
00206     publishParams.id = 0;
00207     publishParams.payload = messageBuffer;
00208     publishParams.payloadLen = (size_t) serializeResult;
00209 
00210     return aws_iot_mqtt_publish(pClient, topicBuffer, topicSize, &publishParams);
00211 }
00212 
00213 #ifdef __cplusplus
00214 }
00215 #endif