Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArLogFileConnection.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include <ctype.h>
00027 
00028 #include "ArExport.h"
00029 #include "ariaOSDef.h"
00030 #include "ArLogFileConnection.h"
00031 #include "ArLog.h"
00032 #include "ariaUtil.h"
00033 
00034 AREXPORT ArLogFileConnection::ArLogFileConnection()
00035 {
00036   myStatus = STATUS_NEVER_OPENED;
00037   myLogFile = NULL;
00038   myFD = NULL;
00039   stopAfter = 1;
00040   strcpy(myName, "random");
00041   strcpy(myType, "amigo");
00042   strcpy(mySubtype, "amigo");
00043 }
00044 
00045 AREXPORT ArLogFileConnection::~ArLogFileConnection()
00046 {
00047   if (myFD != NULL)
00048     fclose(myFD);
00049 }
00050 
00051 
00052 AREXPORT void ArLogFileConnection::setLogFile(const char *fname)
00053 {
00054   if (fname == NULL)
00055     myLogFile = "robot.log";
00056   else
00057     myLogFile = fname;
00058 }
00059 
00060 AREXPORT bool ArLogFileConnection::openSimple(void)
00061 {
00062   if (internalOpen() == 0)
00063     return true;
00064   else
00065     return false;
00066 }
00067 
00073 AREXPORT int ArLogFileConnection::open(const char *fname)
00074 {
00075   setLogFile(fname);
00076   return internalOpen();
00077 }
00078 
00079 AREXPORT int ArLogFileConnection::internalOpen(void)
00080 {
00081   havePose = false;
00082   myFD = fopen(myLogFile, "r");
00083   if (myFD == NULL)
00084     {
00085       myStatus = STATUS_OPEN_FAILED;
00086       return OPEN_FILE_NOT_FOUND;
00087     }
00088   
00089   char buf[100];
00090   if (fgets(buf,100,myFD) != NULL)
00091     {
00092       if (strncmp(buf, "// Saphira log file", 19) != 0)
00093         {
00094           myStatus = STATUS_OPEN_FAILED;
00095           fclose(myFD);
00096           myFD = NULL;
00097           return OPEN_NOT_A_LOG_FILE;
00098         }
00099     }
00100   else
00101     {
00102       myStatus = STATUS_OPEN_FAILED;
00103       fclose(myFD);
00104       myFD = NULL;
00105       return OPEN_NOT_A_LOG_FILE;
00106     }
00107 
00108   // Set the robot pose
00109   if (fgets(buf,100,myFD) != NULL)
00110     {
00111       if (strncmp(buf, "// Robot position", 17) == 0)  // have a position!
00112         {
00113           int x,y,th;
00114           fgets(buf,100,myFD);
00115           sscanf(buf, "%d %d %d", &x, &y, &th);
00116           myPose.setX(x);
00117           myPose.setY(y);
00118           myPose.setTh(th);
00119           havePose = true;
00120         }
00121       if (strncmp(buf, "// Robot name", 13) == 0)  // have a name!
00122         {
00123           fgets(buf,100,myFD);
00124           sscanf(buf, "%s %s %s", myName, myType, mySubtype);
00125         }
00126     }
00127 
00128   myStatus = STATUS_OPEN;
00129   return 0;
00130 }
00131 
00132 void ArLogFileConnection::buildStrMap(void)
00133 {
00134   myStrMap[OPEN_FILE_NOT_FOUND] = "File not found.";
00135   myStrMap[OPEN_NOT_A_LOG_FILE] = "File is not a log file.";
00136 }
00137 
00138 AREXPORT const char * ArLogFileConnection::getOpenMessage(int messageNumber)
00139 {
00140   return myStrMap[messageNumber].c_str();
00141 }
00142 
00143 AREXPORT bool ArLogFileConnection::close(void)
00144 {
00145   myStatus = STATUS_CLOSED_NORMALLY;
00146   if (myFD != NULL)
00147     fclose(myFD);
00148   myFD = NULL;
00149   return true;
00150 }
00151 
00152 AREXPORT int ArLogFileConnection::read(const char *data, unsigned int size, 
00153                    unsigned int msWait)
00154 {
00155   ArTime timeDone;
00156   unsigned int bytesRead = 0;
00157   int n;
00158 
00159   if (getStatus() != STATUS_OPEN) 
00160   {
00161     ArLog::log(ArLog::Terse, 
00162            "ArLogFileConnection::read: Attempt to use port that is not open.");
00163     return -1;
00164   }
00165   
00166   timeDone.setToNow();
00167   timeDone.addMSec(msWait);
00168 
00169   if (stopAfter-- <= 0)
00170     {
00171       stopAfter= 1;
00172       return 0;
00173     }
00174 
00175   if (myFD != NULL)
00176     {
00177       char line[1000];
00178       if (fgets(line, 1000, myFD) == NULL) // done with file, close
00179         {
00180           close();
00181           return -1;
00182         }
00183       // parse the line
00184       int i=0;
00185       n = 0;
00186       while (line[i] != 0)
00187         {
00188           if (isdigit(line[i]))
00189             {
00190               if (isdigit(line[i+1]))
00191                 {
00192                   if (isdigit(line[i+2]))
00193                     {
00194                       const_cast<char *>(data)[n++] = 
00195                         100 * (line[i]-'0') + 10*(line[i+1]-'0') + line[i+2]-'0';
00196                       i++;
00197                     }
00198                   else
00199                       const_cast<char *>(data)[n++] = 10*(line[i]-'0') + line[i+1]-'0';
00200                   i++;
00201                 }
00202               else
00203                 const_cast<char *>(data)[n++] = line[i]-'0';
00204             }
00205           i++;
00206         }
00207     }
00208 
00209 #if 0
00210   if (n > 0)                    // add in checksum
00211     {
00212       int i;
00213       unsigned char nn;
00214       int c = 0;
00215 
00216       i = 3;
00217       nn = data[2] - 2;
00218       while (nn > 1) 
00219         {
00220           c += ((unsigned char)data[i]<<8) | (unsigned char)data[i+1];
00221           c = c & 0xffff;
00222           nn -= 2;
00223           i += 2;
00224         }
00225       if (nn > 0) 
00226         c = c ^ (int)((unsigned char) data[i]);
00227 
00228       const_cast<char *>(data)[n++] = (c << 8) & 0xff;
00229       const_cast<char *>(data)[n++] = c & 0xff;
00230     }
00231 #endif
00232 
00233   bytesRead = n;
00234   return bytesRead;
00235 }
00236 
00237 
00238 
00239 AREXPORT int ArLogFileConnection::write(const char *data, unsigned int size)
00240 {
00241   return size;                  // always write
00242 }
00243 
00244 
00248 AREXPORT const char *ArLogFileConnection::getLogFile(void)
00249 {
00250   return myLogFile;
00251 }
00252 
00253 AREXPORT int ArLogFileConnection::getStatus(void)
00254 {
00255   return myStatus;
00256 }
00257 
00258 AREXPORT bool ArLogFileConnection::isTimeStamping(void)
00259 {
00260   return false;
00261 }
00262 
00263 AREXPORT ArTime ArLogFileConnection::getTimeRead(int index)
00264 {
00265   ArTime now;
00266   now.setToNow();
00267   return now;
00268 }

Generated on Tue Feb 20 10:51:40 2007 for Aria by  doxygen 1.4.0