Contiki-NG
Loading...
Searching...
No Matches
ipso-blockwise-test.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 ipso-objects
33 * @{
34 *
35 * Code to test blockwise transfer together with the LWM2M-engine.
36 * This object tests get with BLOCK2 and put with BLOCK1.
37 *
38 */
39
40#include "lwm2m-engine.h"
41#include "coap.h"
42#include <string.h>
43
44#define DEBUG 0
45#if DEBUG
46#include <stdio.h>
47#define PRINTF(...) printf(__VA_ARGS__)
48#else
49#define PRINTF(...)
50#endif
51
52static lwm2m_object_instance_t reg_object;
53static char junk[64];
54
55static const lwm2m_resource_id_t resources[] =
56 {
57 RO(10000),
58 RO(11000),
59 RW(11001)
60 };
61
62#define LEN 900
63
64static lwm2m_status_t
65opaque_callback(lwm2m_object_instance_t *object,
66 lwm2m_context_t *ctx, int num_to_write)
67{
68 int i;
69 PRINTF("opaque-stream callback num_to_write: %d off: %d outlen: %d\n",
70 num_to_write, ctx->offset, ctx->outbuf->len);
71 for(i = 0; i < num_to_write; i++) {
72 ctx->outbuf->buffer[i + ctx->outbuf->len] = '0' + (i & 31);
73 if(i + ctx->offset == LEN) break;
74 }
75 ctx->outbuf->len += i;
76 if(ctx->offset + i < LEN) {
77 ctx->writer_flags |= WRITER_HAS_MORE;
78 }
79 return LWM2M_STATUS_OK;
80}
81
82/*---------------------------------------------------------------------------*/
83static lwm2m_status_t
84lwm2m_callback(lwm2m_object_instance_t *object,
85 lwm2m_context_t *ctx)
86{
87 uint32_t num;
88 uint8_t more;
89 uint16_t size;
90 uint32_t offset;
91
92 char *str = "just a string";
93
94 PRINTF("Got request at: %d/%d/%d lv:%d\n", ctx->object_id, ctx->object_instance_id, ctx->resource_id, ctx->level);
95
96 if(ctx->level == 1) {
97 /* Should not happen */
98 return LWM2M_STATUS_ERROR;
99 }
100 if(ctx->level == 2) {
101 /* This is a get whole object - or write whole object */
102 return LWM2M_STATUS_ERROR;
103 }
104
105 if(ctx->operation == LWM2M_OP_READ) {
106#if DEBUG
107 if(coap_get_header_block2(ctx->request, &num, &more, &size, &offset)) {
108 PRINTF("CoAP BLOCK2: %d/%d/%d offset:%d\n", num, more, size, offset);
109 }
110#endif
111
112 switch(ctx->resource_id) {
113 case 10000:
114 lwm2m_object_write_string(ctx, str, strlen(str));
115 break;
116 case 11000:
117 case 11001:
118 PRINTF("Preparing object write\n");
119 lwm2m_object_write_opaque_stream(ctx, LEN, opaque_callback);
120 break;
121 default:
122 return LWM2M_STATUS_NOT_FOUND;
123 }
124 } else if(ctx->operation == LWM2M_OP_WRITE) {
125 if(coap_get_header_block1(ctx->request, &num, &more, &size, &offset)) {
126 PRINTF("CoAP BLOCK1: %d/%d/%d offset:%d\n", num, more, size, offset);
127 coap_set_header_block1(ctx->response, num, 0, size);
128 }
129 }
130 return LWM2M_STATUS_OK;
131}
132
133void
134ipso_blockwise_test_init(void)
135{
136 int i;
137 PRINTF("Starting blockwise\n");
138 reg_object.object_id = 4711;
139 reg_object.instance_id = 0;
140 reg_object.resource_ids = resources;
141 reg_object.resource_count =
142 sizeof(resources) / sizeof(lwm2m_resource_id_t);
143 reg_object.callback = lwm2m_callback;
144
145 for(i = 0; i < sizeof(junk); i++) {
146 junk[i] = '0' + i;
147 }
148 junk[i - 1] = 0;
149
150 lwm2m_engine_add_object(&reg_object);
151}
152/** @} */
An implementation of the Constrained Application Protocol (RFC 7252).
Header file for the Contiki OMA LWM2M engine.