lib: Adding ability for data handlers to be registered as soon as the socket is opened.
[barry.git] / src / socket.h
blob52cf0c6415d9fcee7d0a0efed0d753d745934a9d
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2012, 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__
25 #include "dll.h"
26 #include <stdint.h>
27 #include <queue>
28 #include <memory>
29 #include "router.h"
30 #include "data.h"
32 // forward declarations
33 namespace Usb { class Device; }
34 namespace Barry {
35 class Packet;
36 class JLPacket;
37 class JVMPacket;
38 class SocketRoutingQueue;
41 namespace Barry {
43 class SocketBase;
44 class Socket;
45 typedef std::auto_ptr<SocketBase> SocketHandle;
47 class BXEXPORT SocketZero
49 friend class SocketBase;
50 friend class Socket;
52 Usb::Device *m_dev;
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
60 bool m_halfOpen;
61 uint32_t m_challengeSeed;
62 unsigned int m_remainingTries;
63 bool m_modeSequencePacketSeen;
65 // pushback for out of order sequence packets
66 Data m_pushback_buffer;
67 bool m_pushback;
69 private:
70 static void AppendFragment(Data &whole, const Data &fragment);
71 static unsigned int MakeNextFragment(const Data &whole, Data &fragment,
72 unsigned int offset = 0);
73 void CheckSequence(uint16_t socket, const Data &seq);
75 void SendOpen(uint16_t socket, Data &receive);
76 void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
78 // Raw send and receive functions, used for all low level
79 // communication to the USB level.
80 void RawSend(Data &send, int timeout = -1);
81 void RawReceive(Data &receive, int timeout = -1);
83 void Pushback(const Data &buf);
85 protected:
86 bool IsSequencePacketHidden() { return false; }
88 public:
89 explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
90 uint8_t zeroSocketSequenceStart = 0);
91 SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
92 uint8_t zeroSocketSequenceStart = 0);
93 ~SocketZero();
95 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
97 void SetRoutingQueue(SocketRoutingQueue &queue);
98 void UnlinkRoutingQueue();
100 // Send functions for socket 0 only.
101 // These functions will overwrite:
102 // - the zeroSocketSequence byte *inside* the packet
103 // - the socket number to 0
105 void Send(Data &send, int timeout = -1); // send only
106 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
107 void Send(Barry::Packet &packet, int timeout = -1);
108 void Receive(Data &receive, int timeout = -1);
110 // Opens a new socket and returns a Socket object to manage it
111 SocketHandle Open(uint16_t socket, const char *password = 0);
112 // Opens a new socket and returns a Socket object to manage it
113 // Registers the provided handler interested in socket data.
114 // This avoids the race where calling RegisterInterest immediately
115 // on the returned SocketHandle can still miss incoming data.
116 SocketHandle Open(
117 Barry::SocketRoutingQueue::SocketDataHandlerPtr handler,
118 uint16_t socket, const char *password = 0);
119 void Close(Socket &socket);
122 class BXEXPORT SocketBase
124 bool m_resetOnClose;
126 protected:
127 void CheckSequence(const Data &seq);
129 public:
130 SocketBase()
131 : m_resetOnClose(false)
135 virtual ~SocketBase();
138 // Virtual Socket API
140 virtual void Close() = 0;
142 // FIXME - do I need RawSend? Or just a good fragmenter?
143 virtual void RawSend(Data &send, int timeout = -1) = 0;
144 virtual void SyncSend(Data &send, int timeout = -1) = 0;
145 virtual void Receive(Data &receive, int timeout = -1) = 0;
147 virtual void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr()) = 0;
148 virtual void UnregisterInterest() = 0;
150 void ResetOnClose(bool reset = true) { m_resetOnClose = reset; }
151 bool IsResetOnClose() const { return m_resetOnClose; }
154 // Convenience functions that just call the virtuals above
156 void DBFragSend(Data &send, int timeout = -1);
157 void Send(Data &send, Data &receive, int timeout = -1);
158 void Send(Barry::Packet &packet, int timeout = -1);
161 // Protocol based functions... all use the above virtual functions
164 // sends the send packet down to the device, fragmenting if
165 // necessary, and returns the response in receive, defragmenting
166 // if needed
167 // Blocks until response received or timed out in Usb::Device
168 void Packet(Data &send, Data &receive, int timeout = -1);
169 void Packet(Barry::Packet &packet, int timeout = -1);
170 void Packet(Barry::JLPacket &packet, int timeout = -1);
171 void Packet(Barry::JVMPacket &packet, int timeout = -1);
173 // Use this function to send packet to JVM instead of Packet function
174 // FIXME
175 void PacketJVM(Data &send, Data &receive, int timeout = -1);
177 // Use this function to send data packet instead of Packet function
178 // Indeed, Packet function is used to send command (and not data)
179 // FIXME
180 void PacketData(Data &send, Data &receive, bool done_on_sequence,
181 int timeout = -1);
183 // some handy wrappers for the Packet() interface
184 void NextRecord(Data &receive);
188 // Socket class
190 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
191 /// By default, provides raw send/receive access, as well as packet
192 /// writing on socket 0, which is always open.
194 /// There are Open and Close members to open data sockets which are used
195 /// to transfer data to and from the device.
197 /// The destructor will close any non-0 open sockets automatically.
199 /// Requires an active Usb::Device object to work on.
201 class BXEXPORT Socket : public SocketBase
203 friend class SocketZero;
205 SocketZero *m_zero;
206 uint16_t m_socket;
207 uint8_t m_closeFlag;
209 bool m_registered;
211 // buffer data
212 std::auto_ptr<Data> m_sequence;
214 protected:
215 void ForceClosed();
217 // These "Local" function are non-virtual worker functions,
218 // which are safe to be called from the destructor.
219 // If you override the virtual versions in your derived class,
220 // make sure your "Local" versions call us.
221 void LocalClose();
222 void LocalUnregisterInterest();
224 uint16_t GetSocket() const { return m_socket; }
225 uint8_t GetCloseFlag() const { return m_closeFlag; }
227 Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
229 public:
230 ~Socket();
233 // virtual overrides
235 void Close() { LocalClose(); }
236 void RawSend(Data &send, int timeout = -1);
237 void SyncSend(Data &send, int timeout = -1);
238 void Receive(Data &receive, int timeout = -1);
241 // Register a callback for incoming data from the device.
242 // This function assumes that this socket is based on a socketZero
243 // that has a SocketRoutingQueue, otherwise throws logic_error.
244 // It also assumes that nothing has been registered before.
245 // If you wish to re-register, call UnregisterInterest() first,
246 // which is safe to call as many times as you like.
247 void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr());
248 void UnregisterInterest() { LocalUnregisterInterest(); }
252 } // namespace Barry
254 #endif