Index: sources/device/DeviceModels.cpp =================================================================== diff -u -r61f16c988a159401c92730b4cbfca5085c77222f -rbefa8e7e7667c4aa5193022bd80e9f52c2374ad0 --- sources/device/DeviceModels.cpp (.../DeviceModels.cpp) (revision 61f16c988a159401c92730b4cbfca5085c77222f) +++ sources/device/DeviceModels.cpp (.../DeviceModels.cpp) (revision befa8e7e7667c4aa5193022bd80e9f52c2374ad0) @@ -14,13 +14,108 @@ */ #include "DeviceModels.h" +// Qt +#include + +// Project +#include "DeviceError.h" + 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 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; +}