Contiki-NG
lwm2m-queue-mode-object.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, RISE SICS 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 Queue Mode object
39  to manage the parameters from the server side
40  * \author
41  * Carlos Gonzalo Peces <carlosgp143@gmail.com>
42  */
43 
44 #include "lwm2m-object.h"
45 #include "lwm2m-queue-mode.h"
46 
47 /* Log configuration */
48 #include "coap-log.h"
49 #define LOG_MODULE "lwm2m-qmode-object"
50 #define LOG_LEVEL LOG_LEVEL_LWM2M
51 
52 #if LWM2M_QUEUE_MODE_ENABLED && LWM2M_QUEUE_MODE_OBJECT_ENABLED
53 
54 #define LWM2M_QUEUE_MODE_OBJECT_ID 30000
55 #define LWM2M_AWAKE_TIME_ID 30000
56 #define LWM2M_SLEEP_TIME_ID 30001
57 
58 #if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION
59 #define LWM2M_DYNAMIC_ADAPTATION_FLAG_ID 30002
60 #define UPDATE_WITH_MEAN 0 /* 1-mean time 0-maximum time */
61 #endif
62 
63 static const lwm2m_resource_id_t resources[] =
64 { RW(LWM2M_AWAKE_TIME_ID),
65  RW(LWM2M_SLEEP_TIME_ID),
66 #if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION
67  RW(LWM2M_DYNAMIC_ADAPTATION_FLAG_ID),
68 #endif
69 };
70 
71 /*---------------------------------------------------------------------------*/
72 static lwm2m_status_t
73 lwm2m_callback(lwm2m_object_instance_t *object, lwm2m_context_t *ctx)
74 {
75  if(ctx->operation == LWM2M_OP_READ) {
76  switch(ctx->resource_id) {
77  case LWM2M_AWAKE_TIME_ID:
78  lwm2m_object_write_int(ctx, (int32_t)lwm2m_queue_mode_get_awake_time());
79  return LWM2M_STATUS_OK;
80  case LWM2M_SLEEP_TIME_ID:
81  lwm2m_object_write_int(ctx, (int32_t)lwm2m_queue_mode_get_sleep_time());
82  return LWM2M_STATUS_OK;
83 #if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION
84  case LWM2M_DYNAMIC_ADAPTATION_FLAG_ID:
85  lwm2m_object_write_int(ctx, (int32_t)lwm2m_queue_mode_get_dynamic_adaptation_flag());
86  return LWM2M_STATUS_OK;
87 #endif
88  }
89  } else if(ctx->operation == LWM2M_OP_WRITE) {
90  switch(ctx->resource_id) {
91  int32_t value_read;
92  size_t len;
93 
94  case LWM2M_AWAKE_TIME_ID:
95 
96  len = lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size,
97  &value_read);
98  LOG_DBG("Client Awake Time write request value: %d\n", (int)value_read);
99  if(len == 0) {
100  LOG_WARN("FAIL: could not write awake time\n");
101  return LWM2M_STATUS_WRITE_ERROR;
102  } else {
103  lwm2m_queue_mode_set_awake_time(value_read);
104  return LWM2M_STATUS_OK;
105  }
106 
107  case LWM2M_SLEEP_TIME_ID:
108  len = lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size,
109  &value_read);
110  LOG_DBG("Client Sleep Time write request value: %d\n", (int)value_read);
111  if(len == 0) {
112  LOG_WARN("FAIL: could not write sleep time\n");
113  return LWM2M_STATUS_WRITE_ERROR;
114  } else {
115  lwm2m_queue_mode_set_sleep_time(value_read);
116  return LWM2M_STATUS_OK;
117  }
118 #if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION
119  case LWM2M_DYNAMIC_ADAPTATION_FLAG_ID:
120  len = lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size,
121  &value_read);
122  LOG_DBG("Dynamic Adaptation Flag request value: %d\n", (int)value_read);
123  if(len == 0) {
124  LOG_WARN("FAIL: could not write dynamic flag\n");
125  return LWM2M_STATUS_WRITE_ERROR;
126  } else {
127  lwm2m_queue_mode_set_dynamic_adaptation_flag(value_read);
128  return LWM2M_STATUS_OK;
129  }
130 #endif
131  }
132  }
133 
134  return LWM2M_STATUS_OPERATION_NOT_ALLOWED;
135 }
136 /*---------------------------------------------------------------------------*/
137 static lwm2m_object_instance_t queue_object = {
138  .object_id = LWM2M_QUEUE_MODE_OBJECT_ID,
139  .instance_id = 0,
140  .resource_ids = resources,
141  .resource_count = sizeof(resources) / sizeof(lwm2m_resource_id_t),
142  .resource_dim_callback = NULL,
143  .callback = lwm2m_callback,
144 };
145 /*---------------------------------------------------------------------------*/
146 void
147 lwm2m_queue_mode_object_init(void)
148 {
149  lwm2m_engine_add_object(&queue_object);
150 }
151 #endif /* LWM2M_QUEUE_MODE_ENABLED && LWM2M_QUEUE_MODE_OBJECT_ENABLED */
152 /** @} */
Log support for CoAP
Header file for the LWM2M object API
Header file for the Contiki OMA LWM2M Queue Mode implementation to manage the parameters ...