Roy Want / Mbed OS beaconCompileReadyFork
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nrfConfigParamsPersistence.cpp Source File

nrfConfigParamsPersistence.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifdef TARGET_NRF51822 /* Persistent storage supported on nrf51 platforms */
00018 
00019 extern "C" {
00020     #include "pstorage.h"
00021 }
00022 
00023 #include "nrf_error.h"
00024 #include "../../EddystoneService.h"
00025 
00026 /**
00027  * Nordic specific structure used to store params persistently.
00028  * It extends EddystoneService::EddystoneParams_t with a persistence signature.
00029  */
00030 struct PersistentParams_t {
00031     EddystoneService::EddystoneParams_t params;
00032     uint32_t                         persistenceSignature; /* This isn't really a parameter, but having the expected
00033                                                             * magic value in this field indicates persistence. */
00034 
00035     static const uint32_t MAGIC = 0x1BEAC000;              /* Magic that identifies persistence */
00036 };
00037 
00038 /**
00039  * The following is a module-local variable to hold configuration parameters for
00040  * short periods during flash access. This is necessary because the pstorage
00041  * APIs don't copy in the memory provided as data source. The memory cannot be
00042  * freed or reused by the application until this flash access is complete. The
00043  * load and store operations in this module initialize persistentParams and then
00044  * pass it on to the 'pstorage' APIs.
00045  */
00046 static PersistentParams_t persistentParams;
00047 
00048 static pstorage_handle_t pstorageHandle;
00049 
00050 /**
00051  * Dummy callback handler needed by Nordic's pstorage module. This is called
00052  * after every flash access.
00053  */
00054 static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
00055                                          uint8_t            op_code,
00056                                          uint32_t           result,
00057                                          uint8_t           *p_data,
00058                                          uint32_t           data_len)
00059 {
00060     /* Supress compiler warnings */
00061     (void) p_handle;
00062     (void) op_code;
00063     (void) result;
00064     (void) p_data;
00065     (void) data_len;
00066 
00067     /* APP_ERROR_CHECK(result); */
00068 }
00069 
00070 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00071  * pstorage module provided by the Nordic SDK. */
00072 bool loadEddystoneServiceConfigParams(EddystoneService::EddystoneParams_t *paramsP)
00073 {
00074     static bool pstorageInitied = false;
00075     if (!pstorageInitied) {
00076         pstorage_init();
00077 
00078         static pstorage_module_param_t pstorageParams = {
00079             .cb          = pstorageNotificationCallback,
00080             .block_size  = sizeof(PersistentParams_t),
00081             .block_count = 1
00082         };
00083         pstorage_register(&pstorageParams, &pstorageHandle);
00084         pstorageInitied = true;
00085     }
00086 
00087     if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
00088         (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
00089         // On failure zero out and let the service reset to defaults
00090         memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t));
00091         return false;
00092     }
00093 
00094     memcpy(paramsP, &persistentParams.params, sizeof(EddystoneService::EddystoneParams_t));
00095     return true;
00096 }
00097 
00098 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00099  * pstorage module provided by the Nordic SDK. */
00100 void saveEddystoneServiceConfigParams(const EddystoneService::EddystoneParams_t *paramsP)
00101 {
00102     memcpy(&persistentParams.params, paramsP, sizeof(EddystoneService::EddystoneParams_t));
00103     if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
00104         persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
00105         pstorage_store(&pstorageHandle,
00106                        reinterpret_cast<uint8_t *>(&persistentParams),
00107                        sizeof(PersistentParams_t),
00108                        0 /* offset */);
00109     } else {
00110         pstorage_update(&pstorageHandle,
00111                         reinterpret_cast<uint8_t *>(&persistentParams),
00112                         sizeof(PersistentParams_t),
00113                         0 /* offset */);
00114     }
00115 }
00116 
00117 #endif /* #ifdef TARGET_NRF51822 */