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

« Back to documentation index

Show/hide line numbers aws_iot_jobs_json.c Source File

aws_iot_jobs_json.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 #define __STDC_FORMAT_MACROS
00021 #include <string.h>
00022 #include <stdbool.h>
00023 #include <stdlib.h>
00024 #include <stddef.h>
00025 #include <stdio.h>
00026 #include <stdarg.h>
00027 
00028 #include "jsmn.h"
00029 #include "aws_iot_jobs_json.h"
00030 
00031 struct _SerializeState {
00032     int totalSize;
00033     char *nextPtr;
00034     size_t remaingSize;
00035 };
00036 
00037 static void _printToBuffer(struct _SerializeState *state, const char *fmt, ...) {
00038     if (state->totalSize == -1) return;
00039 
00040     va_list vl;
00041     va_start(vl, fmt);
00042     int len = vsnprintf(state->nextPtr, state->remaingSize, fmt, vl);
00043     if (len < 0) {
00044         state->totalSize = -1;
00045     } else {
00046         state->totalSize += len;
00047         if (state->nextPtr != NULL) {
00048             if (state->remaingSize > (size_t) len) {
00049                 state->remaingSize -= (size_t) len;
00050                 state->nextPtr += len;
00051             } else {
00052                 state->remaingSize = 0;
00053                 state->nextPtr = NULL;
00054             }
00055         }
00056     }
00057     va_end(vl);
00058 }
00059 
00060 static void _printKey(struct _SerializeState *state, bool first, const char *key) {
00061     if (first) {
00062         _printToBuffer(state, "{\"%s\":", key);
00063     } else {
00064         _printToBuffer(state, ",\"%s\":", key);
00065     }
00066 }
00067 
00068 static void _printStringValue(struct _SerializeState *state, const char *value) {
00069     if (value == NULL) {
00070         _printToBuffer(state, "null");
00071     } else {
00072         _printToBuffer(state, "\"%s\"", value);
00073     }
00074 }
00075 
00076 static void _printLongValue(struct _SerializeState *state, int64_t value) {
00077     _printToBuffer(state, "%lld", value);
00078 }
00079 
00080 static void _printBooleanValue(struct _SerializeState *state, bool value) {
00081     if(value) {
00082         _printToBuffer(state, "true");
00083     } else {
00084         _printToBuffer(state, "false");
00085     }
00086 }
00087 
00088 int aws_iot_jobs_json_serialize_update_job_execution_request(
00089         char *requestBuffer, size_t bufferSize,
00090         const AwsIotJobExecutionUpdateRequest *request)
00091 {
00092     const char *statusStr = aws_iot_jobs_map_status_to_string(request->status);
00093     if (statusStr == NULL) return -1;
00094     if (requestBuffer == NULL) bufferSize = 0;
00095 
00096     struct _SerializeState state = { 0, requestBuffer, bufferSize };
00097     _printKey(&state, true, "status");
00098     _printStringValue(&state, statusStr);
00099     if (request->statusDetails != NULL) {
00100         _printKey(&state, false, "statusDetails");
00101         _printToBuffer(&state, "%s", request->statusDetails);
00102     }
00103     if (request->executionNumber != 0) {
00104         _printKey(&state, false, "executionNumber");
00105         _printLongValue(&state, request->executionNumber);
00106     }
00107     if (request->expectedVersion != 0) {
00108         _printKey(&state, false, "expectedVersion");
00109         _printLongValue(&state, request->expectedVersion);
00110     }
00111     if (request->includeJobExecutionState) {
00112         _printKey(&state, false, "includeJobExecutionState");
00113         _printBooleanValue(&state, request->includeJobExecutionState);
00114     }
00115     if (request->includeJobDocument) {
00116         _printKey(&state, false, "includeJobDocument");
00117         _printBooleanValue(&state, request->includeJobDocument);
00118     }
00119     if (request->clientToken != NULL) {
00120         _printKey(&state, false, "clientToken");
00121         _printStringValue(&state, request->clientToken);
00122     }
00123 
00124     _printToBuffer(&state, "}");
00125 
00126     return state.totalSize;
00127 }
00128 
00129 int aws_iot_jobs_json_serialize_client_token_only_request(
00130         char *requestBuffer, size_t bufferSize,
00131         const char *clientToken)
00132 {
00133     struct _SerializeState state = { 0, requestBuffer, bufferSize };
00134     _printKey(&state, true, "clientToken");
00135     _printStringValue(&state, clientToken);
00136     _printToBuffer(&state, "}");
00137 
00138     return state.totalSize;
00139 }
00140 
00141 int aws_iot_jobs_json_serialize_describe_job_execution_request(
00142         char *requestBuffer, size_t bufferSize,
00143         const AwsIotDescribeJobExecutionRequest *request)
00144 {
00145     bool first = true;
00146 
00147     if (requestBuffer == NULL) return 0;
00148 
00149     struct _SerializeState state = { 0, requestBuffer, bufferSize };
00150     if (request->clientToken != NULL) {
00151         _printKey(&state, first, "clientToken");
00152         _printStringValue(&state, request->clientToken);
00153         first = false;
00154     }
00155     if (request->executionNumber != 0) {
00156         _printKey(&state, first, "executionNumber");
00157         _printLongValue(&state, request->executionNumber);
00158         first = false;
00159     }
00160     if (request->includeJobDocument) {
00161         _printKey(&state, first, "includeJobDocument");
00162         _printBooleanValue(&state, request->includeJobDocument);
00163     }
00164 
00165     _printToBuffer(&state, "}");
00166 
00167     return state.totalSize;
00168 }
00169 
00170 int aws_iot_jobs_json_serialize_start_next_job_execution_request(
00171         char *requestBuffer, size_t bufferSize,
00172         const AwsIotStartNextPendingJobExecutionRequest *request)
00173 {
00174     if (requestBuffer == NULL) bufferSize = 0;
00175     struct _SerializeState state = { 0, requestBuffer, bufferSize };
00176     if (request->statusDetails != NULL) {
00177         _printKey(&state, true, "statusDetails");
00178         _printToBuffer(&state, "%s", request->statusDetails);
00179     }
00180     if (request->clientToken != NULL) {
00181         if(request->statusDetails != NULL) {
00182             _printKey(&state, false, "clientToken");
00183         } else {
00184             _printKey(&state, true, "clientToken");
00185         }
00186         _printStringValue(&state, request->clientToken);
00187     }
00188     if (request->clientToken == NULL && request->statusDetails == NULL) {
00189         _printToBuffer(&state, "{");
00190     }
00191     _printToBuffer(&state, "}");
00192     return state.totalSize;
00193 }
00194 
00195 #ifdef __cplusplus
00196 }
00197 #endif