Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/net/http_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
extern "C" {
#endif

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i remember having seen this change a while back wanted to comment on it but somehow forgot, dont really like all of this ifdefing, it would cleaner if we split the url parser out into its own library instead and reused it in the http library and elsewhere. All of those #ifs make the code and header unreadable

/* Also update SONAME in the Makefile whenever you change these. */
#define HTTP_PARSER_VERSION_MAJOR 2
#define HTTP_PARSER_VERSION_MINOR 7
#define HTTP_PARSER_VERSION_PATCH 1
#endif

#include <sys/types.h>
#if defined(_WIN32) && !defined(__MINGW32__) && \
Expand All @@ -47,6 +49,7 @@ typedef unsigned __int64 u64_t;
#include <stddef.h>
#endif

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
/* Maximium header size allowed. If the macro is not defined
* before including this header then the default is used. To
* change the maximum header size, define the macro in the build
Expand Down Expand Up @@ -227,6 +230,7 @@ struct http_parser_settings {
http_cb on_chunk_header;
http_cb on_chunk_complete;
};
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */


enum http_parser_url_fields {
Expand Down Expand Up @@ -261,6 +265,7 @@ struct http_parser_url {
};


#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
/* Returns the library version. Bits 16-23 contain the major version number,
* bits 8-15 the minor version number and bits 0-7 the patch level.
* Usage example:
Expand Down Expand Up @@ -305,6 +310,7 @@ const char *http_errno_name(enum http_errno err);

/* Return a string description of the given error */
const char *http_errno_description(enum http_errno err);
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */

/* Initialize all http_parser_url members to 0 */
void http_parser_url_init(struct http_parser_url *u);
Expand All @@ -313,11 +319,13 @@ void http_parser_url_init(struct http_parser_url *u);
int http_parser_parse_url(const char *buf, size_t buflen,
int is_connect, struct http_parser_url *u);

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
/* Pause or un-pause the parser; a nonzero value pauses */
void http_parser_pause(struct http_parser *parser, int paused);

/* Checks if this is the final chunk of the body. */
int http_body_is_final(const struct http_parser *parser);
#endif

#ifdef __cplusplus
}
Expand Down
26 changes: 25 additions & 1 deletion include/net/lwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,15 @@ int lwm2m_device_add_err(u8_t error_code);
#define RESULT_UPDATE_FAILED 8
#define RESULT_UNSUP_PROTO 9

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
void lwm2m_firmware_set_write_cb(lwm2m_engine_set_data_cb_t cb);
lwm2m_engine_set_data_cb_t lwm2m_firmware_get_write_cb(void);

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT)
void lwm2m_firmware_set_update_cb(lwm2m_engine_exec_cb_t cb);
lwm2m_engine_exec_cb_t lwm2m_firmware_get_update_cb(void);
#endif
#endif

/* LWM2M Engine */

Expand Down Expand Up @@ -192,8 +198,26 @@ int lwm2m_engine_start(struct lwm2m_ctx *client_ctx,

/* LWM2M RD Client */

/* Client events */
enum lwm2m_rd_client_event {
LWM2M_RD_CLIENT_EVENT_NONE,
LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_FAILURE,
LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_COMPLETE,
LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE,
LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE,
LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE,
LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE,
LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE,
LWM2M_RD_CLIENT_EVENT_DISCONNECT
};

/* Event callback */
typedef void (*lwm2m_ctx_event_cb_t)(struct lwm2m_ctx *ctx,
enum lwm2m_rd_client_event event);

int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
char *peer_str, u16_t peer_port,
const char *ep_name);
const char *ep_name,
lwm2m_ctx_event_cb_t event_cb);

#endif /* __LWM2M_H__ */
1 change: 1 addition & 0 deletions samples/net/lwm2m_client/prj_frdm_k64f.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CONFIG_LWM2M_RD_CLIENT_INSTANCE_COUNT=2
CONFIG_LWM2M_IPSO_SUPPORT=y
CONFIG_LWM2M_IPSO_TEMP_SENSOR=y
CONFIG_LWM2M_IPSO_LIGHT_CONTROL=y
CONFIG_HTTP_PARSER_URL_ONLY=y

CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"
Expand Down
1 change: 1 addition & 0 deletions samples/net/lwm2m_client/prj_qemu_x86.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CONFIG_LWM2M_RD_CLIENT_INSTANCE_COUNT=2
CONFIG_LWM2M_IPSO_SUPPORT=y
CONFIG_LWM2M_IPSO_TEMP_SENSOR=y
CONFIG_LWM2M_IPSO_LIGHT_CONTROL=y
CONFIG_HTTP_PARSER_URL_ONLY=y

CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"
Expand Down
70 changes: 65 additions & 5 deletions samples/net/lwm2m_client/src/lwm2m-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,23 @@ static int device_factory_default_cb(u16_t obj_inst_id)
return 1;
}

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT)
static int firmware_update_cb(u16_t obj_inst_id)
{
SYS_LOG_DBG("UPDATE");

/* TODO: kick off update process */

/* If success, set the update result as RESULT_SUCCESS.
* In reality, it should be set at function lwm2m_setup()
*/
lwm2m_engine_set_u8("5/0/3", STATE_IDLE);
lwm2m_engine_set_u8("5/0/5", RESULT_SUCCESS);
return 1;
}
#endif

#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
static int firmware_block_received_cb(u16_t obj_inst_id,
u8_t *data, u16_t data_len,
bool last_block, size_t total_size)
Expand All @@ -161,6 +172,7 @@ static int firmware_block_received_cb(u16_t obj_inst_id,
data_len, last_block);
return 1;
}
#endif

static int lwm2m_setup(void)
{
Expand Down Expand Up @@ -204,10 +216,12 @@ static int lwm2m_setup(void)

/* setup FIRMWARE object */

lwm2m_engine_register_post_write_callback("5/0/0",
firmware_block_received_cb);
#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
lwm2m_firmware_set_write_cb(firmware_block_received_cb);
lwm2m_engine_register_exec_callback("5/0/2", firmware_update_cb);
#endif
#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT)
lwm2m_firmware_set_update_cb(firmware_update_cb);
#endif

/* setup TEMP SENSOR object */

Expand All @@ -227,6 +241,50 @@ static int lwm2m_setup(void)
return 0;
}

static void rd_client_event(struct lwm2m_ctx *client,
enum lwm2m_rd_client_event client_event)
{
switch (client_event) {

case LWM2M_RD_CLIENT_EVENT_NONE:
/* do nothing */
break;

case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_FAILURE:
SYS_LOG_DBG("Bootstrap failure!");
break;

case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_COMPLETE:
SYS_LOG_DBG("Bootstrap complete");
break;

case LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE:
SYS_LOG_DBG("Registration failure!");
break;

case LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE:
SYS_LOG_DBG("Registration complete");
break;

case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE:
SYS_LOG_DBG("Registration update failure!");
break;

case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE:
SYS_LOG_DBG("Registration update complete");
break;

case LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE:
SYS_LOG_DBG("Deregister failure!");
break;

case LWM2M_RD_CLIENT_EVENT_DISCONNECT:
SYS_LOG_DBG("Disconnected");
break;

}
}

void main(void)
{
int ret;
Expand All @@ -251,10 +309,12 @@ void main(void)

#if defined(CONFIG_NET_IPV6)
ret = lwm2m_rd_client_start(&client, CONFIG_NET_APP_PEER_IPV6_ADDR,
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD);
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD,
rd_client_event);
#elif defined(CONFIG_NET_IPV4)
ret = lwm2m_rd_client_start(&client, CONFIG_NET_APP_PEER_IPV4_ADDR,
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD);
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD,
rd_client_event);
#else
SYS_LOG_ERR("LwM2M client requires IPv4 or IPv6.");
ret = -EPROTONOSUPPORT;
Expand Down
7 changes: 7 additions & 0 deletions subsys/net/lib/http/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ config HTTP_PARSER
This parser requires some string-related routines commonly
provided by a libc implementation.

config HTTP_PARSER_URL_ONLY
bool "HTTP URL parser only support"
default n
select HTTP_PARSER
help
This option only enables URL parser of the http_parser library.

config HTTP_PARSER_STRICT
bool "HTTP strict parsing"
default n
Expand Down
12 changes: 12 additions & 0 deletions subsys/net/lib/http/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ do { \
} \
} while (0)

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
/* Don't allow the total size of the HTTP headers (including the status
* line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
* embedders against denial-of-service attacks where the attacker feeds
Expand Down Expand Up @@ -169,6 +170,7 @@ s8_t unhex[256] = {
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */


