aboutsummaryrefslogtreecommitdiff
path: root/SrcUnix/EmTransportUSBUnix.cpp
blob: f8867a01bb352cd62794e6ed56310b0b7e989cee (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

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

#include "EmCommon.h"
#include "EmTransportUSBUnix.h"

#include "EmTransportSerial.h"	// EmTransportSerial
#include "Logging.h"			// LogSerial, LogAppendMsg

#define PRINTF	if (!LogSerial ()) ; else LogAppendMsg


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostHasUSB
 *
 * DESCRIPTION:	Return whether or not USB facilities are available.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if the host has a USB port and we can use it.
 *				False otherwise.
 *
 ***********************************************************************/

Bool EmTransportUSB::HostHasUSB (void)
{
	return false;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostConstruct
 *
 * DESCRIPTION:	Construct platform-specific objects/data.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	The platform-specific serial object.
 *
 ***********************************************************************/

void EmTransportUSB::HostConstruct (void)
{
	fHost = new EmHostTransportUSB;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostDestruct
 *
 * DESCRIPTION:	Destroy platform-specific objects/data.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void EmTransportUSB::HostDestruct (void)
{
	delete fHost;
	fHost = NULL;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostOpen
 *
 * DESCRIPTION:	Open the serial port in a platform-specific fashion.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::HostOpen (void)
{
	fHost->fOpenLocally = true;
	
	fHost->UpdateOpenState ();

	return errNone;

}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostClose
 *
 * DESCRIPTION:	Close the serial port in a platform-specific fashion.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::HostClose (void)
{
	fHost->fOpenLocally = false;
	fHost->fOpenRemotely = false;

	ErrCode	err = fHost->fSerialTransport->Close ();

	return err;

}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostRead
 *
 * DESCRIPTION:	Read bytes from the port in a platform-specific fashion.
 *
 * PARAMETERS:	len - maximum number of bytes to read.
 *				data - buffer to receive the bytes.
 *
 * RETURNED:	0 if no error.  The number of bytes actually read is
 *				returned in len if there was no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::HostRead (long& len, void* data)
{
	ErrCode	err = serErrTimeOut;
	len = 0;

	fHost->UpdateOpenState ();

	if (!fHost->FakeOpenConnection ())
	{
		err = fHost->fSerialTransport->Read (len, data);

		if (err)
		{
			fHost->fOpenRemotely = false;
			fHost->fSerialTransport->Close ();	// Close it
		}
	}

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostWrite
 *
 * DESCRIPTION:	Write bytes to the port in a platform-specific fashion.
 *
 * PARAMETERS:	len - number of bytes in the buffer.
 *				data - buffer containing the bytes.
 *
 * RETURNED:	0 if no error.  The number of bytes actually written is
 *				returned in len if there was no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::HostWrite (long& len, const void* data)
{
	ErrCode	err = errNone;
	len = 0;

	fHost->UpdateOpenState ();

	if (!fHost->FakeOpenConnection ())
	{
		err = fHost->fSerialTransport->Write (len, data);

		if (err)
		{
			fHost->fOpenRemotely = false;
			fHost->fSerialTransport->Close ();	// Close it
		}
	}

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostCanRead
 *
 * DESCRIPTION:	Return whether or not the transport is available for
 *				a read operation (that is, it's connected to another
 *				entity).  Does NOT indicate whether or not there are
 *				actually any bytes available to be read.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	True if so.
 *
 ***********************************************************************/

Bool EmTransportUSB::HostCanRead (void)
{
	return fHost->fOpenLocally && fHost->fOpenRemotely;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostCanWrite
 *
 * DESCRIPTION:	Return whether or not the transport is available for
 *				a write operation (that is, it's connected to another
 *				entity).  Does NOT indicate whether or not there is
 *				actually any room in the transport's internal buffer
 *				for the data being written.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	True if so.
 *
 ***********************************************************************/

Bool EmTransportUSB::HostCanWrite (void)
{
	return fHost->fOpenLocally && fHost->fOpenRemotely;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostBytesInBuffer
 *
 * DESCRIPTION:	Returns the number of bytes that can be read with the
 *				Read method.  Note that bytes may be received in
 *				between the time BytesInBuffer is called and the time
 *				Read is called, so calling the latter with the result
 *				of the former is not guaranteed to fetch all received
 *				and buffered bytes.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Number of bytes that can be read.
 *
 ***********************************************************************/

long EmTransportUSB::HostBytesInBuffer (long minBytes)
{
	long	bytesRead = 0;
	
	fHost->UpdateOpenState ();

	if (!fHost->FakeOpenConnection ())
	{
		try
		{
			bytesRead = fHost->fSerialTransport->BytesInBuffer (minBytes);
		}
		catch (ErrCode)
		{
			fHost->fOpenRemotely = false;
			fHost->fSerialTransport->Close ();
		}
	}

	return bytesRead;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::HostSetConfig
 *
 * DESCRIPTION:	Configure the serial port in a platform-specific
 *				fasion.  The port is assumed to be open.
 *
 * PARAMETERS:	config - configuration information.
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::HostSetConfig (const ConfigUSB& /*config*/)
{
	ErrCode	result = errNone;

	return result;
}


#pragma mark -

/***********************************************************************
 *
 * FUNCTION:	EmHostTransportUSB c'tor
 *
 * DESCRIPTION:	Constructor.  Initialize our data members.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

EmHostTransportUSB::EmHostTransportUSB (void) :
	fOpenLocally (false),
	fOpenRemotely (false),
	fSerialTransport (NULL)
{
	EmTransportSerial::ConfigSerial	config;
	
	config.fPort = "USB";

	fSerialTransport = dynamic_cast<EmTransportSerial*> (config.NewTransport ());
}


/***********************************************************************
 *
 * FUNCTION:	EmHostTransportUSB d'tor
 *
 * DESCRIPTION:	Destructor.  Delete our data members.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

EmHostTransportUSB::~EmHostTransportUSB (void)
{
	delete fSerialTransport;
}


/***********************************************************************
 *
 * FUNCTION:	EmHostTransportUSB::UpdateOpenState
 *
 * DESCRIPTION:	If we want the serial port transport open, and it's not
 *				open, then try opening it.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

void EmHostTransportUSB::UpdateOpenState (void)
{
	// If we need to open the USB driver and haven't done so, yet, then
	// try to do so.

	if (fOpenLocally &&							// Do we need the USB driver open?
		!fOpenRemotely &&						// Is it closed?
		fSerialTransport->Open () == errNone)	// Could we open it?
	{
		fOpenRemotely = true;					// Say it's open
	}
}


/***********************************************************************
 *
 * FUNCTION:	EmHostTransportUSB::FakeOpenConnection
 *
 * DESCRIPTION:	If we've asked the driver to be open, but could not do
 *				so because the USB device is not online, then try to
 *				pretend that we're talking with a non-responsive serial
 *				device.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if HostOpen has been called, but the USB device is
 *				not yet online and we haven't been able to call Open on
 *				the underlying serial transport.  False otherwise.
 *
 ***********************************************************************/

Bool EmHostTransportUSB::FakeOpenConnection (void)
{
	return fOpenLocally && !fOpenRemotely;
}