example_utils.cpp
Go to the documentation of this file.
1 #include <vector>
2 #include <stdexcept>
3 
4 #include <stdarg.h>
5 
6 #include <mip/mip_logging.h>
7 
8 #include "example_utils.hpp"
9 
10 
11 #ifdef WIN32
12  #define PORT_KEY "COM"
13 #else
14  #define PORT_KEY "/dev/"
15 #endif
16 
17 mip::Timestamp getCurrentTimestamp()
18 {
19  using namespace std::chrono;
20  return duration_cast<milliseconds>( steady_clock::now().time_since_epoch() ).count();
21 }
22 
23 void customLog(void* user, mip_log_level level, const char* fmt, va_list args)
24 {
25  // Convert the varargs into a string
26  std::string log;
27  va_list args_copy;
28  va_copy(args_copy, args);
29  const int required_len = vsnprintf(nullptr, 0, fmt, args_copy);
30  if (required_len >= 0)
31  {
32  log.resize(required_len);
33  vsnprintf(&log[0], required_len + 1, fmt, args);
34  }
35  va_end(args_copy);
36 
37  // Print to the proper stream
38  switch (level)
39  {
42  std::cerr << log;
43  break;
44  default:
45  std::cout << log;
46  break;
47  }
48 }
49 
50 std::unique_ptr<ExampleUtils> openFromArgs(const std::string& port_or_hostname, const std::string& baud_or_port, const std::string& binary_file_path)
51 {
52  auto example_utils = std::unique_ptr<ExampleUtils>(new ExampleUtils());
53 
54  if( !binary_file_path.empty() )
55  {
56 #ifdef MIP_USE_EXTRAS
57  example_utils->recordedFile = std::unique_ptr<std::ofstream>(new std::ofstream(binary_file_path));
58  if( !example_utils->recordedFile->is_open() )
59  throw std::runtime_error("Unable to open binary file");
60 #else // MIP_USE_EXTRAS
61  throw std::runtime_error("The program was compiled without binary file recording support. Recompile with -DMIP_USE_EXTRAS=ON");
62 #endif // MIP_USE_EXTRAS
63  }
64 
65  if(port_or_hostname.find(PORT_KEY) == std::string::npos) // Not a serial port
66  {
67 
68 #ifdef MIP_USE_TCP
69  uint32_t port = std::strtoul(baud_or_port.c_str(), nullptr, 10);
70  if( port < 1024 || port > 65535 )
71  throw std::runtime_error("Invalid TCP port (must be between 1024 and 65535.");
72 
73 #ifdef MIP_USE_EXTRAS
74  using RecordingTcpConnection = mip::extras::RecordingConnectionWrapper<mip::platform::TcpConnection>;
75  example_utils->connection = std::unique_ptr<RecordingTcpConnection>(new RecordingTcpConnection(example_utils->recordedFile.get(), example_utils->recordedFile.get(), port_or_hostname, port));
76 #else // MIP_USE_EXTRAS
77  using TcpConnection = mip::platform::TcpConnection;
78  example_utils->connection = std::unique_ptr<TcpConnection>(new TcpConnection(port_or_hostname, port));
79 #endif // MIP_USE_EXTRAS
80 
81  example_utils->device = std::unique_ptr<mip::DeviceInterface>(new mip::DeviceInterface(example_utils->connection.get(), example_utils->buffer, sizeof(example_utils->buffer), 1000, 2000));
82 #else // MIP_USE_TCP
83  throw std::runtime_error("This program was compiled without socket support. Recompile with -DMIP_USE_TCP=1");
84 #endif // MIP_USE_TCP
85 
86  }
87  else // Serial port
88  {
89 
90 #ifdef MIP_USE_SERIAL
91  uint32_t baud = std::strtoul(baud_or_port.c_str(), nullptr, 10);
92  if( baud == 0 )
93  throw std::runtime_error("Serial baud rate must be a decimal integer greater than 0.");
94 
95 #ifdef MIP_USE_EXTRAS
96  using RecordingSerialConnection = mip::extras::RecordingConnectionWrapper<mip::platform::SerialConnection>;
97  example_utils->connection = std::unique_ptr<RecordingSerialConnection>(new RecordingSerialConnection(example_utils->recordedFile.get(), example_utils->recordedFile.get(), port_or_hostname, baud));
98 #else // MIP_USE_EXTRAS
99  using SerialConnection = mip::platform::SerialConnection;
100  example_utils->connection = std::unique_ptr<SerialConnection>(new SerialConnection(port_or_hostname, baud));
101 #endif // MIP_USE_EXTRAS
102 
103  example_utils->device = std::unique_ptr<mip::DeviceInterface>(new mip::DeviceInterface(example_utils->connection.get(), example_utils->buffer, sizeof(example_utils->buffer), mip::C::mip_timeout_from_baudrate(baud), 500));
104 #else // MIP_USE_SERIAL
105  throw std::runtime_error("This program was compiled without serial support. Recompile with -DMIP_USE_SERIAL=1.\n");
106 #endif //MIP_USE_SERIAL
107  }
108 
109  if( !example_utils->connection->connect() )
110  throw std::runtime_error("Failed to open the connection");
111 
112  return example_utils;
113 }
114 
115 std::unique_ptr<ExampleUtils> handleCommonArgs(int argc, const char* argv[], int maxArgs)
116 {
117  // Setup the logger for the MIP SDK
119 
120  if( argc < 3 || argc > maxArgs )
121  {
122  throw std::underflow_error("Usage error");
123  }
124 
125  // If we were passed a file name, record the data in that file
126  std::string binary_file_path = "";
127  if (argc >= 4)
128  binary_file_path = argv[3];
129 
130  return openFromArgs(argv[1], argv[2], binary_file_path);
131 }
132 
133 int printCommonUsage(const char* argv[])
134 {
135  fprintf(stderr, "Usage: %s <portname> <baudrate> <binaryfile>\nUsage: %s <hostname> <port> <binaryfile>\n", argv[0], argv[0]);
136  return 1;
137 }
138 
mip_log_level
uint8_t mip_log_level
Logging level enum.
Definition: mip_logging.h:26
mip_logging.h
port
int port
Definition: CV7_example.c:45
printCommonUsage
int printCommonUsage(const char *argv[])
Definition: example_utils.cpp:133
customLog
void customLog(void *user, mip_log_level level, const char *fmt, va_list args)
Definition: example_utils.cpp:23
PORT_KEY
#define PORT_KEY
Definition: example_utils.cpp:14
getCurrentTimestamp
mip::Timestamp getCurrentTimestamp()
Definition: example_utils.cpp:17
handleCommonArgs
std::unique_ptr< ExampleUtils > handleCommonArgs(int argc, const char *argv[], int maxArgs)
Definition: example_utils.cpp:115
MIP_LOG_LEVEL_DEBUG
#define MIP_LOG_LEVEL_DEBUG
Debug logs are logged for debug purposes.
Definition: mip_logging.h:32
MIP_LOG_LEVEL_ERROR
#define MIP_LOG_LEVEL_ERROR
Error logs are logged when an error occurs.
Definition: mip_logging.h:29
openFromArgs
std::unique_ptr< ExampleUtils > openFromArgs(const std::string &port_or_hostname, const std::string &baud_or_port, const std::string &binary_file_path)
Definition: example_utils.cpp:50
stderr
stderr
time_since_epoch
double time_since_epoch()
MIP_LOG_LEVEL_FATAL
#define MIP_LOG_LEVEL_FATAL
Fatal logs are logged when an unrecoverable error occurs.
Definition: mip_logging.h:28
mip_timeout_from_baudrate
mip_timeout mip_timeout_from_baudrate(uint32_t baudrate)
Computes an appropriate packet timeout for a given serial baud rate.
Definition: mip_parser.c:475
MIP_LOG_INIT
#define MIP_LOG_INIT(callback, level, user)
Helper macro used to initialize the MIP logger.
Definition: mip_logging.h:63


microstrain_inertial_driver
Author(s): Brian Bingham, Parker Hannifin Corp
autogenerated on Mon Jun 24 2024 02:51:40