- fixed compile error on g++ 3.3 systems (missing stdint.h in probe.h)
[barry.git] / src / socket.h
blob3235a208c422cc4b273e5fa02deeb106efde3c06
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2006, 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 #ifndef __BARRY_SOCKET_H__
23 #define __BARRY_SOCKET_H__
25 #include <stdint.h>
27 // forward declarations
28 class Data;
29 namespace Usb { class Device; }
30 namespace Barry { class Packet; }
32 namespace Barry {
35 // Socket class
37 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
38 /// By default, provides raw send/receive access, as well as packet
39 /// writing on socket 0, which is always open.
40 ///
41 /// There are Open and Close members to open data sockets which are used
42 /// to transfer data to and from the device.
43 ///
44 /// The destructor will close any non-0 open sockets automatically.
45 ///
46 /// Requires an active Usb::Device object to work on.
47 ///
48 class Socket
50 Usb::Device &m_dev;
51 int m_writeEp, m_readEp;
52 uint16_t m_socket; // defaults to 0, which is valid,
53 // since socket 0 is always open
54 // If this is not 0, then class will
55 // deal with closing automatically.
56 uint8_t m_flag;
57 uint32_t m_sequenceId;
59 int m_lastStatus;
61 private:
62 // sends 'send' data to device, and waits for response, using
63 // "read first, write second" order observed in capture
64 void AppendFragment(Data &whole, const Data &fragment);
65 unsigned int MakeNextFragment(const Data &whole, Data &fragment, unsigned int offset = 0);
66 void CheckSequence(const Data &seq);
68 public:
69 Socket(Usb::Device &dev, int writeEndpoint, int readEndpoint);
70 ~Socket();
72 int GetLastStatus() const { return m_lastStatus; }
73 uint16_t GetSocket() const { return m_socket; }
75 void Open(uint16_t socket, uint8_t flag);
76 void Close();
78 // Send and Receive are available before Open...
79 // an unopened socket defaults to socket 0, which you need
80 // in order to set the blackberry mode
81 bool Send(const Data &send, Data &receive);
82 bool Receive(Data &receive);
84 // sends the send packet down to the device, fragmenting if
85 // necessary, and returns the response in receive, defragmenting
86 // if needed
87 // Blocks until response received or timed out in Usb::Device
88 bool Packet(const Data &send, Data &receive);
89 bool Packet(Barry::Packet &packet);
91 // some handy wrappers for the Packet() interface
92 bool NextRecord(Data &receive);
96 } // namespace Barry
98 #endif