Kuon  1.1.3
RoadNarrows Robotics Large Outdoor Mobile Robot Project
kuonUtils.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Kuon
4 //
5 // Library: libkuon
6 //
7 // File: kuonUtils.cxx
8 //
9 //
10 /*! \file
11  *
12  * $LastChangedDate: 2014-04-04 17:05:03 -0600 (Fri, 04 Apr 2014) $
13  * $Rev: 3629 $
14  *
15  * \brief Kuon utilities.
16  *
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \copyright
21  * \h_copy 2012-2017. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60 #include <unistd.h>
61 #include <libgen.h>
62 #include <stdio.h>
63 #include <errno.h>
64 
65 #include <string>
66 #include <sstream>
67 
68 #include "rnr/rnrconfig.h"
69 #include "rnr/log.h"
70 
71 #include "Kuon/kuon.h"
72 #include "Kuon/kuonUtils.h"
73 
74 using namespace std;
75 
76 
77 //------------------------------------------------------------------------------
78 // Private Interface
79 //------------------------------------------------------------------------------
80 
81 /*!
82  * \ingroup libkuon
83  * \brief \h_kuon Error Code String Table.
84  *
85  * Table is indexed by \h_kuon error codes (see \ref kuon_ecodes). Keep
86  * in sync.
87  */
88 static const char *EcodeStrTbl[] =
89 {
90  "Ok", ///< [KUON_OK]
91 
92  "Error", ///< [KUON_ECODE_GEN]
93  "System error", ///< [KUON_ECODE_SYS]
94  "Internal error", ///< [KUON_ECODE_INTERNAL]
95  "Bad value", ///< [KUON_ECODE_BAD_VAL]
96  "Too big", ///< [KUON_ECODE_TOO_BIG]
97  "Too small", ///< [KUON_ECODE_TOO_SMALL]
98  "Value out-of-range", ///< [KUON_ECODE_RANGE]
99  "Invalid operation", ///< [KUON_ECODE_BAD_OP]
100  "Operation timed out", ///< [KUON_ECODE_TIMEDOUT]
101  "Device not found", ///< [KUON_ECODE_NO_DEV]
102  "No resource available", ///< [KUON_ECODE_NO_RSRC]
103  "Resource busy", ///< [KUON_ECODE_BUSY]
104  "Cannot execute", ///< [KUON_ECODE_NO_EXEC]
105  "Permissions denied", ///< [KUON_ECODE_PERM]
106  "Motor error", ///< [KUON_ECODE_MOTOR]
107  "Motor controller error", ///< [KUON_ECODE_MOT_CTLR]
108  "Battery error", ///< [KUON_ECODE_BATT]
109  "Bad format", ///< [KUON_ECODE_FORMAT]
110  "BotSense error", ///< [KUON_ECODE_BOTSENSE]
111  "File not found", ///< [KUON_ECODE_NO_FILE]
112  "XML error", ///< [KUON_ECODE_XML]
113  "Robot is in an alarmed state", ///< [KUON_ECODE_ALARMED]
114  "Operation interrupted", ///< [KUON_ECODE_INTR]
115  "Robotic link(s) movement obstructed", ///< [KUON_ECODE_COLLISION]
116  "Robot emergency stopped", ///< [KUON_ECODE_ESTOP]
117 
118  "Invalid error code" ///< [KUON_ECODE_BADEC]
119 };
120 
121 
122 //------------------------------------------------------------------------------
123 // Public Interface
124 //------------------------------------------------------------------------------
125 
126 const char *kuon::getStrError(const int ecode)
127 {
128  int ec = ecode >= 0 ? ecode : -ecode;
129 
130  if( ec >= arraysize(EcodeStrTbl) )
131  {
132  ec = KUON_ECODE_BADEC;
133  }
134 
135  return EcodeStrTbl[ec];
136 }
137 
138 uint_t kuon::strToVersion(const string &str)
139 {
140  int nMajor = 0;
141  int nMinor = 0;
142  int nRevision = 0;
143 
144  sscanf(str.c_str(), "%d.%d.%d", &nMajor, &nMinor, &nRevision);
145 
146  return KUON_VERSION(nMajor, nMinor, nRevision);
147 }
148 
149 string kuon::getRealDeviceName(const string &strDevName)
150 {
151  char buf[MAX_PATH+1];
152  ssize_t len;
153 
154  //
155  // Symbolic link.
156  //
157  if( (len = readlink(strDevName.c_str(), buf, MAX_PATH)) > 0 )
158  {
159  buf[len] = 0;
160 
161  // absollute path
162  if( buf[0] == '/' )
163  {
164  string strRealDevName(buf);
165  return strRealDevName;
166  }
167 
168  // relative path
169  else
170  {
171  char s[strDevName.size()+1];
172  stringstream ss;
173 
174  strcpy(s, strDevName.c_str());
175 
176  char *sDirName = dirname(s);
177 
178  ss << sDirName << "/" << buf;
179 
180  return ss.str();
181  }
182  }
183 
184  //
185  // Real device.
186  //
187  else
188  {
189  return strDevName;
190  }
191 }
Kuon common utilities.
uint_t strToVersion(const std::string &str)
Convert version dotted string to integer equivalent.
#define KUON_VERSION(major, minor, revision)
Convert version triplet to integer equivalent.
Definition: kuon.h:158
RoadNarrows Kuon robot top-level header file.
static const int KUON_ECODE_BADEC
bad error code
Definition: kuon.h:108
std::string getRealDeviceName(const std::string &strDevName)
Get real device name.
static const char * EcodeStrTbl[]
<b><i>Kuon</i></b> Error Code String Table.
Definition: kuonUtils.cxx:88
const char * getStrError(const int ecode)
Get the error string describing the error code.
Definition: kuonUtils.cxx:126