The Uniform Resource Identifier Beacon (UriBeacon) defines Bluetooth 4.0 Advertisement Data that contain Web URIs. URIBeacon provides a way for Bluetooth Low Energy devices to discover nearby URIs, for example, provides a way for a user to discover a short URL and then download it on their smartphone.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_PhysicalWeb by Bluetooth Low Energy

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