2021-10-26 04:24:54 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-01-07 02:16:47 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "mesh.h"
|
2023-08-25 02:28:44 -04:00
|
|
|
#include "mesh/main.h"
|
2019-12-09 01:30:46 -05:00
|
|
|
#include "transport.h"
|
|
|
|
#include "foundation.h"
|
2023-08-25 02:28:44 -04:00
|
|
|
#include "mesh/client_common.h"
|
|
|
|
#include "mesh/common.h"
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
#include "mesh_v1.1/utils.h"
|
|
|
|
|
2020-03-26 23:18:02 -04:00
|
|
|
#define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */
|
2019-12-09 01:30:46 -05:00
|
|
|
|
2020-12-07 04:03:11 -05:00
|
|
|
static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, uint16_t tx_dst)
|
2019-10-21 10:53:25 -04:00
|
|
|
{
|
2019-12-14 04:31:34 -05:00
|
|
|
bt_mesh_client_node_t *node = NULL;
|
|
|
|
sys_snode_t *cur = NULL;
|
|
|
|
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_lock();
|
2019-10-21 10:53:25 -04:00
|
|
|
if (sys_slist_is_empty(list)) {
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2019-10-21 10:53:25 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (cur = sys_slist_peek_head(list);
|
2023-08-28 04:09:37 -04:00
|
|
|
cur != NULL; cur = sys_slist_peek_next(cur)) {
|
2019-10-21 10:53:25 -04:00
|
|
|
node = (bt_mesh_client_node_t *)cur;
|
|
|
|
if (node->ctx.addr == tx_dst) {
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2019-10-21 10:53:25 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2019-10-21 10:53:25 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-04 06:35:08 -04:00
|
|
|
bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg(struct bt_mesh_model *model,
|
|
|
|
struct bt_mesh_msg_ctx *ctx,
|
|
|
|
struct net_buf_simple *buf, bool need_pub)
|
2019-01-07 02:16:47 -05:00
|
|
|
{
|
2019-09-02 00:06:20 -04:00
|
|
|
bt_mesh_client_internal_data_t *data = NULL;
|
|
|
|
bt_mesh_client_user_data_t *cli = NULL;
|
2019-01-07 02:16:47 -05:00
|
|
|
bt_mesh_client_node_t *node = NULL;
|
|
|
|
|
|
|
|
if (!model || !ctx || !buf) {
|
|
|
|
BT_ERR("%s, Invalid parameter", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-02 00:06:20 -04:00
|
|
|
cli = (bt_mesh_client_user_data_t *)model->user_data;
|
2019-01-07 02:16:47 -05:00
|
|
|
if (!cli) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client user data");
|
2019-01-07 02:16:47 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If the received message address is not a unicast address,
|
|
|
|
* the address may be a group/virtual address, and we push
|
|
|
|
* this message to the application layer.
|
|
|
|
*/
|
|
|
|
if (!BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
|
2019-01-07 02:16:47 -05:00
|
|
|
if (cli->publish_status && need_pub) {
|
2019-10-21 10:53:25 -04:00
|
|
|
cli->publish_status(ctx->recv_op, model, ctx, buf);
|
2019-01-07 02:16:47 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If the source address of the received status message is
|
|
|
|
* different with the destination address of the sending
|
|
|
|
* message, then the message is from another element and
|
|
|
|
* push it to application layer.
|
|
|
|
*/
|
2019-09-02 00:06:20 -04:00
|
|
|
data = (bt_mesh_client_internal_data_t *)cli->internal_data;
|
2019-01-07 02:16:47 -05:00
|
|
|
if (!data) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client internal data");
|
2019-01-07 02:16:47 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((node = bt_mesh_client_pick_node(&data->queue, ctx->addr)) == NULL) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
|
2019-01-07 02:16:47 -05:00
|
|
|
if (cli->publish_status && need_pub) {
|
2019-10-21 10:53:25 -04:00
|
|
|
cli->publish_status(ctx->recv_op, model, ctx, buf);
|
2019-01-07 02:16:47 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-21 10:53:25 -04:00
|
|
|
if (node->op_pending != ctx->recv_op) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
|
2019-01-07 02:16:47 -05:00
|
|
|
if (cli->publish_status && need_pub) {
|
2019-10-21 10:53:25 -04:00
|
|
|
cli->publish_status(ctx->recv_op, model, ctx, buf);
|
2019-01-07 02:16:47 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-12-14 02:43:51 -05:00
|
|
|
if (k_delayed_work_remaining_get(&node->timer) == 0) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
|
2019-12-14 02:43:51 -05:00
|
|
|
if (cli->publish_status && need_pub) {
|
|
|
|
cli->publish_status(ctx->recv_op, model, ctx, buf);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-07 02:16:47 -05:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-12-07 04:03:11 -05:00
|
|
|
static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, uint16_t tx_dst)
|
2019-01-07 02:16:47 -05:00
|
|
|
{
|
2019-12-14 04:31:34 -05:00
|
|
|
bt_mesh_client_node_t *node = NULL;
|
|
|
|
sys_snode_t *cur = NULL;
|
|
|
|
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_lock();
|
2019-01-07 02:16:47 -05:00
|
|
|
if (sys_slist_is_empty(list)) {
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2019-01-07 02:16:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (cur = sys_slist_peek_head(list);
|
2023-08-28 04:09:37 -04:00
|
|
|
cur != NULL; cur = sys_slist_peek_next(cur)) {
|
2019-01-07 02:16:47 -05:00
|
|
|
node = (bt_mesh_client_node_t *)cur;
|
|
|
|
if (node->ctx.addr == tx_dst) {
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2019-01-07 02:16:47 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2019-01-07 02:16:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-07 04:03:11 -05:00
|
|
|
static uint32_t bt_mesh_client_get_status_op(const bt_mesh_client_op_pair_t *op_pair,
|
|
|
|
int size, uint32_t opcode)
|
2019-01-07 02:16:47 -05:00
|
|
|
{
|
|
|
|
if (!op_pair || size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bt_mesh_client_op_pair_t *op = op_pair;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (op->cli_op == opcode) {
|
|
|
|
return op->status_op;
|
|
|
|
}
|
|
|
|
op++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
static int32_t bt_mesh_get_adv_duration(struct bt_mesh_msg_ctx *ctx)
|
2019-12-09 01:30:46 -05:00
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
uint16_t duration = 0, adv_int = 0;
|
|
|
|
uint8_t xmit = 0;
|
|
|
|
|
|
|
|
/* Initialize with network transmission */
|
|
|
|
xmit = bt_mesh_net_transmit_get();
|
|
|
|
|
|
|
|
if (bt_mesh_tag_immutable_cred(ctx->send_tag)) {
|
|
|
|
#if CONFIG_BLE_MESH_DF_SRV
|
|
|
|
if (ctx->send_cred == BLE_MESH_DIRECTED_CRED) {
|
|
|
|
xmit = bt_mesh_direct_net_transmit_get(); /* Directed network transmission */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2019-12-09 01:30:46 -05:00
|
|
|
|
|
|
|
adv_int = BLE_MESH_TRANSMIT_INT(xmit);
|
|
|
|
duration = (BLE_MESH_TRANSMIT_COUNT(xmit) + 1) * (adv_int + 10);
|
|
|
|
|
2020-12-07 04:03:11 -05:00
|
|
|
return (int32_t)duration;
|
2019-12-09 01:30:46 -05:00
|
|
|
}
|
|
|
|
|
2020-12-07 04:03:11 -05:00
|
|
|
static int32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx,
|
|
|
|
struct net_buf_simple *msg,
|
|
|
|
uint32_t opcode, int32_t timeout)
|
2019-12-09 01:30:46 -05:00
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
int32_t seg_rtx_to = 0, duration = 0, time = 0;
|
|
|
|
uint8_t seg_count = 0, seg_rtx_num = 0;
|
2020-04-08 09:32:05 -04:00
|
|
|
bool need_seg = false;
|
2020-12-07 04:03:11 -05:00
|
|
|
uint8_t mic_size = 0;
|
2019-12-09 01:30:46 -05:00
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
if (msg->len > BLE_MESH_SDU_UNSEG_MAX ||
|
|
|
|
bt_mesh_tag_send_segmented(ctx->send_tag)) {
|
2019-12-09 01:30:46 -05:00
|
|
|
need_seg = true; /* Needs segmentation */
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
mic_size = (need_seg && ctx->send_szmic == BLE_MESH_SEG_SZMIC_LONG &&
|
|
|
|
net_buf_simple_tailroom(msg) >= BLE_MESH_MIC_LONG) ?
|
2020-05-08 07:49:48 -04:00
|
|
|
BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT;
|
2019-12-09 01:30:46 -05:00
|
|
|
|
|
|
|
if (need_seg) {
|
|
|
|
/* Based on the message length, calculate how many segments are needed.
|
|
|
|
* All the messages sent from here are access messages.
|
|
|
|
*/
|
2023-08-28 04:09:37 -04:00
|
|
|
seg_rtx_num = bt_mesh_get_seg_retrans_num();
|
|
|
|
seg_rtx_to = bt_mesh_get_seg_retrans_timeout(ctx->send_ttl);
|
2019-12-09 01:30:46 -05:00
|
|
|
seg_count = (msg->len + mic_size - 1) / 12U + 1U;
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
duration = bt_mesh_get_adv_duration(ctx);
|
2019-12-09 01:30:46 -05:00
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
/* Currently only consider the time consumption of the same segmented
|
2019-12-09 01:30:46 -05:00
|
|
|
* messages, but if there are other messages between any two retrans-
|
|
|
|
* missions of the same segmented messages, then the whole time will
|
|
|
|
* be longer.
|
2020-05-08 07:49:48 -04:00
|
|
|
*
|
|
|
|
* Since the transport behavior has been changed, i.e. start retransmit
|
|
|
|
* timer after the last segment is sent, so we can simplify the timeout
|
|
|
|
* calculation here. And the retransmit timer will be started event if
|
|
|
|
* the attempts reaches ZERO when the dst is a unicast address.
|
2019-12-09 01:30:46 -05:00
|
|
|
*/
|
2020-12-07 04:03:11 -05:00
|
|
|
int32_t seg_duration = seg_count * (duration + HCI_TIME_FOR_START_ADV);
|
2023-08-28 04:09:37 -04:00
|
|
|
time = (seg_duration + seg_rtx_to) * seg_rtx_num;
|
2019-12-09 01:30:46 -05:00
|
|
|
|
|
|
|
BT_INFO("Original timeout %dms, calculated timeout %dms", timeout, time);
|
|
|
|
|
|
|
|
if (time < timeout) {
|
|
|
|
/* If the calculated time is smaller than the input timeout value,
|
|
|
|
* then use the original timeout value.
|
|
|
|
*/
|
|
|
|
time = timeout;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* For unsegmented access messages, directly use the timeout
|
|
|
|
* value from the application layer.
|
|
|
|
*/
|
|
|
|
time = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_INFO("Client message 0x%08x with timeout %dms", opcode, time);
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2020-12-07 04:03:11 -05:00
|
|
|
static void msg_send_start(uint16_t duration, int err, void *cb_data)
|
2020-02-04 02:26:44 -05:00
|
|
|
{
|
|
|
|
bt_mesh_client_node_t *node = cb_data;
|
|
|
|
|
|
|
|
BT_DBG("%s, duration %ums", __func__, duration);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
if (!k_delayed_work_free(&node->timer)) {
|
|
|
|
bt_mesh_client_free_node(node);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_delayed_work_submit(&node->timer, node->timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct bt_mesh_send_cb send_cb = {
|
|
|
|
.start = msg_send_start,
|
|
|
|
.end = NULL,
|
|
|
|
};
|
|
|
|
|
2020-06-28 03:28:22 -04:00
|
|
|
int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param,
|
|
|
|
struct net_buf_simple *msg, bool need_ack,
|
|
|
|
k_work_handler_t timer_handler)
|
2019-01-07 02:16:47 -05:00
|
|
|
{
|
2019-09-02 00:06:20 -04:00
|
|
|
bt_mesh_client_internal_data_t *internal = NULL;
|
2020-02-03 23:20:41 -05:00
|
|
|
bt_mesh_client_user_data_t *client = NULL;
|
2019-01-07 02:16:47 -05:00
|
|
|
bt_mesh_client_node_t *node = NULL;
|
2020-01-19 05:57:13 -05:00
|
|
|
int err = 0;
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2020-06-28 03:28:22 -04:00
|
|
|
if (!param || !param->model || !msg) {
|
2019-01-07 02:16:47 -05:00
|
|
|
BT_ERR("%s, Invalid parameter", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:28:22 -04:00
|
|
|
client = (bt_mesh_client_user_data_t *)param->model->user_data;
|
2020-02-03 23:20:41 -05:00
|
|
|
if (!client) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client user data");
|
2020-02-03 23:20:41 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal = (bt_mesh_client_internal_data_t *)client->internal_data;
|
|
|
|
if (!internal) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client internal data");
|
2020-02-03 23:20:41 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:28:22 -04:00
|
|
|
if (param->ctx.addr == BLE_MESH_ADDR_UNASSIGNED) {
|
|
|
|
BT_ERR("Invalid DST 0x%04x", param->ctx.addr);
|
2020-02-03 23:20:41 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2020-07-07 21:35:31 -04:00
|
|
|
if (need_ack == false || !BLE_MESH_ADDR_IS_UNICAST(param->ctx.addr)) {
|
|
|
|
/* 1. If this is an unacknowledged message, send it directly.
|
|
|
|
* 2. If this is an acknowledged message, but the destination
|
|
|
|
* is not a unicast address, e.g. a group/virtual address,
|
|
|
|
* then all the corresponding responses will be treated as
|
|
|
|
* publish messages, and no timeout will be used.
|
2020-02-03 23:20:41 -05:00
|
|
|
*/
|
2020-07-07 21:35:31 -04:00
|
|
|
err = bt_mesh_model_send(param->model, ¶m->ctx, msg, param->cb, param->cb_data);
|
|
|
|
if (err) {
|
|
|
|
BT_ERR("Failed to send client message 0x%08x", param->opcode);
|
|
|
|
}
|
|
|
|
return err;
|
2020-02-03 23:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!timer_handler) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid timeout handler");
|
2020-02-03 23:20:41 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:28:22 -04:00
|
|
|
if (bt_mesh_client_check_node_in_list(&internal->queue, param->ctx.addr)) {
|
|
|
|
BT_ERR("Busy sending message to DST 0x%04x", param->ctx.addr);
|
2020-02-03 23:20:41 -05:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
2019-12-09 01:30:46 -05:00
|
|
|
|
2020-02-03 23:20:41 -05:00
|
|
|
/* Don't forget to free the node in the timeout (timer_handler) function. */
|
|
|
|
node = (bt_mesh_client_node_t *)bt_mesh_calloc(sizeof(bt_mesh_client_node_t));
|
|
|
|
if (!node) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("%s, Out of memory", __func__);
|
2020-02-03 23:20:41 -05:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2019-12-09 01:30:46 -05:00
|
|
|
|
2020-06-28 03:28:22 -04:00
|
|
|
memcpy(&node->ctx, ¶m->ctx, sizeof(struct bt_mesh_msg_ctx));
|
2023-08-28 04:09:37 -04:00
|
|
|
node->model = param->model;
|
2020-06-28 03:28:22 -04:00
|
|
|
node->opcode = param->opcode;
|
|
|
|
node->op_pending = bt_mesh_client_get_status_op(client->op_pair, client->op_pair_size, param->opcode);
|
2020-02-03 23:20:41 -05:00
|
|
|
if (node->op_pending == 0U) {
|
2023-08-28 04:09:37 -04:00
|
|
|
BT_ERR("Status opcode not found in op_pair list, opcode 0x%08x", param->opcode);
|
2020-02-03 23:20:41 -05:00
|
|
|
bt_mesh_free(node);
|
|
|
|
return -EINVAL;
|
2019-01-07 02:16:47 -05:00
|
|
|
}
|
2020-06-28 03:28:22 -04:00
|
|
|
node->timeout = bt_mesh_client_calc_timeout(¶m->ctx, msg, param->opcode,
|
|
|
|
param->msg_timeout ? param->msg_timeout : CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT);
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2020-05-27 00:26:25 -04:00
|
|
|
if (k_delayed_work_init(&node->timer, timer_handler)) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Failed to create a timer");
|
2020-05-27 00:26:25 -04:00
|
|
|
bt_mesh_free(node);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2020-02-03 23:20:41 -05:00
|
|
|
|
|
|
|
bt_mesh_list_lock();
|
|
|
|
sys_slist_append(&internal->queue, &node->client_node);
|
|
|
|
bt_mesh_list_unlock();
|
|
|
|
|
2020-02-04 02:26:44 -05:00
|
|
|
/* "bt_mesh_model_send" will post the mesh packet to the mesh adv queue.
|
|
|
|
* Due to the higher priority of adv_thread (than btc task), we need to
|
|
|
|
* send the packet after the list item "node" is initialized properly.
|
|
|
|
*/
|
2020-06-28 03:28:22 -04:00
|
|
|
err = bt_mesh_model_send(param->model, ¶m->ctx, msg, &send_cb, node);
|
2020-02-04 02:26:44 -05:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("Failed to send client message 0x%08x", node->opcode);
|
|
|
|
k_delayed_work_free(&node->timer);
|
|
|
|
bt_mesh_client_free_node(node);
|
|
|
|
}
|
2020-02-03 23:20:41 -05:00
|
|
|
|
2020-02-04 02:26:44 -05:00
|
|
|
return err;
|
2019-01-07 02:16:47 -05:00
|
|
|
}
|
|
|
|
|
2020-01-19 05:57:13 -05:00
|
|
|
static bt_mesh_mutex_t client_model_lock;
|
2019-09-16 03:05:14 -04:00
|
|
|
|
|
|
|
void bt_mesh_client_model_lock(void)
|
|
|
|
{
|
2020-01-19 05:57:13 -05:00
|
|
|
bt_mesh_mutex_lock(&client_model_lock);
|
2019-09-16 03:05:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_client_model_unlock(void)
|
|
|
|
{
|
2020-01-19 05:57:13 -05:00
|
|
|
bt_mesh_mutex_unlock(&client_model_lock);
|
2019-09-16 03:05:14 -04:00
|
|
|
}
|
|
|
|
|
2019-01-07 02:16:47 -05:00
|
|
|
int bt_mesh_client_init(struct bt_mesh_model *model)
|
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
bt_mesh_client_internal_data_t *internal = NULL;
|
2020-07-07 06:25:29 -04:00
|
|
|
bt_mesh_client_user_data_t *client = NULL;
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2020-07-07 06:25:29 -04:00
|
|
|
if (!model || !model->op) {
|
|
|
|
BT_ERR("Invalid vendor client model");
|
2019-01-07 02:16:47 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-07-07 06:25:29 -04:00
|
|
|
client = (bt_mesh_client_user_data_t *)model->user_data;
|
|
|
|
if (!client) {
|
|
|
|
BT_ERR("No vendor client context provided");
|
2019-01-07 02:16:47 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
if (client->internal_data) {
|
|
|
|
BT_WARN("%s, Already", __func__);
|
|
|
|
return -EALREADY;
|
|
|
|
}
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
internal = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t));
|
|
|
|
if (!internal) {
|
|
|
|
BT_ERR("%s, Out of memory", __func__);
|
|
|
|
return -ENOMEM;
|
2019-12-20 01:08:33 -05:00
|
|
|
}
|
2019-01-07 02:16:47 -05:00
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
sys_slist_init(&internal->queue);
|
|
|
|
|
|
|
|
client->model = model;
|
|
|
|
client->internal_data = internal;
|
|
|
|
|
|
|
|
bt_mesh_mutex_create(&client_model_lock);
|
2019-09-16 03:05:14 -04:00
|
|
|
|
2019-01-07 02:16:47 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:19:31 -04:00
|
|
|
#if CONFIG_BLE_MESH_DEINIT
|
2020-01-19 05:57:13 -05:00
|
|
|
int bt_mesh_client_deinit(struct bt_mesh_model *model)
|
|
|
|
{
|
|
|
|
bt_mesh_client_user_data_t *client = NULL;
|
|
|
|
|
|
|
|
if (!model) {
|
2020-07-07 06:25:29 -04:00
|
|
|
BT_ERR("Invalid vendor client model");
|
2020-01-19 05:57:13 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
client = (bt_mesh_client_user_data_t *)model->user_data;
|
|
|
|
if (!client) {
|
2020-07-07 06:25:29 -04:00
|
|
|
BT_ERR("No vendor client context provided");
|
2020-01-19 05:57:13 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->internal_data) {
|
|
|
|
/* Remove items from the list */
|
|
|
|
bt_mesh_client_clear_list(client->internal_data);
|
|
|
|
|
|
|
|
/* Free the allocated internal data */
|
|
|
|
bt_mesh_free(client->internal_data);
|
|
|
|
client->internal_data = NULL;
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
bt_mesh_mutex_free(&client_model_lock);
|
2020-01-19 05:57:13 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-15 22:19:31 -04:00
|
|
|
#endif /* CONFIG_BLE_MESH_DEINIT */
|
2020-01-19 05:57:13 -05:00
|
|
|
|
2019-09-26 07:38:25 -04:00
|
|
|
int bt_mesh_client_free_node(bt_mesh_client_node_t *node)
|
2019-01-07 02:16:47 -05:00
|
|
|
{
|
2019-09-26 07:38:25 -04:00
|
|
|
bt_mesh_client_internal_data_t *internal = NULL;
|
|
|
|
bt_mesh_client_user_data_t *client = NULL;
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
if (!node || !node->model) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client list item");
|
2019-09-26 07:38:25 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
client = (bt_mesh_client_user_data_t *)node->model->user_data;
|
2019-09-26 07:38:25 -04:00
|
|
|
if (!client) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client user data");
|
2019-09-26 07:38:25 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal = (bt_mesh_client_internal_data_t *)client->internal_data;
|
|
|
|
if (!internal) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Invalid client internal data");
|
2019-01-07 02:16:47 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the client node from the queue
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_lock();
|
2019-09-26 07:38:25 -04:00
|
|
|
sys_slist_find_and_remove(&internal->queue, &node->client_node);
|
2019-12-19 22:26:55 -05:00
|
|
|
bt_mesh_list_unlock();
|
2023-08-28 04:09:37 -04:00
|
|
|
|
2019-01-07 02:16:47 -05:00
|
|
|
// Free the node
|
2020-01-19 05:57:13 -05:00
|
|
|
bt_mesh_free(node);
|
2019-01-07 02:16:47 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-20 01:08:33 -05:00
|
|
|
int bt_mesh_client_clear_list(void *data)
|
|
|
|
{
|
|
|
|
bt_mesh_client_internal_data_t *internal = NULL;
|
|
|
|
bt_mesh_client_node_t *node = NULL;
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
BT_ERR("%s, Invalid parameter", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal = (bt_mesh_client_internal_data_t *)data;
|
|
|
|
|
|
|
|
bt_mesh_list_lock();
|
|
|
|
while (!sys_slist_is_empty(&internal->queue)) {
|
|
|
|
node = (void *)sys_slist_get_not_empty(&internal->queue);
|
2020-05-27 00:26:25 -04:00
|
|
|
k_delayed_work_free(&node->timer);
|
2020-01-19 05:57:13 -05:00
|
|
|
bt_mesh_free(node);
|
2019-12-20 01:08:33 -05:00
|
|
|
}
|
|
|
|
bt_mesh_list_unlock();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|