Mark Radbourne / Mbed 2 deprecated iothub_client_sample_amqp

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers optionhandler.c Source File

optionhandler.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 "azure_c_shared_utility/optionhandler.h"
00005 #include "azure_c_shared_utility/xlogging.h"
00006 #include "azure_c_shared_utility/gballoc.h"
00007 #include "azure_c_shared_utility/vector.h"
00008 
00009 typedef struct OPTION_TAG
00010 {
00011     const char* name;
00012     void* storage;
00013 }OPTION;
00014 
00015 typedef struct OPTIONHANDLER_HANDLE_DATA_TAG
00016 {
00017     pfCloneOption cloneOption;
00018     pfDestroyOption destroyOption;
00019     pfSetOption setOption;
00020     VECTOR_HANDLE storage;
00021 }OPTIONHANDLER_HANDLE_DATA;
00022 
00023 OPTIONHANDLER_HANDLE OptionHandler_Create(pfCloneOption cloneOption, pfDestroyOption destroyOption, pfSetOption setOption)
00024 {
00025     /*Codes_SRS_OPTIONHANDLER_02_001: [ OptionHandler_Create shall fail and retun NULL if any parameters are NULL. ]*/
00026     OPTIONHANDLER_HANDLE_DATA* result;
00027     if (
00028         (cloneOption == NULL) ||
00029         (destroyOption == NULL) ||
00030         (setOption == NULL)
00031         )
00032     {
00033         LogError("invalid parameter = pfCloneOption cloneOption=%p, pfDestroyOption destroyOption=%p, pfSetOption setOption=%p", cloneOption, destroyOption, setOption);
00034         result = NULL;
00035     }
00036     else
00037     {
00038         result = (OPTIONHANDLER_HANDLE_DATA*)malloc(sizeof(OPTIONHANDLER_HANDLE_DATA));
00039         if (result == NULL)
00040         {
00041             /*Codes_SRS_OPTIONHANDLER_02_004: [ Otherwise, OptionHandler_Create shall fail and return NULL. ]*/
00042             LogError("unable to malloc");
00043             /*return as is*/
00044         }
00045         else
00046         {
00047             /*Codes_SRS_OPTIONHANDLER_02_002: [ OptionHandler_Create shall create an empty VECTOR that will hold pairs of const char* and void*. ]*/
00048             result->storage = VECTOR_create(sizeof(OPTION));
00049             if (result->storage == NULL)
00050             {
00051                 /*Codes_SRS_OPTIONHANDLER_02_004: [ Otherwise, OptionHandler_Create shall fail and return NULL. ]*/
00052                 LogError("unable to VECTOR_create");
00053                 free(result);
00054                 result= NULL;
00055             }
00056             else
00057             {
00058                 /*Codes_SRS_OPTIONHANDLER_02_003: [ If all the operations succeed then OptionHandler_Create shall succeed and return a non-NULL handle. ]*/
00059                 result->cloneOption = cloneOption;
00060                 result->destroyOption = destroyOption;
00061                 result->setOption = setOption;
00062                 /*return as is*/
00063             }
00064         }
00065     }
00066     return result;
00067 
00068 }
00069 
00070 OPTIONHANDLER_RESULT OptionHandler_AddOption(OPTIONHANDLER_HANDLE handle, const char* name, const void* value)
00071 {
00072     OPTIONHANDLER_RESULT result;
00073     /*Codes_SRS_OPTIONHANDLER_02_001: [ OptionHandler_Create shall fail and retun NULL if any parameters are NULL. ]*/
00074     if (
00075         (handle == NULL) ||
00076         (name == NULL) ||
00077         (value == NULL)
00078         )
00079     {
00080         LogError("invalid arguments: OPTIONHANDLER_HANDLE handle=%p, const char* name=%p, void* value=%p", handle, name, value);
00081         result= OPTIONHANDLER_INVALIDARG;
00082     }
00083     else
00084     {
00085         const char* cloneOfName;
00086         if (mallocAndStrcpy_s((char**)&cloneOfName, name) != 0)
00087         {
00088             /*Codes_SRS_OPTIONHANDLER_02_009: [ Otherwise, OptionHandler_AddProperty shall succeed and return OPTIONHANDLER_ERROR. ]*/
00089             LogError("unable to clone name");
00090             result = OPTIONHANDLER_ERROR;
00091         }
00092         else 
00093         {
00094             /*Codes_SRS_OPTIONHANDLER_02_006: [ OptionHandler_AddProperty shall call pfCloneOption passing name and value. ]*/
00095             void* cloneOfValue = handle->cloneOption(name, value);
00096             if (cloneOfValue == NULL)
00097             {
00098                 /*Codes_SRS_OPTIONHANDLER_02_009: [ Otherwise, OptionHandler_AddProperty shall succeed and return OPTIONHANDLER_ERROR. ]*/
00099                 LogError("unable to clone value");
00100                 free((void*)cloneOfName);
00101                 result = OPTIONHANDLER_ERROR;
00102             }
00103             else
00104             {
00105                 OPTION temp;
00106                 temp.name = cloneOfName;
00107                 temp.storage = cloneOfValue;
00108                 /*Codes_SRS_OPTIONHANDLER_02_007: [ OptionHandler_AddProperty shall use VECTOR APIs to save the name and the newly created clone of value. ]*/
00109                 if (VECTOR_push_back(handle->storage, &temp, 1) != 0)
00110                 {
00111                     /*Codes_SRS_OPTIONHANDLER_02_009: [ Otherwise, OptionHandler_AddProperty shall succeed and return OPTIONHANDLER_ERROR. ]*/
00112                     LogError("unable to VECTOR_push_back");
00113                     handle->destroyOption(name, cloneOfValue);
00114                     free((void*)cloneOfName);
00115                     result = OPTIONHANDLER_ERROR;
00116                 }
00117                 else
00118                 {
00119                     /*Codes_SRS_OPTIONHANDLER_02_008: [ If all the operations succed then OptionHandler_AddProperty shall succeed and return OPTIONHANDLER_OK. ]*/
00120                     result = OPTIONHANDLER_OK;
00121                 }
00122             }
00123         }
00124     }
00125     return result;
00126 }
00127 
00128 OPTIONHANDLER_RESULT OptionHandler_FeedOptions(OPTIONHANDLER_HANDLE handle, void* destinationHandle)
00129 {
00130     OPTIONHANDLER_RESULT result;
00131     /*Codes_SRS_OPTIONHANDLER_02_010: [ OptionHandler_FeedOptions shall fail and return OPTIONHANDLER_INVALIDARG if any argument is NULL. ]*/
00132     if (
00133         (handle == NULL) ||
00134         (destinationHandle == NULL)
00135         )
00136     {
00137         LogError("invalid arguments OPTIONHANDLER_HANDLE handle=%p, void* destinationHandle=%p", handle, destinationHandle);
00138         result = OPTIONHANDLER_INVALIDARG;
00139     }
00140     else
00141     {
00142         /*Codes_SRS_OPTIONHANDLER_02_011: [ Otherwise, OptionHandler_FeedOptions shall use VECTOR's iteration mechanisms to retrieve pairs of name, value (const char* and void*). ]*/
00143         size_t nOptions = VECTOR_size(handle->storage), i;
00144         for (i = 0;i < nOptions;i++)
00145         {
00146             OPTION* option = (OPTION*)VECTOR_element(handle->storage, i);
00147             /*Codes_SRS_OPTIONHANDLER_02_012: [ OptionHandler_FeedOptions shall call for every pair of name,value setOption passing destinationHandle, name and value. ]*/
00148             if (handle->setOption(destinationHandle, option->name, option->storage) != 0)
00149             {
00150                 LogError("failure while trying to _SetOption");
00151                 break;
00152             }
00153         }
00154             
00155         if (i == nOptions)
00156         {
00157             /*Codes_SRS_OPTIONHANDLER_02_014: [ Otherwise, OptionHandler_FeedOptions shall fail and return OPTIONHANDLER_ERROR. ]*/
00158             result = OPTIONHANDLER_OK;
00159         }
00160         else
00161         {
00162             /*Codes_SRS_OPTIONHANDLER_02_013: [ If all the operations succeed then OptionHandler_FeedOptions shall succeed and return OPTIONHANDLER_OK. ]*/
00163             result = OPTIONHANDLER_ERROR;
00164         }
00165     }
00166     return result;
00167 }
00168 
00169 void OptionHandler_Destroy(OPTIONHANDLER_HANDLE handle)
00170 {   
00171     /*Codes_SRS_OPTIONHANDLER_02_015: [ OptionHandler_Destroy shall do nothing if parameter handle is NULL. ]*/
00172     if (handle == NULL)
00173     {
00174         LogError("invalid argument OPTIONHANDLER_HANDLE handle=%p", handle);
00175     }
00176     else
00177     {
00178         /*Codes_SRS_OPTIONHANDLER_02_016: [ Otherwise, OptionHandler_Destroy shall free all used resources. ]*/
00179         size_t nOptions = VECTOR_size(handle->storage), i;
00180         for (i = 0;i < nOptions;i++)
00181         {
00182             OPTION* option = (OPTION*)VECTOR_element(handle->storage, i);
00183             handle->destroyOption(option->name, option->storage);
00184             free((void*)option->name);
00185         }
00186 
00187         VECTOR_destroy(handle->storage);
00188         free(handle);
00189     }
00190 }