Skip to content

Commit

Permalink
pkg/libcoap: Use Sock interface instead of Posix interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Mar 5, 2024
1 parent 9700088 commit d1b223e
Show file tree
Hide file tree
Showing 12 changed files with 2,293 additions and 35 deletions.
2 changes: 2 additions & 0 deletions examples/libcoap-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ USEMODULE += prng_sha1prng

# libcoap support
USEPKG += libcoap
# Uncomment to enable libcoap OSCORE support
# USEMODULE += libcoap_oscore

# Configure if DNS is required
# USEMODULE += sock_dns
Expand Down
40 changes: 29 additions & 11 deletions examples/libcoap-client/client-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
* of use.
*/

#include "coap_config.h"
#include <thread.h>
#include <debug.h>
#include <coap3/coap.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "client-coap.h"
#include <stdio.h>
#include "macros/utils.h"
#include "net/utils.h"
#include <arpa/inet.h>
#include <thread.h>
#include <debug.h>

#ifdef CONFIG_LIBCOAP_CLIENT_URI
#define COAP_CLIENT_URI CONFIG_LIBCOAP_CLIENT_URI
Expand All @@ -43,6 +38,11 @@ static coap_context_t *main_coap_context = NULL;
static coap_optlist_t *optlist = NULL;

static int quit = 0;
static int is_mcast = 0;

#define DEFAULT_WAIT_TIME 15

unsigned int wait_seconds = DEFAULT_WAIT_TIME; /* default timeout in seconds */

