40 #ifndef SRC_LIBLIGHTIFY___LIGHTIFY___HPP_
41 #define SRC_LIBLIGHTIFY___LIGHTIFY___HPP_
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
55 #undef LIGHTIFY_ALLOW_THROW
56 #ifdef LIGHTIFY_ALLOW_THROW
80 unsigned long long GetMAC(
void)
const {
182 int SetRGBW(
int red,
int green,
int blue,
int white,
int time) {
268 int SetRGBW(
int red,
int green,
int blue,
int white,
int time) {
288 _ctx = NULL; _group = NULL;
311 _host = strdup(host);
322 #ifdef LIGHTIFY_ALLOW_THROW
323 if (
err < 0 || !_ctx) {
324 throw std::bad_alloc;
331 if (_host) free(_host);
332 if (_sockfd != -1) close(_sockfd);
347 struct sockaddr_in serv_addr;
348 struct hostent *server;
350 _sockfd = socket(AF_INET, SOCK_STREAM, 0);
359 server = gethostbyname(_host);
360 if (server == NULL) {
363 if (
err >= 0)
err = -EHOSTUNREACH;
367 memset((
char *) &serv_addr, 0,
sizeof(serv_addr));
368 serv_addr.sin_family = AF_INET;
369 memcpy((
char *) &serv_addr.sin_addr.s_addr, (
char *) server->h_addr,
371 serv_addr.sin_port = htons(_port);
374 err = connect(_sockfd, (
struct sockaddr*) &serv_addr,
sizeof(serv_addr));
388 if (_sockfd > 0)
Close();
396 if (_sockfd == -1)
return -EBADF;
398 int local_socketfd = _sockfd;
401 while (--i && 0 != close(local_socketfd)) {
402 if (errno != EINTR)
return errno;
404 if (!i)
return -EINTR;
413 if (_sockfd == -1)
return -EBADF;
418 struct lean_nodemap *last_inserted = NULL;
421 struct lean_nodemap *nm =
new lean_nodemap();
422 if (!nm)
return -ENOMEM;
430 if (!last_inserted) {
433 last_inserted->next = nm;
450 if (_sockfd == -1)
return -EBADF;
455 struct lean_groupmap *last_inserted = NULL;
458 struct lean_groupmap *gm =
new lean_groupmap();
459 if (!gm)
return -ENOMEM;
467 if (!last_inserted) {
470 last_inserted->next = gm;
495 struct lean_nodemap *nm = _nodesmap;
497 if (nm->node->GetMAC() == mac)
return nm->node;
509 if (x >= _no_nodes)
return NULL;
510 struct lean_nodemap *nm = _nodesmap;
511 while(nm && x--) nm = nm->next;
512 return (nm ? nm->node : NULL);
521 if (pos >= _no_groups)
return NULL;
522 struct lean_groupmap *gm = _groupsmap;
523 while(pos--) gm = gm->next;
540 return (_sockfd > 0);
553 if (!host)
return -EINVAL;
554 if (
port > 0xFFFFUL)
return -EINVAL;
556 if (_host) free(_host);
557 _host = strdup(host);
558 if (!_host)
return -ENOMEM;
573 void _free_nodemap(
void) {
574 struct lean_nodemap *nmtmp, *nm = _nodesmap;
585 void _free_groupmap(
void) {
586 struct lean_groupmap *gmtmp, *gm = _groupsmap;
603 struct lean_nodemap {
604 struct lean_nodemap *next;
608 struct lean_groupmap {
609 struct lean_groupmap *next;
613 struct lean_nodemap *_nodesmap;
614 struct lean_groupmap *_groupsmap;
int TurnOnOff(bool onoff)
int SetBrightness(int level, int time)
Lightify_Group(struct lightify_ctx *ctx, struct lightify_group *group)
int SetCCT(int cct, int time)
int SetRGBW(int red, int green, int blue, int white, int time)
Enable the use of exception within this wrapper.
int GetBrightness(void) const
unsigned int GetGroup(void) const
unsigned long long GetMAC(void) const
const char * GetName(void) const
int TurnOnOff(bool onoff)
Lightify_Node(struct lightify_ctx *ctx, struct lightify_node *node)
int GetOnlineState(void) const
int SetBrightness(int level, int time)
lightify_node_type GetLampType(void) const
int SetCCT(int cct, int time)
unsigned int GetZoneAdr(void) const
int SetRGBW(int red, int green, int blue, int white, int time)
Lightify(const char *host, unsigned int port=4000)
Lightify_Node * GetNodeAtPosX(int x) const
int TurnAllOnOff(bool onoff)
Lightify_Group * GetGroupAtPosX(int pos) const
Lightify_Node * GetNode(long long mac)
struct lightify_ctx * GetCtx(void)
const struct lightify_ctx * GetLightifyContext(void) const
int SetHostname(const char *host, unsigned int port=4000)
LIGHTIFY_EXPORT int lightify_new(struct lightify_ctx **ctx, void *reserved)
LIGHTIFY_EXPORT int lightify_free(struct lightify_ctx *ctx)
LIGHTIFY_EXPORT const char * lightify_group_get_name(struct lightify_group *grp)
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)
LIGHTIFY_EXPORT struct lightify_group * lightify_group_get_next(struct lightify_ctx *ctx, struct lightify_group *current)
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_group_request_scan(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)
LIGHTIFY_EXPORT int lightify_group_request_onoff(struct lightify_ctx *ctx, struct lightify_group *group, int onoff)
int lightify_skt_setfd(struct lightify_ctx *ctx, int socket)
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)
enum lightify_node_type lightify_node_get_lamptype(struct lightify_node *node)
int lightify_node_get_cct(struct lightify_node *node)
int lightify_node_get_red(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_node_request_brightness(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int level, unsigned int fadetime)
uint16_t lightify_node_get_grpadr(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_node_request_onoff(struct lightify_ctx *ctx, struct lightify_node *node, int onoff)
int lightify_node_get_green(struct lightify_node *node)
int lightify_node_is_on(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_node_request_cct(struct lightify_ctx *ctx, struct lightify_node *node, unsigned int cct, unsigned int fadetime)
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_next(struct lightify_ctx *ctx, struct lightify_node *node)
int lightify_node_get_onlinestate(struct lightify_node *node)
int lightify_node_is_stale(struct lightify_node *node)
uint64_t lightify_node_get_nodeadr(struct lightify_node *node)
int lightify_node_get_blue(struct lightify_node *node)
const char * lightify_node_get_name(struct lightify_node *node)
int lightify_node_get_white(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_node_request_scan(struct lightify_ctx *ctx)
uint16_t lightify_node_get_zoneadr(struct lightify_node *node)
LIGHTIFY_EXPORT int lightify_node_request_update(struct lightify_ctx *ctx, struct lightify_node *node)
int lightify_node_get_brightness(struct lightify_node *node)
struct lightify_ctx * _ctx