Commit 421ecb77 authored by Pieter De Gendt's avatar Pieter De Gendt Committed by Anas Nashif
Browse files

net: coap: Add coap_next_block_for_option function



Add a function to update the coap block context from a
packet, according to the block option enum provided.

The existing coap_next_block does not handle block1 transfers
properly because we need to inspect the block1 option
returned by the server. This function is reworked to make use
of the newly introduced one.

Signed-off-by: default avatarPieter De Gendt <pieter.degendt@basalte.be>
parent b966ba16
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -646,6 +646,22 @@ int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code);
int coap_update_from_block(const struct coap_packet *cpkt,
			   struct coap_block_context *ctx);

/**
 * @brief Updates @a ctx according to @a option set in @a cpkt
 * so after this is called the current entry indicates the correct
 * offset in the body of data being transferred.
 *
 * @param cpkt Packet in which to look for block-wise transfers options
 * @param ctx Block context to be updated
 * @param option Either COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2
 *
 * @return The offset in the block-wise transfer, 0 if the transfer
 * has finished or a negative value in case of an error.
 */
int coap_next_block_for_option(const struct coap_packet *cpkt,
			       struct coap_block_context *ctx,
			       enum coap_option_num option);

/**
 * @brief Updates @a ctx so after this is called the current entry
 * indicates the correct offset in the body of data being
+24 −7
Original line number Diff line number Diff line
@@ -1077,15 +1077,20 @@ int coap_update_from_block(const struct coap_packet *cpkt,
	return update_descriptive_block(ctx, block2, size2 == -ENOENT ? 0 : size2);
}

size_t coap_next_block(const struct coap_packet *cpkt,
		       struct coap_block_context *ctx)
int coap_next_block_for_option(const struct coap_packet *cpkt,
			       struct coap_block_context *ctx,
			       enum coap_option_num option)
{
	int block;

	if (is_request(cpkt)) {
		block = coap_get_option_int(cpkt, COAP_OPTION_BLOCK1);
	} else {
		block = coap_get_option_int(cpkt, COAP_OPTION_BLOCK2);
	if (option != COAP_OPTION_BLOCK1 && option != COAP_OPTION_BLOCK2) {
		return -EINVAL;
	}

	block = coap_get_option_int(cpkt, option);

	if (block < 0) {
		return block;
	}

	if (!GET_MORE(block)) {
@@ -1094,7 +1099,19 @@ size_t coap_next_block(const struct coap_packet *cpkt,

	ctx->current += coap_block_size_to_bytes(ctx->block_size);

	return ctx->current;
	return (int)ctx->current;
}

size_t coap_next_block(const struct coap_packet *cpkt,
		       struct coap_block_context *ctx)
{
	enum coap_option_num option;
	int ret;

	option = is_request(cpkt) ? COAP_OPTION_BLOCK1 : COAP_OPTION_BLOCK2;
	ret = coap_next_block_for_option(cpkt, ctx, option);

	return MAX(ret, 0);
}

int coap_pending_init(struct coap_pending *pending,