Debian Codegraph @

interfaces/artnet/tod.c

raw symbols SCIP
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * tod.c
17 * Functions to manipulate the TOD
18 * Copyright (C) 2004-2005 Simon Newton
19 */
20
21 #include <stdlib.h>
22
23 #include "tod.h"
24 #include "misc.h"
25
26 /*
27 * adds a uid to the table of devices
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 // malloc
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 // realloc
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 * remove a uid from the table of devices
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 // copy the last entry over this one
97 memcpy(tod->data + offset, last ,ARTNET_RDM_UID_WIDTH);
98
99 tod->length--;
100 return 0;
101 }
102 }
103
104 /*
105 * clear the table of devices
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 }