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

ArNetPacket.cpp

Go to the documentation of this file.
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 "Aria.h"
00027 #include "ArExport.h"
00028 #include "ArNetPacket.h"
00029 
00030 AREXPORT ArNetPacket::ArNetPacket(ArTypes::UByte2 bufferSize) :
00031   ArBasePacket(bufferSize, ArNetPacket::HEADER_LENGTH, NULL, ArNetPacket::FOOTER_LENGTH)
00032 {
00033   myCommand = 0;
00034   myAddedFooter = false;
00035 }
00036 
00037 AREXPORT ArNetPacket::~ArNetPacket()
00038 {
00039 
00040 }
00041 
00042 AREXPORT void ArNetPacket::doubleToBuf(double val)
00043 {
00044   char buf[256];
00045   if (val == -HUGE_VAL)
00046         sprintf(buf, "-INF");
00047   else if (val == HUGE_VAL)
00048         sprintf(buf, "INF");
00049   else
00050         sprintf(buf, "%g", val);
00051   strToBuf(buf);
00052 }
00053 
00054 AREXPORT double ArNetPacket::bufToDouble(void)
00055 {
00056   char buf[256];
00057   char *endPtr;
00058   double ret;
00059 
00060   bufToStr(buf, sizeof(buf));
00061   if (strncmp(buf, "-INF", sizeof(buf)) == 0)
00062   {
00063         ret = -HUGE_VAL;
00064         return ret;
00065   }
00066   else if (strncmp(buf, "INF", sizeof(buf)) == 0)
00067   {
00068         ret = HUGE_VAL;
00069         return ret;
00070   }
00071   else
00072   {
00073         ret = strtod(buf, &endPtr);
00074         if (endPtr[0] == '\0' && endPtr != buf)
00075           return ret;
00076         else
00077       return 0;
00078   }
00079 }
00080 
00081 AREXPORT void ArNetPacket::empty(void)
00082 {
00083   myCommand = 0;
00084   myLength = myHeaderLength;
00085   myAddedFooter = false;
00086   resetValid();
00087 }
00088 
00089 AREXPORT void ArNetPacket::finalizePacket(void)
00090 {
00091   int length;
00092   length = myLength;
00093   int chkSum;
00094 
00095   myLength = 0;
00096   uByteToBuf(0xF);
00097   uByteToBuf(0xC);
00098   if (myAddedFooter)
00099     uByte2ToBuf(length);
00100   else
00101     uByte2ToBuf(length+2);
00102   uByte2ToBuf(myCommand);
00103   if (myAddedFooter)
00104     myLength = length - 2;
00105   else
00106     myLength = length;
00107   chkSum = calcCheckSum();
00108   byteToBuf((chkSum >> 8) & 0xff );
00109   byteToBuf(chkSum & 0xff );
00110   myAddedFooter = true;
00111   //log();
00112   //printf("%d %d %d\n", myLength ,myCommand, chkSum);
00113 }
00114 
00115 AREXPORT void ArNetPacket::resetRead(void)
00116 {
00117   myReadLength = 4;
00118   myCommand = bufToUByte2();
00119   myReadLength = myHeaderLength;
00120   resetValid();
00121 
00122 }
00123 
00124 AREXPORT void ArNetPacket::setCommand(ArTypes::UByte2 command)
00125 {
00126   myCommand = command;
00127 }
00128 
00129 AREXPORT ArTypes::UByte2 ArNetPacket::getCommand(void)
00130 {
00131   return myCommand;
00132 }
00133 
00134 AREXPORT void ArNetPacket::duplicatePacket(ArNetPacket *packet)
00135 {
00136   myLength = packet->myLength;
00137   if (myMaxLength < myLength)
00138     setMaxLength(packet->myLength);
00139   myReadLength = packet->myReadLength;
00140   myHeaderLength = packet->myHeaderLength;
00141   myFooterLength = packet->myFooterLength;
00142   myCommand = packet->myCommand;
00143   myAddedFooter = packet->myAddedFooter;
00144   memcpy(myBuf, packet->getBuf(), packet->myLength + packet->myFooterLength);
00145 }
00146 
00147 AREXPORT ArTypes::Byte2 ArNetPacket::calcCheckSum(void)
00148 {
00149   int i;
00150   int n;
00151   int c = 0;
00152 
00153   //printf("%d\n", myLength); 
00154   //log();
00155   i = 3;
00156   n = myLength - 2;
00157   while (n > 3) {
00158     //printf("n %d i %d c %d c1 %d c2 %d\n", n, i, c, myBuf[i], myBuf[i+1]);
00159     c += ((unsigned char)myBuf[i]<<8) | (unsigned char)myBuf[i+1];
00160     c = c & 0xffff;
00161     n -= 2;
00162     i += 2;
00163   }
00164   //printf("aft n %d i %d c %d\n", n, i, c);
00165   if (n > 0) 
00166     c = c ^ (int)((unsigned char) myBuf[i]);
00167   //printf("%d\n", c);
00168   return c;
00169 }
00170 
00171 AREXPORT bool ArNetPacket::verifyCheckSum(void) 
00172 {
00173   ArTypes::Byte2 chksum;
00174   ArTypes::Byte2 calcedChksum;
00175   unsigned char c1, c2;
00176   int length;
00177 
00178   if (myLength - 2 < myHeaderLength)
00179     return false;
00180 
00181   c2 = myBuf[myLength-2];
00182   c1 = myBuf[myLength-1];
00183   chksum = (c1 & 0xff) | (c2 << 8);
00184   length = myLength;
00185   myLength = myLength - 2;
00186   calcedChksum = calcCheckSum();
00187   myLength = length;
00188 
00189   //printf("%d %d\n", chksum, calcedChksum);
00190   if (chksum == calcedChksum) {
00191     return true;
00192   } else {
00193     return false;
00194   }
00195   
00196 }

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0