Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more
methodreturn.c@17:fa1bba4c6053, 2016-11-16 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Wed Nov 16 21:38:26 2016 -0800
- Revision:
- 17:fa1bba4c6053
- Child:
- 21:6d3dea1abd9c
1.0.10
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 | #ifdef _CRTDBG_MAP_ALLOC |
| AzureIoTClient | 17:fa1bba4c6053 | 6 | #include <crtdbg.h> |
| AzureIoTClient | 17:fa1bba4c6053 | 7 | #endif |
| AzureIoTClient | 17:fa1bba4c6053 | 8 | |
| AzureIoTClient | 17:fa1bba4c6053 | 9 | #include <limits.h> |
| AzureIoTClient | 17:fa1bba4c6053 | 10 | |
| AzureIoTClient | 17:fa1bba4c6053 | 11 | #include "azure_c_shared_utility/gballoc.h" |
| AzureIoTClient | 17:fa1bba4c6053 | 12 | #include "azure_c_shared_utility/xlogging.h" |
| AzureIoTClient | 17:fa1bba4c6053 | 13 | #include "azure_c_shared_utility/crt_abstractions.h" |
| AzureIoTClient | 17:fa1bba4c6053 | 14 | #include "azure_c_shared_utility/strings.h" |
| AzureIoTClient | 17:fa1bba4c6053 | 15 | #include "parson.h" |
| AzureIoTClient | 17:fa1bba4c6053 | 16 | |
| AzureIoTClient | 17:fa1bba4c6053 | 17 | #define METHODRETURN_C |
| AzureIoTClient | 17:fa1bba4c6053 | 18 | #include "methodreturn.h" |
| AzureIoTClient | 17:fa1bba4c6053 | 19 | #undef METHODRETURN_C |
| AzureIoTClient | 17:fa1bba4c6053 | 20 | |
| AzureIoTClient | 17:fa1bba4c6053 | 21 | typedef struct METHODRETURN_HANDLE_DATA_TAG |
| AzureIoTClient | 17:fa1bba4c6053 | 22 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 23 | METHODRETURN_DATA data; |
| AzureIoTClient | 17:fa1bba4c6053 | 24 | }METHODRETURN_HANDLE_DATA; |
| AzureIoTClient | 17:fa1bba4c6053 | 25 | |
| AzureIoTClient | 17:fa1bba4c6053 | 26 | METHODRETURN_HANDLE MethodReturn_Create(int statusCode, const char* jsonValue) |
| AzureIoTClient | 17:fa1bba4c6053 | 27 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 28 | METHODRETURN_HANDLE result; |
| AzureIoTClient | 17:fa1bba4c6053 | 29 | JSON_Value* temp; |
| AzureIoTClient | 17:fa1bba4c6053 | 30 | /*Codes_SRS_METHODRETURN_02_009: [ If jsonValue is not a JSON value then MethodReturn_Create shall fail and return NULL. ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 31 | if ((jsonValue != NULL) && ((temp = json_parse_string(jsonValue), ((temp != NULL) ? json_value_free(temp) : (void)NULL), temp) == NULL)) |
| AzureIoTClient | 17:fa1bba4c6053 | 32 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 33 | LogError("%s is not JSON", jsonValue); |
| AzureIoTClient | 17:fa1bba4c6053 | 34 | result = NULL; |
| AzureIoTClient | 17:fa1bba4c6053 | 35 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 36 | else |
| AzureIoTClient | 17:fa1bba4c6053 | 37 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 38 | result = (METHODRETURN_HANDLE_DATA*)malloc(sizeof(METHODRETURN_HANDLE_DATA)); |
| AzureIoTClient | 17:fa1bba4c6053 | 39 | if (result == NULL) |
| AzureIoTClient | 17:fa1bba4c6053 | 40 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 41 | /*Codes_SRS_METHODRETURN_02_002: [ If any failure is encountered then MethodReturn_Create shall return NULL ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 42 | LogError("unable to malloc"); |
| AzureIoTClient | 17:fa1bba4c6053 | 43 | /*return as is*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 44 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 45 | else |
| AzureIoTClient | 17:fa1bba4c6053 | 46 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 47 | if (jsonValue == NULL) |
| AzureIoTClient | 17:fa1bba4c6053 | 48 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 49 | result->data.jsonValue = NULL; |
| AzureIoTClient | 17:fa1bba4c6053 | 50 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 51 | else |
| AzureIoTClient | 17:fa1bba4c6053 | 52 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 53 | if (mallocAndStrcpy_s(&(result->data.jsonValue), jsonValue) != 0) |
| AzureIoTClient | 17:fa1bba4c6053 | 54 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 55 | LogError("failure in mallocAndStrcpy_s"); |
| AzureIoTClient | 17:fa1bba4c6053 | 56 | free(result); |
| AzureIoTClient | 17:fa1bba4c6053 | 57 | result = NULL; |
| AzureIoTClient | 17:fa1bba4c6053 | 58 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 59 | else |
| AzureIoTClient | 17:fa1bba4c6053 | 60 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 61 | /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 62 | result->data.statusCode = statusCode; |
| AzureIoTClient | 17:fa1bba4c6053 | 63 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 64 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 65 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 66 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 67 | |
| AzureIoTClient | 17:fa1bba4c6053 | 68 | return result; |
| AzureIoTClient | 17:fa1bba4c6053 | 69 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 70 | |
| AzureIoTClient | 17:fa1bba4c6053 | 71 | void MethodReturn_Destroy(METHODRETURN_HANDLE handle) |
| AzureIoTClient | 17:fa1bba4c6053 | 72 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 73 | if (handle == NULL) |
| AzureIoTClient | 17:fa1bba4c6053 | 74 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 75 | /*Codes_SRS_METHODRETURN_02_003: [ If handle is NULL then MethodReturn_Destroy shall return. ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 76 | LogError("invalid argument METHODRETURN_HANDLE handle=%p", handle); |
| AzureIoTClient | 17:fa1bba4c6053 | 77 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 78 | else |
| AzureIoTClient | 17:fa1bba4c6053 | 79 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 80 | /*Codes_SRS_METHODRETURN_02_004: [ Otherwise, MethodReturn_Destroy shall free all used resources by handle. ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 81 | if (handle->data.jsonValue != NULL) |
| AzureIoTClient | 17:fa1bba4c6053 | 82 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 83 | free(handle->data.jsonValue); |
| AzureIoTClient | 17:fa1bba4c6053 | 84 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 85 | free(handle); |
| AzureIoTClient | 17:fa1bba4c6053 | 86 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 87 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 88 | |
| AzureIoTClient | 17:fa1bba4c6053 | 89 | |
| AzureIoTClient | 17:fa1bba4c6053 | 90 | const METHODRETURN_DATA* MethodReturn_GetReturn(METHODRETURN_HANDLE handle) |
| AzureIoTClient | 17:fa1bba4c6053 | 91 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 92 | const METHODRETURN_DATA* result; |
| AzureIoTClient | 17:fa1bba4c6053 | 93 | if (handle == NULL) |
| AzureIoTClient | 17:fa1bba4c6053 | 94 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 95 | /*Codes_SRS_METHODRETURN_02_010: [ If handle is NULL then MethodReturn_GetReturn shall fail and return NULL. ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 96 | result = NULL; |
| AzureIoTClient | 17:fa1bba4c6053 | 97 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 98 | else |
| AzureIoTClient | 17:fa1bba4c6053 | 99 | { |
| AzureIoTClient | 17:fa1bba4c6053 | 100 | /*Codes_SRS_METHODRETURN_02_011: [ Otherwise, MethodReturn_GetReturn shall return a non-NULL const pointer to a METHODRETURN_DATA. ]*/ |
| AzureIoTClient | 17:fa1bba4c6053 | 101 | result = &(handle->data); |
| AzureIoTClient | 17:fa1bba4c6053 | 102 | } |
| AzureIoTClient | 17:fa1bba4c6053 | 103 | return result; |
| AzureIoTClient | 17:fa1bba4c6053 | 104 | } |
