Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:35:22 2017 -0800
Revision:
19:2e0811512ceb
Parent:
13:920e00014ee3
Child:
21:b92006c5b9ff
1.1.6

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