lib: fixed parsing of recurring VEVENTS: DAILY and interval support
[barry/progweb.git] / src / socket.h
blob9b96fb9a2110082efdaef10f5561f49716ab79ea
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 void Close(Socket &socket);
115 class BXEXPORT SocketBase
117 bool m_resetOnClose;
119 protected:
120 void CheckSequence(const Data &seq);
122 public:
123 SocketBase()
124 : m_resetOnClose(false)
128 virtual ~SocketBase();
131 // Virtual Socket API
133 virtual void Close() = 0;
135 // FIXME - do I need RawSend? Or just a good fragmenter?
136 virtual void RawSend(Data &send, int timeout = -1) = 0;
137 virtual void SyncSend(Data &send, int timeout = -1) = 0;
138 virtual void Receive(Data &receive, int timeout = -1) = 0;
140 virtual void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr()) = 0;
141 virtual void UnregisterInterest() = 0;
143 void ResetOnClose(bool reset = true) { m_resetOnClose = reset; }
144 bool IsResetOnClose() const { return m_resetOnClose; }
147 // Convenience functions that just call the virtuals above
149 void DBFragSend(Data &send, int timeout = -1);
150 void Send(Data &send, Data &receive, int timeout = -1);
151 void Send(Barry::Packet &packet, int timeout = -1);
154 // Protocol based functions... all use the above virtual functions
157 // sends the send packet down to the device, fragmenting if
158 // necessary, and returns the response in receive, defragmenting
159 // if needed
160 // Blocks until response received or timed out in Usb::Device
161 void Packet(Data &send, Data &receive, int timeout = -1);
162 void Packet(Barry::Packet &packet, int timeout = -1);
163 void Packet(Barry::JLPacket &packet, int timeout = -1);
164 void Packet(Barry::JVMPacket &packet, int timeout = -1);
166 // Use this function to send packet to JVM instead of Packet function
167 // FIXME
168 void PacketJVM(Data &send, Data &receive, int timeout = -1);
170 // Use this function to send data packet instead of Packet function
171 // Indeed, Packet function is used to send command (and not data)
172 // FIXME
173 void PacketData(Data &send, Data &receive, bool done_on_sequence,
174 int timeout = -1);
176 // some handy wrappers for the Packet() interface
177 void NextRecord(Data &receive);
181 // Socket class
183 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
184 /// By default, provides raw send/receive access, as well as packet
185 /// writing on socket 0, which is always open.
187 /// There are Open and Close members to open data sockets which are used
188 /// to transfer data to and from the device.
190 /// The destructor will close any non-0 open sockets automatically.
192 /// Requires an active Usb::Device object to work on.
194 class BXEXPORT Socket : public SocketBase
196 friend class SocketZero;
198 SocketZero *m_zero;
199 uint16_t m_socket;
200 uint8_t m_closeFlag;
202 bool m_registered;
204 // buffer data
205 std::auto_ptr<Data> m_sequence;
207 protected:
208 void ForceClosed();
210 // These "Local" function are non-virtual worker functions,
211 // which are safe to be called from the destructor.
212 // If you override the virtual versions in your derived class,
213 // make sure your "Local" versions call us.
214 void LocalClose();
215 void LocalUnregisterInterest();
217 uint16_t GetSocket() const { return m_socket; }
218 uint8_t GetCloseFlag() const { return m_closeFlag; }
220 Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
222 public:
223 ~Socket();
226 // virtual overrides
228 void Close() { LocalClose(); }
229 void RawSend(Data &send, int timeout = -1);
230 void SyncSend(Data &send, int timeout = -1);
231 void Receive(Data &receive, int timeout = -1);
234 // Register a callback for incoming data from the device.
235 // This function assumes that this socket is based on a socketZero
236 // that has a SocketRoutingQueue, otherwise throws logic_error.
237 // It also assumes that nothing has been registered before.
238 // If you wish to re-register, call UnregisterInterest() first,
239 // which is safe to call as many times as you like.
240 void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr());
241 void UnregisterInterest() { LocalUnregisterInterest(); }
245 } // namespace Barry
247 #endif