| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#include "tod.h" |
| 24 |
#include "misc.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
int add_tod_uid(tod_t *tod, uint8_t uid[ARTNET_RDM_UID_WIDTH]) { |
| 30 |
uint8_t *addr; |
| 31 |
int size; |
| 32 |
|
| 33 |
if (tod == NULL) |
| 34 |
return -1; |
| 35 |
|
| 36 |
if (tod->data == NULL) { |
| 37 |
|
| 38 |
tod->data = malloc(ARTNET_RDM_UID_WIDTH * ARTNET_TOD_INITIAL_SIZE); |
| 39 |
|
| 40 |
if (tod->data == NULL) { |
| 41 |
artnet_error_malloc(); |
| 42 |
return ARTNET_EMEM; |
| 43 |
} |
| 44 |
tod->length = 1; |
| 45 |
tod->max_length = ARTNET_TOD_INITIAL_SIZE; |
| 46 |
|
| 47 |
} else if (tod->length == tod->max_length) { |
| 48 |
|
| 49 |
size = (tod->max_length + ARTNET_TOD_INCREMENT); |
| 50 |
tod->data = realloc(tod->data, size * ARTNET_RDM_UID_WIDTH); |
| 51 |
|
| 52 |
if (tod->data == NULL) { |
| 53 |
artnet_error_realloc(); |
| 54 |
return ARTNET_EMEM; |
| 55 |
} |
| 56 |
|
| 57 |
tod->max_length = size; |
| 58 |
tod->length++; |
| 59 |
|
| 60 |
} else { |
| 61 |
tod->length++; |
| 62 |
} |
| 63 |
|
| 64 |
addr = tod->data + (tod->length-1) * ARTNET_RDM_UID_WIDTH; |
| 65 |
memcpy(addr, uid, ARTNET_RDM_UID_WIDTH); |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int remove_tod_uid(tod_t *tod, uint8_t uid[ARTNET_RDM_UID_WIDTH]) { |
| 75 |
int i; |
| 76 |
int offset = 0; |
| 77 |
uint8_t *last; |
| 78 |
|
| 79 |
if (tod == NULL) |
| 80 |
return -1; |
| 81 |
|
| 82 |
if (tod->data == NULL) |
| 83 |
return -1; |
| 84 |
|
| 85 |
for (i=0; i < tod->length; i++) { |
| 86 |
offset += ARTNET_RDM_UID_WIDTH; |
| 87 |
if (memcmp(tod->data + offset, uid, ARTNET_RDM_UID_WIDTH) == 0) |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
if (i == tod->length) { |
| 92 |
return -1; |
| 93 |
} else { |
| 94 |
|
| 95 |
last = tod->data + (tod->length-1) * ARTNET_RDM_UID_WIDTH; |
| 96 |
|
| 97 |
memcpy(tod->data + offset, last ,ARTNET_RDM_UID_WIDTH); |
| 98 |
|
| 99 |
tod->length--; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
int flush_tod(tod_t *tod) { |
| 108 |
if (tod == NULL) |
| 109 |
return -1; |
| 110 |
|
| 111 |
free(tod->data); |
| 112 |
tod->data = NULL; |
| 113 |
tod->length = 0; |
| 114 |
tod->max_length = 0; |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
int reset_tod(tod_t *tod) { |
| 121 |
if (tod == NULL) |
| 122 |
return -1; |
| 123 |
|
| 124 |
tod->data = NULL; |
| 125 |
tod->length = 0; |
| 126 |
tod->max_length = 0; |
| 127 |
return 0; |
| 128 |
} |