/*! * * Copyright (c) 2021-2022 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 DeviceModels.cpp * \author (last) Behrouz NematiPour * \date (last) 12-Nov-2021 * \author (original) Behrouz NematiPour * \date (original) 03-Jun-2021 * */ #include "DeviceModels.h" // Qt #include // Project using namespace Model; /*! * \brief MDeviceBluetoothPairedResetResponse::fromByteArray * \details Checks the response and sets up the mode data. * \param vExitCode - Passed script exit code * \return true if passed. */ bool MDeviceBluetoothPairedResetResponse::fromByteArray(const QByteArray &, int *vExitCode) { // initialize data int error = 0; _data.mAccepted = false; // check if the vExitCode passed and it has a value other than zero if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_BCuff_Error_Reset; error = *vExitCode; } else _data.mReason = Device::DeviceError::eDevice_OK; // if vExitCode is not zero go to error since the data is no longer valid if ( _data.mReason ) goto lError; // non-zero Exit code // Now everything is good to extract the data _data.mAccepted = true; _data.mMessage = QObject::tr("Removed previously paired device(s)."); goto lOut ; // normal return lError: _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error); lOut: return _data.mAccepted; } /*! * \brief MDeviceBluetoothPairedQueryResponse::fromByteArray * \details Checks the script returned value and * converts the returning paired device information from the process into the response model. * \param vByteArray - Passed script response data * \param vExitCode - Passed script exit code * \return true if passed value is valid for conversion. */ bool MDeviceBluetoothPairedQueryResponse::fromByteArray(const QByteArray &vByteArray, int *vExitCode) { // initialize data QRegularExpression regX("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"); QRegularExpressionValidator validator(regX); int pos = 0; QStringList deviceInfo; QString info = vByteArray.simplified().trimmed(); int count = 0; int error = 0; _data.mAccepted = false; _data.mAddr = ""; _data.mName = ""; _data.mInfo = deviceInfo; // check if the vExitCode passed and it has a value other than zero if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_BCuff_Error_Query; error = *vExitCode; } else _data.mReason = Device::DeviceError::eDevice_OK; // if vExitCode is not zero go to error since the data is no longer valid if ( _data.mReason ) goto lError; // non-zero Exit code // no paired device was found if ( info.isEmpty() ) { _data.mReason = Device::DeviceError::eDevice_BCuff_Error_Query_Empty; goto lError; // empty response } if ( info == ZERO ) { // it has been observed that the device returns address of all zero which is not what is accepted. _data.mReason = Device::DeviceError::eDevice_BCuff_Error_Query_Empty; goto lError; // incorrect response, address is all zero } deviceInfo = info.split(SEPARATOR); count = deviceInfo.count(); if ( count ) { if ( count != LEN_COUNT) { _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp; goto lError; // incorrect response, length } } if ( validator.validate(deviceInfo[eAddr], pos) != QValidator::Acceptable) { _data.mReason = Device::DeviceError::eDevice_BCuff_Error_Query_Addr; goto lError; // incorrect response, address invalid } // Now everything is good to extract the data _data.mAccepted = true; _data.mAddr = deviceInfo[eAddr]; _data.mName = deviceInfo[eName]; _data.mInfo = deviceInfo; _data.mMessage = QObject::tr("Paired BCuff Query %1 %2").arg(_data.mAddr).arg(_data.mName); goto lOut ; // normal return lError: _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error); lOut: return _data.mAccepted; } /*! * \brief MDeviceBrightnessRequest::setBrightnessSysVal * \details checks for the requested brightness percentage (in range and resolution) * \return returns device script error enum and is 0 on no error */ Device::DeviceError::Scripts_Error_Enum MDeviceBrightnessRequest::setBrightnessSysVal() { // check range(min,max) quint8 len = UI2HW.size(); if ( ! len ) { LOG_DEBUG("Empty UI2HW map"); return Device::DeviceError::eDevice_Scripts_Error_Unknown; } quint8 min = UI2HW.begin() .key(); quint8 max = (UI2HW.end () - 1).key(); if ( ! ( min <= _data.mBrightnessPercent && _data.mBrightnessPercent <= max ) ) { return Device::DeviceError::eDevice_Scripts_Error_OutOfRange; } // check range(resolution) if ( ! UI2HW.contains(_data.mBrightnessPercent) ) { return Device::DeviceError::eDevice_Scripts_Error_Incorrect_Req; } _data.mBrightness_val = UI2HW[_data.mBrightnessPercent]; return Device::DeviceError::eDevice_OK; } /*! * \brief MDeviceBrightnessResponse::setBrightnessPercent * \param vSysVal - updated the response with returned brightness system value * \return returns device script error enum and is 0 on no error */ Device::DeviceError::Scripts_Error_Enum MDeviceBrightnessResponse::setBrightnessPercent(quint8 vSysVal) { if ( ! HW2UI.contains( vSysVal ) ) { return Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp; } _data.mBrightnessPercent = HW2UI[ vSysVal ]; return Device::DeviceError::eDevice_OK; }