#ifdef HTTP_PARSER_STRICT
Expand Down Expand Up @@ -284,6 +286,7 @@ enum state {
s_message_done
};

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
#define PARSING_HEADER(state) (state <= s_headers_done)

enum header_states {
Expand Down Expand Up @@ -311,6 +314,7 @@ enum header_states {
h_connection_close,
h_connection_upgrade
};
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */

enum http_host_state {
s_http_host_dead = 1,
Expand All @@ -327,6 +331,7 @@ enum http_host_state {
s_http_host_port
};

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
static inline
int cb_notify(struct http_parser *parser, enum state *current_state, http_cb cb,
int cb_error, size_t *parsed, size_t already_parsed)
Expand Down Expand Up @@ -382,6 +387,7 @@ int cb_data(struct http_parser *parser, http_data_cb cb, int cb_error,

return 0;
}
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */


/* Macros for character classes; depends on strict-mode */
Expand Down Expand Up @@ -416,6 +422,7 @@ int cb_data(struct http_parser *parser, http_data_cb cb, int cb_error,
(IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
#endif

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
/**
* Verify that a char is a valid visible (printable) US-ASCII
* character or %x80-FF
Expand Down Expand Up @@ -493,6 +500,7 @@ static struct {
};

int http_message_needs_eof(const struct http_parser *parser);
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */

/* Our URL parser.
*
Expand Down Expand Up @@ -655,6 +663,7 @@ static enum state parse_url_char(enum state s, const char ch)
return s_dead;
}

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
static
int parser_header_state(struct http_parser *parser, char ch, char c)
{
Expand Down Expand Up @@ -2624,6 +2633,7 @@ const char *http_errno_description(enum http_errno err)

return http_strerror_tab[err].description;
}
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */

static enum http_host_state
http_parse_host_char(enum http_host_state s, const char ch)
Expand Down Expand Up @@ -2906,6 +2916,7 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
return 0;
}

#if !defined(CONFIG_HTTP_PARSER_URL_ONLY)
void http_parser_pause(struct http_parser *parser, int paused)
{
/* Users should only be pausing/unpausing a parser that is not in an
Expand Down Expand Up @@ -2933,3 +2944,4 @@ unsigned long http_parser_version(void)
HTTP_PARSER_VERSION_MINOR * 0x00100 |
HTTP_PARSER_VERSION_PATCH * 0x00001;
}
#endif /* !CONFIG_HTTP_PARSER_URL_ONLY */
39 changes: 35 additions & 4 deletions subsys/net/lib/lwm2m/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,53 @@ config LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT
bool "Firmware Update object pull support"
default y
depends on LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT
depends on (HTTP_PARSER || HTTP_PARSER_URL_ONLY)
help
Include support for pulling a file from a remote server via
block transfer and "FIRMWARE PACKAGE URI" resource. This option
adds another UDP context and packet handling.

config LWM2M_FIRMWARE_UPDATE_PULL_COAP_BLOCK_SIZE
int "Firmware Update object pull CoAP block size"
depends on LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT
config LWM2M_COAP_BLOCK_SIZE
int "LWM2M CoAP block-wise transfer size"
default 256
default 64 if NET_L2_BT
default 64 if NET_L2_IEEE802154
range 16 1024
help
CoAP block size used by firmware pull when performing block-wise
CoAP block size used by LWM2M when performing block-wise
transfers. Possible values: 16, 32, 64, 128, 256, 512 and 1024.

config LWM2M_NUM_BLOCK1_CONTEXT
int "Maximum # of LWM2M block1 contexts"
default 3
help
This value sets up the maximum number of block1 contexts for
CoAP block-wise transfer we can handle at the same time.

config LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT
bool "Firmware Update object pull via CoAP-CoAP/HTTP proxy support"
depends on LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT
default n
help
Include support for pulling firmware file via a CoAP-CoAP/HTTP proxy.

if LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_SUPPORT

config LWM2M_FIRMWARE_UPDATE_PULL_COAP_PROXY_ADDR
string "CoAP proxy network address"
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
bool "support for JSON writer"
default y
Expand Down
Loading