aboutsummaryrefslogtreecommitdiff
path: root/ROMTransfer/Source/SerialPortTools.c
blob: 0428a4388d5ee97af1cb5ded39cebee91538211d (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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1998-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.
\* ===================================================================== */

#define PILOT_PRECOMPILED_HEADERS_OFF

#include <PalmOS.h>
#include <SerialMgrOld.h>	// SerSettingsType

#include "SerialPortTools.h"

// for Handspring API access
#define	sysTrapHsSelector				sysTrapOEMDispatch
#define hsSelExtKeyboardEnable			5
#define SYS_SEL_TRAP(trapNum, selector) \
	_SYSTEM_API(_CALL_WITH_16BIT_SELECTOR)(_SYSTEM_TABLE, trapNum, selector)

Err		HsExtKeyboardEnable (Boolean enable)
				SYS_SEL_TRAP (sysTrapHsSelector, hsSelExtKeyboardEnable);

#ifndef serMgrVersion

	// Bits of the Palm OS 4.0 Serial Manager we need until
	// the SDK is available

	#define sysFtrNewSerialVersion		2

	#define sysSerialOpenV4				24
	#define sysSerialOpenBkgndV4		25
	#define sysSerialCustomControl		26

	//
	// Open Configuration Structure
	//
	typedef struct SrmOpenConfigType {
		UInt32 baud;			// Baud rate that the connection is to be opened at.
								// Applications that use drivers that do not require
								// baud rates can set this to zero or any other value.
								// Drivers that do not require a baud rate should 
								// ignore this field
		UInt32 function;		//	Designates the function of the connection. A value
								// of zero indictates default behavior for the protocol.
								// Drivers that do not support multiple functions should
								// ignore this field.	
		MemPtr drvrDataP;		// Pointer to driver specific data.
		UInt16 drvrDataSize;	// Size of the driver specific data block.	
		UInt32 sysReserved1;    // System Reserved
		UInt32 sysReserved2;    // System Reserved 
	} SrmOpenConfigType;

	typedef SrmOpenConfigType* SrmOpenConfigPtr;

	Err SrmExtOpen(UInt32 port, SrmOpenConfigType* configP, UInt16 configSize, UInt16 *newPortIdP)
		SERIAL_TRAP(sysSerialOpenV4);

#endif


// Globals.

UInt16			gPortID;
int				gSerialManagerVersion;


// ---------------------------------------------------------------------------
//		¥ Comm_Initialize
// ---------------------------------------------------------------------------
// Opens the communications port.

Int16 Comm_Initialize (UInt32 iBaud)
{
	Err					err = errNone;
	SrmOpenConfigType	config;
	SerSettingsType		serSettings;
	UInt32				srmSettings;
	UInt16				srmSettingsSize;
	UInt32				systemVersion;
	UInt32				ftrValue;

	// Exit if communications have already been established.

	if (gPortID)
		return (serErrAlreadyOpen);

	// Clear the globals.

	gPortID					= 0;
	gSerialManagerVersion	= 0;

	// Determine what version Serial Manager we have:
	//
	//	0 = undetermined
	//	1 = Original driver in Pilot 1000/5000
	//	2 = Updated driver in PalmPilot (new Send/Receive calls)
	//	3 = New Serial Manager (Srm calls)
	//	4 = Updated Serial Manager (includes SrmExtOpen call)

	err = FtrGet (sysFileCSystem, sysFtrNumROMVersion, &systemVersion);

	ErrFatalDisplayIf (err, "Unable to determine System version");

	if (sysGetROMVerMajor(systemVersion) < 2)
	{
		gSerialManagerVersion = 1;
	}
	else
	{
		err = FtrGet (sysFileCSerialMgr, sysFtrNewSerialPresent, &ftrValue);

		if (err || ftrValue == 0)
		{
			gSerialManagerVersion = 2;
		}
		else
		{
			err = FtrGet (sysFileCSerialMgr, sysFtrNewSerialVersion, &ftrValue);

			if (err)
			{
				gSerialManagerVersion = 3;
			}
			else if (ftrValue <= 2)
			{
				gSerialManagerVersion = 3;

				// Palm OS 3.5.2 for Handspring implements
				// sysFtrNewSerialVersion and returns a feature value of 2.
				// Palm OS 4.0 also implements sysFtrNewSerialVersion and
				// returns a value of 2.  However, the two serial managers
				// are different: the latter implements SrmExtOpen.  In
				// order to differentiate between the two, we have to check
				// the OS version.

				if (ftrValue == 2 && sysGetROMVerMajor (systemVersion) >= 4)
				{
					gSerialManagerVersion = 4;
				}
			}
			else
			{
				// ftrValue should be at least 3 for versions of the
				// Serial Manager that implement SrmExtOpen.

				gSerialManagerVersion = ftrValue + 1;
			}
		}
	}

	// <chg 02-01-2000 BP>  If on Handspring device, disable the keyboard 
	// thread before opening the serial library.  

	if (FtrGet ('hsEx', 0, &ftrValue) == errNone)
	{
		HsExtKeyboardEnable (false);
	}

	// Open the serial port in a manner appropriate for this platform.

	if (gSerialManagerVersion <= 2)
	{
		// Find the serial library.

		err = SysLibFind ("Serial Library", &gPortID);
		if (err != errNone)
			return (err);

		// Open the serial port.

		err = SerOpen (gPortID, kPort_Pilot, iBaud);
		if (err != errNone)
			goto Exit;

		// Set the serial attributes.

		serSettings.baudRate	= iBaud;
		serSettings.flags		= serSettingsFlagBitsPerChar8 | serSettingsFlagStopBits1;		
		serSettings.ctsTimeout	= serDefaultCTSTimeout;

		err = SerSetSettings (gPortID, &serSettings);
		if (err != errNone)
			goto Exit;
	}
	else	// if (gSerialManagerVersion >= 3)
	{
		// Open the serial port.

		if (gSerialManagerVersion == 3)
		{
			err = SrmOpen (serPortLocalHotSync, iBaud, &gPortID);
		}
		else
		{
			MemSet (&config, sizeof (config), 0);

			config.baud		= iBaud;
			config.function	= 'ROMX';

			err = SrmExtOpen (	serPortLocalHotSync,
								&config, sizeof (config),
								&gPortID);
		}

		if (err != errNone)
			goto Exit;

		// Set the serial attributes.

		srmSettings		= srmSettingsFlagBitsPerChar8 | srmSettingsFlagStopBits1;
		srmSettingsSize	= sizeof (srmSettings);
		err = SrmControl (gPortID, srmCtlSetFlags,
							&srmSettings, &srmSettingsSize);
		if (err != errNone)
			goto Exit;
	}

Exit:
	if (err != errNone)
	{
		if (gPortID)
		{
			SrmClose (gPortID);
			gPortID = 0;
		}
	}

	return err;
}


// ---------------------------------------------------------------------------
//		¥ Comm_Receive
// ---------------------------------------------------------------------------

Err Comm_Receive (void* bufP, UInt32 count, UInt32 timeout)
{
	Err	err;

	if (gSerialManagerVersion == 1)
	{
		err = SerReceive10 (gPortID, bufP, count, timeout);
	}
	else if (gSerialManagerVersion == 2)
	{
		(void) SerReceive (gPortID, bufP, count, timeout, &err);
	}
	else	// if (gSerialManagerVersion >= 3)
	{
		(void) SrmReceive (gPortID, bufP, count, timeout, &err);
	}

	return err;
}


// ---------------------------------------------------------------------------
//		¥ Comm_Send
// ---------------------------------------------------------------------------

#define CORRUPT_SENDS 0

#if CORRUPT_SENDS
static UInt32 PrvRange (UInt32 maxValue)
{
	static int initialized;
	if (!initialized)
	{
		initialized = true;
		SysRandom (1);
	}

	return (SysRandom (0) * maxValue) / (1UL + sysRandomMax);
}
#endif


Err Comm_Send (void* bufP, UInt32 count)
{	
	Err	err;

#if CORRUPT_SENDS
	void* buf2 = MemPtrNew (count);

	MemMove (buf2, bufP, count);

	bufP = buf2;

	if (PrvRange (100) <= (count / 100))
	{
		// Corrupt a character

		((char*) bufP)[PrvRange (count)]++;
	}
	else if (PrvRange (100) <= (count / 100))
	{
		// Drop a character

		UInt32	index = PrvRange (count);

		if (count - index - 1 > 0)
		{
			MemMove ((char*) bufP + index,
					(char*) bufP + index + 1,
					count - index - 1);
		}
		count--;
	}
#endif

	if (gSerialManagerVersion == 1)
	{
		err = SerSend10 (gPortID, bufP, count);
	}
	else if (gSerialManagerVersion == 2)
	{
		(void) SerSend (gPortID, bufP, count, &err);
	}
	else	// if (gSerialManagerVersion >= 3)
	{
		(void) SrmSend (gPortID, bufP, count, &err);
	}

#if CORRUPT_SENDS
	MemPtrFree (bufP);
#endif

	return err;
}


// ---------------------------------------------------------------------------
//		¥ Comm_Dispose
// ---------------------------------------------------------------------------
// Closes the communications port.

void Comm_Dispose (void)
{
	// Exit if communications have not been established.

	if (!gPortID)
		return;

	// Close the serial port.

	if (gSerialManagerVersion <= 2)
	{
		SerClose (gPortID);
	}
	else	// if (gSerialManagerVersion >= 3)
	{
		SrmClose (gPortID);
	}

	// Clear the globals.

	gPortID = 0;
}


// ---------------------------------------------------------------------------
//		¥ Comm_ClearError
// ---------------------------------------------------------------------------
// Clears the current serial errors.

void Comm_ClearError (void)
{
	// Exit if communications have not been established.

	if (!gPortID)
		return;

	if (gSerialManagerVersion <= 2)
	{
		SerClearErr (gPortID);
	}
	else	// if (gSerialManagerVersion >= 3)
	{
		SrmClearErr (gPortID);
	}
}