Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
wiggly
Date:
Thu Aug 24 14:14:15 2017 +0100
Revision:
34:651c23af382c
Parent:
19:2e0811512ceb
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code

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
Azure.IoT Build 0:fa2de1b79154 28 if (result == NULL)
Azure.IoT Build 0:fa2de1b79154 29 {
Azure.IoT Build 0:fa2de1b79154 30 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 31 }
Azure.IoT Build 0:fa2de1b79154 32 else
Azure.IoT Build 0:fa2de1b79154 33 {
Azure.IoT Build 0:fa2de1b79154 34 /*Codes_SRS_CONSTMAP_17_048: [ConstMap_Create shall accept any non-NULL MAP_HANDLE as input.]*/
Azure.IoT Build 0:fa2de1b79154 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);
Azure.IoT Build 0:fa2de1b79154 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;
Azure.IoT Build 0:fa2de1b79154 42 LOG_CONSTMAP_ERROR(CONSTMAP_ERROR);
Azure.IoT Build 0:fa2de1b79154 43 }
Azure.IoT Build 0:fa2de1b79154 44
Azure.IoT Build 0:fa2de1b79154 45 }
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 52 /*Codes_SRS_CONSTMAP_17_005: [If parameter handle is NULL then ConstMap_Destroy shall take no action.]*/
Azure.IoT Build 0:fa2de1b79154 53 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 54 {
Azure.IoT Build 0:fa2de1b79154 55 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 56 }
Azure.IoT Build 0:fa2de1b79154 57 else
Azure.IoT Build 0:fa2de1b79154 58 {
Azure.IoT Build 0:fa2de1b79154 59 /*Codes_SRS_CONSTMAP_17_049: [ConstMap_Destroy shall decrement the internal reference count of the immutable map.]*/
Azure.IoT Build 0:fa2de1b79154 60 if (DEC_REF(CONSTMAP_HANDLE_DATA, handle) == DEC_RETURN_ZERO)
Azure.IoT Build 0:fa2de1b79154 61 {
Azure.IoT Build 0:fa2de1b79154 62 /*Codes_SRS_CONSTMAP_17_004: [If the reference count is zero, ConstMap_Destroy shall release all resources associated with the immutable map.]*/
Azure.IoT Build 0:fa2de1b79154 63 Map_Destroy(((CONSTMAP_HANDLE_DATA *)handle)->map);
Azure.IoT Build 0:fa2de1b79154 64 free(handle);
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 72 /*Codes_SRS_CONSTMAP_17_038: [ConstMap_Clone returns NULL if parameter handle is NULL.] */
Azure.IoT Build 0:fa2de1b79154 73 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 74 {
Azure.IoT Build 0:fa2de1b79154 75 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 76 }
Azure.IoT Build 0:fa2de1b79154 77 else
Azure.IoT Build 0:fa2de1b79154 78 {
Azure.IoT Build 0:fa2de1b79154 79 /*Codes_SRS_CONSTMAP_17_039: [ConstMap_Clone shall increase the internal reference count of the immutable map indicated by parameter handle]*/
Azure.IoT Build 0:fa2de1b79154 80 /*Codes_SRS_CONSTMAP_17_050: [ConstMap_Clone shall return the non-NULL handle. ]*/
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 109 MAP_HANDLE result = NULL;
Azure.IoT Build 0:fa2de1b79154 110 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 111 {
Azure.IoT Build 0:fa2de1b79154 112 /*Codes_SRS_CONSTMAP_17_051: [ConstMap_CloneWriteable returns NULL if parameter handle is NULL. ]*/
Azure.IoT Build 0:fa2de1b79154 113 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 114 }
Azure.IoT Build 0:fa2de1b79154 115 else
Azure.IoT Build 0:fa2de1b79154 116 {
Azure.IoT Build 0:fa2de1b79154 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.]*/
Azure.IoT Build 0:fa2de1b79154 118 /*Codes_SRS_CONSTMAP_17_053: [If during cloning, any operation fails, then ConstMap_CloneWriteableap_Clone shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 119 /*Codes_SRS_CONSTMAP_17_054: [Otherwise, ConstMap_CloneWriteable shall return a non-NULL handle that can be used in subsequent calls.]*/
Azure.IoT Build 0:fa2de1b79154 120 result = Map_Clone(((CONSTMAP_HANDLE_DATA *)handle)->map);
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 127 bool keyExists = false;
Azure.IoT Build 0:fa2de1b79154 128 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 129 {
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 135 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 136 {
Azure.IoT Build 0:fa2de1b79154 137 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 138 }
Azure.IoT Build 0:fa2de1b79154 139 else
Azure.IoT Build 0:fa2de1b79154 140 {
Azure.IoT Build 0:fa2de1b79154 141 /*Codes_SRS_CONSTMAP_17_025: [Otherwise if a key exists then ConstMap_ContainsKey shall return true.]*/
Azure.IoT Build 0:fa2de1b79154 142 MAP_RESULT mapResult = Map_ContainsKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key, &keyExists);
Azure.IoT Build 0:fa2de1b79154 143 if (mapResult != MAP_OK)
Azure.IoT Build 0:fa2de1b79154 144 {
Azure.IoT Build 0:fa2de1b79154 145 /*Codes_SRS_CONSTMAP_17_026: [If a key doesn't exist, then ConstMap_ContainsKey shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 146 keyExists = false;
Azure.IoT Build 0:fa2de1b79154 147 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult));
Azure.IoT Build 0:fa2de1b79154 148 }
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 156 bool valueExists = false;
Azure.IoT Build 0:fa2de1b79154 157 if (handle == NULL)
Azure.IoT Build 0:fa2de1b79154 158 {
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 164 if (value == NULL)
Azure.IoT Build 0:fa2de1b79154 165 {
Azure.IoT Build 0:fa2de1b79154 166 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 167 }
Azure.IoT Build 0:fa2de1b79154 168 else
Azure.IoT Build 0:fa2de1b79154 169 {
Azure.IoT Build 0:fa2de1b79154 170 /*Codes_SRS_CONSTMAP_17_028: [Otherwise, if a pair has its value equal to the parameter value, the ConstMap_ContainsValue shall return true.]*/
Azure.IoT Build 0:fa2de1b79154 171 MAP_RESULT mapResult = Map_ContainsValue(((CONSTMAP_HANDLE_DATA *)handle)->map, value, &valueExists);
Azure.IoT Build 0:fa2de1b79154 172 if (mapResult != MAP_OK)
Azure.IoT Build 0:fa2de1b79154 173 {
Azure.IoT Build 0:fa2de1b79154 174 /*Codes_SRS_CONSTMAP_17_029: [Otherwise, if such a does not exist, then ConstMap_ContainsValue shall return false.]*/
Azure.IoT Build 0:fa2de1b79154 175 LOG_CONSTMAP_ERROR(ConstMap_ErrorConvert(mapResult));
Azure.IoT Build 0:fa2de1b79154 176 }
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 188 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 193 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 194 {
Azure.IoT Build 0:fa2de1b79154 195 /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 196 LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
Azure.IoT Build 0:fa2de1b79154 197 }
Azure.IoT Build 0:fa2de1b79154 198 else
Azure.IoT Build 0:fa2de1b79154 199 {
Azure.IoT Build 0:fa2de1b79154 200 /*Codes_SRS_CONSTMAP_17_041: [If the key is not found, then ConstMap_GetValue returns NULL.]*/
Azure.IoT Build 0:fa2de1b79154 201 /*Codes_SRS_CONSTMAP_17_042: [Otherwise, ConstMap_GetValue returns the key's value.]*/
Azure.IoT Build 0:fa2de1b79154 202 value = Map_GetValueFromKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key);
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 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 {
Azure.IoT Build 0:fa2de1b79154 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.]
Azure.IoT Build 0:fa2de1b79154 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.]
Azure.IoT Build 0:fa2de1b79154 221 *Codes_SRS_CONSTMAP_17_045: [ ConstMap_GetInternals shall produce in *count the number of stored keys and values.]
Azure.IoT Build 0:fa2de1b79154 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;
Azure.IoT Build 0:fa2de1b79154 227 }