282 static int decode_status(
unsigned char code) {
286 case 0x15:
return ENODEV;
298 if (!ctx)
return NULL;
313 static uint64_t uint64_from_msg(uint8_t *msg) {
315 tmp = msg[7]; tmp <<=8;
316 tmp |= msg[6]; tmp <<=8;
317 tmp |= msg[5]; tmp <<=8;
318 tmp |= msg[4]; tmp <<=8;
319 tmp |= msg[3]; tmp <<=8;
320 tmp |= msg[2]; tmp <<=8;
321 tmp |= msg[1]; tmp <<=8;
326 static void msg_from_uint64(
unsigned char *pmsg, uint64_t mac) {
327 *pmsg++ = mac & 0xff;
328 *pmsg++ = mac >> 8 & 0xff;
329 *pmsg++ = mac >> 16 & 0xff;
330 *pmsg++ = mac >> 24 & 0xff;
331 *pmsg++ = mac >> 32 & 0xff;
332 *pmsg++ = mac >> 40 & 0xff;
333 *pmsg++ = mac >> 48 & 0xff;
334 *pmsg++ = mac >> 56 & 0xff;
342 static uint16_t uint16_from_msg(uint8_t *msg) {
344 tmp = msg[0] | (msg[1]<<8);
358 static void fill_telegram_header(
unsigned char *msg,
unsigned int len,
unsigned long token,
unsigned char flags,
unsigned char command)
372 static int check_header_response(
unsigned char *msg,
unsigned long token,
375 unsigned long token2;
380 if (token != token2)
return -EPROTO;
388 if(!ctx)
return NULL;
396 if(!ctx)
return NULL;
402 if (!ctx)
return NULL;
408 if (!ctx)
return -EINVAL;
416 if (!ctx)
return -EINVAL;
433 if (!c)
return -ENOMEM;
438 #ifdef HAVE_SECURE_GETENV 441 env = secure_getenv(
"lightify_LOG");
446 info(c,
"ctx %p created\n", c);
462 dbg(ctx,
"freeing node %p.\n", ctx->
nodes);
470 dbg(ctx,
"freeing group %p.\n", ctx->
nodes);
476 if (!ctx)
return -EINVAL;
479 free_all_groups(ctx);
481 dbg(ctx,
"context %p freed.\n", ctx);
492 if (!ctx)
return -EINVAL;
516 info(ctx,
"socket_write_fn error %d\n", n);
527 info(ctx,
"socket_read_fn error %d\n", n);
537 n = check_header_response(msg, token, 0x13);
539 info(ctx,
"Invalid response (header)\n");
548 info(ctx,
"Reponse len unexpected for %d nodes: %d!=%d.\n", no_of_nodes,
554 info(ctx,
"strange byte at PAYLOAD_START: %d\n", msg[HEADER_PAYLOAD_START]);
559 while(no_of_nodes--) {
571 info(ctx,
"create node error %d", n);
625 static int lightify_request_set_onoff(
struct lightify_ctx *ctx, uint64_t adr,
int isgroup,
int onoff) {
626 unsigned char msg[32];
628 if (!ctx)
return -EINVAL;
631 onoff = (onoff != 0);
632 isgroup = (isgroup) ? 2 : 0;
634 long token = ++ctx->
cnt;
642 info(ctx,
"socket_write_fn error %d\n", n);
653 info(ctx,
"socket_read_fn error %d\n", n);
660 info(ctx,
" %d => %x\n ",i, msg[i]);
668 n = check_header_response(msg, token, 0x32);
670 info(ctx,
"Invalid response (header)\n");
677 info(ctx,
"unexected node mac / group adr %llx!=%llx", adr, adr2 );
683 info(ctx,
"state %d indicates error.\n", n);
688 static int lightify_request_set_cct(
struct lightify_ctx *ctx, uint64_t adr,
int isgroup,
unsigned int cct,
unsigned int fadetime) {
689 unsigned char msg[32];
691 if (!ctx)
return -EINVAL;
693 long token = ++ctx->
cnt;
694 isgroup = (isgroup) ? 2 : 0;
704 info(ctx,
"socket_write_fn error %d\n", n);
715 info(ctx,
"socket_read_fn error %d\n", n);
724 n = check_header_response(msg, token, 0x33);
726 info(ctx,
"Invalid response (header)\n");
733 info(ctx,
"unexected node mac / group adr %llx!=%llx", adr, adr2 );
741 static int lightify_request_set_rgbw(
struct lightify_ctx *ctx, uint64_t adr,
742 int isgroup,
unsigned int r,
unsigned int g,
743 unsigned int b,
unsigned int w,
unsigned int fadetime) {
744 unsigned char msg[32];
746 if (!ctx)
return -EINVAL;
749 long token = ++ctx->
cnt;
750 isgroup = (isgroup) ? 2 : 0;
762 info(ctx,
"socket_write_fn error %d\n", n);
778 n = check_header_response(msg, token, 0x36);
780 info(ctx,
"Invalid response (header)\n");
787 info(ctx,
"unexected node mac / group adr %llx!=%llx", adr, adr2 );
795 static int lightify_request_set_brightness(
struct lightify_ctx *ctx, uint64_t adr,
796 int isgroup,
unsigned int level,
unsigned int fadetime) {
797 unsigned char msg[32];
799 if (!ctx)
return -EINVAL;
801 long token = ++ctx->
cnt;
802 isgroup = (isgroup) ? 2 : 0;
813 info(ctx,
"socket_write_fn error %d\n", n);
824 info(ctx,
"socket_read_fn error %d\n", n);
832 n = check_header_response(msg, token, 0x31);
834 info(ctx,
"Invalid response (header)\n");
841 info(ctx,
"unexected node mac / group adr %llx!=%llx", adr, adr2 );
853 if (!ctx)
return -EINVAL;
857 onoff = (onoff != 0);
858 int ret = lightify_request_set_onoff(ctx, adr, 0, onoff);
878 if (!ctx || !node )
return -EINVAL;
880 int ret = lightify_request_set_cct(ctx, adr, 0 , cct, fadetime);
891 if (!ctx || !node )
return -EINVAL;
893 int ret = lightify_request_set_rgbw(ctx, adr, 0, r, g ,b ,w ,fadetime);
906 if (!ctx || !node )
return -EINVAL;
908 int ret = lightify_request_set_brightness(ctx, adr, 0, level, fadetime);
921 unsigned char msg[32];
923 if (!ctx)
return -EINVAL;
924 if (!node)
return -EINVAL;
927 long token = ++ctx->
cnt;
933 info(ctx,
"socket_write_fn error %d\n", n);
944 info(ctx,
"socket_read_fn error %d\n", n);
952 n = check_header_response(msg, token, 0x68);
954 info(ctx,
"Invalid response (header)\n");
983 if (!ctx)
return -EINVAL;
993 free_all_groups(ctx);
1006 info(ctx,
"socket_write_fn error %d\n", n);
1017 info(ctx,
"socket_read_fn error %d\n", n);
1027 n = check_header_response(msg, token, 0x1e);
1029 info(ctx,
"Invalid response (header)\n");
1036 info(ctx,
"0x1e: received %d bytes\n",m);
1038 info(ctx,
"Reponse len unexpected for %d groups: %d!=%d.\n", no_of_grps,
1044 info(ctx,
"strange byte at PAYLOAD_START: %d\n", msg[HEADER_PAYLOAD_START]);
1049 while(no_of_grps--) {
1060 info(ctx,
"create group error %d", n);
1074 if (!ctx || !group)
return -EINVAL;
1076 onoff = (onoff != 0);
1088 if (!ctx || !group)
return -EINVAL;
1102 unsigned int b,
unsigned int w,
unsigned int fadetime) {
1103 if (!ctx || !group)
return -EINVAL;
1120 if (!ctx || !group)
return -EINVAL;
int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node **newnode)
LIGHTIFY_EXPORT int lightify_node_request_rgbw(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int r, unsigned int g, unsigned int b, unsigned int w, unsigned int fadetime)
LIGHTIFY_EXPORT int lightify_free(struct lightify_ctx *ctx)
int lightify_node_set_grpadr(struct lightify_node *node, uint16_t adr)
int lightify_node_set_onoff(struct lightify_node *node, uint8_t on)
LIGHTIFY_EXPORT int lightify_node_request_scan(struct lightify_ctx *ctx)
#define info(ctx, arg...)
int(* read_from_socket_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
const char * lightify_node_get_name(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_group_request_onoff(struct lightify_ctx *ctx, struct lightify_group *group, int onoff)
LIGHTIFY_EXPORT int lightify_node_request_onoff(struct lightify_ctx *ctx, struct lightify_node *node, int onoff)
int lightify_node_remove(struct lightify_node *node)
int lightify_set_log_priority(struct lightify_ctx *ctx, int priority)
LIGHTIFY_EXPORT int lightify_group_request_scan(struct lightify_ctx *ctx)
int write_to_socket(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
int(* socket_read_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
Function pointer to the I/O handling – read from.
struct lightify_node * nodes
int lightify_node_set_brightness(struct lightify_node *node, int brightness)
uint64_t lightify_node_get_nodeadr(struct lightify_node *node)
struct lightify_node * lightify_node_get_prevnode(struct lightify_node *node)
LIGHTIFY_EXPORT void * lightify_get_userdata(struct lightify_ctx *ctx)
LIGHTIFY_EXPORT int lightify_group_request_cct(struct lightify_ctx *ctx, struct lightify_group *group, unsigned int cct, unsigned int fadetime)
LIGHTIFY_EXPORT int lightify_group_get_id(struct lightify_group *grp)
int lightify_node_set_stale(struct lightify_node *node, int stale)
LIGHTIFY_EXPORT int lightify_set_socket_fn(struct lightify_ctx *ctx, write_to_socket_fn fpw, read_from_socket_fn fpr)
void * userdata
Function pointer to the I/O handling – write to.
int lightify_node_set_zoneadr(struct lightify_node *node, uint16_t adr)
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_next(struct lightify_ctx *ctx, struct lightify_node *node)
int(* write_to_socket_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
int lightify_node_set_lamptype(struct lightify_node *node, enum lightify_node_type type)
int lightify_node_set_cct(struct lightify_node *node, int cct)
int(* socket_write_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
Function pointer to the I/O handling – read from.
LIGHTIFY_EXPORT int lightify_group_request_brightness(struct lightify_ctx *ctx, struct lightify_group *group, unsigned int level, unsigned int fadetime)
int lightify_node_set_blue(struct lightify_node *node, int blue)
int lightify_node_set_red(struct lightify_node *node, int red)
LIGHTIFY_EXPORT struct lightify_node * lightify_group_get_next_node(struct lightify_group *grp, struct lightify_node *lastnode)
int lightify_node_set_green(struct lightify_node *node, int green)
int lightify_node_set_online_status(struct lightify_node *node, uint8_t state)
LIGHTIFY_EXPORT int lightify_node_request_brightness(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int level, unsigned int fadetime)
LIGHTIFY_EXPORT int lightify_set_userdata(struct lightify_ctx *ctx, void *userdata)
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_previous(struct lightify_ctx *ctx, struct lightify_node *node)
int lightify_group_set_id(struct lightify_group *grp, int id)
void(* log_fn)(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)
LIGHTIFY_EXPORT int lightify_new(struct lightify_ctx **ctx, void *reserved)
int read_from_socket(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
int lightify_node_set_nodeadr(struct lightify_node *node, uint64_t adr)
int lightify_group_new(struct lightify_ctx *ctx, struct lightify_group **newgroup)
struct lightify_node * lightify_node_get_nextnode(struct lightify_node *node)
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_from_mac(struct lightify_ctx *ctx, uint64_t mac)
int log_priority(const char *priority)
LIGHTIFY_EXPORT int lightify_node_request_update(struct lightify_ctx *ctx, struct lightify_node *node)
int lightify_group_remove(struct lightify_group *grp)
LIGHTIFY_EXPORT int lightify_node_request_cct(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int cct, unsigned int fadetime)
void log_stderr(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)
struct lightify_ctx * ctx
LIGHTIFY_EXPORT int lightify_group_request_rgbw(struct lightify_ctx *ctx, struct lightify_group *group, unsigned int r, unsigned int g, unsigned int b, unsigned int w, unsigned int fadetime)
int lightify_node_set_white(struct lightify_node *node, int white)
int lightify_node_set_name(struct lightify_node *node, char *name)
int lightify_group_set_name(struct lightify_group *grp, const unsigned char *name)
struct lightify_group * groups