Kris Scholte Lubberink
/
bleswitcher
test
source/SMDevicePeripheral.cpp@23:3c342cffd585, 2019-04-20 (annotated)
- Committer:
- krissl
- Date:
- Sat Apr 20 12:37:54 2019 +0000
- Revision:
- 23:3c342cffd585
test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krissl | 23:3c342cffd585 | 1 | |
krissl | 23:3c342cffd585 | 2 | /** A peripheral device will advertise, accept the connection and request |
krissl | 23:3c342cffd585 | 3 | * a change in link security. */ |
krissl | 23:3c342cffd585 | 4 | class SMDevicePeripheral : public SMDevice { |
krissl | 23:3c342cffd585 | 5 | public: |
krissl | 23:3c342cffd585 | 6 | SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address) |
krissl | 23:3c342cffd585 | 7 | : SMDevice(ble, event_queue, peer_address) { } |
krissl | 23:3c342cffd585 | 8 | |
krissl | 23:3c342cffd585 | 9 | virtual void start() |
krissl | 23:3c342cffd585 | 10 | { |
krissl | 23:3c342cffd585 | 11 | /* Set up and start advertising */ |
krissl | 23:3c342cffd585 | 12 | uint8_t adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; |
krissl | 23:3c342cffd585 | 13 | /* use the helper to build the payload */ |
krissl | 23:3c342cffd585 | 14 | ble::AdvertisingDataBuilder adv_data_builder( |
krissl | 23:3c342cffd585 | 15 | adv_buffer |
krissl | 23:3c342cffd585 | 16 | ); |
krissl | 23:3c342cffd585 | 17 | |
krissl | 23:3c342cffd585 | 18 | adv_data_builder.setFlags(); |
krissl | 23:3c342cffd585 | 19 | adv_data_builder.setName(DEVICE_NAME); |
krissl | 23:3c342cffd585 | 20 | |
krissl | 23:3c342cffd585 | 21 | /* Set payload for the set */ |
krissl | 23:3c342cffd585 | 22 | ble_error_t error = _ble.gap().setAdvertisingPayload( |
krissl | 23:3c342cffd585 | 23 | ble::LEGACY_ADVERTISING_HANDLE, |
krissl | 23:3c342cffd585 | 24 | adv_data_builder.getAdvertisingData() |
krissl | 23:3c342cffd585 | 25 | ); |
krissl | 23:3c342cffd585 | 26 | |
krissl | 23:3c342cffd585 | 27 | if (error) { |
krissl | 23:3c342cffd585 | 28 | print_error(error, "Gap::setAdvertisingPayload() failed"); |
krissl | 23:3c342cffd585 | 29 | _event_queue.break_dispatch(); |
krissl | 23:3c342cffd585 | 30 | return; |
krissl | 23:3c342cffd585 | 31 | } |
krissl | 23:3c342cffd585 | 32 | |
krissl | 23:3c342cffd585 | 33 | ble::AdvertisingParameters adv_parameters( |
krissl | 23:3c342cffd585 | 34 | ble::advertising_type_t::CONNECTABLE_UNDIRECTED |
krissl | 23:3c342cffd585 | 35 | ); |
krissl | 23:3c342cffd585 | 36 | |
krissl | 23:3c342cffd585 | 37 | error = _ble.gap().setAdvertisingParameters( |
krissl | 23:3c342cffd585 | 38 | ble::LEGACY_ADVERTISING_HANDLE, |
krissl | 23:3c342cffd585 | 39 | adv_parameters |
krissl | 23:3c342cffd585 | 40 | ); |
krissl | 23:3c342cffd585 | 41 | |
krissl | 23:3c342cffd585 | 42 | if (error) { |
krissl | 23:3c342cffd585 | 43 | print_error(error, "Gap::setAdvertisingParameters() failed"); |
krissl | 23:3c342cffd585 | 44 | return; |
krissl | 23:3c342cffd585 | 45 | } |
krissl | 23:3c342cffd585 | 46 | |
krissl | 23:3c342cffd585 | 47 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
krissl | 23:3c342cffd585 | 48 | |
krissl | 23:3c342cffd585 | 49 | if (error) { |
krissl | 23:3c342cffd585 | 50 | print_error(error, "Gap::startAdvertising() failed"); |
krissl | 23:3c342cffd585 | 51 | return; |
krissl | 23:3c342cffd585 | 52 | } |
krissl | 23:3c342cffd585 | 53 | |
krissl | 23:3c342cffd585 | 54 | printf("Please connect to device\r\n"); |
krissl | 23:3c342cffd585 | 55 | |
krissl | 23:3c342cffd585 | 56 | /** This tells the stack to generate a pairingRequest event |
krissl | 23:3c342cffd585 | 57 | * which will require this application to respond before pairing |
krissl | 23:3c342cffd585 | 58 | * can proceed. Setting it to false will automatically accept |
krissl | 23:3c342cffd585 | 59 | * pairing. */ |
krissl | 23:3c342cffd585 | 60 | _ble.securityManager().setPairingRequestAuthorisation(true); |
krissl | 23:3c342cffd585 | 61 | }; |
krissl | 23:3c342cffd585 | 62 | |
krissl | 23:3c342cffd585 | 63 | /** This is called by Gap to notify the application we connected, |
krissl | 23:3c342cffd585 | 64 | * in our case it immediately requests a change in link security */ |
krissl | 23:3c342cffd585 | 65 | virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) |
krissl | 23:3c342cffd585 | 66 | { |
krissl | 23:3c342cffd585 | 67 | ble_error_t error; |
krissl | 23:3c342cffd585 | 68 | |
krissl | 23:3c342cffd585 | 69 | /* remember the device that connects to us now so we can connect to it |
krissl | 23:3c342cffd585 | 70 | * during the next demonstration */ |
krissl | 23:3c342cffd585 | 71 | memcpy(_peer_address, event.getPeerAddress().data(), sizeof(_peer_address)); |
krissl | 23:3c342cffd585 | 72 | |
krissl | 23:3c342cffd585 | 73 | printf("Connected to peer: "); |
krissl | 23:3c342cffd585 | 74 | print_address(event.getPeerAddress().data()); |
krissl | 23:3c342cffd585 | 75 | |
krissl | 23:3c342cffd585 | 76 | _handle = event.getConnectionHandle(); |
krissl | 23:3c342cffd585 | 77 | |
krissl | 23:3c342cffd585 | 78 | /* Request a change in link security. This will be done |
krissl | 23:3c342cffd585 | 79 | * indirectly by asking the master of the connection to |
krissl | 23:3c342cffd585 | 80 | * change it. Depending on circumstances different actions |
krissl | 23:3c342cffd585 | 81 | * may be taken by the master which will trigger events |
krissl | 23:3c342cffd585 | 82 | * which the applications should deal with. */ |
krissl | 23:3c342cffd585 | 83 | error = _ble.securityManager().setLinkSecurity( |
krissl | 23:3c342cffd585 | 84 | _handle, |
krissl | 23:3c342cffd585 | 85 | SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM |
krissl | 23:3c342cffd585 | 86 | ); |
krissl | 23:3c342cffd585 | 87 | |
krissl | 23:3c342cffd585 | 88 | if (error) { |
krissl | 23:3c342cffd585 | 89 | printf("Error during SM::setLinkSecurity %d\r\n", error); |
krissl | 23:3c342cffd585 | 90 | return; |
krissl | 23:3c342cffd585 | 91 | } |
krissl | 23:3c342cffd585 | 92 | }; |
krissl | 23:3c342cffd585 | 93 | }; |