Electroneum
hw::trezor::BridgeTransport Class Reference

#include <transport.hpp>

Inheritance diagram for hw::trezor::BridgeTransport:
Collaboration diagram for hw::trezor::BridgeTransport:

Public Member Functions

 BridgeTransport (boost::optional< std::string > device_path=boost::none, boost::optional< std::string > bridge_host=boost::none)
 
virtual ~BridgeTransport ()=default
 
std::string get_path () const override
 
void enumerate (t_transport_vect &res) override
 
void open () override
 
void close () override
 
void write (const google::protobuf::Message &req) override
 
void read (std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr) override
 
const boost::optional< json > & device_info () const
 
std::ostream & dump (std::ostream &o) const override
 
- Public Member Functions inherited from hw::trezor::Transport
 Transport ()
 
virtual ~Transport ()=default
 
virtual bool ping ()
 
virtual std::shared_ptr< Transportfind_debug ()
 
virtual void write_chunk (const void *buff, size_t size)
 
virtual size_t read_chunk (void *buff, size_t size)
 

Static Public Attributes

static const char * PATH_PREFIX = "bridge:"
 

Additional Inherited Members

- Protected Member Functions inherited from hw::trezor::Transport
virtual bool pre_open ()
 
virtual bool pre_close ()
 
- Protected Attributes inherited from hw::trezor::Transport
long m_open_counter
 

Detailed Description

Definition at line 162 of file transport.hpp.

Constructor & Destructor Documentation

◆ BridgeTransport()

hw::trezor::BridgeTransport::BridgeTransport ( boost::optional< std::string >  device_path = boost::none,
boost::optional< std::string >  bridge_host = boost::none 
)

Definition at line 315 of file transport.cpp.

317  :
318  m_device_path(device_path),
319  m_bridge_host(bridge_host ? bridge_host.get() : DEFAULT_BRIDGE),
320  m_response(boost::none),
321  m_session(boost::none),
322  m_device_info(boost::none)
323  {
324  const char *env_bridge_port = nullptr;
325  if (!bridge_host && (env_bridge_port = getenv("TREZOR_BRIDGE_PORT")) != nullptr)
326  {
327  uint16_t bridge_port;
328  CHECK_AND_ASSERT_THROW_MES(epee::string_tools::get_xtype_from_string(bridge_port, env_bridge_port), "Invalid bridge port: " << env_bridge_port);
329  assert_port_number(bridge_port);
330 
331  m_bridge_host = std::string("127.0.0.1:") + boost::lexical_cast<std::string>(env_bridge_port);
332  MDEBUG("Bridge host: " << m_bridge_host);
333  }
334 
335  m_http_client.set_server(m_bridge_host, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled);
336  }
const std::string DEFAULT_BRIDGE
Definition: transport.hpp:63
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
bool set_server(const std::string &address, boost::optional< login > user, ssl_options_t ssl_options=ssl_support_t::e_ssl_support_autodetect)
Definition: http_client.h:302
::std::string string
Definition: gtest-port.h:1097
unsigned short uint16_t
Definition: stdint.h:125
#define MDEBUG(x)
Definition: misc_log_ex.h:76
PUSH_WARNINGS bool get_xtype_from_string(OUT XType &val, const std::string &str_id)
Definition: string_tools.h:125
Here is the call graph for this function:

◆ ~BridgeTransport()

virtual hw::trezor::BridgeTransport::~BridgeTransport ( )
virtualdefault

Member Function Documentation

◆ close()

void hw::trezor::BridgeTransport::close ( )
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 404 of file transport.cpp.

404  {
405  if (!pre_close()){
406  return;
407  }
408 
409  MTRACE("Closing Trezor:BridgeTransport");
410  if (!m_device_path || !m_session){
411  throw exc::CommunicationException("Device not open");
412  }
413 
414  std::string uri = "/release/" + m_session.get();
415  std::string req;
416  json bridge_res;
417  bool req_status = invoke_bridge_http(uri, req, bridge_res, m_http_client);
418  if (!req_status){
419  throw exc::CommunicationException("Failed to release device");
420  }
421 
422  m_session = boost::none;
423  }
#define MTRACE(x)
Definition: misc_log_ex.h:77
::std::string string
Definition: gtest-port.h:1097
bool invoke_bridge_http(const boost::string_ref uri, const t_req &out_struct, t_res &result_struct, t_transport &transport, const boost::string_ref method="POST", std::chrono::milliseconds timeout=std::chrono::seconds(180))
Definition: transport.hpp:77
virtual bool pre_close()
Definition: transport.cpp:295
rapidjson::Document json
Definition: transport.cpp:49
Here is the call graph for this function:

