tools: added -i charset support to btardump
[barry.git] / src / socket.h
blob5762cb352d10169f0ba96a3a34ff5a50fad60bb5
1 ///
2 /// \file socket.h
3 /// Class wrapper to encapsulate the Blackberry USB logical socket
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 JVMPacket;
38 class SocketRoutingQueue;
41 namespace Barry {
43 class Socket;
44 typedef std::auto_ptr<Socket> SocketHandle;
46 class BXEXPORT SocketZero
48 friend class Socket;
50 Usb::Device *m_dev;
51 SocketRoutingQueue *m_queue;
52 int m_writeEp, m_readEp;
53 uint8_t m_zeroSocketSequence;
55 uint32_t m_sequenceId;
57 // half open socket stata, for passwords
58 bool m_halfOpen;
59 uint32_t m_challengeSeed;
60 unsigned int m_remainingTries;
62 bool m_hideSequencePacket;
64 bool m_resetOnClose;
66 private:
67 static void AppendFragment(Data &whole, const Data &fragment);
68 static unsigned int MakeNextFragment(const Data &whole, Data &fragment,
69 unsigned int offset = 0);
70 void CheckSequence(uint16_t socket, const Data &seq);
72 void SendOpen(uint16_t socket, Data &receive);
73 void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
75 // Raw send and receive functions, used for all low level
76 // communication to the USB level.
77 void RawSend(Data &send, int timeout = -1);
78 void RawReceive(Data &receive, int timeout = -1);
80 protected:
81 bool SequencePacket(const Data &data);
82 bool IsSequencePacketHidden() { return m_hideSequencePacket; }
84 public:
85 explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
86 uint8_t zeroSocketSequenceStart = 0);
87 SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
88 uint8_t zeroSocketSequenceStart = 0);
89 ~SocketZero();
91 uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
93 void SetRoutingQueue(SocketRoutingQueue &queue);
94 void UnlinkRoutingQueue();
96 void SetResetOnClose(bool flag) { m_resetOnClose = flag; }
97 void HideSequencePacket(bool flag) { m_hideSequencePacket = flag; }
99 // Send functions for socket 0 only.
100 // These functions will overwrite:
101 // - the zeroSocketSequence byte *inside* the packet
102 // - the socket number to 0
104 void Send(Data &send, int timeout = -1); // send only
105 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
106 void Send(Barry::Packet &packet, int timeout = -1);
107 void Receive(Data &receive, int timeout = -1);
109 // Opens a new socket and returns a Socket object to manage it
110 SocketHandle Open(uint16_t socket, const char *password = 0);
111 void Close(Socket &socket);
113 // Lower level USB operations
114 void ClearHalt();
119 // Socket class
121 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
122 /// By default, provides raw send/receive access, as well as packet
123 /// writing on socket 0, which is always open.
125 /// There are Open and Close members to open data sockets which are used
126 /// to transfer data to and from the device.
128 /// The destructor will close any non-0 open sockets automatically.
130 /// Requires an active Usb::Device object to work on.
132 class BXEXPORT Socket
134 friend class SocketZero;
136 SocketZero *m_zero;
137 uint16_t m_socket;
138 uint8_t m_closeFlag;
140 bool m_registered;
142 protected:
143 void CheckSequence(const Data &seq);
144 void ForceClosed();
146 Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
148 public:
149 ~Socket();
151 uint16_t GetSocket() const { return m_socket; }
152 uint8_t GetCloseFlag() const { return m_closeFlag; }
154 void Close();
156 // Send and Receive are available before Open...
157 // an unopened socket defaults to socket 0, which you need
158 // in order to set the blackberry mode
159 // The send function will overwrite the zeroSocketSequence byte
160 // *inside* the packet, if the current m_socket is 0.
161 void Send(Data &send, int timeout = -1); // send only
162 void Send(Data &send, Data &receive, int timeout = -1); // send+recv
163 void Send(Barry::Packet &packet, int timeout = -1);
164 void Receive(Data &receive, int timeout = -1);
165 void ReceiveData(Data &receive, int timeout = -1);
167 // Lower level USB operations
168 void ClearHalt();
170 // sends the send packet down to the device, fragmenting if
171 // necessary, and returns the response in receive, defragmenting
172 // if needed
173 // Blocks until response received or timed out in Usb::Device
174 void Packet(Data &send, Data &receive, int timeout = -1);
175 void Packet(Barry::Packet &packet, int timeout = -1);
176 void Packet(Barry::JLPacket &packet, int timeout = -1);
177 void Packet(Barry::JVMPacket &packet, int timeout = -1);
179 // Use this function to send packet to JVM instead of Packet function
180 void InitSequence(int timeout = -1);
181 void PacketJVM(Data &send, Data &receive, int timeout = -1);
183 // Use this function to send data packet instead of Packet function
184 // Indeed, Packet function is used to send command (and not data)
185 void PacketData(Data &send, Data &receive, int timeout = -1);
187 // some handy wrappers for the Packet() interface
188 void NextRecord(Data &receive);
190 // Register a callback for incoming data from the device.
191 // This function assumes that this socket is based on a socketZero
192 // that has a SocketRoutingQueue, otherwise throws logic_error.
193 void RegisterInterest(std::tr1::shared_ptr<SocketRoutingQueue::SocketDataHandler> handler);
194 void UnregisterInterest();
197 // This function is quickly written
198 // It's very durty :( (but it's usefull to test...)
199 void HideSequencePacket(bool flag) { m_zero->HideSequencePacket(flag); }
203 } // namespace Barry
205 #endif