with 2 advertisers

Dependencies:   ADXL345_nRF51 BLE_API advertiser_data mbed nRF51822

Fork of BLE_GAP_Acceleration_Observer by Ames HCI IoT

Committer:
mchan
Date:
Wed Jul 29 17:14:40 2015 +0000
Revision:
16:7593fe102c55
Parent:
14:3bde71241a53
Made to differentiate between advertiser 1 and advertiser 2

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 13:7816784106fe 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) {
mchan 16:7593fe102c55 82 pc.printf("ADVERTISER 1\n\r");
mchan 16:7593fe102c55 83 #ifdef DEBUG
mchan 16:7593fe102c55 84 for (unsigned index = 0; index < params->advertisingDataLen; index++) {
mchan 16:7593fe102c55 85 pc.printf("%02x ", params->advertisingData[index]);
mchan 16:7593fe102c55 86 }
mchan 16:7593fe102c55 87 pc.printf("\n\r");
mchan 16:7593fe102c55 88 #endif
mchan 16:7593fe102c55 89 /* Using only the 3rd part of address for indexing the stored data for now */
mchan 16:7593fe102c55 90 advertiser_data *pdata = get_advertiser_data(params->peerAddr[3]);
mchan 16:7593fe102c55 91 if (!pdata) {
mchan 16:7593fe102c55 92 #ifdef DEBUG
mchan 16:7593fe102c55 93 pc.printf("Too many advertisers!\n\r");
mchan 16:7593fe102c55 94 #endif
mchan 16:7593fe102c55 95 return;
mchan 16:7593fe102c55 96 }
mchan 16:7593fe102c55 97
mchan 16:7593fe102c55 98 /* Always update the rssi */
mchan 16:7593fe102c55 99 uint8_t passdat = (uint8_t)(abs(params->rssi));
mchan 16:7593fe102c55 100 advertiser_data_update_rssi(pdata, passdat);
mchan 16:7593fe102c55 101
mchan 16:7593fe102c55 102 /* Find the manufacturers data */
mchan 16:7593fe102c55 103 const uint8_t *manData = NULL;
mchan 16:7593fe102c55 104 uint8_t manDataLen = 0;
mchan 16:7593fe102c55 105 get_manufacturer_data(params->advertisingData, params->advertisingDataLen, manData, manDataLen);
mchan 16:7593fe102c55 106
mchan 16:7593fe102c55 107 /* If the data is empty, skip the counter check */
mchan 16:7593fe102c55 108 if (!advertiser_data_is_empty(pdata)) {
mchan 16:7593fe102c55 109 uint8_t diff = advertiser_data_counter_difference(pdata, manData, manDataLen);
mchan 16:7593fe102c55 110 if (diff == 0) {
mchan 16:7593fe102c55 111 /* Quit early if we've seen this packet */
mchan 16:7593fe102c55 112 return;
mchan 16:7593fe102c55 113 } else if (diff > 1) {
mchan 16:7593fe102c55 114 #ifdef DEBUG
mchan 16:7593fe102c55 115 pc.printf("resetting: diff is %d\n\r", diff);
mchan 16:7593fe102c55 116 #endif
mchan 16:7593fe102c55 117 /* Reset the data if we missed a packet (likely lost signal for a while) */
mchan 16:7593fe102c55 118 advertiser_data_reset(pdata);
mchan 16:7593fe102c55 119 advertiser_data_update_rssi(pdata, passdat);
mchan 16:7593fe102c55 120 }
mchan 16:7593fe102c55 121 }
mchan 16:7593fe102c55 122
mchan 16:7593fe102c55 123 /* Update everything from the manufacturer data */
mchan 16:7593fe102c55 124 advertiser_data_update(pdata, manData, manDataLen);
mchan 16:7593fe102c55 125
mchan 16:7593fe102c55 126 // TODO: implement the rest of the algorithms
mchan 16:7593fe102c55 127 advertiser_data_print(pdata);
mchan 16:7593fe102c55 128 }else if(params->peerAddr[5] == 0xe6 && params->peerAddr[4] == 0x4f) {
mchan 16:7593fe102c55 129 pc.printf("ADVERTISER 2\n\r");
rjoyce 9:7360ae6b9b1e 130 #ifdef DEBUG
rjoyce 6:460ee10d9930 131 for (unsigned index = 0; index < params->advertisingDataLen; index++) {
rjoyce 9:7360ae6b9b1e 132 pc.printf("%02x ", params->advertisingData[index]);
rjoyce 9:7360ae6b9b1e 133 }
mchan 11:8bfc379ad93f 134 pc.printf("\n\r");
rjoyce 9:7360ae6b9b1e 135 #endif
rjoyce 9:7360ae6b9b1e 136 /* Using only the 3rd part of address for indexing the stored data for now */
rjoyce 9:7360ae6b9b1e 137 advertiser_data *pdata = get_advertiser_data(params->peerAddr[3]);
rjoyce 9:7360ae6b9b1e 138 if (!pdata) {
rjoyce 9:7360ae6b9b1e 139 #ifdef DEBUG
mchan 11:8bfc379ad93f 140 pc.printf("Too many advertisers!\n\r");
rjoyce 9:7360ae6b9b1e 141 #endif
rjoyce 9:7360ae6b9b1e 142 return;
rjoyce 7:d78080148084 143 }
rjoyce 9:7360ae6b9b1e 144
rjoyce 9:7360ae6b9b1e 145 /* Always update the rssi */
mchan 14:3bde71241a53 146 uint8_t passdat = (uint8_t)(abs(params->rssi));
mchan 14:3bde71241a53 147 advertiser_data_update_rssi(pdata, passdat);
rjoyce 9:7360ae6b9b1e 148
rjoyce 9:7360ae6b9b1e 149 /* Find the manufacturers data */
rjoyce 9:7360ae6b9b1e 150 const uint8_t *manData = NULL;
rjoyce 9:7360ae6b9b1e 151 uint8_t manDataLen = 0;
rjoyce 9:7360ae6b9b1e 152 get_manufacturer_data(params->advertisingData, params->advertisingDataLen, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 153
rjoyce 9:7360ae6b9b1e 154 /* If the data is empty, skip the counter check */
rjoyce 9:7360ae6b9b1e 155 if (!advertiser_data_is_empty(pdata)) {
rjoyce 9:7360ae6b9b1e 156 uint8_t diff = advertiser_data_counter_difference(pdata, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 157 if (diff == 0) {
rjoyce 9:7360ae6b9b1e 158 /* Quit early if we've seen this packet */
rjoyce 9:7360ae6b9b1e 159 return;
rjoyce 9:7360ae6b9b1e 160 } else if (diff > 1) {
rjoyce 9:7360ae6b9b1e 161 #ifdef DEBUG
mchan 11:8bfc379ad93f 162 pc.printf("resetting: diff is %d\n\r", diff);
rjoyce 9:7360ae6b9b1e 163 #endif
rjoyce 9:7360ae6b9b1e 164 /* Reset the data if we missed a packet (likely lost signal for a while) */
rjoyce 9:7360ae6b9b1e 165 advertiser_data_reset(pdata);
mchan 14:3bde71241a53 166 advertiser_data_update_rssi(pdata, passdat);
rjoyce 7:d78080148084 167 }
rjoyce 7:d78080148084 168 }
rjoyce 9:7360ae6b9b1e 169
rjoyce 9:7360ae6b9b1e 170 /* Update everything from the manufacturer data */
rjoyce 9:7360ae6b9b1e 171 advertiser_data_update(pdata, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 172
rjoyce 9:7360ae6b9b1e 173 // TODO: implement the rest of the algorithms
rjoyce 9:7360ae6b9b1e 174 advertiser_data_print(pdata);
rgrover1 0:332983584a9c 175 }
rgrover1 0:332983584a9c 176 }
rgrover1 0:332983584a9c 177
rgrover1 0:332983584a9c 178 int main(void)
rgrover1 0:332983584a9c 179 {
rgrover1 0:332983584a9c 180 ble.init();
rgrover1 0:332983584a9c 181
rjoyce 9:7360ae6b9b1e 182 #ifdef DEBUG
rjoyce 9:7360ae6b9b1e 183 pc.baud(9600);
rjoyce 9:7360ae6b9b1e 184 wait(8);
rjoyce 9:7360ae6b9b1e 185 pc.printf("Started scanning...\n\r");
rjoyce 9:7360ae6b9b1e 186 #endif
rjoyce 9:7360ae6b9b1e 187
rjoyce 6:460ee10d9930 188 // Set scan to be constant by interval == window
rjoyce 6:460ee10d9930 189 ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
rgrover1 5:103717ce54e5 190 ble.gap().startScan(advertisementCallback);
rjoyce 9:7360ae6b9b1e 191
rjoyce 9:7360ae6b9b1e 192 for (int i = 0; i < MAX_ADVERTISERS; i++) {
rjoyce 9:7360ae6b9b1e 193 advertiser_data_reset(&advertisers[i]);
rjoyce 9:7360ae6b9b1e 194 }
rgrover1 0:332983584a9c 195
rgrover1 0:332983584a9c 196 while (true) {
rgrover1 0:332983584a9c 197 ble.waitForEvent();
rgrover1 0:332983584a9c 198 }
rgrover1 0:332983584a9c 199 }