Commit 068cc03b authored by Gerson Fernando Budke's avatar Gerson Fernando Budke Committed by Anas Nashif
Browse files

net: coap: Add coap_get_option_int public method

Any CoAP implementation when use at least block transfer or is a server
side need access some CoAP options as integer values. This add a method
at public interface and defines for block wise operations to avoid code
useless code duplication.

Signed-off-by: default avatarGerson Fernando Budke <nandojve@gmail.com>
parent aa9248f5
Loading
Loading
Loading
Loading
+16 −0
Changes for include/net/coap.h: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -147,6 +147,11 @@ enum coap_response_code {

#define COAP_CODE_EMPTY (0)

/* block option helper */
#define GET_BLOCK_NUM(v)        ((v) >> 4)
#define GET_BLOCK_SIZE(v)       (((v) & 0x7))
#define GET_MORE(v)             (!!((v) & 0x08))

struct coap_observer;
struct coap_packet;
struct coap_pending;
@@ -564,6 +569,17 @@ int coap_append_size1_option(struct coap_packet *cpkt,
int coap_append_size2_option(struct coap_packet *cpkt,
			     struct coap_block_context *ctx);

/**
 * @brief Get the integer representation of a CoAP option.
 *
 * @param cpkt Packet to be inspected
 * @param code CoAP option code
 *
 * @return Integer value >= 0 in case of success or negative in case
 * of error.
 */
int coap_get_option_int(const struct coap_packet *cpkt, u16_t code);

/**
 * @brief Retrieves BLOCK{1,2} and SIZE{1,2} from @a cpkt and updates
 * @a ctx accordingly.
+5 −15
Changes for subsys/net/lib/coap/coap.c: 5 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -921,9 +921,9 @@ int coap_append_size2_option(struct coap_packet *cpkt,
	return coap_append_option_int(cpkt, COAP_OPTION_SIZE2, ctx->total_size);
}

static int get_block_option(const struct coap_packet *cpkt, u16_t code)
int coap_get_option_int(const struct coap_packet *cpkt, u16_t code)
{
	struct coap_option option;
	struct coap_option option = {};
	unsigned int val;
	int count = 1;

@@ -937,6 +937,8 @@ static int get_block_option(const struct coap_packet *cpkt, u16_t code)
	return val;
}

#define get_block_option(cpkt, code) coap_get_option_int(cpkt, code)

static int update_descriptive_block(struct coap_block_context *ctx,
				    int block, int size)
{
@@ -1236,19 +1238,7 @@ void coap_pendings_clear(struct coap_pending *pendings, size_t len)
	}
}

static int get_observe_option(const struct coap_packet *cpkt)
{
	struct coap_option option = {};
	u16_t count = 1U;
	int r;

	r = coap_find_options(cpkt, COAP_OPTION_OBSERVE, &option, count);
	if (r <= 0) {
		return -ENOENT;
	}

	return coap_option_value_to_int(&option);
}
#define get_observe_option(cpkt) coap_get_option_int(cpkt, COAP_OPTION_OBSERVE)

struct coap_reply *coap_response_received(
	const struct coap_packet *response,