![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test
Revision 23:3c342cffd585, committed 2019-04-20
- Comitter:
- krissl
- Date:
- Sat Apr 20 12:37:54 2019 +0000
- Parent:
- 22:c394411633f6
- Commit message:
- test;
Changed in this revision
source/SMDevicePeripheral.cpp | Show annotated file Show diff for this revision Revisions of this file |
source/main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r c394411633f6 -r 3c342cffd585 source/SMDevicePeripheral.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/SMDevicePeripheral.cpp Sat Apr 20 12:37:54 2019 +0000 @@ -0,0 +1,93 @@ + +/** A peripheral device will advertise, accept the connection and request + * a change in link security. */ +class SMDevicePeripheral : public SMDevice { +public: + SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address) + : SMDevice(ble, event_queue, peer_address) { } + + virtual void start() + { + /* Set up and start advertising */ + uint8_t adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; + /* use the helper to build the payload */ + ble::AdvertisingDataBuilder adv_data_builder( + adv_buffer + ); + + adv_data_builder.setFlags(); + adv_data_builder.setName(DEVICE_NAME); + + /* Set payload for the set */ + ble_error_t error = _ble.gap().setAdvertisingPayload( + ble::LEGACY_ADVERTISING_HANDLE, + adv_data_builder.getAdvertisingData() + ); + + if (error) { + print_error(error, "Gap::setAdvertisingPayload() failed"); + _event_queue.break_dispatch(); + return; + } + + ble::AdvertisingParameters adv_parameters( + ble::advertising_type_t::CONNECTABLE_UNDIRECTED + ); + + error = _ble.gap().setAdvertisingParameters( + ble::LEGACY_ADVERTISING_HANDLE, + adv_parameters + ); + + if (error) { + print_error(error, "Gap::setAdvertisingParameters() failed"); + return; + } + + error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); + + if (error) { + print_error(error, "Gap::startAdvertising() failed"); + return; + } + + printf("Please connect to device\r\n"); + + /** This tells the stack to generate a pairingRequest event + * which will require this application to respond before pairing + * can proceed. Setting it to false will automatically accept + * pairing. */ + _ble.securityManager().setPairingRequestAuthorisation(true); + }; + + /** This is called by Gap to notify the application we connected, + * in our case it immediately requests a change in link security */ + virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) + { + ble_error_t error; + + /* remember the device that connects to us now so we can connect to it + * during the next demonstration */ + memcpy(_peer_address, event.getPeerAddress().data(), sizeof(_peer_address)); + + printf("Connected to peer: "); + print_address(event.getPeerAddress().data()); + + _handle = event.getConnectionHandle(); + + /* Request a change in link security. This will be done + * indirectly by asking the master of the connection to + * change it. Depending on circumstances different actions + * may be taken by the master which will trigger events + * which the applications should deal with. */ + error = _ble.securityManager().setLinkSecurity( + _handle, + SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM + ); + + if (error) { + printf("Error during SM::setLinkSecurity %d\r\n", error); + return; + } + }; +}; \ No newline at end of file
diff -r c394411633f6 -r 3c342cffd585 source/main.cpp --- a/source/main.cpp Fri Mar 29 12:01:28 2019 +0000 +++ b/source/main.cpp Sat Apr 20 12:37:54 2019 +0000 @@ -414,7 +414,9 @@ if (_is_connecting) { return; } - + printf("found someone "); + printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n", + event.getPeerAddress().data()[5], event.getPeerAddress().data()[4], event.getPeerAddress().data()[3], event.getPeerAddress().data()[2], event.getPeerAddress().data()[1], event.getPeerAddress().data()[0]); /* parse the advertising payload, looking for a discoverable device */ if (memcmp(event.getPeerAddress().data(), _peer_address, sizeof(_peer_address)) == 0) { ble_error_t error = _ble.gap().stopScan(); @@ -538,11 +540,11 @@ #endif while(1) { - { - printf("\r\n PERIPHERAL \r\n\r\n"); - SMDevicePeripheral peripheral(ble, queue, peer_address); - peripheral.run(); - } + // { +// printf("\r\n PERIPHERAL \r\n\r\n"); +// SMDevicePeripheral peripheral(ble, queue, peer_address); +// peripheral.run(); +// } { printf("\r\n CENTRAL \r\n\r\n");