mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-17 08:43:34 +08:00
FirmwareUpdateTrigger - fixes and test
This commit is contained in:
@@ -11,34 +11,66 @@ using namespace uavcan::protocol::file;
|
||||
|
||||
struct FirmwareVersionChecker : public uavcan::IFirmwareVersionChecker
|
||||
{
|
||||
virtual bool shouldSendFirmwareUpdateRequest(uavcan::NodeID node_id,
|
||||
const uavcan::protocol::GetNodeInfo::Response& node_info,
|
||||
FirmwareFilePath& out_firmware_file_path)
|
||||
unsigned should_request_cnt;
|
||||
unsigned should_retry_cnt;
|
||||
unsigned confirmation_cnt;
|
||||
|
||||
std::string firmware_path;
|
||||
|
||||
bool should_retry;
|
||||
std::string expected_node_name_to_update;
|
||||
|
||||
BeginFirmwareUpdate::Response last_error_response;
|
||||
|
||||
FirmwareVersionChecker()
|
||||
: should_request_cnt(0)
|
||||
, should_retry_cnt(0)
|
||||
, confirmation_cnt(0)
|
||||
, should_retry(false)
|
||||
{ }
|
||||
|
||||
virtual bool shouldRequestFirmwareUpdate(uavcan::NodeID node_id,
|
||||
const uavcan::protocol::GetNodeInfo::Response& node_info,
|
||||
FirmwareFilePath& out_firmware_file_path)
|
||||
{
|
||||
(void)node_id;
|
||||
(void)node_info;
|
||||
(void)out_firmware_file_path;
|
||||
return false;
|
||||
should_request_cnt++;
|
||||
std::cout << "REQUEST? " << int(node_id.get()) << "\n" << node_info << std::endl;
|
||||
out_firmware_file_path = firmware_path.c_str();
|
||||
return node_info.name == expected_node_name_to_update;
|
||||
}
|
||||
|
||||
virtual bool shouldRetryFirmwareUpdateRequest(uavcan::NodeID node_id,
|
||||
const BeginFirmwareUpdate::Response& error_response,
|
||||
FirmwareFilePath& out_firmware_file_path)
|
||||
virtual bool shouldRetryFirmwareUpdate(uavcan::NodeID node_id,
|
||||
const BeginFirmwareUpdate::Response& error_response,
|
||||
FirmwareFilePath& out_firmware_file_path)
|
||||
{
|
||||
(void)node_id;
|
||||
(void)error_response;
|
||||
(void)out_firmware_file_path;
|
||||
return false;
|
||||
last_error_response = error_response;
|
||||
std::cout << "RETRY? " << int(node_id.get()) << "\n" << error_response << std::endl;
|
||||
should_retry_cnt++;
|
||||
out_firmware_file_path = firmware_path.c_str();
|
||||
return should_retry;
|
||||
}
|
||||
|
||||
virtual void handleFirmwareUpdateConfirmation(uavcan::NodeID node_id,
|
||||
const BeginFirmwareUpdate::Response& response)
|
||||
{
|
||||
(void)node_id;
|
||||
(void)response;
|
||||
confirmation_cnt++;
|
||||
std::cout << "CONFIRMED " << int(node_id.get()) << "\n" << response << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
static uint8_t response_error_code = 0;
|
||||
|
||||
static void beginFirmwareUpdateRequestCallback(
|
||||
const uavcan::ReceivedDataStructure<typename BeginFirmwareUpdate::Request>& req,
|
||||
uavcan::ServiceResponseDataStructure<typename BeginFirmwareUpdate::Response>& res)
|
||||
{
|
||||
std::cout << "REQUEST\n" << req << std::endl;
|
||||
|
||||
res.error = response_error_code;
|
||||
res.optional_error_message = "foobar";
|
||||
}
|
||||
|
||||
|
||||
TEST(FirmwareUpdateTrigger, Basic)
|
||||
{
|
||||
uavcan::GlobalDataTypeRegistry::instance().reset();
|
||||
@@ -76,8 +108,87 @@ TEST(FirmwareUpdateTrigger, Basic)
|
||||
|
||||
ASSERT_LE(0, provider->startAndPublish());
|
||||
|
||||
ASSERT_FALSE(trigger.isTimerRunning());
|
||||
ASSERT_EQ(0, trigger.getNumPendingNodes());
|
||||
|
||||
/*
|
||||
* Discovering one node
|
||||
* Updating one node
|
||||
* The server that can confirm the request is not running yet
|
||||
*/
|
||||
checker.firmware_path = "firmware_path";
|
||||
checker.expected_node_name_to_update = "Ivan";
|
||||
checker.should_retry = true;
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2000));
|
||||
|
||||
ASSERT_TRUE(trigger.isTimerRunning());
|
||||
ASSERT_EQ(1, trigger.getNumPendingNodes());
|
||||
|
||||
ASSERT_EQ(1, checker.should_request_cnt);
|
||||
ASSERT_EQ(0, checker.should_retry_cnt);
|
||||
ASSERT_EQ(0, checker.confirmation_cnt);
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2000));
|
||||
|
||||
// Still running!
|
||||
ASSERT_TRUE(trigger.isTimerRunning());
|
||||
ASSERT_EQ(1, trigger.getNumPendingNodes());
|
||||
|
||||
/*
|
||||
* Starting the firmware update server that returns an error
|
||||
* The checker will instruct the trigger to repeat
|
||||
*/
|
||||
uavcan::ServiceServer<BeginFirmwareUpdate> srv(nodes.b);
|
||||
|
||||
ASSERT_LE(0, srv.start(beginFirmwareUpdateRequestCallback));
|
||||
|
||||
response_error_code = BeginFirmwareUpdate::Response::ERROR_UNKNOWN;
|
||||
checker.should_retry = true;
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(1100));
|
||||
|
||||
ASSERT_EQ(1, checker.should_request_cnt);
|
||||
ASSERT_EQ(1, checker.should_retry_cnt);
|
||||
ASSERT_EQ(0, checker.confirmation_cnt);
|
||||
|
||||
// Still running!
|
||||
ASSERT_TRUE(trigger.isTimerRunning());
|
||||
ASSERT_EQ(1, trigger.getNumPendingNodes());
|
||||
|
||||
/*
|
||||
* Trying again, this time with ERROR_IN_PROGRESS
|
||||
*/
|
||||
response_error_code = BeginFirmwareUpdate::Response::ERROR_IN_PROGRESS;
|
||||
checker.should_retry = false;
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2100));
|
||||
|
||||
ASSERT_EQ(1, checker.should_request_cnt);
|
||||
ASSERT_EQ(1, checker.should_retry_cnt);
|
||||
ASSERT_EQ(1, checker.confirmation_cnt);
|
||||
|
||||
// Stopped!
|
||||
ASSERT_FALSE(trigger.isTimerRunning());
|
||||
ASSERT_EQ(0, trigger.getNumPendingNodes());
|
||||
|
||||
/*
|
||||
* Restarting the node info provider
|
||||
* Now it doesn't need an update
|
||||
*/
|
||||
provider.reset(new uavcan::NodeStatusProvider(nodes.b));
|
||||
|
||||
provider->setName("Dmitry");
|
||||
provider->setHardwareVersion(hwver);
|
||||
|
||||
ASSERT_LE(0, provider->startAndPublish());
|
||||
|
||||
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2100));
|
||||
|
||||
ASSERT_EQ(2, checker.should_request_cnt);
|
||||
ASSERT_EQ(1, checker.should_retry_cnt);
|
||||
ASSERT_EQ(1, checker.confirmation_cnt);
|
||||
|
||||
// Stopped!
|
||||
ASSERT_FALSE(trigger.isTimerRunning());
|
||||
ASSERT_EQ(0, trigger.getNumPendingNodes());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user