Go to the documentation of this file.
299 static int decode_status(
unsigned char code) {
303 case 0x15:
return ENODEV;
315 if (!
ctx)
return NULL;
330 static uint64_t uint64_from_msg(uint8_t *msg) {
332 tmp = msg[7]; tmp <<=8;
333 tmp |= msg[6]; tmp <<=8;
334 tmp |= msg[5]; tmp <<=8;
335 tmp |= msg[4]; tmp <<=8;
336 tmp |= msg[3]; tmp <<=8;
337 tmp |= msg[2]; tmp <<=8;
338 tmp |= msg[1]; tmp <<=8;
343 static void msg_from_uint64(
unsigned char *pmsg, uint64_t mac) {
344 *pmsg++ = mac & 0xff;
345 *pmsg++ = mac >> 8 & 0xff;
346 *pmsg++ = mac >> 16 & 0xff;
347 *pmsg++ = mac >> 24 & 0xff;
348 *pmsg++ = mac >> 32 & 0xff;
349 *pmsg++ = mac >> 40 & 0xff;
350 *pmsg++ = mac >> 48 & 0xff;
351 *pmsg++ = mac >> 56 & 0xff;
359 static uint16_t uint16_from_msg(uint8_t *msg) {
361 tmp = msg[0] | (msg[1]<<8);
375 static void fill_telegram_header(
unsigned char *msg,
unsigned int len, uint32_t token,
unsigned char flags,
unsigned char command)
389 static int check_header_response(
unsigned char *msg, uint32_t token,
397 if (token != token2)
return -EPROTO;
405 if(!
ctx)
return NULL;
413 if(!
ctx)
return NULL;
419 if (!
ctx)
return NULL;
425 if (!
ctx)
return -EINVAL;
433 if (!
ctx)
return -EINVAL;
450 if (!c)
return -ENOMEM;
455 #ifdef HAVE_SECURE_GETENV
458 env = secure_getenv(
"lightify_LOG");
463 info(c,
"ctx %p created\n", c);
481 dbg(ctx,
"freeing node %p.\n", ctx->
nodes);
489 dbg(ctx,
"freeing group %p.\n", ctx->
groups);
495 if (!ctx)
return -EINVAL;
498 free_all_groups(ctx);
500 dbg(ctx,
"context %p freed.\n", ctx);
512 if (!ctx)
return -EINVAL;
536 info(ctx,
"socket_write_fn error %d\n", n);
547 info(ctx,
"socket_read_fn error %d\n", n);
557 n = check_header_response(msg, token, 0x13);
559 info(ctx,
"Invalid response (header)\n");
566 if (!no_of_nodes)
return 0;
575 dbg(ctx,
"0x13: Dec-15 GW protocol\n");
580 dbg(ctx,
"0x13: Old GW protocol\n");
582 info(ctx,
"Response len unexpected for %d nodes: %d.\n", no_of_nodes, m);
592 while(no_of_nodes--) {
597 if (read_size != n ) {
598 info(
ctx,
"read node info: short read %d!=%d\n", read_size, n);
604 info(
ctx,
"create node error %d", n);
696 static int lightify_request_set_onoff(
struct lightify_ctx *
ctx, uint64_t adr,
int isgroup,
int onoff) {
697 unsigned char msg[32];
699 if (!
ctx)
return -EINVAL;
702 onoff = (onoff != 0);
703 isgroup = (isgroup) ? 2 : 0;
705 uint32_t token = ++
ctx->
cnt;
713 info(
ctx,
"socket_write_fn error %d\n", n);
724 info(
ctx,
"socket_read_fn error %d\n", n);
731 info(
ctx,
" %d => %x\n ",i, msg[i]);
739 n = check_header_response(msg, token, 0x32);
741 info(
ctx,
"Invalid response (header)\n");
748 info(
ctx,
"unexpected node mac / group adr %llx!=%llx", adr, adr2 );
754 info(
ctx,
"state %d indicates error.\n", n);
759 static int lightify_request_set_cct(
struct lightify_ctx *
ctx, uint64_t adr,
int isgroup,
unsigned int cct,
unsigned int fadetime) {
760 unsigned char msg[32];
762 if (!
ctx)
return -EINVAL;
764 uint32_t token = ++
ctx->
cnt;
765 isgroup = (isgroup) ? 2 : 0;
775 info(
ctx,
"socket_write_fn error %d\n", n);
786 info(
ctx,
"socket_read_fn error %d\n", n);
795 n = check_header_response(msg, token, 0x33);
797 info(
ctx,
"Invalid response (header)\n");
804 info(
ctx,
"unexpected node mac / group adr %llx!=%llx", adr, adr2 );
812 static int lightify_request_set_rgbw(
struct lightify_ctx *
ctx, uint64_t adr,
813 int isgroup,
unsigned int r,
unsigned int g,
814 unsigned int b,
unsigned int w,
unsigned int fadetime) {
815 unsigned char msg[32];
817 if (!
ctx)
return -EINVAL;
820 uint32_t token = ++
ctx->
cnt;
821 isgroup = (isgroup) ? 2 : 0;
833 info(
ctx,
"socket_write_fn error %d\n", n);
849 n = check_header_response(msg, token, 0x36);
851 info(
ctx,
"Invalid response (header)\n");
858 info(
ctx,
"unexpected node mac / group adr %llx!=%llx", adr, adr2 );
866 static int lightify_request_set_brightness(
struct lightify_ctx *
ctx, uint64_t adr,
867 int isgroup,
unsigned int level,
unsigned int fadetime) {
868 unsigned char msg[32];
870 if (!
ctx)
return -EINVAL;
872 uint32_t token = ++
ctx->
cnt;
873 isgroup = (isgroup) ? 2 : 0;
884 info(
ctx,
"socket_write_fn error %d\n", n);
895 info(
ctx,
"socket_read_fn error %d\n", n);
903 n = check_header_response(msg, token, 0x31);
905 info(
ctx,
"Invalid response (header)\n");
912 info(
ctx,
"unexpected node mac / group adr %llx!=%llx", adr, adr2 );
923 if (!
ctx)
return -EINVAL;
927 onoff = (onoff != 0);
928 int ret = lightify_request_set_onoff(
ctx, adr, 0, onoff);
948 if (!
ctx || !node )
return -EINVAL;
961 if (!
ctx || !node )
return -EINVAL;
963 int ret = lightify_request_set_rgbw(
ctx, adr, 0, r, g ,b ,w ,
fadetime);
976 if (!
ctx || !node )
return -EINVAL;
978 int ret = lightify_request_set_brightness(
ctx, adr, 0, level,
fadetime);
994 if (!
ctx)
return -EINVAL;
995 if (!node)
return -EINVAL;
998 uint32_t token = ++
ctx->
cnt;
1004 info(
ctx,
"socket_write_fn error %d\n", n);
1015 info(
ctx,
"socket_read_fn error %d\n", n);
1023 n = check_header_response(msg, token, 0x68);
1025 info(
ctx,
"Invalid response (header)\n");
1032 dbg_proto(
ctx,
"Node count expected 1 but is %u\n", (
unsigned int)n);
1038 dbg_proto(
ctx,
"Node address not matching! %lx != %lx\n",
1058 info(
ctx,
"socket_read_fn error %d\n", n);
1061 if (n != read_size) {
1062 info(
ctx,
"body short read %d!=%d\n", read_size, n);
1088 if (!
ctx)
return -EINVAL;
1098 free_all_groups(
ctx);
1111 info(
ctx,
"socket_write_fn error %d\n", n);
1122 info(
ctx,
"socket_read_fn error %d\n", n);
1132 n = check_header_response(msg, token, 0x1e);
1134 info(
ctx,
"Invalid response (header)\n");
1141 info(
ctx,
"0x1e: received %d bytes\n",m);
1143 info(
ctx,
"Response len unexpected for %d groups: %d!=%d.\n", no_of_grps,
1154 while(no_of_grps--) {
1165 info(
ctx,
"create group error %d", n);
1179 if (!
ctx || !group)
return -EINVAL;
1181 onoff = (onoff != 0);
1193 if (!
ctx || !group)
return -EINVAL;
1207 unsigned int b,
unsigned int w,
unsigned int fadetime) {
1208 if (!
ctx || !group)
return -EINVAL;
1225 if (!
ctx || !group)
return -EINVAL;
@ ANSWER_0x13_NODE_ADR64_B0
@ ANSWER_0x32_NODEADR64_B0
@ ANSWER_0x33_NODEADR64_B2
@ QUERY_0x36_NODEADR64_B0
@ ANSWER_0x32_NODEADR64_B4
@ ANSWER_0x33_NODEADR64_B6
@ ANSWER_0x36_RESERVED_MSB
@ ANSWER_0x33_NODEADR64_B4
@ QUERY_0x33_NODEADR64_B3
@ ANSWER_0x68_NODEADR64_B2
@ QUERY_0x33_FADETIME_MSB
@ ANSWER_0x68_NODEADR64_B5
@ ANSWER_0x36_NODEADR64_B4
LIGHTIFY_EXPORT int lightify_free(struct lightify_ctx *ctx)
@ ANSWER_0x36_NODEADR64_B0
LIGHTIFY_EXPORT int lightify_node_request_onoff(struct lightify_ctx *ctx, struct lightify_node *node, int onoff)
@ QUERY_0x31_FADETIME_LSB
#define info(ctx, arg...)
@ ANSWER_0x13_NODESCNT_LSB
@ ANSWER_0x13_NODE_ADR16_LSB
@ ANSWER_0x68_NODEADR64_B4
@ QUERY_0x32_NODEADR64_B0
@ QUERY_0x68_NODEADR64_B5
@ ANSWER_0x31_NODEADR64_B3
@ ANSWER_0x13_NODE_ONOFF_STATE
@ ANSWER_0x36_NODEADR64_B2
int log_priority(const char *priority)
int lightify_node_set_cct(struct lightify_node *node, int cct)
int lightify_node_set_stale(struct lightify_node *node, int stale)
int lightify_node_set_green(struct lightify_node *node, int green)
int lightify_node_set_online_status(struct lightify_node *node, uint8_t state)
@ ANSWER_0x1e_GRP_UNKNOWN_ZERO
@ QUERY_0x68_NODEADR64_B2
@ ANSWER_0x13_NODE_ADR64_B7
@ QUERY_0x68_NODEADR64_B6
@ ANSWER_0x31_NODEADR64_B4
LIGHTIFY_EXPORT int lightify_group_request_brightness(struct lightify_ctx *ctx, struct lightify_group *group, unsigned int level, unsigned int fadetime)
LIGHTIFY_EXPORT int lightify_node_request_scan(struct lightify_ctx *ctx)
LIGHTIFY_EXPORT int lightify_group_request_scan(struct lightify_ctx *ctx)
@ QUERY_0x33_NODEADR64_B2
int lightify_node_set_zoneadr(struct lightify_node *node, uint16_t adr)
const char * lightify_node_get_name(struct lightify_node *node)
@ QUERY_0x31_NODEADR64_B7
@ ANSWER_0x13_NODE_ADR64_B2
@ ANSWER_0x13_NODE_ADR64_B3
@ ANSWER_0x13_NODE_GRP_MEMBER_MSB
struct lightify_group * groups
@ ANSWER_0x36_NODEADR64_B1
@ ANSWER_0x13_NODE_ONLINE_STATE
int lightify_node_set_name(struct lightify_node *node, char *name)
int lightify_node_set_lamptype(struct lightify_node *node, enum lightify_node_type type)
@ QUERY_0x33_NODEADR64_B4
int(* read_from_socket_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
@ ANSWER_0x13_NODE_ADR64_B5
@ ANSWER_0x13_NODE_NAME_START
@ ANSWER_0x33_RESERVED_LSB
@ ANSWER_0x33_NODEADR64_B7
int write_to_socket(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
@ ANSWER_0x13_NODE_DIM_LEVEL
@ ANSWER_0x13_NODE_ADR64_B4
@ QUERY_0x32_NODEADR64_B7
@ ANSWER_0x13_NODE_ADR64_B6
@ QUERY_0x31_FADETIME_MSB
LIGHTIFY_EXPORT void * lightify_get_userdata(struct lightify_ctx *ctx)
int lightify_set_log_priority(struct lightify_ctx *ctx, int priority)
@ ANSWER_0x68_NONODES_LSB
@ ANSWER_0x68_NODEADR64_B1
@ QUERY_0x33_FADETIME_LSB
LIGHTIFY_EXPORT struct lightify_node * lightify_group_get_next_node(struct lightify_group *grp, struct lightify_node *lastnode)
uint64_t lightify_node_get_nodeadr(struct lightify_node *node)
@ QUERY_0x31_NODEADR64_B3
@ ANSWER_0x32_NODEADR64_B2
@ ANSWER_0x13_NODE_GRP_MEMBER_LSB
@ ANSWER_0x31_NODEADR64_B2
@ QUERY_0x32_NODEADR64_B6
@ QUERY_0x33_NODEADR64_B0
int lightify_node_set_brightness(struct lightify_node *node, int brightness)
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_previous(struct lightify_ctx *ctx, struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_set_socket_fn(struct lightify_ctx *ctx, write_to_socket_fn fpw, read_from_socket_fn fpr)
int lightify_node_new(struct lightify_ctx *ctx, struct lightify_node **newnode)
int lightify_node_set_grpadr(struct lightify_node *node, uint16_t adr)
@ ANSWER_0x32_NODEADR64_B3
struct lightify_ctx * ctx
@ QUERY_0x33_NODEADR64_B7
@ ANSWER_0x68_ONLINESTATE
@ ANSWER_0x13_NODESCNT_MSB
@ QUERY_0x36_NODEADR64_B5
@ QUERY_0x36_FADETIME_LSB
@ QUERY_0x36_FADETIME_MSB
@ QUERY_0x31_NODEADR64_B6
@ QUERY_0x32_NODEADR64_B2
@ ANSWER_0x33_NODEADR64_B5
@ QUERY_0x68_NODEADR64_B1
@ QUERY_0x68_NODEADR64_B0
@ ANSWER_0x68_NODEADR64_B0
@ QUERY_0x68_NODEADR64_B7
@ ANSWER_0x13_NODE_CCT_LSB
LIGHTIFY_EXPORT int lightify_node_request_brightness(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int level, unsigned int fadetime)
@ ANSWER_0x31_NODEADR64_B0
@ ANSWER_0x1e_HDR_UNKNOWN_ZERO
@ ANSWER_0x31_NODEADR64_B1
int lightify_group_remove(struct lightify_group *grp)
@ QUERY_0x68_NODEADR64_B4
@ QUERY_0x36_NODEADR64_B2
@ QUERY_0x31_NODEADR64_B5
@ QUERY_0x32_NODEADR64_B4
@ QUERY_0x31_NODEADR64_B0
@ ANSWER_0x32_RESERVED_LSB
int lightify_node_set_nodeadr(struct lightify_node *node, uint64_t adr)
@ ANSWER_0x32_RESERVED_MSB
int lightify_node_set_red(struct lightify_node *node, int red)
struct lightify_node * lightify_node_get_nextnode(struct lightify_node *node)
@ QUERY_0x36_NODEADR64_B6
int lightify_node_set_white(struct lightify_node *node, int white)
@ ANSWER_0x32_NODEADR64_B1
struct lightify_node * lightify_node_get_prevnode(struct lightify_node *node)
@ ANSWER_0x33_RESERVED_MSB
@ QUERY_0x32_NODEADR64_B1
@ QUERY_0x31_NODEADR64_B4
LIGHTIFY_EXPORT int lightify_new(struct lightify_ctx **ctx, void *reserved)
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_next(struct lightify_ctx *ctx, struct lightify_node *node)
@ ANSWER_0x13_NODE_NODETYPE
@ ANSWER_0x68_NODEADR64_B3
@ QUERY_0x68_NODEADR64_B3
@ ANSWER_0x13_NODE_LENGTH
LIGHTIFY_EXPORT int lightify_group_request_cct(struct lightify_ctx *ctx, struct lightify_group *group, unsigned int cct, unsigned int fadetime)
void * userdata
Function pointer to the I/O handling – write to.
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)
LIGHTIFY_EXPORT int lightify_group_get_id(struct lightify_group *grp)
int lightify_node_set_onoff(struct lightify_node *node, uint8_t on)
@ ANSWER_0x68_NONODES_MSB
@ ANSWER_0x13_NODE_ADR64_B1
@ ANSWER_0x36_NODEADR64_B3
@ QUERY_0x33_NODEADR64_B1
@ ANSWER_0x13_NODE_CCT_MSB
void log_stderr(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)
int lightify_group_set_id(struct lightify_group *grp, int id)
@ QUERY_0x31_NODEADR64_B2
LIGHTIFY_EXPORT int lightify_group_request_onoff(struct lightify_ctx *ctx, struct lightify_group *group, int onoff)
struct lightify_node * nodes
int read_from_socket(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
@ ANSWER_0x68_REQUEST_STATUS
@ QUERY_0x33_NODEADR64_B6
@ ANSWER_0x32_NODEADR64_B5
@ ANSWER_0x13_NODE_ADR16_MSB
@ ANSWER_0x36_NODEADR64_B5
@ ANSWER_0x31_NODEADR64_B6
LIGHTIFY_EXPORT int lightify_set_userdata(struct lightify_ctx *ctx, void *userdata)
int lightify_node_set_blue(struct lightify_node *node, int blue)
int lightify_group_new(struct lightify_ctx *ctx, struct lightify_group **newgroup)
@ ANSWER_0x36_RESERVED_LSB
@ QUERY_0x36_NODEADR64_B3
#define dbg_proto(ctx, arg...)
struct lightify_ctx * ctx
@ QUERY_0x36_NODEADR64_B1
@ QUERY_0x31_NODEADR64_B1
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_from_mac(struct lightify_ctx *ctx, uint64_t mac)
int(* socket_read_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
Function pointer to the I/O handling – read from.
@ ANSWER_0x33_NODEADR64_B1
int(* write_to_socket_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
int lightify_node_remove(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_node_request_update(struct lightify_ctx *ctx, struct lightify_node *node)
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)
@ ANSWER_0x33_NODEADR64_B0
@ QUERY_0x32_NODEADR64_B5
@ ANSWER_0x31_NODEADR64_B7
void(* log_fn)(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)
@ QUERY_0x33_NODEADR64_B5
@ ANSWER_0x33_NODEADR64_B3
int(* socket_write_fn)(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
Function pointer to the I/O handling – read from.
@ LIGHTIFY_EXT_COLOUR_LIGHT
@ QUERY_0x32_NODEADR64_B3
@ ANSWER_0x32_NODEADR64_B6
@ ANSWER_0x32_NODEADR64_B7
@ ANSWER_0x31_NODEADR64_B5
int lightify_group_set_name(struct lightify_group *grp, const unsigned char *name)
@ ANSWER_0x36_NODEADR64_B6
@ ANSWER_0x68_NODEADR64_B7
LIGHTIFY_EXPORT int lightify_node_request_cct(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int cct, unsigned int fadetime)
@ QUERY_0x36_NODEADR64_B7
@ QUERY_0x36_NODEADR64_B4
@ ANSWER_0x68_NODEADR64_B6
@ ANSWER_0x36_NODEADR64_B7