David Valleau | 64c8149 | 2018-10-26 13:24:37 -0700 | [diff] [blame] | 1 | #ifndef __OP_COMMANDS_H__ |
| 2 | #define __OP_COMMANDS_H__ |
| 3 | |
| 4 | /* |
| 5 | * This file defines the supported OP command messages from the usbip userspace |
| 6 | * protocol, as well as some utility functions for processing them. |
| 7 | * |
| 8 | * In the context of the defined messages: |
| 9 | * "Req" is used in messages that submit a request. |
| 10 | * "Rep" is used in messages which reply to a request. |
| 11 | * |
| 12 | * For more information about the usbip protocol refer to the following |
| 13 | * documentation: |
| 14 | * https://www.kernel.org/doc/Documentation/usb/usbip_protocol.txt |
| 15 | * https://en.opensuse.org/SDB:USBIP |
| 16 | */ |
| 17 | |
| 18 | #include "device_descriptors.h" |
| 19 | #include "smart_buffer.h" |
| 20 | #include "usbip_constants.h" |
| 21 | |
| 22 | #include <cstdlib> |
| 23 | #include <vector> |
| 24 | |
| 25 | // Forward declaration of UsbPrinter. |
| 26 | class UsbPrinter; |
| 27 | |
| 28 | // Contains the header values that are contained within all of the "OP" messages |
| 29 | // used by usbip. |
| 30 | struct OpHeader { |
| 31 | uint16_t version; // usbip version. |
| 32 | uint16_t command; // op command type. |
| 33 | int status; // op request status. |
| 34 | }; |
| 35 | |
| 36 | // Generic device descriptor used by OpRepDevlist and OpRepImport. |
| 37 | struct OpRepDevice { |
| 38 | char usbPath[256]; |
| 39 | char busID[32]; |
| 40 | int busnum; |
| 41 | int devnum; |
| 42 | int speed; |
| 43 | uint16_t idVendor; |
| 44 | uint16_t idProduct; |
| 45 | uint16_t bcdDevice; |
| 46 | uint8_t bDeviceClass; |
| 47 | uint8_t bDeviceSubClass; |
| 48 | uint8_t bDeviceProtocol; |
| 49 | uint8_t bConfigurationValue; |
| 50 | uint8_t bNumConfigurations; |
| 51 | uint8_t bNumInterfaces; |
| 52 | }; |
| 53 | |
| 54 | // The OpReqDevlistMessage message contains the same information as OpHeader. |
| 55 | typedef OpHeader OpReqDevlist; |
| 56 | |
| 57 | // The header used in an OpRepDevlist message, the only difference from |
| 58 | // OpHeader is that it contains |numExportedDevices|. |
| 59 | struct OpRepDevlistHeader { |
| 60 | OpHeader header; |
| 61 | int numExportedDevices; |
| 62 | }; |
| 63 | |
| 64 | // Basic interface descriptor used by OpRepDevlist. |
| 65 | struct OpRepDevlistInterface { |
| 66 | uint8_t bInterfaceClass; |
| 67 | uint8_t bInterfaceSubClass; |
| 68 | uint8_t bInterfaceProtocol; |
| 69 | uint8_t padding; |
| 70 | }; |
| 71 | |
| 72 | // Defines the OpRepDevlist used to respond to a OpReqDevlist message. |
| 73 | struct OpRepDevlist { |
| 74 | OpRepDevlistHeader header; |
| 75 | // Since our program is used to provide a virtual USB device, we only include |
| 76 | // a single OpRepDevice in our response. |
| 77 | OpRepDevice device; |
| 78 | OpRepDevlistInterface* interfaces; |
| 79 | }; |
| 80 | |
| 81 | // Defines the OpReqImport request used to request a device for import. |
| 82 | struct OpReqImport { |
| 83 | OpHeader header; |
| 84 | char busID[32]; |
| 85 | }; |
| 86 | |
| 87 | // Defins the OpRepImport response which indicates whether the requested device |
| 88 | // was successfully exported. |
| 89 | struct OpRepImport { |
| 90 | OpHeader header; |
| 91 | OpRepDevice device; |
| 92 | }; |
| 93 | |
| 94 | // Sets the corresponding members of |header| using the given values. |
| 95 | void SetOpHeader(uint16_t command, int status, OpHeader* header); |
| 96 | |
| 97 | // Sets the corresponding members of |devlist_header| using the given values. |
| 98 | void SetOpRepDevlistHeader(uint16_t command, int status, int numExportedDevices, |
| 99 | OpRepDevlistHeader* header); |
| 100 | |
| 101 | // Sets the members of |device| using the corresponding values in |
| 102 | // |dev_dsc| and |config|. |
| 103 | void SetOpRepDevice(const UsbDeviceDescriptor& dev_dsc, |
| 104 | const UsbConfigurationDescriptor& configuration, |
| 105 | OpRepDevice* device); |
| 106 | |
| 107 | // Assigns the values from |interfaces| into |rep_interfaces|. |
| 108 | void SetOpRepDevlistInterfaces( |
| 109 | const std::vector<UsbInterfaceDescriptor>& interfaces, |
| 110 | OpRepDevlistInterface** rep_interfaces); |
| 111 | |
| 112 | // Creates the OpRepDevlist message used to respond to requests to list the |
| 113 | // host's exported USB devices. |
| 114 | void CreateOpRepDevlist(const UsbDeviceDescriptor& device, |
| 115 | const UsbConfigurationDescriptor& config, |
| 116 | const std::vector<UsbInterfaceDescriptor>& interfaces, |
| 117 | OpRepDevlist* list); |
| 118 | |
| 119 | // Creates the OpRepImport message used to respond to a request to attach a |
| 120 | // host USB device. |
| 121 | void CreateOpRepImport(const UsbDeviceDescriptor& device, |
| 122 | const UsbConfigurationDescriptor& config, |
| 123 | OpRepImport* rep); |
| 124 | |
| 125 | // Convert the various elements of an "OpRep" message into network |
| 126 | // byte order and pack them into a SmartBuffer to be used for transferring along |
| 127 | // a socket. |
| 128 | SmartBuffer<uint8_t> PackOpHeader(OpHeader header); |
| 129 | SmartBuffer<uint8_t> PackOpRepDevice(OpRepDevice device); |
| 130 | SmartBuffer<uint8_t> PackOpRepDevlistHeader(OpRepDevlistHeader devlist_header); |
| 131 | SmartBuffer<uint8_t> PackOpRepDevlist(OpRepDevlist devlist); |
| 132 | SmartBuffer<uint8_t> PackOpRepImport(OpRepImport import); |
| 133 | |
| 134 | // Convert |header| into host uint8_t order. |
| 135 | void UnpackOpHeader(OpHeader* header); |
| 136 | |
| 137 | #endif // __OP_COMMANDS_H__ |