udev: added 69-blackberry.rules for Fedora 12 systems
[barry.git] / src / protocol.cc
blobbec3dd1ea240040d7991fef761cf2b3c386d1595
1 ///
2 /// \file protocol.cc
3 /// USB Blackberry bulk protocol API
4 ///
6 /*
7 Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "protocol.h"
23 #include "protostructs.h"
24 #include "data.h"
25 #include "endian.h"
26 #include "error.h"
27 #include "debug.h"
29 #include <sstream>
31 namespace Barry { namespace Protocol {
33 // This function is only valid for Packet, JLPacket, and JVMPacket structs,
34 // as long as they don't differ from each other in header layout, when
35 // it comes to the .size field. (see protostructs.h)
36 void CheckSize(const Data &packet, size_t requiredsize)
38 const Packet *p = (const Packet *) packet.GetData();
40 // when packets are larger than 0xFFFF bytes, packet->size is no
41 // longer reliable, so we go with the Data class size
42 if( (packet.GetSize() >= 4 && btohs(p->size) != packet.GetSize() && packet.GetSize() <= 0xFFFF) ||
43 packet.GetSize() < requiredsize )
46 BadSize bs(packet.GetSize() >= 4 ? btohs(p->size) : 0,
47 packet.GetSize(), requiredsize);
48 eout(bs.what());
49 eout(packet);
50 throw bs;
54 unsigned int GetSize(const Data &packet)
56 CheckSize(packet, 4);
58 // when packets are larger than 0xFFFF bytes, packet->size is no
59 // longer reliable, so we go with the Data class size
60 if( packet.GetSize() > 0xFFFF ) {
61 return packet.GetSize();
63 else {
64 const Packet *p = (const Packet *) packet.GetData();
65 return btohs(p->size);
69 }} // namespace Barry::Protocol