net: lwm2m: Move utility functions from engine
Some utility functions belong to lwm2m_util.c. Block contexts belong to lwm2m_message_handling.c Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
parent
7e416c8ec0
commit
1dd9d514f1
11 changed files with 90 additions and 77 deletions
|
@ -107,64 +107,13 @@ static struct zsock_pollfd sock_fds[MAX_POLL_FD];
|
|||
static struct lwm2m_ctx *sock_ctx[MAX_POLL_FD];
|
||||
static int sock_nfds;
|
||||
|
||||
static struct lwm2m_block_context block1_contexts[NUM_BLOCK1_CONTEXT];
|
||||
/* Resource wrappers */
|
||||
struct lwm2m_ctx **lwm2m_sock_ctx(void) { return sock_ctx; }
|
||||
|
||||
int lwm2m_sock_nfds(void) { return sock_nfds; }
|
||||
|
||||
struct lwm2m_block_context *lwm2m_block1_context(void) { return block1_contexts; }
|
||||
|
||||
static int lwm2m_socket_update(struct lwm2m_ctx *ctx);
|
||||
|
||||
/* for debugging: to print IP addresses */
|
||||
char *lwm2m_sprint_ip_addr(const struct sockaddr *addr)
|
||||
{
|
||||
static char buf[NET_IPV6_ADDR_LEN];
|
||||
|
||||
if (addr->sa_family == AF_INET6) {
|
||||
return net_addr_ntop(AF_INET6, &net_sin6(addr)->sin6_addr, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
if (addr->sa_family == AF_INET) {
|
||||
return net_addr_ntop(AF_INET, &net_sin(addr)->sin_addr, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
return "::";
|
||||
}
|
||||
|
||||
static uint8_t to_hex_digit(uint8_t digit)
|
||||
{
|
||||
if (digit >= 10U) {
|
||||
return digit - 10U + 'a';
|
||||
}
|
||||
|
||||
return digit + '0';
|
||||
}
|
||||
|
||||
char *sprint_token(const uint8_t *token, uint8_t tkl)
|
||||
{
|
||||
static char buf[32];
|
||||
char *ptr = buf;
|
||||
|
||||
if (token && tkl != 0) {
|
||||
int i;
|
||||
|
||||
tkl = MIN(tkl, sizeof(buf) / 2 - 1);
|
||||
|
||||
for (i = 0; i < tkl; i++) {
|
||||
*ptr++ = to_hex_digit(token[i] >> 4);
|
||||
*ptr++ = to_hex_digit(token[i] & 0x0F);
|
||||
}
|
||||
|
||||
*ptr = '\0';
|
||||
} else {
|
||||
strcpy(buf, "[no-token]");
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* utility functions */
|
||||
|
||||
int lwm2m_open_socket(struct lwm2m_ctx *client_ctx)
|
||||
|
@ -1029,7 +978,7 @@ static int lwm2m_engine_init(void)
|
|||
sys_slist_append(lwm2m_obs_obj_path_list(), &observe_paths[i].node);
|
||||
}
|
||||
|
||||
(void)memset(block1_contexts, 0, sizeof(block1_contexts));
|
||||
lwm2m_clear_block_contexts();
|
||||
|
||||
if (IS_ENABLED(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)) {
|
||||
/* Init data cache */
|
||||
|
|
|
@ -30,23 +30,6 @@
|
|||
/* length of time in milliseconds to wait for buffer allocations */
|
||||
#define BUF_ALLOC_TIMEOUT K_SECONDS(1)
|
||||
|
||||
/**
|
||||
* @brief Used for debugging to print ip addresses.
|
||||
*
|
||||
* @param addr sockaddr for socket using ipv4 or ipv6
|
||||
* @return ip address in readable form
|
||||
*/
|
||||
char *lwm2m_sprint_ip_addr(const struct sockaddr *addr);
|
||||
|
||||
/**
|
||||
* @brief Converts the token to a printable format.
|
||||
*
|
||||
* @param[in] token Token to be printed
|
||||
* @param[in] tkl Lenghts of token
|
||||
* @return char buffer with the string representation of the token
|
||||
*/
|
||||
char *sprint_token(const uint8_t *token, uint8_t tkl);
|
||||
|
||||
/**
|
||||
* @brief Validates that writing is a legal operation on the field given by the object in
|
||||
* @p obj_inst and the resource id in @p msg. Returns the field to obj_field (if it exists).
|
||||
|
|
|
@ -70,13 +70,13 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|||
|
||||
/* Shared set of in-flight LwM2M messages */
|
||||
static struct lwm2m_message messages[CONFIG_LWM2M_ENGINE_MAX_MESSAGES];
|
||||
static struct lwm2m_block_context block1_contexts[NUM_BLOCK1_CONTEXT];
|
||||
|
||||
/* External resources */
|
||||
sys_slist_t *lwm2m_engine_obj_list(void);
|
||||
|
||||
sys_slist_t *lwm2m_engine_obj_inst_list(void);
|
||||
|
||||
struct lwm2m_block_context *lwm2m_block1_context(void);
|
||||
/* block-wise transfer functions */
|
||||
|
||||
enum coap_block_size lwm2m_default_block_size(void)
|
||||
|
@ -100,6 +100,12 @@ enum coap_block_size lwm2m_default_block_size(void)
|
|||
|
||||
return COAP_BLOCK_256;
|
||||
}
|
||||
|
||||
void lwm2m_clear_block_contexts(void)
|
||||
{
|
||||
(void)memset(block1_contexts, 0, sizeof(block1_contexts));
|
||||
}
|
||||
|
||||
static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_context **ctx)
|
||||
{
|
||||
int i;
|
||||
|
@ -108,14 +114,14 @@ static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_
|
|||
*ctx = NULL;
|
||||
timestamp = k_uptime_get();
|
||||
for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
|
||||
if (lwm2m_block1_context()[i].tkl == 0U) {
|
||||
*ctx = &lwm2m_block1_context()[i];
|
||||
if (block1_contexts[i].tkl == 0U) {
|
||||
*ctx = &block1_contexts[i];
|
||||
break;
|
||||
}
|
||||
|
||||
if (timestamp - lwm2m_block1_context()[i].timestamp >
|
||||
if (timestamp - block1_contexts[i].timestamp >
|
||||
TIMEOUT_BLOCKWISE_TRANSFER_MS) {
|
||||
*ctx = &lwm2m_block1_context()[i];
|
||||
*ctx = &block1_contexts[i];
|
||||
/* TODO: notify application for block
|
||||
* transfer timeout
|
||||
*/
|
||||
|
@ -146,9 +152,9 @@ static int get_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_c
|
|||
*ctx = NULL;
|
||||
|
||||
for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
|
||||
if (lwm2m_block1_context()[i].tkl == tkl &&
|
||||
memcmp(token, lwm2m_block1_context()[i].token, tkl) == 0) {
|
||||
*ctx = &lwm2m_block1_context()[i];
|
||||
if (block1_contexts[i].tkl == tkl &&
|
||||
memcmp(token, block1_contexts[i].token, tkl) == 0) {
|
||||
*ctx = &block1_contexts[i];
|
||||
/* refresh timestamp */
|
||||
(*ctx)->timestamp = k_uptime_get();
|
||||
break;
|
||||
|
|
|
@ -73,4 +73,6 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst, struct lwm2m_eng
|
|||
enum coap_block_size lwm2m_default_block_size(void);
|
||||
|
||||
int lwm2m_parse_peerinfo(char *url, struct lwm2m_ctx *client_ctx, bool is_firmware_uri);
|
||||
void lwm2m_clear_block_contexts(void);
|
||||
|
||||
#endif /* LWM2M_MESSAGE_HANDLING_H */
|
||||
|
|
|
@ -61,6 +61,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|||
#include "lwm2m_engine.h"
|
||||
#include "lwm2m_rd_client.h"
|
||||
#include "lwm2m_rw_link_format.h"
|
||||
#include "lwm2m_util.h"
|
||||
|
||||
#define LWM2M_RD_CLIENT_URI "rd"
|
||||
|
||||
|
|
|
@ -528,3 +528,51 @@ bool lwm2m_obj_path_equal(const struct lwm2m_obj_path *a, const struct lwm2m_obj
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* for debugging: to print IP addresses */
|
||||
char *lwm2m_sprint_ip_addr(const struct sockaddr *addr)
|
||||
{
|
||||
static char buf[NET_IPV6_ADDR_LEN];
|
||||
|
||||
if (addr->sa_family == AF_INET6) {
|
||||
return net_addr_ntop(AF_INET6, &net_sin6(addr)->sin6_addr, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
if (addr->sa_family == AF_INET) {
|
||||
return net_addr_ntop(AF_INET, &net_sin(addr)->sin_addr, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
return "::";
|
||||
}
|
||||
|
||||
static uint8_t to_hex_digit(uint8_t digit)
|
||||
{
|
||||
if (digit >= 10U) {
|
||||
return digit - 10U + 'a';
|
||||
}
|
||||
|
||||
return digit + '0';
|
||||
}
|
||||
|
||||
char *sprint_token(const uint8_t *token, uint8_t tkl)
|
||||
{
|
||||
static char buf[32];
|
||||
char *ptr = buf;
|
||||
|
||||
if (token && tkl != 0) {
|
||||
int i;
|
||||
|
||||
tkl = MIN(tkl, sizeof(buf) / 2 - 1);
|
||||
|
||||
for (i = 0; i < tkl; i++) {
|
||||
*ptr++ = to_hex_digit(token[i] >> 4);
|
||||
*ptr++ = to_hex_digit(token[i] & 0x0F);
|
||||
}
|
||||
|
||||
*ptr = '\0';
|
||||
} else {
|
||||
strcpy(buf, "[no-token]");
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -27,4 +27,22 @@ uint16_t lwm2m_atou16(const uint8_t *buf, uint16_t buflen, uint16_t *len);
|
|||
int lwm2m_string_to_path(const char *pathstr, struct lwm2m_obj_path *path, char delim);
|
||||
|
||||
bool lwm2m_obj_path_equal(const struct lwm2m_obj_path *a, const struct lwm2m_obj_path *b);
|
||||
|
||||
/**
|
||||
* @brief Used for debugging to print ip addresses.
|
||||
*
|
||||
* @param addr sockaddr for socket using ipv4 or ipv6
|
||||
* @return ip address in readable form
|
||||
*/
|
||||
char *lwm2m_sprint_ip_addr(const struct sockaddr *addr);
|
||||
|
||||
/**
|
||||
* @brief Converts the token to a printable format.
|
||||
*
|
||||
* @param[in] token Token to be printed
|
||||
* @param[in] tkl Lenghts of token
|
||||
* @return char buffer with the string representation of the token
|
||||
*/
|
||||
char *sprint_token(const uint8_t *token, uint8_t tkl);
|
||||
|
||||
#endif /* LWM2M_UTIL_H_ */
|
||||
|
|
|
@ -39,6 +39,7 @@ DEFINE_FAKE_VALUE_FUNC(struct lwm2m_engine_obj_field *, lwm2m_get_engine_obj_fie
|
|||
DEFINE_FAKE_VALUE_FUNC(int, lwm2m_get_bool, const struct lwm2m_obj_path *, bool *);
|
||||
DEFINE_FAKE_VALUE_FUNC(sys_slist_t *, lwm2m_engine_obj_inst_list);
|
||||
DEFINE_FAKE_VALUE_FUNC(int, lwm2m_delete_obj_inst, uint16_t, uint16_t);
|
||||
DEFINE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
|
||||
|
||||
static sys_slist_t obs_obj_path_list;
|
||||
sys_slist_t *lwm2m_obs_obj_path_list(void)
|
||||
|
|
|
@ -53,6 +53,7 @@ DECLARE_FAKE_VALUE_FUNC(struct lwm2m_engine_obj_field *, lwm2m_get_engine_obj_fi
|
|||
DECLARE_FAKE_VALUE_FUNC(int, lwm2m_get_bool, const struct lwm2m_obj_path *, bool *);
|
||||
DECLARE_FAKE_VALUE_FUNC(sys_slist_t *, lwm2m_engine_obj_inst_list);
|
||||
DECLARE_FAKE_VALUE_FUNC(int, lwm2m_delete_obj_inst, uint16_t, uint16_t);
|
||||
DECLARE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
|
||||
|
||||
#define DO_FOREACH_FAKE(FUNC) \
|
||||
do { \
|
||||
|
@ -79,6 +80,7 @@ DECLARE_FAKE_VALUE_FUNC(int, lwm2m_delete_obj_inst, uint16_t, uint16_t);
|
|||
FUNC(lwm2m_get_bool) \
|
||||
FUNC(lwm2m_engine_obj_inst_list) \
|
||||
FUNC(lwm2m_delete_obj_inst) \
|
||||
FUNC(lwm2m_clear_block_contexts) \
|
||||
} while (0)
|
||||
|
||||
#endif /* STUBS_H */
|
||||
|
|
|
@ -138,6 +138,7 @@ void test_lwm2m_engine_stop_service(void)
|
|||
|
||||
/* subsys/net/lib/lwm2m/lwm2m_message_handling.h */
|
||||
DEFINE_FAKE_VALUE_FUNC(int, lwm2m_init_message, struct lwm2m_message *);
|
||||
DEFINE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
|
||||
int lwm2m_init_message_fake_default(struct lwm2m_message *msg)
|
||||
{
|
||||
pending_message = msg;
|
||||
|
|
|
@ -68,6 +68,7 @@ void test_lwm2m_engine_stop_service(void);
|
|||
|
||||
/* subsys/net/lib/lwm2m/lwm2m_message_handling.h */
|
||||
DECLARE_FAKE_VALUE_FUNC(int, lwm2m_init_message, struct lwm2m_message *);
|
||||
DECLARE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
|
||||
int lwm2m_init_message_fake_default(struct lwm2m_message *msg);
|
||||
void test_prepare_pending_message_cb(void *cb);
|
||||
|
||||
|
@ -112,6 +113,7 @@ DECLARE_FAKE_VALUE_FUNC(int, do_register_op_link_format, struct lwm2m_message *)
|
|||
FUNC(lwm2m_engine_get_binding) \
|
||||
FUNC(lwm2m_engine_get_queue_mode) \
|
||||
FUNC(do_register_op_link_format) \
|
||||
FUNC(lwm2m_clear_block_contexts) \
|
||||
} while (0)
|
||||
|
||||
#endif /* STUBS_H */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue