Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lwm2m block transfer #54761

Merged
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
27 changes: 27 additions & 0 deletions include/zephyr/net/coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,20 @@ int coap_block_transfer_init(struct coap_block_context *ctx,
enum coap_block_size block_size,
size_t total_size);

/**
* @brief Append BLOCK1 or BLOCK2 option to the packet.
*
* If the CoAP packet is a request then BLOCK1 is appended
* otherwise BLOCK2 is appended.
*
* @param cpkt Packet to be updated
* @param ctx Block context from which to retrieve the
* information for the block option
*
* @return 0 in case of success or negative in case of error.
*/
int coap_append_descriptive_block_option(struct coap_packet *cpkt, struct coap_block_context *ctx);

/**
* @brief Append BLOCK1 option to the packet.
*
Expand Down Expand Up @@ -638,6 +652,19 @@ int coap_append_size2_option(struct coap_packet *cpkt,
*/
int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code);

/**
* @brief Get the block size, more flag and block number from the
* CoAP block1 option.
*
* @param cpkt Packet to be inspected
* @param has_more Is set to the value of the more flag
* @param block_number Is set to the number of the block
*
* @return Integer value of the block size in case of success
* or negative in case of error.
*/
int coap_get_block1_option(const struct coap_packet *cpkt, bool *has_more, uint8_t *block_number);

/**
* @brief Retrieves BLOCK{1,2} and SIZE{1,2} from @a cpkt and updates
* @a ctx accordingly.
Expand Down
23 changes: 23 additions & 0 deletions subsys/net/lib/coap/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,15 @@ int coap_block_transfer_init(struct coap_block_context *ctx,
#define SET_MORE(v, m) ((v) |= (m) ? 0x08 : 0x00)
#define SET_NUM(v, n) ((v) |= ((n) << 4))

int coap_append_descriptive_block_option(struct coap_packet *cpkt, struct coap_block_context *ctx)
{
if (is_request(cpkt)) {
rlubos marked this conversation as resolved.
Show resolved Hide resolved
return coap_append_block1_option(cpkt, ctx);
} else {
return coap_append_block2_option(cpkt, ctx);
}
}

int coap_append_block1_option(struct coap_packet *cpkt,
struct coap_block_context *ctx)
{
Expand Down Expand Up @@ -1041,6 +1050,20 @@ int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code)
return val;
}

int coap_get_block1_option(const struct coap_packet *cpkt, bool *has_more, uint8_t *block_number)
{
int ret = coap_get_option_int(cpkt, COAP_OPTION_BLOCK1);

if (ret < 0) {
return ret;
}

*has_more = GET_MORE(ret);
*block_number = GET_NUM(ret);
ret = 1 << (GET_BLOCK_SIZE(ret) + 4);
return ret;
}

int insert_option(struct coap_packet *cpkt, uint16_t code, const uint8_t *value, uint16_t len)
{
uint16_t offset = cpkt->hdr_len;
Expand Down
32 changes: 32 additions & 0 deletions subsys/net/lib/lwm2m/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@ config LWM2M_COAP_MAX_MSG_SIZE
CoAP message size used by LWM2M. Minimum is the block size used
in blockwise transfers.

config LWM2M_COAP_BLOCK_TRANSFER
bool "CoAP block transfer support for big outgoing LwM2M messages [EXPERIMENTAL]"
select EXPERIMENTAL
help
LwM2M messages with a big body that exceed the block size will be split
into blocks for sending.

if LWM2M_COAP_BLOCK_TRANSFER
config LWM2M_COAP_ENCODE_BUFFER_SIZE
int "LwM2M CoAP buffer size for encoding the full message for block-wise transfer"
default 1024
help
Size of buffers for serializing big LwM2M CoAP messages that need to be
split into blocks for sending.

config LWM2M_NUM_OUTPUT_BLOCK_CONTEXT
int "Maximum # of LWM2M block contexts used for outgoing messages"
default 3
help
Maximum number of CoAP block contexts needed to split messages into blocks for
sending. This limits the numer of messages that need block transfer that can be
handled at the same time.

config LWM2M_LOG_ENCODE_BUFFER_ALLOCATIONS
bool "Log allocations of encode buffers for block wise transfer"
select MEM_SLAB_TRACE_MAX_UTILIZATION
help
The allocation of encode buffers can be tracked to analyse the usage and
to optimize the configuration of number of block contexts and indirectly
the number of available encode buffers.
endif # LWM2M_COAP_BLOCK_TRANSFER

config LWM2M_ENGINE_VALIDATION_BUFFER_SIZE
int "Size of the validation buffer for the incoming data"
default 64
Expand Down
12 changes: 12 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,20 @@ static struct zsock_pollfd sock_fds[MAX_POLL_FD];
static struct lwm2m_ctx *sock_ctx[MAX_POLL_FD];
static int sock_nfds;

/* Resource wrappers */
#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
static struct coap_block_context output_block_contexts[NUM_OUTPUT_BLOCK_CONTEXT];
#endif

/* Resource wrappers */
struct lwm2m_ctx **lwm2m_sock_ctx(void) { return sock_ctx; }

int lwm2m_sock_nfds(void) { return sock_nfds; }

#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
struct coap_block_context *lwm2m_output_block_context(void) { return output_block_contexts; }
#endif

static int lwm2m_socket_update(struct lwm2m_ctx *ctx);

/* utility functions */
Expand Down Expand Up @@ -979,6 +988,9 @@ static int lwm2m_engine_init(void)
}

lwm2m_clear_block_contexts();
#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
(void)memset(output_block_contexts, 0, sizeof(output_block_contexts));
#endif

if (IS_ENABLED(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)) {
/* Init data cache */
Expand Down
Loading