aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/EmTransportUSB.cpp
blob: 04e04e6901f3e8d4a6fb79608b9507e7440634e2 (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* -*- 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 "EmTransportUSB.h"

#include "EmErrCodes.h"			// kError_CommOpen
#include "Logging.h"			// LogSerial


//EmTransportUSB::OpenPortList	EmTransportUSB::fgOpenPorts;

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


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

EmTransportUSB::EmTransportUSB (void) :
	fHost (NULL),
	fConfig (),
	fCommEstablished (false)
{
	this->HostConstruct ();
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB c'tor
 *
 * DESCRIPTION:	Constructor.  Initialize our data members.
 *
 * PARAMETERS:	desc - descriptor information used when opening
 *					the USB port.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

EmTransportUSB::EmTransportUSB (const EmTransportDescriptor& /* desc */) :
	fHost (NULL),
	fConfig (),
	fCommEstablished (false)
{
	ConfigUSB	config;

	this->HostConstruct ();
	this->SetConfig (config);
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB c'tor
 *
 * DESCRIPTION:	Constructor.  Initialize our data members.
 *
 * PARAMETERS:	config - configuration information used when opening
 *					the USB port.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

EmTransportUSB::EmTransportUSB (const ConfigUSB& config) :
	fHost (NULL),
	fConfig (),
	fCommEstablished (false)
{
	this->HostConstruct ();
	this->SetConfig (config);
}


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

EmTransportUSB::~EmTransportUSB (void)
{
	this->Close ();
	this->HostDestruct ();
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::Open
 *
 * DESCRIPTION:	Open the transport using the information provided
 *				either in the constructor or with SetConfig.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::Open (void)
{
	PRINTF ("EmTransportUSB::Open...");

	// Exit if communications have already been established.

	if (fCommEstablished)
	{
		PRINTF ("EmTransportUSB::Open: USB port already open...leaving...");
		return kError_CommOpen;
	}

//	EmAssert (fgOpenPorts.find (fConfig.fPort) == fgOpenPorts.end ());

	ErrCode	err = this->HostOpen ();

	if (!err)
		err = this->HostSetConfig (fConfig);

	if (err)
	{
		this->HostClose ();
	}
	else
	{
		fCommEstablished = true;
//		fgOpenPorts[fConfig.fPort] = this;
	}

	if (err)
		PRINTF ("EmTransportUSB::Open: err = %ld", err);

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::Close
 *
 * DESCRIPTION:	Close the transport.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::Close (void)
{
	if (!fCommEstablished)
	{
		PRINTF ("EmTransportUSB::Close: USB port not open...leaving...");
		return kError_CommNotOpen;
	}

	fCommEstablished = false;
//	fgOpenPorts.erase (fConfig.fPort);

	ErrCode	err = this->HostClose ();

	if (err)
		PRINTF ("EmTransportUSB::Close: err = %ld", err);

	PRINTF ("EmTransportUSB::Close: now closed...");

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::Read
 *
 * DESCRIPTION:	Read up to the given number of bytes, storing them in
 *				the given buffer.
 *
 * 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::Read (long& len, void* data)
{
	PRINTF ("EmTransportUSB::Read...");

	if (!fCommEstablished)
	{
		PRINTF ("EmTransportUSB::Read: port not open, leaving");
		return kError_CommNotOpen;
	}

	ErrCode	err = this->HostRead (len, data);

	if (err)
		PRINTF ("EmTransportUSB::Read: err = %ld", err);
	else
		if (LogSerialData ())
			LogAppendData (data, len, "EmTransportUSB::Read: reading %ld bytes.", len);
		else
			PRINTF ("EmTransportUSB::Read: reading %ld bytes", len);

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::Write
 *
 * DESCRIPTION:	Write up the the given number of bytes, using the data
 *				in the given buffer.
 *
 * 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::Write (long& len, const void* data)
{
	PRINTF ("EmTransportUSB::Write...");

	if (!fCommEstablished)
	{
		PRINTF ("EmTransportUSB::Write: port not open, leaving");
		return kError_CommNotOpen;
	}

	ErrCode	err = this->HostWrite (len, data);

	if (err)
		PRINTF ("EmTransportUSB::Write: err = %ld", err);
	else
		if (LogSerialData ())
			LogAppendData (data, len, "EmTransportUSB::Write: writing %ld bytes.", len);
		else
			PRINTF ("EmTransportUSB::Write: writing %ld bytes", len);

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::CanRead
 *
 * 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::CanRead (void)
{
	return this->HostCanRead ();
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::CanWrite
 *
 * 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::CanWrite (void)
{
	return this->HostCanRead ();
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::BytesInBuffer
 *
 * 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:	minBytes - try to buffer at least this many bytes.
 *					Return when we have this many bytes buffered, or
 *					until some small timeout has occurred.
 *
 * RETURNED:	Number of bytes that can be read.
 *
 ***********************************************************************/

long EmTransportUSB::BytesInBuffer (long minBytes)
{
	if (!fCommEstablished)
		return 0;

	return this->HostBytesInBuffer (minBytes);
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::GetSpecificName
 *
 * DESCRIPTION:	Returns the port name, or host address, depending on the
 *				transport in question.
 *
 * PARAMETERS:	
 *
 * RETURNED:	string, appropriate to the transport in question.
 *
 ***********************************************************************/
 
 string EmTransportUSB::GetSpecificName (void)
 {
 	return "USB";
 }


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::SetConfig
 *
 * DESCRIPTION:	Set the configuration to be used when opening the port,
 *				or to reconfigure a currently open port.
 *
 * PARAMETERS:	config - the configuration to use.
 *
 * RETURNED:	0 if no error.
 *
 ***********************************************************************/

ErrCode EmTransportUSB::SetConfig (const ConfigUSB& config)
{
	PRINTF ("EmTransportUSB::SetConfig...");

	if (config == fConfig)
	{
		PRINTF ("EmTransportUSB::SetConfig: Config unchanged, so not setting settings...");
		return errNone;
	}

	fConfig = config;

	// Exit if communications have not been established.

	if (!fCommEstablished)
	{
		PRINTF ("EmTransportUSB::SetConfig: USB port closed, so not setting settings...");
		return kError_CommNotOpen;
	}

	ErrCode	err = this->HostSetConfig (fConfig);

	if (err)
		PRINTF ("EmTransportUSB::SetConfig: err = %ld", err);

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::GetConfig
 *
 * DESCRIPTION:	Return the configuration specified in the constructor
 *				or in the last call to SetConfig.
 *
 * PARAMETERS:	config - config object to receive the settings.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void EmTransportUSB::GetConfig (ConfigUSB& config)
{
	config = fConfig;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::GetTransport
 *
 * DESCRIPTION:	Return any transport object currently using the port
 *				specified in the given configuration.
 *
 * PARAMETERS:	config - The configuration object containing information
 *					on a port in which we're interested.  All or some
 *					of the information in this object is used when
 *					searching for a transport object already utilizing
 *					the port.
 *
 * RETURNED:	Any found transport object.  May be NULL.
 *
 ***********************************************************************/

EmTransportUSB* EmTransportUSB::GetTransport (const ConfigUSB& /*config*/)
{
#if 0
	OpenPortList::iterator	iter = fgOpenPorts.find (config.fPort);

	if (iter == fgOpenPorts.end ())
		return NULL;

	return iter->second;
#endif

	return NULL;
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::GetDescriptorList
 *
 * DESCRIPTION:	Return the list of USB ports on this computer.  Used
 *				to prepare a menu of USB port choices.
 *
 * PARAMETERS:	nameList - port names are added to this list.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void EmTransportUSB::GetDescriptorList (EmTransportDescriptorList& descList)
{
	descList.clear ();

	if (EmTransportUSB::HostHasUSB ())
	{
		descList.push_back (EmTransportDescriptor (kTransportUSB));
	}
}


#pragma mark -

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

EmTransportUSB::ConfigUSB::ConfigUSB ()
{
}
			

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

EmTransportUSB::ConfigUSB::~ConfigUSB (void)
{
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::ConfigUSB::NewTransport
 *
 * DESCRIPTION:	Create a new transport object based on the configuration
 *				information in this object.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	The new transport object.
 *
 ***********************************************************************/

EmTransport* EmTransportUSB::ConfigUSB::NewTransport (void)
{
	return new EmTransportUSB (*this);
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::ConfigUSB::GetTransport
 *
 * DESCRIPTION:	Return any transport object currently using the port
 *				specified in the given configuration.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Any found transport object.  May be NULL.
 *
 ***********************************************************************/

EmTransport* EmTransportUSB::ConfigUSB::GetTransport (void)
{
	return EmTransportUSB::GetTransport (*this);
}


/***********************************************************************
 *
 * FUNCTION:	EmTransportUSB::ConfigUSB::operator==
 *
 * DESCRIPTION:	Compare two Config objects to each other
 *
 * PARAMETERS:	other - the object to compare "this" to.
 *
 * RETURNED:	True if the objects are equivalent.
 *
 ***********************************************************************/

bool EmTransportUSB::ConfigUSB::operator==(const ConfigUSB& /*other*/) const
{
	return true;
}