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: Gerson Fernando Budke <nandojve@gmail.com>
This commit is contained in:
Gerson Fernando Budke 2020-05-27 23:49:43 -03:00 committed by Jukka Rissanen
commit 76e32ba32b
2 changed files with 21 additions and 15 deletions

View file

@ -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, uint16_t code);
/**
* @brief Retrieves BLOCK{1,2} and SIZE{1,2} from @a cpkt and updates
* @a ctx accordingly.

View file

@ -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, uint16_t code)
int coap_get_option_int(const struct coap_packet *cpkt, uint16_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, uint16_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 = {};
uint16_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,