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

Generated on Wed Sep 24 21:27:32 2008 for Barry by  doxygen 1.5.1