File: PipePacketer.cpp

package info (click to toggle)
android-file-transfer 4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,496 kB
  • sloc: cpp: 12,909; python: 140; lex: 47; xml: 26; sh: 13; makefile: 6
file content (257 lines) | stat: -rw-r--r-- 7,466 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
    This file is part of Android File Transfer For Linux.
    Copyright (C) 2015-2020  Vladimir Menshakov

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License,
    or (at your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <mtp/ptp/PipePacketer.h>
#include <mtp/ptp/Response.h>
#include <mtp/ptp/EventCode.h>
#include <mtp/ptp/InputStream.h>
#include <mtp/ptp/ByteArrayObjectStream.h>
#include <mtp/ptp/JoinedObjectStream.h>
#include <mtp/ptp/OutputStream.h>
#include <mtp/ptp/OperationRequest.h>
#include <mtp/usb/Request.h>
#include <usb/Device.h>
#include <usb/Interface.h>
#include <mtp/log.h>


namespace mtp
{

	void PipePacketer::Write(const IObjectInputStreamPtr &inputStream, int timeout)
	{ _pipe->Write(inputStream, timeout); }

	void PipePacketer::Write(const ByteArray &data, int timeout)
	{ Write(std::make_shared<ByteArrayObjectInputStream>(data), timeout); }

	namespace
	{
		class MessageParsingStream final: public JoinedObjectOutputStreamBase
		{
			FixedSizeByteArrayObjectOutputStreamPtr	_header;
			IObjectOutputStreamPtr					_stream;
			u64										_offset;
			u64										_size;

		private:
			IObjectOutputStreamPtr GetStream1() const override
			{ return _header; }

			IObjectOutputStreamPtr GetStream2() const override
			{ return _stream; }

		public:
			MessageParsingStream(IObjectOutputStreamPtr stream):
				_header(new FixedSizeByteArrayObjectOutputStream(4)),
				_stream(stream), _offset(0), _size(4)
			{ }

			bool Finished() const
			{ return _offset >= _size; }

			void OnStream1Exhausted() override
			{
				InputStream is(_header->GetData());
				u32 size;
				is >> size;
				if (size < 4)
					throw std::runtime_error("invalid size/malformed message");
				_size = size;
			}

			size_t Write(const u8 *data, size_t size) override
			{
				size_t r = JoinedObjectOutputStreamBase::Write(data, size);
				_offset += r;
				if (!_stream1Exhausted && _offset >= _header->GetData().size()) {
					_stream1Exhausted = true;
					OnStream1Exhausted();
				}
				return r;
			}
		};
		DECLARE_PTR(MessageParsingStream);
	}

	void PipePacketer::ReadMessage(const IObjectOutputStreamPtr &outputStream, int timeout)
	{ _pipe->Read(outputStream, timeout); }

	void PipePacketer::PollEvent(int timeout)
	{
		ByteArray interruptData = _pipe->ReadInterrupt(timeout);
		if (interruptData.empty())
			return;

		HexDump("interrupt", interruptData);
		InputStream stream(interruptData);
		ContainerType containerType;
		u32 size;
		EventCode eventCode;
		u32 sessionId;
		u32 transactionId;
		stream >> size;
		stream >> containerType;
		stream >> eventCode;
		stream >> sessionId;
		stream >> transactionId;
		if (containerType != ContainerType::Event)
			throw std::runtime_error("not an event");
		debug("event ", hex(eventCode, 8));
	}

	namespace
	{
		struct DummyOutputStream final: IObjectOutputStream, public CancellableStream
		{
			size_t Write(const u8 *data, size_t size) override
			{ return size; }
		};

		class HeaderParserObjectOutputStream final: public JoinedObjectOutputStreamBase
		{
			size_t									_offset;
			u32										_transaction;
			FixedSizeByteArrayObjectOutputStreamPtr	_header;
			ByteArrayObjectOutputStreamPtr			_response;
			IObjectOutputStreamPtr					_dataOutput;
			IObjectOutputStreamPtr					_output;
			bool									_valid;
			bool									_finished;
			ResponseType							_responseCode;

		private:
			virtual IObjectOutputStreamPtr GetStream1() const override
			{ return _header; }
			virtual IObjectOutputStreamPtr GetStream2() const override
			{ if (!_output) throw std::runtime_error("no data stream"); return _output; }

			void OnStream1Exhausted() override
			{
				InputStream stream(_header->GetData());
				Response header;
				header.Read(stream);
				if (_transaction && _transaction != header.Transaction)
				{
					error("drop message ", hex(header.ContainerType, 4), ", response: ", hex(header.ResponseType, 4), ", transaction: ", hex(header.Transaction, 8), ", transaction: ", hex(_transaction, 8));
					_valid = false;
					_output = std::make_shared<DummyOutputStream>();
					return;
				}

				switch(header.ContainerType)
				{
				case ContainerType::Data:
					_output			= _dataOutput;
					break;
				case ContainerType::Response:
					_output			= _response;
					_responseCode	= header.ResponseType;
					_finished		= true;
					break;
				default:
					_valid			= false;
					_output			= std::make_shared<DummyOutputStream>();
				}
			}

		public:
			HeaderParserObjectOutputStream(u32 transaction, IObjectOutputStreamPtr dataOutput):
				_offset(0), _transaction(transaction),
				_header(new FixedSizeByteArrayObjectOutputStream(Response::Size)),
				_response(new ByteArrayObjectOutputStream),
				_dataOutput(dataOutput),
				_valid(true), _finished(false) { }

			ResponseType GetResponseCode() const
			{ return _responseCode; }

			const ByteArray &GetResponse() const
			{ return _response->GetData(); }

			bool Valid() const
			{ return _valid; }
			bool Finished() const
			{ return _finished; }

			size_t Write(const u8 *data, size_t size) override
			{
				size_t r = JoinedObjectOutputStreamBase::Write(data, size);
				_offset += r;
				if (!_stream1Exhausted && _offset >= _header->GetData().size()) {
					_stream1Exhausted = true;
					OnStream1Exhausted();
				}
				return r;
			}
		};
		DECLARE_PTR(HeaderParserObjectOutputStream);
	}

	void PipePacketer::Read(u32 transaction, const IObjectOutputStreamPtr &object, ResponseType &code, ByteArray &response, int timeout)
	{
		response.clear();

		HeaderParserObjectOutputStreamPtr parser;
		MessageParsingStreamPtr output;

		while(true)
		{
			if (!parser)
				parser.reset(new HeaderParserObjectOutputStream(transaction, object));
			if (!output)
				output.reset(new MessageParsingStream(parser));

			ReadMessage(output, timeout);
			if (parser->Finished())
			{
				response = parser->GetResponse();
				code = parser->GetResponseCode();
				break;
			}

			if (output->Finished()) {
				parser.reset();
				output.reset();
			}
		}

		//HexDump("response", response);
	}

	void PipePacketer::Read(u32 transaction, ByteArray &data, ResponseType &code, ByteArray &response, int timeout)
	{
		ByteArrayObjectOutputStreamPtr stream(new ByteArrayObjectOutputStream);
		Read(transaction, stream, code, response, timeout);
		data = stream->GetData();
	}

	void PipePacketer::Abort(u32 transaction, int timeout)
	{
		_pipe->Cancel();
		OperationRequest req(OperationCode::CancelTransaction, transaction);
		HexDump("abort control message", req.Data);
		/* 0x21: host-to-device, class specific, recipient - interface, 0x64: cancel request */
		_pipe->GetDevice()->WriteControl(
			(u8)(usb::RequestType::HostToDevice | usb::RequestType::Class | usb::RequestType::Interface),
			0x64,
			0, _pipe->GetInterface()->GetIndex(), req.Data, timeout);
	}


}