AdventCalendar2015
Dependencies: BLE_API mbed nRF51822
Fork of BLE_ButtonSense by
Revision 1:f66e5ed21c90, committed 2015-12-19
- Comitter:
- yueee_yt
- Date:
- Sat Dec 19 11:15:17 2015 +0000
- Parent:
- 0:2dec89c76f68
- Commit message:
- AdventCalendar2015
Changed in this revision
diff -r 2dec89c76f68 -r f66e5ed21c90 BLE_API.lib --- a/BLE_API.lib Mon Jul 06 09:30:41 2015 +0000 +++ b/BLE_API.lib Sat Dec 19 11:15:17 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#9c2edf20ea56 +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#bfc5b9b6ecf5
diff -r 2dec89c76f68 -r f66e5ed21c90 main.cpp --- a/main.cpp Mon Jul 06 09:30:41 2015 +0000 +++ b/main.cpp Sat Dec 19 11:15:17 2015 +0000 @@ -23,62 +23,261 @@ BLE ble; -Gap::Handle_t connectionHandle = 0xFFFF; +int mode=0; + +const uint8_t WriteCharacteristicAddr[]= {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; + +uint8_t peripheral1_ADDRESS[]= {0x3b ,0x72 ,0x34 ,0x88 ,0xe2 ,0xd4}; +uint8_t peripheral2_ADDRESS[]= {0x5c ,0xde ,0xa7 ,0xdd ,0xec ,0xe1}; +uint8_t peripheral3_ADDRESS[]= {0xf5 ,0x5a ,0x21 ,0xa5 ,0xc0 ,0xde}; +uint8_t peripheral4_ADDRESS[]= {0xe0 ,0x15 ,0xdc ,0x65 ,0x1b ,0xd0}; +uint8_t peripheral5_ADDRESS[]= {0x01 ,0xc6 ,0x18 ,0x2f ,0x66 ,0xdd}; + +Gap::Handle_t peripheral1_connectionHandle = 0xFFFF; +Gap::Handle_t peripheral2_connectionHandle = 0xFFFF; +Gap::Handle_t peripheral3_connectionHandle = 0xFFFF; +Gap::Handle_t peripheral4_connectionHandle = 0xFFFF; +Gap::Handle_t peripheral5_connectionHandle = 0xFFFF; + +DiscoveredCharacteristic peripheral1_WriteCharacteristic; +DiscoveredCharacteristic peripheral2_WriteCharacteristic; +DiscoveredCharacteristic peripheral3_WriteCharacteristic; +DiscoveredCharacteristic peripheral4_WriteCharacteristic; +DiscoveredCharacteristic peripheral5_WriteCharacteristic; DigitalOut alivenessLED(LED1, 1); -bool foundButtonCharacteristic = false; -DiscoveredCharacteristic buttonCharacteristic; void periodicCallback(void) { alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */ + int er; + if(mode==0){ + er= ble.gap().connect(peripheral1_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection + mode=1;printf("mode1 \r\n"); + if(er!=0)printf("connection error:%x \r\n",er); + } + if(mode==4){ + er= ble.gap().connect(peripheral2_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection + mode=11;printf("mode11 \r\n"); + if(er!=0)printf("connection error:%x \r\n",er); + } + if(mode==14){ + er= ble.gap().connect(peripheral3_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection + mode=21;printf("mode21 \r\n"); + if(er!=0)printf("connection error:%x \r\n",er); + } + if(mode==24){ + er= ble.gap().connect(peripheral4_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection + mode=31;printf("mode31 \r\n"); + if(er!=0)printf("connection error:%x \r\n",er); + } + if(mode==34){ + er= ble.gap().connect(peripheral5_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection + mode=41;printf("mode41 \r\n"); + if(er!=0)printf("connection error:%x \r\n",er); + } } void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { - if (params->peerAddr[0] != 0x29) { /* !ALERT! Update this filter to suit your device. */ - return; - } + //if (params->peerAddr[0] != 0x29) { /* !ALERT! Update this filter to suit your device. */ + // return; + //} printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type); - ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); + //ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); } -void serviceDiscoveryCallback(const DiscoveredService *service) { - if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { - printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); - } else { - printf("S UUID-"); - const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); - for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { - printf("%02x", longUUIDBytes[i]); - } - printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); +void peripheral1_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) +{ + bool flag; + if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) { + const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); + flag=true; + for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) + if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false; + if(flag==true) { + printf("#peripheral1_Write characteristic found\r\n"); + peripheral1_WriteCharacteristic = *characteristicP; + mode=3;printf("mode3 \r\n"); + } + } +} + +void peripheral2_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) +{ + bool flag; + if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) { + const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); + flag=true; + for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) + if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false; + if(flag==true) { + printf("#peripheral2_Write characteristic found\r\n"); + peripheral2_WriteCharacteristic = *characteristicP; + mode=13;printf("mode13 \r\n"); + } } } -void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { - printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); - if (characteristicP->getShortUUID() == 0xa001) { /* !ALERT! Update this filter to suit your device. */ - buttonCharacteristic = *characteristicP; - foundButtonCharacteristic = true; +void peripheral3_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) +{ + bool flag; + if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) { + const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); + flag=true; + for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) + if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false; + if(flag==true) { + printf("#peripheral3_Write characteristic found\r\n"); + peripheral3_WriteCharacteristic = *characteristicP; + mode=23;printf("mode23 \r\n"); + } + } +} + +void peripheral4_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) +{ + bool flag; + if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) { + const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); + flag=true; + for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) + if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false; + if(flag==true) { + printf("#peripheral4_Write characteristic found\r\n"); + peripheral4_WriteCharacteristic = *characteristicP; + mode=33;printf("mode33 \r\n"); + } + } +} + +void peripheral5_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) +{ + bool flag; + if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) { + const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); + flag=true; + for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) + if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false; + if(flag==true) { + printf("#peripheral5_Write characteristic found\r\n"); + peripheral5_WriteCharacteristic = *characteristicP; + mode=43;printf("mode43 \r\n"); + } } } void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { - printf("terminated SD for handle %u\r\n", connectionHandle); + if(peripheral1_connectionHandle==connectionHandle) { + printf("peripheral1_terminated SD\r\n"); + mode=4;printf("mode4 \r\n"); + } + if(peripheral2_connectionHandle==connectionHandle) { + printf("peripheral2_terminated SD\r\n"); + mode=14;printf("mode14 \r\n"); + } + if(peripheral3_connectionHandle==connectionHandle) { + printf("peripheral3_terminated SD\r\n"); + mode=24;printf("mode24 \r\n"); + } + if(peripheral4_connectionHandle==connectionHandle) { + printf("peripheral4_terminated SD\r\n"); + mode=34;printf("mode34 \r\n"); + } + if(peripheral5_connectionHandle==connectionHandle) { + printf("peripheral5_terminated SD\r\n"); + mode=44;printf("mode44 \r\n"); + } +} + +void peripheral1_serviceDiscoveryCallback(const DiscoveredService *service) +{ +} +void peripheral2_serviceDiscoveryCallback(const DiscoveredService *service) +{ +} +void peripheral3_serviceDiscoveryCallback(const DiscoveredService *service) +{ +} +void peripheral4_serviceDiscoveryCallback(const DiscoveredService *service) +{ +} +void peripheral5_serviceDiscoveryCallback(const DiscoveredService *service) +{ } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { + bool f; + int i; + printf("connection callback\r\n"); if (params->role == Gap::CENTRAL) { - connectionHandle = params->handle; - ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); - ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xa000, 0xa001*/); + + f=true; + for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral1_ADDRESS[i])f=false; + if(f==true) { + peripheral1_connectionHandle=params->handle; + ble.gattClient().launchServiceDiscovery(params->handle, peripheral1_serviceDiscoveryCallback, peripheral1_characteristicDiscoveryCallback); + ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); + mode=2;printf("mode2 \r\n"); + } + + f=true; + for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral2_ADDRESS[i])f=false; + if(f==true) { + peripheral2_connectionHandle=params->handle; + ble.gattClient().launchServiceDiscovery(params->handle, peripheral2_serviceDiscoveryCallback, peripheral2_characteristicDiscoveryCallback); + ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); + mode=12;printf("mode12 \r\n"); + } + + f=true; + for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral3_ADDRESS[i])f=false; + if(f==true) { + peripheral3_connectionHandle=params->handle; + ble.gattClient().launchServiceDiscovery(params->handle, peripheral3_serviceDiscoveryCallback, peripheral3_characteristicDiscoveryCallback); + ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); + mode=22;printf("mode22 \r\n"); + } + + f=true; + for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral4_ADDRESS[i])f=false; + if(f==true) { + peripheral4_connectionHandle=params->handle; + ble.gattClient().launchServiceDiscovery(params->handle, peripheral4_serviceDiscoveryCallback, peripheral4_characteristicDiscoveryCallback); + ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); + mode=32;printf("mode32 \r\n"); + } + + f=true; + for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral5_ADDRESS[i])f=false; + if(f==true) { + peripheral5_connectionHandle=params->handle; + ble.gattClient().launchServiceDiscovery(params->handle, peripheral5_serviceDiscoveryCallback, peripheral5_characteristicDiscoveryCallback); + ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); + mode=42;printf("mode42 \r\n"); + } } } -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { - printf("disconnected\r\n"); +//void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ + printf("disconnect\r\n"); + if(peripheral1_connectionHandle==params->handle) { + printf("peripheral1:Disconnected\r\n"); + } + if(peripheral2_connectionHandle==params->handle) { + printf("peripheral2:Disconnected\r\n"); + } + if(peripheral3_connectionHandle==params->handle) { + printf("peripheral3:Disconnected\r\n"); + } + if(peripheral4_connectionHandle==params->handle) { + printf("peripheral4:Disconnected\r\n"); + } + if(peripheral5_connectionHandle==params->handle) { + printf("peripheral5:Disconnected\r\n"); + } } void hvxCallback(const GattHVXCallbackParams *params) { @@ -97,25 +296,12 @@ ble.gap().onConnection(connectionCallback); ble.gap().onDisconnection(disconnectionCallback); - ble.gap().setScanParams(500, 400); - ble.gap().startScan(advertisementCallback); + // ble.gap().setScanParams(500, 400); + // ble.gap().startScan(advertisementCallback); - ble.gattClient().onHVX(hvxCallback); + // ble.gattClient().onHVX(hvxCallback); while (true) { - if (foundButtonCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) { - foundButtonCharacteristic = false; /* need to do the following only once */ - - /* Note: Yuckiness alert! The following needs to be encapsulated in a neat API. - * It isn't clear whether we should provide a DiscoveredCharacteristic::enableNoticiation() or - * DiscoveredCharacteristic::discoverDescriptors() followed by DiscoveredDescriptor::write(...). */ - uint16_t value = BLE_HVX_NOTIFICATION; - ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ, - connectionHandle, - buttonCharacteristic.getValueHandle() + 1, /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */ - sizeof(uint16_t), /* HACK Alert! size should be made into a BLE_API constant. */ - reinterpret_cast<const uint8_t *>(&value)); - } ble.waitForEvent(); } }
diff -r 2dec89c76f68 -r f66e5ed21c90 mbed.bld --- a/mbed.bld Mon Jul 06 09:30:41 2015 +0000 +++ b/mbed.bld Sat Dec 19 11:15:17 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file
diff -r 2dec89c76f68 -r f66e5ed21c90 nRF51822.lib --- a/nRF51822.lib Mon Jul 06 09:30:41 2015 +0000 +++ b/nRF51822.lib Sat Dec 19 11:15:17 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#6c82f06746bb +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0