/*! * * 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 DeviceModels.cpp * \author (last) Behrouz NematiPour * \date (last) 18-Jul-2023 * \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 model 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 MDeviceCryptSetupResponse::fromByteArray * \details Checks the response and sets up the model 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_Len; 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 MDeviceFactoryResetResponse::fromByteArray * \details Checks the response and sets up the model 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 model 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 MDeviceWifiListResponse::fromByteArray * \details Checks the response and sets up the model data. * \param vExitCode - Passed script exit code * \return true if passed. */ bool MDeviceWifiListResponse::fromByteArray(const QByteArray &vByteArray, int *vExitCode) { //DEBUG qDebug() << __FUNCTION__ << vByteArray; // initialize data bool ok = false; int error = 0; _data.mCompleted = true; _data.mAccepted = false; _data.mReason = Device::DeviceError::eDevice_OK; // set as default if the val is invalid. _data.mWifiList = ""; // get the value QString val = vByteArray; ok = ! val.isNull() && val.isSimpleText(); // check if the vExitCode passed and it has a value other than zero if ( ! ok ){ _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp_Type ; goto lError; } // there is not a valid value if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_WifiList_Error ; goto lApply; } // there still a valid value // Now everything is good to extract the data _data.mAccepted = true; _data.mMessage = QObject::tr("The WiFi scan is complete."); goto lApply; // normal return // apply returned value in case the passed value is valid (type, range) // regardless of the exit code (success, fail) lApply: _data.mWifiList= val; goto lOut; // immediate exit on fail exitcode and invalid returned value. lError: if ( vExitCode ) error = *vExitCode; _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; lOut: return _data.mAccepted; } /*! * \brief MDeviceWifiInfoResponse::fromByteArray * \details Checks the response and sets up the model data. * \param vExitCode - Passed script exit code * \return true if passed. */ bool MDeviceWifiInfoResponse::fromByteArray(const QByteArray &vByteArray, int *vExitCode) { // initialize data bool ok = false; int error = 0; _data.mCompleted = true; _data.mAccepted = false; _data.mReason = Device::DeviceError::eDevice_OK; // set as default if the val is invalid. _data.mWifiInfo = ""; // get the value QString val = vByteArray; ok = ! val.isNull() && val.isSimpleText(); // check if the vExitCode passed and it has a value other than zero if ( ! ok ){ _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp_Type ; goto lError; } // there is not a valid value if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_WifiInfo_Error ; goto lApply; } // there still a valid value // Now everything is good to extract the data _data.mAccepted = true; _data.mMessage = QObject::tr("The WiFi info is complete."); goto lApply; // normal return // apply returned value in case the passed value is valid (type, range) // regardless of the exit code (success, fail) lApply: _data.mWifiInfo= val; goto lOut; // immediate exit on fail exitcode and invalid returned value. lError: if ( vExitCode ) error = *vExitCode; _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; lOut: return _data.mAccepted; } /*! * \brief MDeviceConnectWifiResponse::fromByteArray * \details Checks the response and sets up the model data. * \param vExitCode - Passed script exit code * \return true if passed. */ bool MDeviceConnectWifiResponse::fromByteArray(const QByteArray &vByteArray, int *vExitCode) { // initialize data bool ok = false; int error = 0; _data.mCompleted = true; _data.mAccepted = false; _data.mReason = Device::DeviceError::eDevice_OK; // set as default if the val is invalid. _data.mConnectResult = ""; // get the value QString val = vByteArray; ok = ! val.isNull() && val.isSimpleText(); // check if the vExitCode passed and it has a value other than zero if ( ! ok ){ _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp_Type ; goto lError; } // there is not a valid value if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_ConnectWifi_Error ; goto lApply; } // there still a valid value // Now everything is good to extract the data _data.mAccepted = true; _data.mMessage = QObject::tr("The WiFi (Dis)Connection is complete.") ; goto lOut ; // normal return // apply returned value in case the passed value is valid (type, range) // regardless of the exit code (success, fail) lApply: _data.mConnectResult = val; goto lOut; // immediate exit on fail exitcode and invalid returned value. lError: if ( vExitCode ) error = *vExitCode; _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 model data. * \param vExitCode - Passed script exit code * \return true if passed. */ bool MDeviceUSBMountResponse::fromByteArray(const QByteArray &vByteArray, int *vExitCode) { //DEBUG qDebug() << __FUNCTION__ << vByteArray; // initialize data bool ok = false; int error = 0; _data.mCompleted = true; // mark the data as final to be identified by the view. _data.mAccepted = false; _data.mReason = Device::DeviceError::eDevice_OK; // set as default if the val is invalid (min=0). // N/A // get the value ok = true; // check if the vExitCode passed and it has a value other than zero if ( ! ok ){ _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp_Type ; goto lError ; } // there is not a valid value if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_USBMount_Error ; goto lRevert; } // there still a valid value // Now everything is good to extract the data _data.mAccepted = true; _data.mMessage = QObject::tr("The USB (un)mount is complete."); goto lOut ; // normal return // apply returned value in case the passed value is valid (type, range) // regardless of the exit code (success, fail) lRevert: // N/A // immediate exit on fail exitcode and invalid returned value. lError: if ( vExitCode ) error = *vExitCode; _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; lOut: return _data.mAccepted;} bool MDeviceBrightnessResponse::fromByteArray(const QByteArray &vByteArray, int *vExitCode) { //DEBUG qDebug() << __FUNCTION__ << vByteArray; // initialize data bool ok = false; int error = 0; _data.mCompleted = true; // mark the data as final to be identified by the view. _data.mAccepted = false; _data.mReason = Device::DeviceError::eDevice_OK; // set as default if the val is invalid (min=0). _data.mBrightness = HW2UI.last(); // get the value quint8 val = vByteArray.toUShort(&ok); // check if the vExitCode passed and it has a value other than zero if ( ! ok ){ _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp_Type ; goto lError; } // there is not a valid value if ( ! HW2UI.contains(val) ){ _data.mReason = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Rsp_Range ; goto lError; } // there is not a valid value if ( vExitCode && *vExitCode ){ _data.mReason = Device::DeviceError::eDevice_Brightness_Error ; goto lApply; } // there still a valid value // Now everything is good to extract the data _data.mAccepted = true; _data.mMessage = QObject::tr("The Brightness set is complete."); goto lApply; // normal return // apply returned value in case the passed value is valid (type, range) // regardless of the exit code (success, fail) lApply: _data.mBrightness = HW2UI[val]; goto lOut; // immediate exit on fail exitcode and invalid returned value. lError: if ( vExitCode ) error = *vExitCode; _data.mMessage = Device::DeviceError::deviceErrorText(static_cast(_data.mReason), error) + "\n" + vByteArray; lOut: return _data.mAccepted; } QByteArray MDeviceBrightnessRequest::toByteArray(Device::DeviceError::Scripts_Error_Enum *vError) { // initialize data Device::DeviceError::Scripts_Error_Enum error = Device::DeviceError::Scripts_Error_Enum::eDevice_OK; /// return QString::number(UI2HW[_data.mBrightness]).toLatin1(); // set as default if the val is invalid (min=0). // get the value quint8 val = UI2HW.last(); // check if the vExitCode passed and it has a value other than zero if ( ! UI2HW.contains(_data.mBrightness) ){ error = Device::DeviceError::eDevice_Scripts_Error_Incorrect_Req_Range ; goto lError; } // there is not a valid value // if no error assign the passed value to val. val = UI2HW[_data.mBrightness]; return QString::number(val).toLatin1(); // set the error to ok (success), or any error(fail). lError: if ( vError && error ) *vError = error; return ""; }