Skip to content

Commit 4deb64a

Browse files
committed
Merge pull request #1 from pan-/PHY_API
Phy api
1 parent 7d871bf commit 4deb64a

File tree

2 files changed

+266
-1
lines changed

2 files changed

+266
-1
lines changed

features/FEATURE_BLE/ble/BLETypes.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,110 @@ struct peer_address_type_t :SafeEnum<peer_address_type_t, uint8_t> {
572572
SafeEnum<peer_address_type_t, uint8_t>(PUBLIC) { }
573573
};
574574

575+
/**
576+
* Type that describes a bluetooth PHY(sical) transport.
577+
*/
578+
struct phy_t : SafeEnum<phy_t, uint8_t> {
579+
/** struct scoped enum wrapped by the class */
580+
enum type {
581+
/**
582+
* 1Mbit/s LE.
583+
*
584+
* @note This physical transport was available since Bluetooth 4.0
585+
*/
586+
LE_1M = 1,
587+
588+
/**
589+
* 2Mbit/s LE.
590+
*
591+
* Modulation is similar to LE_1M but differs in rate. Therefore range
592+
* performances are in the same ballpark as LE_1M while the increased rate
593+
* minimize time spent to transfer or receive a packet which leads to a
594+
* better power consumption and/or faster transfer.
595+
*
596+
* @note This transport has been introduced with the Bluetooth 5.
597+
* @note When operating at 2Mbit/s range is not exactly identical to the
598+
* range at 1Mbit/s due to a loss in sensitivity.
599+
*/
600+
LE_2M = 2,
601+
602+
/**
603+
* LE Coded PHY.
604+
*
605+
* This transport reuse the 1Mbit/s channel with different coding schemes.
606+
* Either two (S=2) or eight (S=8) symbols can be used to represent a
607+
* bit while the 1Mbit/s transport use 1 symbol to code 1 bit of data.
608+
*
609+
* Here is the data rate of the two coding schemes:
610+
* - S=2: 500kbit/s
611+
* - S=8: 125kbit/s
612+
*
613+
* The goal of the coded PHY is to increase the range of BLE devices.
614+
* Of course given it takes more time to transfer data, transmission
615+
* and reception last longer which leads to an increase in power
616+
* consumption.
617+
*
618+
* @note This transport has been introduced with the Bluetooth 5.
619+
*/
620+
LE_CODED
621+
};
622+
623+
/**
624+
* Construct a new instance of phy_t.
625+
*/
626+
phy_t(type value) :
627+
SafeEnum<phy_t, uint8_t>(value) { }
628+
};
629+
630+
/**
631+
* Type that describe a set of PHY(sical) transports.
632+
*/
633+
struct phys_t {
634+
/**
635+
* If equal to 1 then the set includes phy_t::LE_1M.
636+
*/
637+
uint8_t le_1m:1;
638+
639+
/**
640+
* If equal to 1 then the set includes phy_t::LE_2M.
641+
*/
642+
uint8_t le_2m:1;
643+
644+
/**
645+
* If equal to 1 then the set includes phy_t::LE_CODED.
646+
*/
647+
uint8_t le_coded:1;
648+
};
649+
650+
/**
651+
* Type describing the number of symbols per bit in le coded PHY.
652+
*/
653+
struct coded_symbol_per_bit_t :SafeEnum<coded_symbol_per_bit_t, uint8_t> {
654+
/** struct scoped enum wrapped by the class */
655+
enum type {
656+
/**
657+
* The Number of symbol used to code a bit is undefined.
658+
*/
659+
UNDEFINED,
660+
661+
/**
662+
* Two symbols to code a bit.
663+
*/
664+
S2,
665+
666+
/**
667+
* Eight symbols to code a bit.
668+
*/
669+
S8
670+
};
671+
672+
/**
673+
* Construct a new instance of coded_symbol_per_bit_t.
674+
*/
675+
coded_symbol_per_bit_t(type value) :
676+
SafeEnum<coded_symbol_per_bit_t, uint8_t>(value) { }
677+
};
678+
575679
} // namespace ble
576680

577681
/**

features/FEATURE_BLE/ble/Gap.h

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,21 @@ class Gap {
533533
*/
534534
typedef ble::peer_address_type_t PeerAddressType_t;
535535

536+
/**
537+
* Enumeration of BLE PHY
538+
*/
539+
typedef ble::phy_t Phy_t;
540+
541+
/**
542+
* Set of BLE PHYs
543+
*/
544+
typedef ble::phys_t Phys_t;
545+
546+
/**
547+
* Enumeration of type of symbols that can be used with LE coded PHY.
548+
*/
549+
typedef ble::coded_symbol_per_bit_t CodedSymbolPerBit_t;
550+
536551
/**
537552
* Parameters of a BLE connection.
538553
*/
@@ -1071,6 +1086,68 @@ class Gap {
10711086
typedef CallChainOfFunctionPointersWithContext<const Gap *>
10721087
GapShutdownCallbackChain_t;
10731088

1089+
1090+
/**
1091+
* Definition of the general handler of Gap related events.
1092+
*/
1093+
struct EventHandler {
1094+
/**
1095+
* Function invoked when the current transmitter and receiver PHY have
1096+
* been read for a given connection.
1097+
*
1098+
* @param status Status of the operation: BLE_ERROR_NONE in case of
1099+
* success or an appropriate error code.
1100+
*
1101+
* @param connectionHandle: The handle of the connection for which the
1102+
* PHYs have been read.
1103+
*
1104+
* @param txPhy PHY used by the transmitter.
1105+
*
1106+
* @param rxPhy PHY used by the receiver.
1107+
*/
1108+
virtual void onPhyRead(
1109+
ble_error_t status,
1110+
Handle_t connectionHandle,
1111+
ble::phy_t txPhy,
1112+
ble::phy_t rxPhy
1113+
) = 0;
1114+
1115+
/**
1116+
* Function invoked when the update process of the PHY has been completed.
1117+
*
1118+
* The process can be initiated by a call to the function setPhy, the
1119+
* local bluetooth subsystem or the peer.
1120+
*
1121+
* @param status Status of the operation: BLE_ERROR_NONE in case of
1122+
* success or an appropriate error code.
1123+
*
1124+
* @param connectionHandle: The handle of the connection on which the
1125+
* operation was made.
1126+
*
1127+
* @param txPhy PHY used by the transmitter.
1128+
*
1129+
* @param rxPhy PHY used by the receiver.
1130+
*
1131+
* @note Success doesn't mean the PHY has been updated it means both
1132+
* ends have negociated the best phy according to their configuration and
1133+
* capabilities. The PHY currently used are present in the txPhy and
1134+
* rxPhy parameters.
1135+
*/
1136+
virtual void onPhyUpdateComplete(
1137+
ble_error_t status,
1138+
Handle_t connectionHandle,
1139+
ble::phy_t txPhy,
1140+
ble::phy_t rxPhy
1141+
) = 0;
1142+
1143+
protected:
1144+
/**
1145+
* Prevent polymorphic deletion and avoid uncessery virtual destructor
1146+
* as the Gap class will never delete the instance it contains.
1147+
*/
1148+
~EventHandler() { }
1149+
};
1150+
10741151
/*
10751152
* The following functions are meant to be overridden in the platform-specific subclass.
10761153
*/
@@ -1331,6 +1408,72 @@ class Gap {
13311408
const GapScanningParams *scanParams
13321409
);
13331410

1411+
/**
1412+
* Read the PHY used by the transmitter and the receiver on a connection.
1413+
*
1414+
* Once the PHY has been read, it is reported back via the function onPhyRead
1415+
* of the event handler registered by the application.
1416+
*
1417+
* @param connection Handle of the connection for which the PHY being used is
1418+
* queried.
1419+
*
1420+
* @return BLE_ERROR_NONE if the read PHY procedure has been started or an
1421+
* appropriate error code.
1422+
*/
1423+
virtual ble_error_t readPhy(Handle_t connection) {
1424+
return BLE_ERROR_NOT_IMPLEMENTED;
1425+
}
1426+
1427+
/**
1428+
* Set the prefered PHYs to use in a connection.
1429+
*
1430+
* @param txPhy: Set of PHYs prefered for tx operations. If NULL then no
1431+
* prefered PHYs are set and the default value of the subsytem is used.
1432+
*
1433+
* @param rxPhy: Set of PHYs prefered for rx operations. If NULL then no
1434+
* prefered PHYs are set and the default value of the subsytem is used.
1435+
*
1436+
* @return BLE_ERROR_NONE if the preferences have been set or an appropriate
1437+
* error code.
1438+
*/
1439+
virtual ble_error_t setPreferedPhys(
1440+
const Phys_t* txPhys,
1441+
const Phys_t* rxPhys
1442+
) {
1443+
return BLE_ERROR_NOT_IMPLEMENTED;
1444+
}
1445+
1446+
/**
1447+
* Update the PHY used by a connection.
1448+
*
1449+
* Once the update process has been completed, it is reported back to the
1450+
* application via the function onPhyUpdateComplete of the event handler
1451+
* registered by the application.
1452+
*
1453+
* @param connection Handle of the connection to update.
1454+
*
1455+
* @param txPhys Set of PHYs prefered for tx operations. If NULL then the
1456+
* choice is up to the Bluetooth subsystem.
1457+
*
1458+
* @param rxPhys Set of PHYs prefered for rx operations. If NULL then the
1459+
* choice is up to the Bluetooth subsystem.
1460+
*
1461+
* @param codedSymbol Number of symbols used to code a bit when le coded is
1462+
* used. If the value is UNDEFINED then the choice is up to the Bluetooth
1463+
* subsystem.
1464+
*
1465+
* @return BLE_ERROR_NONE if the update PHY procedure has been successfully
1466+
* started or an error code.
1467+
*/
1468+
virtual ble_error_t setPhy(
1469+
Handle_t connection,
1470+
const Phys_t* txPhys,
1471+
const Phys_t* rxPhys,
1472+
CodedSymbolPerBit_t codedSymbol
1473+
) {
1474+
return BLE_ERROR_NONE;
1475+
}
1476+
13341477
/**
13351478
* Initiate a disconnection procedure.
13361479
*
@@ -2446,6 +2589,17 @@ class Gap {
24462589

24472590
/* Event handlers. */
24482591
public:
2592+
2593+
/**
2594+
* Assign the event handler implementation that will be used by the gap
2595+
* module to signal events back to the application.
2596+
*
2597+
* @param handler Application implementation of an Eventhandler.
2598+
*/
2599+
void setEventHandler(EventHandler* handler) {
2600+
_eventHandler = handler;
2601+
}
2602+
24492603
/**
24502604
* Register a callback handling timeout events.
24512605
*
@@ -2674,6 +2828,7 @@ class Gap {
26742828
disconnectionCallChain.clear();
26752829
radioNotificationCallback = NULL;
26762830
onAdvertisementReport = NULL;
2831+
_eventHandler = NULL;
26772832

26782833
return BLE_ERROR_NONE;
26792834
}
@@ -2694,7 +2849,8 @@ class Gap {
26942849
radioNotificationCallback(),
26952850
onAdvertisementReport(),
26962851
connectionCallChain(),
2697-
disconnectionCallChain() {
2852+
disconnectionCallChain(),
2853+
_eventHandler(NULL) {
26982854
_advPayload.clear();
26992855
_scanResponse.clear();
27002856
}
@@ -2938,6 +3094,11 @@ class Gap {
29383094
*/
29393095
DisconnectionEventCallbackChain_t disconnectionCallChain;
29403096

3097+
/**
3098+
* Event handler provided by the application.
3099+
*/
3100+
EventHandler* _eventHandler;
3101+
29413102
private:
29423103
/**
29433104
* Callchain containing all registered callback handlers for shutdown

0 commit comments

Comments
 (0)