Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
source/PersistentStorageHelper/nrfPersistentStorageHelper/nrfConfigParamsPersistence.cpp@0:1c7da5f83647, 2016-11-29 (annotated)
- Committer:
- sarahmarshy
- Date:
- Tue Nov 29 06:29:10 2016 +0000
- Revision:
- 0:1c7da5f83647
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sarahmarshy | 0:1c7da5f83647 | 1 | /* mbed Microcontroller Library |
| sarahmarshy | 0:1c7da5f83647 | 2 | * Copyright (c) 2006-2015 ARM Limited |
| sarahmarshy | 0:1c7da5f83647 | 3 | * |
| sarahmarshy | 0:1c7da5f83647 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| sarahmarshy | 0:1c7da5f83647 | 5 | * you may not use this file except in compliance with the License. |
| sarahmarshy | 0:1c7da5f83647 | 6 | * You may obtain a copy of the License at |
| sarahmarshy | 0:1c7da5f83647 | 7 | * |
| sarahmarshy | 0:1c7da5f83647 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| sarahmarshy | 0:1c7da5f83647 | 9 | * |
| sarahmarshy | 0:1c7da5f83647 | 10 | * Unless required by applicable law or agreed to in writing, software |
| sarahmarshy | 0:1c7da5f83647 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| sarahmarshy | 0:1c7da5f83647 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| sarahmarshy | 0:1c7da5f83647 | 13 | * See the License for the specific language governing permissions and |
| sarahmarshy | 0:1c7da5f83647 | 14 | * limitations under the License. |
| sarahmarshy | 0:1c7da5f83647 | 15 | */ |
| sarahmarshy | 0:1c7da5f83647 | 16 | |
| sarahmarshy | 0:1c7da5f83647 | 17 | #if defined(TARGET_NRF51822) || defined(TARGET_NRF52832) /* Persistent storage supported on nrf51 platforms */ |
| sarahmarshy | 0:1c7da5f83647 | 18 | |
| sarahmarshy | 0:1c7da5f83647 | 19 | extern "C" { |
| sarahmarshy | 0:1c7da5f83647 | 20 | #include "pstorage.h" |
| sarahmarshy | 0:1c7da5f83647 | 21 | } |
| sarahmarshy | 0:1c7da5f83647 | 22 | |
| sarahmarshy | 0:1c7da5f83647 | 23 | #include "nrf_error.h" |
| sarahmarshy | 0:1c7da5f83647 | 24 | #include "../../EddystoneService.h" |
| sarahmarshy | 0:1c7da5f83647 | 25 | #include <cstddef> |
| sarahmarshy | 0:1c7da5f83647 | 26 | |
| sarahmarshy | 0:1c7da5f83647 | 27 | /** |
| sarahmarshy | 0:1c7da5f83647 | 28 | * Nordic specific structure used to store params persistently. |
| sarahmarshy | 0:1c7da5f83647 | 29 | * It extends EddystoneService::EddystoneParams_t with a persistence signature. |
| sarahmarshy | 0:1c7da5f83647 | 30 | */ |
| sarahmarshy | 0:1c7da5f83647 | 31 | struct PersistentParams_t { |
| sarahmarshy | 0:1c7da5f83647 | 32 | EddystoneService::EddystoneParams_t params; |
| sarahmarshy | 0:1c7da5f83647 | 33 | uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected |
| sarahmarshy | 0:1c7da5f83647 | 34 | * magic value in this field indicates persistence. */ |
| sarahmarshy | 0:1c7da5f83647 | 35 | |
| sarahmarshy | 0:1c7da5f83647 | 36 | static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */ |
| sarahmarshy | 0:1c7da5f83647 | 37 | }; |
| sarahmarshy | 0:1c7da5f83647 | 38 | |
| sarahmarshy | 0:1c7da5f83647 | 39 | /** |
| sarahmarshy | 0:1c7da5f83647 | 40 | * The following is a module-local variable to hold configuration parameters for |
| sarahmarshy | 0:1c7da5f83647 | 41 | * short periods during flash access. This is necessary because the pstorage |
| sarahmarshy | 0:1c7da5f83647 | 42 | * APIs don't copy in the memory provided as data source. The memory cannot be |
| sarahmarshy | 0:1c7da5f83647 | 43 | * freed or reused by the application until this flash access is complete. The |
| sarahmarshy | 0:1c7da5f83647 | 44 | * load and store operations in this module initialize persistentParams and then |
| sarahmarshy | 0:1c7da5f83647 | 45 | * pass it on to the 'pstorage' APIs. |
| sarahmarshy | 0:1c7da5f83647 | 46 | */ |
| sarahmarshy | 0:1c7da5f83647 | 47 | static PersistentParams_t persistentParams; |
| sarahmarshy | 0:1c7da5f83647 | 48 | |
| sarahmarshy | 0:1c7da5f83647 | 49 | static pstorage_handle_t pstorageHandle; |
| sarahmarshy | 0:1c7da5f83647 | 50 | |
| sarahmarshy | 0:1c7da5f83647 | 51 | /** |
| sarahmarshy | 0:1c7da5f83647 | 52 | * Dummy callback handler needed by Nordic's pstorage module. This is called |
| sarahmarshy | 0:1c7da5f83647 | 53 | * after every flash access. |
| sarahmarshy | 0:1c7da5f83647 | 54 | */ |
| sarahmarshy | 0:1c7da5f83647 | 55 | static void pstorageNotificationCallback(pstorage_handle_t *p_handle, |
| sarahmarshy | 0:1c7da5f83647 | 56 | uint8_t op_code, |
| sarahmarshy | 0:1c7da5f83647 | 57 | uint32_t result, |
| sarahmarshy | 0:1c7da5f83647 | 58 | uint8_t *p_data, |
| sarahmarshy | 0:1c7da5f83647 | 59 | uint32_t data_len) |
| sarahmarshy | 0:1c7da5f83647 | 60 | { |
| sarahmarshy | 0:1c7da5f83647 | 61 | /* Supress compiler warnings */ |
| sarahmarshy | 0:1c7da5f83647 | 62 | (void) p_handle; |
| sarahmarshy | 0:1c7da5f83647 | 63 | (void) op_code; |
| sarahmarshy | 0:1c7da5f83647 | 64 | (void) result; |
| sarahmarshy | 0:1c7da5f83647 | 65 | (void) p_data; |
| sarahmarshy | 0:1c7da5f83647 | 66 | (void) data_len; |
| sarahmarshy | 0:1c7da5f83647 | 67 | |
| sarahmarshy | 0:1c7da5f83647 | 68 | /* APP_ERROR_CHECK(result); */ |
| sarahmarshy | 0:1c7da5f83647 | 69 | } |
| sarahmarshy | 0:1c7da5f83647 | 70 | |
| sarahmarshy | 0:1c7da5f83647 | 71 | /* Platform-specific implementation for persistence on the nRF5x. Based on the |
| sarahmarshy | 0:1c7da5f83647 | 72 | * pstorage module provided by the Nordic SDK. */ |
| sarahmarshy | 0:1c7da5f83647 | 73 | bool loadEddystoneServiceConfigParams(EddystoneService::EddystoneParams_t *paramsP) |
| sarahmarshy | 0:1c7da5f83647 | 74 | { |
| sarahmarshy | 0:1c7da5f83647 | 75 | static bool pstorageInitied = false; |
| sarahmarshy | 0:1c7da5f83647 | 76 | if (!pstorageInitied) { |
| sarahmarshy | 0:1c7da5f83647 | 77 | pstorage_init(); |
| sarahmarshy | 0:1c7da5f83647 | 78 | |
| sarahmarshy | 0:1c7da5f83647 | 79 | static pstorage_module_param_t pstorageParams = { |
| sarahmarshy | 0:1c7da5f83647 | 80 | .cb = pstorageNotificationCallback, |
| sarahmarshy | 0:1c7da5f83647 | 81 | .block_size = sizeof(PersistentParams_t), |
| sarahmarshy | 0:1c7da5f83647 | 82 | .block_count = 1 |
| sarahmarshy | 0:1c7da5f83647 | 83 | }; |
| sarahmarshy | 0:1c7da5f83647 | 84 | pstorage_register(&pstorageParams, &pstorageHandle); |
| sarahmarshy | 0:1c7da5f83647 | 85 | pstorageInitied = true; |
| sarahmarshy | 0:1c7da5f83647 | 86 | } |
| sarahmarshy | 0:1c7da5f83647 | 87 | |
| sarahmarshy | 0:1c7da5f83647 | 88 | if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) || |
| sarahmarshy | 0:1c7da5f83647 | 89 | (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) { |
| sarahmarshy | 0:1c7da5f83647 | 90 | // On failure zero out and let the service reset to defaults |
| sarahmarshy | 0:1c7da5f83647 | 91 | memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t)); |
| sarahmarshy | 0:1c7da5f83647 | 92 | return false; |
| sarahmarshy | 0:1c7da5f83647 | 93 | } |
| sarahmarshy | 0:1c7da5f83647 | 94 | |
| sarahmarshy | 0:1c7da5f83647 | 95 | memcpy(paramsP, &persistentParams.params, sizeof(EddystoneService::EddystoneParams_t)); |
| sarahmarshy | 0:1c7da5f83647 | 96 | return true; |
| sarahmarshy | 0:1c7da5f83647 | 97 | } |
| sarahmarshy | 0:1c7da5f83647 | 98 | |
| sarahmarshy | 0:1c7da5f83647 | 99 | /* Platform-specific implementation for persistence on the nRF5x. Based on the |
| sarahmarshy | 0:1c7da5f83647 | 100 | * pstorage module provided by the Nordic SDK. */ |
| sarahmarshy | 0:1c7da5f83647 | 101 | void saveEddystoneServiceConfigParams(const EddystoneService::EddystoneParams_t *paramsP) |
| sarahmarshy | 0:1c7da5f83647 | 102 | { |
| sarahmarshy | 0:1c7da5f83647 | 103 | memcpy(&persistentParams.params, paramsP, sizeof(EddystoneService::EddystoneParams_t)); |
| sarahmarshy | 0:1c7da5f83647 | 104 | if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) { |
| sarahmarshy | 0:1c7da5f83647 | 105 | persistentParams.persistenceSignature = PersistentParams_t::MAGIC; |
| sarahmarshy | 0:1c7da5f83647 | 106 | pstorage_store(&pstorageHandle, |
| sarahmarshy | 0:1c7da5f83647 | 107 | reinterpret_cast<uint8_t *>(&persistentParams), |
| sarahmarshy | 0:1c7da5f83647 | 108 | sizeof(PersistentParams_t), |
| sarahmarshy | 0:1c7da5f83647 | 109 | 0 /* offset */); |
| sarahmarshy | 0:1c7da5f83647 | 110 | } else { |
| sarahmarshy | 0:1c7da5f83647 | 111 | pstorage_update(&pstorageHandle, |
| sarahmarshy | 0:1c7da5f83647 | 112 | reinterpret_cast<uint8_t *>(&persistentParams), |
| sarahmarshy | 0:1c7da5f83647 | 113 | sizeof(PersistentParams_t), |
| sarahmarshy | 0:1c7da5f83647 | 114 | 0 /* offset */); |
| sarahmarshy | 0:1c7da5f83647 | 115 | } |
| sarahmarshy | 0:1c7da5f83647 | 116 | } |
| sarahmarshy | 0:1c7da5f83647 | 117 | |
| sarahmarshy | 0:1c7da5f83647 | 118 | /* Saves only the TimeParams (a subset of Config Params) for speed/power efficiency |
| sarahmarshy | 0:1c7da5f83647 | 119 | * Platform-specific implementation for persistence on the nRF5x. Based on the |
| sarahmarshy | 0:1c7da5f83647 | 120 | * pstorage module provided by the Nordic SDK. */ |
| sarahmarshy | 0:1c7da5f83647 | 121 | void saveEddystoneTimeParams(const TimeParams_t *timeP) |
| sarahmarshy | 0:1c7da5f83647 | 122 | { |
| sarahmarshy | 0:1c7da5f83647 | 123 | // Copy the time params object to the main datastructure |
| sarahmarshy | 0:1c7da5f83647 | 124 | memcpy(&persistentParams.params.timeParams, timeP, sizeof(TimeParams_t)); |
| sarahmarshy | 0:1c7da5f83647 | 125 | // Test if this is the first pstorage update, or an update |
| sarahmarshy | 0:1c7da5f83647 | 126 | if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) { |
| sarahmarshy | 0:1c7da5f83647 | 127 | persistentParams.persistenceSignature = PersistentParams_t::MAGIC; |
| sarahmarshy | 0:1c7da5f83647 | 128 | pstorage_store(&pstorageHandle, |
| sarahmarshy | 0:1c7da5f83647 | 129 | reinterpret_cast<uint8_t *>(&persistentParams), |
| sarahmarshy | 0:1c7da5f83647 | 130 | sizeof(TimeParams_t), |
| sarahmarshy | 0:1c7da5f83647 | 131 | offsetof(PersistentParams_t, params.timeParams) /* offset */); |
| sarahmarshy | 0:1c7da5f83647 | 132 | } else { |
| sarahmarshy | 0:1c7da5f83647 | 133 | pstorage_update(&pstorageHandle, |
| sarahmarshy | 0:1c7da5f83647 | 134 | reinterpret_cast<uint8_t *>(&persistentParams), |
| sarahmarshy | 0:1c7da5f83647 | 135 | sizeof(TimeParams_t), |
| sarahmarshy | 0:1c7da5f83647 | 136 | offsetof(PersistentParams_t, params.timeParams) /* offset */); |
| sarahmarshy | 0:1c7da5f83647 | 137 | } |
| sarahmarshy | 0:1c7da5f83647 | 138 | } |
| sarahmarshy | 0:1c7da5f83647 | 139 | |
| sarahmarshy | 0:1c7da5f83647 | 140 | #endif /* #ifdef TARGET_NRF51822 */ |