mirror of
https://github.com/obgm/libcoap.git
synced 2026-12-05 20:02:19 +03:00
examples/coap-server.c: whitespace cleanup, reformatting, no changes
Just some cleanup like whitespace, punctiation marking and indentation issues.
This commit is contained in:
1 parent
bda1a2360b
commit
60bb15edd7
1 file changed
+122
-95
+122
-95
@@ -1,10 +1,12 @@
|
||||
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 * -*- */
|
||||
|
||||
/* coap -- simple implementation of the Constrained Application Protocol (CoAP)
|
||||
* as defined in RFC 7252
|
||||
*
|
||||
* Copyright (C) 2010--2015 Olaf Bergmann <bergmann@tzi.org>
|
||||
*
|
||||
* This file is part of the CoAP library libcoap. Please see
|
||||
* README for terms of use.
|
||||
* This file is part of the CoAP library libcoap. Please see README for terms
|
||||
* of use.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
@@ -61,31 +63,39 @@ handle_sigint(int signum UNUSED_PARAM) {
|
||||
}
|
||||
|
||||
#define INDEX "This is a test server made with libcoap (see http://libcoap.sf.net)\n" \
|
||||
"Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>\n\n"
|
||||
"Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>\n\n"
|
||||
|
||||
static void
|
||||
hnd_get_index(coap_context_t *ctx UNUSED_PARAM, struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer UNUSED_PARAM, coap_pdu_t *request UNUSED_PARAM, str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response) {
|
||||
hnd_get_index(coap_context_t *ctx UNUSED_PARAM,
|
||||
struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer UNUSED_PARAM,
|
||||
coap_pdu_t *request UNUSED_PARAM,
|
||||
str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response) {
|
||||
unsigned char buf[3];
|
||||
|
||||
response->hdr->code = COAP_RESPONSE_CODE(205);
|
||||
|
||||
coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
|
||||
coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
coap_add_option(response,
|
||||
COAP_OPTION_CONTENT_TYPE,
|
||||
coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
|
||||
coap_add_option(response,
|
||||
COAP_OPTION_MAXAGE,
|
||||
coap_encode_var_bytes(buf, 0x2ffff), buf);
|
||||
|
||||
coap_add_option(response, COAP_OPTION_MAXAGE,
|
||||
coap_encode_var_bytes(buf, 0x2ffff), buf);
|
||||
|
||||
coap_add_data(response, strlen(INDEX), (unsigned char *)INDEX);
|
||||
}
|
||||
|
||||
static void
|
||||
hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer, coap_pdu_t *request, str *token,
|
||||
coap_pdu_t *response) {
|
||||
hnd_get_time(coap_context_t *ctx,
|
||||
struct coap_resource_t *resource,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer,
|
||||
coap_pdu_t *request,
|
||||
str *token,
|
||||
coap_pdu_t *response) {
|
||||
coap_opt_iterator_t opt_iter;
|
||||
coap_opt_t *option;
|
||||
unsigned char buf[40];
|
||||
@@ -97,54 +107,62 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
* when query ?ticks is given. */
|
||||
|
||||
/* if my_clock_base was deleted, we pretend to have no such resource */
|
||||
response->hdr->code =
|
||||
response->hdr->code =
|
||||
my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);
|
||||
|
||||
if (coap_find_observer(resource, peer, token)) {
|
||||
/* FIXME: need to check for resource->dirty? */
|
||||
coap_add_option(response, COAP_OPTION_OBSERVE,
|
||||
coap_encode_var_bytes(buf, ctx->observe), buf);
|
||||
coap_add_option(response,
|
||||
COAP_OPTION_OBSERVE,
|
||||
coap_encode_var_bytes(buf, ctx->observe), buf);
|
||||
}
|
||||
|
||||
if (my_clock_base)
|
||||
coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
|
||||
coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
coap_add_option(response,
|
||||
COAP_OPTION_CONTENT_FORMAT,
|
||||
coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
|
||||
coap_add_option(response, COAP_OPTION_MAXAGE,
|
||||
coap_encode_var_bytes(buf, 0x01), buf);
|
||||
coap_add_option(response,
|
||||
COAP_OPTION_MAXAGE,
|
||||
coap_encode_var_bytes(buf, 0x01), buf);
|
||||
|
||||
if (my_clock_base) {
|
||||
|
||||
/* calculate current time */
|
||||
coap_ticks(&t);
|
||||
now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
|
||||
|
||||
if (request != NULL
|
||||
&& (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))
|
||||
&& memcmp(COAP_OPT_VALUE(option), "ticks",
|
||||
min(5, COAP_OPT_LENGTH(option))) == 0) {
|
||||
/* output ticks */
|
||||
len = snprintf((char *)buf,
|
||||
min(sizeof(buf), response->max_size - response->length),
|
||||
"%u", (unsigned int)now);
|
||||
coap_add_data(response, len, buf);
|
||||
|
||||
} else { /* output human-readable time */
|
||||
if (request != NULL
|
||||
&& (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))
|
||||
&& memcmp(COAP_OPT_VALUE(option), "ticks",
|
||||
min(5, COAP_OPT_LENGTH(option))) == 0) {
|
||||
/* output ticks */
|
||||
len = snprintf((char *)buf,
|
||||
min(sizeof(buf),
|
||||
response->max_size - response->length),
|
||||
"%u", (unsigned int)now);
|
||||
coap_add_data(response, len, buf);
|
||||
|
||||
} else { /* output human-readable time */
|
||||
struct tm *tmp;
|
||||
tmp = gmtime(&now);
|
||||
len = strftime((char *)buf,
|
||||
min(sizeof(buf), response->max_size - response->length),
|
||||
"%b %d %H:%M:%S", tmp);
|
||||
len = strftime((char *)buf,
|
||||
min(sizeof(buf),
|
||||
response->max_size - response->length),
|
||||
"%b %d %H:%M:%S", tmp);
|
||||
coap_add_data(response, len, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
hnd_put_time(coap_context_t *ctx UNUSED_PARAM, struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer UNUSED_PARAM, coap_pdu_t *request, str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response) {
|
||||
hnd_put_time(coap_context_t *ctx UNUSED_PARAM,
|
||||
struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer UNUSED_PARAM,
|
||||
coap_pdu_t *request,
|
||||
str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response) {
|
||||
coap_tick_t t;
|
||||
size_t size;
|
||||
unsigned char *data;
|
||||
@@ -155,41 +173,47 @@ hnd_put_time(coap_context_t *ctx UNUSED_PARAM, struct coap_resource_t *resource
|
||||
*/
|
||||
|
||||
/* if my_clock_base was deleted, we pretend to have no such resource */
|
||||
response->hdr->code =
|
||||
response->hdr->code =
|
||||
my_clock_base ? COAP_RESPONSE_CODE(204) : COAP_RESPONSE_CODE(201);
|
||||
|
||||
resource->dirty = 1;
|
||||
|
||||
coap_get_data(request, &size, &data);
|
||||
|
||||
if (size == 0) /* re-init */
|
||||
|
||||
if (size == 0) /* re-init */
|
||||
my_clock_base = clock_offset;
|
||||
else {
|
||||
my_clock_base = 0;
|
||||
coap_ticks(&t);
|
||||
while(size--)
|
||||
while(size--)
|
||||
my_clock_base = my_clock_base * 10 + *data++;
|
||||
my_clock_base -= t / COAP_TICKS_PER_SECOND;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
hnd_delete_time(coap_context_t *ctx UNUSED_PARAM, struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer UNUSED_PARAM, coap_pdu_t *request UNUSED_PARAM, str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response UNUSED_PARAM) {
|
||||
my_clock_base = 0; /* mark clock as "deleted" */
|
||||
|
||||
hnd_delete_time(coap_context_t *ctx UNUSED_PARAM,
|
||||
struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer UNUSED_PARAM,
|
||||
coap_pdu_t *request UNUSED_PARAM,
|
||||
str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response UNUSED_PARAM) {
|
||||
my_clock_base = 0; /* mark clock as "deleted" */
|
||||
|
||||
/* type = request->hdr->type == COAP_MESSAGE_CON */
|
||||
/* ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; */
|
||||
}
|
||||
|
||||
#ifndef WITHOUT_ASYNC
|
||||
static void
|
||||
hnd_get_async(coap_context_t *ctx, struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer, coap_pdu_t *request, str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response) {
|
||||
hnd_get_async(coap_context_t *ctx,
|
||||
struct coap_resource_t *resource UNUSED_PARAM,
|
||||
const coap_endpoint_t *local_interface UNUSED_PARAM,
|
||||
coap_address_t *peer,
|
||||
coap_pdu_t *request,
|
||||
str *token UNUSED_PARAM,
|
||||
coap_pdu_t *response) {
|
||||
coap_opt_iterator_t opt_iter;
|
||||
coap_opt_t *option;
|
||||
unsigned long delay = 5;
|
||||
@@ -213,33 +237,36 @@ hnd_get_async(coap_context_t *ctx, struct coap_resource_t *resource UNUSED_PARA
|
||||
delay = delay * 10 + (*p - '0');
|
||||
}
|
||||
|
||||
async = coap_register_async(ctx, peer, request,
|
||||
COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM,
|
||||
(void *)(COAP_TICKS_PER_SECOND * delay));
|
||||
async = coap_register_async(ctx,
|
||||
peer,
|
||||
request,
|
||||
COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM,
|
||||
(void *)(COAP_TICKS_PER_SECOND * delay));
|
||||
}
|
||||
|
||||
static void
|
||||
check_async(coap_context_t *ctx, const coap_endpoint_t *local_if,
|
||||
coap_tick_t now) {
|
||||
check_async(coap_context_t *ctx,
|
||||
const coap_endpoint_t *local_if,
|
||||
coap_tick_t now) {
|
||||
coap_pdu_t *response;
|
||||
coap_async_state_t *tmp;
|
||||
|
||||
size_t size = sizeof(coap_hdr_t) + 13;
|
||||
|
||||
if (!async || now < async->created + (unsigned long)async->appdata)
|
||||
if (!async || now < async->created + (unsigned long)async->appdata)
|
||||
return;
|
||||
|
||||
response = coap_pdu_init(async->flags & COAP_ASYNC_CONFIRM
|
||||
? COAP_MESSAGE_CON
|
||||
: COAP_MESSAGE_NON,
|
||||
COAP_RESPONSE_CODE(205), 0, size);
|
||||
response = coap_pdu_init(async->flags & COAP_ASYNC_CONFIRM
|
||||
? COAP_MESSAGE_CON
|
||||
: COAP_MESSAGE_NON,
|
||||
COAP_RESPONSE_CODE(205), 0, size);
|
||||
if (!response) {
|
||||
debug("check_async: insufficient memory, we'll try later\n");
|
||||
async->appdata =
|
||||
async->appdata =
|
||||
(void *)((unsigned long)async->appdata + 15 * COAP_TICKS_PER_SECOND);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
response->hdr->id = coap_new_message_id(ctx);
|
||||
|
||||
if (async->tokenlen)
|
||||
@@ -248,8 +275,8 @@ check_async(coap_context_t *ctx, const coap_endpoint_t *local_if,
|
||||
coap_add_data(response, 4, (unsigned char *)"done");
|
||||
|
||||
if (coap_send(ctx, local_if, &async->peer, response) == COAP_INVALID_TID) {
|
||||
debug("check_async: cannot send response for message %d\n",
|
||||
response->hdr->id);
|
||||
debug("check_async: cannot send response for message %d\n",
|
||||
response->hdr->id);
|
||||
}
|
||||
coap_delete_pdu(response);
|
||||
coap_remove_async(ctx, async->id, &tmp);
|
||||
@@ -304,18 +331,18 @@ usage( const char *program, const char *version) {
|
||||
program = ++p;
|
||||
|
||||
fprintf( stderr, "%s v%s -- a small CoAP implementation\n"
|
||||
"(c) 2010,2011,2015 Olaf Bergmann <bergmann@tzi.org>\n\n"
|
||||
"usage: %s [-A address] [-p port]\n\n"
|
||||
"\t-A address\tinterface address to bind to\n"
|
||||
"\t-g group\tjoin the given multicast group\n"
|
||||
"\t-p port\t\tlisten on specified port\n"
|
||||
"\t-v num\t\tverbosity level (default: 3)\n",
|
||||
program, version, program );
|
||||
"(c) 2010,2011,2015 Olaf Bergmann <bergmann@tzi.org>\n\n"
|
||||
"usage: %s [-A address] [-p port]\n\n"
|
||||
"\t-A address\tinterface address to bind to\n"
|
||||
"\t-g group\tjoin the given multicast group\n"
|
||||
"\t-p port\t\tlisten on specified port\n"
|
||||
"\t-v num\t\tverbosity level (default: 3)\n",
|
||||
program, version, program );
|
||||
}
|
||||
|
||||
static coap_context_t *
|
||||
get_context(const char *node, const char *port) {
|
||||
coap_context_t *ctx = NULL;
|
||||
coap_context_t *ctx = NULL;
|
||||
int s;
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp;
|
||||
@@ -324,12 +351,12 @@ get_context(const char *node, const char *port) {
|
||||
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
||||
hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */
|
||||
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
|
||||
|
||||
|
||||
s = getaddrinfo(node, port, &hints, &result);
|
||||
if ( s != 0 ) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* iterate through results until success */
|
||||
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
||||
@@ -342,15 +369,15 @@ get_context(const char *node, const char *port) {
|
||||
|
||||
ctx = coap_new_context(&addr);
|
||||
if (ctx) {
|
||||
/* TODO: output address:port for successful binding */
|
||||
goto finish;
|
||||
/* TODO: output address:port for successful binding */
|
||||
goto finish;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fprintf(stderr, "no context available for interface '%s'\n", node);
|
||||
|
||||
finish:
|
||||
finish:
|
||||
freeaddrinfo(result);
|
||||
return ctx;
|
||||
}
|
||||
@@ -369,7 +396,7 @@ join(coap_context_t *ctx, char *group_name){
|
||||
result = getaddrinfo("::", NULL, &hints, &reslocal);
|
||||
if (result < 0) {
|
||||
fprintf(stderr, "join: cannot resolve link-local interface: %s\n",
|
||||
gai_strerror(result));
|
||||
gai_strerror(result));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
@@ -377,7 +404,7 @@ join(coap_context_t *ctx, char *group_name){
|
||||
for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) {
|
||||
if (ainfo->ai_family == AF_INET6) {
|
||||
mreq.ipv6mr_interface =
|
||||
((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id;
|
||||
((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -391,20 +418,20 @@ join(coap_context_t *ctx, char *group_name){
|
||||
|
||||
if (result < 0) {
|
||||
fprintf(stderr, "join: cannot resolve multicast address: %s\n",
|
||||
gai_strerror(result));
|
||||
gai_strerror(result));
|
||||
goto finish;
|
||||
}
|
||||
|
||||
for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
|
||||
if (ainfo->ai_family == AF_INET6) {
|
||||
mreq.ipv6mr_multiaddr =
|
||||
((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
|
||||
((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result = setsockopt(ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
|
||||
(char *)&mreq, sizeof(mreq));
|
||||
(char *)&mreq, sizeof(mreq));
|
||||
if (result < 0)
|
||||
perror("join: setsockopt");
|
||||
|
||||
@@ -491,17 +518,17 @@ main(int argc, char **argv) {
|
||||
}
|
||||
result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
|
||||
|
||||
if ( result < 0 ) { /* error */
|
||||
if ( result < 0 ) { /* error */
|
||||
if (errno != EINTR)
|
||||
perror("select");
|
||||
} else if ( result > 0 ) { /* read from socket */
|
||||
perror("select");
|
||||
} else if ( result > 0 ) { /* read from socket */
|
||||
if ( FD_ISSET( ctx->sockfd, &readfds ) ) {
|
||||
coap_read( ctx ); /* read received data */
|
||||
/* coap_dispatch( ctx ); /\* and dispatch PDUs from receivequeue *\/ */
|
||||
coap_read( ctx ); /* read received data */
|
||||
/* coap_dispatch( ctx ); /\* and dispatch PDUs from receivequeue *\/ */
|
||||
}
|
||||
} else { /* timeout */
|
||||
} else { /* timeout */
|
||||
if (time_resource) {
|
||||
time_resource->dirty = 1;
|
||||
time_resource->dirty = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user