Skip to content

Commit 62db91e

Browse files
committed
Bluetooth: Mesh: Move bt_mesh_proxy_role to proxy_msg.c
Mesh bt_mesh_proxy_role structure to proxy_msg.c Signed-off-by: Lingao Meng <[email protected]>
1 parent 6f4b18b commit 62db91e

File tree

4 files changed

+83
-96
lines changed

4 files changed

+83
-96
lines changed

subsys/bluetooth/mesh/pb_gatt_srv.c

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,14 @@
3434

3535
static bool prov_fast_adv;
3636

37-
static void proxy_complete_pdu(struct bt_mesh_proxy_role *role);
3837
static int gatt_send(struct bt_conn *conn,
3938
const void *data, uint16_t len,
4039
bt_gatt_complete_func_t end, void *user_data);
4140

42-
static struct bt_mesh_proxy_role cli = {
43-
.cb = {
44-
.send = gatt_send,
45-
.recv = proxy_complete_pdu,
46-
},
47-
};
48-
41+
static struct bt_mesh_proxy_role *cli;
4942
static bool service_registered;
5043

51-
static void proxy_complete_pdu(struct bt_mesh_proxy_role *role)
44+
static void proxy_msg_recv(struct bt_mesh_proxy_role *role)
5245
{
5346
switch (role->msg_type) {
5447
case BT_MESH_PROXY_PROV:
@@ -68,7 +61,7 @@ static ssize_t gatt_recv(struct bt_conn *conn,
6861
{
6962
const uint8_t *data = buf;
7063

71-
if (cli.conn != conn) {
64+
if (cli->conn != conn) {
7265
BT_ERR("No PB-GATT Client found");
7366
return -ENOTCONN;
7467
}
@@ -83,7 +76,7 @@ static ssize_t gatt_recv(struct bt_conn *conn,
8376
return -EINVAL;
8477
}
8578

86-
return bt_mesh_proxy_msg_recv(&cli, buf, len);
79+
return bt_mesh_proxy_msg_recv(cli, buf, len);
8780
}
8881

8982
static void gatt_connected(struct bt_conn *conn, uint8_t err)
@@ -96,11 +89,9 @@ static void gatt_connected(struct bt_conn *conn, uint8_t err)
9689
return;
9790
}
9891

99-
cli.conn = bt_conn_ref(conn);
92+
cli = bt_mesh_proxy_role_setup(conn, gatt_send, proxy_msg_recv);
10093

10194
BT_DBG("conn %p err 0x%02x", (void *)conn, err);
102-
103-
bt_mesh_proxy_msg_init(&cli);
10495
}
10596

10697
static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
@@ -113,27 +104,19 @@ static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
113104
return;
114105
}
115106

116-
if (cli.conn != conn) {
117-
BT_WARN("No PB-GATT Client found");
118-
return;
119-
}
120-
121-
BT_DBG("conn %p reason 0x%02x", (void *)conn, reason);
107+
cli = NULL;
122108

123109
bt_mesh_pb_gatt_close(conn);
124110

125-
if (bt_mesh_is_provisioned()) {
126-
(void)bt_mesh_pb_gatt_disable();
127-
128-
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
129-
(void)bt_mesh_proxy_gatt_enable();
130-
}
111+
if (!bt_mesh_is_provisioned()) {
112+
return;
131113
}
132114

133-
bt_conn_unref(cli.conn);
134-
cli.conn = NULL;
115+
(void)bt_mesh_pb_gatt_disable();
135116

136-
bt_mesh_adv_update();
117+
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
118+
(void)bt_mesh_proxy_gatt_enable();
119+
}
137120
}
138121

