lib: added back m_seen_usb_error check in DefaultRead()
[barry.git] / src / socket.h
blobd1081203d9e3df21ea7342f642560bcc5ff40397
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 "dll.h"
26 #include <stdint.h>
27 #include <queue>
28 #include <memory>
29 #include "router.h"
31 // forward declarations
32 namespace Usb { class Device; }
33 namespace Barry {
34 class Data;
35 class Packet;
36 class JLPacket;
37 class JVMPacket;
38 class SocketRoutingQueue;
41 namespace Barry {
43 class Socket;
44 typedef std::auto_ptr<Socket> SocketHandle;
46 class BXEXPORT SocketZero
48 friend class Socket;
50 Usb::Device *m_dev;
51 SocketRoutingQueue *m_queue;
52 int m_writeEp, m_readEp;
53 uint8_t m_zeroSocketSequence;
55 uint32_t m_sequenceId;
57 // half open socket stata, for passwords
58 bool m_halfOpen;
59 uint32_t m_challengeSeed;
60 unsigned int m_remainingTries;
62 bool m_hideSequencePacket;
64 bool m_resetOnClose;
66 private:
67 static void AppendFragment(Data &whole, const Data &fragment);
68 static unsigned int MakeNextFragment(const Data &whole, Data &fragment,
69 unsigned int offset = 0);
70 void CheckSequence(uint16_t socket, const Data &seq);
72 void SendOpen(uint16_t socket, Data &receive);
73 void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
75 // Raw send and receive functions, used for all low level
76 // communication to the USB level.
77 void RawSend(Data &send, int timeout = -1);
78 void RawReceive(Data &receive, int timeout = -1);
80 protected:
81 bool SequencePacket(const Data &data);
82 bool IsSequencePacketHidden() { return m_hideSequencePacket; }
84 public:
85 void SetResetOnClose(bool flag) { m_resetOnClose = flag; }
86 void HideSequencePacket(bool flag) { m_hideSequencePacket = flag; }
87 explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
88 uint8_t zeroSocketSequenceStart = 0);
89 SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
90 uint8_t zeroSocketSequenceStart = 0);
91 ~SocketZero();
93 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
95 void SetRoutingQueue(SocketRoutingQueue &queue);
96 void UnlinkRoutingQueue();
98 // Send functions for socket 0 only.
99 // These functions will overwrite:
100 // - the zeroSocketSequence byte *inside* the packet
101 // - the socket number to 0
103 void Send(Data &send, int timeout = -1); // send only
104 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
105 void Send(Barry::Packet &packet, int timeout = -1);
106 void Receive(Data &receive, int timeout = -1);
108 // Opens a new socket and returns a Socket object to manage it
109 SocketHandle Open(uint16_t socket, const char *password = 0);
110 void Close(Socket &socket);
112 // Lower level USB operations
113 void ClearHalt();
118 // Socket class
120 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
121 /// By default, provides raw send/receive access, as well as packet
122 /// writing on socket 0, which is always open.
124 /// There are Open and Close members to open data sockets which are used
125 /// to transfer data to and from the device.
127 /// The destructor will close any non-0 open sockets automatically.
129 /// Requires an active Usb::Device object to work on.
131 class BXEXPORT Socket
133 friend class SocketZero;
135 SocketZero *m_zero;
136 uint16_t m_socket;
137 uint8_t m_closeFlag;
139 bool m_registered;
141 protected:
142 void CheckSequence(const Data &seq);
143 void ForceClosed();
145 Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
147 public:
148 ~Socket();
150 uint16_t GetSocket() const { return m_socket; }
151 uint8_t GetCloseFlag() const { return m_closeFlag; }
153 void Close();
155 // Send and Receive are available before Open...
156 // an unopened socket defaults to socket 0, which you need
157 // in order to set the blackberry mode
158 // The send function will overwrite the zeroSocketSequence byte
159 // *inside* the packet, if the current m_socket is 0.
160 void Send(Data &send, int timeout = -1); // send only
161 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
162 void Send(Barry::Packet &packet, int timeout = -1);
163 void Receive(Data &receive, int timeout = -1);
164 void ReceiveData(Data &receive, int timeout = -1);
166 // Lower level USB operations
167 void ClearHalt();
169 // sends the send packet down to the device, fragmenting if
170 // necessary, and returns the response in receive, defragmenting
171 // if needed
172 // Blocks until response received or timed out in Usb::Device
173 void Packet(Data &send, Data &receive, int timeout = -1);
174 void Packet(Barry::Packet &packet, int timeout = -1);
175 void Packet(Barry::JLPacket &packet, int timeout = -1);
176 void Packet(Barry::JVMPacket &packet, int timeout = -1);
178 // Use this function to send packet to JVM instead of Packet function
179 void InitSequence(int timeout = -1);
180 void PacketJVM(Data &send, Data &receive, int timeout = -1);
182 // Use this function to send data packet instead of Packet function
183 // Indeed, Packet function is used to send command (and not data)
184 void PacketData(Data &send, Data &receive, int timeout = -1);
186 // some handy wrappers for the Packet() interface
187 void NextRecord(Data &receive);
189 // Register a callback for incoming data from the device.
190 // This function assumes that this socket is based on a socketZero
191 // that has a SocketRoutingQueue, otherwise throws logic_error.
192 void RegisterInterest(std::tr1::shared_ptr<SocketRoutingQueue::SocketDataHandler> handler);
193 void UnregisterInterest();
196 // This function is quickly written
197 // It's very durty :( (but it's usefull to test...)
198 void HideSequencePacket(bool flag) { m_zero->HideSequencePacket(flag); }
202 } // namespace Barry
204 #endif