3 /// Class wrapper to encapsulate the Blackberry USB logical socket
7 Copyright (C) 2005-2011, 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__
32 // forward declarations
33 namespace Usb
{ class Device
; }
38 class SocketRoutingQueue
;
45 typedef std::auto_ptr
<SocketBase
> SocketHandle
;
47 class BXEXPORT SocketZero
49 friend class SocketBase
;
53 SocketRoutingQueue
*m_queue
;
54 int m_writeEp
, m_readEp
;
55 uint8_t m_zeroSocketSequence
;
57 uint32_t m_sequenceId
;
59 // half open socket stata, for passwords
61 uint32_t m_challengeSeed
;
62 unsigned int m_remainingTries
;
63 bool m_modeSequencePacketSeen
;
65 // pushback for out of order sequence packets
66 Data m_pushback_buffer
;
70 static void AppendFragment(Data
&whole
, const Data
&fragment
);
71 static unsigned int MakeNextFragment(const Data
&whole
, Data
&fragment
,
72 unsigned int offset
= 0);
73 void CheckSequence(uint16_t socket
, const Data
&seq
);
75 void SendOpen(uint16_t socket
, Data
&receive
);
76 void SendPasswordHash(uint16_t socket
, const char *password
, Data
&receive
);
78 // Raw send and receive functions, used for all low level
79 // communication to the USB level.
80 void RawSend(Data
&send
, int timeout
= -1);
81 void RawReceive(Data
&receive
, int timeout
= -1);
83 void Pushback(const Data
&buf
);
86 bool IsSequencePacketHidden() { return false; }
89 explicit SocketZero(SocketRoutingQueue
&queue
, int writeEndpoint
,
90 uint8_t zeroSocketSequenceStart
= 0);
91 SocketZero(Usb::Device
&dev
, int writeEndpoint
, int readEndpoint
,
92 uint8_t zeroSocketSequenceStart
= 0);
95 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence
; }
97 void SetRoutingQueue(SocketRoutingQueue
&queue
);
98 void UnlinkRoutingQueue();
100 // Send functions for socket 0 only.
101 // These functions will overwrite:
102 // - the zeroSocketSequence byte *inside* the packet
103 // - the socket number to 0
105 void Send(Data
&send
, int timeout
= -1); // send only
106 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
107 void Send(Barry::Packet
&packet
, int timeout
= -1);
108 void Receive(Data
&receive
, int timeout
= -1);
110 // Opens a new socket and returns a Socket object to manage it
111 SocketHandle
Open(uint16_t socket
, const char *password
= 0);
112 void Close(Socket
&socket
);
115 class BXEXPORT SocketBase
120 void CheckSequence(const Data
&seq
);
124 : m_resetOnClose(false)
128 virtual ~SocketBase();
131 // Virtual Socket API
133 virtual void Close() = 0;
135 // FIXME - do I need RawSend? Or just a good fragmenter?
136 virtual void RawSend(Data
&send
, int timeout
= -1) = 0;
137 virtual void SyncSend(Data
&send
, int timeout
= -1) = 0;
138 virtual void Receive(Data
&receive
, int timeout
= -1) = 0;
140 virtual void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler
= Barry::SocketRoutingQueue::SocketDataHandlerPtr()) = 0;
141 virtual void UnregisterInterest() = 0;
143 void ResetOnClose(bool reset
= true) { m_resetOnClose
= reset
; }
144 bool IsResetOnClose() const { return m_resetOnClose
; }
147 // Convenience functions that just call the virtuals above
149 void DBFragSend(Data
&send
, int timeout
= -1);
150 void Send(Data
&send
, Data
&receive
, int timeout
= -1);
151 void Send(Barry::Packet
&packet
, int timeout
= -1);
154 // Protocol based functions... all use the above virtual functions
157 // sends the send packet down to the device, fragmenting if
158 // necessary, and returns the response in receive, defragmenting
160 // Blocks until response received or timed out in Usb::Device
161 void Packet(Data
&send
, Data
&receive
, int timeout
= -1);
162 void Packet(Barry::Packet
&packet
, int timeout
= -1);
163 void Packet(Barry::JLPacket
&packet
, int timeout
= -1);
164 void Packet(Barry::JVMPacket
&packet
, int timeout
= -1);
166 // Use this function to send packet to JVM instead of Packet function
168 void PacketJVM(Data
&send
, Data
&receive
, int timeout
= -1);
170 // Use this function to send data packet instead of Packet function
171 // Indeed, Packet function is used to send command (and not data)
173 void PacketData(Data
&send
, Data
&receive
, bool done_on_sequence
,
176 // some handy wrappers for the Packet() interface
177 void NextRecord(Data
&receive
);
183 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
184 /// By default, provides raw send/receive access, as well as packet
185 /// writing on socket 0, which is always open.
187 /// There are Open and Close members to open data sockets which are used
188 /// to transfer data to and from the device.
190 /// The destructor will close any non-0 open sockets automatically.
192 /// Requires an active Usb::Device object to work on.
194 class BXEXPORT Socket
: public SocketBase
196 friend class SocketZero
;
205 std::auto_ptr
<Data
> m_sequence
;
210 // These "Local" function are non-virtual worker functions,
211 // which are safe to be called from the destructor.
212 // If you override the virtual versions in your derived class,
213 // make sure your "Local" versions call us.
215 void LocalUnregisterInterest();
217 uint16_t GetSocket() const { return m_socket
; }
218 uint8_t GetCloseFlag() const { return m_closeFlag
; }
220 Socket(SocketZero
&zero
, uint16_t socket
, uint8_t closeFlag
);
228 void Close() { LocalClose(); }
229 void RawSend(Data
&send
, int timeout
= -1);
230 void SyncSend(Data
&send
, int timeout
= -1);
231 void Receive(Data
&receive
, int timeout
= -1);
234 // Register a callback for incoming data from the device.
235 // This function assumes that this socket is based on a socketZero
236 // that has a SocketRoutingQueue, otherwise throws logic_error.
237 // It also assumes that nothing has been registered before.
238 // If you wish to re-register, call UnregisterInterest() first,
239 // which is safe to call as many times as you like.
240 void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler
= Barry::SocketRoutingQueue::SocketDataHandlerPtr());
241 void UnregisterInterest() { LocalUnregisterInterest(); }