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

ArLogFileConnection.cpp

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

Generated on Wed Oct 19 12:56:36 2005 for Aria by  doxygen 1.4.0