File: cmp.c

package info (click to toggle)
coldsync 3.0%2Bpre3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,188 kB
  • ctags: 2,033
  • sloc: ansic: 20,386; perl: 2,302; cpp: 1,640; yacc: 1,102; lex: 802; makefile: 533; sh: 177
file content (186 lines) | stat: -rw-r--r-- 4,605 bytes parent folder | download | duplicates (2)
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
/* cmp.c
 *
 * Implements Palm's Connection Management Protocol (CMP).
 *
 *	Copyright (C) 1999, Andrew Arensburger.
 *	You may distribute this file under the terms of the Artistic
 *	License, as specified in the README file.
 *
 * $Id: cmp.c,v 1.15 2002/04/27 18:36:31 azummo Exp $
 */
#include "config.h"
#include <stdio.h>
#include "palm.h"
#include <pconn/palm_errno.h>
#include <pconn/padp.h>
#include <pconn/cmp.h>
#include <pconn/util.h>

#if HAVE_LIBINTL_H
#  include <libintl.h>		/* For i18n */
#endif	/* HAVE_LIBINTL_H */

int cmp_trace = 0;		/* Debugging level for CMP */
#define CMP_TRACE(n)	if (cmp_trace >= (n))

int
cmp_read(PConnection *pconn,
	 struct cmp_packet *packet)
{
	int err;
	const ubyte *inbuf = NULL;	/* Input data (from PADP) */
	uword inlen;		/* Length of input data */
	const ubyte *rptr;	/* Pointer into buffers (for reading) */

	pconn->palm_errno = PALMERR_NOERR;

	/* Read a PADP packet */
	err = padp_read(pconn, &inbuf, &inlen);
	if (err < 0)
	{
		CMP_TRACE(3)
			fprintf(stderr, "cmp_read: padp_read() returned %d\n",
				err);
		return err;	/* Error */
	}

	CMP_TRACE(7)
	{
		fprintf(stderr, "CMP: Received a packet:\n");
		debug_dump(stderr, "CMP <<<", inbuf, inlen);
	}

	/* Parse the packet */
	rptr = inbuf;
	packet->type = get_ubyte(&rptr);
	packet->flags = get_ubyte(&rptr);
	packet->ver_major = get_ubyte(&rptr);
	packet->ver_minor = get_ubyte(&rptr);
	packet->reserved = 0;	/* Just to be pedantic */
	rptr += 2;		/* Skip over the reserved word */
	packet->rate = get_udword(&rptr);

	CMP_TRACE(5)
		fprintf(stderr,
			"CMP: Got a message: type %d, flags 0x%02x, "
			"v%d.%d, rate %ld\n",
			packet->type,
			packet->flags,
			packet->ver_major,
			packet->ver_minor,
			packet->rate);

	return 0;
}

int
cmp_write(PConnection *pconn,			/* File descriptor */
	  const struct cmp_packet *packet)	/* The packet to send */
{
	int err;
	static ubyte outbuf[CMP_PACKET_LEN];
	ubyte *wptr;		/* Pointer into buffers (for writing) */

	pconn->palm_errno = PALMERR_NOERR;

	CMP_TRACE(5)
		fprintf(stderr,
			"CMP: Sending type %d, flags 0x%02x, v%d.%d, "
			"rate %ld\n",
			packet->type,
			packet->flags,
			packet->ver_major,
			packet->ver_minor,
			packet->rate);

	/* Build the outgoing packet from 'packet' */
	wptr = outbuf;
	put_ubyte(&wptr, packet->type);
	put_ubyte(&wptr, packet->flags);
	put_ubyte(&wptr, packet->ver_major);
	put_ubyte(&wptr, packet->ver_minor);
	put_uword(&wptr, 0);
	put_udword(&wptr, packet->rate);

	/* Send the packet as a PADP packet */
	CMP_TRACE(7)
	{
		fprintf(stderr, "CMP: Sending a packet:\n");
		debug_dump(stderr, "CMP >>>", outbuf, CMP_PACKET_LEN);
	}

	err = padp_write(pconn, outbuf, CMP_PACKET_LEN);
		/* Error-handling: just inherit the error from padp_write() */

	return err;
}

/* cmp_accept
 * Negotiate the CMP part of establishing a connection with the Palm. 'bps'
 * gives the desktop's desired speed. 0 means "I don't care. Whatever the
 * Palm suggests".
 * Returns the speed in bps if successful, or ~0 in case of error.
 * This function is here, and not in PConnection_* because it's used for
 * both serial and USB connections.
 */
udword
cmp_accept(PConnection *pconn, udword bps)
{
	int err;
	struct cmp_packet cmpp;

	do {
		CMP_TRACE(5)
			fprintf(stderr, "===== Waiting for wakeup packet\n");

		err = cmp_read(pconn, &cmpp);
		if (err < 0)
		{
			if (PConn_get_palmerrno(pconn) == PALMERR_TIMEOUT)
				continue;
	
			fprintf(stderr, _("Error during cmp_read: (%d) %s.\n"),
				(int) PConn_get_palmerrno(pconn),
				_(palm_strerror(PConn_get_palmerrno(pconn))));
			return ~0;
		}
	} while (cmpp.type != (ubyte) CMP_TYPE_WAKEUP);

	CMP_TRACE(5)
		fprintf(stderr, "===== Got a wakeup packet\n");

	/* Compose a reply */
	cmpp.type = (ubyte) CMP_TYPE_INIT;
	cmpp.ver_major = CMP_VER_MAJOR;
	cmpp.ver_minor = CMP_VER_MINOR;
	if (bps != 0)
		cmpp.rate = bps;
	cmpp.flags = CMP_IFLAG_CHANGERATE;
		/* At this point, the protocol allows us to leave the
		 * CMP_IFLAG_CHANGERATE flag at 0, to indicate that the
		 * main sync should continue at whatever rate we're
		 * currently at (9600 bps). In practice, however, nobody's
		 * going to want to sync at 9600 bps, so always set the
		 * flag.
		 */

	CMP_TRACE(5)
		fprintf(stderr, "===== Sending INIT packet\n");
	err = cmp_write(pconn, &cmpp);
	if (err < 0)
		return ~0;

	CMP_TRACE(5)
		fprintf(stderr, "===== Finished sending INIT packet\n");

	CMP_TRACE(4)
		fprintf(stderr, "Initialized CMP, returning speed %ld\n",
			cmpp.rate);
	return cmpp.rate;
}

/* This is for Emacs's benefit:
 * Local Variables: ***
 * fill-column:	75 ***
 * End: ***
 */