3 /// Class wrapper to encapsulate the Blackberry USB logical socket
7 Copyright (C) 2005-2009, 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 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);
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
);
117 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
118 /// By default, provides raw send/receive access, as well as packet
119 /// writing on socket 0, which is always open.
121 /// There are Open and Close members to open data sockets which are used
122 /// to transfer data to and from the device.
124 /// The destructor will close any non-0 open sockets automatically.
126 /// Requires an active Usb::Device object to work on.
128 class BXEXPORT Socket
130 friend class SocketZero
;
139 void CheckSequence(const Data
&seq
);
142 Socket(SocketZero
&zero
, uint16_t socket
, uint8_t closeFlag
);
147 uint16_t GetSocket() const { return m_socket
; }
148 uint8_t GetCloseFlag() const { return m_closeFlag
; }
152 // Send and Receive are available before Open...
153 // an unopened socket defaults to socket 0, which you need
154 // in order to set the blackberry mode
155 // The send function will overwrite the zeroSocketSequence byte
156 // *inside* the packet, if the current m_socket is 0.
157 void Send(Data
&send
, int timeout
= -1); // send only
158 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
159 void Send(Barry::Packet
&packet
, int timeout
= -1);
160 void Receive(Data
&receive
, int timeout
= -1);
161 void ReceiveData(Data
&receive
, int timeout
= -1);
163 // sends the send packet down to the device, fragmenting if
164 // necessary, and returns the response in receive, defragmenting
166 // Blocks until response received or timed out in Usb::Device
167 void Packet(Data
&send
, Data
&receive
, int timeout
= -1);
168 void Packet(Barry::Packet
&packet
, int timeout
= -1);
169 void Packet(Barry::JLPacket
&packet
, int timeout
= -1);
170 void Packet(Barry::JVMPacket
&packet
, int timeout
= -1);
172 // Use this function to send packet to JVM instead of Packet function
173 void InitSequence(int timeout
= -1);
174 void PacketJVM(Data
&send
, Data
&receive
, int timeout
= -1);
176 // Use this function to send data packet instead of Packet function
177 // Indeed, Packet function is used to send command (and not data)
178 void PacketData(Data
&send
, Data
&receive
, int timeout
= -1);
180 // some handy wrappers for the Packet() interface
181 void NextRecord(Data
&receive
);
183 // Register a callback for incoming data from the device.
184 // This function assumes that this socket is based on a socketZero
185 // that has a SocketRoutingQueue, otherwise throws logic_error.
186 void RegisterInterest(SocketRoutingQueue::SocketDataHandler handler
, void *context
);
187 void UnregisterInterest();
190 // This function is quickly written
191 // It's very durty :( (but it's usefull to test...)
192 void HideSequencePacket(bool flag
) { m_zero
->HideSequencePacket(flag
); }