Skip to content

Commit

Permalink
initial rest_client
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikVE committed Mar 10, 2022
1 parent 7276c12 commit 9f11b19
Show file tree
Hide file tree
Showing 48 changed files with 4,112 additions and 29 deletions.
10 changes: 10 additions & 0 deletions examples/emcute_mqttsn/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ BOARD ?= native
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

WIFI_SSID ?= "WIFI_SSID"
WIFI_PASS ?= "WIFI_PASS"

# Include packages that pull up and auto-init the link layer.
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
USEMODULE += netdev_default
# USEMODULE += esp_wifi
USEMODULE += auto_init_gnrc_netif
# Specify the mandatory networking modules for IPv6
USEMODULE += gnrc_ipv6_default
Expand Down Expand Up @@ -42,3 +46,9 @@ DEVELHELP ?= 1
QUIET ?= 1

include $(RIOTBASE)/Makefile.include

# needs to be put after "include $(RIOTBASE)/Makefile.include"
ifneq (,$(filter arch_esp,$(FEATURES_USED)))
CFLAGS += -DESP_WIFI_SSID=\"$(WIFI_SSID)\"
CFLAGS += -DESP_WIFI_PASS=\"$(WIFI_PASS)\"
endif
10 changes: 10 additions & 0 deletions examples/gcoap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ BOARD ?= native
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

WIFI_SSID ?= "WIFI_SSID"
WIFI_PASS ?= "WIFI_PASS"
CFLAGS += -DCONFIG_GCOAP_PDU_BUF_SIZE=1024

# Include packages that pull up and auto-init the link layer.
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
USEMODULE += netdev_default
Expand Down Expand Up @@ -82,6 +86,12 @@ endif

include $(RIOTBASE)/Makefile.include

# needs to be put after "include $(RIOTBASE)/Makefile.include"
ifneq (,$(filter arch_esp,$(FEATURES_USED)))
CFLAGS += -DESP_WIFI_SSID=\"$(WIFI_SSID)\"
CFLAGS += -DESP_WIFI_PASS=\"$(WIFI_PASS)\"
endif

# For now this goes after the inclusion of Makefile.include so Kconfig symbols
# are available. Only set configuration via CFLAGS if Kconfig is not being used
# for this module.
Expand Down
9 changes: 9 additions & 0 deletions examples/gcoap/Makefile.board.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Put board specific dependencies here

ifneq (,$(filter arch_esp,$(FEATURES_USED)))
USEMODULE += esp_wifi
endif

ifeq ($(BOARD),native)
USEMODULE += netdev_default
endif
24 changes: 19 additions & 5 deletions examples/gcoap/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@

static bool _proxied = false;
static sock_udp_ep_t _proxy_remote;
static char proxy_uri[64];
static char proxy_uri[512];

/* Retain request path to re-request if response includes block. User must not
* start a new request (with a new path) until any blockwise transfer
* completes or times out. */
#define _LAST_REQ_PATH_MAX (64)
#define _LAST_REQ_PATH_MAX (512)
static char _last_req_path[_LAST_REQ_PATH_MAX];

uint16_t req_count = 0;
Expand Down Expand Up @@ -211,6 +211,7 @@ static size_t _send(uint8_t *buf, size_t len, char *addr_str, char *port_str)
}

bytes_sent = gcoap_req_send(buf, len, remote, _resp_handler, NULL);
printf("len = %d\n", len);
if (bytes_sent > 0) {
req_count++;
}
Expand Down Expand Up @@ -345,11 +346,12 @@ int gcoap_cli_cmd(int argc, char **argv)

if (_proxied) {
if (strstr(argv[apos], ":") != NULL) { /* IPv6 might contain '.', but IPv4 cannot contain ':' */
uri_len = snprintf(proxy_uri, 64, "coap://[%s]:%s%s", argv[apos], argv[apos+1], uri);
uri_len = snprintf(proxy_uri, 64, "http://[%s]:%s%s", argv[apos], argv[apos+1], uri);
}
else {
uri_len = snprintf(proxy_uri, 64, "coap://%s:%s%s", argv[apos], argv[apos+1], uri);
uri_len = snprintf(proxy_uri, 64, "http://%s:%s%s", argv[apos], argv[apos+1], uri);
}

uri = proxy_uri;

gcoap_req_init(&pdu, &buf[0], CONFIG_GCOAP_PDU_BUF_SIZE, code_pos, NULL);
Expand All @@ -366,18 +368,25 @@ int gcoap_cli_cmd(int argc, char **argv)

size_t paylen = (argc == apos + 4) ? strlen(argv[apos+3]) : 0;
if (paylen) {
coap_opt_add_format(&pdu, COAP_FORMAT_TEXT);
coap_opt_add_format(&pdu, COAP_FORMAT_CBOR);
}

if (_proxied) {
coap_opt_add_proxy_uri(&pdu, uri);
}

if (paylen) {
coap_opt_add_uint(&pdu, 55, COAP_FORMAT_CBOR);
coap_opt_add_uint(&pdu, 59, COAP_FORMAT_JSON);

printf("payload = %s\n", argv[apos+3]);
printf("paylen = %d\n", paylen);
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
printf("A len = %d\n", len);
if (pdu.payload_len >= paylen) {
memcpy(pdu.payload, argv[apos+3], paylen);
len += paylen;
printf("len += paylen = %d += %d\n", len, paylen);
}
else {
puts("gcoap_cli: msg buffer too small");
Expand All @@ -386,10 +395,15 @@ int gcoap_cli_cmd(int argc, char **argv)
}
else {
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_NONE);
printf("B len = %d\n", len);
}

printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu),
(unsigned) len);
for (size_t i = 0; i < len; i++) {
printf("%02x", buf[i]);
}
printf("\n");
if (!_send(&buf[0], len, argv[apos], argv[apos+1])) {
puts("gcoap_cli: msg send failed");
}
Expand Down
4 changes: 2 additions & 2 deletions examples/gcoap/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ int main(void)

