liblightify
node.c
Go to the documentation of this file.
1 /*
2  liblightify -- library to control OSRAM's LIGHTIFY
3 
4 Copyright (c) 2015, Tobias Frost <tobi@coldtobi.de>
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of the author nor the
15  names of its contributors may be used to endorse or promote products
16  derived from this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include "liblightify-private.h"
31 #include "node.h"
32 #include "context.h"
33 
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <string.h>
38 
39 #define MAX_NODE_NANE_LEN (16)
40 
43 struct lightify_node {
44 
46  struct lightify_ctx *ctx;
47 
48  /* linked list */
51 
52  /* node address and groups */
53  uint64_t node_address;
54  uint16_t zone_address;
55  uint16_t group_address;
59 
61  char *name;
62 
63  int red;
64  int green;
65  int blue;
66  int white;
67  int cct;
69  int brightness;
71  int is_on;
73 
77  int is_stale;
78 };
79 
80 int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node** newnode) {
81 
82  struct lightify_node *n;
83  struct lightify_node *m;
84 
85  if (!ctx) return -EINVAL;
86 
87  n = calloc(1,sizeof(struct lightify_node));
88 
89  if (!n) return -ENOMEM;
90 
91  *newnode = n;
92 
93  n->red = -1;
94  n->green = -1;
95  n->blue = -1;
96  n->white = -1;
97  n->cct = -1;
98  n->brightness = -1;
99  n->is_on = -1;
100  n->online_status = -1;
101 
102  n->ctx = ctx;
103  m = ctx->nodes;
104 
105  if (!m) {
106  ctx->nodes = n;
107  return 0;
108  }
109 
110  while(m->next) m=m->next;
111  n->prev = m;
112  m->next = n;
113 
114  return 0;
115 }
116 
118 
119  if (!node) return -EINVAL;
120 
121  struct lightify_node *next = node->next;
122  struct lightify_node *prev = node->prev;
123 
124  if (prev) {
125  prev->next = next;
126  } else {
127  // first node
128  node->ctx->nodes=next;
129  }
130 
131  if (next) next->prev = prev;
132 
133  if (node->name) free(node->name);
134 
135  free(node);
136 
137  return 0;
138 }
139 
141  if (!node) return NULL;
142  return node->next;
143 }
144 
146  if (!node) return NULL;
147  return node->prev;
148 }
149 
150 
151 int lightify_node_set_name(struct lightify_node* node, char *name) {
152  if (!node) return -EINVAL;
153 
154  if (node->name) free(node->name);
155  node->name = NULL;
156 
157  if(name) {
158  node->name = strndup(name, MAX_NODE_NANE_LEN);
159  if (!node->name) return -ENOMEM;
160  }
161  return 0;
162 }
163 
165  if (!node) return NULL;
166  return node->name;
167 }
168 
169 int lightify_node_set_nodeadr(struct lightify_node* node, uint64_t adr) {
170  if(!node) return -EINVAL;
171  node->node_address=adr;
172  return 0;
173 }
174 
176  if (!node) return 0;
177  return node->node_address;
178 }
179 
180 int lightify_node_set_zoneadr(struct lightify_node* node, uint16_t adr) {
181  if(!node) return -EINVAL;
182  node->zone_address=adr;
183  return 0;
184 }
185 
187  if (!node) return 0;
188  return node->zone_address;
189 }
190 
191 int lightify_node_set_grpadr(struct lightify_node* node, uint16_t adr) {
192  if(!node) return -EINVAL;
193  node->group_address=adr;
194  return 0;
195 }
196 
198  if (!node) return 0;
199  return node->group_address;
200 }
201 
203  if(!node) return -EINVAL;
204  node->node_type = type;
205  return 0;
206 }
207 
209  if(!node) return -EINVAL;
210  return node->node_type;
211 }
212 
213 int lightify_node_set_red(struct lightify_node* node, int red) {
214  if(!node) return -EINVAL;
215  node->red = red;
216  return 0;
217 }
218 
220  if(!node) return -EINVAL;
221  return node->red;
222 }
223 
224 int lightify_node_set_blue(struct lightify_node* node, int blue) {
225  if(!node) return -EINVAL;
226  node->blue = blue;
227  return 0;
228 }
229 
231  if(!node) return -EINVAL;
232  return node->blue;
233 }
234 
236  if(!node) return -EINVAL;
237  node->green = green;
238  return 0;
239 }
240 
242  if(!node) return -EINVAL;
243  return node->green;
244 }
245 
247  if(!node) return -EINVAL;
248  node->white = white;
249  return 0;
250 }
251 
253  if(!node) return -EINVAL;
254  return node->white;
255 }
256 
257 int lightify_node_set_cct(struct lightify_node* node, int cct) {
258  if(!node) return -EINVAL;
259  node->cct = cct;
260  return 0;
261 }
262 
264  if(!node) return -EINVAL;
265  return node->cct;
266 }
267 
269  if(!node) return -EINVAL;
270  node->brightness = brightness;
271  return 0;
272 }
273 
275  if(!node) return -EINVAL;
276  return node->brightness;
277 }
278 
279 
280 int lightify_node_set_onoff(struct lightify_node* node, uint8_t on) {
281  if (!node) return -EINVAL;
282  node->is_on= on;
283  return 0;
284 }
285 
287  if(!node) return -EINVAL;
288  return node->is_on;
289 }
290 
291 int lightify_node_set_online_status(struct lightify_node* node, uint8_t state) {
292  if (!node) return -EINVAL;
293  node->online_status= state;
294  return 0;
295 }
296 
298  if(!node) return -EINVAL;
299  return node->online_status;
300 }
301 
303  if(!node) return -EINVAL;
304  return node->is_stale;
305 }
306 
307 int lightify_node_set_stale(struct lightify_node *node, int stale) {
308  if(!node) return -EINVAL;
309  node->is_stale = stale;
310  return 0;
311 }
int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node **newnode)
Definition: node.c:80
char * name
Definition: node.c:61
uint16_t group_address
Definition: node.c:55
#define MAX_NODE_NANE_LEN
Definition: node.c:39
enum lightify_node_online_state online_status
Definition: node.c:72
int is_stale
Definition: node.c:77
struct lightify_node * next
Definition: node.c:49
int lightify_node_set_grpadr(struct lightify_node *node, uint16_t adr)
Definition: node.c:191
int lightify_node_set_onoff(struct lightify_node *node, uint8_t on)
Definition: node.c:280
LIGHTIFY_EXPORT const char * lightify_node_get_name(struct lightify_node *node)
Definition: node.c:164
int white
Definition: node.c:66
enum lightify_node_type node_type
Definition: node.c:58
LIGHTIFY_EXPORT int lightify_node_get_brightness(struct lightify_node *node)
Definition: node.c:274
int lightify_node_remove(struct lightify_node *node)
Definition: node.c:117
struct lightify_node * nodes
Definition: context.h:85
int lightify_node_set_brightness(struct lightify_node *node, int brightness)
Definition: node.c:268
LIGHTIFY_EXPORT uint64_t lightify_node_get_nodeadr(struct lightify_node *node)
Definition: node.c:175
struct lightify_node * lightify_node_get_prevnode(struct lightify_node *node)
Definition: node.c:145
LIGHTIFY_EXPORT int lightify_node_is_stale(struct lightify_node *node)
Definition: node.c:302
LIGHTIFY_EXPORT int lightify_node_is_on(struct lightify_node *node)
Definition: node.c:286
int lightify_node_set_stale(struct lightify_node *node, int stale)
Definition: node.c:307
int lightify_node_set_zoneadr(struct lightify_node *node, uint16_t adr)
Definition: node.c:180
int lightify_node_set_lamptype(struct lightify_node *node, enum lightify_node_type type)
Definition: node.c:202
int lightify_node_set_cct(struct lightify_node *node, int cct)
Definition: node.c:257
#define LIGHTIFY_EXPORT
int lightify_node_set_blue(struct lightify_node *node, int blue)
Definition: node.c:224
LIGHTIFY_EXPORT uint16_t lightify_node_get_grpadr(struct lightify_node *node)
Definition: node.c:197
int is_on
Definition: node.c:71
int lightify_node_set_red(struct lightify_node *node, int red)
Definition: node.c:213
int blue
Definition: node.c:65
LIGHTIFY_EXPORT int lightify_node_get_red(struct lightify_node *node)
Definition: node.c:219
lightify_node_online_state
Definition: liblightify.h:118
int red
Definition: node.c:63
int lightify_node_set_green(struct lightify_node *node, int green)
Definition: node.c:235
int lightify_node_set_online_status(struct lightify_node *node, uint8_t state)
Definition: node.c:291
LIGHTIFY_EXPORT int lightify_node_get_cct(struct lightify_node *node)
Definition: node.c:263
LIGHTIFY_EXPORT int lightify_node_get_white(struct lightify_node *node)
Definition: node.c:252
lightify_node_type
Definition: liblightify.h:105
int green
Definition: node.c:64
int lightify_node_set_nodeadr(struct lightify_node *node, uint64_t adr)
Definition: node.c:169
LIGHTIFY_EXPORT enum lightify_node_type lightify_node_get_lamptype(struct lightify_node *node)
Definition: node.c:208
struct lightify_node * lightify_node_get_nextnode(struct lightify_node *node)
Definition: node.c:140
LIGHTIFY_EXPORT int lightify_node_get_onlinestate(struct lightify_node *node)
Definition: node.c:297
struct lightify_node * prev
Definition: node.c:50
LIGHTIFY_EXPORT int lightify_node_get_green(struct lightify_node *node)
Definition: node.c:241
uint64_t node_address
Definition: node.c:53
uint16_t zone_address
Definition: node.c:54
LIGHTIFY_EXPORT int lightify_node_get_blue(struct lightify_node *node)
Definition: node.c:230
int brightness
Definition: node.c:69
struct lightify_ctx * ctx
Definition: node.c:46
int lightify_node_set_white(struct lightify_node *node, int white)
Definition: node.c:246
int lightify_node_set_name(struct lightify_node *node, char *name)
Definition: node.c:151
LIGHTIFY_EXPORT uint16_t lightify_node_get_zoneadr(struct lightify_node *node)
Definition: node.c:186
int cct
Definition: node.c:67