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:
Vincent Coubard
Date:
Tue Jul 26 14:40:25 2016 +0100
Revision:
0:4c8f8bf32a99
Child:
1:9db4d46bb63f
Update example at tag mbed-os-5.0.1-rc1

Who changed what in which revision?

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