node.c
Go to the documentation of this file.
/*
liblightify -- library to control OSRAM's LIGHTIFY
Copyright (c) 2015, Tobias Frost <tobi@coldtobi.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "liblightify-private.h"
#include "node.h"
#include "context.h"
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MAX_NODE_NANE_LEN (16)
struct lightify_node {
struct lightify_ctx *ctx;
/* linked list */
struct lightify_node *next;
struct lightify_node *prev;
/* node address and groups */
uint64_t node_address;
uint16_t zone_address;
uint16_t group_address;
enum lightify_node_type node_type;
char *name;
int red;
int green;
int blue;
int white;
int cct;
int brightness;
int is_on;
enum lightify_node_online_state online_status;
int is_stale;
};
int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node** newnode) {
struct lightify_node *n;
struct lightify_node *m;
if (!ctx) return -EINVAL;
n = calloc(1,sizeof(struct lightify_node));
if (!n) return -ENOMEM;
*newnode = n;
n->red = -1;
n->green = -1;
n->blue = -1;
n->white = -1;
n->cct = -1;
n->brightness = -1;
n->is_on = -1;
n->online_status = -1;
n->ctx = ctx;
m = ctx->nodes;
if (!m) {
ctx->nodes = n;
return 0;
}
while(m->next) m=m->next;
n->prev = m;
m->next = n;
return 0;
}
int lightify_node_remove(struct lightify_node* node) {
if (!node) return -EINVAL;
struct lightify_node *next = node->next;
struct lightify_node *prev = node->prev;
if (prev) {
prev->next = next;
} else {
// first node
node->ctx->nodes=next;
}
if (next) next->prev = prev;
if (node->name) free(node->name);
free(node);
return 0;
}
struct lightify_node* lightify_node_get_nextnode(struct lightify_node *node) {
if (!node) return NULL;
return node->next;
}
struct lightify_node* lightify_node_get_prevnode(struct lightify_node *node) {
if (!node) return NULL;
return node->prev;
}
int lightify_node_set_name(struct lightify_node* node, char *name) {
if (!node) return -EINVAL;
if (node->name) free(node->name);
node->name = NULL;
if(name) {
node->name = strndup(name, MAX_NODE_NANE_LEN);
if (!node->name) return -ENOMEM;
}
return 0;
}
LIGHTIFY_EXPORT const char* lightify_node_get_name(struct lightify_node* node) {
if (!node) return NULL;
return node->name;
}
int lightify_node_set_nodeadr(struct lightify_node* node, uint64_t adr) {
if(!node) return -EINVAL;
node->node_address=adr;
return 0;
}
LIGHTIFY_EXPORT uint64_t lightify_node_get_nodeadr(struct lightify_node* node) {
if (!node) return 0;
return node->node_address;
}
int lightify_node_set_zoneadr(struct lightify_node* node, uint16_t adr) {
if(!node) return -EINVAL;
node->zone_address=adr;
return 0;
}
LIGHTIFY_EXPORT uint16_t lightify_node_get_zoneadr(struct lightify_node* node) {
if (!node) return 0;
return node->zone_address;
}
int lightify_node_set_grpadr(struct lightify_node* node, uint16_t adr) {
if(!node) return -EINVAL;
node->group_address=adr;
return 0;
}
LIGHTIFY_EXPORT uint16_t lightify_node_get_grpadr(struct lightify_node* node) {
if (!node) return 0;
return node->group_address;
}
int lightify_node_set_lamptype(struct lightify_node* node, enum lightify_node_type type) {
if(!node) return -EINVAL;
node->node_type = type;
return 0;
}
LIGHTIFY_EXPORT enum lightify_node_type lightify_node_get_lamptype(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->node_type;
}
int lightify_node_set_red(struct lightify_node* node, int red) {
if(!node) return -EINVAL;
node->red = red;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_red(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->red;
}
int lightify_node_set_blue(struct lightify_node* node, int blue) {
if(!node) return -EINVAL;
node->blue = blue;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_blue(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->blue;
}
int lightify_node_set_green(struct lightify_node* node, int green) {
if(!node) return -EINVAL;
node->green = green;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_green(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->green;
}
int lightify_node_set_white(struct lightify_node* node, int white) {
if(!node) return -EINVAL;
node->white = white;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_white(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->white;
}
int lightify_node_set_cct(struct lightify_node* node, int cct) {
if(!node) return -EINVAL;
node->cct = cct;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_cct(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->cct;
}
int lightify_node_set_brightness(struct lightify_node* node, int brightness) {
if(!node) return -EINVAL;
node->brightness = brightness;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_brightness(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->brightness;
}
int lightify_node_set_onoff(struct lightify_node* node, uint8_t on) {
if (!node) return -EINVAL;
node->is_on= on;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_is_on(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->is_on;
}
int lightify_node_set_online_status(struct lightify_node* node, uint8_t state) {
if (!node) return -EINVAL;
node->online_status= state;
return 0;
}
LIGHTIFY_EXPORT int lightify_node_get_onlinestate(struct lightify_node* node) {
if(!node) return -EINVAL;
return node->online_status;
}
LIGHTIFY_EXPORT int lightify_node_is_stale(struct lightify_node *node) {
if(!node) return -EINVAL;
return node->is_stale;
}
int lightify_node_set_stale(struct lightify_node *node, int stale) {
if(!node) return -EINVAL;
node->is_stale = stale;
return 0;
}
lightify_node_new
int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node **newnode)
Definition: node.c:80
lightify_node::name
char * name
Definition: node.c:61
lightify_node::group_address
uint16_t group_address
Definition: node.c:55
MAX_NODE_NANE_LEN
#define MAX_NODE_NANE_LEN
Definition: node.c:39
lightify_node::online_status
enum lightify_node_online_state online_status
Definition: node.c:72
lightify_node::is_stale
int is_stale
Definition: node.c:77
lightify_node::next
struct lightify_node * next
Definition: node.c:49
node.h
context.h
lightify_node_set_grpadr
int lightify_node_set_grpadr(struct lightify_node *node, uint16_t adr)
Definition: node.c:191
lightify_node_set_onoff
int lightify_node_set_onoff(struct lightify_node *node, uint8_t on)
Definition: node.c:280
lightify_node_get_name
LIGHTIFY_EXPORT const char * lightify_node_get_name(struct lightify_node *node)
Definition: node.c:164
lightify_node::white
int white
Definition: node.c:66
lightify_node::node_type
enum lightify_node_type node_type
Definition: node.c:58
lightify_node_get_brightness
LIGHTIFY_EXPORT int lightify_node_get_brightness(struct lightify_node *node)
Definition: node.c:274
lightify_node_remove
int lightify_node_remove(struct lightify_node *node)
Definition: node.c:117
lightify_ctx::nodes
struct lightify_node * nodes
Definition: context.h:85
lightify_node_set_brightness
int lightify_node_set_brightness(struct lightify_node *node, int brightness)
Definition: node.c:268
lightify_node_get_nodeadr
LIGHTIFY_EXPORT uint64_t lightify_node_get_nodeadr(struct lightify_node *node)
Definition: node.c:175
lightify_node_get_prevnode
struct lightify_node * lightify_node_get_prevnode(struct lightify_node *node)
Definition: node.c:145
lightify_node_is_stale
LIGHTIFY_EXPORT int lightify_node_is_stale(struct lightify_node *node)
Definition: node.c:302
lightify_ctx
Definition: context.h:63
lightify_node_is_on
LIGHTIFY_EXPORT int lightify_node_is_on(struct lightify_node *node)
Definition: node.c:286
lightify_node_set_stale
int lightify_node_set_stale(struct lightify_node *node, int stale)
Definition: node.c:307
lightify_node_set_zoneadr
int lightify_node_set_zoneadr(struct lightify_node *node, uint16_t adr)
Definition: node.c:180
liblightify-private.h
lightify_node_set_lamptype
int lightify_node_set_lamptype(struct lightify_node *node, enum lightify_node_type type)
Definition: node.c:202
lightify_node_set_cct
int lightify_node_set_cct(struct lightify_node *node, int cct)
Definition: node.c:257
LIGHTIFY_EXPORT
#define LIGHTIFY_EXPORT
lightify_node_set_blue
int lightify_node_set_blue(struct lightify_node *node, int blue)
Definition: node.c:224
lightify_node_get_grpadr
LIGHTIFY_EXPORT uint16_t lightify_node_get_grpadr(struct lightify_node *node)
Definition: node.c:197
lightify_node::is_on
int is_on
Definition: node.c:71
lightify_node_set_red
int lightify_node_set_red(struct lightify_node *node, int red)
Definition: node.c:213
lightify_node::blue
int blue
Definition: node.c:65
lightify_node_get_red
LIGHTIFY_EXPORT int lightify_node_get_red(struct lightify_node *node)
Definition: node.c:219
lightify_node_online_state
lightify_node_online_state
Definition: liblightify.h:118
lightify_node::red
int red
Definition: node.c:63
lightify_node_set_green
int lightify_node_set_green(struct lightify_node *node, int green)
Definition: node.c:235
lightify_node_set_online_status
int lightify_node_set_online_status(struct lightify_node *node, uint8_t state)
Definition: node.c:291
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_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_get_lamptype
LIGHTIFY_EXPORT enum lightify_node_type lightify_node_get_lamptype(struct lightify_node *node)
Definition: node.c:208
lightify_node_get_nextnode
struct lightify_node * lightify_node_get_nextnode(struct lightify_node *node)
Definition: node.c:140
lightify_node_get_onlinestate
LIGHTIFY_EXPORT int lightify_node_get_onlinestate(struct lightify_node *node)
Definition: node.c:297
lightify_node::prev
struct lightify_node * prev
Definition: node.c:50
lightify_node_get_green
LIGHTIFY_EXPORT int lightify_node_get_green(struct lightify_node *node)
Definition: node.c:241
lightify_node::node_address
uint64_t node_address
Definition: node.c:53
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
lightify_node::brightness
int brightness
Definition: node.c:69
lightify_node::ctx
struct lightify_ctx * ctx
Definition: node.c:46
lightify_node_set_white
int lightify_node_set_white(struct lightify_node *node, int white)
Definition: node.c:246
lightify_node_set_name
int lightify_node_set_name(struct lightify_node *node, char *name)
Definition: node.c:151
lightify_node_get_zoneadr
LIGHTIFY_EXPORT uint16_t lightify_node_get_zoneadr(struct lightify_node *node)
Definition: node.c:186
lightify_node::cct
int cct
Definition: node.c:67