Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:01:41 2017 -0800
Revision:
21:b92006c5b9ff
Parent:
19:2e0811512ceb
Child:
30:ce3813c5a692
1.1.8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 13:920e00014ee3 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 13:920e00014ee3 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 13:920e00014ee3 3
AzureIoTClient 13:920e00014ee3 4 #include <stdlib.h>
AzureIoTClient 13:920e00014ee3 5 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 13:920e00014ee3 6 #include "azure_c_shared_utility/singlylinkedlist.h"
AzureIoTClient 21:b92006c5b9ff 7 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 13:920e00014ee3 8
AzureIoTClient 13:920e00014ee3 9 typedef struct LIST_ITEM_INSTANCE_TAG
AzureIoTClient 13:920e00014ee3 10 {
AzureIoTClient 13:920e00014ee3 11 const void* item;
AzureIoTClient 13:920e00014ee3 12 void* next;
AzureIoTClient 13:920e00014ee3 13 } LIST_ITEM_INSTANCE;
AzureIoTClient 13:920e00014ee3 14
AzureIoTClient 13:920e00014ee3 15 typedef struct SINGLYLINKEDLIST_INSTANCE_TAG
AzureIoTClient 13:920e00014ee3 16 {
AzureIoTClient 13:920e00014ee3 17 LIST_ITEM_INSTANCE* head;
AzureIoTClient 13:920e00014ee3 18 } LIST_INSTANCE;
AzureIoTClient 13:920e00014ee3 19
AzureIoTClient 13:920e00014ee3 20 SINGLYLINKEDLIST_HANDLE singlylinkedlist_create(void)
AzureIoTClient 13:920e00014ee3 21 {
AzureIoTClient 13:920e00014ee3 22 LIST_INSTANCE* result;
AzureIoTClient 13:920e00014ee3 23
AzureIoTClient 13:920e00014ee3 24 /* Codes_SRS_LIST_01_001: [singlylinkedlist_create shall create a new list and return a non-NULL handle on success.] */
AzureIoTClient 13:920e00014ee3 25 result = (LIST_INSTANCE*)malloc(sizeof(LIST_INSTANCE));
AzureIoTClient 13:920e00014ee3 26 if (result != NULL)
AzureIoTClient 13:920e00014ee3 27 {
AzureIoTClient 13:920e00014ee3 28 /* Codes_SRS_LIST_01_002: [If any error occurs during the list creation, singlylinkedlist_create shall return NULL.] */
AzureIoTClient 13:920e00014ee3 29 result->head = NULL;
AzureIoTClient 13:920e00014ee3 30 }
AzureIoTClient 13:920e00014ee3 31
AzureIoTClient 13:920e00014ee3 32 return result;
AzureIoTClient 13:920e00014ee3 33 }
AzureIoTClient 13:920e00014ee3 34
AzureIoTClient 13:920e00014ee3 35 void singlylinkedlist_destroy(SINGLYLINKEDLIST_HANDLE list)
AzureIoTClient 13:920e00014ee3 36 {
AzureIoTClient 13:920e00014ee3 37 /* Codes_SRS_LIST_01_004: [If the list argument is NULL, no freeing of resources shall occur.] */
AzureIoTClient 13:920e00014ee3 38 if (list != NULL)
AzureIoTClient 13:920e00014ee3 39 {
AzureIoTClient 13:920e00014ee3 40 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 41
AzureIoTClient 13:920e00014ee3 42 while (list_instance->head != NULL)
AzureIoTClient 13:920e00014ee3 43 {
AzureIoTClient 13:920e00014ee3 44 LIST_ITEM_INSTANCE* current_item = list_instance->head;
AzureIoTClient 13:920e00014ee3 45 list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 13:920e00014ee3 46 free(current_item);
AzureIoTClient 13:920e00014ee3 47 }
AzureIoTClient 13:920e00014ee3 48
AzureIoTClient 13:920e00014ee3 49 /* Codes_SRS_LIST_01_003: [singlylinkedlist_destroy shall free all resources associated with the list identified by the handle argument.] */
AzureIoTClient 13:920e00014ee3 50 free(list_instance);
AzureIoTClient 13:920e00014ee3 51 }
AzureIoTClient 13:920e00014ee3 52 }
AzureIoTClient 13:920e00014ee3 53
AzureIoTClient 13:920e00014ee3 54 LIST_ITEM_HANDLE singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item)
AzureIoTClient 13:920e00014ee3 55 {
AzureIoTClient 13:920e00014ee3 56 LIST_ITEM_INSTANCE* result;
AzureIoTClient 13:920e00014ee3 57
AzureIoTClient 13:920e00014ee3 58 /* Codes_SRS_LIST_01_006: [If any of the arguments is NULL, singlylinkedlist_add shall not add the item to the list and return NULL.] */
AzureIoTClient 13:920e00014ee3 59 if ((list == NULL) ||
AzureIoTClient 13:920e00014ee3 60 (item == NULL))
AzureIoTClient 13:920e00014ee3 61 {
AzureIoTClient 13:920e00014ee3 62 result = NULL;
AzureIoTClient 13:920e00014ee3 63 }
AzureIoTClient 13:920e00014ee3 64 else
AzureIoTClient 13:920e00014ee3 65 {
AzureIoTClient 13:920e00014ee3 66 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 67 result = (LIST_ITEM_INSTANCE*)malloc(sizeof(LIST_ITEM_INSTANCE));
AzureIoTClient 13:920e00014ee3 68
AzureIoTClient 13:920e00014ee3 69 if (result == NULL)
AzureIoTClient 13:920e00014ee3 70 {
AzureIoTClient 13:920e00014ee3 71 /* Codes_SRS_LIST_01_007: [If allocating the new list node fails, singlylinkedlist_add shall return NULL.] */
AzureIoTClient 13:920e00014ee3 72 result = NULL;
AzureIoTClient 13:920e00014ee3 73 }
AzureIoTClient 13:920e00014ee3 74 else
AzureIoTClient 13:920e00014ee3 75 {
AzureIoTClient 13:920e00014ee3 76 /* Codes_SRS_LIST_01_005: [singlylinkedlist_add shall add one item to the tail of the list and on success it shall return a handle to the added item.] */
AzureIoTClient 13:920e00014ee3 77 result->next = NULL;
AzureIoTClient 13:920e00014ee3 78 result->item = item;
AzureIoTClient 13:920e00014ee3 79
AzureIoTClient 13:920e00014ee3 80 if (list_instance->head == NULL)
AzureIoTClient 13:920e00014ee3 81 {
AzureIoTClient 13:920e00014ee3 82 list_instance->head = result;
AzureIoTClient 13:920e00014ee3 83 }
AzureIoTClient 13:920e00014ee3 84 else
AzureIoTClient 13:920e00014ee3 85 {
AzureIoTClient 13:920e00014ee3 86 LIST_ITEM_INSTANCE* current = list_instance->head;
AzureIoTClient 13:920e00014ee3 87 while (current->next != NULL)
AzureIoTClient 13:920e00014ee3 88 {
AzureIoTClient 13:920e00014ee3 89 current = (LIST_ITEM_INSTANCE*)current->next;
AzureIoTClient 13:920e00014ee3 90 }
AzureIoTClient 13:920e00014ee3 91
AzureIoTClient 13:920e00014ee3 92 current->next = result;
AzureIoTClient 13:920e00014ee3 93 }
AzureIoTClient 13:920e00014ee3 94 }
AzureIoTClient 13:920e00014ee3 95 }
AzureIoTClient 13:920e00014ee3 96
AzureIoTClient 13:920e00014ee3 97 return result;
AzureIoTClient 13:920e00014ee3 98 }
AzureIoTClient 13:920e00014ee3 99
AzureIoTClient 13:920e00014ee3 100 int singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item)
AzureIoTClient 13:920e00014ee3 101 {
AzureIoTClient 13:920e00014ee3 102 int result;
AzureIoTClient 13:920e00014ee3 103
AzureIoTClient 13:920e00014ee3 104 /* Codes_SRS_LIST_01_024: [If any of the arguments list or item_handle is NULL, singlylinkedlist_remove shall fail and return a non-zero value.] */
AzureIoTClient 13:920e00014ee3 105 if ((list == NULL) ||
AzureIoTClient 13:920e00014ee3 106 (item == NULL))
AzureIoTClient 13:920e00014ee3 107 {
AzureIoTClient 21:b92006c5b9ff 108 result = __FAILURE__;
AzureIoTClient 13:920e00014ee3 109 }
AzureIoTClient 13:920e00014ee3 110 else
AzureIoTClient 13:920e00014ee3 111 {
AzureIoTClient 13:920e00014ee3 112 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 113 LIST_ITEM_INSTANCE* current_item = list_instance->head;
AzureIoTClient 13:920e00014ee3 114 LIST_ITEM_INSTANCE* previous_item = NULL;
AzureIoTClient 13:920e00014ee3 115
AzureIoTClient 13:920e00014ee3 116 while (current_item != NULL)
AzureIoTClient 13:920e00014ee3 117 {
AzureIoTClient 13:920e00014ee3 118 if (current_item == item)
AzureIoTClient 13:920e00014ee3 119 {
AzureIoTClient 13:920e00014ee3 120 if (previous_item != NULL)
AzureIoTClient 13:920e00014ee3 121 {
AzureIoTClient 13:920e00014ee3 122 previous_item->next = current_item->next;
AzureIoTClient 13:920e00014ee3 123 }
AzureIoTClient 13:920e00014ee3 124 else
AzureIoTClient 13:920e00014ee3 125 {
AzureIoTClient 13:920e00014ee3 126 list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 13:920e00014ee3 127 }
AzureIoTClient 13:920e00014ee3 128
AzureIoTClient 13:920e00014ee3 129 free(current_item);
AzureIoTClient 13:920e00014ee3 130
AzureIoTClient 13:920e00014ee3 131 break;
AzureIoTClient 13:920e00014ee3 132 }
AzureIoTClient 13:920e00014ee3 133 previous_item = current_item;
AzureIoTClient 13:920e00014ee3 134 current_item = (LIST_ITEM_INSTANCE*)current_item->next;
AzureIoTClient 13:920e00014ee3 135 }
AzureIoTClient 13:920e00014ee3 136
AzureIoTClient 13:920e00014ee3 137 if (current_item == NULL)
AzureIoTClient 13:920e00014ee3 138 {
AzureIoTClient 13:920e00014ee3 139 /* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */
AzureIoTClient 21:b92006c5b9ff 140 result = __FAILURE__;
AzureIoTClient 13:920e00014ee3 141 }
AzureIoTClient 13:920e00014ee3 142 else
AzureIoTClient 13:920e00014ee3 143 {
AzureIoTClient 13:920e00014ee3 144 /* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */
AzureIoTClient 13:920e00014ee3 145 result = 0;
AzureIoTClient 13:920e00014ee3 146 }
AzureIoTClient 13:920e00014ee3 147 }
AzureIoTClient 13:920e00014ee3 148
AzureIoTClient 13:920e00014ee3 149 return result;
AzureIoTClient 13:920e00014ee3 150 }
AzureIoTClient 13:920e00014ee3 151
AzureIoTClient 13:920e00014ee3 152 LIST_ITEM_HANDLE singlylinkedlist_get_head_item(SINGLYLINKEDLIST_HANDLE list)
AzureIoTClient 13:920e00014ee3 153 {
AzureIoTClient 13:920e00014ee3 154 LIST_ITEM_HANDLE result;
AzureIoTClient 13:920e00014ee3 155
AzureIoTClient 13:920e00014ee3 156 if (list == NULL)
AzureIoTClient 13:920e00014ee3 157 {
AzureIoTClient 13:920e00014ee3 158 /* Codes_SRS_LIST_01_009: [If the list argument is NULL, singlylinkedlist_get_head_item shall return NULL.] */
AzureIoTClient 13:920e00014ee3 159 result = NULL;
AzureIoTClient 13:920e00014ee3 160 }
AzureIoTClient 13:920e00014ee3 161 else
AzureIoTClient 13:920e00014ee3 162 {
AzureIoTClient 13:920e00014ee3 163 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 164
AzureIoTClient 13:920e00014ee3 165 /* Codes_SRS_LIST_01_008: [singlylinkedlist_get_head_item shall return the head of the list.] */
AzureIoTClient 13:920e00014ee3 166 /* Codes_SRS_LIST_01_010: [If the list is empty, singlylinkedlist_get_head_item_shall_return NULL.] */
AzureIoTClient 13:920e00014ee3 167 result = list_instance->head;
AzureIoTClient 13:920e00014ee3 168 }
AzureIoTClient 13:920e00014ee3 169
AzureIoTClient 13:920e00014ee3 170 return result;
AzureIoTClient 13:920e00014ee3 171 }
AzureIoTClient 13:920e00014ee3 172
AzureIoTClient 13:920e00014ee3 173 LIST_ITEM_HANDLE singlylinkedlist_get_next_item(LIST_ITEM_HANDLE item_handle)
AzureIoTClient 13:920e00014ee3 174 {
AzureIoTClient 13:920e00014ee3 175 LIST_ITEM_HANDLE result;
AzureIoTClient 13:920e00014ee3 176
AzureIoTClient 13:920e00014ee3 177 if (item_handle == NULL)
AzureIoTClient 13:920e00014ee3 178 {
AzureIoTClient 13:920e00014ee3 179 /* Codes_SRS_LIST_01_019: [If item_handle is NULL then singlylinkedlist_get_next_item shall return NULL.] */
AzureIoTClient 13:920e00014ee3 180 result = NULL;
AzureIoTClient 13:920e00014ee3 181 }
AzureIoTClient 13:920e00014ee3 182 else
AzureIoTClient 13:920e00014ee3 183 {
AzureIoTClient 13:920e00014ee3 184 /* Codes_SRS_LIST_01_018: [singlylinkedlist_get_next_item shall return the next item in the list following the item item_handle.] */
AzureIoTClient 13:920e00014ee3 185 result = (LIST_ITEM_HANDLE)((LIST_ITEM_INSTANCE*)item_handle)->next;
AzureIoTClient 13:920e00014ee3 186 }
AzureIoTClient 13:920e00014ee3 187
AzureIoTClient 13:920e00014ee3 188 return result;
AzureIoTClient 13:920e00014ee3 189 }
AzureIoTClient 13:920e00014ee3 190
AzureIoTClient 13:920e00014ee3 191 const void* singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle)
AzureIoTClient 13:920e00014ee3 192 {
AzureIoTClient 13:920e00014ee3 193 const void* result;
AzureIoTClient 13:920e00014ee3 194
AzureIoTClient 13:920e00014ee3 195 if (item_handle == NULL)
AzureIoTClient 13:920e00014ee3 196 {
AzureIoTClient 13:920e00014ee3 197 /* Codes_SRS_LIST_01_021: [If item_handle is NULL, singlylinkedlist_item_get_value shall return NULL.] */
AzureIoTClient 13:920e00014ee3 198 result = NULL;
AzureIoTClient 13:920e00014ee3 199 }
AzureIoTClient 13:920e00014ee3 200 else
AzureIoTClient 13:920e00014ee3 201 {
AzureIoTClient 13:920e00014ee3 202 /* Codes_SRS_LIST_01_020: [singlylinkedlist_item_get_value shall return the value associated with the list item identified by the item_handle argument.] */
AzureIoTClient 13:920e00014ee3 203 result = ((LIST_ITEM_INSTANCE*)item_handle)->item;
AzureIoTClient 13:920e00014ee3 204 }
AzureIoTClient 13:920e00014ee3 205
AzureIoTClient 13:920e00014ee3 206 return result;
AzureIoTClient 13:920e00014ee3 207 }
AzureIoTClient 13:920e00014ee3 208
AzureIoTClient 13:920e00014ee3 209 LIST_ITEM_HANDLE singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE list, LIST_MATCH_FUNCTION match_function, const void* match_context)
AzureIoTClient 13:920e00014ee3 210 {
AzureIoTClient 13:920e00014ee3 211 LIST_ITEM_HANDLE result;
AzureIoTClient 13:920e00014ee3 212
AzureIoTClient 13:920e00014ee3 213 if ((list == NULL) ||
AzureIoTClient 13:920e00014ee3 214 (match_function == NULL))
AzureIoTClient 13:920e00014ee3 215 {
AzureIoTClient 13:920e00014ee3 216 /* Codes_SRS_LIST_01_012: [If the list or the match_function argument is NULL, singlylinkedlist_find shall return NULL.] */
AzureIoTClient 13:920e00014ee3 217 result = NULL;
AzureIoTClient 13:920e00014ee3 218 }
AzureIoTClient 13:920e00014ee3 219 else
AzureIoTClient 13:920e00014ee3 220 {
AzureIoTClient 13:920e00014ee3 221 LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list;
AzureIoTClient 13:920e00014ee3 222 LIST_ITEM_INSTANCE* current = list_instance->head;
AzureIoTClient 13:920e00014ee3 223
AzureIoTClient 13:920e00014ee3 224 /* Codes_SRS_LIST_01_011: [singlylinkedlist_find shall iterate through all items in a list and return the first one that satisfies a certain match function.] */
AzureIoTClient 13:920e00014ee3 225 while (current != NULL)
AzureIoTClient 13:920e00014ee3 226 {
AzureIoTClient 13:920e00014ee3 227 /* Codes_SRS_LIST_01_014: [list find shall determine whether an item satisfies the match criteria by invoking the match function for each item in the list until a matching item is found.] */
AzureIoTClient 13:920e00014ee3 228 /* Codes_SRS_LIST_01_013: [The match_function shall get as arguments the list item being attempted to be matched and the match_context as is.] */
AzureIoTClient 13:920e00014ee3 229 if (match_function((LIST_ITEM_HANDLE)current, match_context) == true)
AzureIoTClient 13:920e00014ee3 230 {
AzureIoTClient 13:920e00014ee3 231 /* Codes_SRS_LIST_01_017: [If the match function returns true, singlylinkedlist_find shall consider that item as matching.] */
AzureIoTClient 13:920e00014ee3 232 break;
AzureIoTClient 13:920e00014ee3 233 }
AzureIoTClient 13:920e00014ee3 234
AzureIoTClient 13:920e00014ee3 235 /* Codes_SRS_LIST_01_016: [If the match function returns false, singlylinkedlist_find shall consider that item as not matching.] */
AzureIoTClient 13:920e00014ee3 236 current = (LIST_ITEM_INSTANCE*)current->next;
AzureIoTClient 13:920e00014ee3 237 }
AzureIoTClient 13:920e00014ee3 238
AzureIoTClient 13:920e00014ee3 239 if (current == NULL)
AzureIoTClient 13:920e00014ee3 240 {
AzureIoTClient 13:920e00014ee3 241 /* Codes_SRS_LIST_01_015: [If the list is empty, singlylinkedlist_find shall return NULL.] */
AzureIoTClient 13:920e00014ee3 242 result = NULL;
AzureIoTClient 13:920e00014ee3 243 }
AzureIoTClient 13:920e00014ee3 244 else
AzureIoTClient 13:920e00014ee3 245 {
AzureIoTClient 13:920e00014ee3 246 result = current;
AzureIoTClient 13:920e00014ee3 247 }
AzureIoTClient 13:920e00014ee3 248 }
AzureIoTClient 13:920e00014ee3 249
AzureIoTClient 13:920e00014ee3 250 return result;
AzureIoTClient 13:920e00014ee3 251 }