Release tarball for barry-0.9
[barry.git] / src / socket.h
blobfb757f3a67c39fd12acb0d9b2cd39971a8a4a9f7
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2007, 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 namespace Usb { class Device; }
29 namespace Barry { class Data; class Packet; }
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_zeroSocketSequence;
56 uint8_t m_flag;
57 uint32_t m_sequenceId;
59 // half open socket stata, for passwords
60 bool m_halfOpen;
61 uint32_t m_challengeSeed;
62 unsigned int m_remainingTries;
64 private:
65 // sends 'send' data to device, and waits for response, using
66 // "read first, write second" order observed in capture
67 void AppendFragment(Data &whole, const Data &fragment);
68 unsigned int MakeNextFragment(const Data &whole, Data &fragment, unsigned int offset = 0);
69 void CheckSequence(const Data &seq);
71 void SendOpen(uint16_t socket, Data &receive);
72 void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
74 public:
75 Socket(Usb::Device &dev, int writeEndpoint, int readEndpoint,
76 uint8_t zeroSocketSequenceStart = 0);
77 ~Socket();
79 uint16_t GetSocket() const { return m_socket; }
80 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
82 void Open(uint16_t socket, const char *password = 0);
83 void Close();
85 // Send and Receive are available before Open...
86 // an unopened socket defaults to socket 0, which you need
87 // in order to set the blackberry mode
88 // The send function will overwrite the zeroSocketSequence byte
89 // *inside* the packet, if the current m_socket is 0.
90 void Send(Data &send, int timeout = -1); // send only
91 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
92 void Send(Barry::Packet &packet, int timeout = -1);
93 void Receive(Data &receive, int timeout = -1);
95 // sends the send packet down to the device, fragmenting if
96 // necessary, and returns the response in receive, defragmenting
97 // if needed
98 // Blocks until response received or timed out in Usb::Device
99 void Packet(Data &send, Data &receive, int timeout = -1);
100 void Packet(Barry::Packet &packet, int timeout = -1);
102 // some handy wrappers for the Packet() interface
103 void NextRecord(Data &receive);
107 } // namespace Barry
109 #endif