/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
char line_buf[4 * SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, 4 * SHELL_DEFAULT_BUFSIZE);

/* should never be reached */
return 0;
Expand Down
2 changes: 0 additions & 2 deletions examples/gcoap_dtls/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ else
endif

USEMODULE += gcoap
# Additional networking modules that can be dropped if not needed
USEMODULE += gnrc_icmpv6_echo

# Required by gcoap example
USEMODULE += od
Expand Down
120 changes: 120 additions & 0 deletions examples/rest_client/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# name of your application
APPLICATION = rest-client

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

WIFI_SSID ?= "WIFI_SSID"
WIFI_PASS ?= "WIFI_PASS"

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 to show compiler invocation lines by default
QUIET ?= 1

USEMODULE += rest_client

USEPKG += tinycbor
USEMODULE += tinycbor_float
USEMODULE += ztimer_msec

ENABLE_DEBUG ?= 1
CFLAGS += -DREST_CLIENT_ENABLE_DEBUG=$(ENABLE_DEBUG)
ifneq (0,$(ENABLE_DEBUG))
USEMODULE += rest_client_debug
endif

# for now only one should be possible at the same time
TRANSPORT_COAP ?= 0
TRANSPORT_COAP_SECURE ?= 0
TRANSPORT_MQTT ?= 0
TRANSPORT_MQTT_SN ?= 0

ifneq (0,$(TRANSPORT_COAP))
USEMODULE += rest_client_transport_coap
endif

ifneq (0,$(TRANSPORT_COAP_SECURE))
USEMODULE += rest_client_transport_coaps
endif

ifneq (0,$(TRANSPORT_MQTT))
USEMODULE += rest_client_transport_mqtt
endif

ifneq (0,$(TRANSPORT_MQTT_SN))
USEMODULE += rest_client_transport_mqttsn
endif

# general coap settings
ifneq (,$(filter 1, $(TRANSPORT_COAP) $(TRANSPORT_COAP_SECURE)))
CFLAGS += -DCONFIG_GCOAP_PDU_BUF_SIZE=1024
endif

# coaps specific config
ifeq (1,$(TRANSPORT_COAP_SECURE))
# Todo: remove once merged
# CFLAGS += -DCONFIG_GCOAP_DTLS_HANDSHAKE_TIMEOUT_MSEC=5000
endif

QOS ?= 2
CFLAGS += -DCONFIG_QOS=$(QOS)

# we need to use user_settings.h to control the wolfMQTT package
CFLAGS += -DWOLFMQTT_USER_SETTINGS=1

LWIP_IPV4 ?= 0
LWIP_IPV6 ?= 1

USEMODULE += lwip_netdev

ifneq (0,$(LWIP_IPV4))
USEMODULE += ipv4_addr
USEMODULE += lwip_arp
USEMODULE += lwip_ipv4
USEMODULE += lwip_dhcp_auto
CFLAGS += -DETHARP_SUPPORT_STATIC_ENTRIES=1

# not included when using IPv4-only, but required by CoAP
ifneq (, $(filter 1, $(TRANSPORT_COAP) $(TRANSPORT_COAP_SECURE)))
USEMODULE += random
endif

# Todo: only temporary until rebase
CFLAGS += -DSOCK_HAS_IPV4
endif

ifneq (0,$(LWIP_IPV6))
USEMODULE += ipv6_addr
USEMODULE += lwip_ipv6
USEMODULE += lwip_ipv6_autoconfig
endif

# Instead of simulating an Ethernet connection, we can also simulate
# an IEEE 802.15.4 radio using ZEP
USE_ZEP ?= 0

# set the ZEP port for native
ZEP_PORT_BASE ?= 17754
ifeq (1,$(USE_ZEP))
TERMFLAGS += -z [::1]:$(ZEP_PORT_BASE)
USEMODULE += socket_zep

ifneq (,$(ZEP_MAC))
TERMFLAGS += --eui64=$(ZEP_MAC)
endif
endif

include $(RIOTBASE)/Makefile.include

# needs to be put after "include $(RIOTBASE)/Makefile.include"
ifneq (,$(filter arch_esp,$(FEATURES_USED)))
CFLAGS += -DESP_WIFI_SSID=\"$(WIFI_SSID)\"
CFLAGS += -DESP_WIFI_PASS=\"$(WIFI_PASS)\"
endif
14 changes: 14 additions & 0 deletions examples/rest_client/Makefile.board.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Put board specific dependencies here

ifneq (,$(filter arch_esp,$(FEATURES_USED)))
USEMODULE += esp_wifi
else ifeq ($(BOARD),nucleo-f207zg)
USEMODULE += stm32_eth_auto

# default stack size is too small for MQTT and CoAPs based transport
CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=5000
else ifeq ($(BOARD),native)
USEMODULE += netdev_tap
else
USEMODULE += netdev_default
endif
Loading

0 comments on commit 9f11b19

Please sign in to comment.