jan
Dependencies: BLE_API mbed nRF51822
nrfConfigParamsPersistence.cpp@0:f17fb1bb4f3a, 2015-11-14 (annotated)
- Committer:
- janjongboom
- Date:
- Sat Nov 14 09:39:14 2015 +0000
- Revision:
- 0:f17fb1bb4f3a
jan
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
janjongboom | 0:f17fb1bb4f3a | 1 | /* mbed Microcontroller Library |
janjongboom | 0:f17fb1bb4f3a | 2 | * Copyright (c) 2006-2015 ARM Limited |
janjongboom | 0:f17fb1bb4f3a | 3 | * |
janjongboom | 0:f17fb1bb4f3a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
janjongboom | 0:f17fb1bb4f3a | 5 | * you may not use this file except in compliance with the License. |
janjongboom | 0:f17fb1bb4f3a | 6 | * You may obtain a copy of the License at |
janjongboom | 0:f17fb1bb4f3a | 7 | * |
janjongboom | 0:f17fb1bb4f3a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
janjongboom | 0:f17fb1bb4f3a | 9 | * |
janjongboom | 0:f17fb1bb4f3a | 10 | * Unless required by applicable law or agreed to in writing, software |
janjongboom | 0:f17fb1bb4f3a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
janjongboom | 0:f17fb1bb4f3a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
janjongboom | 0:f17fb1bb4f3a | 13 | * See the License for the specific language governing permissions and |
janjongboom | 0:f17fb1bb4f3a | 14 | * limitations under the License. |
janjongboom | 0:f17fb1bb4f3a | 15 | */ |
janjongboom | 0:f17fb1bb4f3a | 16 | |
janjongboom | 0:f17fb1bb4f3a | 17 | #include "pstorage.h" |
janjongboom | 0:f17fb1bb4f3a | 18 | #include "nrf_error.h" |
janjongboom | 0:f17fb1bb4f3a | 19 | #include "ConfigParamsPersistence.h" |
janjongboom | 0:f17fb1bb4f3a | 20 | |
janjongboom | 0:f17fb1bb4f3a | 21 | /** |
janjongboom | 0:f17fb1bb4f3a | 22 | * Nordic specific structure used to store params persistently. |
janjongboom | 0:f17fb1bb4f3a | 23 | * It extends URIBeaconConfigService::Params_t with a persistence signature. |
janjongboom | 0:f17fb1bb4f3a | 24 | */ |
janjongboom | 0:f17fb1bb4f3a | 25 | struct PersistentParams_t { |
janjongboom | 0:f17fb1bb4f3a | 26 | URIBeaconConfigService::Params_t params; |
janjongboom | 0:f17fb1bb4f3a | 27 | uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected |
janjongboom | 0:f17fb1bb4f3a | 28 | * magic value in this field indicates persistence. */ |
janjongboom | 0:f17fb1bb4f3a | 29 | |
janjongboom | 0:f17fb1bb4f3a | 30 | static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */ |
janjongboom | 0:f17fb1bb4f3a | 31 | }; |
janjongboom | 0:f17fb1bb4f3a | 32 | |
janjongboom | 0:f17fb1bb4f3a | 33 | /** |
janjongboom | 0:f17fb1bb4f3a | 34 | * The following is a module-local variable to hold configuration parameters for |
janjongboom | 0:f17fb1bb4f3a | 35 | * short periods during flash access. This is necessary because the pstorage |
janjongboom | 0:f17fb1bb4f3a | 36 | * APIs don't copy in the memory provided as data source. The memory cannot be |
janjongboom | 0:f17fb1bb4f3a | 37 | * freed or reused by the application until this flash access is complete. The |
janjongboom | 0:f17fb1bb4f3a | 38 | * load and store operations in this module initialize persistentParams and then |
janjongboom | 0:f17fb1bb4f3a | 39 | * pass it on to the 'pstorage' APIs. |
janjongboom | 0:f17fb1bb4f3a | 40 | */ |
janjongboom | 0:f17fb1bb4f3a | 41 | static PersistentParams_t persistentParams; |
janjongboom | 0:f17fb1bb4f3a | 42 | |
janjongboom | 0:f17fb1bb4f3a | 43 | static pstorage_handle_t pstorageHandle; |
janjongboom | 0:f17fb1bb4f3a | 44 | |
janjongboom | 0:f17fb1bb4f3a | 45 | /** |
janjongboom | 0:f17fb1bb4f3a | 46 | * Dummy callback handler needed by Nordic's pstorage module. This is called |
janjongboom | 0:f17fb1bb4f3a | 47 | * after every flash access. |
janjongboom | 0:f17fb1bb4f3a | 48 | */ |
janjongboom | 0:f17fb1bb4f3a | 49 | static void pstorageNotificationCallback(pstorage_handle_t *p_handle, |
janjongboom | 0:f17fb1bb4f3a | 50 | uint8_t op_code, |
janjongboom | 0:f17fb1bb4f3a | 51 | uint32_t result, |
janjongboom | 0:f17fb1bb4f3a | 52 | uint8_t *p_data, |
janjongboom | 0:f17fb1bb4f3a | 53 | uint32_t data_len) |
janjongboom | 0:f17fb1bb4f3a | 54 | { |
janjongboom | 0:f17fb1bb4f3a | 55 | /* APP_ERROR_CHECK(result); */ |
janjongboom | 0:f17fb1bb4f3a | 56 | } |
janjongboom | 0:f17fb1bb4f3a | 57 | |
janjongboom | 0:f17fb1bb4f3a | 58 | /* Platform-specific implementation for persistence on the nRF5x. Based on the |
janjongboom | 0:f17fb1bb4f3a | 59 | * pstorage module provided by the Nordic SDK. */ |
janjongboom | 0:f17fb1bb4f3a | 60 | bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP) |
janjongboom | 0:f17fb1bb4f3a | 61 | { |
janjongboom | 0:f17fb1bb4f3a | 62 | static bool pstorageInitied = false; |
janjongboom | 0:f17fb1bb4f3a | 63 | if (!pstorageInitied) { |
janjongboom | 0:f17fb1bb4f3a | 64 | pstorage_init(); |
janjongboom | 0:f17fb1bb4f3a | 65 | |
janjongboom | 0:f17fb1bb4f3a | 66 | static pstorage_module_param_t pstorageParams = { |
janjongboom | 0:f17fb1bb4f3a | 67 | .cb = pstorageNotificationCallback, |
janjongboom | 0:f17fb1bb4f3a | 68 | .block_size = sizeof(PersistentParams_t), |
janjongboom | 0:f17fb1bb4f3a | 69 | .block_count = 1 |
janjongboom | 0:f17fb1bb4f3a | 70 | }; |
janjongboom | 0:f17fb1bb4f3a | 71 | pstorage_register(&pstorageParams, &pstorageHandle); |
janjongboom | 0:f17fb1bb4f3a | 72 | pstorageInitied = true; |
janjongboom | 0:f17fb1bb4f3a | 73 | } |
janjongboom | 0:f17fb1bb4f3a | 74 | |
janjongboom | 0:f17fb1bb4f3a | 75 | if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) || |
janjongboom | 0:f17fb1bb4f3a | 76 | (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) { |
janjongboom | 0:f17fb1bb4f3a | 77 | // On failure zero out and let the service reset to defaults |
janjongboom | 0:f17fb1bb4f3a | 78 | memset(paramsP, 0, sizeof(URIBeaconConfigService::Params_t)); |
janjongboom | 0:f17fb1bb4f3a | 79 | return false; |
janjongboom | 0:f17fb1bb4f3a | 80 | } |
janjongboom | 0:f17fb1bb4f3a | 81 | |
janjongboom | 0:f17fb1bb4f3a | 82 | memcpy(paramsP, &persistentParams.params, sizeof(URIBeaconConfigService::Params_t)); |
janjongboom | 0:f17fb1bb4f3a | 83 | return true; |
janjongboom | 0:f17fb1bb4f3a | 84 | } |
janjongboom | 0:f17fb1bb4f3a | 85 | |
janjongboom | 0:f17fb1bb4f3a | 86 | /* Platform-specific implementation for persistence on the nRF5x. Based on the |
janjongboom | 0:f17fb1bb4f3a | 87 | * pstorage module provided by the Nordic SDK. */ |
janjongboom | 0:f17fb1bb4f3a | 88 | void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP) |
janjongboom | 0:f17fb1bb4f3a | 89 | { |
janjongboom | 0:f17fb1bb4f3a | 90 | memcpy(&persistentParams.params, paramsP, sizeof(URIBeaconConfigService::Params_t)); |
janjongboom | 0:f17fb1bb4f3a | 91 | if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) { |
janjongboom | 0:f17fb1bb4f3a | 92 | persistentParams.persistenceSignature = PersistentParams_t::MAGIC; |
janjongboom | 0:f17fb1bb4f3a | 93 | pstorage_store(&pstorageHandle, |
janjongboom | 0:f17fb1bb4f3a | 94 | reinterpret_cast<uint8_t *>(&persistentParams), |
janjongboom | 0:f17fb1bb4f3a | 95 | sizeof(PersistentParams_t), |
janjongboom | 0:f17fb1bb4f3a | 96 | 0 /* offset */); |
janjongboom | 0:f17fb1bb4f3a | 97 | } else { |
janjongboom | 0:f17fb1bb4f3a | 98 | pstorage_update(&pstorageHandle, |
janjongboom | 0:f17fb1bb4f3a | 99 | reinterpret_cast<uint8_t *>(&persistentParams), |
janjongboom | 0:f17fb1bb4f3a | 100 | sizeof(PersistentParams_t), |
janjongboom | 0:f17fb1bb4f3a | 101 | 0 /* offset */); |
janjongboom | 0:f17fb1bb4f3a | 102 | } |
janjongboom | 0:f17fb1bb4f3a | 103 | } |