net: lwm2m: introduce getter/setter for OPAQUE data
Each content formatter should have a way of handling opaque data. For instance TLV data will individually be able to specify a length but plain text will take up the rest of the packet. Signed-off-by: Michael Scott <michael.scott@linaro.org>
This commit is contained in:
parent
1bab82f8f0
commit
dd95dbcd9e
6 changed files with 84 additions and 5 deletions
|
@ -1834,6 +1834,30 @@ static int lwm2m_read_handler(struct lwm2m_engine_obj_inst *obj_inst,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t lwm2m_engine_get_opaque_more(struct lwm2m_input_context *in,
|
||||||
|
u8_t *buf, size_t buflen, bool *last_block)
|
||||||
|
{
|
||||||
|
u16_t in_len = in->opaque_len;
|
||||||
|
|
||||||
|
if (in_len > buflen) {
|
||||||
|
in_len = buflen;
|
||||||
|
}
|
||||||
|
|
||||||
|
in->opaque_len -= in_len;
|
||||||
|
if (in->opaque_len == 0) {
|
||||||
|
*last_block = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
in->frag = net_frag_read(in->frag, in->offset, &in->offset, in_len,
|
||||||
|
buf);
|
||||||
|
if (!in->frag && in->offset == 0xffff) {
|
||||||
|
*last_block = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (size_t)in_len;
|
||||||
|
}
|
||||||
|
|
||||||
/* This function is exposed for the content format writers */
|
/* This function is exposed for the content format writers */
|
||||||
int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
|
int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
|
||||||
struct lwm2m_engine_res_inst *res,
|
struct lwm2m_engine_res_inst *res,
|
||||||
|
|
|
@ -112,6 +112,9 @@ enum coap_block_size lwm2m_default_block_size(void);
|
||||||
|
|
||||||
int lwm2m_engine_add_service(void (*service)(void), u32_t period_ms);
|
int lwm2m_engine_add_service(void (*service)(void), u32_t period_ms);
|
||||||
|
|
||||||
|
size_t lwm2m_engine_get_opaque_more(struct lwm2m_input_context *in,
|
||||||
|
u8_t *buf, size_t buflen, bool *last_block);
|
||||||
|
|
||||||
#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
|
#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
|
||||||
u8_t lwm2m_firmware_get_update_state(void);
|
u8_t lwm2m_firmware_get_update_state(void);
|
||||||
void lwm2m_firmware_set_update_state(u8_t state);
|
void lwm2m_firmware_set_update_state(u8_t state);
|
||||||
|
|
|
@ -229,6 +229,9 @@ struct lwm2m_input_context {
|
||||||
|
|
||||||
/* length of incoming coap/lwm2m payload */
|
/* length of incoming coap/lwm2m payload */
|
||||||
u16_t payload_len;
|
u16_t payload_len;
|
||||||
|
|
||||||
|
/* length of incoming opaque */
|
||||||
|
u16_t opaque_len;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* LWM2M format writer for the various formats supported */
|
/* LWM2M format writer for the various formats supported */
|
||||||
|
@ -265,6 +268,9 @@ struct lwm2m_writer {
|
||||||
size_t (*put_bool)(struct lwm2m_output_context *out,
|
size_t (*put_bool)(struct lwm2m_output_context *out,
|
||||||
struct lwm2m_obj_path *path,
|
struct lwm2m_obj_path *path,
|
||||||
bool value);
|
bool value);
|
||||||
|
size_t (*put_opaque)(struct lwm2m_output_context *out,
|
||||||
|
struct lwm2m_obj_path *path,
|
||||||
|
char *buf, size_t buflen);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct lwm2m_reader {
|
struct lwm2m_reader {
|
||||||
|
@ -280,6 +286,8 @@ struct lwm2m_reader {
|
||||||
float64_value_t *value);
|
float64_value_t *value);
|
||||||
size_t (*get_bool)(struct lwm2m_input_context *in,
|
size_t (*get_bool)(struct lwm2m_input_context *in,
|
||||||
bool *value);
|
bool *value);
|
||||||
|
size_t (*get_opaque)(struct lwm2m_input_context *in,
|
||||||
|
u8_t *buf, size_t buflen, bool *last_block);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* LWM2M engine context */
|
/* LWM2M engine context */
|
||||||
|
@ -388,6 +396,17 @@ static inline size_t engine_put_bool(struct lwm2m_output_context *out,
|
||||||
return out->writer->put_bool(out, path, value);
|
return out->writer->put_bool(out, path, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline size_t engine_put_opaque(struct lwm2m_output_context *out,
|
||||||
|
struct lwm2m_obj_path *path,
|
||||||
|
char *buf, size_t buflen)
|
||||||
|
{
|
||||||
|
if (out->writer->put_opaque) {
|
||||||
|
return out->writer->put_opaque(out, path, buf, buflen);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static inline size_t engine_get_s32(struct lwm2m_input_context *in,
|
static inline size_t engine_get_s32(struct lwm2m_input_context *in,
|
||||||
s32_t *value)
|
s32_t *value)
|
||||||
{
|
{
|
||||||
|
@ -424,4 +443,15 @@ static inline size_t engine_get_bool(struct lwm2m_input_context *in,
|
||||||
return in->reader->get_bool(in, value);
|
return in->reader->get_bool(in, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline size_t engine_get_opaque(struct lwm2m_input_context *in,
|
||||||
|
u8_t *buf, size_t buflen,
|
||||||
|
bool *last_block)
|
||||||
|
{
|
||||||
|
if (in->reader->get_opaque) {
|
||||||
|
return in->reader->get_opaque(in, buf, buflen, last_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* LWM2M_OBJECT_H_ */
|
#endif /* LWM2M_OBJECT_H_ */
|
||||||
|
|
|
@ -462,7 +462,8 @@ const struct lwm2m_writer json_writer = {
|
||||||
put_string,
|
put_string,
|
||||||
put_float32fix,
|
put_float32fix,
|
||||||
put_float64fix,
|
put_float64fix,
|
||||||
put_bool
|
put_bool,
|
||||||
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static int parse_path(const u8_t *buf, u16_t buflen,
|
static int parse_path(const u8_t *buf, u16_t buflen,
|
||||||
|
|
|
@ -699,6 +699,16 @@ static size_t get_bool(struct lwm2m_input_context *in, bool *value)
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t get_opaque(struct lwm2m_input_context *in,
|
||||||
|
u8_t *value, size_t buflen, bool *last_block)
|
||||||
|
{
|
||||||
|
struct oma_tlv tlv;
|
||||||
|
|
||||||
|
oma_tlv_get(&tlv, in, false);
|
||||||
|
in->opaque_len = tlv.length;
|
||||||
|
return lwm2m_engine_get_opaque_more(in, value, buflen, last_block);
|
||||||
|
}
|
||||||
|
|
||||||
const struct lwm2m_writer oma_tlv_writer = {
|
const struct lwm2m_writer oma_tlv_writer = {
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
|
@ -711,7 +721,8 @@ const struct lwm2m_writer oma_tlv_writer = {
|
||||||
put_string,
|
put_string,
|
||||||
put_float32fix,
|
put_float32fix,
|
||||||
put_float64fix,
|
put_float64fix,
|
||||||
put_bool
|
put_bool,
|
||||||
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct lwm2m_reader oma_tlv_reader = {
|
const struct lwm2m_reader oma_tlv_reader = {
|
||||||
|
@ -720,7 +731,8 @@ const struct lwm2m_reader oma_tlv_reader = {
|
||||||
get_string,
|
get_string,
|
||||||
get_float32fix,
|
get_float32fix,
|
||||||
get_float64fix,
|
get_float64fix,
|
||||||
get_bool
|
get_bool,
|
||||||
|
get_opaque
|
||||||
};
|
};
|
||||||
|
|
||||||
static int do_write_op_tlv_item(struct lwm2m_engine_context *context)
|
static int do_write_op_tlv_item(struct lwm2m_engine_context *context)
|
||||||
|
|
|
@ -301,6 +301,13 @@ static size_t get_bool(struct lwm2m_input_context *in,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t get_opaque(struct lwm2m_input_context *in,
|
||||||
|
u8_t *value, size_t buflen, bool *last_block)
|
||||||
|
{
|
||||||
|
in->opaque_len = pkt_length_left(in);
|
||||||
|
return lwm2m_engine_get_opaque_more(in, value, buflen, last_block);
|
||||||
|
}
|
||||||
|
|
||||||
const struct lwm2m_writer plain_text_writer = {
|
const struct lwm2m_writer plain_text_writer = {
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
|
@ -313,7 +320,8 @@ const struct lwm2m_writer plain_text_writer = {
|
||||||
put_string,
|
put_string,
|
||||||
put_float32fix,
|
put_float32fix,
|
||||||
put_float64fix,
|
put_float64fix,
|
||||||
put_bool
|
put_bool,
|
||||||
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct lwm2m_reader plain_text_reader = {
|
const struct lwm2m_reader plain_text_reader = {
|
||||||
|
@ -322,7 +330,8 @@ const struct lwm2m_reader plain_text_reader = {
|
||||||
get_string,
|
get_string,
|
||||||
get_float32fix,
|
get_float32fix,
|
||||||
get_float64fix,
|
get_float64fix,
|
||||||
get_bool
|
get_bool,
|
||||||
|
get_opaque
|
||||||
};
|
};
|
||||||
|
|
||||||
int do_write_op_plain_text(struct lwm2m_engine_obj *obj,
|
int do_write_op_plain_text(struct lwm2m_engine_obj *obj,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue