- added src/endian.h... still need to add configure support to
[barry.git] / src / socket.h
blob4bc792f434483984769590bf84374c59f25ed0d5
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; }
31 namespace Barry {
34 // Socket class
36 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
37 /// By default, provides raw send/receive access, as well as packet
38 /// writing on socket 0, which is always open.
39 ///
40 /// There are Open and Close members to open data sockets which are used
41 /// to transfer data to and from the device.
42 ///
43 /// The destructor will close any non-0 open sockets automatically.
44 ///
45 /// Requires an active Usb::Device object to work on.
46 ///
47 class Socket
49 Usb::Device &m_dev;
50 int m_writeEp, m_readEp;
51 uint16_t m_socket; // defaults to 0, which is valid,
52 // since socket 0 is always open
53 // If this is not 0, then class will
54 // deal with closing automatically.
55 uint8_t m_flag;
56 uint32_t m_sequenceId;
58 int m_lastStatus;
60 private:
61 // sends 'send' data to device, and waits for response, using
62 // "read first, write second" order observed in capture
63 void AppendFragment(Data &whole, const Data &fragment);
64 unsigned int MakeNextFragment(const Data &whole, Data &fragment, unsigned int offset = 0);
65 void CheckSequence(const Data &seq);
67 public:
68 Socket(Usb::Device &dev, int writeEndpoint, int readEndpoint);
69 ~Socket();
71 int GetLastStatus() const { return m_lastStatus; }
72 uint16_t GetSocket() const { return m_socket; }
74 void Open(uint16_t socket, uint8_t flag);
75 void Close();
77 // Send and Receive are available before Open...
78 // an unopened socket defaults to socket 0, which you need
79 // in order to set the blackberry mode
80 bool Send(const Data &send, Data &receive);
81 bool Receive(Data &receive);
83 // sends the send packet down to the device, fragmenting if
84 // necessary, and returns the response in receive, defragmenting
85 // if needed
86 // Blocks until response received or timed out in Usb::Device
87 bool Packet(const Data &send, Data &receive);
89 // some handy wrappers for the Packet() interface
90 bool NextRecord(Data &receive);
94 } // namespace Barry
96 #endif