tzwrapper.cc: fixed use of iterator after erase
[barry.git] / src / socket.h
blobb760bef14377605eb4be2aa7cdffdc15939f8292
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 // Used to notify the socket when the connection is being opened
151 virtual void Opening(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler) = 0;
152 // Used to notify the socket when the connection has been successfully opened
153 virtual void Opened() = 0;
155 void ResetOnClose(bool reset = true) { m_resetOnClose = reset; }
156 bool IsResetOnClose() const { return m_resetOnClose; }
159 // Convenience functions that just call the virtuals above
161 void DBFragSend(Data &send, int timeout = -1);
162 void Send(Data &send, Data &receive, int timeout = -1);
163 void Send(Barry::Packet &packet, int timeout = -1);
166 // Protocol based functions... all use the above virtual functions
169 // sends the send packet down to the device, fragmenting if
170 // necessary, and returns the response in receive, defragmenting
171 // if needed
172 // Blocks until response received or timed out in Usb::Device
173 void Packet(Data &send, Data &receive, int timeout = -1);
174 void Packet(Barry::Packet &packet, int timeout = -1);
175 void Packet(Barry::JLPacket &packet, int timeout = -1);
176 void Packet(Barry::JVMPacket &packet, int timeout = -1);
178 // Use this function to send packet to JVM instead of Packet function
179 // FIXME
180 void PacketJVM(Data &send, Data &receive, int timeout = -1);
182 // Use this function to send data packet instead of Packet function
183 // Indeed, Packet function is used to send command (and not data)
184 // FIXME
185 void PacketData(Data &send, Data &receive, bool done_on_sequence,
186 int timeout = -1);
188 // some handy wrappers for the Packet() interface
189 void NextRecord(Data &receive);
193 // Socket class
195 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
196 /// By default, provides raw send/receive access, as well as packet
197 /// writing on socket 0, which is always open.
199 /// There are Open and Close members to open data sockets which are used
200 /// to transfer data to and from the device.
202 /// The destructor will close any non-0 open sockets automatically.
204 /// Requires an active Usb::Device object to work on.
206 class BXEXPORT Socket : public SocketBase
208 friend class SocketZero;
210 SocketZero *m_zero;
211 uint16_t m_socket;
212 uint8_t m_closeFlag;
214 bool m_registered;
216 // buffer data
217 std::auto_ptr<Data> m_sequence;
219 protected:
220 void ForceClosed();
222 // These "Local" function are non-virtual worker functions,
223 // which are safe to be called from the destructor.
224 // If you override the virtual versions in your derived class,
225 // make sure your "Local" versions call us.
226 void LocalClose();
227 void LocalUnregisterInterest();
229 uint16_t GetSocket() const { return m_socket; }
230 uint8_t GetCloseFlag() const { return m_closeFlag; }
232 Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
234 public:
235 ~Socket();
238 // virtual overrides
240 void Close() { LocalClose(); }
241 void RawSend(Data &send, int timeout = -1);
242 void SyncSend(Data &send, int timeout = -1);
243 void Receive(Data &receive, int timeout = -1);
246 // Register a callback for incoming data from the device.
247 // This function assumes that this socket is based on a socketZero
248 // that has a SocketRoutingQueue, otherwise throws logic_error.
249 // It also assumes that nothing has been registered before.
250 // If you wish to re-register, call UnregisterInterest() first,
251 // which is safe to call as many times as you like.
252 void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr());
253 void UnregisterInterest() { LocalUnregisterInterest(); }
255 void Opening(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler);
256 void Opened();
260 } // namespace Barry
262 #endif