◆ device_info()

const boost::optional< json > & hw::trezor::BridgeTransport::device_info ( ) const

Definition at line 476 of file transport.cpp.

476  {
477  return m_device_info;
478  }

◆ dump()

std::ostream & hw::trezor::BridgeTransport::dump ( std::ostream &  o) const
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 480 of file transport.cpp.

480  {
481  return o << "BridgeTransport<path=" << (m_device_path ? get_path() : "None")
482  << ", info=" << (m_device_info ? t_serialize(m_device_info.get()) : "None")
483  << ", session=" << (m_session ? m_session.get() : "None")
484  << ">";
485  }
bool t_serialize(const std::string &in, std::string &out)
Definition: transport.cpp:55
void get(std::istream &input, bool &res)
Definition: io.h:62
std::string get_path() const override
Definition: transport.cpp:338
Here is the call graph for this function:

◆ enumerate()

void hw::trezor::BridgeTransport::enumerate ( t_transport_vect res)
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 347 of file transport.cpp.

347  {
348  json bridge_res;
349  std::string req;
350 
351  bool req_status = invoke_bridge_http("/enumerate", req, bridge_res, m_http_client);
352  if (!req_status){
353  throw exc::CommunicationException("Bridge enumeration failed");
354  }
355 
356  for(rapidjson::Value::ConstValueIterator itr = bridge_res.Begin(); itr != bridge_res.End(); ++itr){
357  auto element = itr->GetObject();
358  auto t = std::make_shared<BridgeTransport>(boost::make_optional(json_get_string(element["path"])));
359 
360  auto itr_vendor = element.FindMember("vendor");
361  auto itr_product = element.FindMember("product");
362  if (itr_vendor != element.MemberEnd() && itr_product != element.MemberEnd()
363  && itr_vendor->value.IsNumber() && itr_product->value.IsNumber()){
364  try {
365  const auto id_vendor = (uint16_t) itr_vendor->value.GetUint64();
366  const auto id_product = (uint16_t) itr_product->value.GetUint64();
367  const auto device_idx = get_device_idx(id_vendor, id_product);
368  if (!is_device_supported(device_idx)){
369  MDEBUG("Device with idx " << device_idx << " is not supported. Vendor: " << id_vendor << ", product: " << id_product);
370  continue;
371  }
372  } catch(const std::exception &e){
373  MERROR("Could not detect vendor & product: " << e.what());
374  }
375  }
376 
377  t->m_device_info.emplace();
378  t->m_device_info->CopyFrom(*itr, t->m_device_info->GetAllocator());
379  res.push_back(t);
380  }
381  }
const char * res
Definition: hmac_keccak.cpp:41
#define MERROR(x)
Definition: misc_log_ex.h:73
::std::string string
Definition: gtest-port.h:1097
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:57
unsigned short uint16_t
Definition: stdint.h:125
#define MDEBUG(x)
Definition: misc_log_ex.h:76
bool invoke_bridge_http(const boost::string_ref uri, const t_req &out_struct, t_res &result_struct, t_transport &transport, const boost::string_ref method="POST", std::chrono::milliseconds timeout=std::chrono::seconds(180))
Definition: transport.hpp:77
rapidjson::Document json
Definition: transport.cpp:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_path()

std::string hw::trezor::BridgeTransport::get_path ( ) const
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 338 of file transport.cpp.

338  {
339  if (!m_device_path){
340  return "";
341  }
342 
343  std::string path(PATH_PREFIX);
344  return path + m_device_path.get();
345  }
::std::string string
Definition: gtest-port.h:1097
static const char * PATH_PREFIX
Definition: transport.hpp:170
Here is the caller graph for this function:

◆ open()

void hw::trezor::BridgeTransport::open ( )
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 383 of file transport.cpp.

