Chanel's edits

Dependencies:   max32630fthr USBDevice

PPGService.h

Committer:
saleiferis
Date:
2020-04-07
Revision:
19:f230229cb6f3
Parent:
15:b15b4b6c6da8

File content as of revision 19:f230229cb6f3:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MBED_BLE_PPG_SERVICE_H__
#define MBED_BLE_PPG_SERVICE_H__

#include "ble/BLE.h"

//#if BLE_FEATURE_GATT_SERVER

/**
 * BLE Heart Rate Service.
 *
 * @par purpose
 *
 * Fitness applications use the heart rate service to expose the heart
 * beat per minute measured by a heart rate sensor.
 *
 * Clients can read the intended location of the sensor and the last heart rate
 * value measured. Additionally, clients can subscribe to server initiated
 * updates of the heart rate value measured by the sensor. The service delivers
 * these updates to the subscribed client in a notification packet.
 *
 * The subscription mechanism is useful to save power; it avoids unecessary data
 * traffic between the client and the server, which may be induced by polling the
 * value of the heart rate measurement characteristic.
 *
 * @par usage
 *
 * When this class is instantiated, it adds a heart rate service in the GattServer.
 * The service contains the location of the sensor and the initial value measured
 * by the sensor.
 *
 * Application code can invoke updateHeartRate() when a new heart rate measurement
 * is acquired; this function updates the value of the heart rate measurement
 * characteristic and notifies the new value to subscribed clients.
 *
 * @note You can find specification of the heart rate service here:
 * https://www.bluetooth.com/specifications/gatt
 *
 * @attention The service does not expose information related to the sensor
 * contact, the accumulated energy expanded or the interbeat intervals.
 *
 * @attention The heart rate profile limits the number of instantiations of the
 * heart rate services to one.
 */
 extern Serial pc;
 const char* UUID_STR_PPG_SERVICE  = "E14C6C9D-3497-4835-8F8B-28D7AF2E6A15";
 const char* UUID_STR_PPG_RED_CHAR  = "E14C6C9D-3497-4836-8F8B-28D7AF2E6A15";
 const char* UUID_STR_PPG_IR_CHAR  = "E14C6C9D-3497-4837-8F8B-28D7AF2E6A15";

 
class PPGService {

public:
    /**
     * Construct and initialize a heart rate service.
     *
     * The construction process adds a GATT heart rate service in @p _ble
     * GattServer, sets the value of the heart rate measurement characteristic
     * to @p ppgVal and the value of the body sensor location characteristic
     * to @p location.
     *
     * @param[in] _ble BLE device that hosts the heart rate service.
     * @param[in] ppgVal Heart beats per minute measured by the heart rate
     * sensor.
     * @param[in] location Intended location of the heart rate sensor.
     */
    PPGService(BLE &_ble, int16_t ppgValRed, int16_t ppgValIR) :
        ble(_ble),
        valueBytesRed(ppgValRed),
        valueBytesIR(ppgValIR),
        ppgValueRed(
            UUID_STR_PPG_RED_CHAR, //TODO: replace with UUID for PPG sample characteristic
            valueBytesRed.getPointer(),
            valueBytesRed.getNumValueBytes(),
            PPGValueBytes::MAX_VALUE_BYTES,
            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        ppgValueIR(
            UUID_STR_PPG_IR_CHAR, //TODO: replace with UUID for PPG sample characteristic
            valueBytesIR.getPointer(),
            valueBytesIR.getNumValueBytes(),
            PPGValueBytes::MAX_VALUE_BYTES,
            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
    {
        //pc.printf("Setup service");
        setupService();
    }

    /**
     * Update the heart rate that the service exposes.
     *
     * The server sends a notification of the new value to clients that have
     * subscribed to updates of the heart rate measurement characteristic; clients
     * reading the heart rate measurement characteristic after the update obtain
     * the updated value.
     *
     * @param[in] ppgVal Heart rate measured in BPM.
     *
     * @attention This function must be called in the execution context of the
     * BLE stack.
     */
    void updatePPGRedValue(uint16_t ppgVal) {
        valueBytesRed.updatePPGValue(ppgVal);
        ble.gattServer().write(
            ppgValueRed.getValueHandle(),
            valueBytesRed.getPointer(),
            valueBytesRed.getNumValueBytes()
        );
    }
    void updatePPGIRValue(uint16_t ppgVal) {
        valueBytesIR.updatePPGValue(ppgVal);
        ble.gattServer().write(
            ppgValueIR.getValueHandle(),
            valueBytesIR.getPointer(),
            valueBytesIR.getNumValueBytes()
        );
    }

protected:
    /**
     * Construct and add to the GattServer the heart rate service.
     */
    void setupService(void) {
        GattCharacteristic *charTable[] = {
            &ppgValueRed,
            &ppgValueIR
        };
        GattService ppgService(
            UUID_STR_PPG_SERVICE, //TODO: ECG service UUID
            charTable,
            sizeof(charTable) / sizeof(GattCharacteristic*)
        );

        ble.gattServer().addService(ppgService);
    }

protected:
    /*
     * Heart rate measurement value.
     */
    struct PPGValueBytes {
        /* 1 byte for the Flags, and up to two bytes for heart rate value. */
        static const unsigned MAX_VALUE_BYTES = 2; // int16 per ECG sample
        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);

        PPGValueBytes(int16_t ppgVal) : valueBytes()
        {
            updatePPGValue(ppgVal);
        }

        void updatePPGValue(int16_t ppgVal)
        {
            *valueBytes = ppgVal;
            
        }

        uint8_t *getPointer(void)
        {
            return valueBytes;
        }

        const uint8_t *getPointer(void) const
        {
            return valueBytes;
        }

        unsigned getNumValueBytes(void) const
        {   
            /*
            if (valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) {
                return 1 + sizeof(uint16_t);
            } else {
                return 1 + sizeof(uint8_t);
            }
            */
            return sizeof(int16_t); // sending two bytes per ECG sample
        }

    private:
        uint8_t valueBytes[MAX_VALUE_BYTES];
    };

protected:
    BLE &ble;
    PPGValueBytes valueBytesRed;
    PPGValueBytes valueBytesIR;
    GattCharacteristic ppgValueRed;
    GattCharacteristic ppgValueIR;
};

//#endif // BLE_FEATURE_GATT_SERVER

#endif /* #ifndef MBED_BLE_HEART_RATE_SERVICE_H__*/