Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
src/aws_iot_jobs_json.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 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 17 | extern "C" { |
JMF | 0:082731ede69f | 18 | #endif |
JMF | 0:082731ede69f | 19 | |
JMF | 0:082731ede69f | 20 | #define __STDC_FORMAT_MACROS |
JMF | 0:082731ede69f | 21 | #include <string.h> |
JMF | 0:082731ede69f | 22 | #include <stdbool.h> |
JMF | 0:082731ede69f | 23 | #include <stdlib.h> |
JMF | 0:082731ede69f | 24 | #include <stddef.h> |
JMF | 0:082731ede69f | 25 | #include <stdio.h> |
JMF | 0:082731ede69f | 26 | #include <stdarg.h> |
JMF | 0:082731ede69f | 27 | |
JMF | 0:082731ede69f | 28 | #include "jsmn.h" |
JMF | 0:082731ede69f | 29 | #include "aws_iot_jobs_json.h" |
JMF | 0:082731ede69f | 30 | |
JMF | 0:082731ede69f | 31 | struct _SerializeState { |
JMF | 0:082731ede69f | 32 | int totalSize; |
JMF | 0:082731ede69f | 33 | char *nextPtr; |
JMF | 0:082731ede69f | 34 | size_t remaingSize; |
JMF | 0:082731ede69f | 35 | }; |
JMF | 0:082731ede69f | 36 | |
JMF | 0:082731ede69f | 37 | static void _printToBuffer(struct _SerializeState *state, const char *fmt, ...) { |
JMF | 0:082731ede69f | 38 | if (state->totalSize == -1) return; |
JMF | 0:082731ede69f | 39 | |
JMF | 0:082731ede69f | 40 | va_list vl; |
JMF | 0:082731ede69f | 41 | va_start(vl, fmt); |
JMF | 0:082731ede69f | 42 | int len = vsnprintf(state->nextPtr, state->remaingSize, fmt, vl); |
JMF | 0:082731ede69f | 43 | if (len < 0) { |
JMF | 0:082731ede69f | 44 | state->totalSize = -1; |
JMF | 0:082731ede69f | 45 | } else { |
JMF | 0:082731ede69f | 46 | state->totalSize += len; |
JMF | 0:082731ede69f | 47 | if (state->nextPtr != NULL) { |
JMF | 0:082731ede69f | 48 | if (state->remaingSize > (size_t) len) { |
JMF | 0:082731ede69f | 49 | state->remaingSize -= (size_t) len; |
JMF | 0:082731ede69f | 50 | state->nextPtr += len; |
JMF | 0:082731ede69f | 51 | } else { |
JMF | 0:082731ede69f | 52 | state->remaingSize = 0; |
JMF | 0:082731ede69f | 53 | state->nextPtr = NULL; |
JMF | 0:082731ede69f | 54 | } |
JMF | 0:082731ede69f | 55 | } |
JMF | 0:082731ede69f | 56 | } |
JMF | 0:082731ede69f | 57 | va_end(vl); |
JMF | 0:082731ede69f | 58 | } |
JMF | 0:082731ede69f | 59 | |
JMF | 0:082731ede69f | 60 | static void _printKey(struct _SerializeState *state, bool first, const char *key) { |
JMF | 0:082731ede69f | 61 | if (first) { |
JMF | 0:082731ede69f | 62 | _printToBuffer(state, "{\"%s\":", key); |
JMF | 0:082731ede69f | 63 | } else { |
JMF | 0:082731ede69f | 64 | _printToBuffer(state, ",\"%s\":", key); |
JMF | 0:082731ede69f | 65 | } |
JMF | 0:082731ede69f | 66 | } |
JMF | 0:082731ede69f | 67 | |
JMF | 0:082731ede69f | 68 | static void _printStringValue(struct _SerializeState *state, const char *value) { |
JMF | 0:082731ede69f | 69 | if (value == NULL) { |
JMF | 0:082731ede69f | 70 | _printToBuffer(state, "null"); |
JMF | 0:082731ede69f | 71 | } else { |
JMF | 0:082731ede69f | 72 | _printToBuffer(state, "\"%s\"", value); |
JMF | 0:082731ede69f | 73 | } |
JMF | 0:082731ede69f | 74 | } |
JMF | 0:082731ede69f | 75 | |
JMF | 0:082731ede69f | 76 | static void _printLongValue(struct _SerializeState *state, int64_t value) { |
JMF | 0:082731ede69f | 77 | _printToBuffer(state, "%lld", value); |
JMF | 0:082731ede69f | 78 | } |
JMF | 0:082731ede69f | 79 | |
JMF | 0:082731ede69f | 80 | static void _printBooleanValue(struct _SerializeState *state, bool value) { |
JMF | 0:082731ede69f | 81 | if(value) { |
JMF | 0:082731ede69f | 82 | _printToBuffer(state, "true"); |
JMF | 0:082731ede69f | 83 | } else { |
JMF | 0:082731ede69f | 84 | _printToBuffer(state, "false"); |
JMF | 0:082731ede69f | 85 | } |
JMF | 0:082731ede69f | 86 | } |
JMF | 0:082731ede69f | 87 | |
JMF | 0:082731ede69f | 88 | int aws_iot_jobs_json_serialize_update_job_execution_request( |
JMF | 0:082731ede69f | 89 | char *requestBuffer, size_t bufferSize, |
JMF | 0:082731ede69f | 90 | const AwsIotJobExecutionUpdateRequest *request) |
JMF | 0:082731ede69f | 91 | { |
JMF | 0:082731ede69f | 92 | const char *statusStr = aws_iot_jobs_map_status_to_string(request->status); |
JMF | 0:082731ede69f | 93 | if (statusStr == NULL) return -1; |
JMF | 0:082731ede69f | 94 | if (requestBuffer == NULL) bufferSize = 0; |
JMF | 0:082731ede69f | 95 | |
JMF | 0:082731ede69f | 96 | struct _SerializeState state = { 0, requestBuffer, bufferSize }; |
JMF | 0:082731ede69f | 97 | _printKey(&state, true, "status"); |
JMF | 0:082731ede69f | 98 | _printStringValue(&state, statusStr); |
JMF | 0:082731ede69f | 99 | if (request->statusDetails != NULL) { |
JMF | 0:082731ede69f | 100 | _printKey(&state, false, "statusDetails"); |
JMF | 0:082731ede69f | 101 | _printToBuffer(&state, "%s", request->statusDetails); |
JMF | 0:082731ede69f | 102 | } |
JMF | 0:082731ede69f | 103 | if (request->executionNumber != 0) { |
JMF | 0:082731ede69f | 104 | _printKey(&state, false, "executionNumber"); |
JMF | 0:082731ede69f | 105 | _printLongValue(&state, request->executionNumber); |
JMF | 0:082731ede69f | 106 | } |
JMF | 0:082731ede69f | 107 | if (request->expectedVersion != 0) { |
JMF | 0:082731ede69f | 108 | _printKey(&state, false, "expectedVersion"); |
JMF | 0:082731ede69f | 109 | _printLongValue(&state, request->expectedVersion); |
JMF | 0:082731ede69f | 110 | } |
JMF | 0:082731ede69f | 111 | if (request->includeJobExecutionState) { |
JMF | 0:082731ede69f | 112 | _printKey(&state, false, "includeJobExecutionState"); |
JMF | 0:082731ede69f | 113 | _printBooleanValue(&state, request->includeJobExecutionState); |
JMF | 0:082731ede69f | 114 | } |
JMF | 0:082731ede69f | 115 | if (request->includeJobDocument) { |
JMF | 0:082731ede69f | 116 | _printKey(&state, false, "includeJobDocument"); |
JMF | 0:082731ede69f | 117 | _printBooleanValue(&state, request->includeJobDocument); |
JMF | 0:082731ede69f | 118 | } |
JMF | 0:082731ede69f | 119 | if (request->clientToken != NULL) { |
JMF | 0:082731ede69f | 120 | _printKey(&state, false, "clientToken"); |
JMF | 0:082731ede69f | 121 | _printStringValue(&state, request->clientToken); |
JMF | 0:082731ede69f | 122 | } |
JMF | 0:082731ede69f | 123 | |
JMF | 0:082731ede69f | 124 | _printToBuffer(&state, "}"); |
JMF | 0:082731ede69f | 125 | |
JMF | 0:082731ede69f | 126 | return state.totalSize; |
JMF | 0:082731ede69f | 127 | } |
JMF | 0:082731ede69f | 128 | |
JMF | 0:082731ede69f | 129 | int aws_iot_jobs_json_serialize_client_token_only_request( |
JMF | 0:082731ede69f | 130 | char *requestBuffer, size_t bufferSize, |
JMF | 0:082731ede69f | 131 | const char *clientToken) |
JMF | 0:082731ede69f | 132 | { |
JMF | 0:082731ede69f | 133 | struct _SerializeState state = { 0, requestBuffer, bufferSize }; |
JMF | 0:082731ede69f | 134 | _printKey(&state, true, "clientToken"); |
JMF | 0:082731ede69f | 135 | _printStringValue(&state, clientToken); |
JMF | 0:082731ede69f | 136 | _printToBuffer(&state, "}"); |
JMF | 0:082731ede69f | 137 | |
JMF | 0:082731ede69f | 138 | return state.totalSize; |
JMF | 0:082731ede69f | 139 | } |
JMF | 0:082731ede69f | 140 | |
JMF | 0:082731ede69f | 141 | int aws_iot_jobs_json_serialize_describe_job_execution_request( |
JMF | 0:082731ede69f | 142 | char *requestBuffer, size_t bufferSize, |
JMF | 0:082731ede69f | 143 | const AwsIotDescribeJobExecutionRequest *request) |
JMF | 0:082731ede69f | 144 | { |
JMF | 0:082731ede69f | 145 | bool first = true; |
JMF | 0:082731ede69f | 146 | |
JMF | 0:082731ede69f | 147 | if (requestBuffer == NULL) return 0; |
JMF | 0:082731ede69f | 148 | |
JMF | 0:082731ede69f | 149 | struct _SerializeState state = { 0, requestBuffer, bufferSize }; |
JMF | 0:082731ede69f | 150 | if (request->clientToken != NULL) { |
JMF | 0:082731ede69f | 151 | _printKey(&state, first, "clientToken"); |
JMF | 0:082731ede69f | 152 | _printStringValue(&state, request->clientToken); |
JMF | 0:082731ede69f | 153 | first = false; |
JMF | 0:082731ede69f | 154 | } |
JMF | 0:082731ede69f | 155 | if (request->executionNumber != 0) { |
JMF | 0:082731ede69f | 156 | _printKey(&state, first, "executionNumber"); |
JMF | 0:082731ede69f | 157 | _printLongValue(&state, request->executionNumber); |
JMF | 0:082731ede69f | 158 | first = false; |
JMF | 0:082731ede69f | 159 | } |
JMF | 0:082731ede69f | 160 | if (request->includeJobDocument) { |
JMF | 0:082731ede69f | 161 | _printKey(&state, first, "includeJobDocument"); |
JMF | 0:082731ede69f | 162 | _printBooleanValue(&state, request->includeJobDocument); |
JMF | 0:082731ede69f | 163 | } |
JMF | 0:082731ede69f | 164 | |
JMF | 0:082731ede69f | 165 | _printToBuffer(&state, "}"); |
JMF | 0:082731ede69f | 166 | |
JMF | 0:082731ede69f | 167 | return state.totalSize; |
JMF | 0:082731ede69f | 168 | } |
JMF | 0:082731ede69f | 169 | |
JMF | 0:082731ede69f | 170 | int aws_iot_jobs_json_serialize_start_next_job_execution_request( |
JMF | 0:082731ede69f | 171 | char *requestBuffer, size_t bufferSize, |
JMF | 0:082731ede69f | 172 | const AwsIotStartNextPendingJobExecutionRequest *request) |
JMF | 0:082731ede69f | 173 | { |
JMF | 0:082731ede69f | 174 | if (requestBuffer == NULL) bufferSize = 0; |
JMF | 0:082731ede69f | 175 | struct _SerializeState state = { 0, requestBuffer, bufferSize }; |
JMF | 0:082731ede69f | 176 | if (request->statusDetails != NULL) { |
JMF | 0:082731ede69f | 177 | _printKey(&state, true, "statusDetails"); |
JMF | 0:082731ede69f | 178 | _printToBuffer(&state, "%s", request->statusDetails); |
JMF | 0:082731ede69f | 179 | } |
JMF | 0:082731ede69f | 180 | if (request->clientToken != NULL) { |
JMF | 0:082731ede69f | 181 | if(request->statusDetails != NULL) { |
JMF | 0:082731ede69f | 182 | _printKey(&state, false, "clientToken"); |
JMF | 0:082731ede69f | 183 | } else { |
JMF | 0:082731ede69f | 184 | _printKey(&state, true, "clientToken"); |
JMF | 0:082731ede69f | 185 | } |
JMF | 0:082731ede69f | 186 | _printStringValue(&state, request->clientToken); |
JMF | 0:082731ede69f | 187 | } |
JMF | 0:082731ede69f | 188 | if (request->clientToken == NULL && request->statusDetails == NULL) { |
JMF | 0:082731ede69f | 189 | _printToBuffer(&state, "{"); |
JMF | 0:082731ede69f | 190 | } |
JMF | 0:082731ede69f | 191 | _printToBuffer(&state, "}"); |
JMF | 0:082731ede69f | 192 | return state.totalSize; |
JMF | 0:082731ede69f | 193 | } |
JMF | 0:082731ede69f | 194 | |
JMF | 0:082731ede69f | 195 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 196 | } |
JMF | 0:082731ede69f | 197 | #endif |