socket.h

Go to the documentation of this file.
00001 ///
00002 /// \file       socket.h
00003 ///             Class wrapper to encapsulate the Blackberry USB logical socket
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_SOCKET_H__
00023 #define __BARRY_SOCKET_H__
00024 
00025 #include "dll.h"
00026 #include <stdint.h>
00027 #include <queue>
00028 #include <memory>
00029 #include "router.h"
00030 #include "data.h"
00031 
00032 // forward declarations
00033 namespace Usb { class Device; }
00034 namespace Barry {
00035         class Packet;
00036         class JLPacket;
00037         class JVMPacket;
00038         class SocketRoutingQueue;
00039 }
00040 
00041 namespace Barry {
00042 
00043 class SocketBase;
00044 class Socket;
00045 typedef std::auto_ptr<SocketBase>       SocketHandle;
00046 
00047 class BXEXPORT SocketZero
00048 {
00049         friend class SocketBase;
00050         friend class Socket;
00051 
00052         Usb::Device *m_dev;
00053         SocketRoutingQueue *m_queue;
00054         int m_writeEp, m_readEp;
00055         uint8_t m_zeroSocketSequence;
00056 
00057         uint32_t m_sequenceId;
00058 
00059         // half open socket stata, for passwords
00060         bool m_halfOpen;
00061         uint32_t m_challengeSeed;
00062         unsigned int m_remainingTries;
00063         bool m_modeSequencePacketSeen;
00064 
00065         // pushback for out of order sequence packets
00066         Data m_pushback_buffer;
00067         bool m_pushback;
00068 
00069 private:
00070         static void AppendFragment(Data &whole, const Data &fragment);
00071         static unsigned int MakeNextFragment(const Data &whole, Data &fragment,
00072                 unsigned int offset = 0);
00073         void CheckSequence(uint16_t socket, const Data &seq);
00074 
00075         void SendOpen(uint16_t socket, Data &receive);
00076         void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
00077 
00078         // Raw send and receive functions, used for all low level
00079         // communication to the USB level.
00080         void RawSend(Data &send, int timeout = -1);
00081         void RawReceive(Data &receive, int timeout = -1);
00082 
00083         void Pushback(const Data &buf);
00084 
00085 protected:
00086         bool IsSequencePacketHidden() { return false; }
00087 
00088 public:
00089         explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
00090                 uint8_t zeroSocketSequenceStart = 0);
00091         SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
00092                 uint8_t zeroSocketSequenceStart = 0);
00093         ~SocketZero();
00094 
00095         uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
00096 
00097         void SetRoutingQueue(SocketRoutingQueue &queue);
00098         void UnlinkRoutingQueue();
00099 
00100         // Send functions for socket 0 only.
00101         // These functions will overwrite:
00102         //     - the zeroSocketSequence byte *inside* the packet
00103         //     - the socket number to 0
00104         //
00105         void Send(Data &send, int timeout = -1);        // send only
00106         void Send(Data &send, Data &receive, int timeout = -1); // send+recv
00107         void Send(Barry::Packet &packet, int timeout = -1);
00108         void Receive(Data &receive, int timeout = -1);
00109 
00110         // Opens a new socket and returns a Socket object to manage it
00111         SocketHandle Open(uint16_t socket, const char *password = 0);
00112         void Close(Socket &socket);
00113 };
00114 
00115 class BXEXPORT SocketBase
00116 {
00117         bool m_resetOnClose;
00118 
00119 protected:
00120         void CheckSequence(const Data &seq);
00121 
00122 public:
00123         SocketBase()
00124                 : m_resetOnClose(false)
00125         {
00126         }
00127 
00128         virtual ~SocketBase();
00129 
00130         //
00131         // Virtual Socket API
00132         //
00133         virtual void Close() = 0;
00134 
00135         // FIXME - do I need RawSend?  Or just a good fragmenter?
00136         virtual void RawSend(Data &send, int timeout = -1) = 0;
00137         virtual void SyncSend(Data &send, int timeout = -1) = 0;
00138         virtual void Receive(Data &receive, int timeout = -1) = 0;
00139 
00140         virtual void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr()) = 0;
00141         virtual void UnregisterInterest() = 0;
00142 
00143         void ResetOnClose(bool reset = true) { m_resetOnClose = reset; }
00144         bool IsResetOnClose() const { return m_resetOnClose; }
00145 
00146         //
00147         // Convenience functions that just call the virtuals above
00148         //
00149         void DBFragSend(Data &send, int timeout = -1);
00150         void Send(Data &send, Data &receive, int timeout = -1);
00151         void Send(Barry::Packet &packet, int timeout = -1);
00152 
00153         //
00154         // Protocol based functions... all use the above virtual functions
00155         //
00156 
00157         // sends the send packet down to the device, fragmenting if
00158         // necessary, and returns the response in receive, defragmenting
00159         // if needed
00160         // Blocks until response received or timed out in Usb::Device
00161         void Packet(Data &send, Data &receive, int timeout = -1);
00162         void Packet(Barry::Packet &packet, int timeout = -1);
00163         void Packet(Barry::JLPacket &packet, int timeout = -1);
00164         void Packet(Barry::JVMPacket &packet, int timeout = -1);
00165 
00166         // Use this function to send packet to JVM instead of Packet function
00167         // FIXME
00168         void PacketJVM(Data &send, Data &receive, int timeout = -1);
00169 
00170         // Use this function to send data packet instead of Packet function
00171         // Indeed, Packet function is used to send command (and not data)
00172         // FIXME
00173         void PacketData(Data &send, Data &receive, bool done_on_sequence,
00174                 int timeout = -1);
00175 
00176         // some handy wrappers for the Packet() interface
00177         void NextRecord(Data &receive);
00178 };
00179 
00180 //
00181 // Socket class
00182 //
00183 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
00184 /// By default, provides raw send/receive access, as well as packet
00185 /// writing on socket 0, which is always open.
00186 ///
00187 /// There are Open and Close members to open data sockets which are used
00188 /// to transfer data to and from the device.
00189 ///
00190 /// The destructor will close any non-0 open sockets automatically.
00191 ///
00192 /// Requires an active Usb::Device object to work on.
00193 ///
00194 class BXEXPORT Socket : public SocketBase
00195 {
00196         friend class SocketZero;
00197 
00198         SocketZero *m_zero;
00199         uint16_t m_socket;
00200         uint8_t m_closeFlag;
00201 
00202         bool m_registered;
00203 
00204         // buffer data
00205         std::auto_ptr<Data> m_sequence;
00206 
00207 protected:
00208         void ForceClosed();
00209 
00210         // These "Local" function are non-virtual worker functions,
00211         // which are safe to be called from the destructor.
00212         // If you override the virtual versions in your derived class,
00213         // make sure your "Local" versions call us.
00214         void LocalClose();
00215         void LocalUnregisterInterest();
00216 
00217         uint16_t GetSocket() const { return m_socket; }
00218         uint8_t GetCloseFlag() const { return m_closeFlag; }
00219 
00220         Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
00221 
00222 public:
00223         ~Socket();
00224 
00225         //
00226         // virtual overrides
00227         //
00228         void Close() { LocalClose(); }
00229         void RawSend(Data &send, int timeout = -1);
00230         void SyncSend(Data &send, int timeout = -1);
00231         void Receive(Data &receive, int timeout = -1);
00232 
00233 
00234         // Register a callback for incoming data from the device.
00235         // This function assumes that this socket is based on a socketZero
00236         // that has a SocketRoutingQueue, otherwise throws logic_error.
00237         // It also assumes that nothing has been registered before.
00238         // If you wish to re-register, call UnregisterInterest() first,
00239         // which is safe to call as many times as you like.
00240         void RegisterInterest(Barry::SocketRoutingQueue::SocketDataHandlerPtr handler = Barry::SocketRoutingQueue::SocketDataHandlerPtr());
00241         void UnregisterInterest() { LocalUnregisterInterest(); }
00242 };
00243 
00244 
00245 } // namespace Barry
00246 
00247 #endif
00248