Index: sources/device/DeviceModels.cpp =================================================================== diff -u -r61f16c988a159401c92730b4cbfca5085c77222f -r6c6f1f5d466badd9b4fd67be7c907234c342b2a2 --- sources/device/DeviceModels.cpp (.../DeviceModels.cpp) (revision 61f16c988a159401c92730b4cbfca5085c77222f) +++ sources/device/DeviceModels.cpp (.../DeviceModels.cpp) (revision 6c6f1f5d466badd9b4fd67be7c907234c342b2a2) @@ -1,26 +1,284 @@ /*! * - * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. + * Copyright (c) 2021-2023 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 MDeviceResponseBase.cpp + * \file DeviceModels.cpp * \author (last) Behrouz NematiPour - * \date (last) 01-Jun-2021 + * \date (last) 04-Mar-2022 * \author (original) Behrouz NematiPour - * \date (original) 01-Jun-2021 + * \date (original) 03-Jun-2021 * */ #include "DeviceModels.h" +// Qt +#include + +// Project + using namespace Model; /*! - * \brief MAdjustChemicalConfirmResponse::data - * \details Provides model's Data from the received messages data values - * \return Data + * \brief MDeviceBluetoothPairedResetResponse::fromByteArray + * \details Checks the response and sets up the mode data. + * \param vExitCode - Passed script exit code + * \return true if passed. */ -DeviceBrightnessResponseData MDeviceBrightnessResponse::data() const { - return _data; +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 MDeviceCryptSetupResponse::fromByteArray + * \details Checks the response and sets up the mode data. + * \param vExitCode - Passed script exit code + * \return true if passed. + */ +bool MDeviceCryptSetupResponse::fromByteArray(const QByteArray &vByteArray, 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_CryptSetup_Error; 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("The Encrypt Partition Command Complete."); goto lOut ; // normal return + +lError: + _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; + +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; +} + + +/*! + * \brief MDeviceFactoryResetResponse::fromByteArray + * \details Checks the response and sets up the mode data. + * \param vExitCode - Passed script exit code + * \return true if passed. + */ +bool MDeviceFactoryResetResponse::fromByteArray(const QByteArray &vByteArray, 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_FactoryReset_Error; 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("The Factory Reset Command Complete."); goto lOut ; // normal return + +lError: + _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; + +lOut: + return _data.mAccepted; +} + + +/*! + * \brief MDeviceDecommissionResponse::fromByteArray + * \details Checks the response and sets up the mode data. + * \param vExitCode - Passed script exit code + * \return true if passed. + */ +bool MDeviceDecommissionResponse::fromByteArray(const QByteArray &vByteArray, 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_FactoryReset_Error; 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("The Decommissioning Command Complete."); goto lOut ; // normal return + +lError: + _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; + +lOut: + return _data.mAccepted; +} + +/*! + * \brief MDeviceUSBMountResponse::fromByteArray + * \details Checks the response and sets up the mode data. + * \param vExitCode - Passed script exit code + * \return true if passed. + */ +bool MDeviceUSBMountResponse::fromByteArray(const QByteArray &vByteArray, 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_USBMount_Error; 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("The USB (un)mount Command Complete."); goto lOut ; // normal return + +lError: + _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; + +lOut: + return _data.mAccepted; +}