Demo for the Tx Power service
Dependencies: BLE_API mbed nRF51822
Demo for the Tx Power service (included in the services folder). i kept the service format the same as the official mbed libraries.
Services/TxPower.h@0:1d89681580f2, 2014-12-15 (annotated)
- Committer:
- KarimAzzouz
- Date:
- Mon Dec 15 17:54:09 2014 +0000
- Revision:
- 0:1d89681580f2
Demo for the Tx power service
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KarimAzzouz | 0:1d89681580f2 | 1 | #ifndef TX_POWER_H |
KarimAzzouz | 0:1d89681580f2 | 2 | #define TX_POWER_H |
KarimAzzouz | 0:1d89681580f2 | 3 | |
KarimAzzouz | 0:1d89681580f2 | 4 | #include "BLEDevice.h" |
KarimAzzouz | 0:1d89681580f2 | 5 | |
KarimAzzouz | 0:1d89681580f2 | 6 | /** |
KarimAzzouz | 0:1d89681580f2 | 7 | * @class Tx power service |
KarimAzzouz | 0:1d89681580f2 | 8 | * @brief Tx power Service. This service exposes a device’s current transmit power level when in a connection (-100dbm to +20). |
KarimAzzouz | 0:1d89681580f2 | 9 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.tx_power.xml |
KarimAzzouz | 0:1d89681580f2 | 10 | * Tx power Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.tx_power_level.xml |
KarimAzzouz | 0:1d89681580f2 | 11 | */ |
KarimAzzouz | 0:1d89681580f2 | 12 | class TxPowerService |
KarimAzzouz | 0:1d89681580f2 | 13 | { |
KarimAzzouz | 0:1d89681580f2 | 14 | |
KarimAzzouz | 0:1d89681580f2 | 15 | public: |
KarimAzzouz | 0:1d89681580f2 | 16 | /** |
KarimAzzouz | 0:1d89681580f2 | 17 | * @param[ref] _ble |
KarimAzzouz | 0:1d89681580f2 | 18 | * BLEDevice object for the underlying controller. |
KarimAzzouz | 0:1d89681580f2 | 19 | * @param[in] TxValue |
KarimAzzouz | 0:1d89681580f2 | 20 | * signed 8bit Transmit power. |
KarimAzzouz | 0:1d89681580f2 | 21 | */ |
KarimAzzouz | 0:1d89681580f2 | 22 | TxPowerService(BLEDevice &_ble, int8_t TxValue): |
KarimAzzouz | 0:1d89681580f2 | 23 | ble(_ble), |
KarimAzzouz | 0:1d89681580f2 | 24 | TxPower(TxValue), |
KarimAzzouz | 0:1d89681580f2 | 25 | TxPowerChar(GattCharacteristic::UUID_TX_POWER_LEVEL_CHAR, (uint8_t*)&TxPower, sizeof(TxPower), sizeof(TxPower), |
KarimAzzouz | 0:1d89681580f2 | 26 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { |
KarimAzzouz | 0:1d89681580f2 | 27 | |
KarimAzzouz | 0:1d89681580f2 | 28 | static bool serviceAdded = false; /* We should only ever add one Tx power service. */ |
KarimAzzouz | 0:1d89681580f2 | 29 | if (serviceAdded) { |
KarimAzzouz | 0:1d89681580f2 | 30 | return; |
KarimAzzouz | 0:1d89681580f2 | 31 | } |
KarimAzzouz | 0:1d89681580f2 | 32 | |
KarimAzzouz | 0:1d89681580f2 | 33 | GattCharacteristic *charTable[] = {&TxPowerChar}; |
KarimAzzouz | 0:1d89681580f2 | 34 | GattService TxPowerService(GattService::UUID_TX_POWER_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
KarimAzzouz | 0:1d89681580f2 | 35 | ble.addService(TxPowerService); |
KarimAzzouz | 0:1d89681580f2 | 36 | serviceAdded = true; |
KarimAzzouz | 0:1d89681580f2 | 37 | |
KarimAzzouz | 0:1d89681580f2 | 38 | } |
KarimAzzouz | 0:1d89681580f2 | 39 | |
KarimAzzouz | 0:1d89681580f2 | 40 | /** |
KarimAzzouz | 0:1d89681580f2 | 41 | * @brief Update the Tx power with a new value. with a valid range from -100dbm to +20dbm according to the service Char. |
KarimAzzouz | 0:1d89681580f2 | 42 | * |
KarimAzzouz | 0:1d89681580f2 | 43 | * @param newTxPower |
KarimAzzouz | 0:1d89681580f2 | 44 | * update the Tx power with the new value. |
KarimAzzouz | 0:1d89681580f2 | 45 | */ |
KarimAzzouz | 0:1d89681580f2 | 46 | void updateTxPower(int8_t newTxPower) { |
KarimAzzouz | 0:1d89681580f2 | 47 | |
KarimAzzouz | 0:1d89681580f2 | 48 | if(newTxPower >= -100 && newTxPower <= 20) { |
KarimAzzouz | 0:1d89681580f2 | 49 | |
KarimAzzouz | 0:1d89681580f2 | 50 | TxPower = newTxPower; |
KarimAzzouz | 0:1d89681580f2 | 51 | ble.updateCharacteristicValue (TxPowerChar.getValueAttribute().getHandle(), (uint8_t*)&TxPower, 1); |
KarimAzzouz | 0:1d89681580f2 | 52 | |
KarimAzzouz | 0:1d89681580f2 | 53 | } |
KarimAzzouz | 0:1d89681580f2 | 54 | } |
KarimAzzouz | 0:1d89681580f2 | 55 | |
KarimAzzouz | 0:1d89681580f2 | 56 | private: |
KarimAzzouz | 0:1d89681580f2 | 57 | BLEDevice &ble; |
KarimAzzouz | 0:1d89681580f2 | 58 | int8_t TxPower; |
KarimAzzouz | 0:1d89681580f2 | 59 | GattCharacteristic TxPowerChar; |
KarimAzzouz | 0:1d89681580f2 | 60 | }; |
KarimAzzouz | 0:1d89681580f2 | 61 | |
KarimAzzouz | 0:1d89681580f2 | 62 | #endif |