hw-monitor.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 #include "hw-monitor.h"
4 #include "types.h"
5 #include <iomanip>
6 
7 namespace librealsense
8 {
9  std::string hw_monitor::get_firmware_version_string(const std::vector<uint8_t>& buff, size_t index, size_t length)
10  {
11  std::stringstream formattedBuffer;
12  std::string s = "";
13  for (auto i = 1; i <= length; i++)
14  {
15  formattedBuffer << s << static_cast<int>(buff[index + (length - i)]);
16  s = ".";
17  }
18 
19  return formattedBuffer.str();
20  }
21 
22  std::string hw_monitor::get_module_serial_string(const std::vector<uint8_t>& buff, size_t index, size_t length)
23  {
24  std::stringstream formattedBuffer;
25  for (auto i = 0; i < length; i++)
26  formattedBuffer << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(buff[index + i]);
27 
28  return formattedBuffer.str();
29  }
30 
31  void hw_monitor::fill_usb_buffer(int opCodeNumber, int p1, int p2, int p3, int p4,
32  uint8_t const * data, int dataLength, uint8_t* bufferToSend, int& length)
33  {
34  auto preHeaderData = IVCAM_MONITOR_MAGIC_NUMBER;
35 
36  uint8_t* writePtr = bufferToSend;
37  auto header_size = 4;
38 
39  auto cur_index = 2;
40  memcpy(writePtr + cur_index, &preHeaderData, sizeof(uint16_t));
41  cur_index += sizeof(uint16_t);
42  memcpy(writePtr + cur_index, &opCodeNumber, sizeof(uint32_t));
43  cur_index += sizeof(uint32_t);
44  memcpy(writePtr + cur_index, &p1, sizeof(uint32_t));
45  cur_index += sizeof(uint32_t);
46  memcpy(writePtr + cur_index, &p2, sizeof(uint32_t));
47  cur_index += sizeof(uint32_t);
48  memcpy(writePtr + cur_index, &p3, sizeof(uint32_t));
49  cur_index += sizeof(uint32_t);
50  memcpy(writePtr + cur_index, &p4, sizeof(uint32_t));
51  cur_index += sizeof(uint32_t);
52 
53  if (dataLength)
54  {
55  librealsense::copy(writePtr + cur_index, data, dataLength);
56  cur_index += dataLength;
57  }
58 
59  length = cur_index;
60  uint16_t tmp_size = length - header_size;
61  memcpy(bufferToSend, &tmp_size, sizeof(uint16_t)); // Length doesn't include header
62  }
63 
64 
65  void hw_monitor::execute_usb_command(uint8_t *out, size_t outSize, uint32_t & op, uint8_t * in, size_t & inSize) const
66  {
67  std::vector<uint8_t> out_vec(out, out + outSize);
68  auto res = _locked_transfer->send_receive(out_vec);
69 
70  // read
71  if (in && inSize)
72  {
73  if (res.size() < static_cast<int>(sizeof(uint32_t)))
74  throw invalid_value_exception("Incomplete bulk usb transfer!");
75 
77  throw invalid_value_exception("Out buffer is greater than max buffer size!");
78 
79  op = *reinterpret_cast<uint32_t *>(res.data());
80  if (res.size() > static_cast<int>(inSize))
81  throw invalid_value_exception("bulk transfer failed - user buffer too small");
82 
83  inSize = res.size();
84  librealsense::copy(in, res.data(), inSize);
85  }
86  }
87 
88  void hw_monitor::update_cmd_details(hwmon_cmd_details& details, size_t receivedCmdLen, unsigned char* outputBuffer)
89  {
90  details.receivedCommandDataLength = receivedCmdLen;
91 
92  if (details.oneDirection) return;
93 
94  if (details.receivedCommandDataLength < 4)
95  throw invalid_value_exception("received incomplete response to usb command");
96 
97  details.receivedCommandDataLength -= 4;
98  librealsense::copy(details.receivedOpcode.data(), outputBuffer, 4);
99 
100  if (details.receivedCommandDataLength > 0)
101  librealsense::copy(details.receivedCommandData.data(), outputBuffer + 4, details.receivedCommandDataLength);
102  }
103 
105  {
106  unsigned char outputBuffer[HW_MONITOR_BUFFER_SIZE];
107 
108  uint32_t op{};
109  size_t receivedCmdLen = HW_MONITOR_BUFFER_SIZE;
110 
111  execute_usb_command(details.sendCommandData.data(), details.sizeOfSendCommandData, op, outputBuffer, receivedCmdLen);
112  update_cmd_details(details, receivedCmdLen, outputBuffer);
113  }
114 
115  std::vector< uint8_t > hw_monitor::send( std::vector< uint8_t > const & data ) const
116  {
117  return _locked_transfer->send_receive(data);
118  }
119 
120  std::vector< uint8_t >
122  {
123  hwmon_cmd newCommand(cmd);
124  auto opCodeXmit = static_cast<uint32_t>(newCommand.cmd);
125 
126  hwmon_cmd_details details;
127  details.oneDirection = newCommand.oneDirection;
128  details.timeOut = newCommand.timeOut;
129 
130  fill_usb_buffer(opCodeXmit,
131  newCommand.param1,
132  newCommand.param2,
133  newCommand.param3,
134  newCommand.param4,
135  newCommand.data,
136  newCommand.sizeOfSendCommandData,
137  details.sendCommandData.data(),
138  details.sizeOfSendCommandData);
139 
140  if (locked_transfer)
141  {
142  return _locked_transfer->send_receive({ details.sendCommandData.begin(),details.sendCommandData.end()});
143  }
144 
145  send_hw_monitor_command(details);
146 
147  // Error/exit conditions
148  if( p_response )
149  *p_response = hwm_Success;
150  if( newCommand.oneDirection )
151  return std::vector<uint8_t>();
152 
153  librealsense::copy(newCommand.receivedOpcode, details.receivedOpcode.data(), 4);
156 
157  // endian?
158  auto opCodeAsUint32 = pack(details.receivedOpcode[3], details.receivedOpcode[2],
159  details.receivedOpcode[1], details.receivedOpcode[0]);
160  if (opCodeAsUint32 != opCodeXmit)
161  {
162  auto err_type = static_cast<hwmon_response>(opCodeAsUint32);
163  std::string err = hwmon_error_string( cmd, err_type );
164  LOG_DEBUG( err );
165  if( p_response )
166  {
167  *p_response = err_type;
168  return std::vector<uint8_t>();
169  }
170  throw invalid_value_exception( err );
171  }
172 
173  return std::vector<uint8_t>(newCommand.receivedCommandData,
174  newCommand.receivedCommandData + newCommand.receivedCommandDataLength);
175  }
176 
178  {
179  auto str = hwmon_error2str( e );
180  to_string err;
181  err << "hwmon command 0x" << std::hex << unsigned(cmd.cmd) << '(';
182  err << ' ' << cmd.param1;
183  err << ' ' << cmd.param2;
184  err << ' ' << cmd.param3;
185  err << ' ' << cmd.param4 << std::dec;
186  err << " ) failed (response " << e << "= " << ( str.empty() ? "unknown" : str ) << ")";
187  return err;
188  }
189 
190 
191  void hw_monitor::get_gvd(size_t sz, unsigned char* gvd, uint8_t gvd_cmd) const
192  {
193  command command(gvd_cmd);
194  auto data = send(command);
195  auto minSize = std::min(sz, data.size());
196  librealsense::copy(gvd, data.data(), minSize);
197  }
198 
200  {
201  std::vector<unsigned char> gvd(HW_MONITOR_BUFFER_SIZE);
202  get_gvd(gvd.size(), gvd.data(), gvd_cmd);
203  bool value;
204  librealsense::copy(&value, gvd.data() + offset, 1);
205  return value;
206  }
207 }
void send_hw_monitor_command(hwmon_cmd_details &details) const
Definition: hw-monitor.cpp:104
std::string hwmon_error2str(hwmon_response e)
Definition: hw-monitor.h:188
GLdouble s
uint8_t receivedCommandData[HW_MONITOR_BUFFER_SIZE]
Definition: hw-monitor.h:270
GLfloat value
unsigned short uint16_t
Definition: stdint.h:79
GLsizei const GLchar *const * string
std::shared_ptr< locked_transfer > _locked_transfer
Definition: hw-monitor.h:317
std::array< uint8_t, HW_MONITOR_BUFFER_SIZE > receivedCommandData
Definition: hw-monitor.h:309
unsigned char uint8_t
Definition: stdint.h:78
e
Definition: rmse.py:177
static void fill_usb_buffer(int opCodeNumber, int p1, int p2, int p3, int p4, uint8_t const *data, int dataLength, uint8_t *bufferToSend, int &length)
Definition: hw-monitor.cpp:31
GLuint index
static void update_cmd_details(hwmon_cmd_details &details, size_t receivedCmdLen, unsigned char *outputBuffer)
Definition: hw-monitor.cpp:88
const uint16_t IVCAM_MONITOR_MAGIC_NUMBER
Definition: hw-monitor.h:37
void execute_usb_command(uint8_t *out, size_t outSize, uint32_t &op, uint8_t *in, size_t &inSize) const
Definition: hw-monitor.cpp:65
uint8_t data[HW_MONITOR_BUFFER_SIZE]
Definition: hw-monitor.h:266
static std::string get_firmware_version_string(const std::vector< uint8_t > &buff, size_t index, size_t length=4)
Definition: hw-monitor.cpp:9
std::vector< uint8_t > send(std::vector< uint8_t > const &data) const
Definition: hw-monitor.cpp:115
unsigned int uint32_t
Definition: stdint.h:80
static std::string get_module_serial_string(const std::vector< uint8_t > &buff, size_t index, size_t length=6)
Definition: hw-monitor.cpp:22
std::string hwmon_error_string(command const &cmd, hwmon_response e)
Definition: hw-monitor.cpp:177
std::array< uint8_t, 4 > receivedOpcode
Definition: hw-monitor.h:308
void get_gvd(size_t sz, unsigned char *gvd, uint8_t gvd_cmd) const
Definition: hw-monitor.cpp:191
GLuint in
Definition: glext.h:8859
int min(int a, int b)
Definition: lz4s.c:73
bool is_camera_locked(uint8_t gvd_cmd, uint32_t offset) const
Definition: hw-monitor.cpp:199
int i
GLenum GLuint GLenum GLsizei length
GLuint res
Definition: glext.h:8856
#define LOG_DEBUG(...)
Definition: src/types.h:239
std::array< uint8_t, HW_MONITOR_BUFFER_SIZE > sendCommandData
Definition: hw-monitor.h:305
const uint16_t HW_MONITOR_BUFFER_SIZE
Definition: hw-monitor.h:41
uint32_t pack(uint8_t c0, uint8_t c1, uint8_t c2, uint8_t c3)
Definition: src/types.h:1157
const uint16_t IVCAM_MONITOR_MAX_BUFFER_SIZE
Definition: hw-monitor.h:38
Definition: parser.hpp:150
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836
GLintptr offset


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16