/*! * \copyright * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file BluetoothInterface.h * \author (last) Behrouz NematiPour * \author (original) Behrouz NematiPour * \date (last) 22-Aug-2021 * \date (original) 22-Aug-2021 */ #pragma once // Qt #include #include #include #include #include // Project #include "main.h" // Doxygen : do not remove #include "MBluetooth.h" #include "MUIBloodPressureData.h" // Define #define _BluetoothInterface Bluetooth::BluetoothInterface::I() // forward declarations class tst_initializations; // namespace namespace Bluetooth { using namespace Model; /*! * \brief The Bluetooth Controller class * \details The wrapper class of the Bluetooth which does the thread handling. * \warning In the Bluetooth interface it checks about the Bluetooth POST to avoid creating threads, objects. * The validity of the Bluetooth interface has to be always checked before use. */ class BluetoothInterface : public QObject { Q_OBJECT // Singleton SINGLETON(BluetoothInterface) // friends friend class ::tst_initializations; typedef QMap< QBluetoothUuid::ServiceClassUuid, QLowEnergyService *> ServiceMap; QThread *_thread = nullptr; bool _init = false ; bool _isValid = false ; const char *_invalidLocalAddress = "00:00:00:00:00:00"; QStringList _supportedDeviceKeywords { "BP7000", "BLEsmart", "BLESmart" }; // Regarding to Omron Documents. QBluetoothLocalDevice *_local = nullptr ; QBluetoothDeviceDiscoveryAgent *_agent = nullptr ; QLowEnergyController *_device = nullptr ; QLowEnergyService *_serviceDeviceInformation = nullptr ; QLowEnergyService *_serviceCurrentTime = nullptr ; QLowEnergyService *_serviceBloodPressure = nullptr ; QLowEnergyService *_serviceBattery = nullptr ; QBluetoothDeviceInfo _temp ; bool _reconnectionActive = false ; const quint16 _interval = 1000 ; // the interface timer base interval in mSec public: bool isValid( ) const { return _isValid ; } void valid(bool vValid) { _isValid = vValid; } protected: void timerEvent(QTimerEvent *event) override; public slots: bool init(); bool init(QThread &vThread); void doNotifyStatePOSTError(); private slots: void quit(); // Local Device Slots void onLocalDeviceConnect (const QBluetoothAddress &vAddress ); void onLocalDeviceDisconnect (const QBluetoothAddress &vAddress ); void onLocalError ( QBluetoothLocalDevice::Error vError ); // Discovery Agent Slots void onAgentDiscoverDevice (const QBluetoothDeviceInfo & vDevice ); void onAgentDiscoverFinish (); void onAgentDiscoverCancel (); void onAgentDiscoverError ( QBluetoothDeviceDiscoveryAgent::Error vError ); // Device Slots void onDeviceConnect ( ); void onDeviceDisconnect ( ); void onDeviceDiscoverService (const QBluetoothUuid &vService); void onDeviceDiscoverFinish ( ); void onDeviceError ( QLowEnergyController::Error vError ); void onServiceCharacteristicChanged (const QLowEnergyCharacteristic &vCharacteristic , const QByteArray &vValue ); void onServiceCharacteristicRead (const QLowEnergyCharacteristic &vCharacteristic , const QByteArray &vValue ); void onServiceCharacteristicWritten (const QLowEnergyCharacteristic &vCharacteristic , const QByteArray &vValue ); void onServiceDescriptorRead (const QLowEnergyDescriptor &vDescriptor , const QByteArray &vValue ); void onServiceDescriptorWritten (const QLowEnergyDescriptor &vDescriptor , const QByteArray &vValue ); void onServiceError ( QLowEnergyService::ServiceError vError ); void onServiceStateChanged ( QLowEnergyService::ServiceState vState ); void onDeviceSelect (const BluetoothDeviceData &vDevice); private: bool startScan (); bool stopScan (); void initConnections (); void initConnectionsDevice (); void initConnectionsService (QLowEnergyService *vService); void initThread(QThread &vThread); void quitThread(); bool initDevice (); bool makeDevice (); void quitDevice (); void initServices (const QBluetoothUuid &vService); void makeServices (const QBluetoothUuid &vService); void quitServices (); bool isLocalValid (); bool isAgentValid (); bool isInfoValid (); bool isDeviceValid (); // bool isServiceValid (QLowEnergyService *vService); bool isDetailValid (const QLowEnergyCharacteristic &vDetail); bool isDeviceSupported (const QString &vName ); bool connectToDevice (); bool reconnectToDevice (); void discoverServices (); void discoverServicesDetails(); void enableNotify (); void readMeasurements (); void readInformation (); void printCharacteristics (QLowEnergyService *vService); void notifyStateChange (const BluetoothData &vData); void interpretBloodPressure (const QByteArray &vData); signals: void didStateChange (const BluetoothData &vData); void didActionReceive (const UIBloodPressureData &vData); void didDeviceChange (const BluetoothDeviceData &vDevice); void didDeviceSelect (const BluetoothDeviceData &vDevice); SAFE_CALL(doStart ) SAFE_CALL(doScan ) SAFE_CALL(doConnectToDevice ) SAFE_CALL(doDiscoverServices ) SAFE_CALL(doEnableNotify ) SAFE_CALL(doReadMeasurements ) SAFE_CALL(doReadInformation ) }; }