Dissertation project, looking at BLE communication in cars. This project is BLE peripheral acting as car indicator stalk
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_test1 by
main.cpp@1:ebdf445c4bcc, 2015-01-18 (annotated)
- Committer:
- alexanderlea
- Date:
- Sun Jan 18 15:46:43 2015 +0000
- Revision:
- 1:ebdf445c4bcc
- Parent:
- 0:af868ad47854
- Child:
- 2:f538ff758828
Renamed variable from level to command
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexanderlea | 0:af868ad47854 | 1 | /* |
alexanderlea | 0:af868ad47854 | 2 | * TODO: |
alexanderlea | 0:af868ad47854 | 3 | * - Make it work! |
alexanderlea | 0:af868ad47854 | 4 | */ |
alexanderlea | 0:af868ad47854 | 5 | |
alexanderlea | 0:af868ad47854 | 6 | #include "mbed.h" |
alexanderlea | 0:af868ad47854 | 7 | #include "BLEDevice.h" |
alexanderlea | 0:af868ad47854 | 8 | #include "CarCommsService.h" |
alexanderlea | 0:af868ad47854 | 9 | #include "TestGattService.h" |
alexanderlea | 0:af868ad47854 | 10 | #include <string> |
alexanderlea | 0:af868ad47854 | 11 | |
alexanderlea | 0:af868ad47854 | 12 | using namespace std; |
alexanderlea | 0:af868ad47854 | 13 | |
alexanderlea | 0:af868ad47854 | 14 | BLEDevice ble; |
alexanderlea | 0:af868ad47854 | 15 | DigitalOut led1(LED1); |
alexanderlea | 0:af868ad47854 | 16 | Serial pc(USBTX, USBRX); |
alexanderlea | 0:af868ad47854 | 17 | |
alexanderlea | 0:af868ad47854 | 18 | /*Variable Declarations*/ |
alexanderlea | 1:ebdf445c4bcc | 19 | const static char DEVICE_NAME[] = "BLE_NODE"; |
alexanderlea | 0:af868ad47854 | 20 | |
alexanderlea | 0:af868ad47854 | 21 | ///* |
alexanderlea | 0:af868ad47854 | 22 | //* Define a custom UUID, first as an array of uint8_t and then convert to |
alexanderlea | 0:af868ad47854 | 23 | //* a proper UUID later. The UUID must be 16 bytes (128-bits, 16 letters) |
alexanderlea | 0:af868ad47854 | 24 | //* long - here we have padded out to 16 bytes by adding an extra '0' at |
alexanderlea | 0:af868ad47854 | 25 | //* the end. Make sure you fill more than 4 bytes otherwise it will count |
alexanderlea | 0:af868ad47854 | 26 | //* as a 'short' code and you could end up using a predefined value from |
alexanderlea | 0:af868ad47854 | 27 | //* the BLE spec. |
alexanderlea | 0:af868ad47854 | 28 | //*/ |
alexanderlea | 0:af868ad47854 | 29 | //uint8_t raw_characteristic_uuid[16] = { |
alexanderlea | 0:af868ad47854 | 30 | // 'M', 'Y', '_', 'T', |
alexanderlea | 0:af868ad47854 | 31 | // 'E', 'S', 'T', '_', |
alexanderlea | 0:af868ad47854 | 32 | // 'C', 'H', 'A', 'R', |
alexanderlea | 0:af868ad47854 | 33 | // 0, 0, 0, 0 |
alexanderlea | 0:af868ad47854 | 34 | //}; |
alexanderlea | 0:af868ad47854 | 35 | // |
alexanderlea | 0:af868ad47854 | 36 | //// Create a proper UUID |
alexanderlea | 0:af868ad47854 | 37 | //UUID characteristic_uuid = {0x4d,0x32,0x81,0xc0,0x86,0xd1,0x11,0xe4,0xb0,0x84,0x00,0x02,0xa5,0xd5,0xc5,0x10}; |
alexanderlea | 0:af868ad47854 | 38 | |
alexanderlea | 0:af868ad47854 | 39 | static volatile bool connected; |
alexanderlea | 0:af868ad47854 | 40 | //Ticker ticker; |
alexanderlea | 0:af868ad47854 | 41 | // |
alexanderlea | 0:af868ad47854 | 42 | // |
alexanderlea | 0:af868ad47854 | 43 | //// Setup some dummy properties for our characteristic |
alexanderlea | 0:af868ad47854 | 44 | //static uint8_t my_char_values[2] = { 15, 10 }; |
alexanderlea | 0:af868ad47854 | 45 | // |
alexanderlea | 0:af868ad47854 | 46 | ///* |
alexanderlea | 0:af868ad47854 | 47 | //* Here we create our Characteristic adding in the dummy parameter values |
alexanderlea | 0:af868ad47854 | 48 | //* we just setup, we also make it readable and writeable meaning that a |
alexanderlea | 0:af868ad47854 | 49 | //* central can read and update the parameters we have stored. |
alexanderlea | 0:af868ad47854 | 50 | //*/ |
alexanderlea | 0:af868ad47854 | 51 | //GattCharacteristic pattern( |
alexanderlea | 0:af868ad47854 | 52 | // characteristic_uuid, |
alexanderlea | 0:af868ad47854 | 53 | // my_char_values, |
alexanderlea | 0:af868ad47854 | 54 | // sizeof(my_char_values), |
alexanderlea | 0:af868ad47854 | 55 | // sizeof(my_char_values), |
alexanderlea | 0:af868ad47854 | 56 | // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
alexanderlea | 0:af868ad47854 | 57 | //); |
alexanderlea | 0:af868ad47854 | 58 | // |
alexanderlea | 0:af868ad47854 | 59 | ///* |
alexanderlea | 0:af868ad47854 | 60 | //* List the Characteristics of our custom Service, can have one or more |
alexanderlea | 0:af868ad47854 | 61 | //* of these each with a custom UUID and parameter values. |
alexanderlea | 0:af868ad47854 | 62 | //*/ |
alexanderlea | 0:af868ad47854 | 63 | //GattCharacteristic *my_service_chars[] = { |
alexanderlea | 0:af868ad47854 | 64 | // &new_alert, |
alexanderlea | 0:af868ad47854 | 65 | //}; |
alexanderlea | 0:af868ad47854 | 66 | // |
alexanderlea | 0:af868ad47854 | 67 | // Now setup a custom Service UUID, in the same fashion as for the Characteristic |
alexanderlea | 0:af868ad47854 | 68 | //uint8_t raw_service_uuid[16] = { |
alexanderlea | 0:af868ad47854 | 69 | // 'M', 'Y', '_', 'T', |
alexanderlea | 0:af868ad47854 | 70 | // 'E', 'S', 'T', '_', |
alexanderlea | 0:af868ad47854 | 71 | // 'S', 'E', 'R', 'V', |
alexanderlea | 0:af868ad47854 | 72 | // 'I', 'C', 'E', 0 |
alexanderlea | 0:af868ad47854 | 73 | //}; |
alexanderlea | 0:af868ad47854 | 74 | |
alexanderlea | 0:af868ad47854 | 75 | |
alexanderlea | 0:af868ad47854 | 76 | |
alexanderlea | 0:af868ad47854 | 77 | // |
alexanderlea | 0:af868ad47854 | 78 | //UUID service_uuid = UUID(raw_service_uuid); |
alexanderlea | 0:af868ad47854 | 79 | //// Setup the Service with the UUID and all of the Characteristics |
alexanderlea | 0:af868ad47854 | 80 | //GattService my_service( |
alexanderlea | 0:af868ad47854 | 81 | // service_uuid, |
alexanderlea | 0:af868ad47854 | 82 | // my_service_chars, |
alexanderlea | 0:af868ad47854 | 83 | // sizeof(my_service_chars) / sizeof(GattCharacteristic *) |
alexanderlea | 0:af868ad47854 | 84 | //); |
alexanderlea | 0:af868ad47854 | 85 | // |
alexanderlea | 0:af868ad47854 | 86 | ///* |
alexanderlea | 0:af868ad47854 | 87 | //* Now list the long UUIDs of the services we offer, these will be bundled into the |
alexanderlea | 0:af868ad47854 | 88 | //* advertisement. It may look like repetition of 'raw_service_uuid' but here you can |
alexanderlea | 0:af868ad47854 | 89 | //* list multiple UUIDs one after another. |
alexanderlea | 0:af868ad47854 | 90 | //*/ |
alexanderlea | 0:af868ad47854 | 91 | //static const uint8_t uuid128_list[] = { |
alexanderlea | 0:af868ad47854 | 92 | // 'M', 'Y', '_', 'T', 'E', 'S', 'T', '_', 'S', 'E', 'R', 'V', 'I', 'C', 'E', 0 |
alexanderlea | 0:af868ad47854 | 93 | // // List more long UUIDs below... |
alexanderlea | 0:af868ad47854 | 94 | //}; |
alexanderlea | 0:af868ad47854 | 95 | |
alexanderlea | 0:af868ad47854 | 96 | void blink(void) |
alexanderlea | 0:af868ad47854 | 97 | { |
alexanderlea | 0:af868ad47854 | 98 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
alexanderlea | 0:af868ad47854 | 99 | } |
alexanderlea | 0:af868ad47854 | 100 | |
alexanderlea | 0:af868ad47854 | 101 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
alexanderlea | 0:af868ad47854 | 102 | { |
alexanderlea | 0:af868ad47854 | 103 | pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason); |
alexanderlea | 0:af868ad47854 | 104 | connected=false; |
alexanderlea | 0:af868ad47854 | 105 | ble.startAdvertising(); |
alexanderlea | 0:af868ad47854 | 106 | } |
alexanderlea | 0:af868ad47854 | 107 | |
alexanderlea | 0:af868ad47854 | 108 | |
alexanderlea | 0:af868ad47854 | 109 | //Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t * |
alexanderlea | 0:af868ad47854 | 110 | void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms) |
alexanderlea | 0:af868ad47854 | 111 | { |
alexanderlea | 0:af868ad47854 | 112 | pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr); |
alexanderlea | 0:af868ad47854 | 113 | |
alexanderlea | 0:af868ad47854 | 114 | connected = true; |
alexanderlea | 0:af868ad47854 | 115 | // ble.stopAdvertising(); |
alexanderlea | 0:af868ad47854 | 116 | } |
alexanderlea | 0:af868ad47854 | 117 | |
alexanderlea | 0:af868ad47854 | 118 | int main(void) |
alexanderlea | 0:af868ad47854 | 119 | { |
alexanderlea | 0:af868ad47854 | 120 | uint8_t testLevel = 50; |
alexanderlea | 0:af868ad47854 | 121 | |
alexanderlea | 0:af868ad47854 | 122 | //blinky |
alexanderlea | 0:af868ad47854 | 123 | led1 = 1; |
alexanderlea | 0:af868ad47854 | 124 | Ticker t; |
alexanderlea | 0:af868ad47854 | 125 | t.attach(blink, 1.0f); |
alexanderlea | 0:af868ad47854 | 126 | |
alexanderlea | 0:af868ad47854 | 127 | //Create BLE stuff |
alexanderlea | 0:af868ad47854 | 128 | ble.init(); |
alexanderlea | 0:af868ad47854 | 129 | ble.onDisconnection(disconnectionCallback); |
alexanderlea | 0:af868ad47854 | 130 | ble.onConnection(connectionCallback); |
alexanderlea | 0:af868ad47854 | 131 | |
alexanderlea | 0:af868ad47854 | 132 | //CarCommsService commsService(ble, cmd); |
alexanderlea | 0:af868ad47854 | 133 | TestGattService testService(ble, testLevel); |
alexanderlea | 0:af868ad47854 | 134 | |
alexanderlea | 0:af868ad47854 | 135 | |
alexanderlea | 0:af868ad47854 | 136 | /* |
alexanderlea | 0:af868ad47854 | 137 | **BREDR_NOT_SUPPORTED = BLE only |
alexanderlea | 0:af868ad47854 | 138 | **LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out) |
alexanderlea | 0:af868ad47854 | 139 | **ADV_CONNECTABLE_UNDIRECTED = Any central device can connect |
alexanderlea | 0:af868ad47854 | 140 | */ |
alexanderlea | 0:af868ad47854 | 141 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
alexanderlea | 0:af868ad47854 | 142 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
alexanderlea | 0:af868ad47854 | 143 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
alexanderlea | 0:af868ad47854 | 144 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
alexanderlea | 0:af868ad47854 | 145 | |
alexanderlea | 0:af868ad47854 | 146 | //Advertise |
alexanderlea | 0:af868ad47854 | 147 | ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600 |
alexanderlea | 0:af868ad47854 | 148 | ble.startAdvertising(); |
alexanderlea | 0:af868ad47854 | 149 | |
alexanderlea | 0:af868ad47854 | 150 | pc.printf("Advertising node %s\n\r", DEVICE_NAME); |
alexanderlea | 0:af868ad47854 | 151 | |
alexanderlea | 0:af868ad47854 | 152 | //GATT service stuff - referring to CarCommsService.h |
alexanderlea | 0:af868ad47854 | 153 | //string testCommand = "Hello world"; |
alexanderlea | 0:af868ad47854 | 154 | // CarCommsService carComm(ble, 150); |
alexanderlea | 0:af868ad47854 | 155 | // |
alexanderlea | 0:af868ad47854 | 156 | // uint8_t x = carComm.getCommand(); |
alexanderlea | 0:af868ad47854 | 157 | // |
alexanderlea | 0:af868ad47854 | 158 | // pc.printf("Command: %u \n", x); |
alexanderlea | 0:af868ad47854 | 159 | |
alexanderlea | 0:af868ad47854 | 160 | |
alexanderlea | 0:af868ad47854 | 161 | while(true) { |
alexanderlea | 0:af868ad47854 | 162 | |
alexanderlea | 0:af868ad47854 | 163 | ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup) |
alexanderlea | 0:af868ad47854 | 164 | |
alexanderlea | 0:af868ad47854 | 165 | //if(led1 == 1) { |
alexanderlea | 0:af868ad47854 | 166 | // cmd = 1; |
alexanderlea | 0:af868ad47854 | 167 | // } else { |
alexanderlea | 0:af868ad47854 | 168 | // cmd = 0; |
alexanderlea | 0:af868ad47854 | 169 | // } |
alexanderlea | 0:af868ad47854 | 170 | // |
alexanderlea | 0:af868ad47854 | 171 | // commsService.sendCommand(0); |
alexanderlea | 0:af868ad47854 | 172 | // pc.printf("Command: %u\r\n", commsService.getCommand()); |
alexanderlea | 0:af868ad47854 | 173 | |
alexanderlea | 0:af868ad47854 | 174 | testLevel++; |
alexanderlea | 0:af868ad47854 | 175 | |
alexanderlea | 0:af868ad47854 | 176 | if (testLevel > 100) { |
alexanderlea | 1:ebdf445c4bcc | 177 | testLevel = 0; |
alexanderlea | 0:af868ad47854 | 178 | } |
alexanderlea | 0:af868ad47854 | 179 | |
alexanderlea | 0:af868ad47854 | 180 | pc.printf("Level = %u\r\n", testLevel); |
alexanderlea | 1:ebdf445c4bcc | 181 | testService.sendCommand(testLevel); |
alexanderlea | 0:af868ad47854 | 182 | |
alexanderlea | 0:af868ad47854 | 183 | //if(connected) { |
alexanderlea | 0:af868ad47854 | 184 | // led1 = 1; |
alexanderlea | 0:af868ad47854 | 185 | // //read data |
alexanderlea | 0:af868ad47854 | 186 | // //pc.printf("Command: %u \n", carComm.getCommand()); |
alexanderlea | 0:af868ad47854 | 187 | // } else { |
alexanderlea | 0:af868ad47854 | 188 | // led1 = 0; |
alexanderlea | 0:af868ad47854 | 189 | // pc.printf("Waiting...\n\r"); |
alexanderlea | 0:af868ad47854 | 190 | // ble.waitForEvent(); |
alexanderlea | 0:af868ad47854 | 191 | // connected = false; |
alexanderlea | 0:af868ad47854 | 192 | // } |
alexanderlea | 0:af868ad47854 | 193 | } |
alexanderlea | 0:af868ad47854 | 194 | } |