Device: Extend open() API for long-running tasks

pull/35/head
Paul Hollinsky 2021-04-29 19:08:31 -04:00
parent 3d4e0a27e3
commit b7c7d4349a
3 changed files with 56 additions and 9 deletions

View File

@ -2,6 +2,7 @@
#include "icsneo/communication/message/callback/messagecallback.h"
#include "icsneo/api/eventmanager.h"
#include "icsneo/communication/command.h"
#include "icsneo/device/extensions/deviceextension.h"
#include <string.h>
#include <iostream>
#include <sstream>
@ -164,7 +165,7 @@ void Device::enforcePollingMessageLimit() {
}
}
bool Device::open() {
bool Device::open(OpenFlags flags, OpenStatusHandler handler) {
if(!com) {
report(APIEvent::Type::Unknown, APIEvent::Severity::Error);
return false;
@ -178,8 +179,8 @@ bool Device::open() {
if(attemptErr != APIEvent::Type::NoErrorFound) {
// We could not communicate with the device, let's see if an extension can
bool tryAgain = false;
forEachExtension([&tryAgain](const std::shared_ptr<DeviceExtension>& ext) -> bool {
if(ext->onDeviceCommunicationDead())
forEachExtension([&tryAgain, &flags, &handler](const std::shared_ptr<DeviceExtension>& ext) -> bool {
if(ext->onDeviceCommunicationDead(flags, handler))
tryAgain = true;
return true;
});
@ -197,8 +198,8 @@ bool Device::open() {
}
bool block = false;
forEachExtension([&block](const std::shared_ptr<DeviceExtension>& ext) {
if(ext->onDeviceOpen())
forEachExtension([&block, &flags, &handler](const std::shared_ptr<DeviceExtension>& ext) {
if(ext->onDeviceOpen(flags, handler))
return true;
block = true;
return false;

View File

@ -8,13 +8,13 @@
#include <utility>
#include <cstring>
#include <atomic>
#include <type_traits>
#include "icsneo/api/eventmanager.h"
#include "icsneo/api/lifetime.h"
#include "icsneo/device/neodevice.h"
#include "icsneo/device/idevicesettings.h"
#include "icsneo/device/nullsettings.h"
#include "icsneo/device/devicetype.h"
#include "icsneo/device/extensions/deviceextension.h"
#include "icsneo/communication/communication.h"
#include "icsneo/communication/packetizer.h"
#include "icsneo/communication/encoder.h"
@ -29,6 +29,8 @@
namespace icsneo {
class DeviceExtension;
class Device {
public:
virtual ~Device();
@ -50,7 +52,50 @@ public:
return os;
}
virtual bool open();
class OpenFlags {
public:
enum Enum {
/**
* Even if the firmware does not match the current firmware version,
* the device will not be updated.
*
* Note: The device may still be flashed if the device has no firmware
*
* This has no effect if the DFU extension is not present
*/
SuppressAutoUpdate = 1 << 0,
/**
* Force reflash the device.
*
* This has no effect if the DFU extension is not present
*/
ForceReflash = 1 << 1
};
using EnumType = std::underlying_type<Enum>::type;
OpenFlags(Enum e = Enum(0)) : val(e) {}
EnumType operator&(Enum e) const { return EnumType(val) & EnumType(e); }
private:
const Enum val;
};
enum class OpenDirective {
Continue,
Cancel,
Skip
};
enum class OpenStatusType {
Question,
Progress
};
using OpenStatusHandler = std::function<OpenDirective(OpenStatusType type, const std::string& status, optional<double> progress)>;
bool open(OpenFlags flags = {}, OpenStatusHandler handler =
[](OpenStatusType type, const std::string& _s, optional<double> _p) { return OpenDirective::Continue; });
virtual bool close();
virtual bool isOnline() const { return online; }
virtual bool isOpen() const { return com->isOpen(); }

View File

@ -6,6 +6,7 @@
#include <memory>
#include "icsneo/communication/message/message.h"
#include "icsneo/api/eventmanager.h"
#include "icsneo/device/device.h"
namespace icsneo {
@ -18,10 +19,10 @@ public:
virtual const char* getName() const = 0;
// Return false to block opening
virtual bool onDeviceOpen() { return true; }
virtual bool onDeviceOpen(Device::OpenFlags flags, const Device::OpenStatusHandler& handler) { return true; }
// Return true to indicate that communication should now be back
virtual bool onDeviceCommunicationDead() { return false; }
virtual bool onDeviceCommunicationDead(Device::OpenFlags flags, const Device::OpenStatusHandler& handler) { return false; }
virtual void onGoOnline() {}
virtual void onGoOffline() {}