We are making a bluetooth application for a vehicle.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
bike_service.h
- Committer:
- bobbaddeley
- Date:
- 2017-06-26
- Revision:
- 23:a31b178e2263
- Child:
- 24:d352571b1c1f
File content as of revision 23:a31b178e2263:
/* Bike Service */ #ifndef __BLE_BIKE_SERVICE_H__ #define __BLE_BIKE_SERVICE_H__ #include "ble/BLE.h" /** * @class BikeService * @brief BLE Service for Heck Bike. */ class BikeService { public: /** * @brief Constructor with 8-bit HRM Counter value. * * @param[ref] _ble * Reference to the underlying BLE. * @param[in] hrmCounter (8-bit) * Initial value for the HRM counter. * @param[in] location * Sensor's location. */ BikeService(BLE &_ble) : ble(_ble), hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { setupService(); } /** * @brief Set a new 8-bit value for the heart rate. * * @param[in] hrmCounter * Heart rate in BPM. */ void updateHeartRate(uint8_t hrmCounter) { valueBytes.updateHeartRate(hrmCounter); ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); } /** * Set a new 16-bit value for the heart rate. * * @param[in] hrmCounter * Heart rate in BPM. */ void updateHeartRate(uint16_t hrmCounter) { valueBytes.updateHeartRate(hrmCounter); ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); } /** * This callback allows the heart rate service to receive updates to the * controlPoint characteristic. * * @param[in] params * Information about the characterisitc being updated. */ virtual void onDataWritten(const GattWriteCallbackParams *params) { if (params->handle == controlPoint.getValueAttribute().getHandle()) { /* Do something here if the new value is 1; else you can override this method by * extending this class. * @NOTE: If you are extending this class, be sure to also call * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in * your constructor. */ } } protected: void setupService(void) { GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint}; GattService bikeService(0x8EC5, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.addService(bikeService); ble.onDataWritten(this, &BikeService::onDataWritten); } protected: /* Private internal representation for the bytes used to work with the value of the heart rate characteristic. */ struct HeartRateValueBytes { static const unsigned MAX_VALUE_BYTES = 3; /* Flags, and up to two bytes for heart rate. */ static const unsigned FLAGS_BYTE_INDEX = 0; static const unsigned VALUE_FORMAT_BITNUM = 0; static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() { updateHeartRate(hrmCounter); } HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() { updateHeartRate(hrmCounter); } void updateHeartRate(uint8_t hrmCounter) { valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; } void updateHeartRate(uint16_t hrmCounter) { valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); } uint8_t *getPointer(void) { return valueBytes; } const uint8_t *getPointer(void) const { return valueBytes; } unsigned getNumValueBytes(void) const { return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t)); } private: /* First byte: 8-bit values, no extra info. Second byte: uint8_t HRM value */ /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ uint8_t valueBytes[MAX_VALUE_BYTES]; }; protected: BLE &ble; HeartRateValueBytes valueBytes; uint8_t controlPointValue; GattCharacteristic hrmRate; ReadOnlyGattCharacteristic<uint8_t> hrmLocation; WriteOnlyGattCharacteristic<uint8_t> controlPoint; }; #endif /* #ifndef __BLE_BIKE_SERVICE_H__*/