1 /* coap-server.c -- Example CoAP server using Contiki and libcoap
2 *
3 * Copyright (C) 2011 Olaf Bergmann <bergmann@tzi.org>
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 Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33 #include "coap_config.h"
34
35 #define DEBUG DEBUG_PRINT
36 #include "net/ip/uip-debug.h"
37 #include "net/net-debug.h"
38
39 #include <string.h>
40
41 #include "coap.h"
42
43 static coap_context_t *coap_context;
44
45 static clock_time_t clock_offset;
46 /* changeable clock base (see handle_put_time()) */
47 static clock_time_t my_clock_base = 0;
48 static coap_resource_t *time_resource = NULL; /* just for testing */
49
50 PROCESS(coap_server_process, "CoAP server process");
51 AUTOSTART_PROCESSES(&coap_server_process);
52 /*---------------------------------------------------------------------------*/
53 void
init_coap_server(coap_context_t ** ctx)54 init_coap_server(coap_context_t **ctx) {
55 coap_address_t listen_addr;
56 uip_ipaddr_t gw_addr;
57
58 assert(ctx);
59
60 coap_set_log_level(LOG_DEBUG);
61
62 coap_address_init(&listen_addr);
63 listen_addr.port = UIP_HTONS(COAP_DEFAULT_PORT);
64
65 uip_ip6addr(&listen_addr.addr, 0xaaaa, 0, 0, 0, 0, 0, 0, NODE_ADDR);
66 #ifndef CONTIKI_TARGET_MINIMAL_NET
67 uip_ds6_prefix_add(&listen_addr.addr, 64, 0, 0, 0, 0);
68 #endif /* not CONTIKI_TARGET_MINIMAL_NET */
69
70 uip_ds6_addr_add(&listen_addr.addr, 0, ADDR_MANUAL);
71
72 /* set default route to gateway aaaa::1 */
73 uip_ip6addr(&gw_addr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0x0001);
74 uip_ds6_defrt_add(&gw_addr, 0);
75
76 PRINTLLADDR(&uip_lladdr);
77 printf("\r\n");
78 PRINT6ADDR(&listen_addr.addr);
79 printf("\r\n");
80
81 *ctx = coap_new_context(&listen_addr);
82
83 if (!*ctx) {
84 coap_log(LOG_CRIT, "cannot create CoAP context\r\n");
85 }
86 }
87
88 /*---------------------------------------------------------------------------*/
89 #ifndef min
90 # define min(a,b) ((a) < (b) ? (a) : (b))
91 #endif
92
93 void
hnd_get_time(coap_context_t * ctx,struct coap_resource_t * resource,coap_session_t * session,coap_pdu_t * request,coap_binary_t * token,coap_string_t * query,coap_pdu_t * response)94 hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
95 coap_session_t *session,
96 coap_pdu_t *request, coap_binary_t *token,
97 coap_string_t *query,
98 coap_pdu_t *response) {
99 unsigned char buf[40];
100 size_t len;
101 coap_tick_t now;
102 coap_tick_t t;
103
104 /* FIXME: return time, e.g. in human-readable by default and ticks
105 * when query ?ticks is given. */
106
107 /* if my_clock_base was deleted, we pretend to have no such resource */
108 response->code =
109 my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);
110
111 if (coap_find_observer(resource, session, token)) {
112 coap_add_option(response, COAP_OPTION_OBSERVE,
113 coap_encode_var_safe(buf, sizeof(buf),
114 resource->observe),
115 buf);
116 }
117
118 if (my_clock_base)
119 coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
120 coap_encode_var_safe(buf, sizeof(buf),
121 COAP_MEDIATYPE_TEXT_PLAIN),
122 buf);
123
124 coap_add_option(response, COAP_OPTION_MAXAGE,
125 coap_encode_var_safe(buf, sizeof(buf), 0x01), buf);
126
127 if (my_clock_base) {
128
129 /* calculate current time */
130 coap_ticks(&t);
131 now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
132
133
134 if (query != NULL
135 && coap_string_equal(query, coap_make_str_const("ticks"))) {
136 /* output ticks */
137 len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now);
138 coap_add_data(response, len, buf);
139 }
140 }
141 }
142
143 void
init_coap_resources(coap_context_t * ctx)144 init_coap_resources(coap_context_t *ctx) {
145 coap_resource_t *r;
146 #if 0
147 r = coap_resource_init(NULL, 0, 0);
148 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index);
149
150 coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
151 coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"General Info\""), 0);
152 coap_add_resource(ctx, r);
153 #endif
154 /* store clock base to use in /time */
155 my_clock_base = clock_offset;
156
157 r = coap_resource_init(coap_make_str_const("time"), 0);
158 if (!r)
159 goto error;
160
161 coap_resource_set_get_observable(r, 1);
162 time_resource = r;
163 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
164 #if 0
165 coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time);
166 coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
167 #endif
168 coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
169 /* coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"Internal Clock\""), 0); */
170 coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"ticks\""), 0);
171 coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""), 0);
172
173 coap_add_resource(ctx, r);
174 #if 0
175 #ifndef WITHOUT_ASYNC
176 r = coap_resource_init(coap_make_str_const("async"), 0);
177 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async);
178
179 coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
180 coap_add_resource(ctx, r);
181 #endif /* WITHOUT_ASYNC */
182 #endif
183
184 return;
185 error:
186 coap_log(LOG_CRIT, "cannot create resource\n");
187 }
188
189 /* struct etimer notify_timer; */
190 struct etimer dirty_timer;
191
192 /*---------------------------------------------------------------------------*/
PROCESS_THREAD(coap_server_process,ev,data)193 PROCESS_THREAD(coap_server_process, ev, data)
194 {
195 PROCESS_BEGIN();
196
197 clock_offset = clock_time();
198 init_coap_server(&coap_context);
199
200 if (!coap_context) {
201 coap_log(LOG_EMERG, "cannot create context\n");
202 PROCESS_EXIT();
203 }
204
205 init_coap_resources(coap_context);
206
207 if (!coap_context) {
208 coap_log(LOG_EMERG, "cannot create context\n");
209 PROCESS_EXIT();
210 }
211
212 /* etimer_set(¬ify_timer, 5 * CLOCK_SECOND); */
213 etimer_set(&dirty_timer, 30 * CLOCK_SECOND);
214
215 while(1) {
216 PROCESS_YIELD();
217 if(ev == tcpip_event) {
218 /* There is something to read on the endpoint */
219 coap_run_once(coap_context, COAP_RUN_BLOCK);
220 } else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) {
221 coap_resource_notify_observers(time_resource, NULL);
222 etimer_reset(&dirty_timer);
223 }
224 }
225
226 PROCESS_END();
227 }
228 /*---------------------------------------------------------------------------*/
229