protocol.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       protocol.cc
00003 ///             USB Blackberry bulk protocol API
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "protocol.h"
00023 #include "protostructs.h"
00024 #include "data.h"
00025 #include "endian.h"
00026 #include "error.h"
00027 #include "debug.h"
00028 
00029 #include <sstream>
00030 
00031 namespace Barry { namespace Protocol {
00032 
00033 void CheckSize(const Data &packet, size_t requiredsize)
00034 {
00035         const Packet *p = (const Packet *) packet.GetData();
00036 
00037         // when packets are larger than 0xFFFF bytes, packet->size is no
00038         // longer reliable, so we go with the Data class size
00039         if( (btohs(p->size) != packet.GetSize() && packet.GetSize() <= 0xFFFF) ||
00040             packet.GetSize() < requiredsize )
00041 
00042         {
00043                 BadSize bs(btohs(p->size), packet.GetSize(), requiredsize);
00044                 eout(bs.what());
00045                 eout(packet);
00046                 throw bs;
00047         }
00048 }
00049 
00050 unsigned int GetSize(const Data &packet)
00051 {
00052         CheckSize(packet, 4);
00053 
00054         // when packets are larger than 0xFFFF bytes, packet->size is no
00055         // longer reliable, so we go with the Data class size
00056         if( packet.GetSize() > 0xFFFF ) {
00057                 return packet.GetSize();
00058         }
00059         else {
00060                 const Packet *p = (const Packet *) packet.GetData();
00061                 return btohs(p->size);
00062         }
00063 }
00064 
00065 }} // namespace Barry::Protocol
00066 

Generated on Wed Sep 24 21:27:32 2008 for Barry by  doxygen 1.5.1