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 #include "net/ip/uip-debug.h"
35 
36 #include <string.h>
37 
38 #include "debug.h"
39 #include "coap.h"
40 
41 static coap_context_t *coap_context;
42 
43 static clock_time_t clock_offset;
44 /* changeable clock base (see handle_put_time()) */
45 static clock_time_t my_clock_base = 0;
46 static coap_resource_t *time_resource = NULL; /* just for testing */
47 
48 PROCESS(coap_server_process, "CoAP server process");
49 AUTOSTART_PROCESSES(&coap_server_process);
50 /*---------------------------------------------------------------------------*/
51 void
init_coap_server(coap_context_t ** ctx)52 init_coap_server(coap_context_t **ctx) {
53   coap_address_t listen_addr;
54   uip_ipaddr_t gw_addr;
55 
56   assert(ctx);
57 
58   coap_set_log_level(LOG_DEBUG);
59 
60   coap_address_init(&listen_addr);
61   listen_addr.port = UIP_HTONS(COAP_DEFAULT_PORT);
62 
63   uip_ip6addr(&listen_addr.addr, 0xaaaa, 0, 0, 0, 0, 0, 0, NODE_ADDR);
64 #ifndef CONTIKI_TARGET_MINIMAL_NET
65   uip_ds6_prefix_add(&listen_addr.addr, 64, 0, 0, 0, 0);
66 #endif /* not CONTIKI_TARGET_MINIMAL_NET */
67 
68   uip_ds6_addr_add(&listen_addr.addr, 0, ADDR_MANUAL);
69 
70   /* set default route to gateway aaaa::1 */
71   uip_ip6addr(&gw_addr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0x0001);
72   uip_ds6_defrt_add(&gw_addr, 0);
73 
74   uip_debug_lladdr_print(&uip_lladdr);
75   printf("\r\n");
76   uip_debug_ipaddr_print(&listen_addr.addr);
77   printf("\r\n");
78 
79   *ctx = coap_new_context(&listen_addr);
80 
81   if (!*ctx) {
82     coap_log(LOG_CRIT, "cannot create CoAP context\r\n");
83   }
84 }
85 
86 /*---------------------------------------------------------------------------*/
87 #ifndef min
88 # define min(a,b) ((a) < (b) ? (a) : (b))
89 #endif
90 
91 void
hnd_get_time(coap_context_t * ctx,struct coap_resource_t * resource,const coap_endpoint_t * local_interface,coap_address_t * peer,coap_pdu_t * request,str * token,coap_pdu_t * response)92 hnd_get_time(coap_context_t  *ctx, struct coap_resource_t *resource,
93 	     const coap_endpoint_t *local_interface,
94 	     coap_address_t *peer, coap_pdu_t *request, str *token,
95 	     coap_pdu_t *response) {
96   coap_opt_iterator_t opt_iter;
97   coap_opt_t *option;
98   unsigned char buf[40];
99   size_t len;
100   coap_tick_t now;
101   coap_tick_t t;
102 
103   /* FIXME: return time, e.g. in human-readable by default and ticks
104    * when query ?ticks is given. */
105 
106   /* if my_clock_base was deleted, we pretend to have no such resource */
107   response->hdr->code =
108     my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);
109 
110   if (coap_find_observer(resource, peer, token)) {
111     /* FIXME: need to check for resource->dirty? */
112     coap_add_option(response, COAP_OPTION_OBSERVE,
113 		    coap_encode_var_bytes(buf, ctx->observe), buf);
114   }
115 
116   if (my_clock_base)
117     coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
118 		    coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
119 
120   coap_add_option(response, COAP_OPTION_MAXAGE,
121 	  coap_encode_var_bytes(buf, 0x01), buf);
122 
123   if (my_clock_base) {
124 
125     /* calculate current time */
126     coap_ticks(&t);
127     now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
128 
129     if (request != NULL
130 	&& (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))
131 	&& memcmp(COAP_OPT_VALUE(option), "ticks",
132 		  min(5, COAP_OPT_LENGTH(option))) == 0) {
133       /* output ticks */
134       len = snprintf((char *)buf,
135 	   min(sizeof(buf), response->max_size - response->length),
136 		     "%u", (unsigned int)now);
137       coap_add_data(response, len, buf);
138 
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, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
151   coap_add_attr(r, (unsigned char *)"title", 5, (unsigned char *)"\"General Info\"", 14, 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((unsigned char *)"time", 4, 0);
158   if (!r)
159     goto error;
160 
161   r->observable = 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, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
169   /* coap_add_attr(r, (unsigned char *)"title", 5, (unsigned char *)"\"Internal Clock\"", 16, 0); */
170   coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"\"Ticks\"", 7, 0);
171   coap_add_attr(r, (unsigned char *)"if", 2, (unsigned char *)"\"clock\"", 7, 0);
172 
173   coap_add_resource(ctx, r);
174 #if 0
175 #ifndef WITHOUT_ASYNC
176   r = coap_resource_init((unsigned char *)"async", 5, 0);
177   coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async);
178 
179   coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 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(&notify_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       coap_read(coap_context);	/* read received data */
219       /* coap_dispatch(coap_context); /\* and dispatch PDUs from receivequeue *\/ */
220     } else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) {
221       time_resource->dirty = 1;
222       etimer_reset(&dirty_timer);
223     }
224   }
225 
226   PROCESS_END();
227 }
228 /*---------------------------------------------------------------------------*/
229