ST / Mbed OS mbed-os-example-ble-EddystoneService

This example is a fork of the following mbed-os example:

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-EddystoneService/

Please read the documentation in this page.

Committer:
mbed_official
Date:
Thu Jul 28 23:14:36 2016 +0100
Revision:
1:9db4d46bb63f
Parent:
0:4c8f8bf32a99
Child:
2:9ee673e0b86a
Merge branch 'master' of https://github.com/ARMmbed/mbed-os-example-ble


Commit copied from ./src/github.com/ARMmbed/mbed-os-example-ble

Who changed what in which revision?

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