1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include "common/libs/fs/shared_fd.h" 19 #include "common/libs/usbforward/protocol.h" 20 21 namespace vadb { 22 // USBCommand is an abstraction of a proxied USB command. 23 // Instances of this object all share the following life cycle: 24 // 1) A specific instance (COMMAND) is being created. 25 // 2) Instance owner (OWNER) sends RequestHeader. 26 // 3) OWNER calls COMMAND.OnRequest() to send any relevant, additional 27 // information. 28 // 4) OWNER queues COMMAND until response arrives. 29 // 30 // At this point instance owner can process next command in queue. Then, 31 // eventually: 32 // 33 // 5) OWNER receives matching ResponseHeader. 34 // 6) OWNER calls COMMAND.OnResponse(), supplying FD that carries additional 35 // data. 36 // 7) OWNER dequeues and deletes COMMAND. 37 class USBCommand { 38 public: 39 USBCommand() = default; 40 virtual ~USBCommand() = default; 41 42 // Command returns a specific usbforward command ID associated with this 43 // request. 44 virtual usb_forward::Command Command() = 0; 45 46 // OnRequest is called whenever additional data relevant to this command 47 // (other than RequestHeader) should be sent. 48 // Returns false, if communication with remote host failed (and should be 49 // terminated). 50 virtual bool OnRequest(const cvd::SharedFD& data) = 0; 51 52 // OnResponse is called whenever additional data relevant to this command 53 // (other than ResponseHeader) should be received. 54 // Returns false, if communication with remote host failed (and should be 55 // terminated). 56 virtual bool OnResponse(bool is_success, const cvd::SharedFD& data) = 0; 57 58 private: 59 USBCommand(const USBCommand& other) = delete; 60 USBCommand& operator=(const USBCommand& other) = delete; 61 }; 62 63 } // namespace vadb 64