/*! * * Copyright (c) 2019-2020 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 VBluetooth.cpp * \author (last) Behrouz NematiPour * \date (last) 8/23/2021 * \author (original) Behrouz NematiPour * \date (original) 8/23/2021 * */ #include "VBluetooth.h" // Qt // Project #include "ApplicationController.h" #include "GuiController.h" #include "BluetoothInterface.h" using namespace Bluetooth; using namespace Model; View::VBluetooth::VBluetooth(QObject *parent) : QAbstractListModel(parent) { initConnections(); } /*! * \brief VBluetooth::initConnections * Makes the necessary connections. Called inside VIEW_DEF_CLASS */ void View::VBluetooth::initConnections() { connect(&_BluetoothInterface, SIGNAL(didStateChange (BluetoothData )), this , SLOT( onStateChange (BluetoothData ))); connect(&_BluetoothInterface, SIGNAL(didDeviceChange(BluetoothDeviceData)), this , SLOT( onDeviceChange(BluetoothDeviceData))); connect(this , SIGNAL(didDeviceSelect(QString, QString )), this , SLOT( onDeviceSelect(QString, QString ))); } int View::VBluetooth::rowCount(const QModelIndex &) const { return _devices.count(); } void View::VBluetooth::reset() { beginResetModel(); _devices.clear(); endResetModel(); } void View::VBluetooth::onStateChange(const BluetoothData &vData) { if ( vData.state == MBluetooth::eIS_Local_Error_POST ) { isInvalid( true ); notify( vData.state ); return; } scanEnabled( ! (vData.state == MBluetooth::eIS_Close || // local vData.state == MBluetooth::eIS_Local_Init || // device vData.state == MBluetooth::eIS_Device_Connect || // service vData.state == MBluetooth::eIS_Service_Start || vData.state == MBluetooth::eIS_Service_Discover || vData.state == MBluetooth::eIS_Service_Detail )); switch (vData.state) { // The device name is not always available and the interface may be in investigation. case MBluetooth::eIS_Scan_NotFound : case MBluetooth::eIS_Scan_Discover : case MBluetooth::eIS_Scan_Found : deviceAddr (vData.deviceAddr ); deviceName (vData.deviceName ); devicePair (vData.devicePair ); break; case MBluetooth::eIS_Local_Init : localAddr (vData.localAddr ); localName (vData.localName ); break; case MBluetooth::eIS_Detail_Change : case MBluetooth::eIS_Detail_Read : case MBluetooth::eIS_Detail_Write : case MBluetooth::eIS_Config_Read : case MBluetooth::eIS_Config_Write : detailName (vData.detailName ); detailAddr (vData.detailAddr ); detailValue (vData.detailValue ); break; case MBluetooth::eIS_Scan_Start : reset(); break; case MBluetooth::eIS_Idle : case MBluetooth::eIS_Local_Connect : case MBluetooth::eIS_Local_Error_Invalid : case MBluetooth::eIS_Local_Error_POST : case MBluetooth::eIS_Local_Error_Off : case MBluetooth::eIS_Local_Error_IO : case MBluetooth::eIS_Local_Error : case MBluetooth::eIS_Local_Disconnect : case MBluetooth::eIS_Scan_Reject : case MBluetooth::eIS_Scan_Stop : case MBluetooth::eIS_Scan_Done : case MBluetooth::eIS_Device_Init : case MBluetooth::eIS_Device_Start : case MBluetooth::eIS_Device_Connect : case MBluetooth::eIS_Device_Error_Init : case MBluetooth::eIS_Device_Error : case MBluetooth::eIS_Device_Done : case MBluetooth::eIS_Device_Disconnect : case MBluetooth::eIS_Service_Start : case MBluetooth::eIS_Service_Error : case MBluetooth::eIS_Service_Discover : case MBluetooth::eIS_Service_Detail : case MBluetooth::eIS_Service_Detail_Invalid : case MBluetooth::eIS_Service_Detail_Error : case MBluetooth::eIS_Service_Detail_Done : case MBluetooth::eIS_Service_Done : case MBluetooth::eIS_Close : break; } error (vData.error ); devicePin (vData.devicePin ); notify( vData.state ); } void View::VBluetooth::onDeviceChange(const BluetoothDeviceData &vDevice) { int row = 0; while (row < _devices.count()) ++row; beginInsertRows(QModelIndex(), row, row); _devices.insert(row, vDevice); endInsertRows(); qDebug() << _devices.count(); for (auto device: _devices) { qDebug() << device.addr << device.name << device.pair; } } void View::VBluetooth::onDeviceSelect(const QString &vAddr, const QString &vName) { BluetoothDeviceData data; data.addr = vAddr; data.name = vName; emit _BluetoothInterface.didDeviceSelect(data); } QString View::VBluetooth::toText(MBluetooth::InterfaceStates vState) const { QString message; switch (vState) { case MBluetooth::eIS_Idle : message = "" ; break; case MBluetooth::eIS_Close : message = tr("BluetoothInterface Closed" ); break; // Used BluetoothInterface to be consistent with the case MBluetooth::eIS_Local_Init : message = tr("The Bluetooth Adapter Is Ready %1" ).arg(_localAddr ); break; case MBluetooth::eIS_Local_Connect : message = tr("The Bluetooth Adapter Connected" ); break; case MBluetooth::eIS_Local_Disconnect : message = tr("The Bluetooth Adapter Disconnected" ); break; case MBluetooth::eIS_Local_Error_Invalid : message = tr("No Valid Bluetooth Adapter" ); break; case MBluetooth::eIS_Local_Error_POST : message = tr("The Bluetooth Adapter POST Failed" ); break; case MBluetooth::eIS_Local_Error_Off : message = tr("The Bluetooth Adapter Is Off" ); break; case MBluetooth::eIS_Local_Error_IO : message = tr("The Bluetooth Adapter IO Error" ); break; case MBluetooth::eIS_Local_Error : message = tr("The Bluetooth Adapter Unknown Error" ); break; case MBluetooth::eIS_Scan_NotFound : message = tr("No Valid device found" ); break; case MBluetooth::eIS_Scan_Start : message = tr("Scanning ..." ); break; case MBluetooth::eIS_Scan_Reject : message = tr("Scanning Rejected" ); break; case MBluetooth::eIS_Scan_Discover : message = tr("Device Discovered" ); break; case MBluetooth::eIS_Scan_Found : message = tr("Blood Pressure Device Found" ); break; case MBluetooth::eIS_Scan_Stop : message = tr("Scanning Stopped" ); break; case MBluetooth::eIS_Scan_Done : message = tr("Scanning Finished" ); break; case MBluetooth::eIS_Device_Init : message = tr("Device Initializing ..." ); break; case MBluetooth::eIS_Device_Error_Init : message = tr("Device Initialization Error" ); break; case MBluetooth::eIS_Device_Start : message = tr("Device Connecting ..." ); break; case MBluetooth::eIS_Device_Error : message = tr("Device Connection Error" ); break; case MBluetooth::eIS_Device_Connect : message = tr("Device Connected" ); break; case MBluetooth::eIS_Device_Done : message = tr("Device Clean Up" ); break; case MBluetooth::eIS_Device_Disconnect : message = tr("Device Disconnected" ); break; case MBluetooth::eIS_Service_Start : message = tr("Service Scanning ..." ); break; case MBluetooth::eIS_Service_Error : message = tr("Service Error: %1" ).arg(_error ); break; case MBluetooth::eIS_Service_Discover : message = tr("Service Discovered" ); break; case MBluetooth::eIS_Service_Detail : message = tr("Service Detail Discovering ..." ); break; case MBluetooth::eIS_Service_Detail_Error : message = tr("Service Detail Error" ); break; case MBluetooth::eIS_Service_Detail_Invalid : message = tr("Service Detail Invalid" ); break; case MBluetooth::eIS_Service_Detail_Done : message = tr("Service Detail Done" ); break; case MBluetooth::eIS_Service_Done : message = tr("Service Clean Up" ); break; case MBluetooth::eIS_Detail_Change : message = tr("Service Characteristic Changed" ); break; case MBluetooth::eIS_Detail_Read : message = tr("Service Characteristic Read" ); break; case MBluetooth::eIS_Detail_Write : message = tr("Service Characteristic Write" ); break; case MBluetooth::eIS_Config_Read : message = tr("Service Descriptor Read" ); break; case MBluetooth::eIS_Config_Write : message = tr("Service Descriptor Write" ); break; // IMPORTANT: Do not use the "default:" to let compiler check for all the enumeration which are not handled. } return message; } void View::VBluetooth::notify(MBluetooth::InterfaceStates vState) { QString message = toText(vState); notification(message); // Console Log // DEBUG: message = _deviceAddr + " " + message + " " + _detailName + " " + _detailValue; // message = message.trimmed().simplified(); // qDebug().noquote().nospace() << message; // Service Log if ( vState != MBluetooth::eIS_Device_Start ) { // this is the "Device Connecting ..." state and has the interval of 1s and will fill up the service log fast, so removed from log. LOG_DEBUG(message); } } void View::VBluetooth::doScan () { _BluetoothInterface.doScan (); } void View::VBluetooth::doConnectToDevice () { _BluetoothInterface.doConnectToDevice (); } void View::VBluetooth::doDiscoverServices () { _BluetoothInterface.doDiscoverServices (); } void View::VBluetooth::doEnableNotify () { _BluetoothInterface.doEnableNotify (); } void View::VBluetooth::doReadMeasurements () { _BluetoothInterface.doReadMeasurements (); } void View::VBluetooth::doReadInformation () { _BluetoothInterface.doReadInformation (); }