Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Mon Nov 27 15:24:13 2017 +0000
Revision:
33:449fcc43b287
Parent:
32:61d2328ec1a7
Child:
34:9856c51ec646
iBeacon removed. 128bit UUID added (iPhone works).

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