From 6ebdc5f44721574b4fa89c688ffd7545f652ed6d Mon Sep 17 00:00:00 2001 From: Mateusz Grzywacz Date: Thu, 16 Jul 2020 16:20:48 +0200 Subject: [PATCH] [EGD-3435] fix SMS send again (moved to Query) --- image/user/sms.db | Bin 24576 -> 24576 bytes module-db/CMakeLists.txt | 1 + module-db/Common/Common.hpp | 3 +- module-db/Interface/SMSRecord.cpp | 30 ++++++- module-db/Interface/SMSRecord.hpp | 16 ++-- module-db/Tables/SMSTable.cpp | 36 +++++++- module-db/Tables/SMSTable.hpp | 2 + module-db/queries/sms/QuerySMSSearch.cpp | 1 + .../queries/sms/QuerySMSSearchByType.cpp | 32 +++++++ .../queries/sms/QuerySMSSearchByType.hpp | 34 ++++++++ .../service-cellular/ServiceCellular.cpp | 78 +++++++++++++----- .../service-cellular/ServiceCellular.hpp | 7 +- 12 files changed, 208 insertions(+), 32 deletions(-) create mode 100644 module-db/queries/sms/QuerySMSSearchByType.cpp create mode 100644 module-db/queries/sms/QuerySMSSearchByType.hpp diff --git a/image/user/sms.db b/image/user/sms.db index 3ae01e3215f9f5d03cfe000e6fb8441aac22f5b0..3da0aade6ce5e29f252c1e25e7c6d57ab6a0231d 100644 GIT binary patch delta 65 zcmV-H0KWf#zyW~30gxL3GLalZ1u_6GL&LFTrB4~F50(z;4z3P{4k->24g3w|4bBa> X4WJL%vjGo&4YQzKIsyR>liOb<)_4{+ delta 65 zcmV-H0KWf#zyW~30gxL3Fp(TX1uy_EKiRQlrB4~z533KB4(SfA4u%dX4iXLg4do5a X4Yv)TvjGo&4YQzKIsyRzliOb<-QE^9 diff --git a/module-db/CMakeLists.txt b/module-db/CMakeLists.txt index 7d2498542..35ea14021 100644 --- a/module-db/CMakeLists.txt +++ b/module-db/CMakeLists.txt @@ -48,6 +48,7 @@ set(SOURCES queries/RecordQuery.cpp queries/sms/QuerySMSSearch.cpp + queries/sms/QuerySMSSearchByType.cpp queries/sms/QuerySmsThreadMarkAsRead.cpp queries/calllog/QueryCalllogSetAllRead.cpp queries/notifications/QueryNotificationsGet.cpp diff --git a/module-db/Common/Common.hpp b/module-db/Common/Common.hpp index 5488a6bba..0944f57d2 100644 --- a/module-db/Common/Common.hpp +++ b/module-db/Common/Common.hpp @@ -9,10 +9,11 @@ */ #pragma once +#include // for uint32_t /** * Types of sms message */ -enum class SMSType +enum class SMSType : uint32_t { DRAFT = 0x01, FAILED = 0x02, diff --git a/module-db/Interface/SMSRecord.cpp b/module-db/Interface/SMSRecord.cpp index d32ab813d..5d5313b8f 100644 --- a/module-db/Interface/SMSRecord.cpp +++ b/module-db/Interface/SMSRecord.cpp @@ -1,9 +1,10 @@ #include "SMSRecord.hpp" #include "ContactRecord.hpp" #include "ThreadRecord.hpp" +#include "queries/sms/QuerySMSSearchByType.hpp" #include -#include +#include #include SMSRecord::SMSRecord(const SMSTableRow &w, const utils::PhoneNumber::View &num) @@ -235,3 +236,30 @@ SMSRecord SMSRecordInterface::GetByID(uint32_t id) return SMSRecord{sms, number}; } + +std::unique_ptr SMSRecordInterface::runQuery(const db::Query *query) +{ + if (const auto local_query = dynamic_cast(query)) { + return runQueryImpl(local_query); + } + return nullptr; +} + +std::unique_ptr SMSRecordInterface::runQueryImpl( + const db::query::SMSSearchByType *query) +{ + auto db_result = smsDB->sms.getManyByType(query->type, query->starting_postion, query->depth); + + auto records = std::make_unique>(); + + ContactRecordInterface contactInterface(contactsDB); + for (const auto &w : db_result.second) { + + auto contactRec = contactInterface.GetByID(w.contactID); + if (contactRec.numbers.size() != 0) { + // TODO: or numberUser? or other number + records->push_back({w, contactRec.numbers[0].number}); + } + } + return std::make_unique(db_result.first, *records); +} diff --git a/module-db/Interface/SMSRecord.hpp b/module-db/Interface/SMSRecord.hpp index 1df407f7e..8490b15dc 100644 --- a/module-db/Interface/SMSRecord.hpp +++ b/module-db/Interface/SMSRecord.hpp @@ -1,13 +1,15 @@ #pragma once -#include "Record.hpp" #include -#include "../Databases/SmsDB.hpp" -#include "../Databases/ContactsDB.hpp" -#include "utf8/UTF8.hpp" -#include "../Common/Common.hpp" +#include "Record.hpp" +#include "module-db/Databases/SmsDB.hpp" +#include "module-db/Databases/ContactsDB.hpp" +#include "module-db/queries/sms/QuerySMSSearchByType.hpp" +#include "module-db/Common/Common.hpp" + +#include "utf8/UTF8.hpp" #include struct SMSRecord : public Record @@ -53,8 +55,12 @@ class SMSRecordInterface : public RecordInterface SMSRecordField field, const char *str) override final; + std::unique_ptr runQuery(const db::Query *query) override; + private: const uint32_t snippetLength = 45; SmsDB *smsDB = nullptr; ContactsDB *contactsDB = nullptr; + + std::unique_ptr runQueryImpl(const db::query::SMSSearchByType *query); }; diff --git a/module-db/Tables/SMSTable.cpp b/module-db/Tables/SMSTable.cpp index 5fdc94d4e..66b8282c8 100644 --- a/module-db/Tables/SMSTable.cpp +++ b/module-db/Tables/SMSTable.cpp @@ -143,7 +143,7 @@ std::vector SMSTable::getLimitOffsetByField(uint32_t offset, do { ret.push_back(SMSTableRow{ - (*retQuery)[0].getUInt32(), // ID + {(*retQuery)[0].getUInt32()}, // ID (*retQuery)[1].getUInt32(), // threadID (*retQuery)[2].getUInt32(), // contactID (*retQuery)[3].getUInt32(), // date @@ -178,3 +178,37 @@ uint32_t SMSTable::countByFieldId(const char *field, uint32_t id) return uint32_t{(*queryRet)[0].getUInt32()}; } + +std::pair> SMSTable::getManyByType(SMSType type, uint32_t offset, uint32_t limit) +{ + auto ret = std::pair>{0, {}}; + auto count = db->query("SELECT COUNT (*) from sms WHERE type='%lu';", type); + ret.first = count == nullptr ? 0 : (*count)[0].getUInt32(); + if (ret.first != 0) { + limit = limit == 0 ? ret.first : limit; // no limit intended + auto retQuery = db->query( + "SELECT * from sms WHERE type='%lu' ORDER BY date ASC LIMIT %lu OFFSET %lu;", type, limit, offset); + + if (retQuery == nullptr || retQuery->getRowCount() == 0) { + ret.second = {}; + } + else { + do { + ret.second.push_back(SMSTableRow{ + {(*retQuery)[0].getUInt32()}, // ID + (*retQuery)[1].getUInt32(), // threadID + (*retQuery)[2].getUInt32(), // contactID + (*retQuery)[3].getUInt32(), // date + (*retQuery)[4].getUInt32(), // dateSent + (*retQuery)[5].getUInt32(), // errorCode + (*retQuery)[6].getString(), // body + static_cast((*retQuery)[7].getUInt32()), // type + }); + } while (retQuery->nextRow()); + } + } + else { + ret.second = {}; + } + return ret; +} diff --git a/module-db/Tables/SMSTable.hpp b/module-db/Tables/SMSTable.hpp index 91470ae5e..ee223d4f5 100644 --- a/module-db/Tables/SMSTable.hpp +++ b/module-db/Tables/SMSTable.hpp @@ -45,6 +45,8 @@ class SMSTable : public Table uint32_t count() override final; uint32_t countByFieldId(const char *field, uint32_t id) override final; + std::pair> getManyByType(SMSType type, uint32_t offset, uint32_t limit); + private: const char *createTableQuery = "CREATE TABLE IF NOT EXISTS sms(" "_id INTEGER PRIMARY KEY," diff --git a/module-db/queries/sms/QuerySMSSearch.cpp b/module-db/queries/sms/QuerySMSSearch.cpp index 348de0fb1..22cbe66fe 100644 --- a/module-db/queries/sms/QuerySMSSearch.cpp +++ b/module-db/queries/sms/QuerySMSSearch.cpp @@ -1,3 +1,4 @@ +#include "SMSTable.hpp" #include "QuerySMSSearch.hpp" namespace db::query diff --git a/module-db/queries/sms/QuerySMSSearchByType.cpp b/module-db/queries/sms/QuerySMSSearchByType.cpp new file mode 100644 index 000000000..56a6ea60b --- /dev/null +++ b/module-db/queries/sms/QuerySMSSearchByType.cpp @@ -0,0 +1,32 @@ +#include +#include "QuerySMSSearchByType.hpp" +#include + +namespace db::query +{ + SMSSearchByType::SMSSearchByType(SMSType type_to_search, unsigned int starting_position, unsigned int depth) + : Query(Query::Type::Read), type(type_to_search), starting_postion(starting_position), depth(depth) + {} + + auto SMSSearchByType::debugInfo() const -> std::string + { + return "SMSSearchByType"; + } + + SMSSearchByTypeResult::SMSSearchByTypeResult(unsigned int results_max_depth, std::vector result_rows) + : results(result_rows), results_max_depth(results_max_depth) + {} + auto SMSSearchByTypeResult::getResults() const -> std::vector + { + return results; + } + auto SMSSearchByTypeResult::debugInfo() const -> std::string + { + return "SMSSearchByTypeResult"; + } + auto SMSSearchByTypeResult::getMaxDepth() const -> unsigned int + { + return results_max_depth; + } + +} // namespace db::query diff --git a/module-db/queries/sms/QuerySMSSearchByType.hpp b/module-db/queries/sms/QuerySMSSearchByType.hpp new file mode 100644 index 000000000..87e46a7be --- /dev/null +++ b/module-db/queries/sms/QuerySMSSearchByType.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +class SMSRecord; + +namespace db::query +{ + /// implements search for SMS by type + class SMSSearchByType : public Query + { + public: + SMSType type; + unsigned int starting_postion; + unsigned int depth = 0; // 0 meaning no depth limit + SMSSearchByType(SMSType type_to_search, unsigned int starting_position, unsigned int depth = 0); + + [[nodiscard]] auto debugInfo() const -> std::string override; + }; + + class SMSSearchByTypeResult : public QueryResult + { + std::vector results; + unsigned int results_max_depth = 0; + + public: + SMSSearchByTypeResult(unsigned int results_max_depth, std::vector result_rows); + [[nodiscard]] auto getResults() const -> std::vector; + [[nodiscard]] auto getMaxDepth() const -> unsigned int; + [[nodiscard]] auto debugInfo() const -> std::string override; + }; + +} // namespace db::query diff --git a/module-services/service-cellular/ServiceCellular.cpp b/module-services/service-cellular/ServiceCellular.cpp index b74203c6b..ae188cf52 100644 --- a/module-services/service-cellular/ServiceCellular.cpp +++ b/module-services/service-cellular/ServiceCellular.cpp @@ -28,18 +28,16 @@ #include "MessageType.hpp" #include "messages/CellularMessage.hpp" -#include - -#include "log/log.hpp" +#include "SignalStrength.hpp" #include "service-appmgr/ApplicationManager.hpp" #include "service-appmgr/messages/APMMessage.hpp" -#include "service-cellular/SignalStrength.hpp" #include "service-evtmgr/messages/EVMessages.hpp" #include "ucs2/UCS2.hpp" #include "service-db/api/DBServiceAPI.hpp" #include "service-db/messages/DBNotificationMessage.hpp" +#include "service-db/messages/QueryMessage.hpp" #include "service-evtmgr/api/EventManagerServiceAPI.hpp" #include "service-antenna/api/AntennaServiceAPI.hpp" @@ -55,10 +53,13 @@ #include #include +#include + #include #include #include #include +#include const char *ServiceCellular::serviceName = "ServiceCellular"; @@ -472,7 +473,23 @@ bool ServiceCellular::handle_audio_conf_procedure() return true; } -sys::Message_t ServiceCellular::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage * /*resp*/) +auto ServiceCellular::handle(db::query::SMSSearchByTypeResult *response) -> bool +{ + if (response->getResults().size() > 0) { + LOG_DEBUG("sending %ud last queued message(s)", static_cast(response->getResults().size())); + for (auto &rec : response->getResults()) { + if (rec.type == SMSType::QUEUED) { + sendSMS(rec); + } + } + if (response->getMaxDepth() > response->getResults().size()) { + LOG_WARN("There still is/are messages QUEUED for sending"); + } + } + return true; +} + +sys::Message_t ServiceCellular::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp) { std::shared_ptr responseMsg; @@ -683,7 +700,14 @@ sys::Message_t ServiceCellular::DataReceivedHandler(sys::DataMessage *msgl, sys: } if (msg->interface == db::Interface::Name::SMS && (msg->type == db::Query::Type::Create || msg->type == db::Query::Type::Update)) { - sendSMS(); + // note: this gets triggered on every type update, e.g. on QUEUED → FAILED so way too often + + // are there new messges queued for sending ? + auto limitTo = 15; // how many to send in this Query + DBServiceAPI::GetQuery(this, + db::Interface::Name::SMS, + std::make_unique(SMSType::QUEUED, 0, limitTo)); + return std::make_shared(); } return std::make_shared(sys::ReturnCodes::Failure); @@ -731,9 +755,9 @@ sys::Message_t ServiceCellular::DataReceivedHandler(sys::DataMessage *msgl, sys: cmux->SelectAntenna(msg->antenna); vTaskDelay(50); // sleep for 50 ms... - auto actualAntenna = cmux->GetAntenna(); - bool changedAntenna = (actualAntenna == msg->antenna); - responseMsg = std::make_shared(changedAntenna); + auto actualAntenna = cmux->GetAntenna(); + bool changedAntenna = (actualAntenna == msg->antenna); + responseMsg = std::make_shared(changedAntenna); auto notification = std::make_shared(); sys::Bus::SendMulticast(notification, sys::BusChannels::AntennaNotifications, this); @@ -845,12 +869,29 @@ sys::Message_t ServiceCellular::DataReceivedHandler(sys::DataMessage *msgl, sys: responseMsg = std::make_shared(false); } } - if (responseMsg == nullptr) { - return std::make_shared(); + if (responseMsg != nullptr) { + responseMsg->responseTo = msgl->messageType; + return responseMsg; } - responseMsg->responseTo = msgl->messageType; - return responseMsg; + // handle database response + bool responseHandled = false; + if (resp != nullptr) { + if (auto msg = dynamic_cast(resp)) { + if (auto response = dynamic_cast(msg->getResult())) { + responseHandled = handle(response); + } + } + if (responseHandled) { + return std::make_shared(); + } + else { + return std::make_shared(sys::ReturnCodes::Unresolved); + } + } + else { + return std::make_shared(); + } } namespace { @@ -971,22 +1012,15 @@ std::optional> ServiceCellular::identifyNotific return std::nullopt; } -bool ServiceCellular::sendSMS(void) +bool ServiceCellular::sendSMS(SMSRecord record) { - auto record = DBServiceAPI::SMSGetLastRecord(this); - - // skip if it's not sms to send - if (record.type != SMSType::QUEUED) { - return false; - } - LOG_INFO("Trying to send SMS"); uint32_t textLen = record.body.length(); constexpr uint32_t commandTimeout = 5000; constexpr uint32_t singleMessageLen = 30; - bool result = false; + bool result = false; auto channel = cmux->get(TS0710::Channel::Commands); if (channel) { // if text fit in single message send diff --git a/module-services/service-cellular/ServiceCellular.hpp b/module-services/service-cellular/ServiceCellular.hpp index 582509280..52827186d 100644 --- a/module-services/service-cellular/ServiceCellular.hpp +++ b/module-services/service-cellular/ServiceCellular.hpp @@ -11,6 +11,7 @@ #define PUREPHONE_SERVICECELLULAR_HPP #include "CellularCall.hpp" +#include "SMSRecord.hpp" #include #include #include @@ -47,8 +48,7 @@ class ServiceCellular : public sys::Service static const char *serviceName; - bool sendSMS(UTF8 &number, UTF8 &text); - bool sendSMS(void); + bool sendSMS(SMSRecord record); bool receiveSMS(std::string messageNumber); /** * @brief Its getting selected SIM card own number. @@ -138,6 +138,9 @@ class ServiceCellular : public sys::Service void startStateTimer(uint32_t timeout); void stopStateTimer(void); void handleStateTimer(void); + + // db response handlers + auto handle(db::query::SMSSearchByTypeResult *response) -> bool; }; #endif // PUREPHONE_SERVICECELLULAR_HPP