For switch science magazine

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_EddystoneURL_Beacon by Roy Want

Committer:
roywant
Date:
Tue Aug 18 00:45:39 2015 +0000
Revision:
1:0ad7573b5918
Parent:
0:094734b4e8bd
Converted BLE_URIBeacon to a Eddystone-URL by changing the Service Data UUID and Frame byte. All other edits are changing class, variable names and comments to Eddystone.

Who changed what in which revision?

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