| 1 | |
| 2 | // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, |
| 6 | // are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions, and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions, and the following disclaimer in the documentation |
| 13 | // and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 | // may be used to endorse or promote products derived from this software without |
| 17 | // specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 | // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 | // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 | // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 | // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 | // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 | // |
| 32 | |
| 33 | #include "xsportinfo.h" |
| 34 | #include <ctype.h> |
| 35 | #include <string.h> // strlen |
| 36 | #include <stdlib.h> // atoi |
| 37 | |
| 38 | /*! \class XsPortInfo |
| 39 | \brief Contains a descriptor for opening a communication port to an Xsens device. |
| 40 | */ |
| 41 | /*! \addtogroup cinterface C Interface |
| 42 | @{ |
| 43 | */ |
| 44 | |
| 45 | /*! \relates XsPortInfo |
| 46 | \brief Initializes the object to the empty state |
| 47 | */ |
| 48 | void XsPortInfo_clear(XsPortInfo* thisPtr) |
| 49 | { |
| 50 | thisPtr->m_baudrate = XBR_Invalid; |
| 51 | thisPtr->m_deviceId.m_deviceId = 0; |
| 52 | thisPtr->m_deviceId.m_productCode[0] = '\0'; |
| 53 | thisPtr->m_portName[0] = '\0'; |
| 54 | thisPtr->m_linesOptions = XPLO_All_Ignore; |
| 55 | } |
| 56 | |
| 57 | /*! \relates XsPortInfo |
| 58 | \brief Returns true if the XsPortInfo object is empty |
| 59 | */ |
| 60 | int XsPortInfo_empty(const struct XsPortInfo* thisPtr) |
| 61 | { |
| 62 | return (thisPtr->m_portName[0] == '\0'); |
| 63 | } |
| 64 | |
| 65 | /*! \relates XsPortInfo |
| 66 | \brief The port number |
| 67 | \returns Returns the port number |
| 68 | \note Available on Windows only |
| 69 | */ |
| 70 | int XsPortInfo_portNumber(const struct XsPortInfo* thisPtr) |
| 71 | { |
| 72 | size_t i; |
| 73 | |
| 74 | if (XsPortInfo_empty(thisPtr)) |
| 75 | return 0; |
| 76 | |
| 77 | for (i = 0; i < strlen(thisPtr->m_portName); i++) { |
| 78 | if (isdigit(thisPtr->m_portName[i])) { |
| 79 | return atoi(&thisPtr->m_portName[i]); |
| 80 | } |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /*! \relates XsPortInfo |
| 86 | \brief Returns true if this port info object contains a USB device |
| 87 | */ |
| 88 | int XsPortInfo_isUsb(const struct XsPortInfo* thisPtr) |
| 89 | { |
| 90 | #ifdef XSENS_WINDOWS |
| 91 | return strncmp("\\\\?\\usb", thisPtr->m_portName, 7) == 0; |
| 92 | #else |
| 93 | return strncmp("USB", thisPtr->m_portName, 3) == 0; // libusb devices start with USB |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | /*! |
| 98 | * \relates XsPortInfo |
| 99 | * \brief Returns true if this port info object contains a network device |
| 100 | */ |
| 101 | int XsPortInfo_isNetwork(const struct XsPortInfo* thisPtr) |
| 102 | { |
| 103 | return strncmp("NET:", thisPtr->m_portName, 4) == 0; |
| 104 | } |
| 105 | |
| 106 | /*! |
| 107 | * \relates XsPortInfo |
| 108 | * \brief Returns the network service name of this port |
| 109 | */ |
| 110 | const char* XsPortInfo_networkServiceName(const struct XsPortInfo* thisPtr) |
| 111 | { |
| 112 | return &thisPtr->m_portName[4]; |
| 113 | } |
| 114 | |
| 115 | /*! \relates XsPortInfo |
| 116 | \brief The usb bus |
| 117 | \returns Returns the Usb bus number |
| 118 | \note Available on Linux only |
| 119 | */ |
| 120 | int XsPortInfo_usbBus(const struct XsPortInfo* thisPtr) |
| 121 | { |
| 122 | #ifndef XSENS_WINDOWS |
| 123 | if (XsPortInfo_isUsb(thisPtr)) |
| 124 | return atoi(&thisPtr->m_portName[3]); |
| 125 | #else |
| 126 | (void) thisPtr; |
| 127 | #endif |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /*! \relates XsPortInfo |
| 132 | \brief The usb address |
| 133 | \returns Returns the usb address |
| 134 | \note Available on Linux only |
| 135 | */ |
| 136 | int XsPortInfo_usbAddress(const struct XsPortInfo* thisPtr) |
| 137 | { |
| 138 | #ifndef XSENS_WINDOWS |
| 139 | if (XsPortInfo_isUsb(thisPtr)) |
| 140 | return atoi(&thisPtr->m_portName[7]); |
| 141 | #else |
| 142 | (void) thisPtr; |
| 143 | #endif |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /*! \brief Swap the contents of \a a with those of \a b |
| 148 | */ |
| 149 | void XsPortInfo_swap(struct XsPortInfo* a, struct XsPortInfo* b) |
| 150 | { |
| 151 | int i; |
| 152 | char c; |
| 153 | XsPortLinesOptions pLineOpts; |
| 154 | |
| 155 | XsBaudRate t = a->m_baudrate; |
| 156 | a->m_baudrate = b->m_baudrate; |
| 157 | b->m_baudrate = t; |
| 158 | |
| 159 | XsDeviceId_swap(&a->m_deviceId, &b->m_deviceId); |
| 160 | |
| 161 | for (i = 0; i < 256; ++i) |
| 162 | { |
| 163 | c = a->m_portName[i]; |
| 164 | a->m_portName[i] = b->m_portName[i]; |
| 165 | b->m_portName[i] = c; |
| 166 | } |
| 167 | |
| 168 | pLineOpts = a->m_linesOptions; |
| 169 | a->m_linesOptions = b->m_linesOptions; |
| 170 | b->m_linesOptions = pLineOpts; |
| 171 | } |
| 172 | |
| 173 | /*! @} */ |