15 #include <sys/select.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
30 #define COAP_RESOURCE_CHECK_TIME 2
33 #define min(a,b) ((a) < (b) ? (a) : (b))
56 #define INDEX "This is a test server made with libcoap (see http://libcoap.sf.net)\n" \
57 "Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>\n\n"
82 unsigned char buf[40];
95 if (request != NULL &&
103 if (resource->
dirty == 1)
126 len = snprintf((
char *)buf,
128 "%u", (
unsigned int)now);
134 len = strftime((
char *)buf,
136 "%b %d %H:%M:%S", tmp);
184 #ifndef WITHOUT_ASYNC
191 unsigned long delay = 5;
195 if (async->
id != request->
hdr->
id) {
209 delay = delay * 10 + (*p -
'0');
224 if (!async || now < async->created + (
unsigned long)async->
appdata)
232 debug(
"check_async: insufficient memory, we'll try later\n");
246 debug(
"check_async: cannot send response for message %d\n",
263 coap_add_attr(r, (
unsigned char *)
"ct", 2, (
unsigned char *)
"0", 1, 0);
264 coap_add_attr(r, (
unsigned char *)
"title", 5, (
unsigned char *)
"\"General Info\"", 14, 0);
275 coap_add_attr(r, (
unsigned char *)
"ct", 2, (
unsigned char *)
"0", 1, 0);
276 coap_add_attr(r, (
unsigned char *)
"title", 5, (
unsigned char *)
"\"Internal Clock\"", 16, 0);
277 coap_add_attr(r, (
unsigned char *)
"rt", 2, (
unsigned char *)
"\"Ticks\"", 7, 0);
279 coap_add_attr(r, (
unsigned char *)
"if", 2, (
unsigned char *)
"\"clock\"", 7, 0);
284 #ifndef WITHOUT_ASYNC
288 coap_add_attr(r, (
unsigned char *)
"ct", 2, (
unsigned char *)
"0", 1, 0);
294 usage(
const char *program,
const char *version) {
297 p = strrchr( program,
'/' );
301 fprintf( stderr,
"%s v%s -- a small CoAP implementation\n"
302 "(c) 2010,2011 Olaf Bergmann <bergmann@tzi.org>\n\n"
303 "usage: %s [-A address] [-p port]\n\n"
304 "\t-A address\tinterface address to bind to\n"
305 "\t-p port\t\tlisten on specified port\n"
306 "\t-v num\t\tverbosity level (default: 3)\n",
307 program, version, program );
314 struct addrinfo hints;
315 struct addrinfo *result, *rp;
317 memset(&hints, 0,
sizeof(
struct addrinfo));
318 hints.ai_family = AF_UNSPEC;
319 hints.ai_socktype = SOCK_DGRAM;
320 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
322 s = getaddrinfo(node, port, &hints, &result);
324 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(s));
329 for (rp = result; rp != NULL; rp = rp->ai_next) {
332 if (rp->ai_addrlen <=
sizeof(addr.addr)) {
334 addr.size = rp->ai_addrlen;
335 memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen);
345 fprintf(stderr,
"no context available for interface '%s'\n", node);
348 freeaddrinfo(result);
356 struct timeval tv, *timeout;
360 char addr_str[NI_MAXHOST] =
"::";
361 char port_str[NI_MAXSERV] =
"5683";
365 while ((opt = getopt(argc, argv,
"A:p:v:")) != -1) {
368 strncpy(addr_str, optarg, NI_MAXHOST-1);
369 addr_str[NI_MAXHOST - 1] =
'\0';
372 strncpy(port_str, optarg, NI_MAXSERV-1);
373 port_str[NI_MAXSERV - 1] =
'\0';
376 log_level = strtol(optarg, NULL, 10);
396 FD_SET( ctx->
sockfd, &readfds );
401 while ( nextpdu && nextpdu->
t <= now ) {
416 result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
421 }
else if ( result > 0 ) {
422 if ( FD_ISSET( ctx->
sockfd, &readfds ) ) {
428 time_resource->
dirty = 1;
432 #ifndef WITHOUT_ASYNC
437 #ifndef WITHOUT_OBSERVE