Contiki-NG
Loading...
Searching...
No Matches
lwm2m-device.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 lwm2m
33 * @{
34 */
35
36/**
37 * \file
38 * Implementation of the Contiki OMA LWM2M device
39 * \author
40 * Joakim Eriksson <joakime@sics.se>
41 * Niclas Finne <nfi@sics.se>
42 */
43
44#include "lwm2m-object.h"
45#include "lwm2m-device.h"
46#include "lwm2m-engine.h"
47#include <string.h>
48
49/* Log configuration */
50#include "coap-log.h"
51#define LOG_MODULE "lwm2m-dev"
52#define LOG_LEVEL LOG_LEVEL_LWM2M
53
54static const lwm2m_resource_id_t resources[] =
55 { RO(LWM2M_DEVICE_MANUFACTURER_ID),
56 RO(LWM2M_DEVICE_MODEL_NUMBER_ID),
57 RO(LWM2M_DEVICE_SERIAL_NUMBER_ID),
58 RO(LWM2M_DEVICE_FIRMWARE_VERSION_ID),
59 RO(LWM2M_DEVICE_AVAILABLE_POWER_SOURCES), /* Multi-resource-instance */
60 RO(LWM2M_DEVICE_POWER_SOURCE_VOLTAGE), /* Multi-resource-instance */
61 RO(LWM2M_DEVICE_POWER_SOURCE_CURRENT), /* Multi-resource-instance */
62 RO(LWM2M_DEVICE_TYPE_ID),
63 EX(LWM2M_DEVICE_REBOOT_ID),
64 RW(LWM2M_DEVICE_TIME_ID),
65 EX(LWM2M_DEVICE_FACTORY_DEFAULT_ID),
66 };
67
68#ifndef LWM2M_DEVICE_MANUFACTURER
69#define LWM2M_DEVICE_MANUFACTURER "RISE SICS"
70#endif
71#ifndef LWM2M_DEVICE_MODEL_NUMBER
72#define LWM2M_DEVICE_MODEL_NUMBER "1"
73#endif
74#ifndef LWM2M_DEVICE_SERIAL_NUMBER
75#define LWM2M_DEVICE_SERIAL_NUMBER "1"
76#endif
77#ifndef LWM2M_DEVICE_FIRMWARE_VERSION
78#define LWM2M_DEVICE_FIRMWARE_VERSION CONTIKI_VERSION
79#endif
80#ifndef LWM2M_DEVICE_TYPE
81#define LWM2M_DEVICE_TYPE "Contiki-NG LWM2M"
82#endif
83
84/* All three must be defined */
85#ifndef LWM2M_DEVICE_POWER_AVAILABLE
86#define LWM2M_DEVICE_POWER_AVAILABLE {1,5}
87#define LWM2M_DEVICE_POWER_VOLTAGE {2500,5000}
88#define LWM2M_DEVICE_POWER_CURRENT {500,1000}
89#endif
90
91static int32_t time_offset = 0;
92
93/* Internal battery and USB - just for test...*/
94static uint16_t power_avail[] = LWM2M_DEVICE_POWER_AVAILABLE;
95static uint16_t power_voltage[] = LWM2M_DEVICE_POWER_VOLTAGE;
96static uint16_t power_current[] = LWM2M_DEVICE_POWER_CURRENT;
97/*---------------------------------------------------------------------------*/
98int32_t
99lwm2m_device_get_time(void)
100{
101 return coap_timer_seconds() + time_offset;
102}
103/*---------------------------------------------------------------------------*/
104void
105lwm2m_device_set_time(int32_t time)
106{
107 time_offset = time - coap_timer_seconds();
108}
109/*---------------------------------------------------------------------------*/
110static lwm2m_status_t
111write_string(lwm2m_context_t *ctx, const char *text)
112{
113 lwm2m_object_write_string(ctx, text, strlen(text));
114 return LWM2M_STATUS_OK;
115}
116/*---------------------------------------------------------------------------*/
117static int
118output_multi_i16(lwm2m_context_t *ctx, const uint16_t *data, int count)
119{
120 int i;
121 size_t len;
122 len = lwm2m_object_write_enter_ri(ctx);
123 for(i = 0; i < count; i++) {
124 len += lwm2m_object_write_int_ri(ctx, i, data[i]);
125 }
126 len += lwm2m_object_write_exit_ri(ctx);
127 return len;
128}
129
130/*---------------------------------------------------------------------------*/
131static int
132lwm2m_dim_callback(lwm2m_object_instance_t *object, uint16_t resource_id)
133{
134 switch(resource_id) {
135 case LWM2M_DEVICE_AVAILABLE_POWER_SOURCES:
136 case LWM2M_DEVICE_POWER_SOURCE_VOLTAGE:
137 case LWM2M_DEVICE_POWER_SOURCE_CURRENT:
138 return sizeof(power_avail) / sizeof(uint16_t);
139 break;
140 }
141 /* zero means that it is no dim parameter to send?? */
142 return 0;
143}
144/*---------------------------------------------------------------------------*/
145static lwm2m_status_t
146lwm2m_callback(lwm2m_object_instance_t *object, lwm2m_context_t *ctx)
147{
148 if(ctx->operation == LWM2M_OP_READ) {
149 switch(ctx->resource_id) {
150 case LWM2M_DEVICE_MANUFACTURER_ID:
151 return write_string(ctx, LWM2M_DEVICE_MANUFACTURER);
152 case LWM2M_DEVICE_MODEL_NUMBER_ID:
153 return write_string(ctx, LWM2M_DEVICE_MODEL_NUMBER);
154 case LWM2M_DEVICE_SERIAL_NUMBER_ID:
155 return write_string(ctx, LWM2M_DEVICE_SERIAL_NUMBER);
156 case LWM2M_DEVICE_FIRMWARE_VERSION_ID:
157 return write_string(ctx, LWM2M_DEVICE_FIRMWARE_VERSION);
158 case LWM2M_DEVICE_TYPE_ID:
159 return write_string(ctx, LWM2M_DEVICE_TYPE);
160 case LWM2M_DEVICE_TIME_ID:
161 LOG_DBG("Reading time: %u\n", (unsigned int)lwm2m_device_get_time());
162 lwm2m_object_write_int(ctx, lwm2m_device_get_time());
163 return LWM2M_STATUS_OK;
164 case LWM2M_DEVICE_AVAILABLE_POWER_SOURCES:
165 /* Power Multi-resource case - just use array index as ID */
166 output_multi_i16(ctx, power_avail,
167 sizeof(power_avail)/sizeof(uint16_t));
168 return LWM2M_STATUS_OK;
169 case LWM2M_DEVICE_POWER_SOURCE_VOLTAGE:
170 output_multi_i16(ctx, power_voltage,
171 sizeof(power_voltage)/sizeof(uint16_t));
172 return LWM2M_STATUS_OK;
173 case LWM2M_DEVICE_POWER_SOURCE_CURRENT:
174 output_multi_i16(ctx, power_current,
175 sizeof(power_current)/sizeof(uint16_t));
176 return LWM2M_STATUS_OK;
177 default:
178 LOG_WARN("Not found: %u\n", ctx->resource_id);
179 return LWM2M_STATUS_NOT_FOUND;
180 }
181
182 } else if(ctx->operation == LWM2M_OP_EXECUTE) {
183 if(ctx->resource_id == LWM2M_DEVICE_REBOOT_ID) {
184 /* Do THE REBOOT */
185 LOG_INFO("REBOOT\n");
186 return LWM2M_STATUS_OK;
187 }
188
189 } else if(ctx->operation == LWM2M_OP_WRITE) {
190 if(ctx->resource_id == LWM2M_DEVICE_TIME_ID) {
191 int32_t lw_time;
192 size_t len;
193 len = lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size,
194 &lw_time);
195 if(len == 0) {
196 LOG_WARN("FAIL: could not write time\n");
197 return LWM2M_STATUS_WRITE_ERROR;
198 } else {
199 lwm2m_device_set_time(lw_time);
200 LOG_DBG("Write time %lu sec => offset = %ld\n",
201 (unsigned long)lw_time, (long)time_offset);
202 return LWM2M_STATUS_OK;
203 }
204 }
205 }
206
207 return LWM2M_STATUS_OPERATION_NOT_ALLOWED;
208}
209/*---------------------------------------------------------------------------*/
210static lwm2m_object_instance_t device = {
211 .object_id = LWM2M_OBJECT_DEVICE_ID,
212 .instance_id = 0,
213 .resource_ids = resources,
214 .resource_count = sizeof(resources) / sizeof(lwm2m_resource_id_t),
215 .resource_dim_callback = lwm2m_dim_callback,
216 .callback = lwm2m_callback,
217};
218/*---------------------------------------------------------------------------*/
219void
220lwm2m_device_init(void)
221{
222 lwm2m_engine_add_object(&device);
223}
224/*---------------------------------------------------------------------------*/
225/** @} */
Log support for CoAP.
static volatile uint64_t count
Num.
Definition clock.c:50
static uint32_t coap_timer_seconds(void)
Get the time since boot in seconds.
Definition coap-timer.h:93
Header file for the Contiki OMA LWM2M device.
Header file for the Contiki OMA LWM2M engine.
Header file for the LWM2M object API.