For switch science magazine

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_EddystoneURL_Beacon by Roy Want

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 #include "pstorage.h"
00018 #include "nrf_error.h"
00019 #include "ConfigParamsPersistence.h"
00020 
00021 /**
00022  * Nordic specific structure used to store params persistently.
00023  * It extends EddystoneURLConfigService::Params_t with a persistence signature.
00024  */
00025 struct PersistentParams_t {
00026     EddystoneURLConfigService::Params_t params;
00027     uint32_t                         persistenceSignature; /* This isn't really a parameter, but having the expected
00028                                                             * magic value in this field indicates persistence. */
00029 
00030     static const uint32_t MAGIC = 0x1BEAC000;              /* Magic that identifies persistence */
00031 };
00032 
00033 /**
00034  * The following is a module-local variable to hold configuration parameters for
00035  * short periods during flash access. This is necessary because the pstorage
00036  * APIs don't copy in the memory provided as data source. The memory cannot be
00037  * freed or reused by the application until this flash access is complete. The
00038  * load and store operations in this module initialize persistentParams and then
00039  * pass it on to the 'pstorage' APIs.
00040  */
00041 static PersistentParams_t persistentParams;
00042 
00043 static pstorage_handle_t pstorageHandle;
00044 
00045 /**
00046  * Dummy callback handler needed by Nordic's pstorage module. This is called
00047  * after every flash access.
00048  */
00049 static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
00050                                          uint8_t            op_code,
00051                                          uint32_t           result,
00052                                          uint8_t           *p_data,
00053                                          uint32_t           data_len)
00054 {
00055     /* APP_ERROR_CHECK(result); */
00056 }
00057 
00058 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00059  * pstorage module provided by the Nordic SDK. */
00060 bool loadEddystoneURLConfigParams(EddystoneURLConfigService::Params_t *paramsP)
00061 {
00062     static bool pstorageInitied = false;
00063     if (!pstorageInitied) {
00064         pstorage_init();
00065 
00066         static pstorage_module_param_t pstorageParams = {
00067             .cb          = pstorageNotificationCallback,
00068             .block_size  = sizeof(PersistentParams_t),
00069             .block_count = 1
00070         };
00071         pstorage_register(&pstorageParams, &pstorageHandle);
00072         pstorageInitied = true;
00073     }
00074 
00075     if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
00076         (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
00077         // On failure zero out and let the service reset to defaults
00078         memset(paramsP, 0, sizeof(EddystoneURLConfigService::Params_t));
00079         return false;
00080     }
00081 
00082     memcpy(paramsP, &persistentParams.params, sizeof(EddystoneURLConfigService::Params_t));
00083     return true;
00084 }
00085 
00086 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00087  * pstorage module provided by the Nordic SDK. */
00088 void saveEddystoneURLConfigParams(const EddystoneURLConfigService::Params_t *paramsP)
00089 {
00090     memcpy(&persistentParams.params, paramsP, sizeof(EddystoneURLConfigService::Params_t));
00091     if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
00092         persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
00093         pstorage_store(&pstorageHandle,
00094                        reinterpret_cast<uint8_t *>(&persistentParams),
00095                        sizeof(PersistentParams_t),
00096                        0 /* offset */);
00097     } else {
00098         pstorage_update(&pstorageHandle,
00099                         reinterpret_cast<uint8_t *>(&persistentParams),
00100                         sizeof(PersistentParams_t),
00101                         0 /* offset */);
00102     }
00103 }