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