Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Mon Jun 11 15:41:10 2018 -0700
Revision:
45:1119d0f2c4d8
Parent:
19:2e0811512ceb
Child:
48:81866008bba4
1.2.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 #include <stdlib.h>
AzureIoTClient 19:2e0811512ceb 5 #include <stdbool.h>
AzureIoTClient 19:2e0811512ceb 6 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 7 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 8 #include "azure_c_shared_utility/map.h"
Azure.IoT Build 0:fa2de1b79154 9 #include "azure_c_shared_utility/constmap.h"
Azure.IoT Build 6:c55b013dfc2a 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 11 #include "azure_c_shared_utility/refcount.h"
Azure.IoT Build 0:fa2de1b79154 12
Azure.IoT Build 0:fa2de1b79154 13 DEFINE_ENUM_STRINGS(CONSTMAP_RESULT, CONSTMAP_RESULT_VALUES);
Azure.IoT Build 0:fa2de1b79154 14
Azure.IoT Build 0:fa2de1b79154 15 typedef struct CONSTMAP_HANDLE_DATA_TAG
Azure.IoT Build 0:fa2de1b79154 16 {
Azure.IoT Build 0:fa2de1b79154 17 MAP_HANDLE map;
Azure.IoT Build 0:fa2de1b79154 18 } CONSTMAP_HANDLE_DATA;
Azure.IoT Build 0:fa2de1b79154 19
Azure.IoT Build 0:fa2de1b79154 20 DEFINE_REFCOUNT_TYPE(CONSTMAP_HANDLE_DATA);
Azure.IoT Build 0:fa2de1b79154 21
AzureIoTClient 1:9190c0f4d23a 22 #define LOG_CONSTMAP_ERROR(result) LogError("result = %s", ENUM_TO_STRING(CONSTMAP_RESULT, (result)));
Azure.IoT Build 0:fa2de1b79154 23
Azure.IoT Build 0:fa2de1b79154 24 CONSTMAP_HANDLE ConstMap_Create(MAP_HANDLE sourceMap)
Azure.IoT Build 0:fa2de1b79154 25 {
Azure.IoT Build 0:fa2de1b79154 26 CONSTMAP_HANDLE_DATA* result = REFCOUNT_TYPE_CREATE(CONSTMAP_HANDLE_DATA);
Azure.IoT Build 0:fa2de1b79154 27
AzureIoTClient 45:1119d0f2c4d8 28 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 29 {
AzureIoTClient 45:1119d0f2c4d8 30 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 31 }
AzureIoTClient 45:1119d0f2c4d8 32 else
AzureIoTClient 45:1119d0f2c4d8 33 {
AzureIoTClient 45:1119d0f2c4d8 34 /*Codes_SRS_CONSTMAP_17_048: [ConstMap_Create shall accept any non-NULL MAP_HANDLE as input.]*/
AzureIoTClient 45:1119d0f2c4d8 35 /*Codes_SRS_CONSTMAP_17_001: [ConstMap_Create shall create an immutable map, populated by the key, value pairs in the source map.]*/
Azure.IoT Build 0:fa2de1b79154 36 result->map = Map_Clone(sourceMap);
Azure.IoT Build 0:fa2de1b79154 37 if (result->map == NULL)
Azure.IoT Build 0:fa2de1b79154 38 {
Azure.IoT Build 0:fa2de1b79154 39 free(result);
AzureIoTClient 45:1119d0f2c4d8 40 /*Codes_SRS_CONSTMAP_17_002: [If during creation there are any errors, then ConstMap_Create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 41 result = NULL;
AzureIoTClient 45:1119d0f2c4d8 42 LOG_CONSTMAP_ERROR(CONSTMAP_ERROR);
Azure.IoT Build 0:fa2de1b79154 43 }
Azure.IoT Build 0:fa2de1b79154 44
Azure.IoT Build 0:fa2de1b79154 45 }
AzureIoTClient 45:1119d0f2c4d8 46 /*Codes_SRS_CONSTMAP_17_003: [Otherwise, it shall return a non-NULL handle that can be used in subsequent calls.]*/
Azure.IoT Build 0:fa2de1b79154 47 return (CONSTMAP_HANDLE)result;
Azure.IoT Build 0:fa2de1b79154 48 }
Azure.IoT Build 0:fa2de1b79154 49
Azure.IoT Build 0:fa2de1b79154 50 void ConstMap_Destroy(CONSTMAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 51 {
AzureIoTClient 45:1119d0f2c4d8 52 /*Codes_SRS_CONSTMAP_17_005: [If parameter handle is NULL then ConstMap_Destroy shall take no action.]*/
AzureIoTClient 45:1119d0f2c4d8 53 if (handle == NULL)
AzureIoTClient 45:1119d0f2c4d8 54 {
AzureIoTClient 45:1119d0f2c4d8 55 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 56 }
AzureIoTClient 45:1119d0f2c4d8 57 else
Azure.IoT Build 0:fa2de1b79154 58 {
AzureIoTClient 45:1119d0f2c4d8 59 /*Codes_SRS_CONSTMAP_17_049: [ConstMap_Destroy shall decrement the internal reference count of the immutable map.]*/
AzureIoTClient 45:1119d0f2c4d8 60 if (DEC_REF(CONSTMAP_HANDLE_DATA, handle) == DEC_RETURN_ZERO)
AzureIoTClient 45:1119d0f2c4d8 61 {
AzureIoTClient 45:1119d0f2c4d8 62 /*Codes_SRS_CONSTMAP_17_004: [If the reference count is zero, ConstMap_Destroy shall release all resources associated with the immutable map.]*/
AzureIoTClient 45:1119d0f2c4d8 63 Map_Destroy(((CONSTMAP_HANDLE_DATA *)handle)->map);
AzureIoTClient 45:1119d0f2c4d8 64 free(handle);
AzureIoTClient 45:1119d0f2c4d8 65 }
Azure.IoT Build 0:fa2de1b79154 66
Azure.IoT Build 0:fa2de1b79154 67 }
Azure.IoT Build 0:fa2de1b79154 68 }
Azure.IoT Build 0:fa2de1b79154 69
Azure.IoT Build 0:fa2de1b79154 70 CONSTMAP_HANDLE ConstMap_Clone(CONSTMAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 71 {
AzureIoTClient 45:1119d0f2c4d8 72 /*Codes_SRS_CONSTMAP_17_038: [ConstMap_Clone returns NULL if parameter handle is NULL.] */
AzureIoTClient 45:1119d0f2c4d8 73 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 74 {
AzureIoTClient 45:1119d0f2c4d8 75 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 76 }
AzureIoTClient 45:1119d0f2c4d8 77 else
AzureIoTClient 45:1119d0f2c4d8 78 {
AzureIoTClient 45:1119d0f2c4d8 79 /*Codes_SRS_CONSTMAP_17_039: [ConstMap_Clone shall increase the internal reference count of the immutable map indicated by parameter handle]*/
AzureIoTClient 45:1119d0f2c4d8 80 /*Codes_SRS_CONSTMAP_17_050: [ConstMap_Clone shall return the non-NULL handle. ]*/
AzureIoTClient 45:1119d0f2c4d8 81 INC_REF(CONSTMAP_HANDLE_DATA, handle);
Azure.IoT Build 0:fa2de1b79154 82 }
Azure.IoT Build 0:fa2de1b79154 83 return (handle);
Azure.IoT Build 0:fa2de1b79154 84 }
Azure.IoT Build 0:fa2de1b79154 85
Azure.IoT Build 0:fa2de1b79154 86 static CONSTMAP_RESULT ConstMap_ErrorConvert(MAP_RESULT mapResult)
Azure.IoT Build 0:fa2de1b79154 87 {
Azure.IoT Build 0:fa2de1b79154 88 CONSTMAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 89 switch (mapResult)
Azure.IoT Build 0:fa2de1b79154 90 {
Azure.IoT Build 0:fa2de1b79154 91 case MAP_OK:
Azure.IoT Build 0:fa2de1b79154 92 result = CONSTMAP_OK;
Azure.IoT Build 0:fa2de1b79154 93 break;
Azure.IoT Build 0:fa2de1b79154 94 case MAP_INVALIDARG:
Azure.IoT Build 0:fa2de1b79154 95 result = CONSTMAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 96 break;
Azure.IoT Build 0:fa2de1b79154 97 case MAP_KEYNOTFOUND:
Azure.IoT Build 0:fa2de1b79154 98 result = CONSTMAP_KEYNOTFOUND;
Azure.IoT Build 0:fa2de1b79154 99 break;
Azure.IoT Build 0:fa2de1b79154 100 default:
Azure.IoT Build 0:fa2de1b79154 101 result = CONSTMAP_ERROR;
Azure.IoT Build 0:fa2de1b79154 102 break;
Azure.IoT Build 0:fa2de1b79154 103 }
Azure.IoT Build 0:fa2de1b79154 104 return result;
Azure.IoT Build 0:fa2de1b79154 105 }
Azure.IoT Build 0:fa2de1b79154 106
Azure.IoT Build 0:fa2de1b79154 107 MAP_HANDLE ConstMap_CloneWriteable(CONSTMAP_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 108 {
AzureIoTClient 45:1119d0f2c4d8 109 MAP_HANDLE result = NULL;
AzureIoTClient 45:1119d0f2c4d8 110 if (handle == NULL)
AzureIoTClient 45:1119d0f2c4d8 111 {
AzureIoTClient 45:1119d0f2c4d8 112 /*Codes_SRS_CONSTMAP_17_051: [ConstMap_CloneWriteable returns NULL if parameter handle is NULL. ]*/
AzureIoTClient 45:1119d0f2c4d8 113 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 114 }
AzureIoTClient 45:1119d0f2c4d8 115 else
AzureIoTClient 45:1119d0f2c4d8 116 {
AzureIoTClient 45:1119d0f2c4d8 117 /*Codes_SRS_CONSTMAP_17_052: [ConstMap_CloneWriteable shall create a new, writeable map, populated by the key, value pairs in the parameter defined by handle.]*/
AzureIoTClient 45:1119d0f2c4d8 118 /*Codes_SRS_CONSTMAP_17_053: [If during cloning, any operation fails, then ConstMap_CloneWriteableap_Clone shall return NULL.]*/
AzureIoTClient 45:1119d0f2c4d8 119 /*Codes_SRS_CONSTMAP_17_054: [Otherwise, ConstMap_CloneWriteable shall return a non-NULL handle that can be used in subsequent calls.]*/
AzureIoTClient 45:1119d0f2c4d8 120 result = Map_Clone(((CONSTMAP_HANDLE_DATA *)handle)->map);
AzureIoTClient 45:1119d0f2c4d8 121 }
AzureIoTClient 45:1119d0f2c4d8 122 return result;
Azure.IoT Build 0:fa2de1b79154 123 }
Azure.IoT Build 0:fa2de1b79154 124
Azure.IoT Build 0:fa2de1b79154 125 bool ConstMap_ContainsKey(CONSTMAP_HANDLE handle, const char* key )
Azure.IoT Build 0:fa2de1b79154 126 {
AzureIoTClient 45:1119d0f2c4d8 127 bool keyExists = false;
Azure.IoT Build 0:fa2de1b79154 128 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 129 {
AzureIoTClient 45:1119d0f2c4d8 130 /*Codes_SRS_CONSTMAP_17_024: [If parameter handle or key are NULL then ConstMap_ContainsKey shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 131 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 132 }
Azure.IoT Build 0:fa2de1b79154 133 else
Azure.IoT Build 0:fa2de1b79154 134 {
AzureIoTClient 45:1119d0f2c4d8 135 if (key == NULL)
AzureIoTClient 45:1119d0f2c4d8 136 {
AzureIoTClient 45:1119d0f2c4d8 137 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 138 }
AzureIoTClient 45:1119d0f2c4d8 139 else
AzureIoTClient 45:1119d0f2c4d8 140 {
AzureIoTClient 45:1119d0f2c4d8 141 /*Codes_SRS_CONSTMAP_17_025: [Otherwise if a key exists then ConstMap_ContainsKey shall return true.]*/
AzureIoTClient 45:1119d0f2c4d8 142 MAP_RESULT mapResult = Map_ContainsKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key, &keyExists);
AzureIoTClient 45:1119d0f2c4d8 143 if (mapResult != MAP_OK)
AzureIoTClient 45:1119d0f2c4d8 144 {
AzureIoTClient 45:1119d0f2c4d8 145 /*Codes_SRS_CONSTMAP_17_026: [If a key doesn't exist, then ConstMap_ContainsKey shall return false.]*/
AzureIoTClient 45:1119d0f2c4d8 146 keyExists = false;
AzureIoTClient 45:1119d0f2c4d8 147 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult));
AzureIoTClient 45:1119d0f2c4d8 148 }
AzureIoTClient 45:1119d0f2c4d8 149 }
Azure.IoT Build 0:fa2de1b79154 150 }
Azure.IoT Build 0:fa2de1b79154 151 return keyExists;
Azure.IoT Build 0:fa2de1b79154 152 }
Azure.IoT Build 0:fa2de1b79154 153
Azure.IoT Build 0:fa2de1b79154 154 bool ConstMap_ContainsValue(CONSTMAP_HANDLE handle, const char* value)
Azure.IoT Build 0:fa2de1b79154 155 {
AzureIoTClient 45:1119d0f2c4d8 156 bool valueExists = false;
Azure.IoT Build 0:fa2de1b79154 157 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 158 {
AzureIoTClient 45:1119d0f2c4d8 159 /*Codes_SRS_CONSTMAP_17_027: [If parameter handle or value is NULL then ConstMap_ContainsValue shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 160 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 161 }
Azure.IoT Build 0:fa2de1b79154 162 else
Azure.IoT Build 0:fa2de1b79154 163 {
AzureIoTClient 45:1119d0f2c4d8 164 if (value == NULL)
AzureIoTClient 45:1119d0f2c4d8 165 {
AzureIoTClient 45:1119d0f2c4d8 166 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 167 }
AzureIoTClient 45:1119d0f2c4d8 168 else
AzureIoTClient 45:1119d0f2c4d8 169 {
AzureIoTClient 45:1119d0f2c4d8 170 /*Codes_SRS_CONSTMAP_17_028: [Otherwise, if a pair has its value equal to the parameter value, the ConstMap_ContainsValue shall return true.]*/
AzureIoTClient 45:1119d0f2c4d8 171 MAP_RESULT mapResult = Map_ContainsValue(((CONSTMAP_HANDLE_DATA *)handle)->map, value, &valueExists);
AzureIoTClient 45:1119d0f2c4d8 172 if (mapResult != MAP_OK)
AzureIoTClient 45:1119d0f2c4d8 173 {
AzureIoTClient 45:1119d0f2c4d8 174 /*Codes_SRS_CONSTMAP_17_029: [Otherwise, if such a does not exist, then ConstMap_ContainsValue shall return false.]*/
AzureIoTClient 45:1119d0f2c4d8 175 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult));
AzureIoTClient 45:1119d0f2c4d8 176 }
AzureIoTClient 45:1119d0f2c4d8 177 }
Azure.IoT Build 0:fa2de1b79154 178 }
Azure.IoT Build 0:fa2de1b79154 179 return valueExists;
Azure.IoT Build 0:fa2de1b79154 180 }
Azure.IoT Build 0:fa2de1b79154 181
Azure.IoT Build 0:fa2de1b79154 182 const char* ConstMap_GetValue(CONSTMAP_HANDLE handle, const char* key)
Azure.IoT Build 0:fa2de1b79154 183 {
AzureIoTClient 45:1119d0f2c4d8 184 const char* value = NULL;
Azure.IoT Build 0:fa2de1b79154 185
Azure.IoT Build 0:fa2de1b79154 186 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 187 {
AzureIoTClient 45:1119d0f2c4d8 188 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/
AzureIoTClient 45:1119d0f2c4d8 189 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 190 }
Azure.IoT Build 0:fa2de1b79154 191 else
Azure.IoT Build 0:fa2de1b79154 192 {
AzureIoTClient 45:1119d0f2c4d8 193 if (key == NULL)
AzureIoTClient 45:1119d0f2c4d8 194 {
AzureIoTClient 45:1119d0f2c4d8 195 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/
AzureIoTClient 45:1119d0f2c4d8 196 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
AzureIoTClient 45:1119d0f2c4d8 197 }
AzureIoTClient 45:1119d0f2c4d8 198 else
AzureIoTClient 45:1119d0f2c4d8 199 {
AzureIoTClient 45:1119d0f2c4d8 200 /*Codes_SRS_CONSTMAP_17_041: [If the key is not found, then ConstMap_GetValue returns NULL.]*/
AzureIoTClient 45:1119d0f2c4d8 201 /*Codes_SRS_CONSTMAP_17_042: [Otherwise, ConstMap_GetValue returns the key's value.]*/
AzureIoTClient 45:1119d0f2c4d8 202 value = Map_GetValueFromKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key);
AzureIoTClient 45:1119d0f2c4d8 203 }
Azure.IoT Build 0:fa2de1b79154 204 }
Azure.IoT Build 0:fa2de1b79154 205 return value;
Azure.IoT Build 0:fa2de1b79154 206 }
Azure.IoT Build 0:fa2de1b79154 207
Azure.IoT Build 0:fa2de1b79154 208 CONSTMAP_RESULT ConstMap_GetInternals(CONSTMAP_HANDLE handle, const char*const** keys, const char*const** values, size_t* count)
Azure.IoT Build 0:fa2de1b79154 209 {
Azure.IoT Build 0:fa2de1b79154 210 CONSTMAP_RESULT result;
Azure.IoT Build 0:fa2de1b79154 211 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 212 {
AzureIoTClient 45:1119d0f2c4d8 213 /*Codes_SRS_CONSTMAP_17_046: [If parameter handle, keys, values or count is NULL then ConstMap_GetInternals shall return CONSTMAP_INVALIDARG.]*/
Azure.IoT Build 0:fa2de1b79154 214 result = CONSTMAP_INVALIDARG;
Azure.IoT Build 0:fa2de1b79154 215 LOG_CONSTMAP_ERROR(result);
Azure.IoT Build 0:fa2de1b79154 216 }
Azure.IoT Build 0:fa2de1b79154 217 else
Azure.IoT Build 0:fa2de1b79154 218 {
AzureIoTClient 45:1119d0f2c4d8 219 /*Codes_SRS_CONSTMAP_17_043: [ConstMap_GetInternals shall produce in *keys a pointer to an array of const char* having all the keys stored so far by the map.]
AzureIoTClient 45:1119d0f2c4d8 220 *Codes_SRS_CONSTMAP_17_044: [ConstMap_GetInternals shall produce in *values a pointer to an array of const char* having all the values stored so far by the map.]
AzureIoTClient 45:1119d0f2c4d8 221 *Codes_SRS_CONSTMAP_17_045: [ ConstMap_GetInternals shall produce in *count the number of stored keys and values.]
AzureIoTClient 45:1119d0f2c4d8 222 */
Azure.IoT Build 0:fa2de1b79154 223 MAP_RESULT mapResult = Map_GetInternals(((CONSTMAP_HANDLE_DATA *)handle)->map, keys, values, count);
Azure.IoT Build 0:fa2de1b79154 224 result = ConstMap_ErrorConvert(mapResult);
Azure.IoT Build 0:fa2de1b79154 225 }
Azure.IoT Build 0:fa2de1b79154 226 return result;
AzureIoTClient 45:1119d0f2c4d8 227 }