messages that expect a reply may optionally block to wait for it.
This commit is contained in:
parent
a355edd722
commit
e316c71a83
|
@ -23,6 +23,8 @@
|
|||
*/
|
||||
#include "widget.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace ENTTEC {
|
||||
|
||||
Widget::Widget()
|
||||
|
@ -386,24 +388,55 @@ void Widget::rebootBootloader()
|
|||
|
||||
/**
|
||||
* @brief Widget::getSerialNumber
|
||||
* @param timeout block for up to 'timeout' milliseconds waiting for a reply
|
||||
* @param timeout Milliseconds to block while waiting for the device to reply.
|
||||
* @return True if the serial number has been updated.
|
||||
*/
|
||||
void Widget::getSerialNumber()
|
||||
bool Widget::getSerialNumber(int timeout)
|
||||
{
|
||||
{
|
||||
std::scoped_lock lock(mtx_metadata_);
|
||||
reply_serialNumber_ = std::promise<bool>(); // expect a reply to this message
|
||||
}
|
||||
auto ftr = reply_serialNumber_.get_future(); // will be "true" when reply received
|
||||
auto msg = std::make_shared<Pro::MsgGetWidgetSerialRequest>();
|
||||
sendMessage(msg);
|
||||
auto status = ftr.wait_for(std::chrono::milliseconds(timeout)); // wait for the reply
|
||||
switch (status) {
|
||||
case std::future_status::ready:
|
||||
return true;
|
||||
case std::future_status::timeout:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Widget::getParameters
|
||||
* @param user_length
|
||||
* @param timeout Milliseconds to block while waiting for the device to reply.
|
||||
* @return True if the parameters has been updated.
|
||||
*/
|
||||
void Widget::getParameters(size_t user_length)
|
||||
bool Widget::getParameters(size_t user_length, int timeout)
|
||||
{
|
||||
{
|
||||
std::scoped_lock lock(mtx_metadata_);
|
||||
reply_parameters_ = std::promise<bool>(); // expect a reply to this message
|
||||
}
|
||||
auto ftr = reply_parameters_.get_future(); // will be "true" when reply received
|
||||
auto msg = std::make_shared<Pro::MsgGetWidgetParametersRequest>();
|
||||
msg->size = std::min(user_length, Pro::USER_CONFIGURATION_MAX);
|
||||
sendMessage(msg);
|
||||
auto status = ftr.wait_for(std::chrono::milliseconds(timeout)); // wait for the reply
|
||||
switch (status) {
|
||||
case std::future_status::ready:
|
||||
return true;
|
||||
case std::future_status::timeout:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "pro.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <universe.h>
|
||||
#include <vector>
|
||||
|
@ -78,9 +79,9 @@ protected:
|
|||
// USB Host operations
|
||||
virtual void sendMessage(std::shared_ptr<Pro::MessageData>) const;
|
||||
void rebootBootloader();
|
||||
void getParameters(size_t user_length = 0);
|
||||
bool getParameters(size_t user_length = 0, int timeout = 0);
|
||||
void setParameters() const;
|
||||
void getSerialNumber();
|
||||
bool getSerialNumber(int timeout = 0);
|
||||
void sendDmx(bool trimmed = true) const;
|
||||
|
||||
/**
|
||||
|
@ -123,6 +124,8 @@ private:
|
|||
|
||||
mutable std::mutex mtx_metadata_;
|
||||
std::shared_ptr<void> token_data_changed_;
|
||||
std::promise<bool> reply_parameters_;
|
||||
std::promise<bool> reply_serialNumber_;
|
||||
};
|
||||
|
||||
} // namespace ENTTEC
|
||||
|
|
Loading…
Reference in New Issue
Block a user