Contiki 3.x
ipso-light-control.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Yanzi Networks AB.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /**
32  * \addtogroup ipso-objects
33  * @{
34  *
35  */
36 
37 /**
38  * \file
39  * Implementation of OMA LWM2M / IPSO Light Control
40  * \author
41  * Joakim Eriksson <joakime@sics.se>
42  * Niclas Finne <nfi@sics.se>
43  */
44 
45 #include "ipso-objects.h"
46 #include "lwm2m-object.h"
47 #include "lwm2m-engine.h"
48 
49 #ifdef IPSO_LIGHT_CONTROL
50 extern const struct ipso_objects_actuator IPSO_LIGHT_CONTROL;
51 #endif /* IPSO_LIGHT_CONTROL */
52 /*---------------------------------------------------------------------------*/
53 static unsigned long last_on_time;
54 static uint32_t total_on_time;
55 static int dim_level = 0;
56 static uint8_t is_on = 0;
57 /*---------------------------------------------------------------------------*/
58 static int
59 read_state(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outsize)
60 {
61  return ctx->writer->write_boolean(ctx, outbuf, outsize, is_on ? 1 : 0);
62 }
63 /*---------------------------------------------------------------------------*/
64 static int
65 write_state(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t insize,
66  uint8_t *outbuf, size_t outsize)
67 {
68  int value;
69  size_t len;
70 
71  len = ctx->reader->read_boolean(ctx, inbuf, insize, &value);
72  if(len > 0) {
73  if(value) {
74  if(!is_on) {
75  is_on = 1;
76  last_on_time = clock_seconds();
77  }
78  } else {
79  if(is_on) {
80  total_on_time += clock_seconds() - last_on_time;
81  is_on = 0;
82  }
83  }
84 #ifdef IPSO_LIGHT_CONTROL
85  if(IPSO_LIGHT_CONTROL.set_on) {
86  IPSO_LIGHT_CONTROL.set_on(value);
87  } else if(IPSO_LIGHT_CONTROL.set_dim_level) {
88  dim_level = value ? 100 : 0;
89  IPSO_LIGHT_CONTROL.set_dim_level(dim_level);
90  }
91 #endif /* IPSO_LIGHT_CONTROL */
92  }
93  return len;
94 }
95 /*---------------------------------------------------------------------------*/
96 static int
97 read_dim(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outsize)
98 {
99  return ctx->writer->write_int(ctx, outbuf, outsize, dim_level);
100 }
101 /*---------------------------------------------------------------------------*/
102 static int
103 write_dim(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t insize,
104  uint8_t *outbuf, size_t outsize)
105 {
106  int32_t value;
107  size_t len;
108 
109  len = ctx->reader->read_int(ctx, inbuf, insize, &value);
110  if(len > 0) {
111  if(value < 0) {
112  value = 0;
113  } else if(value > 100) {
114  value = 100;
115  }
116 
117  dim_level = value;
118  if(value > 0) {
119  if(!is_on) {
120  is_on = 1;
121  last_on_time = clock_seconds();
122  }
123  } else {
124  if(is_on) {
125  total_on_time += clock_seconds() - last_on_time;
126  is_on = 0;
127  }
128  }
129 #ifdef IPSO_LIGHT_CONTROL
130  if(IPSO_LIGHT_CONTROL.set_dim_level) {
131  IPSO_LIGHT_CONTROL.set_dim_level(dim_level);
132  } else if(IPSO_LIGHT_CONTROL.set_on) {
133  IPSO_LIGHT_CONTROL.set_on(is_on);
134  }
135 #endif /* IPSO_LIGHT_CONTROL */
136  }
137  return len;
138 }
139 /*---------------------------------------------------------------------------*/
140 static int
141 read_on_time(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outsize)
142 {
143  unsigned long now;
144  if(is_on) {
145  /* Update the on time */
146  now = clock_seconds();
147  total_on_time += now - last_on_time;
148  last_on_time = now;
149  }
150  return ctx->writer->write_int(ctx, outbuf, outsize, (int32_t)total_on_time);
151 }
152 /*---------------------------------------------------------------------------*/
153 static int
154 write_on_time(lwm2m_context_t *ctx,
155  const uint8_t *inbuf, size_t insize,
156  uint8_t *outbuf, size_t outsize)
157 {
158  int32_t value;
159  size_t len;
160 
161  len = ctx->reader->read_int(ctx, inbuf, insize, &value);
162  if(len > 0 && value == 0) {
163  total_on_time = 0;
164  if(is_on) {
165  last_on_time = clock_seconds();
166  }
167  }
168  return len;
169 }
170 /*---------------------------------------------------------------------------*/
171 LWM2M_RESOURCES(light_control_resources,
172  LWM2M_RESOURCE_CALLBACK(5850, { read_state, write_state, NULL }),
173  LWM2M_RESOURCE_CALLBACK(5851, { read_dim, write_dim, NULL }),
174  LWM2M_RESOURCE_CALLBACK(5852, { read_on_time, write_on_time, NULL }),
175  );
176 LWM2M_INSTANCES(light_control_instances,
177  LWM2M_INSTANCE(0, light_control_resources));
178 LWM2M_OBJECT(light_control, 3311, light_control_instances);
179 /*---------------------------------------------------------------------------*/
180 void
181 ipso_light_control_init(void)
182 {
183 #ifdef IPSO_LIGHT_CONTROL
184  if(IPSO_LIGHT_CONTROL.init) {
185  IPSO_LIGHT_CONTROL.init();
186  }
187  if(IPSO_LIGHT_CONTROL.is_on) {
188  is_on = IPSO_LIGHT_CONTROL.is_on();
189  }
190  if(IPSO_LIGHT_CONTROL.get_dim_level) {
191  dim_level = IPSO_LIGHT_CONTROL.get_dim_level();
192  if(dim_level > 0 && IPSO_LIGHT_CONTROL.is_on == NULL) {
193  is_on = 1;
194  }
195  }
196 #endif /* IPSO_LIGHT_CONTROL */
197  last_on_time = clock_seconds();
198 
199  lwm2m_engine_register_object(&light_control);
200 }
201 /*---------------------------------------------------------------------------*/
202 /** @} */
Header file for the Contiki IPSO Objects for OMA LWM2M
Header file for the Contiki OMA LWM2M object API
Header file for the Contiki OMA LWM2M engine
#define NULL
The null pointer.
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:54