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 explicit SocketZero(SocketRoutingQueue
&queue
, int writeEndpoint
,
76 uint8_t zeroSocketSequenceStart
= 0);
77 SocketZero(Usb::Device
&dev
, int writeEndpoint
, int readEndpoint
,
78 uint8_t zeroSocketSequenceStart
= 0);
81 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence
; }
83 void SetRoutingQueue(SocketRoutingQueue
&queue
);
84 void UnlinkRoutingQueue();
86 // Send functions for socket 0 only.
87 // These functions will overwrite:
88 // - the zeroSocketSequence byte *inside* the packet
89 // - the socket number to 0
91 void Send(Data
&send
, int timeout
= -1); // send only
92 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
93 void Send(Barry::Packet
&packet
, int timeout
= -1);
94 // void Receive(Data &receive, int timeout = -1);
96 // Opens a new socket and returns a Socket object to manage it
97 SocketHandle
Open(uint16_t socket
, const char *password
= 0);
98 void Close(Socket
&socket
);
105 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
106 /// By default, provides raw send/receive access, as well as packet
107 /// writing on socket 0, which is always open.
109 /// There are Open and Close members to open data sockets which are used
110 /// to transfer data to and from the device.
112 /// The destructor will close any non-0 open sockets automatically.
114 /// Requires an active Usb::Device object to work on.
116 class BXEXPORT Socket
118 friend class SocketZero
;
127 void CheckSequence(const Data
&seq
);
130 Socket(SocketZero
&zero
, uint16_t socket
, uint8_t closeFlag
);
135 uint16_t GetSocket() const { return m_socket
; }
136 uint8_t GetCloseFlag() const { return m_closeFlag
; }
140 // Send and Receive are available before Open...
141 // an unopened socket defaults to socket 0, which you need
142 // in order to set the blackberry mode
143 // The send function will overwrite the zeroSocketSequence byte
144 // *inside* the packet, if the current m_socket is 0.
145 void Send(Data
&send
, int timeout
= -1); // send only
146 void Send(Data
&send
, Data
&receive
, int timeout
= -1); // send+recv
147 void Send(Barry::Packet
&packet
, int timeout
= -1);
148 void Receive(Data
&receive
, int timeout
= -1);
150 // sends the send packet down to the device, fragmenting if
151 // necessary, and returns the response in receive, defragmenting
153 // Blocks until response received or timed out in Usb::Device
154 void Packet(Data
&send
, Data
&receive
, int timeout
= -1);
155 void Packet(Barry::Packet
&packet
, int timeout
= -1);
157 // some handy wrappers for the Packet() interface
158 void NextRecord(Data
&receive
);
160 // Register a callback for incoming data from the device.
161 // This function assumes that this socket is based on a socketZero
162 // that has a SocketRoutingQueue, otherwise throws logic_error.
163 void RegisterInterest(SocketRoutingQueue::SocketDataHandler handler
, void *context
);
164 void UnregisterInterest();