Electroneum
device.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2019, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #include "device.hpp"
31 #include "device_default.hpp"
32 #ifdef WITH_DEVICE_LEDGER
33 #include "device_ledger.hpp"
34 #endif
35 #include "misc_log_ex.h"
36 
37 
38 namespace hw {
39 
40  /* ======================================================================= */
41  /* SETUP */
42  /* ======================================================================= */
43 
44  static device_registry *get_device_registry(bool clear = false){
45  static device_registry *registry = new device_registry();
46  if (clear)
47  {
48  delete registry;
49  registry = NULL;
50  }
51  return registry;
52  }
53 
54  static void clear_device_registry(){
55  get_device_registry(true);
56  }
57 
59  hw::core::register_all(registry);
60  #ifdef WITH_DEVICE_LEDGER
61  hw::ledger::register_all(registry);
62  #endif
63  atexit(clear_device_registry);
64  }
65 
66  bool device_registry::register_device(const std::string & device_name, device * hw_device){
67  auto search = registry.find(device_name);
68  if (search != registry.end()){
69  return false;
70  }
71 
72  registry.insert(std::make_pair(device_name, std::unique_ptr<device>(hw_device)));
73  return true;
74  }
75 
76  device& device_registry::get_device(const std::string & device_descriptor){
77  // Device descriptor can contain further specs after first :
78  auto delim = device_descriptor.find(':');
79  auto device_descriptor_lookup = device_descriptor;
80  if (delim != std::string::npos) {
81  device_descriptor_lookup = device_descriptor.substr(0, delim);
82  }
83 
84  auto device = registry.find(device_descriptor_lookup);
85  if (device == registry.end()) {
86  MERROR("Device not found in registry: '" << device_descriptor << "'. Known devices: ");
87  for( const auto& sm_pair : registry ) {
88  MERROR(" - " << sm_pair.first);
89  }
90  throw std::runtime_error("device not found: " + device_descriptor);
91  }
92  return *device->second;
93  }
94 
95  device& get_device(const std::string & device_descriptor) {
96  device_registry *registry = get_device_registry();
97  return registry->get_device(device_descriptor);
98  }
99 
100  bool register_device(const std::string & device_name, device * hw_device){
101  device_registry *registry = get_device_registry();
102  return registry->register_device(device_name, hw_device);
103  }
104 
105 }
#define MERROR(x)
Definition: misc_log_ex.h:73
::std::string string
Definition: gtest-port.h:1097
void register_all(std::map< std::string, std::unique_ptr< device >> &registry)
bool register_device(const std::string &device_name, device *hw_device)
Definition: device.cpp:100
bool register_device(const std::string &device_name, device *hw_device)
Definition: device.cpp:66
device & get_device(const std::string &device_descriptor)
Definition: device.cpp:76
void register_all(std::map< std::string, std::unique_ptr< device >> &registry)
device & get_device(const std::string &device_descriptor)
Definition: device.cpp:95
Definition: device.cpp:38