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__
31 // forward declarations
32 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
;
66 static void AppendFragment(Data
&whole
, const Data
&fragment
);
67 static unsigned int MakeNextFragment(const Data
&whole
, Data
&fragment
,
68 unsigned int offset
= 0);
69 void CheckSequence(uint16_t socket
, const Data
&seq
);
71 void SendOpen(uint16_t socket
, Data
&receive
);
72 void SendPasswordHash(uint16_t socket
, const char *password
, Data
&receive
);
74 // Raw send and receive functions, used for all low level
75 // communication to the USB level.
76 void RawSend(Data
&send
, int timeout
= -1);
77 void RawReceive(Data
&receive
, int timeout
= -1);
80 bool IsSequencePacketHidden() { return false; }
83 explicit SocketZero(SocketRoutingQueue
&queue
, int writeEndpoint
,
84 uint8_t zeroSocketSequenceStart
= 0);
85 SocketZero(Usb::Device
&dev
, int writeEndpoint
, int readEndpoint
,
86 uint8_t zeroSocketSequenceStart
= 0);
89 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence
; }
91 void SetRoutingQueue(SocketRoutingQueue
&queue
);
92 void UnlinkRoutingQueue();
94 // Send functions for socket 0 only.
95 // These functions will overwrite:
96 // - the zeroSocketSequence byte *inside* the packet
97 // - the socket number to 0
99 void Send(Data
&send
, int timeout
= -1); // send only
100 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
101 void Send(Barry::Packet
&packet
, int timeout
= -1);
102 void Receive(Data
&receive
, int timeout
= -1);
104 // Opens a new socket and returns a Socket object to manage it
105 SocketHandle
Open(uint16_t socket
, const char *password
= 0);
106 void Close(Socket
&socket
);
109 class BXEXPORT SocketBase
114 void CheckSequence(const Data
&seq
);
118 : m_resetOnClose(false)
122 virtual ~SocketBase();
125 // Virtual Socket API
127 virtual void Close() = 0;
129 // FIXME - do I need RawSend? Or just a good fragmenter?
130 virtual void RawSend(Data
&send
, int timeout
= -1) = 0;
131 virtual void SyncSend(Data
&send
, int timeout
= -1) = 0;
132 virtual void Receive(Data
&receive
, int timeout
= -1) = 0;
134 virtual void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler
= Barry::SocketRoutingQueue::SocketDataHandlerPtr()) = 0;
135 virtual void UnregisterInterest() = 0;
137 void ResetOnClose(bool reset
= true) { m_resetOnClose
= reset
; }
138 bool IsResetOnClose() const { return m_resetOnClose
; }
141 // Convenience functions that just call the virtuals above
143 void DBFragSend(Data
&send
, int timeout
= -1);
144 void Send(Data
&send
, Data
&receive
, int timeout
= -1);
145 void Send(Barry::Packet
&packet
, int timeout
= -1);
148 // Protocol based functions... all use the above virtual functions
151 // sends the send packet down to the device, fragmenting if
152 // necessary, and returns the response in receive, defragmenting
154 // Blocks until response received or timed out in Usb::Device
155 void Packet(Data
&send
, Data
&receive
, int timeout
= -1);
156 void Packet(Barry::Packet
&packet
, int timeout
= -1);
157 void Packet(Barry::JLPacket
&packet
, int timeout
= -1);
158 void Packet(Barry::JVMPacket
&packet
, int timeout
= -1);
160 // Use this function to send packet to JVM instead of Packet function
162 void PacketJVM(Data
&send
, Data
&receive
, int timeout
= -1);
164 // Use this function to send data packet instead of Packet function
165 // Indeed, Packet function is used to send command (and not data)
167 void PacketData(Data
&send
, Data
&receive
, bool done_on_sequence
,
170 // some handy wrappers for the Packet() interface
171 void NextRecord(Data
&receive
);
177 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
178 /// By default, provides raw send/receive access, as well as packet
179 /// writing on socket 0, which is always open.
181 /// There are Open and Close members to open data sockets which are used
182 /// to transfer data to and from the device.
184 /// The destructor will close any non-0 open sockets automatically.
186 /// Requires an active Usb::Device object to work on.
188 class BXEXPORT Socket
: public SocketBase
190 friend class SocketZero
;
199 std::auto_ptr
<Data
> m_sequence
;
204 // These "Local" function are non-virtual worker functions,
205 // which are safe to be called from the destructor.
206 // If you override the virtual versions in your derived class,
207 // make sure your "Local" versions call us.
209 void LocalUnregisterInterest();
211 uint16_t GetSocket() const { return m_socket
; }
212 uint8_t GetCloseFlag() const { return m_closeFlag
; }
214 Socket(SocketZero
&zero
, uint16_t socket
, uint8_t closeFlag
);
222 void Close() { LocalClose(); }
223 void RawSend(Data
&send
, int timeout
= -1);
224 void SyncSend(Data
&send
, int timeout
= -1);
225 void Receive(Data
&receive
, int timeout
= -1);
228 // Register a callback for incoming data from the device.
229 // This function assumes that this socket is based on a socketZero
230 // that has a SocketRoutingQueue, otherwise throws logic_error.
231 // It also assumes that nothing has been registered before.
232 // If you wish to re-register, call UnregisterInterest() first,
233 // which is safe to call as many times as you like.
234 void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler
= Barry::SocketRoutingQueue::SocketDataHandlerPtr());
235 void UnregisterInterest() { LocalUnregisterInterest(); }