2018-07-15 03:58:59 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2019-05-16 03:00:18 +08:00
|
|
|
* Copyright (C) 2018-2019 Intel Corporation. All rights reserved.
|
2018-07-15 03:58:59 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "mesh/mesh-defs.h"
|
|
|
|
|
|
|
|
#include "mesh/mesh.h"
|
|
|
|
#include "mesh/crypto.h"
|
|
|
|
#include "mesh/node.h"
|
2019-07-15 07:23:12 +08:00
|
|
|
#include "mesh/mesh-config.h"
|
2018-07-15 03:58:59 +08:00
|
|
|
#include "mesh/net.h"
|
|
|
|
#include "mesh/appkey.h"
|
|
|
|
#include "mesh/cfgmod.h"
|
2018-12-15 06:05:36 +08:00
|
|
|
#include "mesh/error.h"
|
|
|
|
#include "mesh/dbus.h"
|
|
|
|
#include "mesh/util.h"
|
|
|
|
#include "mesh/model.h"
|
2019-07-04 20:33:37 +08:00
|
|
|
#include "mesh/keyring.h"
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-05-24 22:48:10 +08:00
|
|
|
/* Divide and round to ceiling (up) to calculate segment count */
|
|
|
|
#define CEILDIV(val, div) (((val) + (div) - 1) / (div))
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
#define VIRTUAL_BASE 0x10000
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model {
|
|
|
|
const struct mesh_model_ops *cbs;
|
|
|
|
void *user_data;
|
|
|
|
struct l_queue *bindings;
|
|
|
|
struct l_queue *subs;
|
|
|
|
struct l_queue *virtuals;
|
|
|
|
struct mesh_model_pub *pub;
|
|
|
|
uint32_t id;
|
|
|
|
uint8_t ele_idx;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mesh_virtual {
|
|
|
|
uint16_t ref_cnt;
|
2019-06-30 14:43:54 +08:00
|
|
|
uint16_t addr; /* 16-bit virtual address, used in messages */
|
|
|
|
uint8_t label[16]; /* 128 bit label UUID */
|
2018-07-15 03:58:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* These struct is used to pass lots of params to l_queue_foreach */
|
|
|
|
struct mod_forward {
|
|
|
|
struct mesh_virtual *virt;
|
|
|
|
const uint8_t *data;
|
|
|
|
uint16_t src;
|
|
|
|
uint16_t dst;
|
|
|
|
uint16_t unicast;
|
2019-07-17 06:56:44 +08:00
|
|
|
uint16_t app_idx;
|
|
|
|
uint16_t net_idx;
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t size;
|
|
|
|
uint8_t ttl;
|
|
|
|
int8_t rssi;
|
|
|
|
bool szmict;
|
2018-12-15 06:05:36 +08:00
|
|
|
bool has_dst;
|
2018-07-15 03:58:59 +08:00
|
|
|
bool done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct l_queue *mesh_virtuals;
|
|
|
|
|
|
|
|
static struct timeval tx_start;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static bool is_internal(uint32_t id)
|
|
|
|
{
|
|
|
|
if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
static void unref_virt(void *data)
|
|
|
|
{
|
|
|
|
struct mesh_virtual *virt = data;
|
|
|
|
|
|
|
|
if (virt->ref_cnt > 0)
|
|
|
|
virt->ref_cnt--;
|
|
|
|
|
|
|
|
if (virt->ref_cnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_queue_remove(mesh_virtuals, virt);
|
|
|
|
l_free(virt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool simple_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static bool has_binding(struct l_queue *bindings, uint16_t idx)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *l;
|
|
|
|
|
|
|
|
for (l = l_queue_get_entries(bindings); l; l = l->next) {
|
|
|
|
if (L_PTR_TO_UINT(l->data) == idx)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:43:54 +08:00
|
|
|
static bool find_virt_by_label(const void *a, const void *b)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
|
|
|
const struct mesh_virtual *virt = a;
|
2019-06-30 14:43:54 +08:00
|
|
|
const uint8_t *label = b;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-06-30 14:43:54 +08:00
|
|
|
return memcmp(virt->label, label, 16) == 0;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static bool match_model_id(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct mesh_model *model = a;
|
|
|
|
uint32_t id = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return (mesh_model_get_model_id(model) == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mesh_model *get_model(struct mesh_node *node, uint8_t ele_idx,
|
|
|
|
uint32_t id, int *status)
|
|
|
|
{
|
|
|
|
struct l_queue *models;
|
|
|
|
struct mesh_model *model;
|
|
|
|
|
|
|
|
models = node_get_element_models(node, ele_idx, status);
|
|
|
|
if (!models) {
|
2019-06-30 14:43:53 +08:00
|
|
|
*status = MESH_STATUS_INVALID_MODEL;
|
2018-12-15 06:05:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
model = l_queue_find(models, match_model_id, L_UINT_TO_PTR(id));
|
|
|
|
|
2019-06-06 09:18:32 +08:00
|
|
|
*status = (model) ? MESH_STATUS_SUCCESS : MESH_STATUS_INVALID_MODEL;
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mesh_model *find_model(struct mesh_node *node, uint16_t addr,
|
2019-06-30 14:43:53 +08:00
|
|
|
uint32_t mod_id, int *status)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
|
|
|
int ele_idx;
|
|
|
|
|
|
|
|
ele_idx = node_get_element_idx(node, addr);
|
|
|
|
|
|
|
|
if (ele_idx < 0) {
|
2019-06-30 14:43:53 +08:00
|
|
|
*status = MESH_STATUS_INVALID_ADDRESS;
|
2018-07-15 03:58:59 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
return get_model(node, (uint8_t) ele_idx, mod_id, status);
|
2018-12-15 06:05:36 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 06:58:20 +08:00
|
|
|
static uint32_t pub_period_to_ms(uint8_t pub_period)
|
2018-12-15 06:05:36 +08:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2019-03-09 06:58:20 +08:00
|
|
|
n = pub_period >> 2;
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2019-03-09 06:58:20 +08:00
|
|
|
switch (pub_period & 0x3) {
|
2018-12-15 06:05:36 +08:00
|
|
|
default:
|
|
|
|
return n * 100;
|
|
|
|
case 2:
|
|
|
|
n *= 10;
|
|
|
|
/* Fall Through */
|
|
|
|
case 1:
|
|
|
|
return n * 1000;
|
|
|
|
case 3:
|
|
|
|
return n * 10 * 60 * 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct l_dbus_message *create_config_update_msg(struct mesh_node *node,
|
|
|
|
uint8_t ele_idx, uint32_t id,
|
|
|
|
struct l_dbus_message_builder **builder)
|
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
struct l_dbus_message *msg;
|
|
|
|
const char *owner;
|
|
|
|
const char *path;
|
|
|
|
uint16_t model_id;
|
|
|
|
|
|
|
|
owner = node_get_owner(node);
|
|
|
|
path = node_get_element_path(node, ele_idx);
|
|
|
|
if (!path || !owner)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
l_debug("Send \"UpdateModelConfiguration\"");
|
|
|
|
msg = l_dbus_message_new_method_call(dbus, owner, path,
|
|
|
|
MESH_ELEMENT_INTERFACE,
|
|
|
|
"UpdateModelConfiguration");
|
|
|
|
|
|
|
|
*builder = l_dbus_message_builder_new(msg);
|
|
|
|
|
|
|
|
model_id = (uint16_t) id;
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(*builder, 'q', &model_id);
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_array(*builder, "{sv}");
|
|
|
|
|
|
|
|
if ((id & VENDOR_ID_MASK) != VENDOR_ID_MASK) {
|
|
|
|
uint16_t vendor = id >> 16;
|
|
|
|
dbus_append_dict_entry_basic(*builder, "Vendor", "q", &vendor);
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_update_model_pub_period(struct mesh_node *node,
|
|
|
|
uint8_t ele_idx, uint32_t model_id,
|
|
|
|
uint32_t period)
|
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
struct l_dbus_message *msg;
|
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
|
|
|
|
msg = create_config_update_msg(node, ele_idx, model_id, &builder);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
|
|
|
|
&period);
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
2019-07-04 15:40:40 +08:00
|
|
|
l_dbus_message_builder_finalize(builder);
|
2018-12-15 06:05:36 +08:00
|
|
|
l_dbus_message_builder_destroy(builder);
|
2019-07-04 15:40:40 +08:00
|
|
|
l_dbus_send(dbus, msg);
|
2018-12-15 06:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void append_dict_uint16_array(struct l_dbus_message_builder *builder,
|
|
|
|
struct l_queue *q, const char *key)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_dict(builder, "sv");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', key);
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "aq");
|
|
|
|
l_dbus_message_builder_enter_array(builder, "q");
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(q); entry; entry = entry->next) {
|
|
|
|
uint16_t value = (uint16_t) L_PTR_TO_UINT(entry->data);
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder,'q', &value);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
l_dbus_message_builder_leave_dict(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_update_model_bindings(struct mesh_node *node,
|
|
|
|
struct mesh_model *mod)
|
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
struct l_dbus_message *msg;
|
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
|
|
|
|
msg = create_config_update_msg(node, mod->ele_idx, mod->id,
|
|
|
|
&builder);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
append_dict_uint16_array(builder, mod->bindings, "Bindings");
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
2019-07-04 15:40:40 +08:00
|
|
|
l_dbus_message_builder_finalize(builder);
|
2018-12-15 06:05:36 +08:00
|
|
|
l_dbus_message_builder_destroy(builder);
|
2019-07-04 15:40:40 +08:00
|
|
|
l_dbus_send(dbus, msg);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2019-11-26 22:54:01 +08:00
|
|
|
static void append_dict_subs_array(struct l_dbus_message_builder *builder,
|
|
|
|
struct l_queue *subs,
|
|
|
|
struct l_queue *virts,
|
|
|
|
const char *key)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_dict(builder, "sv");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', key);
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "av");
|
|
|
|
l_dbus_message_builder_enter_array(builder, "v");
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (l_queue_isempty(subs))
|
2019-11-26 22:54:01 +08:00
|
|
|
goto virts;
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(subs); entry; entry = entry->next) {
|
|
|
|
uint16_t grp = L_PTR_TO_UINT(entry->data);
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "q");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &grp);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
virts:
|
2020-02-26 02:44:16 +08:00
|
|
|
if (l_queue_isempty(virts))
|
2019-11-26 22:54:01 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(virts); entry; entry = entry->next) {
|
|
|
|
const struct mesh_virtual *virt = entry->data;
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "ay");
|
|
|
|
dbus_append_byte_array(builder, virt->label,
|
|
|
|
sizeof(virt->label));
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
l_dbus_message_builder_leave_dict(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_update_model_subscriptions(struct mesh_node *node,
|
|
|
|
struct mesh_model *mod)
|
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
struct l_dbus_message *msg;
|
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
|
|
|
|
msg = create_config_update_msg(node, mod->ele_idx, mod->id,
|
|
|
|
&builder);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
append_dict_subs_array(builder, mod->subs, mod->virtuals,
|
|
|
|
"Subscriptions");
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
l_dbus_message_builder_finalize(builder);
|
|
|
|
l_dbus_message_builder_destroy(builder);
|
|
|
|
l_dbus_send(dbus, msg);
|
|
|
|
}
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
static void forward_model(void *a, void *b)
|
|
|
|
{
|
|
|
|
struct mesh_model *mod = a;
|
|
|
|
struct mod_forward *fwd = b;
|
|
|
|
struct mesh_virtual *virt;
|
2020-02-26 02:44:16 +08:00
|
|
|
uint16_t dst;
|
2018-12-15 06:05:36 +08:00
|
|
|
bool result;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
l_debug("model %8.8x with idx %3.3x", mod->id, fwd->app_idx);
|
2019-07-04 20:33:36 +08:00
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
if (fwd->app_idx != APP_IDX_DEV_LOCAL &&
|
|
|
|
fwd->app_idx != APP_IDX_DEV_REMOTE &&
|
|
|
|
!has_binding(mod->bindings, fwd->app_idx))
|
2018-07-15 03:58:59 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
dst = fwd->dst;
|
2019-09-07 14:07:59 +08:00
|
|
|
if (dst == fwd->unicast || IS_FIXED_GROUP_ADDRESS(dst))
|
2018-12-15 06:05:36 +08:00
|
|
|
fwd->has_dst = true;
|
2018-07-15 03:58:59 +08:00
|
|
|
else if (fwd->virt) {
|
|
|
|
virt = l_queue_find(mod->virtuals, simple_match, fwd->virt);
|
|
|
|
if (virt) {
|
2018-12-15 06:05:36 +08:00
|
|
|
fwd->has_dst = true;
|
2020-02-26 02:44:16 +08:00
|
|
|
dst = virt->addr;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(dst)))
|
2018-12-15 06:05:36 +08:00
|
|
|
fwd->has_dst = true;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!fwd->has_dst)
|
|
|
|
return;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
/* Return, if this is not a internal model */
|
|
|
|
if (!mod->cbs)
|
2018-07-15 03:58:59 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
result = false;
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
if (mod->cbs->recv)
|
2019-07-17 06:56:44 +08:00
|
|
|
result = mod->cbs->recv(fwd->src, dst, fwd->unicast,
|
|
|
|
fwd->app_idx, fwd->net_idx,
|
2018-07-15 03:58:59 +08:00
|
|
|
fwd->data, fwd->size, fwd->ttl, mod->user_data);
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (dst == fwd->unicast && result)
|
2018-07-15 03:58:59 +08:00
|
|
|
fwd->done = true;
|
|
|
|
}
|
|
|
|
|
2019-10-04 04:07:47 +08:00
|
|
|
static int app_packet_decrypt(struct mesh_net *net, const uint8_t *data,
|
|
|
|
uint16_t size, bool szmict, uint16_t src,
|
|
|
|
uint16_t dst, uint8_t *virt, uint16_t virt_size,
|
|
|
|
uint8_t key_aid, uint32_t seq,
|
|
|
|
uint32_t iv_idx, uint8_t *out)
|
|
|
|
{
|
|
|
|
struct l_queue *app_keys = mesh_net_get_app_keys(net);
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
if (!app_keys)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(app_keys); entry;
|
|
|
|
entry = entry->next) {
|
|
|
|
const uint8_t *old_key = NULL, *new_key = NULL;
|
|
|
|
uint8_t old_key_aid, new_key_aid;
|
|
|
|
int app_idx;
|
|
|
|
bool decrypted;
|
|
|
|
|
|
|
|
app_idx = appkey_get_key_idx(entry->data,
|
|
|
|
&old_key, &old_key_aid,
|
|
|
|
&new_key, &new_key_aid);
|
|
|
|
|
|
|
|
if (app_idx < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (old_key && old_key_aid == key_aid) {
|
2020-02-19 03:30:10 +08:00
|
|
|
decrypted = mesh_crypto_payload_decrypt(virt, virt_size,
|
|
|
|
data, size, szmict, src, dst, key_aid,
|
2019-10-04 04:07:47 +08:00
|
|
|
seq, iv_idx, out, old_key);
|
|
|
|
|
|
|
|
if (decrypted) {
|
|
|
|
print_packet("Used App Key", old_key, 16);
|
|
|
|
return app_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_packet("Failed App Key", old_key, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_key && new_key_aid == key_aid) {
|
2020-02-19 03:30:10 +08:00
|
|
|
decrypted = mesh_crypto_payload_decrypt(virt, virt_size,
|
|
|
|
data, size, szmict, src, dst, key_aid,
|
2019-10-04 04:07:47 +08:00
|
|
|
seq, iv_idx, out, new_key);
|
|
|
|
|
|
|
|
if (decrypted) {
|
|
|
|
print_packet("Used App Key", new_key, 16);
|
|
|
|
return app_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_packet("Failed App Key", new_key, 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static int dev_packet_decrypt(struct mesh_node *node, const uint8_t *data,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t size, bool szmict, uint16_t src,
|
2019-10-04 04:07:46 +08:00
|
|
|
uint16_t dst, uint8_t key_aid, uint32_t seq,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint32_t iv_idx, uint8_t *out)
|
|
|
|
{
|
2019-07-04 20:33:38 +08:00
|
|
|
uint8_t dev_key[16];
|
2019-07-04 20:33:36 +08:00
|
|
|
const uint8_t *key;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-04 20:33:36 +08:00
|
|
|
key = node_get_device_key(node);
|
|
|
|
if (!key)
|
2019-07-04 20:33:38 +08:00
|
|
|
return -1;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
|
2019-10-04 04:07:46 +08:00
|
|
|
dst, key_aid, seq, iv_idx, out, key))
|
2019-07-04 20:33:36 +08:00
|
|
|
return APP_IDX_DEV_LOCAL;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-04 20:33:38 +08:00
|
|
|
if (!keyring_get_remote_dev_key(node, src, dev_key))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
key = dev_key;
|
|
|
|
if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
|
2019-10-04 04:07:46 +08:00
|
|
|
dst, key_aid, seq, iv_idx, out, key))
|
2019-07-04 20:33:38 +08:00
|
|
|
return APP_IDX_DEV_REMOTE;
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virt_packet_decrypt(struct mesh_net *net, const uint8_t *data,
|
|
|
|
uint16_t size, bool szmict, uint16_t src,
|
2019-10-04 04:07:46 +08:00
|
|
|
uint16_t dst, uint8_t key_aid, uint32_t seq,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint32_t iv_idx, uint8_t *out,
|
|
|
|
struct mesh_virtual **decrypt_virt)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *v;
|
|
|
|
|
|
|
|
for (v = l_queue_get_entries(mesh_virtuals); v; v = v->next) {
|
|
|
|
struct mesh_virtual *virt = v->data;
|
|
|
|
int decrypt_idx;
|
|
|
|
|
2019-06-30 14:43:54 +08:00
|
|
|
if (virt->addr != dst)
|
2018-07-15 03:58:59 +08:00
|
|
|
continue;
|
|
|
|
|
2019-10-04 04:07:47 +08:00
|
|
|
decrypt_idx = app_packet_decrypt(net, data, size, szmict, src,
|
|
|
|
dst, virt->label, 16,
|
|
|
|
key_aid, seq, iv_idx,
|
|
|
|
out);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
if (decrypt_idx >= 0) {
|
|
|
|
*decrypt_virt = virt;
|
|
|
|
return decrypt_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmplt(uint16_t remote, uint8_t status,
|
|
|
|
void *data, uint16_t size,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct timeval tx_end;
|
|
|
|
|
|
|
|
if (status)
|
2018-12-15 06:05:36 +08:00
|
|
|
l_debug("Tx-->%4.4x (%d octets) Failed (%d)",
|
2018-07-15 03:58:59 +08:00
|
|
|
remote, size, status);
|
|
|
|
else
|
2018-12-15 06:05:36 +08:00
|
|
|
l_debug("Tx-->%4.4x (%d octets) Succeeded", remote, size);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
/* print_packet("Sent Data", data, size); */
|
|
|
|
|
|
|
|
gettimeofday(&tx_end, NULL);
|
|
|
|
if (tx_end.tv_sec == tx_start.tv_sec) {
|
2019-02-28 02:12:55 +08:00
|
|
|
l_debug("Duration 0.%6.6lu seconds",
|
2018-07-15 03:58:59 +08:00
|
|
|
tx_end.tv_usec - tx_start.tv_usec);
|
|
|
|
} else {
|
|
|
|
if (tx_start.tv_usec > tx_end.tv_usec)
|
2019-02-28 02:12:55 +08:00
|
|
|
l_debug("Duration %lu.%6.6lu seconds",
|
2018-07-15 03:58:59 +08:00
|
|
|
tx_end.tv_sec - tx_start.tv_sec - 1,
|
|
|
|
tx_end.tv_usec + 1000000 - tx_start.tv_usec);
|
|
|
|
else
|
2019-02-28 02:12:55 +08:00
|
|
|
l_debug("Duration %lu.%6.6lu seconds",
|
2018-07-15 03:58:59 +08:00
|
|
|
tx_end.tv_sec - tx_start.tv_sec,
|
|
|
|
tx_end.tv_usec - tx_start.tv_usec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static bool msg_send(struct mesh_node *node, bool credential, uint16_t src,
|
2019-07-17 06:56:44 +08:00
|
|
|
uint32_t dst, uint16_t app_idx, uint16_t net_idx,
|
|
|
|
uint8_t *label, uint8_t ttl,
|
|
|
|
const void *msg, uint16_t msg_len)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-07-17 06:56:44 +08:00
|
|
|
uint8_t dev_key[16];
|
2018-07-15 03:58:59 +08:00
|
|
|
uint32_t iv_index, seq_num;
|
2019-07-17 06:56:44 +08:00
|
|
|
const uint8_t *key;
|
2018-07-15 03:58:59 +08:00
|
|
|
uint8_t *out;
|
2019-07-17 06:56:44 +08:00
|
|
|
uint8_t key_aid = APP_AID_DEV;
|
2018-07-15 03:58:59 +08:00
|
|
|
bool szmic = false;
|
2019-07-17 06:56:44 +08:00
|
|
|
bool ret = false;
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t out_len = msg_len + sizeof(uint32_t);
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_net *net = node_get_net(node);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
/* Use large MIC if it doesn't affect segmentation */
|
|
|
|
if (msg_len > 11 && msg_len <= 376) {
|
2019-05-24 22:48:10 +08:00
|
|
|
if (CEILDIV(out_len, 12) == CEILDIV(out_len + 4, 12)) {
|
2018-07-15 03:58:59 +08:00
|
|
|
szmic = true;
|
|
|
|
out_len = msg_len + sizeof(uint64_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
if (app_idx == APP_IDX_DEV_LOCAL) {
|
|
|
|
key = node_get_device_key(node);
|
|
|
|
if (!key)
|
|
|
|
return false;
|
|
|
|
} else if (app_idx == APP_IDX_DEV_REMOTE) {
|
|
|
|
if (!keyring_get_remote_dev_key(node, dst, dev_key))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
key = dev_key;
|
|
|
|
} else {
|
|
|
|
key = appkey_get_key(node_get_net(node), app_idx, &key_aid);
|
|
|
|
if (!key) {
|
|
|
|
l_debug("no app key for (%x)", app_idx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_idx = appkey_net_idx(node_get_net(node), app_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_debug("(%x) %p", app_idx, key);
|
|
|
|
l_debug("net_idx %x", net_idx);
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
out = l_malloc(out_len);
|
|
|
|
|
|
|
|
iv_index = mesh_net_get_iv_index(net);
|
|
|
|
|
2020-01-31 02:59:22 +08:00
|
|
|
seq_num = mesh_net_next_seq_num(net);
|
2019-06-30 14:43:54 +08:00
|
|
|
if (!mesh_crypto_payload_encrypt(label, msg, out, msg_len, src, dst,
|
2019-07-17 06:56:44 +08:00
|
|
|
key_aid, seq_num, iv_index, szmic, key)) {
|
2018-07-15 03:58:59 +08:00
|
|
|
l_error("Failed to Encrypt Payload");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print_packet("Encrypted with", key, 16); */
|
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
ret = mesh_net_app_send(net, credential, src, dst, key_aid, net_idx,
|
|
|
|
ttl, seq_num, iv_index, szmic, out,
|
|
|
|
out_len, cmplt, NULL);
|
2018-07-15 03:58:59 +08:00
|
|
|
done:
|
|
|
|
l_free(out);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static void remove_pub(struct mesh_node *node, struct mesh_model *mod)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2020-02-26 02:44:16 +08:00
|
|
|
if (mod->pub) {
|
|
|
|
if (mod->pub->virt)
|
|
|
|
unref_virt(mod->pub->virt);
|
|
|
|
|
|
|
|
l_free(mod->pub);
|
|
|
|
mod->pub = NULL;
|
|
|
|
}
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!mod->cbs)
|
|
|
|
/* External models */
|
|
|
|
config_update_model_pub_period(node, mod->ele_idx, mod->id, 0);
|
|
|
|
else if (mod->cbs && mod->cbs->pub)
|
|
|
|
/* Internal models */
|
|
|
|
mod->cbs->pub(NULL);
|
|
|
|
}
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static void model_unbind_idx(struct mesh_node *node, struct mesh_model *mod,
|
|
|
|
uint16_t idx)
|
|
|
|
{
|
|
|
|
l_queue_remove(mod->bindings, L_UINT_TO_PTR(idx));
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!mod->cbs)
|
|
|
|
/* External model */
|
|
|
|
config_update_model_bindings(node, mod);
|
|
|
|
else if (mod->cbs->bind)
|
|
|
|
/* Internal model */
|
2018-07-15 03:58:59 +08:00
|
|
|
mod->cbs->bind(idx, ACTION_DELETE);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
/* Remove model publication if the publication key is unbound */
|
2020-02-26 02:44:16 +08:00
|
|
|
if (mod->pub && idx == mod->pub->idx)
|
|
|
|
remove_pub(node, mod);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static void model_bind_idx(struct mesh_node *node, struct mesh_model *mod,
|
|
|
|
uint16_t idx)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-03-22 10:26:10 +08:00
|
|
|
if (!mod->bindings)
|
|
|
|
mod->bindings = l_queue_new();
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
l_queue_push_tail(mod->bindings, L_UINT_TO_PTR(idx));
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
l_debug("Add %4.4x to model %8.8x", idx, mod->id);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!mod->cbs)
|
|
|
|
/* External model */
|
|
|
|
config_update_model_bindings(node, mod);
|
|
|
|
else if (mod->cbs->bind)
|
|
|
|
/* Internal model */
|
2018-07-15 03:58:59 +08:00
|
|
|
mod->cbs->bind(idx, ACTION_ADD);
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2019-06-30 14:43:53 +08:00
|
|
|
uint16_t app_idx, bool unbind)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model *mod;
|
2019-07-15 07:23:17 +08:00
|
|
|
bool is_present, is_vendor;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod) {
|
2018-12-15 06:05:36 +08:00
|
|
|
l_debug("Model not found");
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2019-07-15 07:23:17 +08:00
|
|
|
is_vendor = id < VENDOR_ID_MASK && id > 0xffff;
|
|
|
|
id = !is_vendor ? (id & 0xffff) : id;
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
|
|
|
|
return MESH_STATUS_INVALID_MODEL;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!appkey_have_key(node_get_net(node), app_idx))
|
2018-07-15 03:58:59 +08:00
|
|
|
return MESH_STATUS_INVALID_APPKEY;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
is_present = has_binding(mod->bindings, app_idx);
|
|
|
|
|
|
|
|
if (!is_present && unbind)
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
if (is_present && !unbind)
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
if (unbind) {
|
2018-12-15 06:05:36 +08:00
|
|
|
model_unbind_idx(node, mod, app_idx);
|
2019-07-15 07:23:17 +08:00
|
|
|
if (!mesh_config_model_binding_del(node_config_get(node),
|
2019-10-02 00:27:28 +08:00
|
|
|
addr, is_vendor, id, app_idx))
|
2018-07-15 03:58:59 +08:00
|
|
|
return MESH_STATUS_STORAGE_FAIL;
|
|
|
|
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (l_queue_length(mod->bindings) >= MAX_BINDINGS)
|
|
|
|
return MESH_STATUS_INSUFF_RESOURCES;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-15 07:23:17 +08:00
|
|
|
if (!mesh_config_model_binding_add(node_config_get(node),
|
2019-10-02 00:27:28 +08:00
|
|
|
addr, is_vendor, id, app_idx))
|
2018-07-15 03:58:59 +08:00
|
|
|
return MESH_STATUS_STORAGE_FAIL;
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
model_bind_idx(node, mod, app_idx);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:43:54 +08:00
|
|
|
static struct mesh_virtual *add_virtual(const uint8_t *v)
|
|
|
|
{
|
|
|
|
struct mesh_virtual *virt = l_queue_find(mesh_virtuals,
|
|
|
|
find_virt_by_label, v);
|
|
|
|
|
|
|
|
if (virt) {
|
|
|
|
virt->ref_cnt++;
|
|
|
|
return virt;
|
|
|
|
}
|
|
|
|
|
|
|
|
virt = l_new(struct mesh_virtual, 1);
|
|
|
|
|
|
|
|
if (!mesh_crypto_virtual_addr(v, &virt->addr)) {
|
|
|
|
l_free(virt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(virt->label, v, 16);
|
|
|
|
virt->ref_cnt = 1;
|
|
|
|
l_queue_push_head(mesh_virtuals, virt);
|
|
|
|
|
|
|
|
return virt;
|
|
|
|
}
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
static int set_pub(struct mesh_model *mod, uint16_t pub_addr,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t idx, bool cred_flag, uint8_t ttl,
|
2020-02-26 02:44:16 +08:00
|
|
|
uint8_t period, uint8_t retransmit)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2020-02-26 02:44:16 +08:00
|
|
|
if (!mod->pub)
|
|
|
|
mod->pub = l_new(struct mesh_model_pub, 1);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
mod->pub->addr = pub_addr;
|
|
|
|
mod->pub->credential = cred_flag;
|
|
|
|
mod->pub->idx = idx;
|
|
|
|
mod->pub->ttl = ttl;
|
|
|
|
mod->pub->period = period;
|
|
|
|
mod->pub->retransmit = retransmit;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
2019-06-30 14:43:54 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
static int set_virt_pub(struct mesh_model *mod, const uint8_t *label,
|
|
|
|
uint16_t idx, bool cred_flag, uint8_t ttl,
|
|
|
|
uint8_t period, uint8_t retransmit)
|
|
|
|
{
|
|
|
|
struct mesh_virtual *virt = NULL;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
virt = add_virtual(label);
|
|
|
|
if (!virt)
|
|
|
|
return MESH_STATUS_STORAGE_FAIL;
|
2019-06-30 14:43:54 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (!mod->pub)
|
|
|
|
mod->pub = l_new(struct mesh_model_pub, 1);
|
|
|
|
|
|
|
|
mod->pub->virt = virt;
|
|
|
|
return set_pub(mod, virt->addr, idx, cred_flag, ttl, period,
|
|
|
|
retransmit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_virt_sub(struct mesh_net *net, struct mesh_model *mod,
|
|
|
|
const uint8_t *label, uint16_t *dst)
|
|
|
|
{
|
|
|
|
struct mesh_virtual *virt = l_queue_find(mod->virtuals,
|
|
|
|
find_virt_by_label, label);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (!virt) {
|
|
|
|
virt = add_virtual(label);
|
|
|
|
if (!virt)
|
|
|
|
return MESH_STATUS_STORAGE_FAIL;
|
2019-06-06 09:18:32 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
l_queue_push_head(mod->virtuals, virt);
|
2020-02-26 02:44:16 +08:00
|
|
|
mesh_net_dst_reg(net, virt->addr);
|
|
|
|
l_debug("Added virtual sub addr %4.4x", virt->addr);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dst)
|
2020-02-26 02:44:16 +08:00
|
|
|
*dst = virt->addr;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_sub(struct mesh_net *net, struct mesh_model *mod,
|
|
|
|
const uint8_t *group, bool b_virt, uint16_t *dst)
|
|
|
|
{
|
|
|
|
uint16_t grp;
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (b_virt)
|
|
|
|
return add_virt_sub(net, mod, group, dst);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
grp = l_get_le16(group);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (dst)
|
|
|
|
*dst = grp;
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (!l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(grp))) {
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (!mod->subs)
|
|
|
|
mod->subs = l_queue_new();
|
2019-06-30 14:43:54 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
l_queue_push_tail(mod->subs, L_UINT_TO_PTR(grp));
|
|
|
|
mesh_net_dst_reg(net, grp);
|
|
|
|
l_debug("Added group subscription %4.4x", grp);
|
2019-06-30 14:43:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-07-04 20:33:38 +08:00
|
|
|
static void send_dev_key_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
|
2019-09-27 02:14:43 +08:00
|
|
|
uint16_t src, uint16_t app_idx,
|
|
|
|
uint16_t net_idx, uint16_t size,
|
|
|
|
const uint8_t *data)
|
2019-07-04 20:33:38 +08:00
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
struct l_dbus_message *msg;
|
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
const char *owner;
|
|
|
|
const char *path;
|
2019-09-27 02:14:43 +08:00
|
|
|
bool remote = (app_idx != APP_IDX_DEV_LOCAL);
|
2019-07-04 20:33:38 +08:00
|
|
|
|
|
|
|
owner = node_get_owner(node);
|
|
|
|
path = node_get_element_path(node, ele_idx);
|
|
|
|
if (!path || !owner)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_debug("Send \"DevKeyMessageReceived\"");
|
|
|
|
|
|
|
|
msg = l_dbus_message_new_method_call(dbus, owner, path,
|
|
|
|
MESH_ELEMENT_INTERFACE,
|
|
|
|
"DevKeyMessageReceived");
|
|
|
|
|
|
|
|
builder = l_dbus_message_builder_new(msg);
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &src);
|
2019-09-27 02:14:43 +08:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'b', &remote);
|
2019-07-04 20:33:38 +08:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &net_idx);
|
|
|
|
dbus_append_byte_array(builder, data, size);
|
|
|
|
|
|
|
|
l_dbus_message_builder_finalize(builder);
|
|
|
|
l_dbus_message_builder_destroy(builder);
|
|
|
|
|
|
|
|
l_dbus_send(dbus, msg);
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:54:00 +08:00
|
|
|
static void send_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
|
|
|
|
uint16_t src, uint16_t dst,
|
|
|
|
const struct mesh_virtual *virt,
|
|
|
|
uint16_t app_idx,
|
2018-12-15 06:05:36 +08:00
|
|
|
uint16_t size, const uint8_t *data)
|
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
struct l_dbus_message *msg;
|
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
const char *owner;
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
owner = node_get_owner(node);
|
|
|
|
path = node_get_element_path(node, ele_idx);
|
|
|
|
if (!path || !owner)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_debug("Send \"MessageReceived\"");
|
|
|
|
|
|
|
|
msg = l_dbus_message_new_method_call(dbus, owner, path,
|
|
|
|
MESH_ELEMENT_INTERFACE, "MessageReceived");
|
|
|
|
|
|
|
|
builder = l_dbus_message_builder_new(msg);
|
|
|
|
|
2019-07-04 15:40:40 +08:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &src);
|
2019-10-04 04:07:46 +08:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &app_idx);
|
2019-11-26 22:54:00 +08:00
|
|
|
|
|
|
|
if (virt) {
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "ay");
|
|
|
|
dbus_append_byte_array(builder, virt->label,
|
|
|
|
sizeof(virt->label));
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
} else {
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "q");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &dst);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
}
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2019-07-04 15:40:40 +08:00
|
|
|
dbus_append_byte_array(builder, data, size);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2019-07-04 15:40:40 +08:00
|
|
|
l_dbus_message_builder_finalize(builder);
|
2019-07-04 00:34:03 +08:00
|
|
|
l_dbus_message_builder_destroy(builder);
|
2018-12-15 06:05:36 +08:00
|
|
|
l_dbus_send(dbus, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint32_t seq, uint32_t iv_index, uint8_t ttl,
|
2019-07-17 06:56:44 +08:00
|
|
|
uint16_t net_idx, uint16_t src, uint16_t dst,
|
|
|
|
uint8_t key_aid, const uint8_t *data, uint16_t size)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
|
|
|
uint8_t *clear_text;
|
|
|
|
struct mod_forward forward = {
|
|
|
|
.src = src,
|
|
|
|
.dst = dst,
|
|
|
|
.data = NULL,
|
|
|
|
.size = size - (szmict ? 8 : 4),
|
|
|
|
.ttl = ttl,
|
|
|
|
.virt = NULL,
|
|
|
|
};
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_net *net = node_get_net(node);
|
2018-07-15 03:58:59 +08:00
|
|
|
uint8_t num_ele;
|
|
|
|
int decrypt_idx, i, ele_idx;
|
2020-01-31 02:59:22 +08:00
|
|
|
uint16_t addr, crpl;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_virtual *decrypt_virt = NULL;
|
2018-12-15 06:05:36 +08:00
|
|
|
bool result = false;
|
|
|
|
bool is_subscription;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
l_debug("iv_index %8.8x key_aid = %2.2x", iv_index, key_aid);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!dst)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ele_idx = node_get_element_idx(node, dst);
|
|
|
|
|
|
|
|
if (dst < 0x8000 && ele_idx < 0)
|
|
|
|
/* Unicast and not addressed to us */
|
|
|
|
return false;
|
|
|
|
|
2020-01-31 02:59:24 +08:00
|
|
|
/* Don't process if already in RPL */
|
|
|
|
crpl = node_get_crpl(node);
|
|
|
|
if (net_msg_check_replay_cache(net, src, crpl, seq, iv_index))
|
2018-07-15 03:58:59 +08:00
|
|
|
return false;
|
|
|
|
|
2020-01-31 02:59:24 +08:00
|
|
|
clear_text = l_malloc(size);
|
2018-07-15 03:58:59 +08:00
|
|
|
forward.data = clear_text;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The packet needs to be decoded by the correct key which
|
2019-07-17 06:56:44 +08:00
|
|
|
* is hinted by key_aid, but is not necessarily definitive
|
2018-07-15 03:58:59 +08:00
|
|
|
*/
|
2019-07-17 06:56:44 +08:00
|
|
|
if (key_aid == APP_AID_DEV || mesh_net_provisioner_mode_get(net))
|
2018-12-15 06:05:36 +08:00
|
|
|
decrypt_idx = dev_packet_decrypt(node, data, size, szmict, src,
|
2019-07-17 06:56:44 +08:00
|
|
|
dst, key_aid, seq0, iv_index,
|
2018-07-15 03:58:59 +08:00
|
|
|
clear_text);
|
|
|
|
else if ((dst & 0xc000) == 0x8000)
|
|
|
|
decrypt_idx = virt_packet_decrypt(net, data, size, szmict, src,
|
2019-07-17 06:56:44 +08:00
|
|
|
dst, key_aid, seq0,
|
2018-07-15 03:58:59 +08:00
|
|
|
iv_index, clear_text,
|
|
|
|
&decrypt_virt);
|
|
|
|
else
|
2019-10-04 04:07:47 +08:00
|
|
|
decrypt_idx = app_packet_decrypt(net, data, size, szmict, src,
|
|
|
|
dst, NULL, 0,
|
|
|
|
key_aid, seq0, iv_index,
|
|
|
|
clear_text);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
if (decrypt_idx < 0) {
|
|
|
|
l_error("model.c - Failed to decrypt application payload");
|
2018-12-15 06:05:36 +08:00
|
|
|
result = false;
|
2018-07-15 03:58:59 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_packet("Clr Rx", clear_text, size - (szmict ? 8 : 4));
|
|
|
|
|
|
|
|
forward.virt = decrypt_virt;
|
2019-07-17 06:56:44 +08:00
|
|
|
forward.app_idx = decrypt_idx;
|
|
|
|
forward.net_idx = net_idx;
|
2018-07-15 03:58:59 +08:00
|
|
|
num_ele = node_get_num_elements(node);
|
|
|
|
addr = node_get_primary(node);
|
|
|
|
|
|
|
|
if (!num_ele || IS_UNASSIGNED(addr))
|
|
|
|
goto done;
|
|
|
|
|
2019-09-07 14:07:59 +08:00
|
|
|
/*
|
|
|
|
* In case of fixed group addresses check if the
|
|
|
|
* corresponding mode is enabled.
|
|
|
|
*/
|
|
|
|
if (dst == PROXIES_ADDRESS &&
|
|
|
|
(node_proxy_mode_get(node) != MESH_MODE_ENABLED))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (dst == FRIENDS_ADDRESS &&
|
|
|
|
(node_friend_mode_get(node) != MESH_MODE_ENABLED))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (dst == RELAYS_ADDRESS) {
|
|
|
|
uint8_t cnt;
|
|
|
|
uint16_t interval;
|
|
|
|
|
|
|
|
if (node_relay_mode_get(node, &cnt, &interval) !=
|
|
|
|
MESH_MODE_ENABLED)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
is_subscription = !(IS_UNICAST(dst));
|
|
|
|
|
2019-09-07 14:07:59 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
for (i = 0; i < num_ele; i++) {
|
|
|
|
struct l_queue *models;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!is_subscription && ele_idx != i)
|
2018-07-15 03:58:59 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
forward.unicast = addr + i;
|
2018-12-15 06:05:36 +08:00
|
|
|
forward.has_dst = false;
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
models = node_get_element_models(node, i, NULL);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
/* Internal models */
|
2018-07-15 03:58:59 +08:00
|
|
|
l_queue_foreach(models, forward_model, &forward);
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
/*
|
|
|
|
* Cycle through external models if the message has not been
|
|
|
|
* handled by internal models
|
|
|
|
*/
|
2019-07-04 20:33:38 +08:00
|
|
|
if (forward.has_dst && !forward.done) {
|
|
|
|
if ((decrypt_idx & APP_IDX_MASK) == decrypt_idx)
|
2019-11-26 22:54:00 +08:00
|
|
|
send_msg_rcvd(node, i, src, dst, decrypt_virt,
|
2019-07-17 06:56:44 +08:00
|
|
|
forward.app_idx, forward.size,
|
2019-07-04 20:33:38 +08:00
|
|
|
forward.data);
|
|
|
|
else if (decrypt_idx == APP_IDX_DEV_REMOTE ||
|
2020-01-09 18:32:22 +08:00
|
|
|
decrypt_idx == APP_IDX_DEV_LOCAL)
|
2019-09-27 02:14:43 +08:00
|
|
|
send_dev_key_msg_rcvd(node, i, src, decrypt_idx,
|
|
|
|
0, forward.size, forward.data);
|
2019-07-04 20:33:38 +08:00
|
|
|
}
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Either the message has been processed internally or
|
|
|
|
* has been passed on to an external model.
|
|
|
|
*/
|
2020-01-31 02:59:24 +08:00
|
|
|
result |= forward.has_dst | forward.done;
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
/* If the message was to unicast address, we are done */
|
|
|
|
if (!is_subscription && ele_idx == i)
|
2018-07-15 03:58:59 +08:00
|
|
|
break;
|
2019-09-07 14:07:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For the fixed group addresses, i.e., all-proxies,
|
|
|
|
* all-friends, all-relays, all-nodes, the message is delivered
|
|
|
|
* to a primary element only.
|
|
|
|
*/
|
|
|
|
if (IS_FIXED_GROUP_ADDRESS(dst))
|
|
|
|
break;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2020-01-31 02:59:24 +08:00
|
|
|
/* If message has been handled by us, add to RPL */
|
|
|
|
if (result)
|
|
|
|
net_msg_add_replay_cache(net, src, seq, iv_index);
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
done:
|
|
|
|
l_free(clear_text);
|
2020-01-31 02:59:24 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
return result;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_publish(struct mesh_node *node, uint32_t mod_id,
|
|
|
|
uint16_t src, uint8_t ttl,
|
2018-07-15 03:58:59 +08:00
|
|
|
const void *msg, uint16_t msg_len)
|
|
|
|
{
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_net *net = node_get_net(node);
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model *mod;
|
2019-06-30 14:43:54 +08:00
|
|
|
uint8_t *label = NULL;
|
2019-07-17 06:56:44 +08:00
|
|
|
uint16_t net_idx;
|
2018-12-15 06:05:36 +08:00
|
|
|
bool result;
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
/* print_packet("Mod Tx", msg, msg_len); */
|
|
|
|
|
|
|
|
if (!net || msg_len > 380)
|
2018-12-15 06:05:36 +08:00
|
|
|
return MESH_ERROR_INVALID_ARGS;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
/* If SRC is 0, use the Primary Element */
|
|
|
|
if (src == 0)
|
|
|
|
src = mesh_net_get_address(net);
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, src, mod_id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod) {
|
2018-12-15 06:05:36 +08:00
|
|
|
l_debug("model %x not found", mod_id);
|
|
|
|
return MESH_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mod->pub) {
|
|
|
|
l_debug("publication doesn't exist (model %x)", mod_id);
|
|
|
|
return MESH_ERROR_DOES_NOT_EXIST;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gettimeofday(&tx_start, NULL);
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (IS_UNASSIGNED(mod->pub->addr))
|
2019-06-30 14:43:54 +08:00
|
|
|
return MESH_ERROR_DOES_NOT_EXIST;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (mod->pub->virt)
|
|
|
|
label = mod->pub->virt->label;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
l_debug("publish dst=%x", mod->pub->addr);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
net_idx = appkey_net_idx(net, mod->pub->idx);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
result = msg_send(node, mod->pub->credential != 0, src,
|
2020-02-26 02:44:16 +08:00
|
|
|
mod->pub->addr, mod->pub->idx, net_idx,
|
|
|
|
label, ttl, msg, msg_len);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
return result ? MESH_ERROR_NONE : MESH_ERROR_FAILED;
|
|
|
|
}
|
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t dst,
|
|
|
|
uint16_t app_idx, uint16_t net_idx,
|
|
|
|
uint8_t ttl,
|
2018-12-15 06:05:36 +08:00
|
|
|
const void *msg, uint16_t msg_len)
|
|
|
|
{
|
|
|
|
/* print_packet("Mod Tx", msg, msg_len); */
|
|
|
|
|
|
|
|
/* If SRC is 0, use the Primary Element */
|
|
|
|
if (src == 0)
|
|
|
|
src = node_get_primary(node);
|
|
|
|
|
|
|
|
gettimeofday(&tx_start, NULL);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
if (IS_UNASSIGNED(dst))
|
2018-12-15 06:05:36 +08:00
|
|
|
return false;
|
|
|
|
|
2019-07-17 06:56:44 +08:00
|
|
|
return msg_send(node, false, src, dst, app_idx, net_idx,
|
|
|
|
NULL, ttl, msg, msg_len);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2019-06-30 14:43:55 +08:00
|
|
|
const uint8_t *pub_addr, uint16_t idx, bool cred_flag,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint8_t ttl, uint8_t period, uint8_t retransmit,
|
2020-02-26 02:44:16 +08:00
|
|
|
bool is_virt, uint16_t *dst)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
|
|
|
struct mesh_model *mod;
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
|
|
|
|
return MESH_STATUS_INVALID_PUB_PARAM;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!appkey_have_key(node_get_net(node), idx))
|
2018-07-15 03:58:59 +08:00
|
|
|
return MESH_STATUS_INVALID_APPKEY;
|
|
|
|
|
|
|
|
/*
|
2018-12-15 06:05:36 +08:00
|
|
|
* If the publication address is set to unassigned address value,
|
2019-06-30 14:43:53 +08:00
|
|
|
* remove the publication
|
2018-07-15 03:58:59 +08:00
|
|
|
*/
|
2020-02-26 02:44:16 +08:00
|
|
|
if (!is_virt && IS_UNASSIGNED(l_get_le16(pub_addr))) {
|
2018-12-15 06:05:36 +08:00
|
|
|
remove_pub(node, mod);
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
/* Check if the old publication destination is a virtual label */
|
|
|
|
if (mod->pub && mod->pub->virt) {
|
|
|
|
unref_virt(mod->pub->virt);
|
|
|
|
mod->pub->virt = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_virt) {
|
|
|
|
status = set_pub(mod, l_get_le16(pub_addr), idx, cred_flag,
|
|
|
|
ttl, period, retransmit);
|
|
|
|
} else
|
|
|
|
status = set_virt_pub(mod, pub_addr, idx, cred_flag, ttl,
|
|
|
|
period, retransmit);
|
|
|
|
|
|
|
|
*dst = mod->pub->addr;
|
2019-06-30 14:43:55 +08:00
|
|
|
|
|
|
|
if (status != MESH_STATUS_SUCCESS)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (!mod->cbs)
|
|
|
|
/* External model */
|
|
|
|
config_update_model_pub_period(node, mod->ele_idx, id,
|
2019-03-09 06:58:20 +08:00
|
|
|
pub_period_to_ms(period));
|
2019-06-30 14:43:55 +08:00
|
|
|
else
|
|
|
|
/* Internal model, call registered callbacks */
|
|
|
|
mod->cbs->pub(mod->pub);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
|
|
|
return MESH_STATUS_SUCCESS;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
struct mesh_model_pub *mesh_model_pub_get(struct mesh_node *node, uint16_t addr,
|
|
|
|
uint32_t mod_id, int *status)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
|
|
|
struct mesh_model *mod;
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, mod_id, status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return mod->pub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_model_free(void *data)
|
|
|
|
{
|
|
|
|
struct mesh_model *mod = data;
|
|
|
|
|
|
|
|
l_queue_destroy(mod->bindings, NULL);
|
|
|
|
l_queue_destroy(mod->subs, NULL);
|
|
|
|
l_queue_destroy(mod->virtuals, unref_virt);
|
|
|
|
l_free(mod->pub);
|
|
|
|
l_free(mod);
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:57:48 +08:00
|
|
|
struct mesh_model *mesh_model_new(uint8_t ele_idx, uint32_t id)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
|
|
|
struct mesh_model *mod = l_new(struct mesh_model, 1);
|
|
|
|
|
|
|
|
mod->id = id;
|
|
|
|
mod->ele_idx = ele_idx;
|
|
|
|
mod->virtuals = l_queue_new();
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
/* Internal models only */
|
|
|
|
static void restore_model_state(struct mesh_model *mod)
|
|
|
|
{
|
2018-07-15 03:58:59 +08:00
|
|
|
const struct mesh_model_ops *cbs;
|
|
|
|
const struct l_queue_entry *b;
|
|
|
|
|
|
|
|
cbs = mod->cbs;
|
2018-12-15 06:05:36 +08:00
|
|
|
if (!cbs)
|
|
|
|
return;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-07-04 04:53:41 +08:00
|
|
|
if (!l_queue_isempty(mod->bindings) && cbs->bind) {
|
2018-07-15 03:58:59 +08:00
|
|
|
for (b = l_queue_get_entries(mod->bindings); b; b = b->next) {
|
|
|
|
if (cbs->bind(L_PTR_TO_UINT(b->data), ACTION_ADD) !=
|
2019-06-30 14:43:53 +08:00
|
|
|
MESH_STATUS_SUCCESS)
|
2018-07-15 03:58:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod->pub && cbs->pub)
|
|
|
|
cbs->pub(mod->pub);
|
2018-12-15 06:05:36 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
uint32_t mesh_model_get_model_id(const struct mesh_model *model)
|
|
|
|
{
|
|
|
|
return model->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This registers an internal model, i.e. implemented within meshd */
|
|
|
|
bool mesh_model_register(struct mesh_node *node, uint8_t ele_idx,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint32_t mod_id,
|
|
|
|
const struct mesh_model_ops *cbs,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct mesh_model *mod;
|
2019-06-06 09:18:32 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
/* Internal models are always SIG models */
|
|
|
|
mod_id = VENDOR_ID_MASK | mod_id;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-06-06 09:18:32 +08:00
|
|
|
mod = get_model(node, ele_idx, mod_id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mod->cbs = cbs;
|
|
|
|
mod->user_data = user_data;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
restore_model_state(mod);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
void mesh_model_app_key_delete(struct mesh_node *node, struct l_queue *models,
|
|
|
|
uint16_t app_idx)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2018-12-15 06:05:36 +08:00
|
|
|
const struct l_queue_entry *entry = l_queue_get_entries(models);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
for (; entry; entry = entry->next) {
|
|
|
|
struct mesh_model *model = entry->data;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
model_unbind_idx(node, model, app_idx);
|
|
|
|
}
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_binding_del(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t app_idx)
|
|
|
|
{
|
|
|
|
l_debug("0x%x, 0x%x, %d", addr, id, app_idx);
|
2018-12-15 06:05:36 +08:00
|
|
|
return update_binding(node, addr, id, app_idx, true);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_binding_add(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t app_idx)
|
|
|
|
{
|
|
|
|
l_debug("0x%x, 0x%x, %d", addr, id, app_idx);
|
2018-12-15 06:05:36 +08:00
|
|
|
return update_binding(node, addr, id, app_idx, false);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_get_bindings(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint8_t *buf, uint16_t buf_size, uint16_t *size)
|
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model *mod;
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
uint16_t n;
|
|
|
|
uint32_t idx_pair;
|
|
|
|
int i;
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
if (!mod) {
|
|
|
|
*size = 0;
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
entry = l_queue_get_entries(mod->bindings);
|
|
|
|
n = 0;
|
|
|
|
i = 0;
|
|
|
|
idx_pair = 0;
|
|
|
|
|
|
|
|
for (; entry; entry = entry->next) {
|
|
|
|
uint16_t app_idx = (uint16_t) (L_PTR_TO_UINT(entry->data));
|
|
|
|
|
|
|
|
if (!(i & 0x1)) {
|
|
|
|
idx_pair = app_idx;
|
|
|
|
} else {
|
|
|
|
idx_pair <<= 12;
|
|
|
|
idx_pair += app_idx;
|
2019-06-30 14:43:53 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
/* Unlikely, but check for overflow*/
|
|
|
|
if ((n + 3) > buf_size) {
|
|
|
|
l_warn("Binding list too large");
|
|
|
|
goto done;
|
|
|
|
}
|
2019-06-30 14:43:53 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
l_put_le32(idx_pair, buf);
|
|
|
|
buf += 3;
|
|
|
|
n += 3;
|
|
|
|
}
|
2019-06-06 09:18:32 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the last app key if present */
|
|
|
|
if (i & 0x1 && ((n + 2) <= buf_size)) {
|
|
|
|
l_put_le16(idx_pair, buf);
|
|
|
|
n += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
*size = n;
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_sub_get(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2018-07-15 03:58:59 +08:00
|
|
|
uint8_t *buf, uint16_t buf_size, uint16_t *size)
|
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
int16_t n;
|
|
|
|
struct mesh_model *mod;
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
entry = l_queue_get_entries(mod->subs);
|
|
|
|
*size = 0;
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (; entry; entry = entry->next) {
|
|
|
|
if ((n + 2) > buf_size)
|
|
|
|
return MESH_STATUS_UNSPECIFIED_ERROR;
|
|
|
|
|
|
|
|
l_put_le16((uint16_t) L_PTR_TO_UINT(entry->data), buf);
|
|
|
|
buf += 2;
|
|
|
|
n += 2;
|
|
|
|
}
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
entry = l_queue_get_entries(mod->virtuals);
|
|
|
|
|
|
|
|
for (; entry; entry = entry->next) {
|
|
|
|
struct mesh_virtual *virt = entry->data;
|
|
|
|
|
|
|
|
if ((n + 2) > buf_size)
|
|
|
|
return MESH_STATUS_UNSPECIFIED_ERROR;
|
|
|
|
|
|
|
|
l_put_le16((uint16_t) L_PTR_TO_UINT(virt->addr), buf);
|
|
|
|
buf += 2;
|
|
|
|
n += 2;
|
|
|
|
}
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
*size = n;
|
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_sub_add(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2020-02-26 02:44:16 +08:00
|
|
|
const uint8_t *group, bool is_virt, uint16_t *dst)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model *mod;
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
status = add_sub(node_get_net(node), mod, group, is_virt, dst);
|
2019-11-26 22:54:01 +08:00
|
|
|
|
|
|
|
if (status != MESH_STATUS_SUCCESS)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (!mod->cbs)
|
|
|
|
/* External models */
|
|
|
|
config_update_model_subscriptions(node, mod);
|
|
|
|
|
|
|
|
return MESH_STATUS_SUCCESS;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_sub_ovr(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2020-02-26 02:44:16 +08:00
|
|
|
const uint8_t *group, bool is_virt, uint16_t *dst)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct l_queue *virtuals, *subs;
|
|
|
|
struct mesh_model *mod;
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
subs = mod->subs;
|
|
|
|
virtuals = mod->virtuals;
|
|
|
|
mod->subs = l_queue_new();
|
|
|
|
mod->virtuals = l_queue_new();
|
|
|
|
|
|
|
|
if (!mod->subs || !mod->virtuals)
|
|
|
|
return MESH_STATUS_INSUFF_RESOURCES;
|
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
status = add_sub(node_get_net(node), mod, group, is_virt, dst);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
if (status != MESH_STATUS_SUCCESS) {
|
|
|
|
/* Adding new group failed, so revert to old lists */
|
2018-07-15 03:58:59 +08:00
|
|
|
l_queue_destroy(mod->subs, NULL);
|
|
|
|
mod->subs = subs;
|
|
|
|
l_queue_destroy(mod->virtuals, unref_virt);
|
|
|
|
mod->virtuals = virtuals;
|
|
|
|
} else {
|
|
|
|
const struct l_queue_entry *entry;
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_net *net = node_get_net(node);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
entry = l_queue_get_entries(subs);
|
2019-06-06 09:18:32 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
for (; entry; entry = entry->next)
|
|
|
|
mesh_net_dst_unreg(net,
|
|
|
|
(uint16_t) L_PTR_TO_UINT(entry->data));
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
/* Destroy old lists */
|
2018-07-15 03:58:59 +08:00
|
|
|
l_queue_destroy(subs, NULL);
|
|
|
|
l_queue_destroy(virtuals, unref_virt);
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:54:01 +08:00
|
|
|
if (!mod->cbs)
|
|
|
|
/* External models */
|
|
|
|
config_update_model_subscriptions(node, mod);
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_sub_del(struct mesh_node *node, uint16_t addr, uint32_t id,
|
2020-02-26 02:44:16 +08:00
|
|
|
const uint8_t *group, bool is_virt, uint16_t *dst)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
uint16_t grp;
|
|
|
|
struct mesh_model *mod;
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2020-02-26 02:44:16 +08:00
|
|
|
if (is_virt) {
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_virtual *virt;
|
|
|
|
|
2019-06-30 14:43:54 +08:00
|
|
|
virt = l_queue_find(mod->virtuals, find_virt_by_label, group);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (virt) {
|
|
|
|
l_queue_remove(mod->virtuals, virt);
|
2019-06-30 14:43:54 +08:00
|
|
|
grp = virt->addr;
|
2018-07-15 03:58:59 +08:00
|
|
|
unref_virt(virt);
|
|
|
|
} else {
|
|
|
|
if (!mesh_crypto_virtual_addr(group, &grp))
|
|
|
|
return MESH_STATUS_STORAGE_FAIL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
grp = l_get_le16(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = grp;
|
|
|
|
|
|
|
|
if (l_queue_remove(mod->subs, L_UINT_TO_PTR(grp)))
|
2018-12-15 06:05:36 +08:00
|
|
|
mesh_net_dst_unreg(node_get_net(node), grp);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-11-26 22:54:01 +08:00
|
|
|
if (!mod->cbs)
|
|
|
|
/* External models */
|
|
|
|
config_update_model_subscriptions(node, mod);
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
return MESH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
int mesh_model_sub_del_all(struct mesh_node *node, uint16_t addr, uint32_t id)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-06-30 14:43:53 +08:00
|
|
|
int status;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model *mod;
|
|
|
|
const struct l_queue_entry *entry;
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_net *net = node_get_net(node);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
mod = find_model(node, addr, id, &status);
|
2018-07-15 03:58:59 +08:00
|
|
|
if (!mod)
|
2019-06-30 14:43:53 +08:00
|
|
|
return status;
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
entry = l_queue_get_entries(mod->subs);
|
2019-06-06 09:18:32 +08:00
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
for (; entry; entry = entry->next)
|
|
|
|
mesh_net_dst_unreg(net, (uint16_t) L_PTR_TO_UINT(entry->data));
|
|
|
|
|
2019-11-18 18:51:18 +08:00
|
|
|
l_queue_clear(mod->subs, NULL);
|
|
|
|
l_queue_clear(mod->virtuals, unref_virt);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
2019-11-26 22:54:01 +08:00
|
|
|
if (!mod->cbs)
|
|
|
|
/* External models */
|
|
|
|
config_update_model_subscriptions(node, mod);
|
|
|
|
|
2019-06-30 14:43:53 +08:00
|
|
|
return MESH_STATUS_SUCCESS;
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_model *mesh_model_setup(struct mesh_node *node, uint8_t ele_idx,
|
|
|
|
void *data)
|
2018-07-15 03:58:59 +08:00
|
|
|
{
|
2019-07-15 07:23:13 +08:00
|
|
|
struct mesh_config_model *db_mod = data;
|
2018-07-15 03:58:59 +08:00
|
|
|
struct mesh_model *mod;
|
2018-12-15 06:05:36 +08:00
|
|
|
struct mesh_net *net;
|
2019-07-15 07:23:13 +08:00
|
|
|
struct mesh_config_pub *pub = db_mod->pub;
|
2018-07-15 03:58:59 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
if (db_mod->num_bindings > MAX_BINDINGS) {
|
|
|
|
l_warn("Binding list too long %u (max %u)",
|
|
|
|
db_mod->num_bindings, MAX_BINDINGS);
|
2018-07-15 03:58:59 +08:00
|
|
|
return NULL;
|
2018-12-15 06:05:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-07 04:57:48 +08:00
|
|
|
mod = mesh_model_new(ele_idx, db_mod->vendor ? db_mod->id :
|
2018-12-15 06:05:36 +08:00
|
|
|
db_mod->id | VENDOR_ID_MASK);
|
2018-07-15 03:58:59 +08:00
|
|
|
|
|
|
|
/* Implicitly bind config server model to device key */
|
|
|
|
if (db_mod->id == CONFIG_SRV_MODEL) {
|
|
|
|
|
|
|
|
if (ele_idx != PRIMARY_ELE_IDX)
|
|
|
|
return NULL;
|
|
|
|
|
2019-07-04 20:33:36 +08:00
|
|
|
l_queue_push_head(mod->bindings,
|
|
|
|
L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
|
2018-07-15 03:58:59 +08:00
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (db_mod->id == CONFIG_CLI_MODEL) {
|
2019-07-04 20:33:36 +08:00
|
|
|
l_queue_push_head(mod->bindings,
|
|
|
|
L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
|
2018-07-15 03:58:59 +08:00
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
net = node_get_net(node);
|
|
|
|
|
2018-07-15 03:58:59 +08:00
|
|
|
/* Add application key bindings if present */
|
|
|
|
if (db_mod->bindings) {
|
|
|
|
mod->bindings = l_queue_new();
|
2018-12-15 06:05:36 +08:00
|
|
|
for (i = 0; i < db_mod->num_bindings; i++)
|
|
|
|
model_bind_idx(node, mod, db_mod->bindings[i]);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add publication if present */
|
2020-02-26 02:44:16 +08:00
|
|
|
if (pub) {
|
2019-11-12 03:54:06 +08:00
|
|
|
uint8_t retransmit = pub->count +
|
|
|
|
((pub->interval / 50 - 1) << 3);
|
2020-02-26 02:44:16 +08:00
|
|
|
if (pub->virt)
|
|
|
|
set_virt_pub(mod, pub->virt_addr, pub->idx,
|
|
|
|
pub->credential, pub->ttl,
|
|
|
|
pub->period, retransmit);
|
|
|
|
else if (!IS_UNASSIGNED(pub->addr))
|
|
|
|
set_pub(mod, pub->addr, pub->idx, pub->credential,
|
|
|
|
pub->ttl, pub->period, retransmit);
|
2018-07-15 03:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add subscriptions if present */
|
|
|
|
if (!db_mod->subs)
|
|
|
|
return mod;
|
|
|
|
|
|
|
|
for (i = 0; i < db_mod->num_subs; i++) {
|
|
|
|
uint16_t group;
|
|
|
|
uint8_t *src;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To keep calculations for virtual label coherent,
|
|
|
|
* convert to little endian.
|
|
|
|
*/
|
|
|
|
l_put_le16(db_mod->subs[i].src.addr, &group);
|
|
|
|
src = db_mod->subs[i].virt ? db_mod->subs[i].src.virt_addr :
|
|
|
|
(uint8_t *) &group;
|
|
|
|
|
|
|
|
if (add_sub(net, mod, src, db_mod->subs[i].virt, NULL) !=
|
|
|
|
MESH_STATUS_SUCCESS) {
|
|
|
|
mesh_model_free(mod);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t mesh_model_opcode_set(uint32_t opcode, uint8_t *buf)
|
|
|
|
{
|
|
|
|
if (opcode <= 0x7e) {
|
|
|
|
buf[0] = opcode;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode >= 0x8000 && opcode <= 0xbfff) {
|
|
|
|
l_put_be16(opcode, buf);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode >= 0xc00000 && opcode <= 0xffffff) {
|
|
|
|
buf[0] = (opcode >> 16) & 0xff;
|
|
|
|
l_put_be16(opcode, buf + 1);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
l_debug("Illegal Opcode %x", opcode);
|
2018-07-15 03:58:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mesh_model_opcode_get(const uint8_t *buf, uint16_t size,
|
|
|
|
uint32_t *opcode, uint16_t *n)
|
|
|
|
{
|
|
|
|
if (!n || !opcode || size < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (buf[0] & 0xc0) {
|
|
|
|
case 0x00:
|
|
|
|
case 0x40:
|
|
|
|
/* RFU */
|
|
|
|
if (buf[0] == 0x7f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*n = 1;
|
|
|
|
*opcode = buf[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x80:
|
|
|
|
if (size < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*n = 2;
|
|
|
|
*opcode = l_get_be16(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xc0:
|
|
|
|
if (size < 3)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*n = 3;
|
|
|
|
*opcode = l_get_be16(buf + 1);
|
|
|
|
*opcode |= buf[0] << 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
print_packet("Bad", buf, size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
void model_build_config(void *model, void *msg_builder)
|
|
|
|
{
|
|
|
|
struct l_dbus_message_builder *builder = msg_builder;
|
|
|
|
struct mesh_model *mod = model;
|
|
|
|
uint16_t id;
|
|
|
|
|
|
|
|
if (is_internal(mod->id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!l_queue_length(mod->subs) && !l_queue_length(mod->virtuals) &&
|
|
|
|
!mod->pub && !l_queue_length(mod->bindings))
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_struct(builder, "qa{sv}");
|
|
|
|
|
|
|
|
/* Model id */
|
|
|
|
id = mod->id & 0xffff;
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'q', &id);
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_array(builder, "{sv}");
|
|
|
|
|
|
|
|
/* For vendor models, add vendor id */
|
|
|
|
if ((mod->id & VENDOR_ID_MASK) != VENDOR_ID_MASK) {
|
|
|
|
uint16_t vendor = mod->id >> 16;
|
|
|
|
dbus_append_dict_entry_basic(builder, "Vendor", "q", &vendor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Model bindings, if present */
|
|
|
|
if (l_queue_length(mod->bindings))
|
|
|
|
append_dict_uint16_array(builder, mod->bindings, "Bindings");
|
|
|
|
|
|
|
|
/* Model periodic publication interval, if present */
|
|
|
|
if (mod->pub) {
|
2019-03-09 06:58:20 +08:00
|
|
|
uint32_t period = pub_period_to_ms(mod->pub->period);
|
2018-12-15 06:05:36 +08:00
|
|
|
dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
|
|
|
|
&period);
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:54:01 +08:00
|
|
|
if (l_queue_length(mod->subs) || l_queue_length(mod->virtuals))
|
|
|
|
append_dict_subs_array(builder, mod->subs, mod->virtuals,
|
|
|
|
"Subscriptions");
|
|
|
|
|
2018-12-15 06:05:36 +08:00
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
l_dbus_message_builder_leave_struct(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_model_init(void)
|
|
|
|
{
|
|
|
|
mesh_virtuals = l_queue_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_model_cleanup(void)
|
|
|
|
{
|
|
|
|
l_queue_destroy(mesh_virtuals, l_free);
|
2019-06-30 14:43:54 +08:00
|
|
|
mesh_virtuals = NULL;
|
2018-12-15 06:05:36 +08:00
|
|
|
}
|