7 #include <boost/thread.hpp> 8 #include <boost/any.hpp> 9 #include <boost/asio.hpp> 10 #include <boost/bind.hpp> 11 #include <boost/enable_shared_from_this.hpp> 12 #include <boost/chrono.hpp> 13 #include <boost/asio/waitable_timer_service.hpp> 16 #include "gtest/gtest.h" 19 typedef boost::asio::basic_waitable_timer<boost::chrono::steady_clock>
steady_timer;
35 Message(std::string content=
"",
int timeout = 100,
int retries = 3, std::string expectedInitResponse =
"", std::string expectedEndResponse=
"");
49 int intervalMessage=0;
58 explicit Transaction(std::list<Message> list):messages(list) {}
63 explicit Transaction(std::initializer_list<Message> list): messages(list){}
85 std::list<std::string> list;
94 virtual void transactionProcessed(boost::system::error_code error_code,
Transaction transaction) = 0;
100 virtual void unsolicitedInfo(
const std::string&
message) = 0;
122 static std::string stringToHex(
const std::string& input,
bool printAsciiChar);
126 boost::system::error_code sendAsync(
const Transaction& transaction);
132 virtual bool rebootCommunications() = 0;
134 virtual boost::system::error_code sendSync(
Transaction& transaction);
136 boost::system::error_code sendSync(
Transaction& transaction);
141 boost::asio::io_service
mIo;
148 virtual boost::system::error_code send(std::string
message) = 0;
155 void receivedMessage(
const char* data,
size_t size);
160 std::vector<boost::shared_ptr<boost::thread>> mTransactionThreads;
162 std::list<Transaction> mQueue;
164 std::vector<MessageManagerObserver*> m_observersList;
166 std::string mDataReceived;
167 std::string mResponse;
168 int mConsecutiveErrors = 0;
172 std::mutex mMuxCV, mMuxStatus, mMuxQueue;
173 std::condition_variable mConditionVar;
175 void setStatus(
Status status);
176 void processQueue(
const boost::system::error_code& ec);
177 void processTransactions(std::list<Transaction>& queue);
178 void processMessages(boost::system::error_code& ec, std::list<Message>& messages,
int interval=0);
179 void processSingleTransaction(boost::system::error_code& ec,
Transaction transaction);
184 FRIEND_TEST(MessageManager_test, Send_ASyncNoRetries);
185 FRIEND_TEST(MessageManager_test, Send_ASyncRetries);
186 FRIEND_TEST(MessageManager_test, Send_ReceiveOK);
187 FRIEND_TEST(MessageManager_test, Send_ReceiveKO);
188 FRIEND_TEST(MessageManager_test, UnsolicitedInfoIdle);
189 FRIEND_TEST(MessageManager_test, UnsolicitedInfoBusy);
190 FRIEND_TEST(MessageManager_test, receiveUntilTimeoutExpired);
191 FRIEND_TEST(MessageManager_test, ReceiveMessageNoInitNoEnd);
192 FRIEND_TEST(MessageManager_test, ReceiveMessageInitNoEnd);
193 FRIEND_TEST(MessageManager_test, ReceiveMessageInitNoEndError);
194 FRIEND_TEST(MessageManager_test, ReceiveMessageInitNoEndDoubleMessage);
195 FRIEND_TEST(MessageManager_test, ReceiveMessageNoInitEndCorrectly);
196 FRIEND_TEST(MessageManager_test, ReceiveMessageInitEndCorrectly);
197 FRIEND_TEST(MessageManager_test, ReceiveMessageInitEndDoubleMsg);
198 FRIEND_TEST(MessageManager_test, BrokenUnsolicitedInfoMsg);
199 FRIEND_TEST(MessageManager_test, BrokenRequestMsg);
200 FRIEND_TEST(MessageManager_test, CoupledMsg_RequestUnsolicited);
201 FRIEND_TEST(MessageManager_test, CoupledMsg_UnsolicitedRequest);
202 FRIEND_TEST(MessageManager_test, CoupledMsg_RequestRequest);
203 FRIEND_TEST(MessageManager_test, IncompleteUnsolicitedMsg);
204 FRIEND_TEST(MessageManager_test, IncompleteRequestMsg);
205 FRIEND_TEST(MessageManager_test, UnsolicitedMsgWhile_RequestInitAndEndMsg);
206 FRIEND_TEST(MessageManager_test, UnsolicitedMsgWhile_RequestInit);
207 FRIEND_TEST(MessageManager_test, UnsolicitedMsgWhile_RequestEnd);
208 FRIEND_TEST(MessageManager_test, UnsolicitedMsgWhile_RequestNoInitNoEnd);
virtual std::list< std::string > getUnsolicitedInfoCodes()
Definition: messageManager.h:84
struct to keep a message from a sender
Definition: defTypes.h:24
Class to define the message object.
Definition: messageManager.h:26
Transaction()
Default constructor.
Definition: messageManager.h:53
int timeout
Timeout interval that the application will wait before considering the command sent as failed defined...
Definition: messageManager.h:30
Manages the status file.
Definition: status.h:44
std::string content
Command to send via serial , which may be transformed depending on the protocol.
Definition: messageManager.h:29
std::string expectedInitResponse
Expected init of the response.
Definition: messageManager.h:32
Transaction(std::list< Message > list)
Constructor using list.
Definition: messageManager.h:58
Message Manager observer class.
Definition: messageManager.h:77
boost::asio::basic_waitable_timer< boost::chrono::steady_clock > steady_timer
Typedef to use a steady timer.
Definition: dpyMwClient.h:52
boost::asio::io_service mIo
Boost asio input output service.
Definition: messageManager.h:141
std::string receivedResponse
Received response.
Definition: messageManager.h:34
std::list< Message > messages
List of messages.
Definition: messageManager.h:48
int retries
Number of times that the application will try to send the command after a timeout or a response diffe...
Definition: messageManager.h:31
std::string id
Transaction id.
Definition: messageManager.h:47
Class to define the transaction object.
Definition: messageManager.h:44
Transaction(std::initializer_list< Message > list)
Constructor using initializer list.
Definition: messageManager.h:63
Definition: otaupdaterApi.h:41
virtual ~MessageManagerObserver()
Destructor.
Definition: messageManager.h:105
std::string expectedEndResponse
Expected end of the response.
Definition: messageManager.h:33
Class which manages the logic relative to send/receive messages. This class implements all the logic ...
Definition: messageManager.h:117