prova

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HeartRateService.h Source File

HeartRateService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_HEART_RATE_SERVICE_H__
00018 #define __BLE_HEART_RATE_SERVICE_H__
00019 
00020 #include "ble/BLE.h"
00021 
00022 /**
00023 * @class HeartRateService
00024 * @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor and the heart rate in beats per minute.
00025 * Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml
00026 * HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
00027 * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml
00028 */
00029 class HeartRateService {
00030 public:
00031     /**
00032     * @enum SensorLocation
00033     * @brief Location of the heart rate sensor on body.
00034     */
00035     enum {
00036         LOCATION_OTHER  = 0, /*!< Other location. */
00037         LOCATION_CHEST ,     /*!< Chest. */
00038         LOCATION_WRIST ,     /*!< Wrist. */
00039         LOCATION_FINGER ,    /*!< Finger. */
00040         LOCATION_HAND ,      /*!< Hand. */
00041         LOCATION_EAR_LOBE ,  /*!< Earlobe. */
00042         LOCATION_FOOT ,      /*!< Foot. */
00043     };
00044 
00045 public:
00046     /**
00047      * @brief Constructor with 8-bit HRM Counter value.
00048      *
00049      * @param[ref] _ble
00050      *               Reference to the underlying BLE.
00051      * @param[in] hrmCounter (8-bit)
00052      *               Initial value for the HRM counter.
00053      * @param[in] location
00054      *               Sensor's location.
00055     ************************************************************************************************************************************************
00056     *                                                   ORIGINALE
00057     ***********************************************************************************************************************************************************
00058      
00059      */
00060     HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) :
00061         ble(_ble),
00062         valueBytes(hrmCounter),
00063         hrmRate(GattCharacteristic:: UUID_HEART_RATE_MEASUREMENT_CHAR  , valueBytes.getPointer(),
00064                 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
00065                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00066         hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
00067         controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
00068         setupService();
00069     }
00070     /*
00071 ***************************************************************************************************************************************************************************
00072                                                         mODIFICATO
00073 
00074 ***************************************************************************************************************************************************************************
00075                     8-bit 
00076 */ /*
00077 HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) :
00078         ble(_ble),
00079         valueBytes(hrmCounter),
00080         hrmRate(GattCharacteristic:: UUID_TEMPERATURE_MEASUREMENT_CHAR  , valueBytes.getPointer(),
00081                 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
00082                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00083         hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
00084         controlPoint(GattCharacteristic::UUID_TEMPERATURE_CHAR , &controlPointValue) {
00085         setupService();
00086 
00087 }
00088 //                      16-bit 
00089 
00090 
00091 HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) :
00092         ble(_ble),
00093         valueBytes(hrmCounter),
00094         hrmRate(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, valueBytes.getPointer(),
00095                 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
00096                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00097         hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
00098         controlPoint(GattCharacteristic::UUID_TEMPERATURE_CHAR, &controlPointValue) {
00099         setupService();
00100     }*/
00101     /**
00102      * @brief Constructor with a 16-bit HRM Counter value.
00103      *
00104      * @param[in] _ble
00105      *               Reference to the underlying BLE.
00106      * @param[in] hrmCounter (8-bit)
00107      *               Initial value for the HRM counter.
00108      * @param[in] location
00109      *               Sensor's location.
00110      ***************************************************************************************************************************************************************************
00111      ***************************************************************************************************************************************************************************
00112      ***************************************************************************************************************************************************************************
00113      SOTTO IL 16 BIT DISABILITATO
00114      
00115      ***************************************************************************************************************************************************************************
00116      ***************************************************************************************************************************************************************************
00117      */
00118      
00119      
00120  HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) :
00121         ble(_ble),
00122         valueBytes(hrmCounter),
00123         hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
00124                 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
00125                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00126         hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
00127         controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
00128         setupService();
00129     }
00130 
00131     /**
00132      * @brief Set a new 8-bit value for the heart rate.
00133      *
00134      * @param[in] hrmCounter
00135      *                  Heart rate in BPM.
00136      */
00137     void updateHeartRate(uint8_t hrmCounter) {
00138         valueBytes.updateHeartRate(hrmCounter);
00139         ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
00140     }
00141 
00142     /**
00143      * Set a new 16-bit value for the heart rate.
00144      *
00145      * @param[in] hrmCounter
00146      *                  Heart rate in BPM.
00147      */
00148     void updateHeartRate(uint16_t hrmCounter) {
00149         valueBytes.updateHeartRate(hrmCounter);
00150         ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
00151     }
00152 
00153     /**
00154      * This callback allows the heart rate service to receive updates to the
00155      * controlPoint characteristic.
00156      *
00157      * @param[in] params
00158      *     Information about the characterisitc being updated.
00159      */
00160     virtual void onDataWritten(const GattWriteCallbackParams *params) {
00161         if (params->handle == controlPoint.getValueAttribute().getHandle()) {
00162             /* Do something here if the new value is 1; else you can override this method by
00163              * extending this class.
00164              * @NOTE: If you are extending this class, be sure to also call
00165              * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
00166              * your constructor.
00167              */
00168         }
00169     }
00170 
00171 protected:
00172     void setupService(void) {
00173         GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint};
00174         //GattService         hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE  , charTable, sizeof(charTable) / sizeof(GattCharacteristic *));       //Modificato
00175 
00176         GattService         hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));                //ORIGILAE
00177 
00178         ble.addService(hrmService);
00179         ble.onDataWritten(this, &HeartRateService::onDataWritten);
00180     }
00181 
00182 protected:
00183     /* Private internal representation for the bytes used to work with the value of the heart rate characteristic. */
00184     struct HeartRateValueBytes {
00185         static const unsigned MAX_VALUE_BYTES  = 3; /* Flags, and up to two bytes for heart rate. */
00186         static const unsigned FLAGS_BYTE_INDEX = 0;
00187 
00188         static const unsigned VALUE_FORMAT_BITNUM = 0;
00189         static const uint8_t  VALUE_FORMAT_FLAG   = (1 << VALUE_FORMAT_BITNUM);
00190 
00191         HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() {
00192             updateHeartRate(hrmCounter);
00193         }
00194 
00195         HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() {
00196             updateHeartRate(hrmCounter);
00197         }
00198 
00199         void updateHeartRate(uint8_t hrmCounter) {
00200             valueBytes[FLAGS_BYTE_INDEX]    &= ~VALUE_FORMAT_FLAG;
00201             valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
00202         }
00203 
00204         void updateHeartRate(uint16_t hrmCounter) {
00205             valueBytes[FLAGS_BYTE_INDEX]    |= VALUE_FORMAT_FLAG;
00206             valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
00207             valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
00208         }
00209 
00210         uint8_t       *getPointer(void) {
00211             return valueBytes;
00212         }
00213 
00214         const uint8_t *getPointer(void) const {
00215             return valueBytes;
00216         }
00217 
00218         unsigned       getNumValueBytes(void) const {
00219             return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
00220         }
00221 
00222     private:
00223         /* First byte: 8-bit values, no extra info. Second byte: uint8_t HRM value */
00224         /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
00225         uint8_t valueBytes[MAX_VALUE_BYTES];
00226     };
00227 
00228 protected:
00229     BLE                 &ble;
00230 
00231     HeartRateValueBytes  valueBytes;
00232     uint8_t              controlPointValue;
00233 
00234     GattCharacteristic                   hrmRate;
00235     ReadOnlyGattCharacteristic<uint8_t>   hrmLocation;
00236     WriteOnlyGattCharacteristic<uint8_t>  controlPoint;
00237 };
00238 
00239 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/