1 /*
2     This file is part of Android File Transfer For Linux.
3     Copyright (C) 2015-2020  Vladimir Menshakov
4 
5     This library is free software; you can redistribute it and/or modify it
6     under the terms of the GNU Lesser General Public License as published by
7     the Free Software Foundation; either version 2.1 of the License,
8     or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public License
16     along with this library; if not, write to the Free Software Foundation,
17     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 #ifndef AFTL_MTP_BACKEND_LIBUSB_USB_DEVICE_H
21 #define AFTL_MTP_BACKEND_LIBUSB_USB_DEVICE_H
22 
23 #include <mtp/ptp/IObjectStream.h>
24 #include <mtp/ByteArray.h>
25 #include <mtp/Token.h>
26 #include <mtp/types.h>
27 #include <mtp/usb/types.h>
28 #include <libusb.h>
29 
30 namespace mtp { namespace usb
31 {
32 	class Context;
33 	DECLARE_PTR(Context);
34 
35 	class Endpoint
36 	{
37 		const libusb_endpoint_descriptor & _endpoint;
38 
39 	public:
Endpoint(const libusb_endpoint_descriptor & endpoint)40 		Endpoint(const libusb_endpoint_descriptor & endpoint) : _endpoint(endpoint) { }
41 
GetAddress()42 		u8 GetAddress() const
43 		{ return _endpoint.bEndpointAddress; }
44 
GetMaxPacketSize()45 		int GetMaxPacketSize() const
46 		{ return _endpoint.wMaxPacketSize; }
47 
GetDirection()48 		EndpointDirection GetDirection() const
49 		{
50 			u8 dir = GetAddress() & LIBUSB_ENDPOINT_DIR_MASK;
51 			if (dir == LIBUSB_ENDPOINT_IN)
52 				return EndpointDirection::In;
53 			else
54 				return EndpointDirection::Out;
55 		}
56 
GetType()57 		EndpointType GetType() const
58 		{
59 			return EndpointType(_endpoint.bmAttributes & 3);
60 		}
61 	};
62 	DECLARE_PTR(Endpoint);
63 
64 	class Interface;
65 	DECLARE_PTR(Interface);
66 	class InterfaceToken;
67 	DECLARE_PTR(InterfaceToken);
68 
69 	class Device : Noncopyable
70 	{
71 	private:
72 		ContextPtr				_context;
73 		libusb_device_handle *	_handle;
74 
75 	public:
76 		Device(ContextPtr ctx, libusb_device_handle * handle);
77 		~Device();
78 
GetHandle()79 		libusb_device_handle * GetHandle()
80 		{ return _handle; }
81 
82 		InterfaceTokenPtr ClaimInterface(const InterfacePtr & interface);
83 
84 		void Reset();
85 		int GetConfiguration() const;
86 		void SetConfiguration(int idx);
87 
88 		void WriteBulk(const EndpointPtr & ep, const IObjectInputStreamPtr &inputStream, int timeout);
89 		void ReadBulk(const EndpointPtr & ep, const IObjectOutputStreamPtr &outputStream, int timeout);
90 
91 		void ReadControl(u8 type, u8 req, u16 value, u16 index, ByteArray &data, int timeout);
92 		void WriteControl(u8 type, u8 req, u16 value, u16 index, const ByteArray &data, int timeout);
93 
94 		void ClearHalt(const EndpointPtr & ep);
95 
96 		std::string GetString(int idx) const;
97 	};
98 	DECLARE_PTR(Device);
99 }}
100 
101 #endif	/* DEVICE_H */
102 
103