3 /// Class wrapper to encapsulate the Blackberry USB logical socket
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__
31 // forward declarations
32 namespace Usb
{ class Device
; }
38 class SocketRoutingQueue
;
44 typedef std::auto_ptr
<Socket
> SocketHandle
;
46 class BXEXPORT SocketZero
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
59 uint32_t m_challengeSeed
;
60 unsigned int m_remainingTries
;
62 bool m_hideSequencePacket
;
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);
81 bool SequencePacket(const Data
&data
);
82 bool IsSequencePacketHidden() { return m_hideSequencePacket
; }
85 explicit SocketZero(SocketRoutingQueue
&queue
, int writeEndpoint
,
86 uint8_t zeroSocketSequenceStart
= 0);
87 SocketZero(Usb::Device
&dev
, int writeEndpoint
, int readEndpoint
,
88 uint8_t zeroSocketSequenceStart
= 0);
91 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence
; }
93 void SetRoutingQueue(SocketRoutingQueue
&queue
);
94 void UnlinkRoutingQueue();
96 void SetResetOnClose(bool flag
) { m_resetOnClose
= flag
; }
97 void HideSequencePacket(bool flag
) { m_hideSequencePacket
= flag
; }
99 // Send functions for socket 0 only.
100 // These functions will overwrite:
101 // - the zeroSocketSequence byte *inside* the packet
102 // - the socket number to 0
104 void Send(Data
&send
, int timeout
= -1); // send only
105 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
106 void Send(Barry::Packet
&packet
, int timeout
= -1);
107 void Receive(Data
&receive
, int timeout
= -1);
109 // Opens a new socket and returns a Socket object to manage it
110 SocketHandle
Open(uint16_t socket
, const char *password
= 0);
111 void Close(Socket
&socket
);
113 // Lower level USB operations
121 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
122 /// By default, provides raw send/receive access, as well as packet
123 /// writing on socket 0, which is always open.
125 /// There are Open and Close members to open data sockets which are used
126 /// to transfer data to and from the device.
128 /// The destructor will close any non-0 open sockets automatically.
130 /// Requires an active Usb::Device object to work on.
132 class BXEXPORT Socket
134 friend class SocketZero
;
143 void CheckSequence(const Data
&seq
);
146 Socket(SocketZero
&zero
, uint16_t socket
, uint8_t closeFlag
);
151 uint16_t GetSocket() const { return m_socket
; }
152 uint8_t GetCloseFlag() const { return m_closeFlag
; }
156 // Send and Receive are available before Open...
157 // an unopened socket defaults to socket 0, which you need
158 // in order to set the blackberry mode
159 // The send function will overwrite the zeroSocketSequence byte
160 // *inside* the packet, if the current m_socket is 0.
161 void Send(Data
&send
, int timeout
= -1); // send only
162 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
163 void Send(Barry::Packet
&packet
, int timeout
= -1);
164 void Receive(Data
&receive
, int timeout
= -1);
165 void ReceiveData(Data
&receive
, int timeout
= -1);
167 // Lower level USB operations
170 // sends the send packet down to the device, fragmenting if
171 // necessary, and returns the response in receive, defragmenting
173 // Blocks until response received or timed out in Usb::Device
174 void Packet(Data
&send
, Data
&receive
, int timeout
= -1);
175 void Packet(Barry::Packet
&packet
, int timeout
= -1);
176 void Packet(Barry::JLPacket
&packet
, int timeout
= -1);
177 void Packet(Barry::JVMPacket
&packet
, int timeout
= -1);
179 // Use this function to send packet to JVM instead of Packet function
180 void InitSequence(int timeout
= -1);
181 void PacketJVM(Data
&send
, Data
&receive
, int timeout
= -1);
183 // Use this function to send data packet instead of Packet function
184 // Indeed, Packet function is used to send command (and not data)
185 void PacketData(Data
&send
, Data
&receive
, int timeout
= -1);
187 // some handy wrappers for the Packet() interface
188 void NextRecord(Data
&receive
);
190 // Register a callback for incoming data from the device.
191 // This function assumes that this socket is based on a socketZero
192 // that has a SocketRoutingQueue, otherwise throws logic_error.
193 void RegisterInterest(std::tr1::shared_ptr
<SocketRoutingQueue::SocketDataHandler
> handler
);
194 void UnregisterInterest();
197 // This function is quickly written
198 // It's very durty :( (but it's usefull to test...)
199 void HideSequencePacket(bool flag
) { m_zero
->HideSequencePacket(flag
); }