liblightify
groups.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 #include "groups.h"
34 
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
47  struct lightify_ctx *ctx;
49  /* Linked list */
50  struct lightify_group *next;
51  struct lightify_group *prev;
54  int id;
55 
57  char *name;
58 };
59 
60 int lightify_group_new(struct lightify_ctx *ctx, struct lightify_group **newgroup) {
61 
62  struct lightify_group *g, *ctx_g;
63  if (!ctx)
64  return -EINVAL;
65 
66  g = calloc(1, sizeof(struct lightify_group));
67  if (!g)
68  return -ENOMEM;
69 
70  ctx_g = ctx->groups;
71  if (!ctx_g) {
72  ctx->groups = g;
73  } else {
74  while (ctx_g->next)
75  ctx_g = ctx_g->next;
76  ctx_g->next = g;
77  g->prev = ctx_g;
78  }
79  *newgroup = g;
80  g->ctx = ctx;
81  return 0;
82 }
83 
85  if (!grp) return -EINVAL;
86 
87  struct lightify_group *next = grp->next;
88  struct lightify_group *prev = grp->prev;
89 
90  if (prev) {
91  prev->next = next;
92  } else {
93  // first grp
94  grp->ctx->groups=next;
95  }
96 
97  if (next) next->prev = prev;
98 
99  if (grp->name) free(grp->name);
100  free(grp);
101  return 0;
102 }
103 
104 int lightify_group_set_name(struct lightify_group *grp, const unsigned char *name) {
105  if (!grp || !name) {
106  return -EINVAL;
107  }
108 
109  if (grp->name) free(grp->name);
110  grp->name = strndup((char*)name, 17);
111  if (!grp->name) {
112  return -ENOMEM;
113  }
114  return 0;
115 }
116 
118  if (!grp) return 0;
119  return grp->name;
120 }
121 
122 int lightify_group_set_id(struct lightify_group *grp, int id) {
123  if(!grp) return -EINVAL;
124  grp->id = id;
125  return 0;
126 }
127 
129  if(!grp) return -EINVAL;
130  return grp->id;
131 }
132 
133 // #FIXME export and document
135  if (!ctx) return NULL;
136  if (!current) return ctx->groups;
137  return current->next;
138 }
139 
141  if (!ctx) return NULL;
142  if (!current) return NULL;
143  return current->prev;
144 }
145 
146 // #FIXME export and document
148  if (!grp) return NULL;
149  uint16_t grpmask = 1 << (grp->id-1);
150 
151  while ( (lastnode = lightify_node_get_next(grp->ctx, lastnode))) {
152  if ( grpmask & lightify_node_get_grpadr(lastnode)) return lastnode;
153  }
154  return NULL;
155 }
struct lightify_group * prev
Definition: groups.c:51
struct lightify_group * next
Definition: groups.c:50
LIGHTIFY_EXPORT struct lightify_group * lightify_group_get_next(struct lightify_ctx *ctx, struct lightify_group *current)
Definition: groups.c:134
LIGHTIFY_EXPORT int lightify_group_get_id(struct lightify_group *grp)
Definition: groups.c:128
LIGHTIFY_EXPORT struct lightify_node * lightify_node_get_next(struct lightify_ctx *ctx, struct lightify_node *node)
Definition: context.c:385
struct lightify_ctx * ctx
Definition: groups.c:47
#define LIGHTIFY_EXPORT
uint16_t lightify_node_get_grpadr(struct lightify_node *node)
Definition: node.c:197
LIGHTIFY_EXPORT struct lightify_node * lightify_group_get_next_node(struct lightify_group *grp, struct lightify_node *lastnode)
Definition: groups.c:147
int lightify_group_set_id(struct lightify_group *grp, int id)
Definition: groups.c:122
int lightify_group_new(struct lightify_ctx *ctx, struct lightify_group **newgroup)
Definition: groups.c:60
LIGHTIFY_EXPORT struct lightify_group * lightify_group_get_previous(struct lightify_ctx *ctx, struct lightify_group *current)
Definition: groups.c:140
int lightify_group_remove(struct lightify_group *grp)
Definition: groups.c:84
char * name
Definition: groups.c:57
LIGHTIFY_EXPORT const char * lightify_group_get_name(struct lightify_group *grp)
Definition: groups.c:117
int lightify_group_set_name(struct lightify_group *grp, const unsigned char *name)
Definition: groups.c:104
struct lightify_group * groups
Definition: context.h:88