/*! * * Copyright (c) 2021-2024 Diality Inc. - All Rights Reserved. * \copyright * 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) Vy * \date (last) 05-Sep-2023 * \author (original) Behrouz NematiPour * \date (original) 24-Aug-2021 * */ #pragma once // Qt #include #include #include #include #include // Project #include "main.h" // Doxygen : do not remove #include "MBluetooth.h" //#include "MUIBloodPressureData.h" #include "DeviceModels.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 QByteArray _bloodPressureNotifyValue = QByteArray::fromHex("0100"); 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 ; quint8 _tempBatt = 0 ; bool _tryingrepairActive = false ; 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 quit(); void doNotifyStatePOSTError(); private slots: // 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); void onAttributeResponse (const DeviceBluetoothPairedQueryResponseData &vData); private: bool startScan (bool vQuitDevice); 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 isDeviceSupported (const QString &vName ); bool connectToDevice (); bool reconnectToDevice (); void discoverServices (); void discoverServicesDetails(); void enableNotify (); void requestMeasurements (); void requestInformation (); void requestBattery (); void printCharacteristics (QLowEnergyService *vService); void notifyStateChange (const BluetoothData &vData); void interpretInformation (); void interpretBattery (const QByteArray &vData); signals: void didStateChange (const BluetoothData &vData); void didDeviceChange (const BluetoothDeviceData &vDevice); void didDeviceSelect (const BluetoothDeviceData &vDevice); SAFE_CALL(doStart) // start the bluetooth adapter. SAFE_CALL(doScan ) // scan for devices. }; }