WXK
2024-12-16 78e84fcf264afd731cd66c807d9fcb690fe12126
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
#include "mesh/mesh.h"
#include "mesh_priv.h"
#include "net.h"
#include "rpl.h"
#include "beacon.h"
#include "settings.h"
#include "heartbeat.h"
#include "friend.h"
#include "cfg.h"
#include "mesh/glue.h"
 
#define MESH_LOG_MODULE BLE_MESH_LOG
#include "log/log.h"
 
/* Miscellaneous configuration server model states */
struct cfg_val {
    uint8_t net_transmit;
    uint8_t relay;
    uint8_t relay_retransmit;
    uint8_t beacon;
    uint8_t gatt_proxy;
    uint8_t frnd;
    uint8_t default_ttl;
};
 
void bt_mesh_beacon_set(bool beacon)
{
    if (atomic_test_bit(bt_mesh.flags, BT_MESH_BEACON) == beacon) {
        return;
    }
 
    atomic_set_bit_to(bt_mesh.flags, BT_MESH_BEACON, beacon);
 
    if (beacon) {
        bt_mesh_beacon_enable();
    } else {
        bt_mesh_beacon_disable();
    }
 
    if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
        atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
    }
}
 
bool bt_mesh_beacon_enabled(void)
{
    return atomic_test_bit(bt_mesh.flags, BT_MESH_BEACON);
}
 
static int feature_set(int feature_flag, enum bt_mesh_feat_state state)
{
    if (state != BT_MESH_FEATURE_DISABLED &&
        state != BT_MESH_FEATURE_ENABLED) {
        return -EINVAL;
    }
 
    if (atomic_test_bit(bt_mesh.flags, feature_flag) ==
        (state == BT_MESH_FEATURE_ENABLED)) {
        return -EALREADY;
    }
 
    atomic_set_bit_to(bt_mesh.flags, feature_flag,
              (state == BT_MESH_FEATURE_ENABLED));
 
    return 0;
}
 
static enum bt_mesh_feat_state feature_get(int feature_flag)
{
    return atomic_test_bit(bt_mesh.flags, feature_flag) ?
               BT_MESH_FEATURE_ENABLED :
               BT_MESH_FEATURE_DISABLED;
}
 
int bt_mesh_gatt_proxy_set(enum bt_mesh_feat_state gatt_proxy)
{
    int err;
 
    if (!IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
        return -ENOTSUP;
    }
 
    err = feature_set(BT_MESH_GATT_PROXY, gatt_proxy);
    if (err) {
        return err;
    }
 
    bt_mesh_hb_feature_changed(BT_MESH_FEAT_PROXY);
 
    if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
        atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
    }
 
    return 0;
}
 
enum bt_mesh_feat_state bt_mesh_gatt_proxy_get(void)
{
    if (!IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
        return BT_MESH_FEATURE_NOT_SUPPORTED;
    }
 
    return feature_get(BT_MESH_GATT_PROXY);
}
 
int bt_mesh_default_ttl_set(uint8_t default_ttl)
{
    if (default_ttl == 1 || default_ttl > BT_MESH_TTL_MAX) {
        return -EINVAL;
    }
 
    if (default_ttl == bt_mesh.default_ttl) {
        return 0;
    }
 
    bt_mesh.default_ttl = default_ttl;
 
    if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
        atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
    }
 
    return 0;
}
 
uint8_t bt_mesh_default_ttl_get(void)
{
    return bt_mesh.default_ttl;
}
 
int bt_mesh_friend_set(enum bt_mesh_feat_state friendship)
{
    int err;
 
    if (!IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
        return -ENOTSUP;
    }
 
    err = feature_set(BT_MESH_FRIEND, friendship);
    if (err) {
        return err;
    }
 
    bt_mesh_hb_feature_changed(BT_MESH_FEAT_FRIEND);
 
    if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
        atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
    }
 
    if (friendship == BT_MESH_FEATURE_DISABLED) {
        bt_mesh_friends_clear();
    }
 
    return 0;
}
 
enum bt_mesh_feat_state bt_mesh_friend_get(void)
{
    if (!IS_ENABLED(CONFIG_BT_MESH_FRIEND)) {
        return BT_MESH_FEATURE_NOT_SUPPORTED;
    }
 
    return feature_get(BT_MESH_FRIEND);
}
 
void bt_mesh_net_transmit_set(uint8_t xmit)
{
    if (bt_mesh.net_xmit == xmit) {
        return;
    }
 
    bt_mesh.net_xmit = xmit;
 
    if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
        atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
    }
}
 
uint8_t bt_mesh_net_transmit_get(void)
{
    return bt_mesh.net_xmit;
}
 
int bt_mesh_relay_set(enum bt_mesh_feat_state relay, uint8_t xmit)
{
    int err;
 
    if (!CONFIG_BT_MESH_RELAY) {
        return -ENOTSUP;
    }
 
    err = feature_set(BT_MESH_RELAY, relay);
    if (err == -EINVAL) {
        return err;
    }
 
    if (err == -EALREADY && bt_mesh.relay_xmit == xmit) {
        return -EALREADY;
    }
 
    bt_mesh.relay_xmit = xmit;
    bt_mesh_hb_feature_changed(BT_MESH_FEAT_RELAY);
 
    if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
        atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        bt_mesh_settings_store_schedule(BT_MESH_SETTINGS_CFG_PENDING);
    }
 
    return 0;
}
 
enum bt_mesh_feat_state bt_mesh_relay_get(void)
{
    return feature_get(BT_MESH_RELAY);
}
 
