Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Thu Nov 23 15:49:10 2017 +0000
Revision:
31:caef580f5943
Parent:
27:2c67f07590fd
Child:
32:61d2328ec1a7
iBeacon advType implemented.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 1:5f34885f5cff 1 /*
jurica238814 0:f8c1e0b2d473 2 *
jurica238814 1:5f34885f5cff 3 * Made by Jurica Resetar @ aconno
jurica238814 1:5f34885f5cff 4 * aconno.de
jurica238814 4:331dddea780e 5 * All rights reserved.
jurica238814 0:f8c1e0b2d473 6 *
jurica238814 0:f8c1e0b2d473 7 */
jurica238814 0:f8c1e0b2d473 8
jurica238814 0:f8c1e0b2d473 9 #include "mbed.h"
jurica238814 0:f8c1e0b2d473 10 #include "ble/BLE.h"
jurica238814 1:5f34885f5cff 11 #include "GapAdvertisingData.h"
jurica238814 0:f8c1e0b2d473 12 #include "acd52832_bsp.h"
jurica238814 6:d14e3df498f4 13 #include "mma8452.h"
jurica238814 16:a338d2417fd5 14 #include "AckService.h"
jurica238814 16:a338d2417fd5 15 #include "nrf52_uart.h"
jurica238814 22:8d106fd5fa84 16 #include "nrf52_digital.h"
Dautor 26:148aa2e2460c 17 #include "acn_nrf52_pwm.h"
jurica238814 1:5f34885f5cff 18
jurica238814 27:2c67f07590fd 19 #define DEBUG (0)
jurica238814 27:2c67f07590fd 20 #define DEBUG_ACC (0)
Dautor 26:148aa2e2460c 21 #define DEBUG_PRINT_UART (0)
jurica238814 17:51a5456a46cd 22 #define DEBUG_MAC (0)
jurica238814 17:51a5456a46cd 23 #define DEBUG_CONNECTION (0)
Dautor 26:148aa2e2460c 24 #define DEBUG_WAKEUP_BUZZER (0)
Dautor 26:148aa2e2460c 25
jurica238814 27:2c67f07590fd 26 #define USE_ACC (0)
jurica238814 8:570eb66d50b5 27
jurica238814 27:2c67f07590fd 28 #define SLEEP_TIME_S (3.00) /* Sleep time (in s) */
jurica238814 27:2c67f07590fd 29 #define ADV_TIMER_TIME_S (1.00) /* Advertising time (in s) */
jurica238814 23:729717272b31 30 #define SCAN_TIMER_TIME_S (0.25) /* Scanning time (in s) */
Dautor 26:148aa2e2460c 31 #define FREE_TIME_S (0.1) /* Time between end of a scanning and sleep mode */
jurica238814 14:d506c0679c0b 32 #define AWAKE_TIME_S (ADV_TIMER_TIME_S+SCAN_TIMER_TIME_S+FREE_TIME_S) /* Was 0.15 */
Dautor 26:148aa2e2460c 33 #define SHORT_SLEEP_TIME_S (0.5) /* Shorter sleep time (s) */
Dautor 26:148aa2e2460c 34 #define SHORT_SLEEP_TIME_PERIOD_S (10) /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
jurica238814 27:2c67f07590fd 35 #define BUZZER_FREQUENCY_Hz (3000)
jurica238814 15:934a04c958f5 36 #define MAC_SIZE_B (6)
jurica238814 12:6b072c2a061c 37
jurica238814 1:5f34885f5cff 38 /* Static constants for the BLE example */
jurica238814 3:2a4ac5b87046 39 #define MAX_BLE_PACKET_SIZE (31)
jurica238814 3:2a4ac5b87046 40 #define MSD_SIZE (18)
jurica238814 3:2a4ac5b87046 41 #define MSD_ID (0xFF)
jurica238814 12:6b072c2a061c 42
Dautor 26:148aa2e2460c 43 #define BUZZ_TIME_S (1) /* Buzz time in s */
jurica238814 25:8ac3ff431ab1 44 #define ADV_INTERVAL (100) /* Advertising interval (in ms) */
jurica238814 14:d506c0679c0b 45 #define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */
jurica238814 14:d506c0679c0b 46 #define SCAN_WINDOW (SCAN_TIMER_TIME_S)
jurica238814 1:5f34885f5cff 47
jurica238814 6:d14e3df498f4 48 /* Static constants for the accelerometer */
jurica238814 13:d51127eed926 49 #define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */
jurica238814 13:d51127eed926 50 #define OUT_Z_MSB 0x05 /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */
jurica238814 6:d14e3df498f4 51 #define I2C_DATA (p29)
jurica238814 6:d14e3df498f4 52 #define I2C_CLK (p2)
jurica238814 6:d14e3df498f4 53 #define INT2_PIN (p4)
jurica238814 10:fd91664032d8 54 #define BUZZER (p31)
jurica238814 0:f8c1e0b2d473 55
jurica238814 31:caef580f5943 56 typedef struct __attribute__((packed, aligned(1))){
jurica238814 31:caef580f5943 57 uint16_t appleID;
jurica238814 31:caef580f5943 58 uint8_t secondID;
jurica238814 31:caef580f5943 59 uint8_t DataSize;
jurica238814 31:caef580f5943 60 uint8_t UUID[18];
jurica238814 31:caef580f5943 61 uint8_t major;
jurica238814 31:caef580f5943 62 uint8_t minor;
jurica238814 31:caef580f5943 63 int8_t RSSI;
jurica238814 31:caef580f5943 64 }IBeaconMSD;
jurica238814 31:caef580f5943 65
jurica238814 31:caef580f5943 66 IBeaconMSD ibeaconMSD;
jurica238814 31:caef580f5943 67
Dautor 26:148aa2e2460c 68 #if DEBUG_PRINT_UART
jurica238814 27:2c67f07590fd 69 #include "nrf52_uart.h"
jurica238814 27:2c67f07590fd 70 NRF52_UART uart(p25, p26, Baud9600);
jurica238814 27:2c67f07590fd 71 char buffer[255];
jurica238814 27:2c67f07590fd 72 #define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); uart.send(buffer, len);}
Dautor 26:148aa2e2460c 73 #else
jurica238814 27:2c67f07590fd 74 #define SEND(...)
jurica238814 16:a338d2417fd5 75 #endif
jurica238814 16:a338d2417fd5 76
Dautor 26:148aa2e2460c 77 bool beaconStateActive = 1;
jurica238814 16:a338d2417fd5 78
jurica238814 16:a338d2417fd5 79 bool shushShush = false;
jurica238814 16:a338d2417fd5 80
jurica238814 18:e844d3e6ab88 81 uint8_t txPower = 4;
jurica238814 27:2c67f07590fd 82 uint8_t sleepFlag = true;
jurica238814 14:d506c0679c0b 83 uint8_t tempSleepTime = SLEEP_TIME_S;
jurica238814 14:d506c0679c0b 84 uint8_t msd[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 25:8ac3ff431ab1 85 uint8_t startBuzz[2] = {0xBA, 0xBE};
jurica238814 25:8ac3ff431ab1 86 uint8_t stopBuzz[2] = {0xDE, 0xAD};
jurica238814 23:729717272b31 87 uint8_t myMacAddress[6] = {};
jurica238814 6:d14e3df498f4 88 uint8_t buzzer_flag = 0;
jurica238814 0:f8c1e0b2d473 89
jurica238814 14:d506c0679c0b 90 enum RadioState{
jurica238814 10:fd91664032d8 91 OFF,
jurica238814 10:fd91664032d8 92 ADVERTISING,
Dautor 26:148aa2e2460c 93 SCANNING,
Dautor 26:148aa2e2460c 94 };
jurica238814 14:d506c0679c0b 95 enum RadioState radioState = OFF;
jurica238814 0:f8c1e0b2d473 96
jurica238814 14:d506c0679c0b 97 void GoToSleep();
jurica238814 14:d506c0679c0b 98 void StartAdvertising();
jurica238814 10:fd91664032d8 99 void startScanning();
jurica238814 10:fd91664032d8 100 void WakeMeUp();
jurica238814 0:f8c1e0b2d473 101
jurica238814 2:5504b714c9ae 102 Ticker WakeSleepT;
jurica238814 7:89c9abaa257e 103 Ticker sleepChanger;
jurica238814 27:2c67f07590fd 104 NRF52_PWM buzzer(NRF_PWM2);
jurica238814 27:2c67f07590fd 105
jurica238814 31:caef580f5943 106 enum advPayloadType{
jurica238814 31:caef580f5943 107 iBEACON = 0,
jurica238814 31:caef580f5943 108 BEACON
jurica238814 31:caef580f5943 109 };
jurica238814 31:caef580f5943 110
jurica238814 19:abf14a5ada93 111 #if USE_ACC
jurica238814 19:abf14a5ada93 112 DigitalOut accPower(p7);
jurica238814 19:abf14a5ada93 113 DigitalOut i2cPower(p5);
jurica238814 19:abf14a5ada93 114 InterruptIn accPulse(INT2_PIN);
jurica238814 21:10c3b8176be0 115 Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
jurica238814 27:2c67f07590fd 116
jurica238814 19:abf14a5ada93 117 #endif
jurica238814 1:5f34885f5cff 118 BLE &ble = BLE::Instance();
jurica238814 16:a338d2417fd5 119 ACKService<4> *ackServicePtr;
jurica238814 10:fd91664032d8 120
jurica238814 22:8d106fd5fa84 121 #if DEBUG || DEBUG_MAC || DEBUG_CONNECTION
Dautor 26:148aa2e2460c 122 NRF52_DigitalOut advLED(p22); // Red
Dautor 26:148aa2e2460c 123 NRF52_DigitalOut scanLED(p23); // Blue
Dautor 26:148aa2e2460c 124 NRF52_DigitalOut connectedLED(p24); // Green
jurica238814 10:fd91664032d8 125 #endif
jurica238814 18:e844d3e6ab88 126
jurica238814 10:fd91664032d8 127 #if DEBUG_ACC
Dautor 26:148aa2e2460c 128 NRF52_DigitalOut int_led(p22);
Dautor 26:148aa2e2460c 129 NRF52_DigitalOut act_led(p22);
jurica238814 8:570eb66d50b5 130 #endif
jurica238814 8:570eb66d50b5 131
Dautor 26:148aa2e2460c 132
Dautor 26:148aa2e2460c 133 void buzzerStart(){
jurica238814 27:2c67f07590fd 134 buzzer.enable(BUZZER_FREQUENCY_Hz);
jurica238814 27:2c67f07590fd 135 //buzzer.enable(1);
Dautor 26:148aa2e2460c 136 buzzer.enableChannel(0, BUZZER);
jurica238814 27:2c67f07590fd 137 buzzer.setDuty(0,0.5f);
Dautor 26:148aa2e2460c 138 }
Dautor 26:148aa2e2460c 139 void buzzerStop(){
jurica238814 27:2c67f07590fd 140 buzzer.enable(0);
jurica238814 27:2c67f07590fd 141 buzzer.setDuty(0, 0);
Dautor 26:148aa2e2460c 142 buzzer.disable();
jurica238814 22:8d106fd5fa84 143 }
jurica238814 16:a338d2417fd5 144
jurica238814 21:10c3b8176be0 145 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
jurica238814 17:51a5456a46cd 146 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 147 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 148 wait_ms(100);
jurica238814 17:51a5456a46cd 149 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 150 wait_ms(100);
jurica238814 17:51a5456a46cd 151 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 152 wait_ms(100);
jurica238814 17:51a5456a46cd 153 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 154 wait_ms(100);
jurica238814 17:51a5456a46cd 155 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 156 wait_ms(100);
jurica238814 17:51a5456a46cd 157 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 158 wait_ms(100);
jurica238814 17:51a5456a46cd 159 scanLED = 1; // Blue
jurica238814 17:51a5456a46cd 160 #endif
jurica238814 17:51a5456a46cd 161 WakeSleepT.detach();
jurica238814 17:51a5456a46cd 162 sleepFlag = false;
jurica238814 16:a338d2417fd5 163 }
jurica238814 16:a338d2417fd5 164
jurica238814 16:a338d2417fd5 165
jurica238814 0:f8c1e0b2d473 166 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 167 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
Dautor 26:148aa2e2460c 168 buzzerStop();
jurica238814 17:51a5456a46cd 169 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 170 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 171 wait_ms(100);
jurica238814 17:51a5456a46cd 172 advLED = !advLED;
jurica238814 17:51a5456a46cd 173 wait_ms(100);
jurica238814 17:51a5456a46cd 174 advLED = !advLED;
jurica238814 17:51a5456a46cd 175 wait_ms(100);
jurica238814 17:51a5456a46cd 176 advLED = !advLED;
jurica238814 17:51a5456a46cd 177 wait_ms(100);
jurica238814 17:51a5456a46cd 178 advLED = 1;
jurica238814 17:51a5456a46cd 179 wait_ms(100);
jurica238814 17:51a5456a46cd 180 advLED = 1;
jurica238814 17:51a5456a46cd 181 #endif
jurica238814 17:51a5456a46cd 182 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 17:51a5456a46cd 183 sleepFlag = true;
jurica238814 18:e844d3e6ab88 184
jurica238814 0:f8c1e0b2d473 185 }
jurica238814 0:f8c1e0b2d473 186
jurica238814 16:a338d2417fd5 187 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
jurica238814 25:8ac3ff431ab1 188 if(params->handle == ackServicePtr->getACKCharacteristicHandle()){
jurica238814 16:a338d2417fd5 189 // Something is written into AckCharacteristic
jurica238814 25:8ac3ff431ab1 190 if(params->data[0] == startBuzz[0]){
jurica238814 25:8ac3ff431ab1 191 if(params->data[1] == startBuzz[1]){
jurica238814 17:51a5456a46cd 192 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 193 connectedLED = !connectedLED; // BLUE
jurica238814 17:51a5456a46cd 194 wait_ms(100);
jurica238814 17:51a5456a46cd 195 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 196 wait_ms(100);
jurica238814 17:51a5456a46cd 197 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 198 wait_ms(100);
jurica238814 17:51a5456a46cd 199 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 200 wait_ms(100);
jurica238814 17:51a5456a46cd 201 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 202 wait_ms(100);
jurica238814 17:51a5456a46cd 203 connectedLED = 1;
jurica238814 17:51a5456a46cd 204 wait_ms(100);
jurica238814 17:51a5456a46cd 205 #endif
Dautor 26:148aa2e2460c 206 buzzerStart();
jurica238814 16:a338d2417fd5 207 return;
jurica238814 16:a338d2417fd5 208 }
jurica238814 25:8ac3ff431ab1 209 }
Dautor 26:148aa2e2460c 210 else if(params->data[0] == stopBuzz[0]){
Dautor 26:148aa2e2460c 211 if(params->data[1] == stopBuzz[1]){
jurica238814 27:2c67f07590fd 212 buzzerStop();
Dautor 26:148aa2e2460c 213 WakeSleepT.detach();
Dautor 26:148aa2e2460c 214 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
Dautor 26:148aa2e2460c 215 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
jurica238814 25:8ac3ff431ab1 216 }
jurica238814 25:8ac3ff431ab1 217 }
Dautor 26:148aa2e2460c 218 else if(params->data[0] == 0x55){
jurica238814 27:2c67f07590fd 219 /* This is used after pairing */
jurica238814 27:2c67f07590fd 220 radioState = SCANNING;
jurica238814 27:2c67f07590fd 221 WakeSleepT.detach();
jurica238814 27:2c67f07590fd 222 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 27:2c67f07590fd 223 shushShush = true;
Dautor 26:148aa2e2460c 224 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
Dautor 26:148aa2e2460c 225 }
Dautor 26:148aa2e2460c 226
jurica238814 16:a338d2417fd5 227 }
jurica238814 16:a338d2417fd5 228 else{
jurica238814 25:8ac3ff431ab1 229 // Execute this for wrong data written into characteristic
jurica238814 25:8ac3ff431ab1 230 return;
jurica238814 16:a338d2417fd5 231 }
jurica238814 16:a338d2417fd5 232 }
jurica238814 0:f8c1e0b2d473 233
jurica238814 0:f8c1e0b2d473 234 /**
jurica238814 0:f8c1e0b2d473 235 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 236 */
jurica238814 0:f8c1e0b2d473 237 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 238 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 239 (void) ble;
jurica238814 0:f8c1e0b2d473 240 (void) error;
jurica238814 0:f8c1e0b2d473 241 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 242 }
jurica238814 0:f8c1e0b2d473 243
jurica238814 0:f8c1e0b2d473 244 /**
jurica238814 0:f8c1e0b2d473 245 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 246 */
jurica238814 0:f8c1e0b2d473 247 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 248 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 249 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 250
jurica238814 0:f8c1e0b2d473 251 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 252 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 253 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 254 return;
jurica238814 0:f8c1e0b2d473 255 }
jurica238814 0:f8c1e0b2d473 256
jurica238814 0:f8c1e0b2d473 257 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 258 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 259 return;
jurica238814 0:f8c1e0b2d473 260 }
jurica238814 16:a338d2417fd5 261
jurica238814 16:a338d2417fd5 262 uint8_t init_values[4] = {0,0,0,0};
jurica238814 1:5f34885f5cff 263 /* Get my MAC address */
jurica238814 1:5f34885f5cff 264 BLEProtocol::AddressType_t temp_address_type;
jurica238814 14:d506c0679c0b 265 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 266 ackServicePtr = new ACKService<4>(ble, init_values);
jurica238814 16:a338d2417fd5 267 ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address
jurica238814 21:10c3b8176be0 268
jurica238814 16:a338d2417fd5 269 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 17:51a5456a46cd 270 ble.gap().onConnection(onConnectionCallback); // -->> Uncomment these two lines for shush-shush
jurica238814 17:51a5456a46cd 271 ble.gattServer().onDataWritten(onDataWrittenCallback);
jurica238814 1:5f34885f5cff 272
jurica238814 14:d506c0679c0b 273 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
jurica238814 31:caef580f5943 274
jurica238814 31:caef580f5943 275 /*
jurica238814 31:caef580f5943 276 * iBeacon
jurica238814 31:caef580f5943 277 */
jurica238814 31:caef580f5943 278 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 31:caef580f5943 279 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&ibeaconMSD, sizeof(ibeaconMSD));
jurica238814 31:caef580f5943 280
jurica238814 10:fd91664032d8 281 ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms!
jurica238814 0:f8c1e0b2d473 282 }
jurica238814 0:f8c1e0b2d473 283
jurica238814 31:caef580f5943 284 void changeAdvPaylod(advPayloadType newState){
jurica238814 31:caef580f5943 285 if(newState == iBEACON){
jurica238814 31:caef580f5943 286 /*
jurica238814 31:caef580f5943 287 * iBeacon
jurica238814 31:caef580f5943 288 */
jurica238814 31:caef580f5943 289 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 31:caef580f5943 290 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&ibeaconMSD, sizeof(ibeaconMSD));
jurica238814 31:caef580f5943 291 }
jurica238814 31:caef580f5943 292 else{
jurica238814 31:caef580f5943 293 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
jurica238814 31:caef580f5943 294 }
jurica238814 31:caef580f5943 295 }
jurica238814 3:2a4ac5b87046 296
jurica238814 3:2a4ac5b87046 297 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 298 uint8_t i=0;
jurica238814 18:e844d3e6ab88 299 uint8_t advLen = params->advertisingDataLen;
jurica238814 18:e844d3e6ab88 300 uint8_t dataLen;
jurica238814 18:e844d3e6ab88 301
jurica238814 18:e844d3e6ab88 302 if((advLen < (MAC_SIZE_B + 2)) || advLen == 0){
jurica238814 15:934a04c958f5 303 // Empty advertisement or not long enough for MAX
jurica238814 18:e844d3e6ab88 304 // +2 for SIZE and MSD ID
jurica238814 15:934a04c958f5 305 return 0;
jurica238814 15:934a04c958f5 306 }
jurica238814 3:2a4ac5b87046 307
jurica238814 3:2a4ac5b87046 308 do{
jurica238814 18:e844d3e6ab88 309 dataLen = params->advertisingData[i];
jurica238814 3:2a4ac5b87046 310 i++;
jurica238814 3:2a4ac5b87046 311 if(params->advertisingData[i] == MSD_ID) return i;
jurica238814 18:e844d3e6ab88 312 else i += (dataLen);
jurica238814 18:e844d3e6ab88 313 }while(i<advLen);
jurica238814 3:2a4ac5b87046 314
jurica238814 3:2a4ac5b87046 315 return 0;
jurica238814 3:2a4ac5b87046 316 }
jurica238814 3:2a4ac5b87046 317
jurica238814 14:d506c0679c0b 318 uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){
jurica238814 14:d506c0679c0b 319 int i=0;
jurica238814 14:d506c0679c0b 320
jurica238814 16:a338d2417fd5 321 /* Get my MAC address */
jurica238814 16:a338d2417fd5 322 BLEProtocol::AddressType_t temp_address_type;
jurica238814 16:a338d2417fd5 323 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 324
jurica238814 18:e844d3e6ab88 325 if(!msdOffset){
jurica238814 17:51a5456a46cd 326 #if DEBUG_MAC
jurica238814 16:a338d2417fd5 327 for(i=0; i<10; i++){
jurica238814 16:a338d2417fd5 328 scanLED = !scanLED; // BLUE
jurica238814 16:a338d2417fd5 329 wait_ms(100);
jurica238814 16:a338d2417fd5 330 }
jurica238814 17:51a5456a46cd 331 #endif
jurica238814 14:d506c0679c0b 332 return 0; // There's no MSD in BLE advertisement data
jurica238814 14:d506c0679c0b 333 }
jurica238814 14:d506c0679c0b 334 for(i=0; i<6; i++){
jurica238814 16:a338d2417fd5 335 if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){ // myMacAddress[0] == 0x91
jurica238814 17:51a5456a46cd 336 #if DEBUG_MAC
jurica238814 17:51a5456a46cd 337 for(i=0; i<10; i++){
jurica238814 17:51a5456a46cd 338 connectedLED = !connectedLED; // Green
jurica238814 17:51a5456a46cd 339 wait_ms(100);
jurica238814 17:51a5456a46cd 340 }
jurica238814 17:51a5456a46cd 341 #endif
jurica238814 14:d506c0679c0b 342 return 0;
jurica238814 14:d506c0679c0b 343 }
jurica238814 14:d506c0679c0b 344 }
jurica238814 17:51a5456a46cd 345 #if DEBUG_MAC
jurica238814 17:51a5456a46cd 346 for(i=0; i<10; i++){
jurica238814 17:51a5456a46cd 347 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 348 wait_ms(100);
jurica238814 17:51a5456a46cd 349 }
jurica238814 18:e844d3e6ab88 350 advLED = 1;
jurica238814 17:51a5456a46cd 351 #endif
jurica238814 14:d506c0679c0b 352 return 1;
jurica238814 14:d506c0679c0b 353 }
jurica238814 14:d506c0679c0b 354
jurica238814 3:2a4ac5b87046 355 /**
jurica238814 3:2a4ac5b87046 356 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 357 */
jurica238814 3:2a4ac5b87046 358 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 19:abf14a5ada93 359 uint8_t msdOffset;
jurica238814 15:934a04c958f5 360
jurica238814 18:e844d3e6ab88 361 msdOffset = findMSDIndex(params); // Should be 1 or 4
jurica238814 3:2a4ac5b87046 362 if(msdOffset == 0){
jurica238814 14:d506c0679c0b 363 return; // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 364 }
jurica238814 14:d506c0679c0b 365 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 366 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 367 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 368 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 16:a338d2417fd5 369 if(CheckMac(params, myMacAddress, msdOffset)){
jurica238814 17:51a5456a46cd 370 //ble.gap().stopScan();
jurica238814 14:d506c0679c0b 371 WakeSleepT.detach();
jurica238814 22:8d106fd5fa84 372 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 1:5f34885f5cff 373 }
jurica238814 1:5f34885f5cff 374 }
jurica238814 1:5f34885f5cff 375 }
jurica238814 2:5504b714c9ae 376 }
jurica238814 2:5504b714c9ae 377 }
jurica238814 2:5504b714c9ae 378
jurica238814 12:6b072c2a061c 379
jurica238814 7:89c9abaa257e 380 /* Call this function few minutes (TBD) after a last scanned advertisment */
jurica238814 7:89c9abaa257e 381 void changeSleepTime(){
jurica238814 14:d506c0679c0b 382 tempSleepTime = SLEEP_TIME_S;
jurica238814 7:89c9abaa257e 383 sleepChanger.detach();
jurica238814 7:89c9abaa257e 384 }
jurica238814 7:89c9abaa257e 385
jurica238814 10:fd91664032d8 386 void startAdvertising(){
jurica238814 31:caef580f5943 387 static uint8_t cnt = 0;
jurica238814 31:caef580f5943 388 advPayloadType nextState;
jurica238814 16:a338d2417fd5 389 if(shushShush){
jurica238814 16:a338d2417fd5 390 // Do not advertise! Go to sleep
jurica238814 16:a338d2417fd5 391 WakeSleepT.detach();
jurica238814 16:a338d2417fd5 392 ble.gap().stopAdvertising();
jurica238814 16:a338d2417fd5 393 WakeMeUp();
jurica238814 16:a338d2417fd5 394 }
jurica238814 16:a338d2417fd5 395 else{
jurica238814 31:caef580f5943 396 if(!cnt){
jurica238814 31:caef580f5943 397 nextState = iBEACON;
jurica238814 31:caef580f5943 398 changeAdvPaylod(nextState);
jurica238814 31:caef580f5943 399 }
jurica238814 31:caef580f5943 400 if(cnt==1){
jurica238814 31:caef580f5943 401 nextState = BEACON;
jurica238814 31:caef580f5943 402 changeAdvPaylod(nextState);
jurica238814 31:caef580f5943 403 }
jurica238814 31:caef580f5943 404 cnt = (cnt+1)%5; // Everyfifth advertising change to iBeacon
jurica238814 31:caef580f5943 405
jurica238814 31:caef580f5943 406 ble.gap().startAdvertising();
jurica238814 31:caef580f5943 407 #if DEBUG
jurica238814 31:caef580f5943 408 advLED = 0;
jurica238814 31:caef580f5943 409 scanLED = 1;
jurica238814 31:caef580f5943 410 #endif
jurica238814 31:caef580f5943 411 WakeSleepT.detach();
jurica238814 31:caef580f5943 412 WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
jurica238814 16:a338d2417fd5 413 }
jurica238814 10:fd91664032d8 414 }
jurica238814 10:fd91664032d8 415
jurica238814 10:fd91664032d8 416 void startScanning(){
jurica238814 10:fd91664032d8 417 ble.gap().stopAdvertising();
jurica238814 23:729717272b31 418 ble.gap().setScanInterval(SCAN_INTERVAL);
jurica238814 23:729717272b31 419 ble.gap().setScanWindow(SCAN_WINDOW);
jurica238814 23:729717272b31 420 ble.gap().setScanParams();
jurica238814 2:5504b714c9ae 421 ble.gap().startScan(advertisementCallback);
jurica238814 10:fd91664032d8 422 #if DEBUG
jurica238814 10:fd91664032d8 423 advLED = 1;
jurica238814 10:fd91664032d8 424 scanLED = 0;
jurica238814 10:fd91664032d8 425 #endif
jurica238814 2:5504b714c9ae 426 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 427 WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S);
jurica238814 10:fd91664032d8 428 }
jurica238814 10:fd91664032d8 429
jurica238814 10:fd91664032d8 430 void WakeMeUp(){
jurica238814 8:570eb66d50b5 431 sleepFlag = 0;
jurica238814 10:fd91664032d8 432 switch(radioState){
jurica238814 10:fd91664032d8 433 case OFF:{
jurica238814 25:8ac3ff431ab1 434 radioState = SCANNING;
jurica238814 10:fd91664032d8 435 startAdvertising();
jurica238814 10:fd91664032d8 436 break;
jurica238814 10:fd91664032d8 437 }
jurica238814 10:fd91664032d8 438 case ADVERTISING:{
jurica238814 14:d506c0679c0b 439 radioState = SCANNING;
jurica238814 10:fd91664032d8 440 startScanning();
jurica238814 10:fd91664032d8 441 break;
jurica238814 10:fd91664032d8 442 }
jurica238814 10:fd91664032d8 443 case SCANNING:{
jurica238814 10:fd91664032d8 444 radioState = OFF;
jurica238814 10:fd91664032d8 445 WakeSleepT.detach();
jurica238814 16:a338d2417fd5 446 //WakeSleepT.attach(GoToSleep, FREE_TIME_S);
jurica238814 16:a338d2417fd5 447 GoToSleep();
jurica238814 10:fd91664032d8 448 break;
jurica238814 10:fd91664032d8 449 }
jurica238814 10:fd91664032d8 450 default: return;
jurica238814 10:fd91664032d8 451 }
jurica238814 2:5504b714c9ae 452 }
jurica238814 2:5504b714c9ae 453
jurica238814 14:d506c0679c0b 454 void GoToSleep(){
jurica238814 2:5504b714c9ae 455 WakeSleepT.detach();
jurica238814 7:89c9abaa257e 456 WakeSleepT.attach(WakeMeUp, tempSleepTime);
jurica238814 2:5504b714c9ae 457 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 458 ble.gap().stopScan();
jurica238814 8:570eb66d50b5 459 sleepFlag = 1;
jurica238814 16:a338d2417fd5 460 #if DEBUG
jurica238814 16:a338d2417fd5 461 advLED = 1;
jurica238814 16:a338d2417fd5 462 scanLED = 1;
jurica238814 16:a338d2417fd5 463 #endif
jurica238814 1:5f34885f5cff 464 }
jurica238814 1:5f34885f5cff 465
jurica238814 19:abf14a5ada93 466 #if USE_ACC
jurica238814 27:2c67f07590fd 467 void pulse_handler(){
jurica238814 27:2c67f07590fd 468 #if DEBUG_WAKEUP_BUZZER
Dautor 26:148aa2e2460c 469 buzzerStart();
jurica238814 27:2c67f07590fd 470 wait_ms(50);
Dautor 26:148aa2e2460c 471 buzzerStop();
jurica238814 27:2c67f07590fd 472 #endif
jurica238814 27:2c67f07590fd 473 #if DEBUG_ACC
jurica238814 27:2c67f07590fd 474 int_led = !int_led;
jurica238814 27:2c67f07590fd 475 #endif
jurica238814 27:2c67f07590fd 476 shushShush = false;
jurica238814 19:abf14a5ada93 477 }
jurica238814 19:abf14a5ada93 478 #endif
jurica238814 6:d14e3df498f4 479
Dautor 26:148aa2e2460c 480 int main(){
jurica238814 18:e844d3e6ab88 481 #if DEBUG || DEBUG_MAC
jurica238814 10:fd91664032d8 482 advLED = 1;
jurica238814 10:fd91664032d8 483 scanLED = 1;
jurica238814 16:a338d2417fd5 484 connectedLED = 1;
jurica238814 10:fd91664032d8 485 #endif
jurica238814 14:d506c0679c0b 486
jurica238814 19:abf14a5ada93 487 #if USE_ACC
jurica238814 19:abf14a5ada93 488 accPower = 1;
jurica238814 19:abf14a5ada93 489 i2cPower = 1;
jurica238814 27:2c67f07590fd 490 #endif
jurica238814 16:a338d2417fd5 491
jurica238814 21:10c3b8176be0 492 //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
jurica238814 21:10c3b8176be0 493 GoToSleep();
jurica238814 16:a338d2417fd5 494 ble.init(bleInitComplete);
jurica238814 16:a338d2417fd5 495 ble.gap().setTxPower(txPower);
jurica238814 19:abf14a5ada93 496 /* SpinWait for initialization to complete. This is necessary because the BLE object is used in the main loop below. */
Dautor 26:148aa2e2460c 497 while (ble.hasInitialized() == false){ /* spin loop */ }
jurica238814 31:caef580f5943 498 __enable_irq();
jurica238814 31:caef580f5943 499
jurica238814 27:2c67f07590fd 500 buzzerStart();
jurica238814 27:2c67f07590fd 501 wait_ms(500);
jurica238814 27:2c67f07590fd 502 buzzerStop();
jurica238814 27:2c67f07590fd 503
jurica238814 31:caef580f5943 504 ibeaconMSD.appleID = 0x004C;
jurica238814 31:caef580f5943 505 ibeaconMSD.secondID = 0x02;
jurica238814 31:caef580f5943 506 ibeaconMSD.DataSize = 0x15;
jurica238814 31:caef580f5943 507 ibeaconMSD.UUID[0] = 0x11;
jurica238814 31:caef580f5943 508 ibeaconMSD.UUID[1] = 0x22;
jurica238814 31:caef580f5943 509 ibeaconMSD.UUID[2] = 0x33;
jurica238814 31:caef580f5943 510 ibeaconMSD.RSSI = -4;
jurica238814 31:caef580f5943 511
jurica238814 31:caef580f5943 512 WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
jurica238814 27:2c67f07590fd 513
jurica238814 27:2c67f07590fd 514 while(true){
jurica238814 27:2c67f07590fd 515 ble.waitForEvent();
jurica238814 0:f8c1e0b2d473 516 }
jurica238814 0:f8c1e0b2d473 517 }