ST
/
mbed-os-example-ble-EddystoneService
BLE EddystoneService example
Embed:
(wiki syntax)
Show/hide line numbers
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 #if defined(TARGET_NRF51822) || defined(TARGET_NRF52832) /* Persistent storage supported on nrf51 and nrf52 platforms */ 00018 00019 extern "C" { 00020 #include "fstorage.h" 00021 } 00022 00023 #include "nrf_error.h" 00024 #include "../../EddystoneService.h" 00025 #include <cstddef> 00026 00027 /** 00028 * Nordic specific structure used to store params persistently. 00029 * It extends EddystoneService::EddystoneParams_t with a persistence signature. 00030 */ 00031 struct PersistentParams_t { 00032 EddystoneService::EddystoneParams_t params; 00033 uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected 00034 * magic value in this field indicates persistence. */ 00035 00036 static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */ 00037 }; 00038 00039 /** 00040 * The following is a module-local variable to hold configuration parameters for 00041 * short periods during flash access. This is necessary because the fstorage 00042 * APIs don't copy in the memory provided as data source. The memory cannot be 00043 * freed or reused by the application until this flash access is complete. The 00044 * load and store operations in this module initialize persistentParams and then 00045 * pass it on to the 'fstorage' APIs. 00046 */ 00047 static PersistentParams_t persistentParams; 00048 00049 /** 00050 * Dummy callback handler needed by Nordic's fstorage module. This is called 00051 * after every flash access. 00052 */ 00053 static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result) 00054 { 00055 /* Supress compiler warnings */ 00056 (void) evt; 00057 (void) result; 00058 } 00059 00060 FS_REGISTER_CFG(fs_config_t fs_config) = { 00061 NULL, // Begin pointer (set by fs_init) 00062 NULL, // End pointer (set by fs_init) 00063 &fs_evt_handler, // Function for event callbacks. 00064 1, // Number of physical flash pages required. 00065 0xFE // Priority for flash usage. 00066 }; 00067 00068 00069 void loadPersistentParams(void) { 00070 // copy from flash into persistent params struct 00071 memcpy(&persistentParams, fs_config.p_start_addr, sizeof(PersistentParams_t)); 00072 } 00073 00074 /* Platform-specific implementation for persistence on the nRF5x. Based on the 00075 * fstorage module provided by the Nordic SDK. */ 00076 bool loadEddystoneServiceConfigParams(EddystoneService::EddystoneParams_t *paramsP) 00077 { 00078 static bool fstorageInited = false; 00079 if (!fstorageInited) { 00080 fs_init(); 00081 fstorageInited = true; 00082 } 00083 00084 loadPersistentParams(); 00085 00086 if ((persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) { 00087 // On failure zero out and let the service reset to defaults 00088 memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t)); 00089 return false; 00090 } 00091 00092 memcpy(paramsP, &persistentParams.params, sizeof(EddystoneService::EddystoneParams_t)); 00093 return true; 00094 } 00095 00096 /* Platform-specific implementation for persistence on the nRF5x. Based on the 00097 * fstorage module provided by the Nordic SDK. */ 00098 void saveEddystoneServiceConfigParams(const EddystoneService::EddystoneParams_t *paramsP) 00099 { 00100 memcpy(&persistentParams.params, paramsP, sizeof(EddystoneService::EddystoneParams_t)); 00101 if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) { 00102 persistentParams.persistenceSignature = PersistentParams_t::MAGIC; 00103 } else { 00104 fs_erase(&fs_config, fs_config.p_start_addr, sizeof(PersistentParams_t) / 4); 00105 } 00106 00107 fs_store(&fs_config, 00108 fs_config.p_start_addr, 00109 reinterpret_cast<uint32_t *>(&persistentParams), 00110 sizeof(PersistentParams_t) / 4); 00111 } 00112 00113 #endif /* #ifdef TARGET_NRF51822 */
Generated on Tue Jul 12 2022 19:55:16 by 1.7.2