Home | History | Annotate | Download | only in adb
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_DEVTOOLS_ADB_ANDROID_USB_DEVICE_H_
      6 #define CHROME_BROWSER_DEVTOOLS_ADB_ANDROID_USB_DEVICE_H_
      7 
      8 #include <map>
      9 #include <queue>
     10 #include <vector>
     11 #include "base/memory/ref_counted.h"
     12 #include "chrome/browser/usb/usb_device_handle.h"
     13 
     14 namespace base {
     15 class MessageLoop;
     16 }
     17 
     18 namespace crypto {
     19 class RSAPrivateKey;
     20 }
     21 
     22 namespace net {
     23 class StreamSocket;
     24 }
     25 
     26 class AndroidUsbSocket;
     27 
     28 class AdbMessage : public base::RefCounted<AdbMessage> {
     29  public:
     30   enum Command {
     31     kCommandSYNC = 0x434e5953,
     32     kCommandCNXN = 0x4e584e43,
     33     kCommandOPEN = 0x4e45504f,
     34     kCommandOKAY = 0x59414b4f,
     35     kCommandCLSE = 0x45534c43,
     36     kCommandWRTE = 0x45545257,
     37     kCommandAUTH = 0x48545541
     38   };
     39 
     40   enum Auth {
     41     kAuthToken = 1,
     42     kAuthSignature = 2,
     43     kAuthRSAPublicKey = 3
     44   };
     45 
     46   AdbMessage(uint32 command,
     47              uint32 arg0,
     48              uint32 arg1,
     49              const std::string& body);
     50 
     51   uint32 command;
     52   uint32 arg0;
     53   uint32 arg1;
     54   std::string body;
     55  private:
     56   friend class base::RefCounted<AdbMessage>;
     57   ~AdbMessage();
     58 
     59   DISALLOW_COPY_AND_ASSIGN(AdbMessage);
     60 };
     61 
     62 class AndroidUsbDevice;
     63 typedef std::vector<scoped_refptr<AndroidUsbDevice> > AndroidUsbDevices;
     64 typedef base::Callback<void(const AndroidUsbDevices&)>
     65     AndroidUsbDevicesCallback;
     66 
     67 class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> {
     68  public:
     69   static void Enumerate(crypto::RSAPrivateKey* rsa_key,
     70                         const AndroidUsbDevicesCallback& callback);
     71 
     72   AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key,
     73                    scoped_refptr<UsbDeviceHandle> device,
     74                    const std::string& serial,
     75                    int inbound_address,
     76                    int outbound_address,
     77                    int zero_mask);
     78 
     79   void InitOnCallerThread();
     80 
     81   net::StreamSocket* CreateSocket(const std::string& command);
     82 
     83   void Send(uint32 command,
     84             uint32 arg0,
     85             uint32 arg1,
     86             const std::string& body);
     87 
     88   scoped_refptr<UsbDeviceHandle> usb_device() { return usb_device_; }
     89 
     90   std::string serial() { return serial_; }
     91 
     92   bool terminated() { return terminated_; }
     93 
     94  private:
     95   friend class base::RefCountedThreadSafe<AndroidUsbDevice>;
     96   virtual ~AndroidUsbDevice();
     97 
     98   void Queue(scoped_refptr<AdbMessage> message);
     99   void ProcessOutgoing();
    100   void OutgoingMessageSent(UsbTransferStatus status,
    101                            scoped_refptr<net::IOBuffer> buffer,
    102                            size_t result);
    103 
    104   void ReadHeader(bool initial);
    105   void ParseHeader(UsbTransferStatus status,
    106                    scoped_refptr<net::IOBuffer> buffer,
    107                    size_t result);
    108 
    109   void ReadBody(scoped_refptr<AdbMessage> message,
    110                 uint32 data_length,
    111                 uint32 data_check);
    112   void ParseBody(scoped_refptr<AdbMessage> message,
    113                  uint32 data_length,
    114                  uint32 data_check,
    115                  UsbTransferStatus status,
    116                  scoped_refptr<net::IOBuffer> buffer,
    117                  size_t result);
    118 
    119   void HandleIncoming(scoped_refptr<AdbMessage> message);
    120 
    121   void TransferError(UsbTransferStatus status);
    122 
    123   void Terminate();
    124 
    125   void SocketDeleted(uint32 socket_id);
    126 
    127   base::MessageLoop* message_loop_;
    128 
    129   scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
    130 
    131   // Device info
    132   scoped_refptr<UsbDeviceHandle> usb_device_;
    133   std::string serial_;
    134   int inbound_address_;
    135   int outbound_address_;
    136   int zero_mask_;
    137 
    138   bool is_connected_;
    139   bool signature_sent_;
    140 
    141   // Created sockets info
    142   uint32 last_socket_id_;
    143   bool terminated_;
    144   typedef std::map<uint32, AndroidUsbSocket*> AndroidUsbSockets;
    145   AndroidUsbSockets sockets_;
    146 
    147   // Outgoing bulk queue
    148   typedef std::pair<scoped_refptr<net::IOBuffer>, size_t> BulkMessage;
    149   std::queue<BulkMessage> outgoing_queue_;
    150 
    151   // Outgoing messages pending connect
    152   typedef std::vector<scoped_refptr<AdbMessage> > PendingMessages;
    153   PendingMessages pending_messages_;
    154 
    155   DISALLOW_COPY_AND_ASSIGN(AndroidUsbDevice);
    156 };
    157 
    158 #endif  // CHROME_BROWSER_DEVTOOLS_ADB_ANDROID_USB_DEVICE_H_
    159