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-2010, 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 
00031 // forward declarations
00032 namespace Usb { class Device; }
00033 namespace Barry {
00034         class Data;
00035         class Packet;
00036         class JLPacket;
00037         class JVMPacket;
00038         class SocketRoutingQueue;
00039 }
00040 
00041 namespace Barry {
00042 
00043 class Socket;
00044 typedef std::auto_ptr<Socket>   SocketHandle;
00045 
00046 class BXEXPORT SocketZero
00047 {
00048         friend class Socket;
00049 
00050         Usb::Device *m_dev;
00051         SocketRoutingQueue *m_queue;
00052         int m_writeEp, m_readEp;
00053         uint8_t m_zeroSocketSequence;
00054 
00055         uint32_t m_sequenceId;
00056 
00057         // half open socket stata, for passwords
00058         bool m_halfOpen;
00059         uint32_t m_challengeSeed;
00060         unsigned int m_remainingTries;
00061 
00062         bool m_hideSequencePacket;
00063 
00064         bool m_resetOnClose;
00065 
00066 private:
00067         static void AppendFragment(Data &whole, const Data &fragment);
00068         static unsigned int MakeNextFragment(const Data &whole, Data &fragment,
00069                 unsigned int offset = 0);
00070         void CheckSequence(uint16_t socket, const Data &seq);
00071 
00072         void SendOpen(uint16_t socket, Data &receive);
00073         void SendPasswordHash(uint16_t socket, const char *password, Data &receive);
00074 
00075         // Raw send and receive functions, used for all low level
00076         // communication to the USB level.
00077         void RawSend(Data &send, int timeout = -1);
00078         void RawReceive(Data &receive, int timeout = -1);
00079 
00080 protected:
00081         bool SequencePacket(const Data &data);
00082         bool IsSequencePacketHidden() { return m_hideSequencePacket; }
00083 
00084 public:
00085         void SetResetOnClose(bool flag) { m_resetOnClose = flag; }
00086         void HideSequencePacket(bool flag) { m_hideSequencePacket = flag; }
00087         explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
00088                 uint8_t zeroSocketSequenceStart = 0);
00089         SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
00090                 uint8_t zeroSocketSequenceStart = 0);
00091         ~SocketZero();
00092 
00093         uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
00094 
00095         void SetRoutingQueue(SocketRoutingQueue &queue);
00096         void UnlinkRoutingQueue();
00097 
00098         // Send functions for socket 0 only.
00099         // These functions will overwrite:
00100         //     - the zeroSocketSequence byte *inside* the packet
00101         //     - the socket number to 0
00102         //
00103         void Send(Data &send, int timeout = -1);        // send only
00104         void Send(Data &send, Data &receive, int timeout = -1); // send+recv
00105         void Send(Barry::Packet &packet, int timeout = -1);
00106         void Receive(Data &receive, int timeout = -1);
00107 
00108         // Opens a new socket and returns a Socket object to manage it
00109         SocketHandle Open(uint16_t socket, const char *password = 0);
00110         void Close(Socket &socket);
00111 
00112         // Lower level USB operations
00113         void ClearHalt();
00114 };
00115 
00116 
00117 //
00118 // Socket class
00119 //
00120 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
00121 /// By default, provides raw send/receive access, as well as packet
00122 /// writing on socket 0, which is always open.
00123 ///
00124 /// There are Open and Close members to open data sockets which are used
00125 /// to transfer data to and from the device.
00126 ///
00127 /// The destructor will close any non-0 open sockets automatically.
00128 ///
00129 /// Requires an active Usb::Device object to work on.
00130 ///
00131 class BXEXPORT Socket
00132 {
00133         friend class SocketZero;
00134 
00135         SocketZero *m_zero;
00136         uint16_t m_socket;
00137         uint8_t m_closeFlag;
00138 
00139         bool m_registered;
00140 
00141 protected:
00142         void CheckSequence(const Data &seq);
00143         void ForceClosed();
00144 
00145         Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
00146 
00147 public:
00148         ~Socket();
00149 
00150         uint16_t GetSocket() const { return m_socket; }
00151         uint8_t GetCloseFlag() const { return m_closeFlag; }
00152 
00153         void Close();
00154 
00155         // Send and Receive are available before Open...
00156         // an unopened socket defaults to socket 0, which you need
00157         // in order to set the blackberry mode
00158         // The send function will overwrite the zeroSocketSequence byte
00159         // *inside* the packet, if the current m_socket is 0.
00160         void Send(Data &send, int timeout = -1);        // send only
00161         void Send(Data &send, Data &receive, int timeout = -1); // send+recv
00162         void Send(Barry::Packet &packet, int timeout = -1);
00163         void Receive(Data &receive, int timeout = -1);
00164         void ReceiveData(Data &receive, int timeout = -1);
00165 
00166         // Lower level USB operations
00167         void ClearHalt();
00168 
00169         // sends the send packet down to the device, fragmenting if
00170         // necessary, and returns the response in receive, defragmenting
00171         // if needed
00172         // Blocks until response received or timed out in Usb::Device
00173         void Packet(Data &send, Data &receive, int timeout = -1);
00174         void Packet(Barry::Packet &packet, int timeout = -1);
00175         void Packet(Barry::JLPacket &packet, int timeout = -1);
00176         void Packet(Barry::JVMPacket &packet, int timeout = -1);
00177 
00178         // Use this function to send packet to JVM instead of Packet function
00179         void InitSequence(int timeout = -1);
00180         void PacketJVM(Data &send, Data &receive, int timeout = -1);
00181 
00182         // Use this function to send data packet instead of Packet function
00183         // Indeed, Packet function is used to send command (and not data)
00184         void PacketData(Data &send, Data &receive, int timeout = -1);
00185 
00186         // some handy wrappers for the Packet() interface
00187         void NextRecord(Data &receive);
00188 
00189         // Register a callback for incoming data from the device.
00190         // This function assumes that this socket is based on a socketZero
00191         // that has a SocketRoutingQueue, otherwise throws logic_error.
00192         void RegisterInterest(SocketRoutingQueue::SocketDataHandler handler, void *context);
00193         void UnregisterInterest();
00194 
00195 
00196         // This function is quickly written
00197         // It's very durty :( (but it's usefull to test...)
00198         void HideSequencePacket(bool flag) { m_zero->HideSequencePacket(flag); }
00199 };
00200 
00201 
00202 } // namespace Barry
00203 
00204 #endif
00205 
Generated by  doxygen 1.6.2-20100208