1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/mesh.h>
8 
9 #include "cfg.h"
10 #include "access.h"
11 #include "foundation.h"
12 #include "msg.h"
13 
14 #define LOG_LEVEL CONFIG_BT_MESH_MODEL_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(bt_mesh_od_priv_proxy_cli);
17 
18 /** On-Demand Private Proxy Client Model Context */
19 static struct bt_mesh_od_priv_proxy_cli *cli;
20 
21 static int32_t msg_timeout;
22 
handle_proxy_status(const struct bt_mesh_model * mod,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)23 static int handle_proxy_status(const struct bt_mesh_model *mod,
24 			    struct bt_mesh_msg_ctx *ctx,
25 			    struct net_buf_simple *buf)
26 {
27 	uint8_t *state;
28 	uint8_t state_rsp;
29 
30 	state_rsp = net_buf_simple_pull_u8(buf);
31 
32 	LOG_DBG("On-Demand Private Proxy status received: state: %u", state_rsp);
33 
34 	if (bt_mesh_msg_ack_ctx_match(&cli->ack_ctx, OP_OD_PRIV_PROXY_STATUS,
35 				       ctx->addr, (void **)&state)) {
36 
37 		if (state) {
38 			*state = state_rsp;
39 		}
40 		bt_mesh_msg_ack_ctx_rx(&cli->ack_ctx);
41 	}
42 
43 
44 	if (cli->od_status) {
45 		cli->od_status(cli, ctx->addr, state_rsp);
46 	}
47 
48 	return 0;
49 }
50 
51 const struct bt_mesh_model_op _bt_mesh_od_priv_proxy_cli_op[] = {
52 	{ OP_OD_PRIV_PROXY_STATUS, BT_MESH_LEN_EXACT(1), handle_proxy_status },
53 
54 	BT_MESH_MODEL_OP_END
55 };
56 
bt_mesh_od_priv_proxy_cli_get(uint16_t net_idx,uint16_t addr,uint8_t * val_rsp)57 int bt_mesh_od_priv_proxy_cli_get(uint16_t net_idx, uint16_t addr, uint8_t *val_rsp)
58 {
59 	struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_DEV(net_idx, addr);
60 	const struct bt_mesh_msg_rsp_ctx rsp = {
61 		.ack = &cli->ack_ctx,
62 		.op = OP_OD_PRIV_PROXY_STATUS,
63 		.user_data = val_rsp,
64 		.timeout = msg_timeout,
65 	};
66 
67 	BT_MESH_MODEL_BUF_DEFINE(msg, OP_OD_PRIV_PROXY_GET, 0);
68 	bt_mesh_model_msg_init(&msg, OP_OD_PRIV_PROXY_GET);
69 
70 	return bt_mesh_msg_ackd_send(cli->model, &ctx, &msg, val_rsp ? &rsp : NULL);
71 }
72 
bt_mesh_od_priv_proxy_cli_set(uint16_t net_idx,uint16_t addr,uint8_t val,uint8_t * val_rsp)73 int bt_mesh_od_priv_proxy_cli_set(uint16_t net_idx, uint16_t addr, uint8_t val, uint8_t *val_rsp)
74 {
75 	struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_DEV(net_idx, addr);
76 	const struct bt_mesh_msg_rsp_ctx rsp = {
77 		.ack = &cli->ack_ctx,
78 		.op = OP_OD_PRIV_PROXY_STATUS,
79 		.user_data = val_rsp,
80 		.timeout = msg_timeout,
81 	};
82 
83 	BT_MESH_MODEL_BUF_DEFINE(msg, OP_OD_PRIV_PROXY_SET, 1);
84 	bt_mesh_model_msg_init(&msg, OP_OD_PRIV_PROXY_SET);
85 
86 	net_buf_simple_add_u8(&msg, val);
87 
88 	return bt_mesh_msg_ackd_send(cli->model, &ctx, &msg, val_rsp ? &rsp : NULL);
89 }
90 
bt_mesh_od_priv_proxy_cli_timeout_set(int32_t timeout)91 void bt_mesh_od_priv_proxy_cli_timeout_set(int32_t timeout)
92 {
93 	msg_timeout = timeout;
94 }
95 
on_demand_proxy_cli_init(const struct bt_mesh_model * mod)96 static int on_demand_proxy_cli_init(const struct bt_mesh_model *mod)
97 {
98 	if (!bt_mesh_model_in_primary(mod)) {
99 		LOG_ERR("On-Demand Private Proxy client not in primary element");
100 		return -EINVAL;
101 	}
102 
103 	cli = mod->rt->user_data;
104 	cli->model = mod;
105 	mod->keys[0] = BT_MESH_KEY_DEV_ANY;
106 	mod->rt->flags |= BT_MESH_MOD_DEVKEY_ONLY;
107 	msg_timeout = CONFIG_BT_MESH_OD_PRIV_PROXY_CLI_TIMEOUT;
108 
109 	bt_mesh_msg_ack_ctx_init(&cli->ack_ctx);
110 	return 0;
111 }
112 
113 const struct bt_mesh_model_cb _bt_mesh_od_priv_proxy_cli_cb = {
114 	.init = on_demand_proxy_cli_init,
115 };
116