@@ -89,10 +89,22 @@ static const size_t SCAN_PARAM_SET_MAX =
8989static const size_t ADV_PARAM_SET_MAX =
9090 sizeof (advertising_params) / sizeof (GapAdvertisingParams);
9191
92+ static const char * to_string (Gap::Phy_t phy) {
93+ switch (phy.value ()) {
94+ case Gap::Phy_t::LE_1M:
95+ return " LE 1M" ;
96+ case Gap::Phy_t::LE_2M:
97+ return " LE 2M" ;
98+ case Gap::Phy_t::LE_CODED:
99+ return " LE coded" ;
100+ default :
101+ return " invalid PHY" ;
102+ }
103+ }
92104
93105/* * Demonstrate advertising, scanning and connecting
94106 */
95- class GAPDevice : private mbed ::NonCopyable<GAPDevice>
107+ class GAPDevice : private mbed ::NonCopyable<GAPDevice>, public Gap::EventHandler
96108{
97109public:
98110 GAPDevice () :
@@ -132,6 +144,9 @@ class GAPDevice : private mbed::NonCopyable<GAPDevice>
132144 makeFunctionPointer (this , &GAPDevice::on_timeout)
133145 );
134146
147+ /* handle gap events */
148+ _ble.gap ().setEventHandler (this );
149+
135150 error = _ble.init (this , &GAPDevice::on_init_complete);
136151
137152 if (error) {
@@ -162,6 +177,14 @@ class GAPDevice : private mbed::NonCopyable<GAPDevice>
162177 printf (" Device address: %02x:%02x:%02x:%02x:%02x:%02x\r\n " ,
163178 addr[5 ], addr[4 ], addr[3 ], addr[2 ], addr[1 ], addr[0 ]);
164179
180+ /* setup the default phy used in connection to 2M to reduce power consumption */
181+ Gap::Phys_t tx_phys = { /* 1M */ false , /* 2M */ true , /* coded */ false };
182+ Gap::Phys_t rx_phys = { /* 1M */ false , /* 2M */ true , /* coded */ false };
183+ ble_error_t err = _ble.gap ().setPreferedPhys (&tx_phys, &rx_phys);
184+ if (err) {
185+ printf (" INFO: GAP::setPreferedPhys failed with error code %s" , BLE::errorToString (err));
186+ }
187+
165188 /* all calls are serialised on the user thread through the event queue */
166189 _event_queue.call (this , &GAPDevice::demo_mode_start);
167190 };
@@ -378,6 +401,56 @@ class GAPDevice : private mbed::NonCopyable<GAPDevice>
378401 _event_queue.call (this , &GAPDevice::demo_mode_end);
379402 };
380403
404+ /* *
405+ * Implementation of Gap::EventHandler::onReadPhy
406+ */
407+ virtual void onReadPhy (
408+ ble_error_t error,
409+ Gap::Handle_t connectionHandle,
410+ Gap::Phy_t txPhy,
411+ Gap::Phy_t rxPhy
412+ ) {
413+ if (error) {
414+ printf (
415+ " Phy read on connection %d failed with error code %s\r\n " ,
416+ connectionHandle,
417+ BLE::errorToString (error)
418+ );
419+ } else {
420+ printf (
421+ " Phy read on connection %d - Tx Phy: %s, Rx Phy: %s\r\n " ,
422+ connectionHandle,
423+ to_string (txPhy),
424+ to_string (rxPhy)
425+ );
426+ }
427+ }
428+
429+ /* *
430+ * Implementation of Gap::EventHandler::onPhyUpdateComplete
431+ */
432+ virtual void onPhyUpdateComplete (
433+ ble_error_t error,
434+ Gap::Handle_t connectionHandle,
435+ Gap::Phy_t txPhy,
436+ Gap::Phy_t rxPhy
437+ ) {
438+ if (error) {
439+ printf (
440+ " Phy update on connection: %d failed with error code %s\r\n " ,
441+ connectionHandle,
442+ BLE::errorToString (error)
443+ );
444+ } else {
445+ printf (
446+ " Phy update on connection %d - Tx Phy: %s, Rx Phy: %s\r\n " ,
447+ connectionHandle,
448+ to_string (txPhy),
449+ to_string (rxPhy)
450+ );
451+ }
452+ }
453+
381454 /* * called if timeout is reached during advertising, scanning
382455 * or connection initiation */
383456 void on_timeout (const Gap::TimeoutSource_t source)
0 commit comments