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 }
lightify_node::next
struct lightify_node * next
Definition: node.c:49
lightify_node::cct
int cct
Definition: node.c:67
lightify_node::brightness
int brightness
Definition: node.c:69
lightify_node_get_lamptype
LIGHTIFY_EXPORT enum lightify_node_type lightify_node_get_lamptype(struct lightify_node *node)
Definition: node.c:208
lightify_node_get_brightness
LIGHTIFY_EXPORT int lightify_node_get_brightness(struct lightify_node *node)
Definition: node.c:274
lightify_node_set_cct
int lightify_node_set_cct(struct lightify_node *node, int cct)
Definition: node.c:257
lightify_node_set_stale
int lightify_node_set_stale(struct lightify_node *node, int stale)
Definition: node.c:307
lightify_node_set_green
int lightify_node_set_green(struct lightify_node *node, int green)
Definition: node.c:235
lightify_node::is_on
int is_on
Definition: node.c:71
lightify_node_set_online_status
int lightify_node_set_online_status(struct lightify_node *node, uint8_t state)
Definition: node.c:291
MAX_NODE_NANE_LEN
#define MAX_NODE_NANE_LEN
Definition: node.c:39
lightify_node_set_zoneadr
int lightify_node_set_zoneadr(struct lightify_node *node, uint16_t adr)
Definition: node.c:180
lightify_node_get_name
const LIGHTIFY_EXPORT char * lightify_node_get_name(struct lightify_node *node)
Definition: node.c:164
lightify_node_is_on
LIGHTIFY_EXPORT int lightify_node_is_on(struct lightify_node *node)
Definition: node.c:286
lightify_node_set_name
int lightify_node_set_name(struct lightify_node *node, char *name)
Definition: node.c:151
lightify_node_set_lamptype
int lightify_node_set_lamptype(struct lightify_node *node, enum lightify_node_type type)
Definition: node.c:202
LIGHTIFY_EXPORT
#define LIGHTIFY_EXPORT
lightify_node::online_status
enum lightify_node_online_state online_status
Definition: node.c:72
lightify_node_get_nodeadr
LIGHTIFY_EXPORT uint64_t lightify_node_get_nodeadr(struct lightify_node *node)
Definition: node.c:175
lightify_node_get_grpadr
LIGHTIFY_EXPORT uint16_t lightify_node_get_grpadr(struct lightify_node *node)
Definition: node.c:197
lightify_node_set_brightness
int lightify_node_set_brightness(struct lightify_node *node, int brightness)
Definition: node.c:268
lightify_node_new
int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node **newnode)
Definition: node.c:80
lightify_node_set_grpadr
int lightify_node_set_grpadr(struct lightify_node *node, uint16_t adr)
Definition: node.c:191
lightify_node_get_red
LIGHTIFY_EXPORT int lightify_node_get_red(struct lightify_node *node)
Definition: node.c:219
lightify_node::ctx
struct lightify_ctx * ctx
Definition: node.c:46
lightify_node::node_type
enum lightify_node_type node_type
Definition: node.c:58
node.h
lightify_node::white
int white
Definition: node.c:66
lightify_node_get_green
LIGHTIFY_EXPORT int lightify_node_get_green(struct lightify_node *node)
Definition: node.c:241
lightify_node_type
lightify_node_type
Definition: liblightify.h:105
lightify_node
Definition: node.c:43
lightify_node::green
int green
Definition: node.c:64
lightify_node_set_nodeadr
int lightify_node_set_nodeadr(struct lightify_node *node, uint64_t adr)
Definition: node.c:169
lightify_node_set_red
int lightify_node_set_red(struct lightify_node *node, int red)
Definition: node.c:213
lightify_node_get_nextnode
struct lightify_node * lightify_node_get_nextnode(struct lightify_node *node)
Definition: node.c:140
lightify_node_online_state
lightify_node_online_state
Definition: liblightify.h:119
lightify_node_set_white
int lightify_node_set_white(struct lightify_node *node, int white)
Definition: node.c:246
lightify_ctx
Definition: context.h:68
lightify_node_get_zoneadr
LIGHTIFY_EXPORT uint16_t lightify_node_get_zoneadr(struct lightify_node *node)
Definition: node.c:186
lightify_node::group_address
uint16_t group_address
Definition: node.c:55
lightify_node_get_prevnode
struct lightify_node * lightify_node_get_prevnode(struct lightify_node *node)
Definition: node.c:145
lightify_node_get_cct
LIGHTIFY_EXPORT int lightify_node_get_cct(struct lightify_node *node)
Definition: node.c:263
lightify_node_get_white
LIGHTIFY_EXPORT int lightify_node_get_white(struct lightify_node *node)
Definition: node.c:252
lightify_node_set_onoff
int lightify_node_set_onoff(struct lightify_node *node, uint8_t on)
Definition: node.c:280
lightify_ctx::nodes
struct lightify_node * nodes
Definition: context.h:90
lightify_node::red
int red
Definition: node.c:63
context.h
liblightify-private.h
lightify_node_set_blue
int lightify_node_set_blue(struct lightify_node *node, int blue)
Definition: node.c:224
lightify_node::blue
int blue
Definition: node.c:65
lightify_node::name
char * name
Definition: node.c:61
lightify_node_get_onlinestate
LIGHTIFY_EXPORT int lightify_node_get_onlinestate(struct lightify_node *node)
Definition: node.c:297
lightify_node_remove
int lightify_node_remove(struct lightify_node *node)
Definition: node.c:117
lightify_node::node_address
uint64_t node_address
Definition: node.c:53
lightify_node_is_stale
LIGHTIFY_EXPORT int lightify_node_is_stale(struct lightify_node *node)
Definition: node.c:302
lightify_node::prev
struct lightify_node * prev
Definition: node.c:50
lightify_node::is_stale
int is_stale
Definition: node.c:77
lightify_node::zone_address
uint16_t zone_address
Definition: node.c:54
lightify_node_get_blue
LIGHTIFY_EXPORT int lightify_node_get_blue(struct lightify_node *node)
Definition: node.c:230