testing for double to float

Dependencies:   ADXL345_nRF51 BLE_API advertiser_data mbed nRF51822

Fork of BLE_GAP_Acceleration_Observer_2_Advertisers by Ames HCI IoT

Committer:
mchan
Date:
Thu Jul 23 19:28:04 2015 +0000
Revision:
11:8bfc379ad93f
Parent:
10:0733d4800ed1
Child:
13:7816784106fe
added carriage returns

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:332983584a9c 1 /* mbed Microcontroller Library
rgrover1 0:332983584a9c 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:332983584a9c 3 *
rgrover1 0:332983584a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:332983584a9c 5 * you may not use this file except in compliance with the License.
rgrover1 0:332983584a9c 6 * You may obtain a copy of the License at
rgrover1 0:332983584a9c 7 *
rgrover1 0:332983584a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:332983584a9c 9 *
rgrover1 0:332983584a9c 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:332983584a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:332983584a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:332983584a9c 13 * See the License for the specific language governing permissions and
rgrover1 0:332983584a9c 14 * limitations under the License.
rgrover1 0:332983584a9c 15 */
rgrover1 0:332983584a9c 16
rgrover1 0:332983584a9c 17 #include "mbed.h"
rgrover1 4:dd8231564124 18 #include "BLE.h"
rjoyce 9:7360ae6b9b1e 19 #include "advertiser_data.h"
rjoyce 9:7360ae6b9b1e 20 #define DEBUG 1
rgrover1 0:332983584a9c 21
rgrover1 5:103717ce54e5 22 BLE ble;
rjoyce 9:7360ae6b9b1e 23 #ifdef DEBUG
rjoyce 9:7360ae6b9b1e 24 Serial pc(USBTX, USBRX);
rjoyce 9:7360ae6b9b1e 25 #endif
rgrover1 0:332983584a9c 26
rjoyce 9:7360ae6b9b1e 27 // Note: if this is set >254 then lots of assumptions are broken...
rjoyce 9:7360ae6b9b1e 28 #define MAX_ADVERTISERS 10
rjoyce 9:7360ae6b9b1e 29 static advertiser_data advertisers[MAX_ADVERTISERS];
rjoyce 9:7360ae6b9b1e 30 static uint8_t advertiser_index[MAX_ADVERTISERS];
rjoyce 9:7360ae6b9b1e 31 static uint8_t advertiser_count = 0;
rjoyce 7:d78080148084 32
rjoyce 9:7360ae6b9b1e 33 uint8_t get_index(uint8_t address)
rjoyce 9:7360ae6b9b1e 34 {
rjoyce 9:7360ae6b9b1e 35 /* Find in index array */
rjoyce 9:7360ae6b9b1e 36 for (unsigned i = 0; i < advertiser_count; i++) {
rjoyce 9:7360ae6b9b1e 37 if (advertiser_index[i] == address) return i;
rjoyce 9:7360ae6b9b1e 38 }
rjoyce 9:7360ae6b9b1e 39 /* Not found, insert if still room */
rjoyce 9:7360ae6b9b1e 40 if (advertiser_count < MAX_ADVERTISERS) {
rjoyce 9:7360ae6b9b1e 41 advertiser_count++;
rjoyce 9:7360ae6b9b1e 42 advertiser_index[advertiser_count] = address;
rjoyce 9:7360ae6b9b1e 43 advertiser_data_reset(&advertisers[advertiser_count]);
rjoyce 9:7360ae6b9b1e 44 return advertiser_count;
rjoyce 9:7360ae6b9b1e 45 } else {
rjoyce 9:7360ae6b9b1e 46 /* TODO: delete oldest? */
rjoyce 9:7360ae6b9b1e 47 return MAX_ADVERTISERS;
rjoyce 9:7360ae6b9b1e 48 }
rjoyce 9:7360ae6b9b1e 49 }
rjoyce 7:d78080148084 50
rjoyce 9:7360ae6b9b1e 51 advertiser_data* get_advertiser_data(uint8_t address)
rjoyce 9:7360ae6b9b1e 52 {
rjoyce 9:7360ae6b9b1e 53 /* Return NULL if index out of bounds */
rjoyce 9:7360ae6b9b1e 54 uint8_t index = get_index(address);
rjoyce 9:7360ae6b9b1e 55 return (index < MAX_ADVERTISERS) ? &advertisers[index] : NULL;
rjoyce 9:7360ae6b9b1e 56 }
rjoyce 9:7360ae6b9b1e 57
rjoyce 9:7360ae6b9b1e 58 void get_manufacturer_data(const uint8_t *advData, uint8_t advDataLen, const uint8_t *&pManData, uint8_t &manDataLen)
rjoyce 9:7360ae6b9b1e 59 {
rjoyce 9:7360ae6b9b1e 60 unsigned index = 0;
rjoyce 9:7360ae6b9b1e 61 unsigned data_size = 0;
rjoyce 9:7360ae6b9b1e 62 pManData = NULL;
rjoyce 9:7360ae6b9b1e 63 manDataLen = 0;
rjoyce 9:7360ae6b9b1e 64 while (index < advDataLen) {
rjoyce 9:7360ae6b9b1e 65 data_size = advData[index];
rjoyce 9:7360ae6b9b1e 66 if (GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA == advData[index+1]) {
rjoyce 9:7360ae6b9b1e 67 pManData = advData + index + 2;
rjoyce 9:7360ae6b9b1e 68 manDataLen = data_size - 2;
rjoyce 9:7360ae6b9b1e 69 return;
rjoyce 9:7360ae6b9b1e 70 }
rjoyce 9:7360ae6b9b1e 71 index += data_size;
rjoyce 9:7360ae6b9b1e 72 }
rjoyce 9:7360ae6b9b1e 73 }
rjoyce 9:7360ae6b9b1e 74
rjoyce 7:d78080148084 75
rjoyce 6:460ee10d9930 76 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
rgrover1 0:332983584a9c 77 {
rjoyce 9:7360ae6b9b1e 78 #ifdef DEBUG
mchan 11:8bfc379ad93f 79 pc.printf("Found device...\n\r");
rjoyce 9:7360ae6b9b1e 80 #endif
rjoyce 6:460ee10d9930 81 if (params->peerAddr[5] == 0xe5 && params->peerAddr[4] == 0x4f) {
rjoyce 9:7360ae6b9b1e 82 #ifdef DEBUG
rjoyce 6:460ee10d9930 83 for (unsigned index = 0; index < params->advertisingDataLen; index++) {
rjoyce 9:7360ae6b9b1e 84 pc.printf("%02x ", params->advertisingData[index]);
rjoyce 9:7360ae6b9b1e 85 }
mchan 11:8bfc379ad93f 86 pc.printf("\n\r");
rjoyce 9:7360ae6b9b1e 87 #endif
rjoyce 9:7360ae6b9b1e 88 /* Using only the 3rd part of address for indexing the stored data for now */
rjoyce 9:7360ae6b9b1e 89 advertiser_data *pdata = get_advertiser_data(params->peerAddr[3]);
rjoyce 9:7360ae6b9b1e 90 if (!pdata) {
rjoyce 9:7360ae6b9b1e 91 #ifdef DEBUG
mchan 11:8bfc379ad93f 92 pc.printf("Too many advertisers!\n\r");
rjoyce 9:7360ae6b9b1e 93 #endif
rjoyce 9:7360ae6b9b1e 94 return;
rjoyce 7:d78080148084 95 }
rjoyce 9:7360ae6b9b1e 96
rjoyce 9:7360ae6b9b1e 97 /* Always update the rssi */
rjoyce 9:7360ae6b9b1e 98 advertiser_data_update_rssi(pdata, params->rssi);
rjoyce 9:7360ae6b9b1e 99
rjoyce 9:7360ae6b9b1e 100 /* Find the manufacturers data */
rjoyce 9:7360ae6b9b1e 101 const uint8_t *manData = NULL;
rjoyce 9:7360ae6b9b1e 102 uint8_t manDataLen = 0;
rjoyce 9:7360ae6b9b1e 103 get_manufacturer_data(params->advertisingData, params->advertisingDataLen, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 104
rjoyce 9:7360ae6b9b1e 105 /* If the data is empty, skip the counter check */
rjoyce 9:7360ae6b9b1e 106 if (!advertiser_data_is_empty(pdata)) {
rjoyce 9:7360ae6b9b1e 107 uint8_t diff = advertiser_data_counter_difference(pdata, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 108 if (diff == 0) {
rjoyce 9:7360ae6b9b1e 109 /* Quit early if we've seen this packet */
rjoyce 9:7360ae6b9b1e 110 return;
rjoyce 9:7360ae6b9b1e 111 } else if (diff > 1) {
rjoyce 9:7360ae6b9b1e 112 #ifdef DEBUG
mchan 11:8bfc379ad93f 113 pc.printf("resetting: diff is %d\n\r", diff);
rjoyce 9:7360ae6b9b1e 114 #endif
rjoyce 9:7360ae6b9b1e 115 /* Reset the data if we missed a packet (likely lost signal for a while) */
rjoyce 9:7360ae6b9b1e 116 advertiser_data_reset(pdata);
rjoyce 9:7360ae6b9b1e 117 advertiser_data_update_rssi(pdata, params->rssi);
rjoyce 7:d78080148084 118 }
rjoyce 7:d78080148084 119 }
rjoyce 9:7360ae6b9b1e 120
rjoyce 9:7360ae6b9b1e 121 /* Update everything from the manufacturer data */
rjoyce 9:7360ae6b9b1e 122 advertiser_data_update(pdata, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 123
rjoyce 9:7360ae6b9b1e 124 // TODO: implement the rest of the algorithms
rjoyce 9:7360ae6b9b1e 125 advertiser_data_print(pdata);
rgrover1 0:332983584a9c 126 }
rgrover1 0:332983584a9c 127 }
rgrover1 0:332983584a9c 128
rgrover1 0:332983584a9c 129 int main(void)
rgrover1 0:332983584a9c 130 {
rgrover1 0:332983584a9c 131 ble.init();
rgrover1 0:332983584a9c 132
rjoyce 9:7360ae6b9b1e 133 #ifdef DEBUG
rjoyce 9:7360ae6b9b1e 134 pc.baud(9600);
rjoyce 9:7360ae6b9b1e 135 wait(8);
rjoyce 9:7360ae6b9b1e 136 pc.printf("Started scanning...\n\r");
rjoyce 9:7360ae6b9b1e 137 #endif
rjoyce 9:7360ae6b9b1e 138
rjoyce 6:460ee10d9930 139 // Set scan to be constant by interval == window
rjoyce 6:460ee10d9930 140 ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
rgrover1 5:103717ce54e5 141 ble.gap().startScan(advertisementCallback);
rjoyce 9:7360ae6b9b1e 142
rjoyce 9:7360ae6b9b1e 143 for (int i = 0; i < MAX_ADVERTISERS; i++) {
rjoyce 9:7360ae6b9b1e 144 advertiser_data_reset(&advertisers[i]);
rjoyce 9:7360ae6b9b1e 145 }
rgrover1 0:332983584a9c 146
rgrover1 0:332983584a9c 147 while (true) {
rgrover1 0:332983584a9c 148 ble.waitForEvent();
rgrover1 0:332983584a9c 149 }
rgrover1 0:332983584a9c 150 }