Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

Committer:
scottjenson
Date:
Mon Oct 12 21:18:21 2015 +0000
Revision:
68:f7f7ec038494
Parent:
67:345bc62c748d
support for button initiated config (for nRFgo usb board and Seeed)

Who changed what in which revision?

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