Button initiated config service
Dependencies: BLE_API_EddystoneConfigService_2 mbed nRF51822
Fork of BLE_EddystoneBeaconConfigService_3 by
nrfConfigParamsPersistence.cpp@2:8020d6d4455a, 2015-05-14 (annotated)
- Committer:
- rgrover1
- Date:
- Thu May 14 16:54:00 2015 +0000
- Revision:
- 2:8020d6d4455a
- Child:
- 23:05e9bb3b13af
reintroduce param persistence into ZipBeacon.
Who changed what in which revision?
User | Revision | Line number | New 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. |
rgrover1 | 2:8020d6d4455a | 23 | * It extends ZipBeaconConfigService::Params_t with a persistence signature. |
rgrover1 | 2:8020d6d4455a | 24 | */ |
rgrover1 | 2:8020d6d4455a | 25 | struct PersistentParams_t { |
rgrover1 | 2:8020d6d4455a | 26 | ZipBeaconConfigService::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; |
rgrover1 | 2:8020d6d4455a | 44 | |
rgrover1 | 2:8020d6d4455a | 45 | /** |
rgrover1 | 2:8020d6d4455a | 46 | * Dummy callback handler needed by Nordic's pstorage module. This is called |
rgrover1 | 2:8020d6d4455a | 47 | * after every flash access. |
rgrover1 | 2:8020d6d4455a | 48 | */ |
rgrover1 | 2:8020d6d4455a | 49 | static void pstorageNotificationCallback(pstorage_handle_t *p_handle, |
rgrover1 | 2:8020d6d4455a | 50 | uint8_t op_code, |
rgrover1 | 2:8020d6d4455a | 51 | uint32_t result, |
rgrover1 | 2:8020d6d4455a | 52 | uint8_t *p_data, |
rgrover1 | 2:8020d6d4455a | 53 | uint32_t data_len) |
rgrover1 | 2:8020d6d4455a | 54 | { |
rgrover1 | 2:8020d6d4455a | 55 | /* APP_ERROR_CHECK(result); */ |
rgrover1 | 2:8020d6d4455a | 56 | } |
rgrover1 | 2:8020d6d4455a | 57 | |
rgrover1 | 2:8020d6d4455a | 58 | /* Platform-specific implementation for persistence on the nRF5x. Based on the |
rgrover1 | 2:8020d6d4455a | 59 | * pstorage module provided by the Nordic SDK. */ |
rgrover1 | 2:8020d6d4455a | 60 | bool loadURIBeaconConfigParams(ZipBeaconConfigService::Params_t *paramsP) |
rgrover1 | 2:8020d6d4455a | 61 | { |
rgrover1 | 2:8020d6d4455a | 62 | static bool pstorageInitied = false; |
rgrover1 | 2:8020d6d4455a | 63 | if (!pstorageInitied) { |
rgrover1 | 2:8020d6d4455a | 64 | pstorage_init(); |
rgrover1 | 2:8020d6d4455a | 65 | |
rgrover1 | 2:8020d6d4455a | 66 | static pstorage_module_param_t pstorageParams = { |
rgrover1 | 2:8020d6d4455a | 67 | .cb = pstorageNotificationCallback, |
rgrover1 | 2:8020d6d4455a | 68 | .block_size = sizeof(PersistentParams_t), |
rgrover1 | 2:8020d6d4455a | 69 | .block_count = 1 |
rgrover1 | 2:8020d6d4455a | 70 | }; |
rgrover1 | 2:8020d6d4455a | 71 | pstorage_register(&pstorageParams, &pstorageHandle); |
rgrover1 | 2:8020d6d4455a | 72 | pstorageInitied = true; |
rgrover1 | 2:8020d6d4455a | 73 | } |
rgrover1 | 2:8020d6d4455a | 74 | |
rgrover1 | 2:8020d6d4455a | 75 | if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) || |
rgrover1 | 2:8020d6d4455a | 76 | (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) { |
rgrover1 | 2:8020d6d4455a | 77 | // On failure zero out and let the service reset to defaults |
rgrover1 | 2:8020d6d4455a | 78 | memset(paramsP, 0, sizeof(ZipBeaconConfigService::Params_t)); |
rgrover1 | 2:8020d6d4455a | 79 | return false; |
rgrover1 | 2:8020d6d4455a | 80 | } |
rgrover1 | 2:8020d6d4455a | 81 | |
rgrover1 | 2:8020d6d4455a | 82 | memcpy(paramsP, &persistentParams.params, sizeof(ZipBeaconConfigService::Params_t)); |
rgrover1 | 2:8020d6d4455a | 83 | return true; |
rgrover1 | 2:8020d6d4455a | 84 | } |
rgrover1 | 2:8020d6d4455a | 85 | |
rgrover1 | 2:8020d6d4455a | 86 | /* Platform-specific implementation for persistence on the nRF5x. Based on the |
rgrover1 | 2:8020d6d4455a | 87 | * pstorage module provided by the Nordic SDK. */ |
rgrover1 | 2:8020d6d4455a | 88 | void saveURIBeaconConfigParams(const ZipBeaconConfigService::Params_t *paramsP) |
rgrover1 | 2:8020d6d4455a | 89 | { |
rgrover1 | 2:8020d6d4455a | 90 | memcpy(&persistentParams.params, paramsP, sizeof(ZipBeaconConfigService::Params_t)); |
rgrover1 | 2:8020d6d4455a | 91 | if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) { |
rgrover1 | 2:8020d6d4455a | 92 | persistentParams.persistenceSignature = PersistentParams_t::MAGIC; |
rgrover1 | 2:8020d6d4455a | 93 | pstorage_store(&pstorageHandle, |
rgrover1 | 2:8020d6d4455a | 94 | reinterpret_cast<uint8_t *>(&persistentParams), |
rgrover1 | 2:8020d6d4455a | 95 | sizeof(PersistentParams_t), |
rgrover1 | 2:8020d6d4455a | 96 | 0 /* offset */); |
rgrover1 | 2:8020d6d4455a | 97 | } else { |
rgrover1 | 2:8020d6d4455a | 98 | pstorage_update(&pstorageHandle, |
rgrover1 | 2:8020d6d4455a | 99 | reinterpret_cast<uint8_t *>(&persistentParams), |
rgrover1 | 2:8020d6d4455a | 100 | sizeof(PersistentParams_t), |
rgrover1 | 2:8020d6d4455a | 101 | 0 /* offset */); |
rgrover1 | 2:8020d6d4455a | 102 | } |
rgrover1 | 2:8020d6d4455a | 103 | } |