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

Generated on Tue Jun 30 16:08:14 2009 for Barry by  doxygen 1.5.8