WXK
2024-12-16 9201a33e45484b3247271759c91c158063baccac
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
/*
 * Copyright (c) 2018 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
#include "nimble_syscfg.h"
#define MESH_LOG_MODULE BLE_MESH_SETTINGS_LOG
 
#if MYNEWT_VAL(BLE_MESH_SETTINGS)
 
#include "mesh_priv.h"
#include "mesh/glue.h"
#include "subnet.h"
#include "app_keys.h"
#include "net.h"
#include "cdb_priv.h"
#include "rpl.h"
#include "crypto.h"
#include "transport.h"
#include "heartbeat.h"
#include "access.h"
#include "pb_gatt_srv.h"
#include "proxy.h"
#include "settings.h"
#include "cfg.h"
 
 
#include "config/config.h"
 
static struct k_work_delayable pending_store;
static ATOMIC_DEFINE(pending_flags, BT_MESH_SETTINGS_FLAG_COUNT);
 
int settings_name_next(char *name, char **next)
{
    int rc = 0;
 
    if (next) {
        *next = NULL;
    }
 
    if (!name) {
        return 0;
    }
 
    /* name might come from flash directly, in flash the name would end
     * with '=' or '\0' depending how storage is done. Flash reading is
     * limited to what can be read
     */
    while ((*name != '\0') && (*name != '=') &&
           (*name != '/')) {
        rc++;
        name++;
    }
 
    if (*name == '/') {
        if (next) {
            *next = name + 1;
        }
        return rc;
    }
 
    return rc;
}
 
static int mesh_commit(void)
{
    if (!bt_mesh_subnet_next(NULL)) {
        /* Nothing to do since we're not yet provisioned */
        return 0;
    }
 
    if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
        (void)bt_mesh_pb_gatt_disable();
    }
 
    bt_mesh_net_settings_commit();
    bt_mesh_model_settings_commit();
 
    atomic_set_bit(bt_mesh.flags, BT_MESH_VALID);
 
    bt_mesh_start();
    return 0;
}
 
/* Pending flags that use K_NO_WAIT as the storage timeout */
#define NO_WAIT_PENDING_BITS (BIT(BT_MESH_SETTINGS_NET_PENDING) |           \
            BIT(BT_MESH_SETTINGS_IV_PENDING)  |           \
            BIT(BT_MESH_SETTINGS_SEQ_PENDING) |           \
            BIT(BT_MESH_SETTINGS_CDB_PENDING))
 
/* Pending flags that use CONFIG_BT_MESH_STORE_TIMEOUT */
#define GENERIC_PENDING_BITS (BIT(BT_MESH_SETTINGS_NET_KEYS_PENDING) |      \
            BIT(BT_MESH_SETTINGS_APP_KEYS_PENDING) |      \
            BIT(BT_MESH_SETTINGS_HB_PUB_PENDING)   |      \
            BIT(BT_MESH_SETTINGS_CFG_PENDING)      |      \
            BIT(BT_MESH_SETTINGS_MOD_PENDING)      |      \
            BIT(BT_MESH_SETTINGS_VA_PENDING))
 
void bt_mesh_settings_store_schedule(enum bt_mesh_settings_flag flag)
{
    int32_t timeout_ms, remaining_ms;
 
    atomic_set_bit(pending_flags, flag);
 
    if (atomic_get(pending_flags) & NO_WAIT_PENDING_BITS) {
        timeout_ms = 0;
    } else if (CONFIG_BT_MESH_RPL_STORE_TIMEOUT >= 0 &&
           atomic_test_bit(pending_flags, BT_MESH_SETTINGS_RPL_PENDING) &&
           !(atomic_get(pending_flags) & GENERIC_PENDING_BITS)) {
        timeout_ms = CONFIG_BT_MESH_RPL_STORE_TIMEOUT * MSEC_PER_SEC;
    } else {
        timeout_ms = CONFIG_BT_MESH_STORE_TIMEOUT * MSEC_PER_SEC;
    }
 
    remaining_ms = k_ticks_to_ms_floor32(
        k_work_delayable_remaining_get(&pending_store));
    BT_DBG("Waiting %u ms vs rem %u ms", timeout_ms, remaining_ms);
    /* If the new deadline is sooner, override any existing
     * deadline; otherwise schedule without changing any existing
     * deadline.
     */
    if (timeout_ms < remaining_ms) {
        k_work_reschedule(&pending_store, K_MSEC(timeout_ms));
    } else {
        k_work_schedule(&pending_store, K_MSEC(timeout_ms));
    }
}
 
void bt_mesh_settings_store_cancel(enum bt_mesh_settings_flag flag)
{
    atomic_clear_bit(pending_flags, flag);
}
 
static void store_pending(struct ble_npl_event *work)
{
    BT_DBG("");
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_RPL_PENDING)) {
        bt_mesh_rpl_pending_store(BT_MESH_ADDR_ALL_NODES);
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_NET_KEYS_PENDING)) {
        bt_mesh_subnet_pending_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_APP_KEYS_PENDING)) {
        bt_mesh_app_key_pending_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_NET_PENDING)) {
        bt_mesh_net_pending_net_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_IV_PENDING)) {
        bt_mesh_net_pending_iv_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_SEQ_PENDING)) {
        bt_mesh_net_pending_seq_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_HB_PUB_PENDING)) {
        bt_mesh_hb_pub_pending_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_CFG_PENDING)) {
        bt_mesh_cfg_pending_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_MOD_PENDING)) {
        bt_mesh_model_pending_store();
    }
 
    if (atomic_test_and_clear_bit(pending_flags,
                      BT_MESH_SETTINGS_VA_PENDING)) {
        bt_mesh_va_pending_store();
    }
 
#if IS_ENABLED(CONFIG_BT_MESH_CDB)
    if (atomic_test_and_clear_bit(pending_flags,
                     BT_MESH_SETTINGS_CDB_PENDING)) {
        bt_mesh_cdb_pending_store();
    }
#endif
}
 
static struct conf_handler bt_mesh_settings_conf_handler = {
    .ch_name = "bt_mesh",
    .ch_get = NULL,
    .ch_set = NULL,
    .ch_commit = mesh_commit,
    .ch_export = NULL,
};
 
void bt_mesh_settings_init(void)
{
    int rc;
 
    rc = conf_register(&bt_mesh_settings_conf_handler);
 
    SYSINIT_PANIC_ASSERT_MSG(rc == 0,
                 "Failed to register bt_mesh_settings conf");
 
    k_work_init_delayable(&pending_store, store_pending);
}
 
#endif /* MYNEWT_VAL(BLE_MESH_SETTINGS) */