aboutsummaryrefslogtreecommitdiff
path: root/src/device/device.cpp
diff options
context:
space:
mode:
authorDusan Klinec <dusan.klinec@gmail.com>2018-08-24 00:50:31 +0200
committerDusan Klinec <dusan.klinec@gmail.com>2018-09-14 16:02:00 +0200
commitf9b22a7b01994f444185f38fe1f645593590c943 (patch)
tree2b15b82269647a2971d1d803490f057a856bb309 /src/device/device.cpp
parentMerge pull request #4324 (diff)
downloadmonero-f9b22a7b01994f444185f38fe1f645593590c943.tar.xz
hw_device: support for multiple devices added [for review]
- device name is a new wallet property - full device name is now a bit more structured so we can address particular device vendor + device path. Example: 'Ledger', 'Trezor:udp', 'Trezor:udp:127.0.0.1:21324', 'Trezor:bridge:usb01'. The part before ':' identifies HW device implementation, the optional part after ':' is device path to look for. - new --hw-device parameter added to the wallet, can name the hardware device - device reconnect added
Diffstat (limited to 'src/device/device.cpp')
-rw-r--r--src/device/device.cpp70
1 files changed, 49 insertions, 21 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);
+ }
+
}