project for eddystone

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_URIBeacon by Bluetooth Low Energy

Committer:
sunsmile2015
Date:
Mon Nov 02 10:14:48 2015 +0000
Revision:
29:db32d8dae04c
Parent:
17:e2c0a1696e39
tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 16:1daa78939a3b 1 /* mbed Microcontroller Library
rgrover1 16:1daa78939a3b 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 16:1daa78939a3b 3 *
rgrover1 16:1daa78939a3b 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 16:1daa78939a3b 5 * you may not use this file except in compliance with the License.
rgrover1 16:1daa78939a3b 6 * You may obtain a copy of the License at
rgrover1 16:1daa78939a3b 7 *
rgrover1 16:1daa78939a3b 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 16:1daa78939a3b 9 *
rgrover1 16:1daa78939a3b 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 16:1daa78939a3b 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 16:1daa78939a3b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 16:1daa78939a3b 13 * See the License for the specific language governing permissions and
rgrover1 16:1daa78939a3b 14 * limitations under the License.
rgrover1 16:1daa78939a3b 15 */
rgrover1 16:1daa78939a3b 16
rgrover1 16:1daa78939a3b 17 #include "pstorage.h"
rgrover1 16:1daa78939a3b 18 #include "nrf_error.h"
rgrover1 16:1daa78939a3b 19 #include "ConfigParamsPersistence.h"
rgrover1 16:1daa78939a3b 20
rgrover1 17:e2c0a1696e39 21 /**
rgrover1 17:e2c0a1696e39 22 * Nordic specific structure used to store params persistently.
rgrover1 17:e2c0a1696e39 23 * It extends URIBeaconConfigService::Params_t with a persistence signature.
rgrover1 17:e2c0a1696e39 24 */
rgrover1 17:e2c0a1696e39 25 struct PersistentParams_t {
rgrover1 17:e2c0a1696e39 26 URIBeaconConfigService::Params_t params;
rgrover1 17:e2c0a1696e39 27 uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected
rgrover1 17:e2c0a1696e39 28 * magic value in this field indicates persistence. */
rgrover1 17:e2c0a1696e39 29
rgrover1 17:e2c0a1696e39 30 static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */
rgrover1 17:e2c0a1696e39 31 };
rgrover1 17:e2c0a1696e39 32
rgrover1 17:e2c0a1696e39 33 /**
rgrover1 17:e2c0a1696e39 34 * The following is a module-local variable to hold configuration parameters for
rgrover1 17:e2c0a1696e39 35 * short periods during flash access. This is necessary because the pstorage
rgrover1 17:e2c0a1696e39 36 * APIs don't copy in the memory provided as data source. The memory cannot be
rgrover1 17:e2c0a1696e39 37 * freed or reused by the application until this flash access is complete. The
rgrover1 17:e2c0a1696e39 38 * load and store operations in this module initialize persistentParams and then
rgrover1 17:e2c0a1696e39 39 * pass it on to the 'pstorage' APIs.
rgrover1 17:e2c0a1696e39 40 */
rgrover1 17:e2c0a1696e39 41 static PersistentParams_t persistentParams;
rgrover1 17:e2c0a1696e39 42
rgrover1 16:1daa78939a3b 43 static pstorage_handle_t pstorageHandle;
rgrover1 16:1daa78939a3b 44
rgrover1 17:e2c0a1696e39 45 /**
rgrover1 17:e2c0a1696e39 46 * Dummy callback handler needed by Nordic's pstorage module. This is called
rgrover1 17:e2c0a1696e39 47 * after every flash access.
rgrover1 17:e2c0a1696e39 48 */
rgrover1 16:1daa78939a3b 49 static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
rgrover1 17:e2c0a1696e39 50 uint8_t op_code,
rgrover1 17:e2c0a1696e39 51 uint32_t result,
rgrover1 17:e2c0a1696e39 52 uint8_t *p_data,
rgrover1 17:e2c0a1696e39 53 uint32_t data_len)
rgrover1 16:1daa78939a3b 54 {
rgrover1 16:1daa78939a3b 55 /* APP_ERROR_CHECK(result); */
rgrover1 16:1daa78939a3b 56 }
rgrover1 16:1daa78939a3b 57
rgrover1 16:1daa78939a3b 58 /* Platform-specific implementation for persistence on the nRF5x. Based on the
rgrover1 16:1daa78939a3b 59 * pstorage module provided by the Nordic SDK. */
rgrover1 17:e2c0a1696e39 60 bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP)
rgrover1 16:1daa78939a3b 61 {
rgrover1 17:e2c0a1696e39 62 static bool pstorageInitied = false;
rgrover1 17:e2c0a1696e39 63 if (!pstorageInitied) {
rgrover1 17:e2c0a1696e39 64 pstorage_init();
rgrover1 16:1daa78939a3b 65
rgrover1 17:e2c0a1696e39 66 static pstorage_module_param_t pstorageParams = {
rgrover1 17:e2c0a1696e39 67 .cb = pstorageNotificationCallback,
rgrover1 17:e2c0a1696e39 68 .block_size = sizeof(PersistentParams_t),
rgrover1 17:e2c0a1696e39 69 .block_count = 1
rgrover1 17:e2c0a1696e39 70 };
rgrover1 17:e2c0a1696e39 71 pstorage_register(&pstorageParams, &pstorageHandle);
rgrover1 17:e2c0a1696e39 72 pstorageInitied = true;
rgrover1 17:e2c0a1696e39 73 }
rgrover1 17:e2c0a1696e39 74
rgrover1 17:e2c0a1696e39 75 if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
rgrover1 17:e2c0a1696e39 76 (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
rgrover1 16:1daa78939a3b 77 // On failure zero out and let the service reset to defaults
rgrover1 16:1daa78939a3b 78 memset(paramsP, 0, sizeof(URIBeaconConfigService::Params_t));
rgrover1 17:e2c0a1696e39 79 return false;
rgrover1 16:1daa78939a3b 80 }
rgrover1 17:e2c0a1696e39 81
rgrover1 17:e2c0a1696e39 82 memcpy(paramsP, &persistentParams.params, sizeof(URIBeaconConfigService::Params_t));
rgrover1 17:e2c0a1696e39 83 return true;
rgrover1 16:1daa78939a3b 84 }
rgrover1 16:1daa78939a3b 85
rgrover1 16:1daa78939a3b 86 /* Platform-specific implementation for persistence on the nRF5x. Based on the
rgrover1 16:1daa78939a3b 87 * pstorage module provided by the Nordic SDK. */
rgrover1 17:e2c0a1696e39 88 void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP)
rgrover1 16:1daa78939a3b 89 {
rgrover1 17:e2c0a1696e39 90 memcpy(&persistentParams.params, paramsP, sizeof(URIBeaconConfigService::Params_t));
rgrover1 17:e2c0a1696e39 91 if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
rgrover1 17:e2c0a1696e39 92 persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
rgrover1 16:1daa78939a3b 93 pstorage_store(&pstorageHandle,
rgrover1 17:e2c0a1696e39 94 reinterpret_cast<uint8_t *>(&persistentParams),
rgrover1 17:e2c0a1696e39 95 sizeof(PersistentParams_t),
rgrover1 16:1daa78939a3b 96 0 /* offset */);
rgrover1 16:1daa78939a3b 97 } else {
rgrover1 16:1daa78939a3b 98 pstorage_update(&pstorageHandle,
rgrover1 17:e2c0a1696e39 99 reinterpret_cast<uint8_t *>(&persistentParams),
rgrover1 17:e2c0a1696e39 100 sizeof(PersistentParams_t),
rgrover1 16:1daa78939a3b 101 0 /* offset */);
rgrover1 16:1daa78939a3b 102 }
rgrover1 16:1daa78939a3b 103 }