Electroneum
hw::trezor::ProtocolV1 Class Reference

#include <transport.hpp>

Inheritance diagram for hw::trezor::ProtocolV1:
Collaboration diagram for hw::trezor::ProtocolV1:

Public Member Functions

 ProtocolV1 ()=default
 
virtual ~ProtocolV1 ()=default
 
void write (Transport &transport, const google::protobuf::Message &req) override
 
void read (Transport &transport, std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr) override
 
- Public Member Functions inherited from hw::trezor::Protocol
 Protocol ()=default
 
virtual ~Protocol ()=default
 
virtual void session_begin (Transport &transport)
 
virtual void session_end (Transport &transport)
 

Detailed Description

Definition at line 124 of file transport.hpp.

Constructor & Destructor Documentation

◆ ProtocolV1()

hw::trezor::ProtocolV1::ProtocolV1 ( )
default

◆ ~ProtocolV1()

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

Member Function Documentation

◆ read()

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

Implements hw::trezor::Protocol.

Definition at line 223 of file transport.cpp.

223  {
224  char chunk[REPLEN];
225 
226  // Initial chunk read
227  size_t nread = transport.read_chunk(chunk, REPLEN);
228  if (nread != REPLEN){
229  throw exc::CommunicationException("Read chunk has invalid size");
230  }
231 
232  if (strncmp(chunk, "?##", 3) != 0){
233  throw exc::CommunicationException("Malformed chunk");
234  }
235 
236  uint16_t tag;
237  uint32_t len;
238  nread -= 3 + 6;
239  deserialize_message_header(chunk + 3, tag, len);
240 
241  std::string data_acc(chunk + 3 + 6, nread);
242  data_acc.reserve(len);
243 
244  while(nread < len){
245  const size_t cur = transport.read_chunk(chunk, REPLEN);
246  if (chunk[0] != '?'){
247  throw exc::CommunicationException("Chunk malformed");
248  }
249 
250  data_acc.append(chunk + 1, cur - 1);
251  nread += cur - 1;
252  }
253 
254  if (msg_type){
255  *msg_type = static_cast<messages::MessageType>(tag);
256  }
257 
258  if (nread < len){
259  throw exc::CommunicationException("Response incomplete");
260  }
261 
262  std::shared_ptr<google::protobuf::Message> msg_wrap(MessageMapper::get_message(tag));
263  if (!msg_wrap->ParseFromArray(data_acc.c_str(), len)){
264  throw exc::CommunicationException("Message could not be parsed");
265  }
266 
267  msg = msg_wrap;
268  }
::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)
#define REPLEN
Definition: transport.cpp:190
std::shared_ptr< Transport > transport(const std::string &path)
Definition: transport.cpp:1204
Here is the call graph for this function:

◆ write()

void hw::trezor::ProtocolV1::write ( Transport transport,
const google::protobuf::Message &  req 
)
overridevirtual

Implements hw::trezor::Protocol.

Definition at line 192 of file transport.cpp.

192  {
193  const auto msg_size = message_size(req);
194  const auto buff_size = serialize_message_buffer_size(msg_size) + 2;
195 
196  std::unique_ptr<uint8_t[]> req_buff(new uint8_t[buff_size]);
197  uint8_t * req_buff_raw = req_buff.get();
198  req_buff_raw[0] = '#';
199  req_buff_raw[1] = '#';
200 
201  serialize_message(req, msg_size, req_buff_raw + 2, buff_size - 2);
202 
203  size_t offset = 0;
204  uint8_t chunk_buff[REPLEN];
205 
206  // Chunk by chunk upload
207  while(offset < buff_size){
208  auto to_copy = std::min((size_t)(buff_size - offset), (size_t)(REPLEN - 1));
209 
210  chunk_buff[0] = '?';
211  memcpy(chunk_buff + 1, req_buff_raw + offset, to_copy);
212 
213  // Pad with zeros
214  if (to_copy < REPLEN - 1){
215  memset(chunk_buff + 1 + to_copy, 0, REPLEN - 1 - to_copy);
216  }
217 
218  transport.write_chunk(chunk_buff, REPLEN);
219  offset += REPLEN - 1;
220  }
221  }
unsigned char uint8_t
Definition: stdint.h:124
void * memcpy(void *a, const void *b, size_t c)
#define REPLEN
Definition: transport.cpp:190
std::shared_ptr< Transport > transport(const std::string &path)
Definition: transport.cpp:1204

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