Contiki 3.x
ipso-temperature.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  * \file
38  * Implementation of OMA LWM2M / IPSO Temperature
39  * \author
40  * Joakim Eriksson <joakime@sics.se>
41  * Niclas Finne <nfi@sics.se>
42  */
43 
44 #include <stdint.h>
45 #include "ipso-objects.h"
46 #include "lwm2m-object.h"
47 #include "lwm2m-engine.h"
48 #include "er-coap-engine.h"
49 
50 #ifdef IPSO_TEMPERATURE
51 extern const struct ipso_objects_sensor IPSO_TEMPERATURE;
52 #endif /* IPSO_TEMPERATURE */
53 
54 #ifndef IPSO_TEMPERATURE_MIN
55 #define IPSO_TEMPERATURE_MIN (-50 * LWM2M_FLOAT32_FRAC)
56 #endif
57 
58 #ifndef IPSO_TEMPERATURE_MAX
59 #define IPSO_TEMPERATURE_MAX (80 * LWM2M_FLOAT32_FRAC)
60 #endif
61 
62 static struct ctimer periodic_timer;
63 static int32_t min_temp;
64 static int32_t max_temp;
65 static int read_temp(int32_t *value);
66 /*---------------------------------------------------------------------------*/
67 static int
68 temp(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outsize)
69 {
70  int32_t value;
71  if(read_temp(&value)) {
72  return ctx->writer->write_float32fix(ctx, outbuf, outsize,
73  value, LWM2M_FLOAT32_BITS);
74  }
75  return 0;
76 }
77 /*---------------------------------------------------------------------------*/
78 LWM2M_RESOURCES(temperature_resources,
79  /* Temperature (Current) */
80  LWM2M_RESOURCE_CALLBACK(5700, { temp, NULL, NULL }),
81  /* Units */
82  LWM2M_RESOURCE_STRING(5701, "Cel"),
83  /* Min Range Value */
84  LWM2M_RESOURCE_FLOATFIX(5603, IPSO_TEMPERATURE_MIN),
85  /* Max Range Value */
86  LWM2M_RESOURCE_FLOATFIX(5604, IPSO_TEMPERATURE_MAX),
87  /* Min Measured Value */
88  LWM2M_RESOURCE_FLOATFIX_VAR(5601, &min_temp),
89  /* Max Measured Value */
90  LWM2M_RESOURCE_FLOATFIX_VAR(5602, &max_temp),
91  );
92 LWM2M_INSTANCES(temperature_instances,
93  LWM2M_INSTANCE(0, temperature_resources));
94 LWM2M_OBJECT(temperature, 3303, temperature_instances);
95 /*---------------------------------------------------------------------------*/
96 static int
97 read_temp(int32_t *value)
98 {
99 #ifdef IPSO_TEMPERATURE
100  int32_t temp;
101  if(IPSO_TEMPERATURE.read_value == NULL ||
102  IPSO_TEMPERATURE.read_value(&temp) != 0) {
103  return 0;
104  }
105 
106  /* Convert milliCelsius to fix float */
107  *value = (temp * LWM2M_FLOAT32_FRAC) / 1000;
108 
109  if(*value < min_temp) {
110  min_temp = *value;
111  lwm2m_object_notify_observers(&temperature, "/0/5601");
112  }
113  if(*value > max_temp) {
114  max_temp = *value;
115  lwm2m_object_notify_observers(&temperature, "/0/5602");
116  }
117  return 1;
118 #else /* IPSO_TEMPERATURE */
119  return 0;
120 #endif /* IPSO_TEMPERATURE */
121 }
122 /*---------------------------------------------------------------------------*/
123 static void
124 handle_periodic_timer(void *ptr)
125 {
126  static int32_t last_value = IPSO_TEMPERATURE_MIN;
127  int32_t v;
128 
129  /* Only notify when the value has changed since last */
130  if(read_temp(&v) && v != last_value) {
131  last_value = v;
132  lwm2m_object_notify_observers(&temperature, "/0/5700");
133  }
134  ctimer_reset(&periodic_timer);
135 }
136 /*---------------------------------------------------------------------------*/
137 void
138 ipso_temperature_init(void)
139 {
140  int32_t v;
141  min_temp = IPSO_TEMPERATURE_MAX;
142  max_temp = IPSO_TEMPERATURE_MIN;
143 
144 #ifdef IPSO_TEMPERATURE
145  if(IPSO_TEMPERATURE.init) {
146  IPSO_TEMPERATURE.init();
147  }
148 #endif /* IPSO_TEMPERATURE */
149 
150  /* register this device and its handlers - the handlers automatically
151  sends in the object to handle */
152  lwm2m_engine_register_object(&temperature);
153 
154  /* update temp and min/max + notify any listeners */
155  read_temp(&v);
156  ctimer_set(&periodic_timer, CLOCK_SECOND * 10, handle_periodic_timer, NULL);
157 }
158 /*---------------------------------------------------------------------------*/
159 /** @} */
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.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
CoAP implementation for the REST Engine.