Kris Scholte Lubberink / Mbed OS bleswitcher
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SMDevicePeripheral.cpp Source File

SMDevicePeripheral.cpp

00001 
00002 /** A peripheral device will advertise, accept the connection and request
00003  * a change in link security. */
00004 class SMDevicePeripheral : public SMDevice {
00005 public:
00006     SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address)
00007         : SMDevice(ble, event_queue, peer_address) { }
00008 
00009     virtual void start()
00010     {
00011         /* Set up and start advertising */
00012         uint8_t adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
00013         /* use the helper to build the payload */
00014         ble::AdvertisingDataBuilder adv_data_builder(
00015             adv_buffer
00016         );
00017 
00018         adv_data_builder.setFlags();
00019         adv_data_builder.setName(DEVICE_NAME);
00020 
00021         /* Set payload for the set */
00022         ble_error_t error = _ble.gap().setAdvertisingPayload(
00023             ble::LEGACY_ADVERTISING_HANDLE,
00024             adv_data_builder.getAdvertisingData()
00025         );
00026 
00027         if (error) {
00028             print_error(error, "Gap::setAdvertisingPayload() failed");
00029             _event_queue.break_dispatch();
00030             return;
00031         }
00032 
00033         ble::AdvertisingParameters adv_parameters(
00034             ble::advertising_type_t::CONNECTABLE_UNDIRECTED
00035         );
00036 
00037         error = _ble.gap().setAdvertisingParameters(
00038             ble::LEGACY_ADVERTISING_HANDLE,
00039             adv_parameters
00040         );
00041 
00042         if (error) {
00043             print_error(error, "Gap::setAdvertisingParameters() failed");
00044             return;
00045         }
00046 
00047         error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
00048 
00049         if (error) {
00050             print_error(error, "Gap::startAdvertising() failed");
00051             return;
00052         }
00053 
00054         printf("Please connect to device\r\n");
00055 
00056         /** This tells the stack to generate a pairingRequest event
00057          * which will require this application to respond before pairing
00058          * can proceed. Setting it to false will automatically accept
00059          * pairing. */
00060         _ble.securityManager().setPairingRequestAuthorisation(true);
00061     };
00062 
00063     /** This is called by Gap to notify the application we connected,
00064      *  in our case it immediately requests a change in link security */
00065     virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event)
00066     {
00067         ble_error_t error;
00068 
00069         /* remember the device that connects to us now so we can connect to it
00070          * during the next demonstration */
00071         memcpy(_peer_address, event.getPeerAddress().data(), sizeof(_peer_address));
00072 
00073         printf("Connected to peer: ");
00074         print_address(event.getPeerAddress().data());
00075 
00076         _handle = event.getConnectionHandle();
00077 
00078         /* Request a change in link security. This will be done
00079          * indirectly by asking the master of the connection to
00080          * change it. Depending on circumstances different actions
00081          * may be taken by the master which will trigger events
00082          * which the applications should deal with. */
00083         error = _ble.securityManager().setLinkSecurity(
00084             _handle,
00085             SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM
00086         );
00087 
00088         if (error) {
00089             printf("Error during SM::setLinkSecurity %d\r\n", error);
00090             return;
00091         }
00092     };
00093 };