This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path5_series_threading_example.cpp
More file actions
680 lines (588 loc) · 25.3 KB
/
Copy path5_series_threading_example.cpp
File metadata and controls
680 lines (588 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
////////////////////////////////////////////////////////////////////////////////
/// @file 5_series_threading_example.cpp
///
/// @defgroup _5_series_threading_example_cpp 5-Series Threading Example [CPP]
///
/// @ingroup examples_cpp
///
/// @brief Example multithreading program for 5-series devices using C++
///
/// @details This example shows a basic setup for 5-series devices to
/// demonstrate multithreading for data collection and command updates
/// using C++. This is not an exhaustive example of all settings for
/// those devices. If this example does not meet your specific setup
/// needs, please consult the MIP SDK API documentation for the proper
/// commands.
///
/// @section _5_series_threading_example_cpp_license License
///
/// @copyright Copyright (c) 2025 MicroStrain by HBK
/// Licensed under MIT License
///
/// @{
///
// Include the MicroStrain Serial connection header
#include <microstrain/connections/serial/serial_connection.hpp>
// Include the MicroStrain logging header for custom logging
#include <microstrain/logging.hpp>
// Include all necessary MIP headers
// Note: The MIP SDK has headers for each module to include all headers associated with the module
// I.E., #include <mip/mip_all.hpp>
#include <mip/mip_interface.hpp>
#include <mip/definitions/commands_3dm.hpp>
#include <mip/definitions/commands_base.hpp>
#include <mip/definitions/data_sensor.hpp>
#include <chrono>
#include <cinttypes>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <thread>
////////////////////////////////////////////////////////////////////////////////
// NOTE: Setting these globally for example purposes
// TODO: Update to the correct port name and baudrate
/// @brief Set the port name for the connection (Serial/USB)
#ifdef _WIN32
static constexpr const char* PORT_NAME = "COM1";
#else // Unix
static constexpr const char* PORT_NAME = "/dev/ttyACM0";
#endif // _WIN32
/// @brief Set the baudrate for the connection (Serial/USB)
/// @note For native serial connections this needs to be 115200 due to the device default settings command
/// Use mip::commands_3dm::*UartBaudrate() to write and save the baudrate on the device
static constexpr uint32_t BAUDRATE = 115200;
// TODO: Update to the desired streaming rate. Setting low for readability purposes
/// @brief Streaming rate in Hz
static constexpr uint16_t SAMPLE_RATE_HZ = 1;
// TODO: Update to change the example run time
/// @brief Example run time
static constexpr uint32_t RUN_TIME_SECONDS = 30;
// TODO: Enable/disable data collection threading
/// @brief Use this to test the behaviors of threading
#define USE_THREADS true
////////////////////////////////////////////////////////////////////////////////
///
/// @} group _5_series_threading_example_cpp
////////////////////////////////////////////////////////////////////////////////
// Custom logging handler callback
static void logCallback(void* _user, const microstrain_log_level _level, const char* _format, va_list _args);
// Used for basic timestamping (since epoch in milliseconds)
// TODO: Update this to whatever timestamping method is desired
static mip::Timestamp getCurrentTimestamp();
// Common device initialization procedure
static void initializeDevice(mip::Interface& _device);
// Message format configuration
static void configureSensorMessageFormat(mip::Interface& _device);
// Packet callback handler
static void packetCallback(void* _user, const mip::PacketView& _packetView, mip::Timestamp _timestamp);
#if USE_THREADS
// Threaded functions
static bool updateDevice(mip::Interface& _device, mip::Timeout _waitTime, bool _fromCmd);
static void dataCollectionThread(mip::Interface& _device, const volatile bool& _running);
#endif // USE_THREADS
// Utility functions the handle application closing and printing error messages
static void terminate(microstrain::Connection* _connection, const char* _message, const bool _successful = false);
static void terminate(mip::Interface& _device, const mip::CmdResult _cmdResult, const char* _format, ...);
int main(const int argc, const char* argv[])
{
// Unused parameters
(void)argc;
(void)argv;
// Note: This is a compile-time way of checking that the proper logging level is enabled
// Note: The max available logging level may differ in pre-packaged installations of the MIP SDK
#ifndef MICROSTRAIN_LOGGING_ENABLED_INFO
#error This example requires a logging level of at least MICROSTRAIN_LOGGING_LEVEL_INFO_ to work properly
#endif // !MICROSTRAIN_LOGGING_ENABLED_INFO
#if USE_THREADS
// Create a mutex for the logging callbacks when multi-threading
fprintf(stdout, "Initializing the threading mutex.\n");
std::mutex mutex;
#endif // USE_THREADS
// Initialize the custom logger to print messages/errors as they occur
// Note: The logging level parameter doesn't need to match the max logging level.
// If the parameter is higher than the max level, higher-level logging functions will be ignored
#if USE_THREADS
MICROSTRAIN_LOG_INIT(&logCallback, MICROSTRAIN_LOG_LEVEL_INFO, static_cast<void*>(&mutex));
#else
MICROSTRAIN_LOG_INIT(&logCallback, MICROSTRAIN_LOG_LEVEL_INFO, nullptr);
#endif // USE_THREADS
// Initialize the connection
MICROSTRAIN_LOG_INFO("Initializing the connection.\n");
microstrain::connections::SerialConnection connection(PORT_NAME, BAUDRATE);
MICROSTRAIN_LOG_INFO("Connecting to the device on port %s with %d baudrate.\n", PORT_NAME, BAUDRATE);
// Open the connection to the device
if (!connection.connect())
{
terminate(&connection, "Could not open the connection!\n");
}
MICROSTRAIN_LOG_INFO("Initializing the device interface.\n");
mip::Interface device(
&connection, // Connection for the device
mip::C::mip_timeout_from_baudrate(BAUDRATE), // Set the base timeout for commands (milliseconds)
2000 // Set the base timeout for command replies (milliseconds)
);
initializeDevice(device);
// Configure the message format for sensor data
configureSensorMessageFormat(device);
// Sensor data packet callback
MICROSTRAIN_LOG_INFO("Registering a sensor data packet callback.\n");
mip::DispatchHandler packetHandler;
// Register the callback for packets
device.registerPacketCallback<&packetCallback>(
packetHandler,
mip::data_sensor::DESCRIPTOR_SET, // Data descriptor set
false, // Process after field callback
nullptr // User data
);
#if USE_THREADS
MICROSTRAIN_LOG_INFO("Initializing the device update function for threading.\n");
// Note: This allows the update function to be split into command and data updates across multiple threads
device.setUpdateFunctionFree<&updateDevice>();
// Data collection thread run flag
volatile bool running = true;
MICROSTRAIN_LOG_INFO("Creating the data collection thread.\n");
std::thread dataThread(dataCollectionThread, std::ref(device), std::ref(running));
#endif // USE_THREADS
// Resume the device
// Note: Since the device was idled for configuration, it needs to be resumed to output the data streams
MICROSTRAIN_LOG_INFO("Resuming the device.\n");
const mip::CmdResult cmdResult = mip::commands_base::resume(device);
if (!cmdResult.isAck())
{
terminate(device, cmdResult, "Could not resume the device!\n");
}
MICROSTRAIN_LOG_INFO("The device is configured... waiting for data.\n");
MICROSTRAIN_LOG_INFO("This example will now output data for %ds.\n", RUN_TIME_SECONDS);
// Get the start time of the device update loop to handle exiting the application
const mip::Timestamp loopStartTime = getCurrentTimestamp();
// Running loop
// Exit after a predetermined time in seconds
while (getCurrentTimestamp() - loopStartTime <= RUN_TIME_SECONDS * 1000)
{
// Stress testing the device with ping
// This attempts to trigger race conditions across threads
// Note: Only one thread at a time can safely send commands
MICROSTRAIN_LOG_WARN("Running device stress test!\n");
for (uint8_t counter = 0; counter < 100; ++counter)
{
// Note: Sending commands calls the device update function every time
mip::commands_base::ping(device);
}
}
#if USE_THREADS
// Signal the data collection thread to stop
running = false;
// Join the thread back before exiting the program
MICROSTRAIN_LOG_INFO("Waiting for the thread to join.\n");
dataThread.join();
#endif // USE_THREADS
terminate(&connection, "Example Completed Successfully.\n", true);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup _5_series_threading_example_cpp
/// @{
///
////////////////////////////////////////////////////////////////////////////////
/// @brief Custom logging callback for MIP SDK message formatting and output
///
/// @details Processes and formats log messages from the MIP SDK based on
/// severity level. Routes messages to appropriate output streams -
/// errors and fatal messages go to stderr while other levels go to
/// stdout. Each message is prefixed with its severity level name.
///
/// @param _user Pointer to the threading mutex (if threading is enabled)
/// @param _level Log message severity level from microstrain_log_level enum
/// @param _format Printf-style format string for the message
/// @param _args Variable argument list containing message parameters
///
static void logCallback(void* _user, const microstrain_log_level _level, const char* _format, va_list _args)
{
#if USE_THREADS
std::mutex* mutex = static_cast<std::mutex*>(_user);
assert(mutex);
// Lock the mutex since the callbacks can happen across threads
mutex->lock();
#else
// Unused parameter
(void)_user;
#endif // USE_THREADS
switch (_level)
{
case MICROSTRAIN_LOG_LEVEL_FATAL:
case MICROSTRAIN_LOG_LEVEL_ERROR:
{
fprintf(stderr, "%s: ", microstrain_logging_level_name(_level));
vfprintf(stderr, _format, _args);
fflush(stderr);
break;
}
case MICROSTRAIN_LOG_LEVEL_WARN:
case MICROSTRAIN_LOG_LEVEL_INFO:
case MICROSTRAIN_LOG_LEVEL_DEBUG:
case MICROSTRAIN_LOG_LEVEL_TRACE:
{
fprintf(stdout, "%s: ", microstrain_logging_level_name(_level));
vfprintf(stdout, _format, _args);
fflush(stdout);
break;
}
case MICROSTRAIN_LOG_LEVEL_OFF:
default:
{
break;
}
}
#if USE_THREADS
// Release the logging callback for other threads
mutex->unlock();
#endif // USE_THREADS
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Gets the current system timestamp in milliseconds
///
/// @details Provides system time measurement using std::chrono for milliseconds
/// since epoch. Uses system_clock to get wall-clock time that
/// corresponds to calendar time and can be synchronized with external
/// time sources.
///
/// @note Update this function to use a different time source if needed for
/// your specific application requirements
///
/// @return Current timestamp in milliseconds since epoch
///
static mip::Timestamp getCurrentTimestamp()
{
const std::chrono::nanoseconds timeSinceEpoch = std::chrono::system_clock::now().time_since_epoch();
return static_cast<mip::Timestamp>(std::chrono::duration_cast<std::chrono::milliseconds>(timeSinceEpoch).count());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initializes and configures a MIP device interface
///
/// @details Performs a complete device initialization sequence:
/// 1. Verifies device communication with a ping command
/// 2. Sets the device to idle mode to ensure reliable configuration
/// 3. Queries and displays detailed device information
/// 4. Loads default device settings for a known state
///
/// @param _device Reference to a MIP device interface to initialize
///
static void initializeDevice(mip::Interface& _device)
{
// Ping the device
// Note: This is a good first step to make sure the device is present
MICROSTRAIN_LOG_INFO("Pinging the device.\n");
mip::CmdResult cmdResult = mip::commands_base::ping(_device);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not ping the device!\n");
}
// Set the device to Idle
// Note: This is good to do during setup as high data traffic can cause commands to fail
MICROSTRAIN_LOG_INFO("Setting the device to idle.\n");
cmdResult = mip::commands_base::setIdle(_device);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not set the device to idle!\n");
}
// Print device info to make sure the correct device is being used
MICROSTRAIN_LOG_INFO("Getting the device information.\n");
mip::commands_base::BaseDeviceInfo deviceInfo;
cmdResult = mip::commands_base::getDeviceInfo(_device, &deviceInfo);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not get the device information!\n");
}
// Extract the major minor and patch values
const uint16_t major = deviceInfo.firmware_version / 1000;
const uint16_t minor = deviceInfo.firmware_version / 100 % 10;
const uint16_t patch = deviceInfo.firmware_version % 100;
// Firmware version format is x.x.xx
char firmwareVersion[16] = {0};
snprintf(firmwareVersion, sizeof(firmwareVersion) / sizeof(firmwareVersion[0]), "%d.%d.%02d", major, minor, patch);
MICROSTRAIN_LOG_INFO("-------- Device Information --------\n");
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Name", deviceInfo.model_name);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Model Number", deviceInfo.model_number);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Serial Number", deviceInfo.serial_number);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Lot Number", deviceInfo.lot_number);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Options", deviceInfo.device_options);
MICROSTRAIN_LOG_INFO("%-16s | %16s\n", "Firmware Version", firmwareVersion);
MICROSTRAIN_LOG_INFO("------------------------------------\n");
// Load the default settings on the device
// Note: This guarantees the device is in a known state
MICROSTRAIN_LOG_INFO("Loading %s.\n", mip::commands_3dm::DeviceSettings::DOC_NAME);
cmdResult = mip::commands_3dm::defaultDeviceSettings(_device);
if (!cmdResult.isAck())
{
// Note: Default settings will reset the baudrate to 115200 and may cause connection issues
if (cmdResult == mip::CmdResult::STATUS_TIMEDOUT && BAUDRATE != 115200)
{
MICROSTRAIN_LOG_WARN("On a native serial connections the baudrate needs to be 115200 for this example to run.\n");
}
terminate(_device, cmdResult, "Could not load %s!\n", mip::commands_3dm::DeviceSettings::DOC_NAME);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Configures message format for sensor data streaming
///
/// @details Sets up sensor data output by:
/// 1. Querying device base rate
/// 2. Validating desired sample rate against base rate
/// 3. Calculating proper decimation
/// 4. Configuring message format with:
/// - Scaled accelerometer
///
/// @param _device Reference to the initialized MIP device interface
///
static void configureSensorMessageFormat(mip::Interface& _device)
{
// Note: Querying the device base rate is only one way to calculate the descriptor decimation
// We could have also set it directly with information from the datasheet
MICROSTRAIN_LOG_INFO("Getting the base rate for sensor data.\n");
uint16_t sensorBaseRate;
mip::CmdResult cmdResult = mip::commands_3dm::imuGetBaseRate(
_device,
&sensorBaseRate // Base rate out
);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not get the base rate for sensor data!\n");
}
// Supported sample rates can be any value from 1 up to the base rate
// Note: Decimation can be anything from 1 to 65,565 (uint16_t::max)
if (SAMPLE_RATE_HZ == 0 || SAMPLE_RATE_HZ > sensorBaseRate)
{
terminate(
_device,
mip::CmdResult::NACK_INVALID_PARAM,
"Invalid sample rate of %dHz! Supported rates are [1, %d].\n",
SAMPLE_RATE_HZ,
sensorBaseRate
);
}
// Calculate the decimation (stream rate) for the device based on its base rate
const uint16_t sensorDecimation = sensorBaseRate / SAMPLE_RATE_HZ;
MICROSTRAIN_LOG_INFO(
"Decimating sensor base rate %d by %d to stream data at %dHz.\n",
sensorBaseRate,
sensorDecimation,
SAMPLE_RATE_HZ
);
// Descriptor rate is a pair of data descriptor set and decimation
const mip::DescriptorRate sensorDescriptors[1] = {
{mip::data_sensor::ScaledAccel::FIELD_DESCRIPTOR, sensorDecimation}
};
MICROSTRAIN_LOG_INFO("Configuring %s for sensor data.\n", mip::commands_3dm::ImuMessageFormat::DOC_NAME);
cmdResult = mip::commands_3dm::writeImuMessageFormat(
_device,
sizeof(sensorDescriptors) / sizeof(sensorDescriptors[0]), // Number of descriptors to include
sensorDescriptors // Descriptor array
);
if (!cmdResult.isAck())
{
terminate(
_device,
cmdResult,
"Could not configure %s for sensor data!\n",
mip::commands_3dm::ImuMessageFormat::DOC_NAME
);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Callback function that processes received MIP packets
///
/// @details This function is called whenever a MIP packet is received from the
/// device.
/// It processes the packet by:
/// 1. Extracting all fields from the packet
/// 2. Building a formatted string of field descriptors
/// 3. Logging packet information including timestamp, descriptor set,
/// and field descriptors
///
/// @param _user Pointer to user data (unused in this implementation)
/// @param _packetView Reference to the received MIP packet
/// @param _timestamp Timestamp when the packet was received
///
static void packetCallback(void* _user, const mip::PacketView& _packetView, mip::Timestamp _timestamp)
{
// Unused parameter
(void)_user;
// Create a buffer for printing purposes
char fieldDescriptorsBuffer[255] = {0};
int bufferOffset = 0;
// Iterate the packet and extract each field
for (const mip::FieldView& fieldView : _packetView)
{
bufferOffset += snprintf(
&fieldDescriptorsBuffer[bufferOffset],
sizeof(fieldDescriptorsBuffer) / sizeof(fieldDescriptorsBuffer[0]) - bufferOffset,
" 0x%02X,",
fieldView.fieldDescriptor()
);
}
// Trim off the last comma
if (bufferOffset > 0)
{
fieldDescriptorsBuffer[bufferOffset - 1] = '\0';
}
MICROSTRAIN_LOG_INFO(
"Received a packet at %" PRIu64 " with descriptor set 0x%02X:%s\n",
_timestamp,
_packetView.descriptorSet(),
fieldDescriptorsBuffer
);
}
#if USE_THREADS
////////////////////////////////////////////////////////////////////////////////
/// @brief Updates the device state based on command or data collection context
///
/// @details Handles device updates differently depending on whether called from
/// command handling or data collection:
/// - For data collection (_fromCmd = false): Performs normal device
/// updates
/// - For commands (_fromCmd = true): Sleeps briefly to save power
/// while avoiding command timeouts
///
/// @param _device Pointer to the MIP device interface
/// @param _waitTime Time to wait for updates (typically used only from
/// commands)
/// @param _fromCmd True if called from command handling, false for data
/// collection
///
/// @returns true if the update was successful, false on error.
/// Always returns true when called from commands to avoid race
/// conditions.
///
static bool updateDevice(mip::Interface& _device, mip::Timeout _waitTime, bool _fromCmd)
{
// Do normal updates only if not called from a command handler
// Note: This is the separation between the main/other thread and the data collection thread
if (!_fromCmd)
{
return _device.defaultUpdate(_waitTime, true);
}
// Sleep for a bit to save power
// Note: Waiting too long in here will cause commands to timeout
std::this_thread::sleep_for(std::chrono::milliseconds(5));
// Note: This needs to return true to avoid terminating the data collection thread
// Note: Returning false may cause a race condition (see comments in mip_interface_wait_for_reply)
return true;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Handles continuous data collection from the device in a separate
/// thread
///
/// @details Main function for the data collection thread that:
/// 1. Continuously updates device state for receiving data
/// 2. Clears command queue if the connection closes to avoid
/// deadlocks
/// 3. Yields thread time when possible
/// 4. Runs until the running state becomes false
///
/// @param _device Reference to the MIP device interface
/// @param _running Reference to volatile boolean controlling thread execution
///
static void dataCollectionThread(mip::Interface& _device, const volatile bool& _running)
{
MICROSTRAIN_LOG_INFO("Data collection thread created!\n");
while (_running)
{
// Update the device for data collection
// Note: The recommended default wait time is 10 ms, but could be 0 for non-blocking read operations
const bool updated = _device.update(
10, // Time to wait
false // From command
);
// Clean up and exit the thread on failed device updates
if (!updated)
{
// Avoid deadlocks if the connection is closed
_device.cmdQueue().clear();
break;
}
std::this_thread::yield();
}
}
#endif // USE_THREADS
////////////////////////////////////////////////////////////////////////////////
/// @brief Handles graceful program termination and cleanup
///
/// @details Handles graceful shutdown when errors occur:
/// - Outputs provided error message
/// - Closes device connection if open
/// - Exits with appropriate status code
///
/// @param _connection Pointer to the device connection to close
/// @param _message Error message to display
/// @param _successful Whether termination is due to success or failure
///
static void terminate(microstrain::Connection* _connection, const char* _message, const bool _successful /* = false */)
{
if (_message && strlen(_message) != 0)
{
if (_successful)
{
MICROSTRAIN_LOG_INFO("%s", _message);
}
else
{
MICROSTRAIN_LOG_ERROR("%s", _message);
}
}
if (!_connection)
{
// Create the device interface with a connection or set it after creation
MICROSTRAIN_LOG_ERROR("Connection not set for the device interface. Cannot close the connection.\n");
}
else
{
if (_connection->isConnected())
{
MICROSTRAIN_LOG_INFO("Closing the connection.\n");
if (!_connection->disconnect())
{
MICROSTRAIN_LOG_ERROR("Failed to close the connection!\n");
}
}
}
MICROSTRAIN_LOG_INFO("Press 'Enter' to exit the program.\n");
// Make sure the console remains open
const int confirmExit = getc(stdin);
(void)confirmExit; // Unused
if (!_successful)
{
exit(1);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Handles graceful program termination and command failure cleanup
///
/// @details Handles command failure scenarios:
/// - Formats and displays an error message with command result
/// - Closes device connection
/// - Exits with failure status
///
/// @param _device MIP device interface for the command that failed
/// @param _cmdResult Result code from a failed command
/// @param _format Printf-style format string for error message
/// @param ... Variable arguments for format string
///
static void terminate(mip::Interface& _device, const mip::CmdResult _cmdResult, const char* _format, ...)
{
if (_format && strlen(_format) != 0)
{
va_list args;
va_start(args, _format);
MICROSTRAIN_LOG_ERROR_V(_format, args);
va_end(args);
}
MICROSTRAIN_LOG_ERROR("Command Result: (%d) %s.\n", _cmdResult.value, _cmdResult.name());
// Get the connection pointer that was set during device initialization
microstrain::Connection* connection = static_cast<microstrain::Connection*>(_device.userPointer());
terminate(connection, "");
}
///
/// @} group _5_series_threading_example_cpp
////////////////////////////////////////////////////////////////////////////////