aboutsummaryrefslogtreecommitdiff
path: root/src/device
diff options
context:
space:
mode:
Diffstat (limited to 'src/device')
-rw-r--r--src/device/device.cpp70
-rw-r--r--src/device/device.hpp24
-rw-r--r--src/device/device_default.hpp2
-rw-r--r--src/device/device_ledger.cpp2
-rw-r--r--src/device/device_ledger.hpp4
5 files changed, 76 insertions, 26 deletions
diff --git a/src/device/device.cpp b/src/device/device.cpp
index 983f59b60..8a8b40061 100644
--- a/src/device/device.cpp
+++ b/src/device/device.cpp
@@ -39,32 +39,60 @@ namespace hw {
/* ======================================================================= */
/* SETUP */
- /* ======================================================================= */
- device& get_device(const std::string device_descriptor) {
-
- struct s_devices {
- std::map<std::string, std::unique_ptr<device>> registry;
- s_devices() : registry() {
- hw::core::register_all(registry);
- #ifdef HAVE_PCSC
- hw::ledger::register_all(registry);
- #endif
- };
- };
-
- static const s_devices devices;
+ /* ======================================================================= */
+
+ static std::unique_ptr<device_registry> registry;
+
+ device_registry::device_registry(){
+ hw::core::register_all(registry);
+ #ifdef HAVE_PCSC
+ hw::ledger::register_all(registry);
+ #endif
+ }
+
+ bool device_registry::register_device(const std::string & device_name, device * hw_device){
+ auto search = registry.find(device_name);
+ if (search != registry.end()){
+ return false;
+ }
+
+ registry.insert(std::make_pair(device_name, std::unique_ptr<device>(hw_device)));
+ return true;
+ }
+
+ device& device_registry::get_device(const std::string & device_descriptor){
+ // Device descriptor can contain further specs after first :
+ auto delim = device_descriptor.find(':');
+ auto device_descriptor_lookup = device_descriptor;
+ if (delim != std::string::npos) {
+ device_descriptor_lookup = device_descriptor.substr(0, delim);
+ }
- auto device = devices.registry.find(device_descriptor);
- if (device == devices.registry.end()) {
- MERROR("device not found in registry: '" << device_descriptor << "'\n" <<
- "known devices:");
-
- for( const auto& sm_pair : devices.registry ) {
+ auto device = registry.find(device_descriptor_lookup);
+ if (device == registry.end()) {
+ MERROR("Device not found in registry: '" << device_descriptor << "'. Known devices: ");
+ for( const auto& sm_pair : registry ) {
MERROR(" - " << sm_pair.first);
}
- throw std::runtime_error("device not found: "+ device_descriptor);
+ throw std::runtime_error("device not found: " + device_descriptor);
}
return *device->second;
}
+ device& get_device(const std::string & device_descriptor) {
+ if (!registry){
+ registry.reset(new device_registry());
+ }
+
+ return registry->get_device(device_descriptor);
+ }
+
+ bool register_device(const std::string & device_name, device * hw_device){
+ if (!registry){
+ registry.reset(new device_registry());
+ }
+
+ return registry->register_device(device_name, hw_device);
+ }
+
}
diff --git a/src/device/device.hpp b/src/device/device.hpp
index c21456daf..87f1430f4 100644
--- a/src/device/device.hpp
+++ b/src/device/device.hpp
@@ -78,7 +78,6 @@ namespace hw {
return false;
}
-
class device {
protected:
std::string name;
@@ -96,6 +95,12 @@ namespace hw {
TRANSACTION_CREATE_FAKE,
TRANSACTION_PARSE
};
+ enum device_type
+ {
+ SOFTWARE = 0,
+ LEDGER = 1
+ };
+
/* ======================================================================= */
/* SETUP/TEARDOWN */
@@ -109,7 +114,9 @@ namespace hw {
virtual bool connect(void) = 0;
virtual bool disconnect(void) = 0;
- virtual bool set_mode(device_mode mode) = 0;
+ virtual bool set_mode(device_mode mode) = 0;
+
+ virtual device_type get_type() const = 0;
/* ======================================================================= */
@@ -202,6 +209,17 @@ namespace hw {
~reset_mode() { hwref.set_mode(hw::device::NONE);}
};
- device& get_device(const std::string device_descriptor) ;
+ class device_registry {
+ private:
+ std::map<std::string, std::unique_ptr<device>> registry;
+
+ public:
+ device_registry();
+ bool register_device(const std::string & device_name, device * hw_device);
+ device& get_device(const std::string & device_descriptor);
+ };
+
+ device& get_device(const std::string & device_descriptor);
+ bool register_device(const std::string & device_name, device * hw_device);
}
diff --git a/src/device/device_default.hpp b/src/device/device_default.hpp
index 8d841d9de..b697e1775 100644
--- a/src/device/device_default.hpp
+++ b/src/device/device_default.hpp
@@ -61,6 +61,8 @@ namespace hw {
bool set_mode(device_mode mode) override;
+ device_type get_type() const {return device_type::SOFTWARE;};
+
/* ======================================================================= */
/* LOCKER */
/* ======================================================================= */
diff --git a/src/device/device_ledger.cpp b/src/device/device_ledger.cpp
index 658b379e4..9b5ea0fd7 100644
--- a/src/device/device_ledger.cpp
+++ b/src/device/device_ledger.cpp
@@ -1354,7 +1354,7 @@ namespace hw {
this->exchange();
//pseudoOuts
- if ((type == rct::RCTTypeSimple) || (type == rct::RCTTypeBulletproof)) {
+ if (type == rct::RCTTypeSimple) {
for ( i = 0; i < inputs_size; i++) {
offset = set_command_header(INS_VALIDATE, 0x01, i+2);
//options
diff --git a/src/device/device_ledger.hpp b/src/device/device_ledger.hpp
index e6c6e5b52..4a3625b2b 100644
--- a/src/device/device_ledger.hpp
+++ b/src/device/device_ledger.hpp
@@ -142,7 +142,9 @@ namespace hw {
bool connect(void) override;
bool disconnect() override;
- bool set_mode(device_mode mode) override;
+ bool set_mode(device_mode mode) override;
+
+ device_type get_type() const {return device_type::LEDGER;};
/* ======================================================================= */
/* LOCKER */