mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Add error system
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include "icsneo/api/errormanager.h"
|
||||
#include "icsneo/device/neodevice.h"
|
||||
#include "icsneo/device/idevicesettings.h"
|
||||
#include "icsneo/device/nullsettings.h"
|
||||
@@ -71,6 +72,7 @@ protected:
|
||||
int messagePollingCallbackID = 0;
|
||||
int internalHandlerCallbackID = 0;
|
||||
std::shared_ptr<Communication> com;
|
||||
device_errorhandler_t err;
|
||||
|
||||
// START Initialization Functions
|
||||
Device(neodevice_t neodevice = { 0 }) {
|
||||
@@ -79,26 +81,44 @@ protected:
|
||||
}
|
||||
|
||||
template<typename Transport, typename Settings = NullSettings>
|
||||
void initialize();
|
||||
void initialize() {
|
||||
err = makeErrorHandler();
|
||||
auto transport = makeTransport<Transport>();
|
||||
setupTransport(transport.get());
|
||||
auto packetizer = makePacketizer();
|
||||
setupPacketizer(packetizer.get());
|
||||
auto encoder = makeEncoder(packetizer);
|
||||
setupEncoder(encoder.get());
|
||||
auto decoder = makeDecoder();
|
||||
setupDecoder(decoder.get());
|
||||
com = makeCommunication(std::move(transport), packetizer, std::move(encoder), std::move(decoder));
|
||||
setupCommunication(com.get());
|
||||
settings = makeSettings<Settings>(com);
|
||||
setupSettings(settings.get());
|
||||
}
|
||||
|
||||
virtual device_errorhandler_t makeErrorHandler() {
|
||||
return [this](APIError::ErrorType type) { ErrorManager::GetInstance().add(type, this); };
|
||||
}
|
||||
|
||||
template<typename Transport>
|
||||
std::unique_ptr<ICommunication> makeTransport() { return std::unique_ptr<ICommunication>(new Transport(getWritableNeoDevice())); }
|
||||
virtual void setupTransport(ICommunication* transport) {}
|
||||
|
||||
virtual std::shared_ptr<Packetizer> makePacketizer() { return std::make_shared<Packetizer>(); }
|
||||
virtual std::shared_ptr<Packetizer> makePacketizer() { return std::make_shared<Packetizer>(err); }
|
||||
virtual void setupPacketizer(Packetizer* packetizer) {}
|
||||
|
||||
virtual std::unique_ptr<Encoder> makeEncoder(std::shared_ptr<Packetizer> p) { return std::unique_ptr<Encoder>(new Encoder(p)); }
|
||||
virtual std::unique_ptr<Encoder> makeEncoder(std::shared_ptr<Packetizer> p) { return std::unique_ptr<Encoder>(new Encoder(err, p)); }
|
||||
virtual void setupEncoder(Encoder* encoder) {}
|
||||
|
||||
virtual std::unique_ptr<Decoder> makeDecoder() { return std::unique_ptr<Decoder>(new Decoder()); }
|
||||
virtual std::unique_ptr<Decoder> makeDecoder() { return std::unique_ptr<Decoder>(new Decoder(err)); }
|
||||
virtual void setupDecoder(Decoder* decoder) {}
|
||||
|
||||
virtual std::shared_ptr<Communication> makeCommunication(
|
||||
std::unique_ptr<ICommunication> t,
|
||||
std::shared_ptr<Packetizer> p,
|
||||
std::unique_ptr<Encoder> e,
|
||||
std::unique_ptr<Decoder> d) { return std::make_shared<Communication>(std::move(t), p, std::move(e), std::move(d)); }
|
||||
std::unique_ptr<Decoder> d) { return std::make_shared<Communication>(err, std::move(t), p, std::move(e), std::move(d)); }
|
||||
virtual void setupCommunication(Communication* com) {}
|
||||
|
||||
template<typename Settings>
|
||||
|
||||
@@ -283,11 +283,7 @@ public:
|
||||
static constexpr uint16_t GS_VERSION = 5;
|
||||
static uint16_t CalculateGSChecksum(const std::vector<uint8_t>& settings);
|
||||
|
||||
// Parameter createInoperableSettings exists because it is serving as a warning that you probably don't want to do this
|
||||
typedef void* warn_t;
|
||||
IDeviceSettings(warn_t createInoperableSettings) : disabled(true), readonly(true), structSize(0) { (void)createInoperableSettings; }
|
||||
|
||||
IDeviceSettings(std::shared_ptr<Communication> com, size_t size) : com(com), structSize(size) {}
|
||||
IDeviceSettings(std::shared_ptr<Communication> com, size_t size) : com(com), err(com->err), structSize(size) {}
|
||||
virtual ~IDeviceSettings() {}
|
||||
bool ok() { return !disabled && settingsLoaded; }
|
||||
|
||||
@@ -313,9 +309,15 @@ public:
|
||||
bool readonly = false;
|
||||
protected:
|
||||
std::shared_ptr<Communication> com;
|
||||
device_errorhandler_t err;
|
||||
size_t structSize;
|
||||
bool settingsLoaded = false;
|
||||
std::vector<uint8_t> settings;
|
||||
|
||||
// Parameter createInoperableSettings exists because it is serving as a warning that you probably don't want to do this
|
||||
typedef void* warn_t;
|
||||
IDeviceSettings(warn_t createInoperableSettings, std::shared_ptr<Communication> com)
|
||||
: disabled(true), readonly(true), err(com->err), structSize(0) { (void)createInoperableSettings; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ namespace icsneo {
|
||||
|
||||
class NullSettings : public IDeviceSettings {
|
||||
public:
|
||||
// Calls the base constructor with "createInoperableSettings"
|
||||
NullSettings(std::shared_ptr<Communication> com = std::shared_ptr<Communication>()) : IDeviceSettings(nullptr) { (void)com; }
|
||||
// Calls the protected base constructor with "createInoperableSettings"
|
||||
NullSettings(std::shared_ptr<Communication> com) : IDeviceSettings(nullptr, com) {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ protected:
|
||||
std::shared_ptr<Packetizer> packetizer,
|
||||
std::unique_ptr<Encoder> encoder,
|
||||
std::unique_ptr<Decoder> decoder
|
||||
) override { return std::make_shared<MultiChannelCommunication>(std::move(transport), packetizer, std::move(encoder), std::move(decoder)); }
|
||||
) override { return std::make_shared<MultiChannelCommunication>(err, std::move(transport), packetizer, std::move(encoder), std::move(decoder)); }
|
||||
|
||||
public:
|
||||
Plasion(neodevice_t neodevice) : Device(neodevice) {}
|
||||
|
||||
Reference in New Issue
Block a user