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

ArBasePacket.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 "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArBasePacket.h"
00030 #include <stdio.h>
00031 
00037 ArBasePacket::ArBasePacket(ArTypes::UByte2 bufferSize, 
00038                                     ArTypes::UByte2 headerLength,
00039                                     char * buf,
00040                                     ArTypes::UByte2 footerLength) 
00041 {
00042   if (buf == NULL && bufferSize > 0) 
00043   {
00044     myOwnMyBuf = true;
00045     myBuf = new char[bufferSize];
00046   } 
00047   else 
00048   {
00049     myOwnMyBuf = false;
00050     myBuf = buf;
00051   }
00052   myHeaderLength = headerLength;
00053   myFooterLength = footerLength;
00054   myReadLength = myHeaderLength;
00055   myMaxLength = bufferSize;
00056   myLength = myHeaderLength;
00057 }
00058 
00059 ArBasePacket::~ArBasePacket()
00060 {
00061   if (myOwnMyBuf && myBuf != NULL)
00062     delete[] myBuf;
00063 }
00064 
00065 
00066 void ArBasePacket::setBuf(char *buf, ArTypes::UByte2 bufferSize)
00067 {
00068   if (myOwnMyBuf) 
00069   {
00070     delete[] myBuf;
00071     myOwnMyBuf = false;
00072   } 
00073   myBuf = buf;
00074   myMaxLength = bufferSize;
00075 }
00076 
00077 void ArBasePacket::setMaxLength(ArTypes::UByte2 bufferSize)
00078 {
00079   if (myMaxLength >= bufferSize)
00080     return;
00081   if (myOwnMyBuf) 
00082   {
00083     delete[] myBuf;
00084     myOwnMyBuf = false;
00085   } 
00086   myBuf = new char[bufferSize];
00087   myMaxLength = bufferSize;
00088   myOwnMyBuf = true;
00089 }
00090 
00091 bool ArBasePacket::setLength(ArTypes::UByte2 length)
00092 {
00093   if (myOwnMyBuf && length > myMaxLength)
00094     return false;
00095   
00096   myLength = length;
00097   return true;
00098 }
00099 
00100 void ArBasePacket::setReadLength(ArTypes::UByte2 readLength)
00101 {
00102   myReadLength = readLength;
00103 }
00104 
00105 bool ArBasePacket::setHeaderLength(ArTypes::UByte2 length)
00106 {
00107   if (myOwnMyBuf && length > myMaxLength)
00108     return false;
00109   
00110   myHeaderLength = length;
00111   return true;
00112 }
00113 
00119 void ArBasePacket::resetRead(void)
00120 {
00121   myReadLength = myHeaderLength;
00122 }
00123 
00128 void ArBasePacket::empty(void)
00129 {
00130   myLength = myHeaderLength;
00131 }
00132 
00133 bool ArBasePacket::isNextGood(int bytes)
00134 {
00135   if (bytes <= 0)
00136     return false;
00137   
00138   // make sure it comes in before the header
00139   if (myReadLength + bytes <= myLength - myFooterLength)
00140     return true;
00141   
00142   return false;
00143 }
00144 
00145 const char *ArBasePacket::getBuf(void)
00146 {
00147   return myBuf;
00148 }
00149 
00150 void ArBasePacket::byteToBuf(ArTypes::Byte val)
00151 {
00152   memcpy(myBuf+myLength, &val, 1);
00153   myLength += 1;
00154 }
00155 
00156 void ArBasePacket::byte2ToBuf(ArTypes::Byte2 val)
00157 {
00158   unsigned char c;
00159   c = (val >> 8) & 0xff;
00160   memcpy(myBuf+myLength+1, &c, 1);
00161   c = val & 0xff;
00162   memcpy(myBuf+myLength, &c, 1);
00163   myLength += 2;
00164 }
00165 
00166 void ArBasePacket::byte4ToBuf(ArTypes::Byte4 val)
00167 {
00168   unsigned char c;
00169   c = (val >> 24) & 0xff;
00170   memcpy(myBuf+myLength+3, &c, 1);
00171   c = (val >> 16) & 0xff;
00172   memcpy(myBuf+myLength+2, &c, 1);
00173   c = (val >> 8) & 0xff;
00174   memcpy(myBuf+myLength+1, &c, 1);
00175   c = val & 0xff;
00176   memcpy(myBuf+myLength, &c, 1);
00177   myLength += 4;
00178 }
00179 
00180 void ArBasePacket::uByteToBuf(ArTypes::UByte val)
00181 {
00182   memcpy(myBuf+myLength, &val, 1);
00183   myLength += 1;
00184 }
00185 
00186 void ArBasePacket::uByte2ToBuf(ArTypes::UByte2 val)
00187 {
00188   unsigned char c;
00189   c = (val >> 8) & 0xff;
00190   memcpy(myBuf+myLength+1, &c, 1);
00191   c = val & 0xff;
00192   memcpy(myBuf+myLength, &c, 1);
00193   myLength += 2;
00194 }
00195 
00196 void ArBasePacket::uByte4ToBuf(ArTypes::UByte4 val)
00197 {
00198   memcpy(myBuf+myLength, &val, 4);
00199   myLength += 4;
00200 }
00201 
00205 void ArBasePacket::strToBuf(const char *str)
00206 {
00207   memcpy(myBuf+myLength, str, strlen(str) + 1);
00208   myLength+=strlen(str)+1;
00209 }
00210 
00215 void ArBasePacket::strNToBuf(const char *str, int length)
00216 {
00217   //byte4ToBuf(length);
00218   memcpy(myBuf+myLength, str, length);
00219   myLength+=length;
00220 }
00221 
00227 void ArBasePacket::strToBufPadded(const char *str, int length)
00228 {
00229   ArTypes::UByte2 len;
00230 
00231   len=strlen(str);
00232   if (len >= length) {
00233     memcpy(myBuf+myLength, str, length);
00234     myLength += length;
00235   }
00236   else
00237   {
00238     memcpy(myBuf+myLength, str, len);
00239     myLength+=len;
00240     memset(myBuf+myLength, 0, length-len);
00241     myLength+=length-len;
00242   }
00243 }
00244 
00249 void ArBasePacket::dataToBuf(const char *data, int length)
00250 {
00251   memcpy(myBuf+myLength, data, length);
00252   myLength+=length;
00253 }
00254 
00255 ArTypes::Byte ArBasePacket::bufToByte(void)
00256 {
00257   ArTypes::Byte ret=0;
00258 
00259   if (isNextGood(1))
00260   {
00261     memcpy(&ret, myBuf+myReadLength, 1);
00262     myReadLength+=1;
00263   }
00264 
00265   return(ret);
00266 }
00267 
00268 ArTypes::Byte2 ArBasePacket::bufToByte2(void)
00269 {
00270   ArTypes::Byte2 ret=0;
00271   unsigned char c1, c2;
00272 
00273   if (isNextGood(2))
00274   {
00275     memcpy(&c1, myBuf+myReadLength, 1);
00276     memcpy(&c2, myBuf+myReadLength+1, 1);
00277     ret = (c1 & 0xff) | (c2 << 8);
00278     myReadLength+=2;
00279   }
00280 
00281   return ret;
00282 }
00283 
00284 ArTypes::Byte4 ArBasePacket::bufToByte4(void)
00285 {
00286   ArTypes::Byte4 ret=0;
00287   unsigned char c1, c2, c3, c4;
00288 
00289   if (isNextGood(4))
00290   {
00291     memcpy(&c1, myBuf+myReadLength, 1);
00292     memcpy(&c2, myBuf+myReadLength+1, 1);
00293     memcpy(&c3, myBuf+myReadLength+2, 1);
00294     memcpy(&c4, myBuf+myReadLength+3, 1);
00295     ret = (c1 & 0xff) | (c2 << 8) | (c3 << 16) | (c4 << 24);
00296     myReadLength+=4;
00297   }
00298 
00299   return ret;
00300 }
00301 
00302 ArTypes::UByte ArBasePacket::bufToUByte(void)
00303 {
00304   ArTypes::UByte ret=0;
00305 
00306   if (isNextGood(1))
00307   {
00308     memcpy(&ret, myBuf+myReadLength, 1);
00309     myReadLength+=1;
00310   }
00311 
00312   return(ret);
00313 }
00314 
00315 ArTypes::UByte2 ArBasePacket::bufToUByte2(void)
00316 {
00317   ArTypes::UByte2 ret=0;
00318   unsigned char c1, c2;
00319 
00320   if (isNextGood(2))
00321   {
00322     memcpy(&c1, myBuf+myReadLength, 1);
00323     memcpy(&c2, myBuf+myReadLength+1, 1);
00324     ret = (c1 & 0xff) | (c2 << 8);
00325     myReadLength+=2;
00326   }
00327 
00328   return ret;
00329 }
00330 
00331 ArTypes::UByte4 ArBasePacket::bufToUByte4(void)
00332 {
00333   ArTypes::Byte4 ret=0;
00334   unsigned char c1, c2, c3, c4;
00335 
00336   if (isNextGood(4))
00337   {
00338     memcpy(&c1, myBuf+myReadLength, 1);
00339     memcpy(&c2, myBuf+myReadLength+1, 1);
00340     memcpy(&c3, myBuf+myReadLength+2, 1);
00341     memcpy(&c4, myBuf+myReadLength+3, 1);
00342     ret = (c1 & 0xff) | (c2 << 8) | (c3 << 16) | (c4 << 24);
00343     myReadLength+=4;
00344   }
00345 
00346   return ret;
00347 }
00348 
00356 void ArBasePacket::bufToStr(char *buf, int len)
00357 {
00358   int i;
00359 
00360   buf[0] = '\0';
00361   // see if we can read
00362   if (isNextGood(1))
00363   {
00364     // while we can read copy over those bytes
00365     for (i = 0; 
00366         isNextGood(1) && i < len && myBuf[myReadLength] != '\0';
00367          ++myReadLength, ++i)
00368       buf[i] = myBuf[myReadLength];
00369     // if we stopped because of a null then copy that one too
00370     if (myBuf[myReadLength] == '\0')
00371     {
00372       buf[i] = myBuf[myReadLength];
00373       myReadLength++;
00374     }
00375   }
00376 }
00377 
00384 void ArBasePacket::bufToData(char *data, int length)
00385 {
00386   if (isNextGood(length))
00387   {
00388     memcpy(data, myBuf+myReadLength, length);
00389     myReadLength += length;
00390   }
00391 }
00392 
00393 
00399 void ArBasePacket::duplicatePacket(ArBasePacket *packet)
00400 {
00401   myLength = packet->getLength();
00402   myReadLength = packet->getReadLength();
00403   memcpy(myBuf, packet->getBuf(), myLength);
00404 }
00405 
00406 void ArBasePacket::log(void)
00407 {
00408   int i;
00409   printf("Packet: ");
00410   for (i = 0; i < myLength; i++)
00411     printf(" %d ", (unsigned char) myBuf[i]);
00412   printf("\n");
00413 }
00414 
00415 void ArBasePacket::printHex(void)
00416 {
00417   int i;
00418   printf("Packet: ");
00419   for (i = 0; i < myLength; i++)
00420     printf(" 0x%x ", (unsigned char) myBuf[i]);
00421   printf("\n");
00422 }
00423 

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