Tree-wide cleanup of trailing whitespace
[barry.git] / src / socket.h
blobfc415a90057355bd54b1361762dd14d365bda8cc
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2009, 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"
31 // forward declarations
32 namespace Usb { class Device; }
33 namespace Barry {
34 class Data;
35 class Packet;
36 class JLPacket;
37 class SocketRoutingQueue;
40 namespace Barry {
42 class Socket;
43 typedef std::auto_ptr<Socket> SocketHandle;
45 class BXEXPORT SocketZero
47 friend class Socket;
49 Usb::Device *m_dev;
50 SocketRoutingQueue *m_queue;
51 int m_writeEp, m_readEp;
52 uint8_t m_zeroSocketSequence;
54 uint32_t m_sequenceId;
56 // half open socket stata, for passwords
57 bool m_halfOpen;
58 uint32_t m_challengeSeed;
59 unsigned int m_remainingTries;
61 bool m_sequencePacket;
63 private:
64 static void AppendFragment(Data &whole, const Data &fragment);
65 static unsigned int MakeNextFragment(const Data &whole, Data &fragment,
66 unsigned int offset = 0);
67 void CheckSequence(uint16_t socket, const Data &seq);
69 void SendOpen(uint16_t socket, Data &receive);
70 void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
72 // Raw send and receive functions, used for all low level
73 // communication to the USB level.
74 void RawSend(Data &send, int timeout = -1);
75 void RawReceive(Data &receive, int timeout = -1);
77 protected:
78 bool SequencePacket(const Data &data);
79 void SetSequencePacket(bool flag) { m_sequencePacket = flag; }
80 bool GetSequencePacket() { return m_sequencePacket; }
82 public:
83 explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
84 uint8_t zeroSocketSequenceStart = 0);
85 SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
86 uint8_t zeroSocketSequenceStart = 0);
87 ~SocketZero();
89 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
91 void SetRoutingQueue(SocketRoutingQueue &queue);
92 void UnlinkRoutingQueue();
94 // Send functions for socket 0 only.
95 // These functions will overwrite:
96 // - the zeroSocketSequence byte *inside* the packet
97 // - the socket number to 0
99 void Send(Data &send, int timeout = -1); // send only
100 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
101 void Send(Barry::Packet &packet, int timeout = -1);
102 // void Receive(Data &receive, int timeout = -1);
104 // Opens a new socket and returns a Socket object to manage it
105 SocketHandle Open(uint16_t socket, const char *password = 0);
106 void Close(Socket &socket);
111 // Socket class
113 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
114 /// By default, provides raw send/receive access, as well as packet
115 /// writing on socket 0, which is always open.
117 /// There are Open and Close members to open data sockets which are used
118 /// to transfer data to and from the device.
120 /// The destructor will close any non-0 open sockets automatically.
122 /// Requires an active Usb::Device object to work on.
124 class BXEXPORT Socket
126 friend class SocketZero;
128 SocketZero *m_zero;
129 uint16_t m_socket;
130 uint8_t m_closeFlag;
132 bool m_registered;
134 protected:
135 void CheckSequence(const Data &seq);
136 void ForceClosed();
138 Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
140 public:
141 ~Socket();
143 uint16_t GetSocket() const { return m_socket; }
144 uint8_t GetCloseFlag() const { return m_closeFlag; }
146 void Close();
148 // Send and Receive are available before Open...
149 // an unopened socket defaults to socket 0, which you need
150 // in order to set the blackberry mode
151 // The send function will overwrite the zeroSocketSequence byte
152 // *inside* the packet, if the current m_socket is 0.
153 void Send(Data &send, int timeout = -1); // send only
154 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
155 void Send(Barry::Packet &packet, int timeout = -1);
156 void Receive(Data &receive, int timeout = -1);
158 // sends the send packet down to the device, fragmenting if
159 // necessary, and returns the response in receive, defragmenting
160 // if needed
161 // Blocks until response received or timed out in Usb::Device
162 void Packet(Data &send, Data &receive, int timeout = -1);
163 void Packet(Barry::Packet &packet, int timeout = -1);
164 void Packet(Barry::JLPacket &packet, int timeout = -1);
166 // Use this function to send data packet instead of Packet function
167 // Indeed, Packet function is used to send command (and not data)
168 void PacketData(Data &send, Data &receive, int timeout = -1);
170 // some handy wrappers for the Packet() interface
171 void NextRecord(Data &receive);
173 // Register a callback for incoming data from the device.
174 // This function assumes that this socket is based on a socketZero
175 // that has a SocketRoutingQueue, otherwise throws logic_error.
176 void RegisterInterest(SocketRoutingQueue::SocketDataHandler handler, void *context);
177 void UnregisterInterest();
180 // This function is quickly written
181 // It's very durty :( (but it's usefull to test...)
182 void SetSequencePacket(bool flag) { m_zero->SetSequencePacket(flag); }
186 } // namespace Barry
188 #endif