-
Notifications
You must be signed in to change notification settings - Fork 1
/
nrfConfigParamsPersistence.cpp
103 lines (92 loc) · 4.29 KB
/
nrfConfigParamsPersistence.cpp
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
/* mbed Microcontroller Library
* Copyright (c) 2006-2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pstorage.h"
#include "nrf_error.h"
#include "ConfigParamsPersistence.h"
/**
* Nordic specific structure used to store params persistently.
* It extends URIBeaconConfigService::Params_t with a persistence signature.
*/
struct PersistentParams_t {
URIBeaconConfigService::Params_t params;
uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected
* magic value in this field indicates persistence. */
static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */
};
/**
* The following is a module-local variable to hold configuration parameters for
* short periods during flash access. This is necessary because the pstorage
* APIs don't copy in the memory provided as data source. The memory cannot be
* freed or reused by the application until this flash access is complete. The
* load and store operations in this module initialize persistentParams and then
* pass it on to the 'pstorage' APIs.
*/
static PersistentParams_t persistentParams;
static pstorage_handle_t pstorageHandle;
/**
* Dummy callback handler needed by Nordic's pstorage module. This is called
* after every flash access.
*/
static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
uint8_t op_code,
uint32_t result,
uint8_t *p_data,
uint32_t data_len)
{
/* APP_ERROR_CHECK(result); */
}
/* Platform-specific implementation for persistence on the nRF5x. Based on the
* pstorage module provided by the Nordic SDK. */
bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP)
{
static bool pstorageInitied = false;
if (!pstorageInitied) {
pstorage_init();
static pstorage_module_param_t pstorageParams = {
.cb = pstorageNotificationCallback,
.block_size = sizeof(PersistentParams_t),
.block_count = 1
};
pstorage_register(&pstorageParams, &pstorageHandle);
pstorageInitied = true;
}
if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
(persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
// On failure zero out and let the service reset to defaults
memset(paramsP, 0, sizeof(URIBeaconConfigService::Params_t));
return false;
}
memcpy(paramsP, &persistentParams.params, sizeof(URIBeaconConfigService::Params_t));
return true;
}
/* Platform-specific implementation for persistence on the nRF5x. Based on the
* pstorage module provided by the Nordic SDK. */
void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP)
{
memcpy(&persistentParams.params, paramsP, sizeof(URIBeaconConfigService::Params_t));
if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
pstorage_store(&pstorageHandle,
reinterpret_cast<uint8_t *>(&persistentParams),
sizeof(PersistentParams_t),
0 /* offset */);
} else {
pstorage_update(&pstorageHandle,
reinterpret_cast<uint8_t *>(&persistentParams),
sizeof(PersistentParams_t),
0 /* offset */);
}
}