Commit 2a7546fb authored by Michael Scott's avatar Michael Scott Committed by Anas Nashif
Browse files

net: lwm2m: add support for coap2coap proxy



Currently, LwM2M firmware download only supports coap2http proxy.
Let's add support for coap2coap proxy as well.

This was tested running Californium demo app cf-proxy on the host
machine with the following setting changed in Californum.properties:
MAX_RESOURCE_BODY_SIZE=524288

Add the following to the samples/net/lwm2m_client/prj.conf:
CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT=y
CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_ADDR="coap://[2001:db8::2]:5682"

Build the sample for qemu_x86 as you would normally, but now
you can use a real world coap address to pull firmware using the 5/0/1
resource.  The host machine running cf-proxy will pull the remote
resource and then deliver it to the running qemu sample.

Signed-off-by: default avatarMichael Scott <michael@opensourcefoundries.com>
parent 5f4c7fcf
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -163,14 +163,6 @@ config LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_ADDR
	help
	  Network address of the CoAP proxy server.

config LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_URI_PATH
	string "CoAP URI path element used by the proxy"
	default "coap2http"
	help
	  CoAP URI path element exported by the CoAP proxy server.
	  Defaults to coap2http, which is the URI path used by the
	  Californium CoAP-HTTP proxy.

endif # LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT

config LWM2M_RW_JSON_SUPPORT
+39 −13
Original line number Diff line number Diff line
@@ -36,6 +36,9 @@ static int firmware_retry;
static struct coap_block_context firmware_block_ctx;

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT)
#define COAP2COAP_PROXY_URI_PATH	"coap2coap"
#define COAP2HTTP_PROXY_URI_PATH	"coap2http"

static char proxy_uri[URI_LEN];
#endif

@@ -54,15 +57,14 @@ static int transfer_request(struct coap_block_context *ctx,
{
	struct lwm2m_message *msg;
	int ret;
	u16_t off;
	u16_t len;
	char *cursor;
#if !defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT)
	int i;
	int path_len;
	char *cursor;
	u16_t off;
	u16_t len;
#else
	char *uri_path =
		CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_URI_PATH;
	char *uri_path;
#endif

	msg = lwm2m_get_message(&firmware_ctx);
@@ -84,14 +86,7 @@ static int transfer_request(struct coap_block_context *ctx,
		goto cleanup;
	}

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT)
	ret = coap_packet_append_option(&msg->cpkt, COAP_OPTION_URI_PATH,
					uri_path, strlen(uri_path));
	if (ret < 0) {
		SYS_LOG_ERR("Error adding URI_PATH '%s'", uri_path);
		goto cleanup;
	}
#else
#if !defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT)
	/* if path is not available, off/len will be zero */
	off = parsed_uri.field_data[UF_PATH].off;
	len = parsed_uri.field_data[UF_PATH].len;
@@ -140,12 +135,41 @@ static int transfer_request(struct coap_block_context *ctx,
	}

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT)
	/* if path is not available, off/len will be zero */
	off = parsed_uri.field_data[UF_SCHEMA].off;
	len = parsed_uri.field_data[UF_SCHEMA].len;
	cursor = firmware_uri + off;

	ret = coap_packet_append_option(&msg->cpkt, COAP_OPTION_PROXY_URI,
					firmware_uri, strlen(firmware_uri));
	if (ret < 0) {
		SYS_LOG_ERR("Error adding PROXY_URI '%s'", firmware_uri);
		goto cleanup;
	}

	/* TODO: convert to lower case */
	if (len < 4 || len > 5) {
		ret = -EPROTONOSUPPORT;
		SYS_LOG_ERR("Unsupported schema");
		goto cleanup;
	}

	if (strncmp(cursor, (len == 4 ? "http" : "https"), len) == 0) {
		uri_path = COAP2HTTP_PROXY_URI_PATH;
	} else if (strncmp(cursor, (len == 4 ? "coap" : "coaps"), len) == 0) {
		uri_path = COAP2COAP_PROXY_URI_PATH;
	} else {
		ret = -EPROTONOSUPPORT;
		SYS_LOG_ERR("Unsupported schema");
		goto cleanup;
	}

	ret = coap_packet_append_option(&msg->cpkt, COAP_OPTION_PROXY_SCHEME,
					uri_path, strlen(uri_path));
	if (ret < 0) {
		SYS_LOG_ERR("Error adding URI_PATH '%s'", uri_path);
		goto cleanup;
	}
#else
	/* Ask the server to provide a size estimate */
	ret = coap_append_option_int(&msg->cpkt, COAP_OPTION_SIZE2, 0);
@@ -169,6 +193,8 @@ cleanup:

	if (ret == -ENOMEM) {
		lwm2m_firmware_set_update_result(RESULT_OUT_OF_MEM);
	} else if (ret == -EPROTONOSUPPORT) {
		lwm2m_firmware_set_update_result(RESULT_UNSUP_PROTO);
	} else {
		lwm2m_firmware_set_update_result(RESULT_CONNECTION_LOST);
	}