• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "coap3/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_resource_t * resource,coap_session_t * session,const coap_pdu_t * request,const coap_string_t * query,coap_pdu_t * response)94 hnd_get_time(coap_resource_t *resource, coap_session_t *session,
95              const coap_pdu_t *request, const coap_string_t *query,
96              coap_pdu_t *response) {
97   unsigned char buf[40];
98   size_t len;
99   coap_tick_t now;
100   coap_tick_t t;
101 
102   if (my_clock_base) {
103 
104     /* calculate current time */
105     coap_ticks(&t);
106     now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
107 
108     if (query != NULL
109         && coap_string_equal(query, coap_make_str_const("ticks"))) {
110           /* output ticks */
111           len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now);
112 
113     } else {      /* output human-readable time */
114       struct tm *tmp;
115       time_t tnow = now;
116       tmp = gmtime(&tnow);
117       if (!tmp) {
118         /* If 'tnow' is not valid */
119         coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
120         return;
121       }
122       else {
123         len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
124       }
125     }
126     coap_add_data_blocked_response(request, response,
127                                    COAP_MEDIATYPE_TEXT_PLAIN, 1,
128                                    len,
129                                    buf);
130   }
131   else {
132     /* if my_clock_base was deleted, we pretend to have no such resource */
133     coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
134   }
135 }
136 
137 void
init_coap_resources(coap_context_t * ctx)138 init_coap_resources(coap_context_t *ctx) {
139   coap_resource_t *r;
140 #if 0
141   r = coap_resource_init(NULL, 0, 0);
142   coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index);
143 
144   coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
145   coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"General Info\""), 0);
146   coap_add_resource(ctx, r);
147 #endif
148   /* store clock base to use in /time */
149   my_clock_base = clock_offset;
150 
151   r = coap_resource_init(coap_make_str_const("time"), 0);
152   if (!r)
153     goto error;
154 
155   coap_resource_set_get_observable(r, 1);
156   time_resource = r;
157   coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
158 #if 0
159   coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time);
160   coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
161 #endif
162   coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
163   /* coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"Internal Clock\""), 0); */
164   coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"ticks\""), 0);
165   coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""), 0);
166 
167   coap_add_resource(ctx, r);
168 #if 0
169 #ifndef WITHOUT_ASYNC
170   r = coap_resource_init(coap_make_str_const("async"), 0);
171   coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async);
172 
173   coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
174   coap_add_resource(ctx, r);
175 #endif /* WITHOUT_ASYNC */
176 #endif
177 
178   return;
179  error:
180   coap_log(LOG_CRIT, "cannot create resource\n");
181 }
182 
183 /* struct etimer notify_timer; */
184 struct etimer dirty_timer;
185 
186 /*---------------------------------------------------------------------------*/
PROCESS_THREAD(coap_server_process,ev,data)187 PROCESS_THREAD(coap_server_process, ev, data)
188 {
189   PROCESS_BEGIN();
190 
191   clock_offset = clock_time();
192   init_coap_server(&coap_context);
193 
194   if (!coap_context) {
195     coap_log(LOG_EMERG, "cannot create context\n");
196     PROCESS_EXIT();
197   }
198 
199   init_coap_resources(coap_context);
200 
201   if (!coap_context) {
202     coap_log(LOG_EMERG, "cannot create context\n");
203     PROCESS_EXIT();
204   }
205 
206   /* etimer_set(&notify_timer, 5 * CLOCK_SECOND); */
207   etimer_set(&dirty_timer, 30 * CLOCK_SECOND);
208 
209   while(1) {
210     PROCESS_YIELD();
211     if(ev == tcpip_event) {
212       /* There is something to read on the endpoint */
213       coap_io_process(coap_context, COAP_IO_WAIT);
214     } else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) {
215       coap_resource_notify_observers(time_resource, NULL);
216       etimer_reset(&dirty_timer);
217     }
218   }
219 
220   PROCESS_END();
221 }
222 /*---------------------------------------------------------------------------*/
223