8 #include "example_utils.hpp"
12 #define PORT_KEY "COM"
14 #define PORT_KEY "/dev/"
19 using namespace std::chrono;
20 return duration_cast<milliseconds>( steady_clock::now().
time_since_epoch() ).count();
28 va_copy(args_copy, args);
29 const int required_len = vsnprintf(
nullptr, 0, fmt, args_copy);
30 if (required_len >= 0)
32 log.resize(required_len);
33 vsnprintf(&log[0], required_len + 1, fmt, args);
50 std::unique_ptr<ExampleUtils>
openFromArgs(
const std::string& port_or_hostname,
const std::string& baud_or_port,
const std::string& binary_file_path)
52 auto example_utils = std::unique_ptr<ExampleUtils>(
new ExampleUtils());
54 if( !binary_file_path.empty() )
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
65 if(port_or_hostname.find(
PORT_KEY) == std::string::npos)
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.");
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
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));
83 throw std::runtime_error(
"This program was compiled without socket support. Recompile with -DMIP_USE_TCP=1");
91 uint32_t baud = std::strtoul(baud_or_port.c_str(),
nullptr, 10);
93 throw std::runtime_error(
"Serial baud rate must be a decimal integer greater than 0.");
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
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
109 if( !example_utils->connection->connect() )
110 throw std::runtime_error(
"Failed to open the connection");
112 return example_utils;
120 if( argc < 3 || argc > maxArgs )
122 throw std::underflow_error(
"Usage error");
126 std::string binary_file_path =
"";
128 binary_file_path = argv[3];
130 return openFromArgs(argv[1], argv[2], binary_file_path);
135 fprintf(
stderr,
"Usage: %s <portname> <baudrate> <binaryfile>\nUsage: %s <hostname> <port> <binaryfile>\n", argv[0], argv[0]);