Scott Jenson / Mbed 2 deprecated BLE_EddystoneBeaconConfigService_Button

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

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 EddystoneConfigService::Params_t with a persistence signature.
00024  */
00025 struct PersistentParams_t {
00026     EddystoneConfigService::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 extern int reboot;
00045 extern int dirty;
00046 extern void ledRebootAnimation();
00047 
00048 
00049 /**
00050  * Dummy callback handler needed by Nordic's pstorage module. This is called
00051  * after every flash access.
00052  */
00053 static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
00054                                          uint8_t            op_code,
00055                                          uint32_t           result,
00056                                          uint8_t           *p_data,
00057                                          uint32_t           data_len)
00058 {
00059     if (dirty) {
00060         ledRebootAnimation();
00061         NVIC_SystemReset();
00062     }
00063     /* APP_ERROR_CHECK(result); */
00064 }
00065 
00066 // ScottJ Routine Rohit asked to setup to reset storage callback after going into advertising mode
00067 void pstorageSetup() {
00068     pstorage_init();
00069     static pstorage_module_param_t pstorageParams = {
00070         .cb          = pstorageNotificationCallback,
00071         .block_size  = sizeof(PersistentParams_t),
00072         .block_count = 1
00073     };
00074     pstorage_register(&pstorageParams, &pstorageHandle);
00075 }
00076 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00077  * pstorage module provided by the Nordic SDK. */
00078 bool loadURIBeaconConfigParams(EddystoneConfigService::Params_t *paramsP)
00079 {
00080     static bool pstorageInitied = false;
00081     if (!pstorageInitied) {
00082         pstorageSetup();
00083         pstorageInitied = true;
00084     }
00085 
00086     if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
00087         (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
00088         // On failure zero out and let the service reset to defaults
00089         memset(paramsP, 0, sizeof(EddystoneConfigService::Params_t));
00090         return false;
00091     }
00092 
00093     memcpy(paramsP, &persistentParams.params, sizeof(EddystoneConfigService::Params_t));
00094     return true;
00095 }
00096 
00097 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00098  * pstorage module provided by the Nordic SDK. */
00099 void saveURIBeaconConfigParams(const EddystoneConfigService::Params_t *paramsP)
00100 {
00101    memcpy(&persistentParams.params, paramsP, sizeof(EddystoneConfigService::Params_t));
00102     if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
00103         persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
00104         pstorage_store(&pstorageHandle,
00105                        reinterpret_cast<uint8_t *>(&persistentParams),
00106                        sizeof(PersistentParams_t),
00107                        0 /* offset */);
00108     } else {
00109         pstorage_update(&pstorageHandle,
00110                         reinterpret_cast<uint8_t *>(&persistentParams),
00111                         sizeof(PersistentParams_t),
00112                         0 /* offset */);
00113     }
00114 }