383  {
384  if (!pre_open()){
385  return;
386  }
387 
388  if (!m_device_path){
389  throw exc::CommunicationException("Coud not open, empty device path");
390  }
391 
392  std::string uri = "/acquire/" + m_device_path.get() + "/null";
393  std::string req;
394  json bridge_res;
395  bool req_status = invoke_bridge_http(uri, req, bridge_res, m_http_client);
396  if (!req_status){
397  throw exc::CommunicationException("Failed to acquire device");
398  }
399 
400  m_session = boost::make_optional(json_get_string(bridge_res["session"]));
401  m_open_counter = 1;
402  }
virtual bool pre_open()
Definition: transport.cpp:279
::std::string string
Definition: gtest-port.h:1097
bool invoke_bridge_http(const boost::string_ref uri, const t_req &out_struct, t_res &result_struct, t_transport &transport, const boost::string_ref method="POST", std::chrono::milliseconds timeout=std::chrono::seconds(180))
Definition: transport.hpp:77
rapidjson::Document json
Definition: transport.cpp:49
Here is the call graph for this function:

◆ read()

void hw::trezor::BridgeTransport::read ( std::shared_ptr< google::protobuf::Message > &  msg,
messages::MessageType *  msg_type = nullptr 
)
overridevirtual

Implements hw::trezor::Transport.

Definition at line 448 of file transport.cpp.

448  {
449  if (!m_response){
450  throw exc::CommunicationException("Could not read, no response stored");
451  }
452 
453  std::string bin_data;
454  if (!epee::string_tools::parse_hexstr_to_binbuff(m_response.get(), bin_data)){
455  throw exc::CommunicationException("Response is not well hexcoded");
456  }
457 
458  uint16_t msg_tag;
459  uint32_t msg_len;
460  deserialize_message_header(bin_data.c_str(), msg_tag, msg_len);
461  if (bin_data.size() != msg_len + 6){
462  throw exc::CommunicationException("Response is not well hexcoded");
463  }
464 
465  if (msg_type){
466  *msg_type = static_cast<messages::MessageType>(msg_tag);
467  }
468 
469  std::shared_ptr<google::protobuf::Message> msg_wrap(MessageMapper::get_message(msg_tag));
470  if (!msg_wrap->ParseFromArray(bin_data.c_str() + 6, msg_len)){
471  throw exc::EncodingException("Response is not well hexcoded");
472  }
473  msg = msg_wrap;
474  }
::std::string string
Definition: gtest-port.h:1097
unsigned short uint16_t
Definition: stdint.h:125
unsigned int uint32_t
Definition: stdint.h:126
::google::protobuf::Message * get_message(int wire_number)
bool parse_hexstr_to_binbuff(const epee::span< const char > s, epee::span< char > &res)
Definition: string_tools.h:92
Here is the call graph for this function:

◆ write()

void hw::trezor::BridgeTransport::write ( const google::protobuf::Message &  req)
overridevirtual

Implements hw::trezor::Transport.

Definition at line 425 of file transport.cpp.

425  {
426  m_response = boost::none;
427 
428  const auto msg_size = message_size(req);
429  const auto buff_size = serialize_message_buffer_size(msg_size);
430 
431  std::unique_ptr<uint8_t[]> req_buff(new uint8_t[buff_size]);
432  uint8_t * req_buff_raw = req_buff.get();
433 
434  serialize_message(req, msg_size, req_buff_raw, buff_size);
435 
436  std::string uri = "/call/" + m_session.get();
437  std::string req_hex = epee::to_hex::string(epee::span<const std::uint8_t>(req_buff_raw, buff_size));
438  std::string res_hex;
439 
440  bool req_status = invoke_bridge_http(uri, req_hex, res_hex, m_http_client);
441  if (!req_status){
442  throw exc::CommunicationException("Call method failed");
443  }
444 
445  m_response = res_hex;
446  }
::std::string string
Definition: gtest-port.h:1097
unsigned char uint8_t
Definition: stdint.h:124
bool invoke_bridge_http(const boost::string_ref uri, const t_req &out_struct, t_res &result_struct, t_transport &transport, const boost::string_ref method="POST", std::chrono::milliseconds timeout=std::chrono::seconds(180))
Definition: transport.hpp:77
static std::string string(const span< const std::uint8_t > src)
Definition: hex.cpp:68
Here is the call graph for this function:

Member Data Documentation

◆ PATH_PREFIX

const char * hw::trezor::BridgeTransport::PATH_PREFIX = "bridge:"
static

Definition at line 170 of file transport.hpp.


The documentation for this class was generated from the following files: