-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoap-server.c
More file actions
220 lines (182 loc) · 7.34 KB
/
coap-server.c
File metadata and controls
220 lines (182 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* coap-server.c -- Example CoAP server using Contiki and libcoap
*
* Copyright (C) 2011 Olaf Bergmann <bergmann@tzi.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
#include "config.h"
#include "net/uip-debug.h"
#include <string.h>
#include "debug.h"
#include "coap.h"
static coap_context_t *coap_context;
/* changeable clock base (see handle_put_time()) */
static clock_time_t my_clock_base = 0;
static coap_resource_t *time_resource = NULL; /* just for testing */
PROCESS(coap_server_process, "CoAP server process");
AUTOSTART_PROCESSES(&coap_server_process);
/*---------------------------------------------------------------------------*/
void
init_coap() {
coap_address_t listen_addr;
coap_address_init(&listen_addr);
listen_addr.port = UIP_HTONS(COAP_DEFAULT_PORT);
#ifdef WITH_CONTIKI
/* initialize uIP address for SLAAC */
uip_ip6addr(&listen_addr.addr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
uip_ds6_set_addr_iid(&listen_addr.addr, &uip_lladdr);
uip_ds6_addr_add(&listen_addr.addr, 0, ADDR_AUTOCONF);
uip_debug_lladdr_print(&uip_lladdr);
printf("\r\n");
uip_debug_ipaddr_print(&listen_addr.addr);
printf("\r\n");
#endif /* WITH_CONTIKI */
#ifdef WITH_CONTIKI
printf("tentative address: [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
listen_addr.addr.u8[0], listen_addr.addr.u8[1],
listen_addr.addr.u8[2], listen_addr.addr.u8[3],
listen_addr.addr.u8[4], listen_addr.addr.u8[5],
listen_addr.addr.u8[6], listen_addr.addr.u8[7],
listen_addr.addr.u8[8], listen_addr.addr.u8[9],
listen_addr.addr.u8[10], listen_addr.addr.u8[11],
listen_addr.addr.u8[12], listen_addr.addr.u8[13],
listen_addr.addr.u8[14], listen_addr.addr.u8[15] ,
uip_ntohs(listen_addr.port));
#endif
coap_context = coap_new_context(&listen_addr);
coap_set_log_level(LOG_DEBUG);
if (!coap_context)
coap_log(LOG_CRIT, "cannot create CoAP context\r\n");
}
/*---------------------------------------------------------------------------*/
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
void
hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
coap_address_t *peer, coap_pdu_t *request, str *token,
coap_pdu_t *response) {
coap_opt_iterator_t opt_iter;
unsigned char buf[2];
coap_tick_t now;
coap_tick_t t;
/* if my_clock_base was deleted, we pretend to have no such resource */
response->hdr->code = COAP_RESPONSE_CODE(205),
coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
coap_add_option(response, COAP_OPTION_MAXAGE,
coap_encode_var_bytes(buf, 0x01), buf);
/* Check if subscription was requested. */
if (request &&
coap_check_option(request, COAP_OPTION_SUBSCRIPTION, &opt_iter) &&
coap_add_observer(resource, peer, token)) {
/* add a new observe value */
coap_add_option(response, COAP_OPTION_SUBSCRIPTION,
coap_encode_var_bytes(buf, ctx->observe), buf);
if (token->length)
coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s);
} else {
coap_add_option(response, COAP_OPTION_SUBSCRIPTION,
coap_encode_var_bytes(buf, ctx->observe), buf);
if (token->length)
coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s);
}
/* calculate current time */
coap_ticks(&t);
now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
/* output ticks */
response->length += snprintf((char *)response->data,
response->max_size - response->length,
"%u", (unsigned int)now);
}
void
init_resources(coap_context_t *ctx) {
coap_resource_t *r;
#if 0
r = coap_resource_init(NULL, 0, 0);
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index);
coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
coap_add_attr(r, (unsigned char *)"title", 5, (unsigned char *)"\"General Info\"", 14, 0);
coap_add_resource(ctx, r);
#endif
/* store clock base to use in /time */
my_clock_base = clock_offset;
r = coap_resource_init((unsigned char *)"time", 4, 0);
if (!r)
goto error;
r->observable = 1;
time_resource = r;
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
#if 0
coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time);
coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
#endif
coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
/* coap_add_attr(r, (unsigned char *)"title", 5, (unsigned char *)"\"Internal Clock\"", 16, 0); */
coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"\"Ticks\"", 7, 0);
coap_add_attr(r, (unsigned char *)"if", 2, (unsigned char *)"\"clock\"", 7, 0);
coap_add_resource(ctx, r);
#if 0
#ifndef WITHOUT_ASYNC
r = coap_resource_init((unsigned char *)"async", 5, 0);
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async);
coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
coap_add_resource(ctx, r);
#endif /* WITHOUT_ASYNC */
#endif
return;
error:
coap_log(LOG_CRIT, "cannot create resource\n");
}
/* struct etimer notify_timer; */
struct etimer dirty_timer;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(coap_server_process, ev, data)
{
PROCESS_BEGIN();
init_coap();
init_resources(coap_context);
if (!coap_context) {
coap_log(LOG_EMERG, "cannot create context\n");
PROCESS_EXIT();
}
/* etimer_set(¬ify_timer, 5 * CLOCK_SECOND); */
etimer_set(&dirty_timer, 30 * CLOCK_SECOND);
while(1) {
PROCESS_YIELD();
if(ev == tcpip_event) {
coap_read(coap_context); /* read received data */
coap_dispatch(coap_context); /* and dispatch PDUs from receivequeue */
} else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) {
time_resource->dirty = 1;
etimer_reset(&dirty_timer);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/