Contiki 3.x
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 oma-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 
48 #define DEBUG 0
49 #if DEBUG
50 #include <stdio.h>
51 #define PRINTF(...) printf(__VA_ARGS__)
52 #else
53 #define PRINTF(...)
54 #endif
55 
56 static int32_t time_offset = 0;
57 /*---------------------------------------------------------------------------*/
58 static int
59 read_lwtime(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outsize)
60 {
61  return ctx->writer->write_int(ctx, outbuf, outsize,
62  time_offset + clock_seconds());
63 }
64 /*---------------------------------------------------------------------------*/
65 static int
66 set_lwtime(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t insize,
67  uint8_t *outbuf, size_t outsize)
68 {
69  /* assume that this only read one TLV value */
70  int32_t lw_time;
71  size_t len = ctx->reader->read_int(ctx, inbuf, insize, &lw_time);
72  if(len == 0) {
73  PRINTF("FAIL: could not read time '%*.s'\n", (int)insize, inbuf);
74  } else {
75  PRINTF("Got: time: %*.s => %" PRId32 "\n", (int)insize, inbuf, lw_time);
76 
77  time_offset = lw_time - clock_seconds();
78  PRINTF("Write time...%" PRId32 " => offset = %" PRId32 "\n",
79  lw_time, time_offset);
80  }
81  /* return the number of bytes read */
82  return len;
83 }
84 /*---------------------------------------------------------------------------*/
85 #ifdef PLATFORM_REBOOT
86 static struct ctimer reboot_timer;
87 static void
88 do_the_reboot(void *ptr)
89 {
90  PLATFORM_REBOOT();
91 }
92 static int
93 reboot(lwm2m_context_t *ctx, const uint8_t *arg, size_t argsize,
94  uint8_t *outbuf, size_t outsize)
95 {
96  PRINTF("Device will reboot!\n");
97  ctimer_set(&reboot_timer, CLOCK_SECOND / 2, do_the_reboot, NULL);
98  return 0;
99 }
100 #endif /* PLATFORM_REBOOT */
101 /*---------------------------------------------------------------------------*/
102 #ifdef PLATFORM_FACTORY_DEFAULT
103 static int
104 factory_reset(lwm2m_context_t *ctx, const uint8_t *arg, size_t arg_size,
105  uint8_t *outbuf, size_t outsize)
106 {
107  PRINTF("Device will do factory default!\n");
108  PLATFORM_FACTORY_DEFAULT();
109  return 0;
110 }
111 #endif /* PLATFORM_FACTORY_DEFAULT */
112 /*---------------------------------------------------------------------------*/
113 LWM2M_RESOURCES(device_resources,
114 #ifdef LWM2M_DEVICE_MANUFACTURER
115  LWM2M_RESOURCE_STRING(0, LWM2M_DEVICE_MANUFACTURER),
116 #endif /* LWM2M_DEVICE_MANUFACTURER */
117 #ifdef LWM2M_DEVICE_TYPE
118  LWM2M_RESOURCE_STRING(17, LWM2M_DEVICE_TYPE),
119 #endif /* LWM2M_DEVICE_TYPE */
120 #ifdef LWM2M_DEVICE_MODEL_NUMBER
121  LWM2M_RESOURCE_STRING(1, LWM2M_DEVICE_MODEL_NUMBER),
122 #endif /* LWM2M_DEVICE_MODEL_NUMBER */
123 #ifdef LWM2M_DEVICE_SERIAL_NO
124  LWM2M_RESOURCE_STRING(2, LWM2M_DEVICE_SERIAL_NO),
125 #endif /* LWM2M_DEVICE_SERIAL_NO */
126 #ifdef LWM2M_DEVICE_FIRMWARE_VERSION
127  LWM2M_RESOURCE_STRING(3, LWM2M_DEVICE_FIRMWARE_VERSION),
128 #endif /* LWM2M_DEVICE_FIRMWARE_VERSION */
129 #ifdef PLATFORM_REBOOT
130  LWM2M_RESOURCE_CALLBACK(4, { NULL, NULL, reboot }),
131 #endif /* PLATFORM_REBOOT */
132 #ifdef PLATFORM_FACTORY_DEFAULT
133  LWM2M_RESOURCE_CALLBACK(5, { NULL, NULL, factory_reset }),
134 #endif /* PLATFORM_FACTORY_DEFAULT */
135  /* Current Time */
136  LWM2M_RESOURCE_CALLBACK(13, { read_lwtime, set_lwtime, NULL }),
137  );
138 LWM2M_INSTANCES(device_instances, LWM2M_INSTANCE(0, device_resources));
139 LWM2M_OBJECT(device, 3, device_instances);
140 /*---------------------------------------------------------------------------*/
141 void
143 {
144  /**
145  * Register this device and its handlers - the handlers
146  * automatically sends in the object to handle.
147  */
148  PRINTF("*** Init lwm2m-device\n");
149  lwm2m_engine_register_object(&device);
150 }
151 /*---------------------------------------------------------------------------*/
152 /** @} */
Header file for the Contiki OMA LWM2M device
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
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:54
void lwm2m_device_init(void)
Definition: lwm2m-device.c:142