version of nsdl to work with lwm2m, updated RD parameters and location option handling
Dependents: ArchPro_LWM2M_LED_Client Weather_Station_LWM2M mbedEndpointNetwork
Fork of nanoservice_client_1_12 by
sn_linked_list.c@10:b5ecd6660d71, 2015-04-13 (annotated)
- Committer:
- michaeljkoster
- Date:
- Mon Apr 13 22:13:40 2015 +0000
- Revision:
- 10:b5ecd6660d71
- Parent:
- 0:aafd54b05111
Add error return from sn_nsdl_register_endpoint() in sn_nsdl.c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zdshelby | 0:aafd54b05111 | 1 | #include <stdio.h> |
zdshelby | 0:aafd54b05111 | 2 | #include <stddef.h> |
zdshelby | 0:aafd54b05111 | 3 | #include <stdint.h> |
zdshelby | 0:aafd54b05111 | 4 | |
zdshelby | 0:aafd54b05111 | 5 | #include "nsdl_types.h" |
zdshelby | 0:aafd54b05111 | 6 | #include "sn_linked_list.h" |
zdshelby | 0:aafd54b05111 | 7 | |
zdshelby | 0:aafd54b05111 | 8 | void *(*sn_linked_list_memory_alloc)(uint16_t); |
zdshelby | 0:aafd54b05111 | 9 | void (*sn_linked_list_memory_free)(void*); |
zdshelby | 0:aafd54b05111 | 10 | |
zdshelby | 0:aafd54b05111 | 11 | void sn_linked_list_init(void *(*linked_list_alloc_function)(uint16_t), void (*linked_list_free_function)(void*)) |
zdshelby | 0:aafd54b05111 | 12 | { |
zdshelby | 0:aafd54b05111 | 13 | |
zdshelby | 0:aafd54b05111 | 14 | sn_linked_list_memory_alloc = linked_list_alloc_function; |
zdshelby | 0:aafd54b05111 | 15 | sn_linked_list_memory_free = linked_list_free_function; |
zdshelby | 0:aafd54b05111 | 16 | } |
zdshelby | 0:aafd54b05111 | 17 | sn_linked_list_t *sn_linked_list_create(void) |
zdshelby | 0:aafd54b05111 | 18 | { |
zdshelby | 0:aafd54b05111 | 19 | |
zdshelby | 0:aafd54b05111 | 20 | sn_linked_list_t *new_linked_list = sn_linked_list_memory_alloc(sizeof(sn_linked_list_t)); |
zdshelby | 0:aafd54b05111 | 21 | |
zdshelby | 0:aafd54b05111 | 22 | if(new_linked_list) |
zdshelby | 0:aafd54b05111 | 23 | { |
zdshelby | 0:aafd54b05111 | 24 | |
zdshelby | 0:aafd54b05111 | 25 | new_linked_list->first_node = NULL; |
zdshelby | 0:aafd54b05111 | 26 | new_linked_list->current_node = NULL; |
zdshelby | 0:aafd54b05111 | 27 | new_linked_list->node_count = 0; |
zdshelby | 0:aafd54b05111 | 28 | } |
zdshelby | 0:aafd54b05111 | 29 | |
zdshelby | 0:aafd54b05111 | 30 | return new_linked_list; |
zdshelby | 0:aafd54b05111 | 31 | |
zdshelby | 0:aafd54b05111 | 32 | } |
zdshelby | 0:aafd54b05111 | 33 | |
zdshelby | 0:aafd54b05111 | 34 | int8_t sn_linked_list_free(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 35 | { |
zdshelby | 0:aafd54b05111 | 36 | |
zdshelby | 0:aafd54b05111 | 37 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 38 | { |
zdshelby | 0:aafd54b05111 | 39 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
zdshelby | 0:aafd54b05111 | 40 | } |
zdshelby | 0:aafd54b05111 | 41 | |
zdshelby | 0:aafd54b05111 | 42 | if(linked_list->first_node) |
zdshelby | 0:aafd54b05111 | 43 | { |
zdshelby | 0:aafd54b05111 | 44 | return SN_LINKED_LIST_ERROR_LIST_NOT_EMPTY; |
zdshelby | 0:aafd54b05111 | 45 | } |
zdshelby | 0:aafd54b05111 | 46 | |
zdshelby | 0:aafd54b05111 | 47 | sn_linked_list_memory_free(linked_list); |
zdshelby | 0:aafd54b05111 | 48 | |
zdshelby | 0:aafd54b05111 | 49 | return SN_LINKED_LIST_ERROR_NO_ERROR; |
zdshelby | 0:aafd54b05111 | 50 | |
zdshelby | 0:aafd54b05111 | 51 | } |
zdshelby | 0:aafd54b05111 | 52 | int8_t sn_linked_list_add_node(sn_linked_list_t *linked_list, void *data) |
zdshelby | 0:aafd54b05111 | 53 | { |
zdshelby | 0:aafd54b05111 | 54 | |
zdshelby | 0:aafd54b05111 | 55 | struct sn_linked_list_node *node_to_add = NULL; |
zdshelby | 0:aafd54b05111 | 56 | |
zdshelby | 0:aafd54b05111 | 57 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 58 | { |
zdshelby | 0:aafd54b05111 | 59 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
zdshelby | 0:aafd54b05111 | 60 | } |
zdshelby | 0:aafd54b05111 | 61 | |
zdshelby | 0:aafd54b05111 | 62 | if(!data) |
zdshelby | 0:aafd54b05111 | 63 | { |
zdshelby | 0:aafd54b05111 | 64 | return SN_LINKED_LIST_ERROR_NO_DATA_TO_ADD; |
zdshelby | 0:aafd54b05111 | 65 | } |
zdshelby | 0:aafd54b05111 | 66 | |
zdshelby | 0:aafd54b05111 | 67 | node_to_add = sn_linked_list_memory_alloc(sizeof(struct sn_linked_list_node)); |
zdshelby | 0:aafd54b05111 | 68 | |
zdshelby | 0:aafd54b05111 | 69 | if(!node_to_add) |
zdshelby | 0:aafd54b05111 | 70 | { |
zdshelby | 0:aafd54b05111 | 71 | |
zdshelby | 0:aafd54b05111 | 72 | return SN_LINKED_LIST_ERROR_DATA_ALLOCATOIN_FAILED; |
zdshelby | 0:aafd54b05111 | 73 | |
zdshelby | 0:aafd54b05111 | 74 | } |
zdshelby | 0:aafd54b05111 | 75 | |
zdshelby | 0:aafd54b05111 | 76 | node_to_add->data = data; |
zdshelby | 0:aafd54b05111 | 77 | node_to_add->next_node = linked_list->first_node; |
zdshelby | 0:aafd54b05111 | 78 | node_to_add->previous_node = NULL; |
zdshelby | 0:aafd54b05111 | 79 | |
zdshelby | 0:aafd54b05111 | 80 | if(linked_list->first_node) |
zdshelby | 0:aafd54b05111 | 81 | { |
zdshelby | 0:aafd54b05111 | 82 | linked_list->first_node->previous_node = node_to_add; |
zdshelby | 0:aafd54b05111 | 83 | } |
zdshelby | 0:aafd54b05111 | 84 | |
zdshelby | 0:aafd54b05111 | 85 | linked_list->first_node = node_to_add; |
zdshelby | 0:aafd54b05111 | 86 | |
zdshelby | 0:aafd54b05111 | 87 | linked_list->node_count++; |
zdshelby | 0:aafd54b05111 | 88 | |
zdshelby | 0:aafd54b05111 | 89 | return SN_LINKED_LIST_ERROR_NO_ERROR; |
zdshelby | 0:aafd54b05111 | 90 | |
zdshelby | 0:aafd54b05111 | 91 | } |
zdshelby | 0:aafd54b05111 | 92 | void *sn_linked_list_get_first_node(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 93 | { |
zdshelby | 0:aafd54b05111 | 94 | |
zdshelby | 0:aafd54b05111 | 95 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 96 | { |
zdshelby | 0:aafd54b05111 | 97 | return NULL; |
zdshelby | 0:aafd54b05111 | 98 | } |
zdshelby | 0:aafd54b05111 | 99 | |
zdshelby | 0:aafd54b05111 | 100 | if(linked_list->first_node) |
zdshelby | 0:aafd54b05111 | 101 | { |
zdshelby | 0:aafd54b05111 | 102 | |
zdshelby | 0:aafd54b05111 | 103 | linked_list->current_node = linked_list->first_node; |
zdshelby | 0:aafd54b05111 | 104 | return linked_list->first_node->data; |
zdshelby | 0:aafd54b05111 | 105 | |
zdshelby | 0:aafd54b05111 | 106 | } |
zdshelby | 0:aafd54b05111 | 107 | |
zdshelby | 0:aafd54b05111 | 108 | return NULL; |
zdshelby | 0:aafd54b05111 | 109 | |
zdshelby | 0:aafd54b05111 | 110 | } |
zdshelby | 0:aafd54b05111 | 111 | void *sn_linked_list_get_next_node(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 112 | { |
zdshelby | 0:aafd54b05111 | 113 | |
zdshelby | 0:aafd54b05111 | 114 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 115 | { |
zdshelby | 0:aafd54b05111 | 116 | return NULL; |
zdshelby | 0:aafd54b05111 | 117 | } |
zdshelby | 0:aafd54b05111 | 118 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 119 | { |
zdshelby | 0:aafd54b05111 | 120 | return NULL; |
zdshelby | 0:aafd54b05111 | 121 | } |
zdshelby | 0:aafd54b05111 | 122 | |
zdshelby | 0:aafd54b05111 | 123 | linked_list->current_node = linked_list->current_node->next_node; |
zdshelby | 0:aafd54b05111 | 124 | |
zdshelby | 0:aafd54b05111 | 125 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 126 | { |
zdshelby | 0:aafd54b05111 | 127 | return NULL; |
zdshelby | 0:aafd54b05111 | 128 | } |
zdshelby | 0:aafd54b05111 | 129 | |
zdshelby | 0:aafd54b05111 | 130 | return linked_list->current_node->data; |
zdshelby | 0:aafd54b05111 | 131 | |
zdshelby | 0:aafd54b05111 | 132 | } |
zdshelby | 0:aafd54b05111 | 133 | void *sn_linked_list_get_current_node(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 134 | { |
zdshelby | 0:aafd54b05111 | 135 | |
zdshelby | 0:aafd54b05111 | 136 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 137 | { |
zdshelby | 0:aafd54b05111 | 138 | return NULL; |
zdshelby | 0:aafd54b05111 | 139 | } |
zdshelby | 0:aafd54b05111 | 140 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 141 | { |
zdshelby | 0:aafd54b05111 | 142 | return NULL; |
zdshelby | 0:aafd54b05111 | 143 | } |
zdshelby | 0:aafd54b05111 | 144 | |
zdshelby | 0:aafd54b05111 | 145 | return linked_list->current_node->data; |
zdshelby | 0:aafd54b05111 | 146 | |
zdshelby | 0:aafd54b05111 | 147 | } |
zdshelby | 0:aafd54b05111 | 148 | void *sn_linked_list_get_previous_node(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 149 | { |
zdshelby | 0:aafd54b05111 | 150 | |
zdshelby | 0:aafd54b05111 | 151 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 152 | { |
zdshelby | 0:aafd54b05111 | 153 | return NULL; |
zdshelby | 0:aafd54b05111 | 154 | } |
zdshelby | 0:aafd54b05111 | 155 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 156 | { |
zdshelby | 0:aafd54b05111 | 157 | return NULL; |
zdshelby | 0:aafd54b05111 | 158 | } |
zdshelby | 0:aafd54b05111 | 159 | |
zdshelby | 0:aafd54b05111 | 160 | linked_list->current_node = linked_list->current_node->previous_node; |
zdshelby | 0:aafd54b05111 | 161 | |
zdshelby | 0:aafd54b05111 | 162 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 163 | { |
zdshelby | 0:aafd54b05111 | 164 | return NULL; |
zdshelby | 0:aafd54b05111 | 165 | } |
zdshelby | 0:aafd54b05111 | 166 | |
zdshelby | 0:aafd54b05111 | 167 | return linked_list->current_node->data; |
zdshelby | 0:aafd54b05111 | 168 | |
zdshelby | 0:aafd54b05111 | 169 | } |
zdshelby | 0:aafd54b05111 | 170 | |
zdshelby | 0:aafd54b05111 | 171 | extern void *sn_linked_list_get_last_node(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 172 | { |
zdshelby | 0:aafd54b05111 | 173 | |
zdshelby | 0:aafd54b05111 | 174 | if(!sn_linked_list_get_first_node(linked_list)) |
zdshelby | 0:aafd54b05111 | 175 | { |
zdshelby | 0:aafd54b05111 | 176 | |
zdshelby | 0:aafd54b05111 | 177 | return NULL; |
zdshelby | 0:aafd54b05111 | 178 | |
zdshelby | 0:aafd54b05111 | 179 | } |
zdshelby | 0:aafd54b05111 | 180 | while(linked_list->current_node->next_node) |
zdshelby | 0:aafd54b05111 | 181 | { |
zdshelby | 0:aafd54b05111 | 182 | |
zdshelby | 0:aafd54b05111 | 183 | sn_linked_list_get_next_node(linked_list); |
zdshelby | 0:aafd54b05111 | 184 | |
zdshelby | 0:aafd54b05111 | 185 | } |
zdshelby | 0:aafd54b05111 | 186 | |
zdshelby | 0:aafd54b05111 | 187 | return linked_list->current_node->data; |
zdshelby | 0:aafd54b05111 | 188 | |
zdshelby | 0:aafd54b05111 | 189 | } |
zdshelby | 0:aafd54b05111 | 190 | |
zdshelby | 0:aafd54b05111 | 191 | void *sn_linked_list_remove_current_node(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 192 | { |
zdshelby | 0:aafd54b05111 | 193 | |
zdshelby | 0:aafd54b05111 | 194 | struct sn_linked_list_node *node_to_remove = NULL; |
zdshelby | 0:aafd54b05111 | 195 | void *data_from_removed_node = NULL; |
zdshelby | 0:aafd54b05111 | 196 | |
zdshelby | 0:aafd54b05111 | 197 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 198 | { |
zdshelby | 0:aafd54b05111 | 199 | return NULL; |
zdshelby | 0:aafd54b05111 | 200 | } |
zdshelby | 0:aafd54b05111 | 201 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 202 | { |
zdshelby | 0:aafd54b05111 | 203 | return NULL; |
zdshelby | 0:aafd54b05111 | 204 | } |
zdshelby | 0:aafd54b05111 | 205 | |
zdshelby | 0:aafd54b05111 | 206 | node_to_remove = linked_list->current_node; |
zdshelby | 0:aafd54b05111 | 207 | |
zdshelby | 0:aafd54b05111 | 208 | if(node_to_remove != linked_list->first_node) |
zdshelby | 0:aafd54b05111 | 209 | { |
zdshelby | 0:aafd54b05111 | 210 | |
zdshelby | 0:aafd54b05111 | 211 | node_to_remove->previous_node->next_node = node_to_remove->next_node; |
zdshelby | 0:aafd54b05111 | 212 | |
zdshelby | 0:aafd54b05111 | 213 | if(node_to_remove->next_node) |
zdshelby | 0:aafd54b05111 | 214 | { |
zdshelby | 0:aafd54b05111 | 215 | node_to_remove->next_node->previous_node = node_to_remove->previous_node; |
zdshelby | 0:aafd54b05111 | 216 | } |
zdshelby | 0:aafd54b05111 | 217 | |
zdshelby | 0:aafd54b05111 | 218 | } |
zdshelby | 0:aafd54b05111 | 219 | else |
zdshelby | 0:aafd54b05111 | 220 | { |
zdshelby | 0:aafd54b05111 | 221 | |
zdshelby | 0:aafd54b05111 | 222 | linked_list->first_node = node_to_remove->next_node; |
zdshelby | 0:aafd54b05111 | 223 | |
zdshelby | 0:aafd54b05111 | 224 | if(node_to_remove->next_node) |
zdshelby | 0:aafd54b05111 | 225 | { |
zdshelby | 0:aafd54b05111 | 226 | node_to_remove->next_node->previous_node = NULL; |
zdshelby | 0:aafd54b05111 | 227 | } |
zdshelby | 0:aafd54b05111 | 228 | |
zdshelby | 0:aafd54b05111 | 229 | } |
zdshelby | 0:aafd54b05111 | 230 | |
zdshelby | 0:aafd54b05111 | 231 | if(!node_to_remove->next_node && node_to_remove->previous_node) |
zdshelby | 0:aafd54b05111 | 232 | { |
zdshelby | 0:aafd54b05111 | 233 | |
zdshelby | 0:aafd54b05111 | 234 | linked_list->current_node = node_to_remove->previous_node; |
zdshelby | 0:aafd54b05111 | 235 | |
zdshelby | 0:aafd54b05111 | 236 | } |
zdshelby | 0:aafd54b05111 | 237 | else |
zdshelby | 0:aafd54b05111 | 238 | { |
zdshelby | 0:aafd54b05111 | 239 | |
zdshelby | 0:aafd54b05111 | 240 | linked_list->current_node = node_to_remove->next_node; |
zdshelby | 0:aafd54b05111 | 241 | |
zdshelby | 0:aafd54b05111 | 242 | } |
zdshelby | 0:aafd54b05111 | 243 | |
zdshelby | 0:aafd54b05111 | 244 | data_from_removed_node = node_to_remove->data; |
zdshelby | 0:aafd54b05111 | 245 | |
zdshelby | 0:aafd54b05111 | 246 | sn_linked_list_memory_free(node_to_remove); |
zdshelby | 0:aafd54b05111 | 247 | |
zdshelby | 0:aafd54b05111 | 248 | linked_list->node_count--; |
zdshelby | 0:aafd54b05111 | 249 | |
zdshelby | 0:aafd54b05111 | 250 | return data_from_removed_node; |
zdshelby | 0:aafd54b05111 | 251 | |
zdshelby | 0:aafd54b05111 | 252 | } |
zdshelby | 0:aafd54b05111 | 253 | |
zdshelby | 0:aafd54b05111 | 254 | int8_t sn_linked_list_update_current_node(sn_linked_list_t *linked_list, void *data) |
zdshelby | 0:aafd54b05111 | 255 | { |
zdshelby | 0:aafd54b05111 | 256 | |
zdshelby | 0:aafd54b05111 | 257 | if(!linked_list) |
zdshelby | 0:aafd54b05111 | 258 | { |
zdshelby | 0:aafd54b05111 | 259 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
zdshelby | 0:aafd54b05111 | 260 | } |
zdshelby | 0:aafd54b05111 | 261 | |
zdshelby | 0:aafd54b05111 | 262 | if(!data) |
zdshelby | 0:aafd54b05111 | 263 | { |
zdshelby | 0:aafd54b05111 | 264 | return SN_LINKED_LIST_ERROR_NO_DATA_TO_ADD; |
zdshelby | 0:aafd54b05111 | 265 | } |
zdshelby | 0:aafd54b05111 | 266 | if(!linked_list->current_node) |
zdshelby | 0:aafd54b05111 | 267 | { |
zdshelby | 0:aafd54b05111 | 268 | return SN_LINKED_LIST_ERROR_INVALID_LIST_POINTER; |
zdshelby | 0:aafd54b05111 | 269 | } |
zdshelby | 0:aafd54b05111 | 270 | |
zdshelby | 0:aafd54b05111 | 271 | linked_list->current_node->data = data; |
zdshelby | 0:aafd54b05111 | 272 | |
zdshelby | 0:aafd54b05111 | 273 | return SN_LINKED_LIST_ERROR_NO_ERROR; |
zdshelby | 0:aafd54b05111 | 274 | |
zdshelby | 0:aafd54b05111 | 275 | } |
zdshelby | 0:aafd54b05111 | 276 | |
zdshelby | 0:aafd54b05111 | 277 | uint16_t sn_linked_list_count_nodes(sn_linked_list_t *linked_list) |
zdshelby | 0:aafd54b05111 | 278 | { |
zdshelby | 0:aafd54b05111 | 279 | |
zdshelby | 0:aafd54b05111 | 280 | return linked_list->node_count; |
zdshelby | 0:aafd54b05111 | 281 | |
zdshelby | 0:aafd54b05111 | 282 | } |
zdshelby | 0:aafd54b05111 | 283 | |
zdshelby | 0:aafd54b05111 | 284 | /* EOF */ |
zdshelby | 0:aafd54b05111 | 285 |