URI-Beacons are handy when there is a need to advertise a small amount of information (usually a URL) to any nearby device. They’re really easy to set up: the code is fully available on the mbed website, so all you’ll need to do is tell the beacon what to broadcast. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_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 extern "C" {
00018 #include "pstorage.h"
00019 }
00020 
00021 #include "nrf_error.h"
00022 #include "ConfigParamsPersistence.h"
00023 
00024 /**
00025  * Nordic specific structure used to store params persistently.
00026  * It extends URIBeaconConfigService::Params_t with a persistence signature.
00027  */
00028 struct PersistentParams_t {
00029     URIBeaconConfigService::Params_t params;
00030     uint32_t                         persistenceSignature; /* This isn't really a parameter, but having the expected
00031                                                             * magic value in this field indicates persistence. */
00032 
00033     static const uint32_t MAGIC = 0x1BEAC000;              /* Magic that identifies persistence */
00034 };
00035 
00036 /**
00037  * The following is a module-local variable to hold configuration parameters for
00038  * short periods during flash access. This is necessary because the pstorage
00039  * APIs don't copy in the memory provided as data source. The memory cannot be
00040  * freed or reused by the application until this flash access is complete. The
00041  * load and store operations in this module initialize persistentParams and then
00042  * pass it on to the 'pstorage' APIs.
00043  */
00044 static PersistentParams_t persistentParams;
00045 
00046 static pstorage_handle_t pstorageHandle;
00047 
00048 /**
00049  * Dummy callback handler needed by Nordic's pstorage module. This is called
00050  * after every flash access.
00051  */
00052 static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
00053                                          uint8_t            op_code,
00054                                          uint32_t           result,
00055                                          uint8_t           *p_data,
00056                                          uint32_t           data_len)
00057 {
00058     /* APP_ERROR_CHECK(result); */
00059 }
00060 
00061 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00062  * pstorage module provided by the Nordic SDK. */
00063 bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP)
00064 {
00065     static bool pstorageInitied = false;
00066     if (!pstorageInitied) {
00067         pstorage_init();
00068 
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         pstorageInitied = true;
00076     }
00077 
00078     if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
00079         (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
00080         // On failure zero out and let the service reset to defaults
00081         memset(paramsP, 0, sizeof(URIBeaconConfigService::Params_t));
00082         return false;
00083     }
00084 
00085     memcpy(paramsP, &persistentParams.params, sizeof(URIBeaconConfigService::Params_t));
00086     return true;
00087 }
00088 
00089 /* Platform-specific implementation for persistence on the nRF5x. Based on the
00090  * pstorage module provided by the Nordic SDK. */
00091 void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP)
00092 {
00093     memcpy(&persistentParams.params, paramsP, sizeof(URIBeaconConfigService::Params_t));
00094     if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
00095         persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
00096         pstorage_store(&pstorageHandle,
00097                        reinterpret_cast<uint8_t *>(&persistentParams),
00098                        sizeof(PersistentParams_t),
00099                        0 /* offset */);
00100     } else {
00101         pstorage_update(&pstorageHandle,
00102                         reinterpret_cast<uint8_t *>(&persistentParams),
00103                         sizeof(PersistentParams_t),
00104                         0 /* offset */);
00105     }
00106 }