Changes to enabled on-line compiler

Committer:
JMF
Date:
Wed May 30 20:59:51 2018 +0000
Revision:
0:082731ede69f
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifdef __cplusplus
JMF 0:082731ede69f 17 extern "C" {
JMF 0:082731ede69f 18 #endif
JMF 0:082731ede69f 19
JMF 0:082731ede69f 20 #include "aws_iot_jobs_topics.h"
JMF 0:082731ede69f 21 #include <string.h>
JMF 0:082731ede69f 22 #include <stdio.h>
JMF 0:082731ede69f 23 #include <stdbool.h>
JMF 0:082731ede69f 24
JMF 0:082731ede69f 25 #define BASE_THINGS_TOPIC "$aws/things/"
JMF 0:082731ede69f 26
JMF 0:082731ede69f 27 #define NOTIFY_OPERATION "notify"
JMF 0:082731ede69f 28 #define NOTIFY_NEXT_OPERATION "notify-next"
JMF 0:082731ede69f 29 #define GET_OPERATION "get"
JMF 0:082731ede69f 30 #define START_NEXT_OPERATION "start-next"
JMF 0:082731ede69f 31 #define WILDCARD_OPERATION "+"
JMF 0:082731ede69f 32 #define UPDATE_OPERATION "update"
JMF 0:082731ede69f 33 #define ACCEPTED_REPLY "accepted"
JMF 0:082731ede69f 34 #define REJECTED_REPLY "rejected"
JMF 0:082731ede69f 35 #define WILDCARD_REPLY "+"
JMF 0:082731ede69f 36
JMF 0:082731ede69f 37 static const char *_get_operation_for_base_topic(AwsIotJobExecutionTopicType topicType) {
JMF 0:082731ede69f 38 switch (topicType) {
JMF 0:082731ede69f 39 case JOB_UPDATE_TOPIC:
JMF 0:082731ede69f 40 return UPDATE_OPERATION;
JMF 0:082731ede69f 41 case JOB_NOTIFY_TOPIC:
JMF 0:082731ede69f 42 return NOTIFY_OPERATION;
JMF 0:082731ede69f 43 case JOB_NOTIFY_NEXT_TOPIC:
JMF 0:082731ede69f 44 return NOTIFY_NEXT_OPERATION;
JMF 0:082731ede69f 45 case JOB_GET_PENDING_TOPIC:
JMF 0:082731ede69f 46 case JOB_DESCRIBE_TOPIC:
JMF 0:082731ede69f 47 return GET_OPERATION;
JMF 0:082731ede69f 48 case JOB_START_NEXT_TOPIC:
JMF 0:082731ede69f 49 return START_NEXT_OPERATION;
JMF 0:082731ede69f 50 case JOB_WILDCARD_TOPIC:
JMF 0:082731ede69f 51 return WILDCARD_OPERATION;
JMF 0:082731ede69f 52 case JOB_UNRECOGNIZED_TOPIC:
JMF 0:082731ede69f 53 default:
JMF 0:082731ede69f 54 return NULL;
JMF 0:082731ede69f 55 }
JMF 0:082731ede69f 56 }
JMF 0:082731ede69f 57
JMF 0:082731ede69f 58 static bool _base_topic_requires_job_id(AwsIotJobExecutionTopicType topicType) {
JMF 0:082731ede69f 59 switch (topicType) {
JMF 0:082731ede69f 60 case JOB_UPDATE_TOPIC:
JMF 0:082731ede69f 61 case JOB_DESCRIBE_TOPIC:
JMF 0:082731ede69f 62 return true;
JMF 0:082731ede69f 63 case JOB_NOTIFY_TOPIC:
JMF 0:082731ede69f 64 case JOB_NOTIFY_NEXT_TOPIC:
JMF 0:082731ede69f 65 case JOB_START_NEXT_TOPIC:
JMF 0:082731ede69f 66 case JOB_GET_PENDING_TOPIC:
JMF 0:082731ede69f 67 case JOB_WILDCARD_TOPIC:
JMF 0:082731ede69f 68 case JOB_UNRECOGNIZED_TOPIC:
JMF 0:082731ede69f 69 default:
JMF 0:082731ede69f 70 return false;
JMF 0:082731ede69f 71 }
JMF 0:082731ede69f 72 }
JMF 0:082731ede69f 73
JMF 0:082731ede69f 74 static const char *_get_suffix_for_topic_type(AwsIotJobExecutionTopicReplyType replyType) {
JMF 0:082731ede69f 75 switch (replyType) {
JMF 0:082731ede69f 76 case JOB_REQUEST_TYPE:
JMF 0:082731ede69f 77 return "";
JMF 0:082731ede69f 78 // break;
JMF 0:082731ede69f 79 case JOB_ACCEPTED_REPLY_TYPE:
JMF 0:082731ede69f 80 return "/" ACCEPTED_REPLY;
JMF 0:082731ede69f 81 // break;
JMF 0:082731ede69f 82 case JOB_REJECTED_REPLY_TYPE:
JMF 0:082731ede69f 83 return "/" REJECTED_REPLY;
JMF 0:082731ede69f 84 // break;
JMF 0:082731ede69f 85 case JOB_WILDCARD_REPLY_TYPE:
JMF 0:082731ede69f 86 return "/" WILDCARD_REPLY;
JMF 0:082731ede69f 87 // break;
JMF 0:082731ede69f 88 case JOB_UNRECOGNIZED_TOPIC_TYPE:
JMF 0:082731ede69f 89 default:
JMF 0:082731ede69f 90 return NULL;
JMF 0:082731ede69f 91 }
JMF 0:082731ede69f 92 }
JMF 0:082731ede69f 93
JMF 0:082731ede69f 94 int aws_iot_jobs_get_api_topic(char *buffer, size_t bufferSize,
JMF 0:082731ede69f 95 AwsIotJobExecutionTopicType topicType, AwsIotJobExecutionTopicReplyType replyType,
JMF 0:082731ede69f 96 const char* thingName, const char* jobId)
JMF 0:082731ede69f 97 {
JMF 0:082731ede69f 98 if (thingName == NULL) {
JMF 0:082731ede69f 99 return -1;
JMF 0:082731ede69f 100 }
JMF 0:082731ede69f 101
JMF 0:082731ede69f 102 if ((topicType == JOB_NOTIFY_TOPIC || topicType == JOB_NOTIFY_NEXT_TOPIC) && replyType != JOB_REQUEST_TYPE) {
JMF 0:082731ede69f 103 return -1;
JMF 0:082731ede69f 104 }
JMF 0:082731ede69f 105
JMF 0:082731ede69f 106 bool requireJobId = _base_topic_requires_job_id(topicType);
JMF 0:082731ede69f 107 if (jobId == NULL && requireJobId) {
JMF 0:082731ede69f 108 return -1;
JMF 0:082731ede69f 109 }
JMF 0:082731ede69f 110
JMF 0:082731ede69f 111 const char *operation = _get_operation_for_base_topic(topicType);
JMF 0:082731ede69f 112 if (operation == NULL) {
JMF 0:082731ede69f 113 return -1;
JMF 0:082731ede69f 114 }
JMF 0:082731ede69f 115
JMF 0:082731ede69f 116 const char *suffix = _get_suffix_for_topic_type(replyType);
JMF 0:082731ede69f 117
JMF 0:082731ede69f 118 if (requireJobId || (topicType == JOB_WILDCARD_TOPIC && jobId != NULL)) {
JMF 0:082731ede69f 119 return snprintf(buffer, bufferSize, BASE_THINGS_TOPIC "%s/jobs/%s/%s%s", thingName, jobId, operation, suffix);
JMF 0:082731ede69f 120 } else if (topicType == JOB_WILDCARD_TOPIC) {
JMF 0:082731ede69f 121 return snprintf(buffer, bufferSize, BASE_THINGS_TOPIC "%s/jobs/#", thingName);
JMF 0:082731ede69f 122 } else {
JMF 0:082731ede69f 123 return snprintf(buffer, bufferSize, BASE_THINGS_TOPIC "%s/jobs/%s%s", thingName, operation, suffix);
JMF 0:082731ede69f 124 }
JMF 0:082731ede69f 125 }
JMF 0:082731ede69f 126
JMF 0:082731ede69f 127 #ifdef __cplusplus
JMF 0:082731ede69f 128 }
JMF 0:082731ede69f 129 #endif