A modelling and serializer library for Microsoft Azure IoTHub client applications
Dependents: sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more
This library implements a serializer library to be used in projects involving Microsoft Azure IoT Hub connectivity. The code is replicated from https://github.com/Azure/azure-iot-sdks
methodreturn.c@36:7d12a5386197, 2018-09-11 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Sep 11 11:14:37 2018 -0700
- Revision:
- 36:7d12a5386197
- Parent:
- 31:bdcce611d29b
1.2.9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 17:fa1bba4c6053 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 17:fa1bba4c6053 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 17:fa1bba4c6053 | 3 | |
AzureIoTClient | 17:fa1bba4c6053 | 4 | #include <stdlib.h> |
AzureIoTClient | 17:fa1bba4c6053 | 5 | #include <limits.h> |
AzureIoTClient | 17:fa1bba4c6053 | 6 | |
AzureIoTClient | 17:fa1bba4c6053 | 7 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 17:fa1bba4c6053 | 8 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 17:fa1bba4c6053 | 9 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 17:fa1bba4c6053 | 10 | #include "azure_c_shared_utility/strings.h" |
AzureIoTClient | 17:fa1bba4c6053 | 11 | #include "parson.h" |
AzureIoTClient | 17:fa1bba4c6053 | 12 | |
AzureIoTClient | 17:fa1bba4c6053 | 13 | #define METHODRETURN_C |
AzureIoTClient | 17:fa1bba4c6053 | 14 | #include "methodreturn.h" |
AzureIoTClient | 17:fa1bba4c6053 | 15 | #undef METHODRETURN_C |
AzureIoTClient | 17:fa1bba4c6053 | 16 | |
AzureIoTClient | 17:fa1bba4c6053 | 17 | typedef struct METHODRETURN_HANDLE_DATA_TAG |
AzureIoTClient | 17:fa1bba4c6053 | 18 | { |
AzureIoTClient | 17:fa1bba4c6053 | 19 | METHODRETURN_DATA data; |
AzureIoTClient | 17:fa1bba4c6053 | 20 | }METHODRETURN_HANDLE_DATA; |
AzureIoTClient | 17:fa1bba4c6053 | 21 | |
AzureIoTClient | 31:bdcce611d29b | 22 | bool is_json_present_and_unparsable(const char* jsonValue) |
AzureIoTClient | 31:bdcce611d29b | 23 | { |
AzureIoTClient | 31:bdcce611d29b | 24 | bool is_present_and_unparsable; |
AzureIoTClient | 31:bdcce611d29b | 25 | if (jsonValue == NULL) |
AzureIoTClient | 31:bdcce611d29b | 26 | { |
AzureIoTClient | 31:bdcce611d29b | 27 | // Null json is not considered invalid here |
AzureIoTClient | 31:bdcce611d29b | 28 | is_present_and_unparsable = false; |
AzureIoTClient | 31:bdcce611d29b | 29 | } |
AzureIoTClient | 31:bdcce611d29b | 30 | else |
AzureIoTClient | 31:bdcce611d29b | 31 | { |
AzureIoTClient | 31:bdcce611d29b | 32 | JSON_Value* temp = json_parse_string(jsonValue); |
AzureIoTClient | 31:bdcce611d29b | 33 | if (temp == NULL) |
AzureIoTClient | 31:bdcce611d29b | 34 | { |
AzureIoTClient | 31:bdcce611d29b | 35 | is_present_and_unparsable = true; |
AzureIoTClient | 31:bdcce611d29b | 36 | } |
AzureIoTClient | 31:bdcce611d29b | 37 | else |
AzureIoTClient | 31:bdcce611d29b | 38 | { |
AzureIoTClient | 31:bdcce611d29b | 39 | json_value_free(temp); |
AzureIoTClient | 31:bdcce611d29b | 40 | is_present_and_unparsable = false; |
AzureIoTClient | 31:bdcce611d29b | 41 | } |
AzureIoTClient | 31:bdcce611d29b | 42 | } |
AzureIoTClient | 31:bdcce611d29b | 43 | return is_present_and_unparsable; |
AzureIoTClient | 31:bdcce611d29b | 44 | } |
AzureIoTClient | 31:bdcce611d29b | 45 | |
AzureIoTClient | 17:fa1bba4c6053 | 46 | METHODRETURN_HANDLE MethodReturn_Create(int statusCode, const char* jsonValue) |
AzureIoTClient | 17:fa1bba4c6053 | 47 | { |
AzureIoTClient | 17:fa1bba4c6053 | 48 | METHODRETURN_HANDLE result; |
AzureIoTClient | 17:fa1bba4c6053 | 49 | /*Codes_SRS_METHODRETURN_02_009: [ If jsonValue is not a JSON value then MethodReturn_Create shall fail and return NULL. ]*/ |
AzureIoTClient | 31:bdcce611d29b | 50 | if (is_json_present_and_unparsable(jsonValue)) |
AzureIoTClient | 17:fa1bba4c6053 | 51 | { |
AzureIoTClient | 17:fa1bba4c6053 | 52 | LogError("%s is not JSON", jsonValue); |
AzureIoTClient | 17:fa1bba4c6053 | 53 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 54 | } |
AzureIoTClient | 17:fa1bba4c6053 | 55 | else |
AzureIoTClient | 17:fa1bba4c6053 | 56 | { |
AzureIoTClient | 17:fa1bba4c6053 | 57 | result = (METHODRETURN_HANDLE_DATA*)malloc(sizeof(METHODRETURN_HANDLE_DATA)); |
AzureIoTClient | 17:fa1bba4c6053 | 58 | if (result == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 59 | { |
AzureIoTClient | 17:fa1bba4c6053 | 60 | /*Codes_SRS_METHODRETURN_02_002: [ If any failure is encountered then MethodReturn_Create shall return NULL ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 61 | LogError("unable to malloc"); |
AzureIoTClient | 17:fa1bba4c6053 | 62 | /*return as is*/ |
AzureIoTClient | 17:fa1bba4c6053 | 63 | } |
AzureIoTClient | 17:fa1bba4c6053 | 64 | else |
AzureIoTClient | 17:fa1bba4c6053 | 65 | { |
AzureIoTClient | 17:fa1bba4c6053 | 66 | if (jsonValue == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 67 | { |
AzureIoTClient | 25:475bf9d91efb | 68 | /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 69 | result->data.jsonValue = NULL; |
AzureIoTClient | 25:475bf9d91efb | 70 | result->data.statusCode = statusCode; |
AzureIoTClient | 17:fa1bba4c6053 | 71 | } |
AzureIoTClient | 17:fa1bba4c6053 | 72 | else |
AzureIoTClient | 17:fa1bba4c6053 | 73 | { |
AzureIoTClient | 17:fa1bba4c6053 | 74 | if (mallocAndStrcpy_s(&(result->data.jsonValue), jsonValue) != 0) |
AzureIoTClient | 17:fa1bba4c6053 | 75 | { |
AzureIoTClient | 17:fa1bba4c6053 | 76 | LogError("failure in mallocAndStrcpy_s"); |
AzureIoTClient | 17:fa1bba4c6053 | 77 | free(result); |
AzureIoTClient | 17:fa1bba4c6053 | 78 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 79 | } |
AzureIoTClient | 17:fa1bba4c6053 | 80 | else |
AzureIoTClient | 17:fa1bba4c6053 | 81 | { |
AzureIoTClient | 17:fa1bba4c6053 | 82 | /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 83 | result->data.statusCode = statusCode; |
AzureIoTClient | 17:fa1bba4c6053 | 84 | } |
AzureIoTClient | 17:fa1bba4c6053 | 85 | } |
AzureIoTClient | 17:fa1bba4c6053 | 86 | } |
AzureIoTClient | 17:fa1bba4c6053 | 87 | } |
AzureIoTClient | 31:bdcce611d29b | 88 | |
AzureIoTClient | 17:fa1bba4c6053 | 89 | return result; |
AzureIoTClient | 17:fa1bba4c6053 | 90 | } |
AzureIoTClient | 17:fa1bba4c6053 | 91 | |
AzureIoTClient | 17:fa1bba4c6053 | 92 | void MethodReturn_Destroy(METHODRETURN_HANDLE handle) |
AzureIoTClient | 17:fa1bba4c6053 | 93 | { |
AzureIoTClient | 17:fa1bba4c6053 | 94 | if (handle == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 95 | { |
AzureIoTClient | 17:fa1bba4c6053 | 96 | /*Codes_SRS_METHODRETURN_02_003: [ If handle is NULL then MethodReturn_Destroy shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 97 | LogError("invalid argument METHODRETURN_HANDLE handle=%p", handle); |
AzureIoTClient | 17:fa1bba4c6053 | 98 | } |
AzureIoTClient | 17:fa1bba4c6053 | 99 | else |
AzureIoTClient | 17:fa1bba4c6053 | 100 | { |
AzureIoTClient | 17:fa1bba4c6053 | 101 | /*Codes_SRS_METHODRETURN_02_004: [ Otherwise, MethodReturn_Destroy shall free all used resources by handle. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 102 | if (handle->data.jsonValue != NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 103 | { |
AzureIoTClient | 17:fa1bba4c6053 | 104 | free(handle->data.jsonValue); |
AzureIoTClient | 17:fa1bba4c6053 | 105 | } |
AzureIoTClient | 17:fa1bba4c6053 | 106 | free(handle); |
AzureIoTClient | 17:fa1bba4c6053 | 107 | } |
AzureIoTClient | 17:fa1bba4c6053 | 108 | } |
AzureIoTClient | 17:fa1bba4c6053 | 109 | |
AzureIoTClient | 17:fa1bba4c6053 | 110 | |
AzureIoTClient | 17:fa1bba4c6053 | 111 | const METHODRETURN_DATA* MethodReturn_GetReturn(METHODRETURN_HANDLE handle) |
AzureIoTClient | 17:fa1bba4c6053 | 112 | { |
AzureIoTClient | 17:fa1bba4c6053 | 113 | const METHODRETURN_DATA* result; |
AzureIoTClient | 17:fa1bba4c6053 | 114 | if (handle == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 115 | { |
AzureIoTClient | 17:fa1bba4c6053 | 116 | /*Codes_SRS_METHODRETURN_02_010: [ If handle is NULL then MethodReturn_GetReturn shall fail and return NULL. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 117 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 118 | } |
AzureIoTClient | 17:fa1bba4c6053 | 119 | else |
AzureIoTClient | 17:fa1bba4c6053 | 120 | { |
AzureIoTClient | 17:fa1bba4c6053 | 121 | /*Codes_SRS_METHODRETURN_02_011: [ Otherwise, MethodReturn_GetReturn shall return a non-NULL const pointer to a METHODRETURN_DATA. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 122 | result = &(handle->data); |
AzureIoTClient | 17:fa1bba4c6053 | 123 | } |
AzureIoTClient | 17:fa1bba4c6053 | 124 | return result; |
AzureIoTClient | 17:fa1bba4c6053 | 125 | } |