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

« Back to documentation index

Show/hide line numbers aws_iot_jobs_topics.c Source File

aws_iot_jobs_topics.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 #ifdef __cplusplus
00017 extern "C" {
00018 #endif
00019 
00020 #include "aws_iot_jobs_topics.h"
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <stdbool.h>
00024 
00025 #define BASE_THINGS_TOPIC "$aws/things/"
00026 
00027 #define NOTIFY_OPERATION "notify"
00028 #define NOTIFY_NEXT_OPERATION "notify-next"
00029 #define GET_OPERATION "get"
00030 #define START_NEXT_OPERATION "start-next"
00031 #define WILDCARD_OPERATION "+"
00032 #define UPDATE_OPERATION "update"
00033 #define ACCEPTED_REPLY "accepted"
00034 #define REJECTED_REPLY "rejected"
00035 #define WILDCARD_REPLY "+"
00036 
00037 static const char *_get_operation_for_base_topic(AwsIotJobExecutionTopicType topicType) {
00038     switch (topicType) {
00039     case JOB_UPDATE_TOPIC:
00040         return UPDATE_OPERATION;
00041     case JOB_NOTIFY_TOPIC:
00042         return NOTIFY_OPERATION;
00043     case JOB_NOTIFY_NEXT_TOPIC:
00044         return NOTIFY_NEXT_OPERATION;
00045     case JOB_GET_PENDING_TOPIC:
00046     case JOB_DESCRIBE_TOPIC:
00047         return GET_OPERATION;
00048     case JOB_START_NEXT_TOPIC:
00049         return START_NEXT_OPERATION;
00050     case JOB_WILDCARD_TOPIC:
00051         return WILDCARD_OPERATION;
00052     case JOB_UNRECOGNIZED_TOPIC:
00053     default:
00054         return NULL;
00055     }
00056 }
00057 
00058 static bool _base_topic_requires_job_id(AwsIotJobExecutionTopicType topicType) {
00059     switch (topicType) {
00060     case JOB_UPDATE_TOPIC:
00061     case JOB_DESCRIBE_TOPIC:
00062         return true;
00063     case JOB_NOTIFY_TOPIC:
00064     case JOB_NOTIFY_NEXT_TOPIC:
00065     case JOB_START_NEXT_TOPIC:
00066     case JOB_GET_PENDING_TOPIC:
00067     case JOB_WILDCARD_TOPIC:
00068     case JOB_UNRECOGNIZED_TOPIC:
00069     default:
00070         return false;
00071     }
00072 }
00073 
00074 static const char *_get_suffix_for_topic_type(AwsIotJobExecutionTopicReplyType replyType) {
00075     switch (replyType) {
00076     case JOB_REQUEST_TYPE:
00077         return "";
00078 //      break;
00079     case JOB_ACCEPTED_REPLY_TYPE:
00080         return "/" ACCEPTED_REPLY;
00081 //      break;
00082     case JOB_REJECTED_REPLY_TYPE:
00083         return "/" REJECTED_REPLY;
00084 //      break;
00085     case JOB_WILDCARD_REPLY_TYPE:
00086         return "/" WILDCARD_REPLY;
00087 //      break;
00088     case JOB_UNRECOGNIZED_TOPIC_TYPE:
00089     default:
00090         return NULL;
00091     }
00092 }
00093 
00094 int aws_iot_jobs_get_api_topic(char *buffer, size_t bufferSize,
00095         AwsIotJobExecutionTopicType topicType, AwsIotJobExecutionTopicReplyType replyType,
00096         const char* thingName, const char* jobId)
00097 {
00098     if (thingName == NULL) {
00099         return -1;
00100     }
00101 
00102     if ((topicType == JOB_NOTIFY_TOPIC || topicType == JOB_NOTIFY_NEXT_TOPIC) && replyType != JOB_REQUEST_TYPE) {
00103         return -1;
00104     }
00105 
00106     bool requireJobId = _base_topic_requires_job_id(topicType);
00107     if (jobId == NULL && requireJobId) {
00108         return -1;
00109     }
00110 
00111     const char *operation = _get_operation_for_base_topic(topicType);
00112     if (operation == NULL) {
00113         return -1;
00114     }
00115 
00116     const char *suffix = _get_suffix_for_topic_type(replyType);
00117 
00118     if (requireJobId || (topicType == JOB_WILDCARD_TOPIC && jobId != NULL)) {
00119         return snprintf(buffer, bufferSize, BASE_THINGS_TOPIC "%s/jobs/%s/%s%s", thingName, jobId, operation, suffix);
00120     } else if (topicType == JOB_WILDCARD_TOPIC) {
00121         return snprintf(buffer, bufferSize, BASE_THINGS_TOPIC "%s/jobs/#", thingName);
00122     } else {
00123         return snprintf(buffer, bufferSize, BASE_THINGS_TOPIC "%s/jobs/%s%s", thingName, operation, suffix);
00124     }
00125 }
00126 
00127 #ifdef __cplusplus
00128 }
00129 #endif