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
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 #include <stdlib.h> 00005 #include <limits.h> 00006 00007 #include "azure_c_shared_utility/gballoc.h" 00008 #include "azure_c_shared_utility/xlogging.h" 00009 #include "azure_c_shared_utility/crt_abstractions.h" 00010 #include "azure_c_shared_utility/strings.h" 00011 #include "parson.h" 00012 00013 #define METHODRETURN_C 00014 #include "methodreturn.h" 00015 #undef METHODRETURN_C 00016 00017 typedef struct METHODRETURN_HANDLE_DATA_TAG 00018 { 00019 METHODRETURN_DATA data; 00020 }METHODRETURN_HANDLE_DATA; 00021 00022 bool is_json_present_and_unparsable(const char* jsonValue) 00023 { 00024 bool is_present_and_unparsable; 00025 if (jsonValue == NULL) 00026 { 00027 // Null json is not considered invalid here 00028 is_present_and_unparsable = false; 00029 } 00030 else 00031 { 00032 JSON_Value* temp = json_parse_string(jsonValue); 00033 if (temp == NULL) 00034 { 00035 is_present_and_unparsable = true; 00036 } 00037 else 00038 { 00039 json_value_free(temp); 00040 is_present_and_unparsable = false; 00041 } 00042 } 00043 return is_present_and_unparsable; 00044 } 00045 00046 METHODRETURN_HANDLE MethodReturn_Create(int statusCode, const char* jsonValue) 00047 { 00048 METHODRETURN_HANDLE result; 00049 /*Codes_SRS_METHODRETURN_02_009: [ If jsonValue is not a JSON value then MethodReturn_Create shall fail and return NULL. ]*/ 00050 if (is_json_present_and_unparsable(jsonValue)) 00051 { 00052 LogError("%s is not JSON", jsonValue); 00053 result = NULL; 00054 } 00055 else 00056 { 00057 result = (METHODRETURN_HANDLE_DATA*)malloc(sizeof(METHODRETURN_HANDLE_DATA)); 00058 if (result == NULL) 00059 { 00060 /*Codes_SRS_METHODRETURN_02_002: [ If any failure is encountered then MethodReturn_Create shall return NULL ]*/ 00061 LogError("unable to malloc"); 00062 /*return as is*/ 00063 } 00064 else 00065 { 00066 if (jsonValue == NULL) 00067 { 00068 /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/ 00069 result->data.jsonValue = NULL; 00070 result->data.statusCode = statusCode; 00071 } 00072 else 00073 { 00074 if (mallocAndStrcpy_s(&(result->data.jsonValue), jsonValue) != 0) 00075 { 00076 LogError("failure in mallocAndStrcpy_s"); 00077 free(result); 00078 result = NULL; 00079 } 00080 else 00081 { 00082 /*Codes_SRS_METHODRETURN_02_001: [ MethodReturn_Create shall create a non-NULL handle containing statusCode and a clone of jsonValue. ]*/ 00083 result->data.statusCode = statusCode; 00084 } 00085 } 00086 } 00087 } 00088 00089 return result; 00090 } 00091 00092 void MethodReturn_Destroy(METHODRETURN_HANDLE handle) 00093 { 00094 if (handle == NULL) 00095 { 00096 /*Codes_SRS_METHODRETURN_02_003: [ If handle is NULL then MethodReturn_Destroy shall return. ]*/ 00097 LogError("invalid argument METHODRETURN_HANDLE handle=%p", handle); 00098 } 00099 else 00100 { 00101 /*Codes_SRS_METHODRETURN_02_004: [ Otherwise, MethodReturn_Destroy shall free all used resources by handle. ]*/ 00102 if (handle->data.jsonValue != NULL) 00103 { 00104 free(handle->data.jsonValue); 00105 } 00106 free(handle); 00107 } 00108 } 00109 00110 00111 const METHODRETURN_DATA* MethodReturn_GetReturn(METHODRETURN_HANDLE handle) 00112 { 00113 const METHODRETURN_DATA* result; 00114 if (handle == NULL) 00115 { 00116 /*Codes_SRS_METHODRETURN_02_010: [ If handle is NULL then MethodReturn_GetReturn shall fail and return NULL. ]*/ 00117 result = NULL; 00118 } 00119 else 00120 { 00121 /*Codes_SRS_METHODRETURN_02_011: [ Otherwise, MethodReturn_GetReturn shall return a non-NULL const pointer to a METHODRETURN_DATA. ]*/ 00122 result = &(handle->data); 00123 } 00124 return result; 00125 }
Generated on Tue Jul 12 2022 19:56:37 by
1.7.2