3 /// Class wrapper to encapsulate the Blackberry USB logical socket
7 Copyright (C) 2005-2008, 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
; }
36 class SocketRoutingQueue
;
42 typedef std::auto_ptr
<Socket
> SocketHandle
;
44 class BXEXPORT SocketZero
49 SocketRoutingQueue
*m_queue
;
50 int m_writeEp
, m_readEp
;
51 uint8_t m_zeroSocketSequence
;
53 uint32_t m_sequenceId
;
55 // half open socket stata, for passwords
57 uint32_t m_challengeSeed
;
58 unsigned int m_remainingTries
;
61 static void AppendFragment(Data
&whole
, const Data
&fragment
);
62 static unsigned int MakeNextFragment(const Data
&whole
, Data
&fragment
,
63 unsigned int offset
= 0);
64 void CheckSequence(uint16_t socket
, const Data
&seq
);
66 void SendOpen(uint16_t socket
, Data
&receive
);
67 void SendPasswordHash(uint16_t socket
, const char *password
, Data
&receive
);
69 // Raw send and receive functions, used for all low level
70 // communication to the USB level.
71 void RawSend(Data
&send
, int timeout
= -1);
72 void RawReceive(Data
&receive
, int timeout
= -1);
75 bool SequencePacket(const Data
&data
);
78 explicit SocketZero(SocketRoutingQueue
&queue
, int writeEndpoint
,
79 uint8_t zeroSocketSequenceStart
= 0);
80 SocketZero(Usb::Device
&dev
, int writeEndpoint
, int readEndpoint
,
81 uint8_t zeroSocketSequenceStart
= 0);
84 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence
; }
86 void SetRoutingQueue(SocketRoutingQueue
&queue
);
87 void UnlinkRoutingQueue();
89 // Send functions for socket 0 only.
90 // These functions will overwrite:
91 // - the zeroSocketSequence byte *inside* the packet
92 // - the socket number to 0
94 void Send(Data
&send
, int timeout
= -1); // send only
95 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
96 void Send(Barry::Packet
&packet
, int timeout
= -1);
97 // void Receive(Data &receive, int timeout = -1);
99 // Opens a new socket and returns a Socket object to manage it
100 SocketHandle
Open(uint16_t socket
, const char *password
= 0);
101 void Close(Socket
&socket
);
108 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
109 /// By default, provides raw send/receive access, as well as packet
110 /// writing on socket 0, which is always open.
112 /// There are Open and Close members to open data sockets which are used
113 /// to transfer data to and from the device.
115 /// The destructor will close any non-0 open sockets automatically.
117 /// Requires an active Usb::Device object to work on.
119 class BXEXPORT Socket
121 friend class SocketZero
;
130 void CheckSequence(const Data
&seq
);
133 Socket(SocketZero
&zero
, uint16_t socket
, uint8_t closeFlag
);
138 uint16_t GetSocket() const { return m_socket
; }
139 uint8_t GetCloseFlag() const { return m_closeFlag
; }
143 // Send and Receive are available before Open...
144 // an unopened socket defaults to socket 0, which you need
145 // in order to set the blackberry mode
146 // The send function will overwrite the zeroSocketSequence byte
147 // *inside* the packet, if the current m_socket is 0.
148 void Send(Data
&send
, int timeout
= -1); // send only
149 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
150 void Send(Barry::Packet
&packet
, int timeout
= -1);
151 void Receive(Data
&receive
, int timeout
= -1);
153 // sends the send packet down to the device, fragmenting if
154 // necessary, and returns the response in receive, defragmenting
156 // Blocks until response received or timed out in Usb::Device
157 void Packet(Data
&send
, Data
&receive
, int timeout
= -1);
158 void Packet(Barry::Packet
&packet
, int timeout
= -1);
160 // some handy wrappers for the Packet() interface
161 void NextRecord(Data
&receive
);
163 // Register a callback for incoming data from the device.
164 // This function assumes that this socket is based on a socketZero
165 // that has a SocketRoutingQueue, otherwise throws logic_error.
166 void RegisterInterest(SocketRoutingQueue::SocketDataHandler handler
, void *context
);
167 void UnregisterInterest();