added

Dependencies:   mbed microbit BLE_API nRF51822

Committer:
suntopbd
Date:
Fri Sep 07 20:17:07 2018 +0000
Revision:
11:6916c05fde52
Microbit BLE UART IoT

Who changed what in which revision?

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