uint8_t bt_mesh_relay_retransmit_get(void)
{
    if (!CONFIG_BT_MESH_RELAY) {
        return 0;
    }
 
    return bt_mesh.relay_xmit;
}
 
bool bt_mesh_fixed_group_match(uint16_t addr)
{
    /* Check for fixed group addresses */
    switch (addr) {
    case BT_MESH_ADDR_ALL_NODES:
        return true;
    case BT_MESH_ADDR_PROXIES:
        return (bt_mesh_gatt_proxy_get() == BT_MESH_FEATURE_ENABLED);
    case BT_MESH_ADDR_FRIENDS:
        return (bt_mesh_friend_get() == BT_MESH_FEATURE_ENABLED);
    case BT_MESH_ADDR_RELAYS:
        return (bt_mesh_relay_get() == BT_MESH_FEATURE_ENABLED);
    default:
        return false;
    }
}
 
#if MYNEWT_VAL(BLE_MESH_SETTINGS)
static int cfg_set(int argc, char **argv, char *val)
{
    struct cfg_val cfg;
    int len, err;
 
    BT_DBG("val %s", val ? val : "(null)");
 
    if (!val) {
        BT_DBG("Cleared configuration state");
        return 0;
    }
 
    len = sizeof(cfg);
    err = settings_bytes_from_str(val, &cfg, &len);
    if (err) {
        BT_ERR("Failed to decode value %s (err %d)", val, err);
        return err;
    }
 
    if (len != sizeof(cfg)) {
        BT_ERR("Unexpected value length (%d != %zu)", len,
                           sizeof(cfg));
        return -EINVAL;
    }
 
    bt_mesh_net_transmit_set(cfg.net_transmit);
    bt_mesh_relay_set(cfg.relay, cfg.relay_retransmit);
    bt_mesh_beacon_set(cfg.beacon);
    bt_mesh_gatt_proxy_set(cfg.gatt_proxy);
    bt_mesh_friend_set(cfg.frnd);
    bt_mesh_default_ttl_set(cfg.default_ttl);
 
    BT_DBG("Restored configuration state");
 
    return 0;
}
 
static void clear_cfg(void)
{
    int err;
 
    err = settings_save_one("bt_mesh/Cfg", NULL);
    if (err) {
        BT_ERR("Failed to clear configuration");
    } else {
        BT_DBG("Cleared configuration");
    }
}
 
static void store_pending_cfg(void)
{
    char buf[BT_SETTINGS_SIZE(sizeof(struct cfg_val))];
    struct cfg_val val;
    char *str;
    int err;
 
    val.net_transmit = bt_mesh_net_transmit_get();
    val.relay = bt_mesh_relay_get();
    val.relay_retransmit = bt_mesh_relay_retransmit_get();
    val.beacon = bt_mesh_beacon_enabled();
    val.gatt_proxy = bt_mesh_gatt_proxy_get();
    val.frnd = bt_mesh_friend_get();
    val.default_ttl = bt_mesh_default_ttl_get();
 
    str = settings_str_from_bytes(&val, sizeof(val), buf, sizeof(buf));
    if (!str) {
        BT_ERR("Unable to encode configuration as value");
        return;
    }
 
    BT_DBG("Saving configuration as value %s", str);
    err = settings_save_one("bt_mesh/Cfg", str);
    if (err) {
        BT_ERR("Failed to store configuration");
    } else {
        BT_DBG("Stored configuration");
    }
}
 
void bt_mesh_cfg_pending_store(void)
{
    if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
        store_pending_cfg();
    } else {
        clear_cfg();
    }
}
 
static struct conf_handler bt_mesh_cfg_conf_handler = {
    .ch_name = "bt_mesh",
    .ch_get = NULL,
    .ch_set = cfg_set,
    .ch_commit = NULL,
    .ch_export = NULL,
};
#endif
 
void bt_mesh_cfg_default_set(void)
{
#if MYNEWT_VAL(BLE_MESH_SETTINGS)
    int rc;
 
    rc = conf_register(&bt_mesh_cfg_conf_handler);
 
    SYSINIT_PANIC_ASSERT_MSG(rc == 0,
                 "Failed to register bt_mesh_settings conf");
#endif
 
    bt_mesh.default_ttl = CONFIG_BT_MESH_DEFAULT_TTL;
    bt_mesh.net_xmit =
        BT_MESH_TRANSMIT(CONFIG_BT_MESH_NETWORK_TRANSMIT_COUNT,
                 CONFIG_BT_MESH_NETWORK_TRANSMIT_INTERVAL);
 
#if defined(CONFIG_BT_MESH_RELAY)
    bt_mesh.relay_xmit =
        BT_MESH_TRANSMIT(CONFIG_BT_MESH_RELAY_RETRANSMIT_COUNT,
                 CONFIG_BT_MESH_RELAY_RETRANSMIT_INTERVAL);
#endif
 
    if (CONFIG_BT_MESH_RELAY_ENABLED) {
        atomic_set_bit(bt_mesh.flags, BT_MESH_RELAY);
    }
 
    if (CONFIG_BT_MESH_BEACON_ENABLED) {
        atomic_set_bit(bt_mesh.flags, BT_MESH_BEACON);
    }
 
    if (CONFIG_BT_MESH_GATT_PROXY_ENABLED) {
        atomic_set_bit(bt_mesh.flags, BT_MESH_GATT_PROXY);
    }
 
    if (CONFIG_BT_MESH_FRIEND_ENABLED) {
        atomic_set_bit(bt_mesh.flags, BT_MESH_FRIEND);
    }
}