139122
static void prov_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
@@ -144,7 +127,7 @@ static void prov_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
144127
static ssize_t prov_ccc_write(struct bt_conn *conn,
145128
const struct bt_gatt_attr *attr, uint16_t value)
146129
{
147-
if (cli.conn != conn) {
130+
if (cli->conn != conn) {
148131
BT_ERR("No PB-GATT Client found");
149132
return -ENOTCONN;
150133
}
@@ -231,12 +214,12 @@ static const struct bt_data prov_ad[] = {
231214
int bt_mesh_pb_gatt_send(struct bt_conn *conn, struct net_buf_simple *buf,
232215
bt_gatt_complete_func_t end, void *user_data)
233216
{
234-
if (cli.conn != conn) {
217+
if (!cli || cli->conn != conn) {
235218
BT_ERR("No PB-GATT Client found");
236219
return -ENOTCONN;
237220
}
238221

239-
return bt_mesh_proxy_msg_send(&cli, BT_MESH_PROXY_PROV, buf, end, user_data);
222+
return bt_mesh_proxy_msg_send(cli, BT_MESH_PROXY_PROV, buf, end, user_data);
240223
}
241224

242225
static size_t gatt_prov_adv_create(struct bt_data prov_sd[1])

subsys/bluetooth/mesh/proxy_msg.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
static uint8_t __noinit bufs[PROXY_MSG_FIRST_BUF_LEN +
6161
((CONFIG_BT_MAX_CONN - 1) * PROXY_BUF_LEN_MAX)];
6262

63+
static struct bt_mesh_proxy_role roles[CONFIG_BT_MAX_CONN];
64+
6365
static void proxy_sar_timeout(struct k_work *work)
6466
{
6567
struct bt_mesh_proxy_role *role;
@@ -190,7 +192,7 @@ int bt_mesh_proxy_msg_send(struct bt_mesh_proxy_role *role, uint8_t type,
190192
return 0;
191193
}
192194

193-
void bt_mesh_proxy_msg_init(struct bt_mesh_proxy_role *role)
195+
static void proxy_msg_init(struct bt_mesh_proxy_role *role)
194196
{
195197
uint8_t i, len;
196198
uint8_t *buf;
@@ -218,3 +220,42 @@ void bt_mesh_proxy_msg_init(struct bt_mesh_proxy_role *role)
218220

219221
k_work_init_delayable(&role->sar_timer, proxy_sar_timeout);
220222
}
223+
224+
struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(struct bt_conn *conn,
225+
proxy_send_cb_t send,
226+
proxy_recv_cb_t recv)
227+
{
228+
struct bt_mesh_proxy_role *role;
229+
230+
role = &roles[bt_conn_index(conn)];
231+
232+
role->conn = bt_conn_ref(conn);
233+
proxy_msg_init(role);
234+
235+
role->cb.recv = recv;
236+
role->cb.send = send;
237+
238+
return role;
239+
}
240+
241+
static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
242+
{
243+
struct bt_mesh_proxy_role *role;
244+
245+
BT_DBG("conn %p reason 0x%02x", (void *)conn, reason);
246+
247+
role = &roles[bt_conn_index(conn)];
248+
249+
/* If this fails, the work handler exits early, as
250+
* there's no active connection.
251+
*/
252+
(void)k_work_cancel_delayable(&role->sar_timer);
253+
bt_conn_unref(role->conn);
254+
role->conn = NULL;
255+
256+
bt_mesh_adv_update();
257+
}
258+
259+
BT_CONN_CB_DEFINE(conn_callbacks) = {
260+
.disconnected = gatt_disconnected,
261+
};

subsys/bluetooth/mesh/proxy_msg.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ ssize_t bt_mesh_proxy_msg_recv(struct bt_mesh_proxy_role *role,
5151
int bt_mesh_proxy_msg_send(struct bt_mesh_proxy_role *role, uint8_t type,
5252
struct net_buf_simple *msg,
5353
bt_gatt_complete_func_t end, void *user_data);
54-
void bt_mesh_proxy_msg_init(struct bt_mesh_proxy_role *role);
54+
struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(struct bt_conn *conn,
55+
proxy_send_cb_t send,
56+
proxy_recv_cb_t recv);
5557

5658
#endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_PROXY_MSG_H_ */

0 commit comments

Comments
 (0)