Contiki-NG
lwm2m-firmware.c
1 /*
2  * Copyright (c) 2016, SICS Swedish ICT 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  * Code for firmware object of lwm2m
36  *
37  */
38 
39 #include "lwm2m-engine.h"
40 #include "lwm2m-firmware.h"
41 #include "coap.h"
42 #include <inttypes.h>
43 #include <string.h>
44 
45 /* Log configuration */
46 #include "coap-log.h"
47 #define LOG_MODULE "lwm2m-fw"
48 #define LOG_LEVEL LOG_LEVEL_LWM2M
49 
50 #define UPDATE_PACKAGE 0
51 #define UPDATE_PACKAGE_URI 1
52 #define UPDATE_UPDATE 2
53 #define UPDATE_STATE 3
54 #define UPDATE_RESULT 5
55 
56 #define STATE_IDLE 1
57 #define STATE_DOWNLOADING 2
58 #define STATE_DOWNLOADED 3
59 
60 #define RESULT_DEFAULT 0
61 #define RESULT_SUCCESS 1
62 #define RESULT_NO_STORAGE 2
63 #define RESULT_OUT_OF_MEM 3
64 #define RESULT_CONNECTION_LOST 4
65 #define RESULT_CRC_FAILED 5
66 #define RESULT_UNSUPPORTED_FW 6
67 #define RESULT_INVALID_URI 7
68 
69 
70 static uint8_t state = STATE_IDLE;
71 static uint8_t result = RESULT_DEFAULT;
72 
73 static lwm2m_object_instance_t reg_object;
74 
75 static const lwm2m_resource_id_t resources[] =
76  { WO(UPDATE_PACKAGE),
77  WO(UPDATE_PACKAGE_URI),
78  RO(UPDATE_STATE),
79  RO(UPDATE_RESULT),
80  EX(UPDATE_UPDATE)
81  };
82 
83 /*---------------------------------------------------------------------------*/
84 static lwm2m_status_t
85 lwm2m_callback(lwm2m_object_instance_t *object,
86  lwm2m_context_t *ctx)
87 {
88  uint32_t num;
89  uint8_t more;
90  uint16_t size;
91  uint32_t offset;
92 
93  LOG_DBG("Got request at: %d/%d/%d lv:%d\n", ctx->object_id,
94  ctx->object_instance_id, ctx->resource_id, ctx->level);
95 
96  if(ctx->level == 1 || ctx->level == 2) {
97  /* Should not happen - as it will be taken care of by the lwm2m engine itself. */
98  return LWM2M_STATUS_ERROR;
99  }
100 
101  if(ctx->operation == LWM2M_OP_READ) {
102  switch(ctx->resource_id) {
103  case UPDATE_STATE:
104  lwm2m_object_write_int(ctx, state); /* 1 means idle */
105  return LWM2M_STATUS_OK;
106  case UPDATE_RESULT:
107  lwm2m_object_write_int(ctx, result); /* 0 means default */
108  return LWM2M_STATUS_OK;
109  }
110  } else if(ctx->operation == LWM2M_OP_WRITE) {
111 
112  if(LOG_DBG_ENABLED) {
113  if(coap_get_header_block1(ctx->request, &num, &more, &size, &offset)) {
114  LOG_DBG("CoAP BLOCK1: %"PRIu32"/%u/%u offset:%"PRIu32
115  " LWM2M CTX->offset=%"PRIu32"\n",
116  num, more, size, offset, ctx->offset);
117  }
118  }
119 
120  switch(ctx->resource_id) {
121  case UPDATE_PACKAGE:
122  /* The firmware is written */
123  LOG_DBG("Firmware received: %"PRIu32" %d fin:%d\n", ctx->offset,
124  (int)ctx->inbuf->size, lwm2m_object_is_final_incoming(ctx));
125  if(lwm2m_object_is_final_incoming(ctx)) {
126  state = STATE_DOWNLOADED;
127  } else {
128  state = STATE_DOWNLOADING;
129  }
130  return LWM2M_STATUS_OK;
131  case UPDATE_PACKAGE_URI:
132  /* The firmware URI is written */
133  LOG_DBG("Firmware URI received: %"PRIu32" %d fin:%d\n", ctx->offset,
134  (int)ctx->inbuf->size, lwm2m_object_is_final_incoming(ctx));
135  if(LOG_DBG_ENABLED) {
136  int i;
137  LOG_DBG("Data: '");
138  for(i = 0; i < ctx->inbuf->size; i++) {
139  LOG_DBG_("%c", ctx->inbuf->buffer[i]);
140  }
141  LOG_DBG_("'\n");
142  }
143  return LWM2M_STATUS_OK;
144  }
145  } else if(ctx->operation == LWM2M_OP_EXECUTE && ctx->resource_id == UPDATE_UPDATE) {
146  /* Perform the update operation */
147  if(state == STATE_DOWNLOADED) {
148  return LWM2M_STATUS_OK;
149  }
150  /* Failure... */
151  }
152  return LWM2M_STATUS_ERROR;
153 }
154 
155 /*---------------------------------------------------------------------------*/
156 void
157 lwm2m_firmware_init(void)
158 {
159  reg_object.object_id = 5;
160  reg_object.instance_id = 0;
161  reg_object.callback = lwm2m_callback;
162  reg_object.resource_ids = resources;
163  reg_object.resource_count = sizeof(resources) / sizeof(lwm2m_resource_id_t);
164 
165  lwm2m_engine_add_object(&reg_object);
166 }
167 /*---------------------------------------------------------------------------*/
168 /** @} */
Log support for CoAP
Header file for the Contiki OMA LWM2M engine
An implementation of the Constrained Application Protocol (RFC 7252).