2019-01-07 07:16:47 +00:00
|
|
|
/* Bluetooth Mesh */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
* Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL)
|
|
|
|
|
2020-03-31 08:49:36 +00:00
|
|
|
#include "btc_ble_mesh_health_model.h"
|
|
|
|
|
2019-01-07 07:16:47 +00:00
|
|
|
#include "foundation.h"
|
|
|
|
#include "mesh_common.h"
|
2020-01-19 10:57:13 +00:00
|
|
|
#include "health_cli.h"
|
|
|
|
|
2019-01-07 07:16:47 +00:00
|
|
|
s32_t health_msg_timeout;
|
|
|
|
|
|
|
|
static bt_mesh_health_client_t *health_cli;
|
|
|
|
|
|
|
|
static const bt_mesh_client_op_pair_t health_op_pair[] = {
|
|
|
|
{ OP_HEALTH_FAULT_GET, OP_HEALTH_FAULT_STATUS },
|
|
|
|
{ OP_HEALTH_FAULT_CLEAR, OP_HEALTH_FAULT_STATUS },
|
|
|
|
{ OP_HEALTH_FAULT_TEST, OP_HEALTH_FAULT_STATUS },
|
|
|
|
{ OP_HEALTH_PERIOD_GET, OP_HEALTH_PERIOD_STATUS },
|
|
|
|
{ OP_HEALTH_PERIOD_SET, OP_HEALTH_PERIOD_STATUS },
|
|
|
|
{ OP_ATTENTION_GET, OP_ATTENTION_STATUS },
|
|
|
|
{ OP_ATTENTION_SET, OP_ATTENTION_STATUS },
|
|
|
|
};
|
|
|
|
|
2020-01-19 10:57:13 +00:00
|
|
|
static bt_mesh_mutex_t health_client_lock;
|
2019-09-16 07:05:14 +00:00
|
|
|
|
|
|
|
static void bt_mesh_health_client_mutex_new(void)
|
|
|
|
{
|
2020-01-19 10:57:13 +00:00
|
|
|
if (!health_client_lock.mutex) {
|
|
|
|
bt_mesh_mutex_create(&health_client_lock);
|
2019-09-16 07:05:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 10:57:13 +00:00
|
|
|
static void bt_mesh_health_client_mutex_free(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_free(&health_client_lock);
|
|
|
|
}
|
|
|
|
|
2019-09-16 07:05:14 +00:00
|
|
|
static void bt_mesh_health_client_lock(void)
|
|
|
|
{
|
2020-01-19 10:57:13 +00:00
|
|
|
bt_mesh_mutex_lock(&health_client_lock);
|
2019-09-16 07:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_mesh_health_client_unlock(void)
|
|
|
|
{
|
2020-01-19 10:57:13 +00:00
|
|
|
bt_mesh_mutex_unlock(&health_client_lock);
|
2019-09-16 07:05:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 07:16:47 +00:00
|
|
|
static void timeout_handler(struct k_work *work)
|
|
|
|
{
|
2019-09-26 11:38:25 +00:00
|
|
|
struct k_delayed_work *timer = NULL;
|
2019-01-07 07:16:47 +00:00
|
|
|
bt_mesh_client_node_t *node = NULL;
|
2020-01-19 10:57:13 +00:00
|
|
|
struct bt_mesh_msg_ctx ctx = {0};
|
|
|
|
u32_t opcode = 0U;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
BT_WARN("Receive health status message timeout");
|
|
|
|
|
2019-09-16 07:05:14 +00:00
|
|
|
bt_mesh_health_client_lock();
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2019-09-26 11:38:25 +00:00
|
|
|
timer = CONTAINER_OF(work, struct k_delayed_work, work);
|
|
|
|
|
|
|
|
if (timer && !k_delayed_work_free(timer)) {
|
|
|
|
node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work);
|
|
|
|
if (node) {
|
2020-01-19 10:57:13 +00:00
|
|
|
memcpy(&ctx, &node->ctx, sizeof(ctx));
|
|
|
|
opcode = node->opcode;
|
2019-09-26 11:38:25 +00:00
|
|
|
bt_mesh_client_free_node(node);
|
2020-01-19 10:57:13 +00:00
|
|
|
bt_mesh_health_client_cb_evt_to_btc(
|
|
|
|
opcode, BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0);
|
2019-09-26 11:38:25 +00:00
|
|
|
}
|
2019-09-16 07:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_health_client_unlock();
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void health_client_cancel(struct bt_mesh_model *model,
|
|
|
|
struct bt_mesh_msg_ctx *ctx,
|
|
|
|
void *status, size_t len)
|
|
|
|
{
|
|
|
|
bt_mesh_client_node_t *node = NULL;
|
|
|
|
struct net_buf_simple buf = {0};
|
|
|
|
u8_t evt_type = 0xFF;
|
|
|
|
|
|
|
|
if (!model || !ctx || !status || !len) {
|
|
|
|
BT_ERR("%s, Invalid parameter", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it is a publish message, sent to the user directly. */
|
|
|
|
buf.data = (u8_t *)status;
|
|
|
|
buf.len = (u16_t)len;
|
2019-09-16 07:05:14 +00:00
|
|
|
|
|
|
|
bt_mesh_health_client_lock();
|
|
|
|
|
2019-09-02 04:06:20 +00:00
|
|
|
node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (!node) {
|
2020-07-04 10:35:08 +00:00
|
|
|
BT_DBG("Unexpected Health Status 0x%04x", ctx->recv_op);
|
2019-01-07 07:16:47 +00:00
|
|
|
} else {
|
|
|
|
switch (node->opcode) {
|
|
|
|
case OP_HEALTH_FAULT_GET:
|
|
|
|
case OP_HEALTH_PERIOD_GET:
|
|
|
|
case OP_ATTENTION_GET:
|
2019-09-02 05:01:06 +00:00
|
|
|
evt_type = BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE;
|
2019-01-07 07:16:47 +00:00
|
|
|
break;
|
|
|
|
case OP_HEALTH_FAULT_CLEAR:
|
|
|
|
case OP_HEALTH_FAULT_TEST:
|
|
|
|
case OP_HEALTH_PERIOD_SET:
|
|
|
|
case OP_ATTENTION_SET:
|
2019-09-02 05:01:06 +00:00
|
|
|
evt_type = BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE;
|
2019-01-07 07:16:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-16 07:05:14 +00:00
|
|
|
if (!k_delayed_work_free(&node->timer)) {
|
2020-01-19 10:57:13 +00:00
|
|
|
u32_t opcode = node->opcode;
|
2019-09-26 11:38:25 +00:00
|
|
|
bt_mesh_client_free_node(node);
|
2020-01-19 10:57:13 +00:00
|
|
|
bt_mesh_health_client_cb_evt_to_btc(
|
|
|
|
opcode, evt_type, model, ctx, (const u8_t *)status, len);
|
2019-09-16 07:05:14 +00:00
|
|
|
}
|
2019-01-07 07:16:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 07:05:14 +00:00
|
|
|
bt_mesh_health_client_unlock();
|
|
|
|
|
2019-01-07 07:16:47 +00:00
|
|
|
switch (ctx->recv_op) {
|
|
|
|
case OP_HEALTH_FAULT_STATUS: {
|
|
|
|
struct bt_mesh_health_fault_status *val;
|
|
|
|
val = (struct bt_mesh_health_fault_status *)status;
|
|
|
|
bt_mesh_free_buf(val->fault_array);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-16 06:53:34 +00:00
|
|
|
case OP_HEALTH_CURRENT_STATUS: {
|
|
|
|
struct bt_mesh_health_current_status *val = status;
|
|
|
|
bt_mesh_free_buf(val->fault_array);
|
|
|
|
break;
|
|
|
|
}
|
2019-01-07 07:16:47 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void health_fault_status(struct bt_mesh_model *model,
|
|
|
|
struct bt_mesh_msg_ctx *ctx,
|
|
|
|
struct net_buf_simple *buf)
|
|
|
|
{
|
|
|
|
struct bt_mesh_health_fault_status status = {0};
|
|
|
|
|
|
|
|
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
|
|
|
|
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
|
|
|
|
bt_hex(buf->data, buf->len));
|
|
|
|
|
|
|
|
status.test_id = net_buf_simple_pull_u8(buf);
|
|
|
|
status.cid = net_buf_simple_pull_le16(buf);
|
|
|
|
status.fault_array = bt_mesh_alloc_buf(buf->len);
|
|
|
|
if (!status.fault_array) {
|
2020-07-04 10:35:08 +00:00
|
|
|
BT_ERR("%s, Out of memory", __func__);
|
2019-01-07 07:16:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_buf_simple_add_mem(status.fault_array, buf->data, buf->len);
|
|
|
|
|
|
|
|
health_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_health_fault_status));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void health_current_status(struct bt_mesh_model *model,
|
|
|
|
struct bt_mesh_msg_ctx *ctx,
|
|
|
|
struct net_buf_simple *buf)
|
|
|
|
{
|
2020-07-16 06:53:34 +00:00
|
|
|
struct bt_mesh_health_current_status status = {0};
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
|
|
|
|
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
|
|
|
|
bt_hex(buf->data, buf->len));
|
|
|
|
|
2020-07-16 06:53:34 +00:00
|
|
|
status.test_id = net_buf_simple_pull_u8(buf);
|
|
|
|
status.cid = net_buf_simple_pull_le16(buf);
|
|
|
|
status.fault_array = bt_mesh_alloc_buf(buf->len);
|
|
|
|
if (!status.fault_array) {
|
|
|
|
BT_ERR("%s, Out of memory", __func__);
|
2019-01-07 07:16:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:53:34 +00:00
|
|
|
net_buf_simple_add_mem(status.fault_array, buf->data, buf->len);
|
2020-01-19 10:57:13 +00:00
|
|
|
|
2020-07-16 06:53:34 +00:00
|
|
|
health_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_health_current_status));
|
2019-01-07 07:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void health_period_status(struct bt_mesh_model *model,
|
|
|
|
struct bt_mesh_msg_ctx *ctx,
|
|
|
|
struct net_buf_simple *buf)
|
|
|
|
{
|
2020-01-19 10:57:13 +00:00
|
|
|
u8_t status = 0U;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
|
|
|
|
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
|
|
|
|
bt_hex(buf->data, buf->len));
|
|
|
|
|
|
|
|
status = net_buf_simple_pull_u8(buf);
|
|
|
|
|
|
|
|
health_client_cancel(model, ctx, &status, sizeof(u8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void health_attention_status(struct bt_mesh_model *model,
|
|
|
|
struct bt_mesh_msg_ctx *ctx,
|
|
|
|
struct net_buf_simple *buf)
|
|
|
|
{
|
2020-01-19 10:57:13 +00:00
|
|
|
u8_t status = 0U;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
|
|
|
|
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
|
|
|
|
bt_hex(buf->data, buf->len));
|
|
|
|
|
|
|
|
status = net_buf_simple_pull_u8(buf);
|
|
|
|
|
|
|
|
health_client_cancel(model, ctx, &status, sizeof(u8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct bt_mesh_model_op bt_mesh_health_cli_op[] = {
|
|
|
|
{ OP_HEALTH_FAULT_STATUS, 3, health_fault_status },
|
|
|
|
{ OP_HEALTH_CURRENT_STATUS, 3, health_current_status },
|
|
|
|
{ OP_HEALTH_PERIOD_STATUS, 1, health_period_status },
|
|
|
|
{ OP_ATTENTION_STATUS, 1, health_attention_status },
|
|
|
|
BLE_MESH_MODEL_OP_END,
|
|
|
|
};
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_attention_get(bt_mesh_client_common_param_t *param)
|
2019-01-07 07:16:47 +00:00
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_GET, 0);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
bt_mesh_model_msg_init(&msg, OP_ATTENTION_GET);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_attention_set(bt_mesh_client_common_param_t *param,
|
2019-01-07 07:16:47 +00:00
|
|
|
u8_t attention, bool need_ack)
|
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_SET, 1);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
bt_mesh_model_msg_init(&msg, need_ack ? OP_ATTENTION_SET : OP_ATTENTION_SET_UNREL);
|
2019-01-07 07:16:47 +00:00
|
|
|
net_buf_simple_add_u8(&msg, attention);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, need_ack, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_period_get(bt_mesh_client_common_param_t *param)
|
2019-01-07 07:16:47 +00:00
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_GET, 0);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
bt_mesh_model_msg_init(&msg, OP_HEALTH_PERIOD_GET);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_period_set(bt_mesh_client_common_param_t *param,
|
2019-01-07 07:16:47 +00:00
|
|
|
u8_t divisor, bool need_ack)
|
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_SET, 1);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
bt_mesh_model_msg_init(&msg, need_ack ? OP_HEALTH_PERIOD_SET : OP_HEALTH_PERIOD_SET_UNREL);
|
2019-01-07 07:16:47 +00:00
|
|
|
net_buf_simple_add_u8(&msg, divisor);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, need_ack, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_fault_test(bt_mesh_client_common_param_t *param,
|
2019-01-07 07:16:47 +00:00
|
|
|
u16_t cid, u8_t test_id, bool need_ack)
|
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_TEST, 3);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
bt_mesh_model_msg_init(&msg, need_ack ? OP_HEALTH_FAULT_TEST : OP_HEALTH_FAULT_TEST_UNREL);
|
2019-01-07 07:16:47 +00:00
|
|
|
net_buf_simple_add_u8(&msg, test_id);
|
|
|
|
net_buf_simple_add_le16(&msg, cid);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, need_ack, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_fault_clear(bt_mesh_client_common_param_t *param,
|
2019-01-07 07:16:47 +00:00
|
|
|
u16_t cid, bool need_ack)
|
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_CLEAR, 2);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
bt_mesh_model_msg_init(&msg, need_ack ? OP_HEALTH_FAULT_CLEAR : OP_HEALTH_FAULT_CLEAR_UNREL);
|
2019-01-07 07:16:47 +00:00
|
|
|
net_buf_simple_add_le16(&msg, cid);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, need_ack, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
int bt_mesh_health_fault_get(bt_mesh_client_common_param_t *param, u16_t cid)
|
2019-01-07 07:16:47 +00:00
|
|
|
{
|
2019-11-04 09:54:50 +00:00
|
|
|
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_GET, 2);
|
2020-01-19 10:57:13 +00:00
|
|
|
int err = 0;
|
2019-01-07 07:16:47 +00:00
|
|
|
|
|
|
|
bt_mesh_model_msg_init(&msg, OP_HEALTH_FAULT_GET);
|
|
|
|
net_buf_simple_add_le16(&msg, cid);
|
|
|
|
|
2020-06-28 07:28:22 +00:00
|
|
|
err = bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
|
2019-01-07 07:16:47 +00:00
|
|
|
if (err) {
|
|
|
|
BT_ERR("%s, send failed (err %d)", __func__, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32_t bt_mesh_health_cli_timeout_get(void)
|
|
|
|
{
|
|
|
|
return health_msg_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_health_cli_timeout_set(s32_t timeout)
|
|
|
|
{
|
|
|
|
health_msg_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_mesh_health_cli_set(struct bt_mesh_model *model)
|
|
|
|
{
|
|
|
|
if (!model || !model->user_data) {
|
2020-07-04 10:35:08 +00:00
|
|
|
BT_ERR("No Health Client context for given model");
|
2019-01-07 07:16:47 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
health_cli = model->user_data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary)
|
|
|
|
{
|
|
|
|
health_internal_data_t *internal = NULL;
|
|
|
|
bt_mesh_health_client_t *client = NULL;
|
|
|
|
|
|
|
|
BT_DBG("primary %u", primary);
|
|
|
|
|
|
|
|
if (!model) {
|
|
|
|
BT_ERR("%s, Invalid parameter", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
client = (bt_mesh_health_client_t *)model->user_data;
|
|
|
|
if (!client) {
|
2020-07-04 10:35:08 +00:00
|
|
|
BT_ERR("No Health Client context provided");
|
2019-01-07 07:16:47 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-12-20 06:08:33 +00:00
|
|
|
if (!client->internal_data) {
|
2020-01-19 10:57:13 +00:00
|
|
|
internal = bt_mesh_calloc(sizeof(health_internal_data_t));
|
2019-12-20 06:08:33 +00:00
|
|
|
if (!internal) {
|
2020-07-04 10:35:08 +00:00
|
|
|
BT_ERR("%s, Out of memory", __func__);
|
2019-12-20 06:08:33 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2019-12-20 06:08:33 +00:00
|
|
|
sys_slist_init(&internal->queue);
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2019-12-20 06:08:33 +00:00
|
|
|
client->model = model;
|
|
|
|
client->op_pair_size = ARRAY_SIZE(health_op_pair);
|
|
|
|
client->op_pair = health_op_pair;
|
|
|
|
client->internal_data = internal;
|
|
|
|
} else {
|
|
|
|
bt_mesh_client_clear_list(client->internal_data);
|
|
|
|
}
|
2019-01-07 07:16:47 +00:00
|
|
|
|
2019-09-16 07:05:14 +00:00
|
|
|
bt_mesh_health_client_mutex_new();
|
|
|
|
|
2019-01-07 07:16:47 +00:00
|
|
|
/* Set the default health client pointer */
|
|
|
|
if (!health_cli) {
|
|
|
|
health_cli = client;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-19 10:57:13 +00:00
|
|
|
|
|
|
|
int bt_mesh_health_cli_deinit(struct bt_mesh_model *model, bool primary)
|
|
|
|
{
|
|
|
|
bt_mesh_health_client_t *client = NULL;
|
|
|
|
|
|
|
|
if (!model) {
|
|
|
|
BT_ERR("%s, Invalid parameter", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
client = (bt_mesh_health_client_t *)model->user_data;
|
|
|
|
if (!client) {
|
2020-07-04 10:35:08 +00:00
|
|
|
BT_ERR("No Health Client context provided");
|
2020-01-19 10:57:13 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_mesh_health_client_mutex_free();
|
|
|
|
|
|
|
|
if (health_cli) {
|
|
|
|
health_cli = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|