# 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-2011, 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         explicit SocketZero(SocketRoutingQueue &queue, int writeEndpoint,
    00086                 uint8_t zeroSocketSequenceStart = 0);
    00087         SocketZero(Usb::Device &dev, int writeEndpoint, int readEndpoint,
    00088                 uint8_t zeroSocketSequenceStart = 0);
    00089         ~SocketZero();
    00090
    00091         uint8_t GetZeroSocketSequence() const { return m_zeroSocketSequence; }
    00092
    00093         void SetRoutingQueue(SocketRoutingQueue &queue);
    00094         void UnlinkRoutingQueue();
    00095
    00096         void SetResetOnClose(bool flag) { m_resetOnClose = flag; }
    00097         void HideSequencePacket(bool flag) { m_hideSequencePacket = flag; }
    00098
    00099         // Send functions for socket 0 only.
    00100         // These functions will overwrite:
    00101         //     - the zeroSocketSequence byte *inside* the packet
    00102         //     - the socket number to 0
    00103         //
    00104         void Send(Data &send, int timeout = -1);        // send only
    00105         void Send(Data &send, Data &receive, int timeout = -1); // send+recv
    00106         void Send(Barry::Packet &packet, int timeout = -1);
    00107         void Receive(Data &receive, int timeout = -1);
    00108
    00109         // Opens a new socket and returns a Socket object to manage it
    00110         SocketHandle Open(uint16_t socket, const char *password = 0);
    00111         void Close(Socket &socket);
    00112
    00113         // Lower level USB operations
    00114         void ClearHalt();
    00115 };
    00116
    00117
    00118 //
    00119 // Socket class
    00120 //
    00121 /// Encapsulates a "logical socket" in the Blackberry USB protocol.
    00122 /// By default, provides raw send/receive access, as well as packet
    00123 /// writing on socket 0, which is always open.
    00124 ///
    00125 /// There are Open and Close members to open data sockets which are used
    00126 /// to transfer data to and from the device.
    00127 ///
    00128 /// The destructor will close any non-0 open sockets automatically.
    00129 ///
    00130 /// Requires an active Usb::Device object to work on.
    00131 ///
    00132 class BXEXPORT Socket
    00133 {
    00134         friend class SocketZero;
    00135
    00136         SocketZero *m_zero;
    00137         uint16_t m_socket;
    00138         uint8_t m_closeFlag;
    00139
    00140         bool m_registered;
    00141
    00142 protected:
    00143         void CheckSequence(const Data &seq);
    00144         void ForceClosed();
    00145
    00146         Socket(SocketZero &zero, uint16_t socket, uint8_t closeFlag);
    00147
    00148 public:
    00149         ~Socket();
    00150
    00151         uint16_t GetSocket() const { return m_socket; }
    00152         uint8_t GetCloseFlag() const { return m_closeFlag; }
    00153
    00154         void Close();
    00155
    00156         // Send and Receive are available before Open...
    00157         // an unopened socket defaults to socket 0, which you need
    00158         // in order to set the blackberry mode
    00159         // The send function will overwrite the zeroSocketSequence byte
    00160         // *inside* the packet, if the current m_socket is 0.
    00161         void Send(Data &send, int timeout = -1);        // send only
    00162         void Send(Data &send, Data &receive, int timeout = -1); // send+recv
    00163         void Send(Barry::Packet &packet, int timeout = -1);
    00164         void Receive(Data &receive, int timeout = -1);
    00165         void ReceiveData(Data &receive, int timeout = -1);
    00166
    00167         // Lower level USB operations
    00168         void ClearHalt();
    00169
    00170         // sends the send packet down to the device, fragmenting if
    00171         // necessary, and returns the response in receive, defragmenting
    00172         // if needed
    00173         // Blocks until response received or timed out in Usb::Device
    00174         void Packet(Data &send, Data &receive, int timeout = -1);
    00175         void Packet(Barry::Packet &packet, int timeout = -1);
    00176         void Packet(Barry::JLPacket &packet, int timeout = -1);
    00177         void Packet(Barry::JVMPacket &packet, int timeout = -1);
    00178
    00179         // Use this function to send packet to JVM instead of Packet function
    00180         void InitSequence(int timeout = -1);
    00181         void PacketJVM(Data &send, Data &receive, int timeout = -1);
    00182
    00183         // Use this function to send data packet instead of Packet function
    00184         // Indeed, Packet function is used to send command (and not data)
    00185         void PacketData(Data &send, Data &receive, int timeout = -1);
    00186
    00187         // some handy wrappers for the Packet() interface
    00188         void NextRecord(Data &receive);
    00189
    00190         // Register a callback for incoming data from the device.
    00191         // This function assumes that this socket is based on a socketZero
    00192         // that has a SocketRoutingQueue, otherwise throws logic_error.
    00193         void RegisterInterest(std::tr1::shared_ptr<SocketRoutingQueue::SocketDataHandler> handler);
    00194         void UnregisterInterest();
    00195
    00196
    00197         // This function is quickly written
    00198         // It's very durty :( (but it's usefull to test...)
    00199         void HideSequencePacket(bool flag) { m_zero->HideSequencePacket(flag); }
    00200 };
    00201
    00202
    00203 } // namespace Barry
    00204
    00205 #endif
    00206