libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
coap-observer.c
Go to the documentation of this file.
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 "config.h"
34 #include "net/uip-debug.h"
35 
36 #include <string.h>
37 
38 #include "debug.h"
39 #include "coap.h"
40 
42 
43 /* Where the resource to subscribe is hosted */
44 static coap_address_t dst;
45 
46 /* The resource to observe */
47 static char resource[] = "/s/light";
48 
49 /* when did the last notify arrive? (0 == never) */
50 static coap_tick_t last_seen = 0;
51 
52 PROCESS(coap_server_process, "CoAP server process");
53 AUTOSTART_PROCESSES(&coap_server_process);
54 /*---------------------------------------------------------------------------*/
55 void
57  coap_address_t listen_addr;
58 
59  coap_address_init(&listen_addr);
60  listen_addr.port = UIP_HTONS(COAP_DEFAULT_PORT);
61 
62 #ifdef WITH_CONTIKI
63  /* initialize uIP address for SLAAC */
64  uip_ip6addr(&listen_addr.addr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
65  uip_ds6_set_addr_iid(&listen_addr.addr, &uip_lladdr);
66  uip_ds6_addr_add(&listen_addr.addr, 0, ADDR_AUTOCONF);
67 
68  uip_debug_lladdr_print(&uip_lladdr);
69  printf("\r\n");
70  uip_debug_ipaddr_print(&listen_addr.addr);
71  printf("\r\n");
72 #endif /* WITH_CONTIKI */
73 
74 #ifdef WITH_CONTIKI
75  printf("tentative address: [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
76  listen_addr.addr.u8[0], listen_addr.addr.u8[1],
77  listen_addr.addr.u8[2], listen_addr.addr.u8[3],
78  listen_addr.addr.u8[4], listen_addr.addr.u8[5],
79  listen_addr.addr.u8[6], listen_addr.addr.u8[7],
80  listen_addr.addr.u8[8], listen_addr.addr.u8[9],
81  listen_addr.addr.u8[10], listen_addr.addr.u8[11],
82  listen_addr.addr.u8[12], listen_addr.addr.u8[13],
83  listen_addr.addr.u8[14], listen_addr.addr.u8[15] ,
84  uip_ntohs(listen_addr.port));
85 #endif
86 
87  coap_context = coap_new_context(&listen_addr);
88 
89  coap_set_log_level(LOG_DEBUG);
90 
91  if (!coap_context)
92  coap_log(LOG_CRIT, "cannot create CoAP context\r\n");
93 }
94 
95 void
97  const coap_address_t *remote,
98  coap_pdu_t *sent,
99  coap_pdu_t *received,
100  const coap_tid_t id) {
101  /* send ACK if received message is confirmable (i.e. a separate response) */
102  coap_send_ack(ctx, remote, received);
103 
104  debug("** process incoming %d.%02d response:\n",
105  (received->hdr->code >> 5), received->hdr->code & 0x1F);
106  coap_show_pdu(received);
107 
109 }
110 
111 /*---------------------------------------------------------------------------*/
113 {
114  coap_pdu_t *request;
115  coap_uri_t uri;
116  PROCESS_BEGIN();
117 
118  init_coap();
119 
120  if (!coap_context) {
121  coap_log(LOG_EMERG, "cannot create context\n");
122  PROCESS_EXIT();
123  }
124 
126 
127  /* setup subscription request */
128 
130  dst.port = uip_htons(COAP_DEFAULT_PORT);
131  uip_ip6addr(&dst.addr, 0xaaaa, 0, 0, 0, 0x206, 0x98ff, 0xfe00, 0x232);
132  /* uip_ip6addr(&dst.addr, 0xfe80, 0, 0, 0, 0x206, 0x98ff, 0xfe00, 0x232); */
133 
135  coap_new_message_id(coap_context),
137 
138  coap_split_uri((unsigned char *)resource, strlen(resource), &uri);
139 
140  if (uri.port != COAP_DEFAULT_PORT) {
141  unsigned char portbuf[2];
143  coap_encode_var_bytes(portbuf, uri.port), portbuf);
144  }
145 
146  if (uri.path.length) {
147 #define BUFSIZE 20
148  unsigned char _buf[BUFSIZE];
149  unsigned char *buf = _buf;
150  size_t buflen;
151  int res;
152 
153  buflen = BUFSIZE;
154 #undef BUFSIZE
155  res = coap_split_path(uri.path.s, uri.path.length, buf, &buflen);
156 
157  while (res--) {
159  COAP_OPT_LENGTH(buf), COAP_OPT_VALUE(buf));
160 
161  buf += COAP_OPT_SIZE(buf);
162  }
163  }
164 
165  coap_add_option(request, COAP_OPTION_SUBSCRIPTION, 0, NULL);
166  {
167  unsigned char buf[2];
168  prng(buf, 2);
169  coap_add_option(request, COAP_OPTION_TOKEN, 2, buf);
170  }
171 
172  if (COAP_INVALID_TID == coap_send_confirmed(coap_context, &dst, request))
173  coap_delete_pdu(request);
174 
175  while(1) {
176  PROCESS_YIELD();
177  if(ev == tcpip_event) {
178  coap_read(coap_context); /* read received data */
179  coap_dispatch(coap_context); /* and dispatch PDUs from receivequeue */
180  }
181  }
182 
183  PROCESS_END();
184 }
185 /*---------------------------------------------------------------------------*/