trying to add correlation

Dependencies:   ADXL345_nRF51 BLE_API advertiser_data mbed nRF51822

Fork of BLE_GAP_Acceleration_Trial by Ames HCI IoT

Committer:
mchan
Date:
Thu Jul 30 16:40:03 2015 +0000
Revision:
19:5b952d4f1024
Parent:
18:bb0566d76c27
correlation test

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;
mchan 19:5b952d4f1024 32 static uint8_t currently_picked_up = 0;
rjoyce 7:d78080148084 33
rjoyce 9:7360ae6b9b1e 34 uint8_t get_index(uint8_t address)
rjoyce 9:7360ae6b9b1e 35 {
rjoyce 9:7360ae6b9b1e 36 /* Find in index array */
rjoyce 9:7360ae6b9b1e 37 for (unsigned i = 0; i < advertiser_count; i++) {
rjoyce 9:7360ae6b9b1e 38 if (advertiser_index[i] == address) return i;
rjoyce 9:7360ae6b9b1e 39 }
rjoyce 9:7360ae6b9b1e 40 /* Not found, insert if still room */
rjoyce 9:7360ae6b9b1e 41 if (advertiser_count < MAX_ADVERTISERS) {
rjoyce 9:7360ae6b9b1e 42 advertiser_count++;
rjoyce 9:7360ae6b9b1e 43 advertiser_index[advertiser_count] = address;
rjoyce 9:7360ae6b9b1e 44 advertiser_data_reset(&advertisers[advertiser_count]);
rjoyce 9:7360ae6b9b1e 45 return advertiser_count;
rjoyce 9:7360ae6b9b1e 46 } else {
rjoyce 9:7360ae6b9b1e 47 /* TODO: delete oldest? */
rjoyce 9:7360ae6b9b1e 48 return MAX_ADVERTISERS;
rjoyce 9:7360ae6b9b1e 49 }
rjoyce 9:7360ae6b9b1e 50 }
rjoyce 7:d78080148084 51
rjoyce 9:7360ae6b9b1e 52 advertiser_data* get_advertiser_data(uint8_t address)
rjoyce 9:7360ae6b9b1e 53 {
rjoyce 9:7360ae6b9b1e 54 /* Return NULL if index out of bounds */
rjoyce 9:7360ae6b9b1e 55 uint8_t index = get_index(address);
rjoyce 9:7360ae6b9b1e 56 return (index < MAX_ADVERTISERS) ? &advertisers[index] : NULL;
rjoyce 9:7360ae6b9b1e 57 }
rjoyce 9:7360ae6b9b1e 58
rjoyce 9:7360ae6b9b1e 59 void get_manufacturer_data(const uint8_t *advData, uint8_t advDataLen, const uint8_t *&pManData, uint8_t &manDataLen)
rjoyce 9:7360ae6b9b1e 60 {
rjoyce 9:7360ae6b9b1e 61 unsigned index = 0;
rjoyce 9:7360ae6b9b1e 62 unsigned data_size = 0;
rjoyce 9:7360ae6b9b1e 63 pManData = NULL;
rjoyce 9:7360ae6b9b1e 64 manDataLen = 0;
rjoyce 9:7360ae6b9b1e 65 while (index < advDataLen) {
rjoyce 9:7360ae6b9b1e 66 data_size = advData[index];
rjoyce 9:7360ae6b9b1e 67 if (GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA == advData[index+1]) {
rjoyce 9:7360ae6b9b1e 68 pManData = advData + index + 2;
rjoyce 9:7360ae6b9b1e 69 manDataLen = data_size - 2;
rjoyce 9:7360ae6b9b1e 70 return;
rjoyce 9:7360ae6b9b1e 71 }
rjoyce 9:7360ae6b9b1e 72 index += data_size;
rjoyce 9:7360ae6b9b1e 73 }
rjoyce 9:7360ae6b9b1e 74 }
rjoyce 9:7360ae6b9b1e 75
rjoyce 7:d78080148084 76
rjoyce 6:460ee10d9930 77 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
rgrover1 0:332983584a9c 78 {
rjoyce 9:7360ae6b9b1e 79 #ifdef DEBUG
mchan 13:7816784106fe 80 //pc.printf("Found device...\n\r");
rjoyce 9:7360ae6b9b1e 81 #endif
rjoyce 6:460ee10d9930 82 if (params->peerAddr[5] == 0xe5 && params->peerAddr[4] == 0x4f) {
rjoyce 9:7360ae6b9b1e 83 #ifdef DEBUG
mchan 18:bb0566d76c27 84 /*for (unsigned index = 0; index < params->advertisingDataLen; index++) {
rjoyce 9:7360ae6b9b1e 85 pc.printf("%02x ", params->advertisingData[index]);
rjoyce 9:7360ae6b9b1e 86 }
mchan 18:bb0566d76c27 87 pc.printf("\n\r");*/
rjoyce 9:7360ae6b9b1e 88 #endif
rjoyce 9:7360ae6b9b1e 89 /* Using only the 3rd part of address for indexing the stored data for now */
rjoyce 9:7360ae6b9b1e 90 advertiser_data *pdata = get_advertiser_data(params->peerAddr[3]);
rjoyce 9:7360ae6b9b1e 91 if (!pdata) {
rjoyce 9:7360ae6b9b1e 92 #ifdef DEBUG
mchan 11:8bfc379ad93f 93 pc.printf("Too many advertisers!\n\r");
rjoyce 9:7360ae6b9b1e 94 #endif
rjoyce 9:7360ae6b9b1e 95 return;
rjoyce 7:d78080148084 96 }
rjoyce 9:7360ae6b9b1e 97
rjoyce 9:7360ae6b9b1e 98 /* Always update the rssi */
mchan 14:3bde71241a53 99 uint8_t passdat = (uint8_t)(abs(params->rssi));
mchan 14:3bde71241a53 100 advertiser_data_update_rssi(pdata, passdat);
rjoyce 9:7360ae6b9b1e 101
rjoyce 9:7360ae6b9b1e 102 /* Find the manufacturers data */
rjoyce 9:7360ae6b9b1e 103 const uint8_t *manData = NULL;
rjoyce 9:7360ae6b9b1e 104 uint8_t manDataLen = 0;
rjoyce 9:7360ae6b9b1e 105 get_manufacturer_data(params->advertisingData, params->advertisingDataLen, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 106
rjoyce 9:7360ae6b9b1e 107 /* If the data is empty, skip the counter check */
rjoyce 9:7360ae6b9b1e 108 if (!advertiser_data_is_empty(pdata)) {
rjoyce 9:7360ae6b9b1e 109 uint8_t diff = advertiser_data_counter_difference(pdata, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 110 if (diff == 0) {
rjoyce 9:7360ae6b9b1e 111 /* Quit early if we've seen this packet */
rjoyce 9:7360ae6b9b1e 112 return;
rjoyce 9:7360ae6b9b1e 113 } else if (diff > 1) {
rjoyce 9:7360ae6b9b1e 114 #ifdef DEBUG
mchan 11:8bfc379ad93f 115 pc.printf("resetting: diff is %d\n\r", diff);
rjoyce 9:7360ae6b9b1e 116 #endif
rjoyce 9:7360ae6b9b1e 117 /* Reset the data if we missed a packet (likely lost signal for a while) */
rjoyce 9:7360ae6b9b1e 118 advertiser_data_reset(pdata);
mchan 14:3bde71241a53 119 advertiser_data_update_rssi(pdata, passdat);
rjoyce 7:d78080148084 120 }
rjoyce 7:d78080148084 121 }
rjoyce 9:7360ae6b9b1e 122
rjoyce 9:7360ae6b9b1e 123 /* Update everything from the manufacturer data */
rjoyce 9:7360ae6b9b1e 124 advertiser_data_update(pdata, manData, manDataLen);
rjoyce 9:7360ae6b9b1e 125
rjoyce 9:7360ae6b9b1e 126 // TODO: implement the rest of the algorithms
mchan 19:5b952d4f1024 127 //advertiser_data_print(pdata,params->peerAddr[2]);
mchan 19:5b952d4f1024 128
mchan 19:5b952d4f1024 129 if(pdata->picked_up){
mchan 19:5b952d4f1024 130 uint8_t temp = params->peerAddr[2];
mchan 19:5b952d4f1024 131 if(currently_picked_up!=temp){
mchan 19:5b952d4f1024 132 currently_picked_up = temp;
mchan 19:5b952d4f1024 133 pc.printf("CURRENTLY PICKING UP DEVICE %i\n\r",currently_picked_up);
mchan 19:5b952d4f1024 134 }
mchan 19:5b952d4f1024 135 }
rgrover1 0:332983584a9c 136 }
rgrover1 0:332983584a9c 137 }
rgrover1 0:332983584a9c 138
rgrover1 0:332983584a9c 139 int main(void)
rgrover1 0:332983584a9c 140 {
rgrover1 0:332983584a9c 141 ble.init();
rgrover1 0:332983584a9c 142
rjoyce 9:7360ae6b9b1e 143 #ifdef DEBUG
rjoyce 9:7360ae6b9b1e 144 pc.baud(9600);
rjoyce 9:7360ae6b9b1e 145 wait(8);
rjoyce 9:7360ae6b9b1e 146 pc.printf("Started scanning...\n\r");
rjoyce 9:7360ae6b9b1e 147 #endif
rjoyce 9:7360ae6b9b1e 148
rjoyce 6:460ee10d9930 149 // Set scan to be constant by interval == window
rjoyce 6:460ee10d9930 150 ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
rgrover1 5:103717ce54e5 151 ble.gap().startScan(advertisementCallback);
rjoyce 9:7360ae6b9b1e 152
rjoyce 9:7360ae6b9b1e 153 for (int i = 0; i < MAX_ADVERTISERS; i++) {
rjoyce 9:7360ae6b9b1e 154 advertiser_data_reset(&advertisers[i]);
rjoyce 9:7360ae6b9b1e 155 }
rgrover1 0:332983584a9c 156
rgrover1 0:332983584a9c 157 while (true) {
rgrover1 0:332983584a9c 158 ble.waitForEvent();
rgrover1 0:332983584a9c 159 }
rgrover1 0:332983584a9c 160 }