static coap_response_t
message_handler(coap_session_t *session,
Expand Down Expand Up @@ -111,6 +111,7 @@ resolve_address(const char *host, const char *service, coap_address_t *dst,
if (addr_info) {
ret = 1;
*dst = addr_info->addr;
is_mcast = coap_is_mcast(dst);
}

coap_free_address_info(addr_info);
Expand All @@ -127,6 +128,8 @@ client_coap_init(int argc, char **argv)
int len;
coap_uri_t uri;
char portbuf[8];
unsigned int wait_ms = 0;
int result = -1;

#define BUFSIZE 100
unsigned char buf[BUFSIZE];
Expand Down Expand Up @@ -214,7 +217,7 @@ client_coap_init(int argc, char **argv)
coap_register_nack_handler(main_coap_context, nack_handler);

/* construct CoAP message */
pdu = coap_pdu_init(coap_is_mcast(&dst) ? COAP_MESSAGE_NON : COAP_MESSAGE_CON,
pdu = coap_pdu_init(is_mcast ? COAP_MESSAGE_NON : COAP_MESSAGE_CON,
COAP_REQUEST_CODE_GET,
coap_new_message_id(session),
coap_session_max_pdu_size(session));
Expand All @@ -237,15 +240,30 @@ client_coap_init(int argc, char **argv)
goto fail;
}
}
if (is_mcast) {
/* Allow for other servers to respond within DEFAULT_LEISURE RFC7252 8.2 */
wait_seconds = coap_session_get_default_leisure(session).integer_part + 1;
}
wait_ms = wait_seconds * 1000;

/* and send the PDU */
mid = coap_send(session, pdu);
if (mid == COAP_INVALID_MID) {
coap_log_warn("Failed to send PDU\n");
goto fail;
}
while (!quit) {
coap_io_process(main_coap_context, 1000);
while (!quit || is_mcast) {
result = coap_io_process(main_coap_context, 1000);
if (result >= 0) {
if (wait_ms > 0) {
if ((unsigned)result >= wait_ms) {
coap_log_info("timeout\n");
break;
} else {
wait_ms -= result;
}
}
}
}
fail:
/* Clean up library usage so client can be run again */
Expand Down
3 changes: 0 additions & 3 deletions examples/libcoap-client/client-coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
extern "C" {
#endif

#include "coap_config.h"
#include <coap3/coap.h>

/* Start up the CoAP Client */
void client_coap_init(int argc, char **argv);

Expand Down
3 changes: 2 additions & 1 deletion examples/libcoap-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ USEMODULE += prng_sha1prng

# libcoap support
USEPKG += libcoap
# Uncomment to enable libcoap OSCORE support
# USEMODULE += libcoap_oscore

USEMODULE += sock_udp
# Configure if DNS is required
USEMODULE += sock_dns

Expand Down
6 changes: 3 additions & 3 deletions examples/libcoap-server/server-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ init_coap_context_endpoints(const char *use_psk)
}

coap_address_init(&listenaddress);
listenaddress.addr.sin6.sin6_family = AF_INET6;
memcpy(&listenaddress.addr.sin6.sin6_addr, &addr,
sizeof(listenaddress.addr.sin6.sin6_addr));
listenaddress.riot.family = AF_INET6;
memcpy(&listenaddress.riot.addr.ipv6, &addr,
sizeof(listenaddress.riot.addr.ipv6));
coap_print_ip_addr(&listenaddress, addr_str, sizeof(addr_str));
coap_log_info("Server IP [%s]\n", addr_str);

Expand Down
3 changes: 0 additions & 3 deletions examples/libcoap-server/server-coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
extern "C" {
#endif

#include "coap_config.h"
#include <coap3/coap.h>

/* Start up the CoAP Server */
void server_coap_init(int argc, char **argv);

Expand Down
14 changes: 12 additions & 2 deletions pkg/libcoap/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if KCONFIG_USEPKG_LIBCOAP

choice LIBCOAP_DEBUG_LEVEL
bool "Set CoAP debugging level"
default LIBCOAP_LOG_WARNING
default LIBCOAP_LOG_INFO
help
Set CoAP debugging level

Expand Down Expand Up @@ -57,6 +57,7 @@ config LIBCOAP_IPV4_SUPPORT
Enable IPv4 functionality for CoAP.

If this option is disabled, redundant CoAP IPv4 code is removed.
[RIOT sock gnrc does not support this]

config LIBCOAP_IPV6_SUPPORT
bool "Enable IPv6 support within CoAP"
Expand All @@ -66,6 +67,8 @@ config LIBCOAP_IPV6_SUPPORT

If this option is disabled, redundant CoAP IPv6 code is removed.

if USEMODULE_SOCK_TCP

config LIBCOAP_TCP_SUPPORT
bool "Enable TCP support within CoAP"
default n
Expand All @@ -74,15 +77,22 @@ config LIBCOAP_TCP_SUPPORT
are to be used. Note that RIOT TCP support also needs to be enabled.

If this option is disabled, redundant CoAP TCP code is removed.
[RIOT sock gnrc does not support this]

endif # USEMODULE_SOCK_TCP

if USEMODULE_LIBCOAP_OSCORE

config LIBCOAP_OSCORE_SUPPORT
bool "Enable OSCORE support within CoAP"
default n
default y
help
Enable OSCORE functionality for CoAP.

If this option is disabled, redundant CoAP OSCORE code is removed.

endif # MODULE_LIBCOAP_OSCORE

config LIBCOAP_OBSERVE_PERSIST
bool "Enable Observe persist support within CoAP"
default n
Expand Down
19 changes: 14 additions & 5 deletions pkg/libcoap/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PKG_NAME=libcoap
PKG_URL=https://github.com/obgm/libcoap
PKG_VERSION=a733325b16e9070c03b0a664a6dbe01ffda7de65
PKG_VERSION=9f499ad42f963c52ad475137a526056b82783fe0
PKG_LICENSE=BSD-2-Clause

LIBCOAP_BUILD_DIR=$(BINDIR)/pkg/$(PKG_NAME)
Expand All @@ -9,7 +9,16 @@ LIBCOAP_INCLUDE_DIR=$(RIOTBASE)/build/pkg/$(PKG_NAME)/include/coap3

include $(RIOTBASE)/pkg/pkg.mk

all:
@cp $(LIBCOAP_SOURCE_DIR)/coap_config.h.riot $(LIBCOAP_SOURCE_DIR)/coap_config.h
@cp $(LIBCOAP_INCLUDE_DIR)/coap.h.riot $(LIBCOAP_INCLUDE_DIR)/coap.h
"$(MAKE)" -C $(LIBCOAP_SOURCE_DIR)/src -f $(CURDIR)/Makefile.libcoap
ifneq (,$(filter libcoap_oscore,$(USEMODULE)))
all: libcoap libcoap_oscore
else
all: libcoap
endif

libcoap:
$(QQ)@cp $(LIBCOAP_SOURCE_DIR)/coap_config.h.riot $(LIBCOAP_SOURCE_DIR)/coap_config.h
$(QQ)@cp $(LIBCOAP_INCLUDE_DIR)/coap.h.riot $(LIBCOAP_INCLUDE_DIR)/coap.h
$(QQ)"$(MAKE)" -C $(LIBCOAP_SOURCE_DIR)/src -f $(CURDIR)/Makefile.libcoap

libcoap_oscore:
$(QQ)"$(MAKE)" -C $(LIBCOAP_SOURCE_DIR)/src/oscore -f $(CURDIR)/Makefile.oscore
9 changes: 5 additions & 4 deletions pkg/libcoap/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
USEMODULE += posix_headers
USEMODULE += posix_inet
USEMODULE += sock_udp
USEMODULE += sock_tcp
USEMODULE += sock_aux_local
USEMODULE += gnrc_sock_async
USEMODULE += sock_async_event

ifneq (,$(filter libcoap,$(USEPKG)))
USEMODULE += libcoap
endif

ifneq (,$(filter libcoap_oscore,$(USEPKG)))
USEMODULE += libcoap_oscore
endif
10 changes: 10 additions & 0 deletions pkg/libcoap/Makefile.oscore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MODULE := libcoap_oscore

SRC := \
oscore.c \
oscore_cbor.c \
oscore_context.c \
oscore_cose.c \
oscore_crypto.c

include $(RIOTBASE)/Makefile.base
Loading

0 comments on commit d1b223e

Please sign in to comment.