Index: sources/cloudsync/CloudSyncController.cpp =================================================================== diff -u -rc9f8f8cf3c6c37fc6460d8675c62c9442c4d4263 -rf57ac5a2760518380bc876f5eb12e7ec0d07c066 --- sources/cloudsync/CloudSyncController.cpp (.../CloudSyncController.cpp) (revision c9f8f8cf3c6c37fc6460d8675c62c9442c4d4263) +++ sources/cloudsync/CloudSyncController.cpp (.../CloudSyncController.cpp) (revision f57ac5a2760518380bc876f5eb12e7ec0d07c066) @@ -23,6 +23,7 @@ #include "DeviceController.h" #include "FileHandler.h" #include "TreatmentLog.h" +#include "crc.h" SINGLETON_DISABLE(CloudSyncController) @@ -147,6 +148,7 @@ // a simple touch or a check-in message? testWatchBuffDate(); testDeviceInfoWait(); + qDebug() << " ~~~~~~~~~~ " << ""; } /*! @@ -232,7 +234,7 @@ Message message; QStringList items; - quint8 id; + quint32 id; if ( vContent.isEmpty() ) { error = eError_OutFileEmpty; ok = false; goto lErr; } @@ -329,6 +331,9 @@ case eError_TxCodeNoParam : text = tr( "CS No Treatment Code provided." ) ; break; case eError_TxCodeEmpty : text = tr( "CS The provided Treatment Code is empty." ) ; break; case eError_NotRegistered : text = tr( "CS Not Sent, Device not registered." ) ; break; + + case eError_Registration : text = tr( "CS The device registration failed." ) ; break; + case eError_TxReport : text = tr( "CS The treatment report delivery failed." ) ; break; } return text; } @@ -371,18 +376,38 @@ case eError_TxCodeNoParam : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; case eError_TxCodeEmpty : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; case eError_NotRegistered : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; + case eError_Registration : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; + case eError_TxReport : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; } return info; } /*! + * \brief CloudSyncController::errorOut + * \details the function to send out the error message to the cloudsync inp buffer. + * \param vErrorID - the error id from CloudSyncController::Errors_Enum in range of 950 to 999 + * \param vInfoItems - + */ +void CloudSyncController::errorOut(CloudSyncController::Errors_Enum vErrorID, const QVariantList &vInfoItems) { + QString msg = QString::number(UI2CS(eMessageID_Error)); + QString len = QString::number(1 + vInfoItems.count()); + QString prm = QString::number(vErrorID); + QStringList buf = { msg, len, prm }; + for (const auto &item: vInfoItems) { + buf += item.toString(); + } + sendUIBuff(buf); +} + +/*! * \brief CloudSyncController::toLog * \details logs the error in debug mode (service log) * \param vErrorID - error id * \param vInfoItems - extra information */ void CloudSyncController::toLog(CloudSyncController::Errors_Enum vErrorID, const QVariantList &vInfoItems) { + errorOut(vErrorID, vInfoItems); LOG_DEBUG(toText(vErrorID) + " " + toInfo(vErrorID, vInfoItems)); } @@ -436,16 +461,19 @@ * \param vMessage - The message id with destination added. 2k, 1K * \return the formatted buffer */ -QString CloudSyncController::makeUIBuff(const qint32 vMessageID) +QStringList CloudSyncController::makeUIBuff(const qint32 vMessageID, const QStringList &vPrm) { - QString msg = QString::number(vMessageID); - QString len = QString::number(_uiHistory[ vMessageID ].count()); - QString prm = _uiHistory[ vMessageID ].join(_separator); - return QString("%1,%2,%3") - .arg( msg ) - .arg( len ) - .arg( prm ) - ; + int count = vPrm.count(); + QStringList prm = count ? vPrm : _uiHistory[ vMessageID ]; + QString msg = QString::number(vMessageID); + QString len = QString::number(count ); + + QStringList dataList; + dataList += msg; + dataList += len; + dataList += prm; + + return dataList; } /*! @@ -492,15 +520,16 @@ * \return true on successful writing to the file buffer. * \sa _inp_File */ -bool CloudSyncController::sendUIBuff(const QString &vData) +bool CloudSyncController::sendUIBuff(const QStringList &vDataList) { bool ok = true; QString inpBuff = "%1,%2,%3,%4"; + inpBuff = inpBuff .arg( _secSinceEpoch ) .arg( Types::safeIncrement(_seq)) - .arg( generateCRC() ) - .arg( vData ); + .arg( generateCRC(_secSinceEpoch, _seq, vDataList)) + .arg( vDataList.join(_separator)); ok = writeInpFile(inpBuff); return ok; @@ -646,6 +675,8 @@ case eMessageID_TxReport : /* No Req/Rsp, it is event based */ break; // This message doesn't have the response since there is no request. UI will send when the data ready by HD. + case eMessageID_Error : ok = errorHandler ( vMessage ); break; + } return ok; } @@ -702,7 +733,7 @@ // if ( ! ok ) { } /* Not defined */ qint32 messageID = UI2CS(eMessageID_ResetFactory); // TODO : Store registration success in here - ok = sendUIBuff(QString("%1,1,%2").arg( messageID ).arg( ok ? eSucceed : eFailed )); + ok = sendUIBuff({ QString("%1").arg( messageID ) ,"1", QString("%1").arg( ok ? eSucceed : eFailed ) }); return ok; } @@ -758,7 +789,7 @@ QString destination = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + Storage::CloudSync_Credentials_Folder_Name; if ( Storage::FileHandler::copyFolder(source, destination) != 0 ) { toLog(eError_CredentialCopy , {destination }); ok = false; goto lOut; } - ok = sendUIBuff(QString("%1,1,%2").arg( messageID ).arg(destination)); + ok = sendUIBuff({QString("%1").arg( messageID ), "1", QString("%1").arg(destination)}); lOut: return ok; @@ -773,7 +804,7 @@ enum { eSucceed, eFailed }; bool ok = false; qint32 messageID = UI2CS(eMessageID_CredentialsSave); - ok = sendUIBuff(QString("%1,1,%2").arg( messageID ).arg( ok ? eSucceed : eFailed )); + ok = sendUIBuff({ QString("%1").arg( messageID ) ,"1", QString("%1").arg( ok ? eSucceed : eFailed ) }); return ok; } @@ -802,6 +833,18 @@ } /*! + * \brief CloudSyncController::errorHandler + * \details nothing for now. + * \param vMessage - the received message + * \return always true for now. + */ +bool CloudSyncController::errorHandler(const Message &vMessage) +{ + qDebug() << "ERR: " << vMessage.params; + return true; +} + +/*! * \brief CloudSyncController::sendDeviceRegister * \details sends the device registration request * \return true on successful send. @@ -836,7 +879,6 @@ bool CloudSyncController::isRegistered() { QString source = QString(Storage::CloudSync_Base_Path_Name) + Storage::CloudSync_Credentials_Folder_Name; - qDebug() << source; QFileInfoList fileInfos = QDir(source).entryInfoList(QDir::NoDotAndDotDot|QDir::Files); return !fileInfos.isEmpty(); } @@ -943,11 +985,38 @@ /*! * \brief CloudSyncController::initDeviceInfoWait - * \details if currently not not in wait, then set the wait to the secs of wait. + * \details if currently not in wait, then set the wait to the secs of wait. */ void CloudSyncController::initDeviceInfoWait() { if ( _deviceInfoStop ) { // if timer is stopped _deviceInfoWait = _deviceInfoSecs; // enable the timer. _deviceInfoStop = false; } } + +/*! + * \brief CloudSyncController::generateCRC + * \details The crc algorithm is to get + * all the message sections including, epoch, seq, and the parameters, + * without any separator + * concatenate them together like a one long string + * and run the crc on them + * \example 1674193951,4,,1001,3,HD1234567890123,DG1234567890123,DEN-14517-UI-BN-S88.01192111.1 + * str: 1674193951410013HD1234567890123DG1234567890123DEN-14517-UI-BN-S88.01192111.1 + * out: 145 + * \note the parameters values will keep untouched, meaning, if a parameter has space it will not be removed. + * \param vSecSinceEpoch + * \param vSeq - the message sequence + * \param vData - the message payload in list of strings + * \return - quint8 crc code + */ +quint8 CloudSyncController::generateCRC(quint64 vSecSinceEpoch, quint64 vSeq, const QStringList &vDataList) +{ + quint8 crc = 0; + QByteArray buf; + buf += QByteArray::number(vSecSinceEpoch); + buf += QByteArray::number(vSeq); + buf += vDataList.join("").toLatin1(); + crc = crc8(buf); + return crc; +} Index: sources/cloudsync/CloudSyncController.h =================================================================== diff -u -rc9f8f8cf3c6c37fc6460d8675c62c9442c4d4263 -rf57ac5a2760518380bc876f5eb12e7ec0d07c066 --- sources/cloudsync/CloudSyncController.h (.../CloudSyncController.h) (revision c9f8f8cf3c6c37fc6460d8675c62c9442c4d4263) +++ sources/cloudsync/CloudSyncController.h (.../CloudSyncController.h) (revision f57ac5a2760518380bc876f5eb12e7ec0d07c066) @@ -75,37 +75,48 @@ QString _deviceInfoDG = ""; QString _deviceInfoUI = ""; + enum Errors_Enum { - eError_Unknown = 500, // Unknown error, initial error before error check + // CS : 900 - 949 + // UI : 950 - 999 + eError_Unknown = 900, // Unknown error, initial error before error check - eError_HeaderCount = 501, - eError_Timestamp = 502, - eError_Sequence = 503, - eError_CRC = 504, - eError_MessageID = 505, - eError_InvalidID = 506, - eError_ParamCount = 507, - eError_ParamMismatch = 508, - eError_ParamMissing = 509, - eError_NoHistory = 510, - eError_Duplicate = 511, + // CS to UI Error start + eError_Registration = 901, + eError_TxReport = 902, - eError_LogFolder = 512, - eError_LogFileInp = 513, - eError_CredentialMake = 514, // the UI vault folder for cloudsync credentials can't be created. - eError_CredentialFile = 515, // the credential files sent to UI can't be find or read or doesn't exist.. - eError_CredentialCopy = 516, // the credential files sent to UI can't be copied to UI vault. - eError_CredentialRemove = 517, // the credential files sent to UI can't be removed. - eError_CredentialEmpty = 518, // the UI folder doesn't have credential files. + // UI to CS Error start + // eError_UI_Base = 950, // No Used + eError_HeaderCount = 951, + eError_Timestamp = 952, + eError_Sequence = 953, + eError_CRC = 954, + eError_MessageID = 955, + eError_InvalidID = 956, + eError_ParamCount = 957, + eError_ParamMismatch = 958, + eError_ParamMissing = 959, - eError_TxCodeNoParam = 519, // the received Tx Code not provided - eError_TxCodeEmpty = 520, // the received Tx Code is empty + eError_NoHistory = 960, + eError_Duplicate = 961, - eError_OutFileEmpty = 521, // Out file has changed from CS2UI but the content is empty. + eError_LogFolder = 962, + eError_LogFileInp = 963, - eError_NotRegistered = 522, // avoid sending any message other than the device regitration, when device is not registered. + eError_CredentialMake = 964, // the UI vault folder for cloudsync credentials can't be created. + eError_CredentialFile = 965, // the credential files sent to UI can't be find or read or doesn't exist.. + eError_CredentialCopy = 966, // the credential files sent to UI can't be copied to UI vault. + eError_CredentialRemove = 967, // the credential files sent to UI can't be removed. + eError_CredentialEmpty = 968, // the UI folder doesn't have credential files. + + eError_TxCodeNoParam = 969, // the received Tx Code not provided + eError_TxCodeEmpty = 970, // the received Tx Code is empty + + eError_OutFileEmpty = 971, // Out file has changed from CS2UI but the content is empty. + + eError_NotRegistered = 972, // avoid sending any message other than the device registration, when device is not registered. }; typedef QHash MessageList; @@ -138,8 +149,6 @@ enum MessageID_Enum { eMessageID_Start = 0, // // [ #1( ID ) <-> #2( ID ) ] Description ( #1 Requests and #2 responses ) - // Manufacturing - eMessageID_Error = 0, // [ CS(2000) <-> UI(1000) ] Error Report eMessageID_DeviceRegister = 1, // [ UI(1001) -> CS( ) ] Device Registration Request eMessageID_DeviceInfo = 2, // [ CS(2002) <-> UI(1002) ] Device information Request @@ -160,12 +169,15 @@ // eMessageID_DeviceReport = 204, // [No CS req defined] UI sends the device report // eMessageID_HeartBeat = 900, // CS sends the periodic Hb and UI can set the interval + eMessageID_Error = 999, // [ CS(2000) <-> UI(1000) ] Error Report + eMessageID_Count }; QHash paramCount { { eMessageID_CredentialsSave , 3 }, { eMessageID_TxCodeDisplay , 1 }, + { eMessageID_Error , 4 }, }; enum DeviceInfo_Enum { @@ -200,29 +212,30 @@ void initThread(QThread &vThread); void quitThread(); - quint8 generateCRC () { return 0; } // has not been implemented/decided yet + quint8 generateCRC (quint64 vSecSinceEpoch, quint64 vSeq, const QStringList &vDataList); bool validateCRC () { return true; } // has not been implemented/decided yet void testWatchBuffDate (); bool interpret (const QString &vContent, Message &vMessage); bool addCSBuffWatch (); - QString makeUIBuff (const qint32 vMessageID ); + QStringList makeUIBuff (const qint32 vMessageID , const QStringList &vPrm = {}); - bool isDuplicate (const qint32 vMessageID , const QStringList &vData); + bool isDuplicate (const qint32 vMessageID , const QStringList &vData); - bool sendUIResponse (const QString &vContent ); - bool sendUIBuff (const QString &vData ); - bool saveUIHistory (const qint32 vAction , const QVariantList &vData); - bool sendUIHistory (const qint32 vAction ); - bool sendMessage (const Message &vMessage ); + bool sendUIResponse (const QString &vContent ); + bool sendUIBuff (const QStringList &vData ); + bool saveUIHistory (const qint32 vAction , const QVariantList &vData); + bool sendUIHistory (const qint32 vAction ); + bool sendMessage (const Message &vMessage ); - bool writeInpFile (const QString &vInpBuff ); + bool writeInpFile (const QString &vInpBuff ); QString toText (Errors_Enum vErrorID); QString toInfo (Errors_Enum vErrorID, const QVariantList &vInfoItems); void toLog (Errors_Enum vErrorID, const QVariantList &vInfoItems); + void errorOut (Errors_Enum vErrorID, const QVariantList &vInfoItems); qint32 UI2CS (qint32 vID) { return vID + eUI ; } qint32 CS2UI (qint32 vID) { return abs(vID - eCS); } @@ -257,4 +270,5 @@ bool sendTxCodeDisplay (const Message &vMessage); // eMessageID_TxCodeDisplay + bool errorHandler (const Message &vMessage); // eMessageID_Error };