File: CFontz633io.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (654 lines) | stat: -rw-r--r-- 19,266 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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/** \file server/drivers/CFontz633io.c
 * I/O routines for the \c CFontzPacket driver. Currently the CFA-631,
 * CFA-533, CFA-633 and CFA-635 LCDs use this type of protocol.
 *
 * \todo  Add reporting (shared/report.h) to the send_#_message functions
 *        if send failed (or an error response is received).
 * \todo  Make the content of a response packet available to the driver.
 * \note  Modifying send_#_functions makes sound only if the CFontz633 driver
 *        has been removed.
 */

/*-
  ===========================================================================
  633 Test code for linux.
  Copyright 2002, David GLAUDE
  Partial copyright
  2001 Crystalfontz America, Inc.
  brent@crystalfontz.com
  Written by Brent A. Crosby
  www.crystalfontz.com
  ===========================================================================

  Partial copyright (C) 2005-2006 Peter Marschall

  Most of the code in this file is based on the translation from the windows
  code.

  Intensive code analysis performed by Peter Marschall showed that,
  at the time of the release of the GPL'ed interfacing code for the
  CF631/633 from CrystalFontz by Brent A. Crosby in 2004, the code
  in this file was either written by David Glaude for LCDproc or
  identical to the GPL'ed code or so close to it that the differences
  are negligible.

  For detailed information about the code analysis, please
  contact the LCDproc maintainer, Peter Marschall <peter@adpm.de>

  This file is hereby released under the terms below:

    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
    any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301

 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <unistd.h>
#include <stdio.h>
#include <string.h>

#if defined(HAVE_SYS_SELECT_H)
# include <sys/select.h>
#else
# include <sys/time.h>
# include <sys/types.h>
#endif

#include "CFontz633io.h"

/* Return values for the check_for_packet() */
#define TRY_AGAIN 0
#define GOOD_MSG 1
#define GIVE_UP 2

/* define CFONTZ633_WRITE_DELAY to use select when waiting for packet acknowledgement */
#if !defined(CFONTZ633_WRITE_DELAY)
# define CFONTZ633_WRITE_DELAY 250
#endif


/* static local functions */
static void send_packet(int fd, COMMAND_PACKET *out, COMMAND_PACKET *in);
static int  get_crc(unsigned char *buf, int len, int seed);
static int  test_packet(int fd, unsigned char response, COMMAND_PACKET *in);
static int  check_for_packet(int fd, COMMAND_PACKET *in, unsigned char expected_length);
#ifdef DEBUG
static void print_packet(COMMAND_PACKET *packet);
#endif


/** \addtogroup CFA_KeyRing
 *
 * KeyRing handling functions.
 * This separates the producer from the consumer.
 * It is just a small fifo of unsigned char.
 * @{
 */

/* Global variable */
KeyRing keyring;

/**
 * Initialize/empty key ring by resetting its read & write pointers.
 * \param kr  Pointer to KeyRing.
 */
void EmptyKeyRing(KeyRing *kr)
{
	kr->head = kr->tail = 0;
}


/**
 * Add byte to key ring.
 * \param kr   Pointer to KeyRing.
 * \param key  Key byte to add.
 * \retval 1  Success (byte added).
 * \retval 0  Failure (key ring is full).
 */
int AddKeyToKeyRing(KeyRing *kr, unsigned char key)
{
	if (((kr->head + 1) % KEYRINGSIZE) != (kr->tail % KEYRINGSIZE)) {
		kr->contents[kr->head % KEYRINGSIZE] = key;
		kr->head = (kr->head + 1) % KEYRINGSIZE;
		return 1;
	}

	/* KeyRing overflow: do not accept extra key */
	return 0;
}


/**
 * Get byte from key ring.
 * \param kr  Pointer to KeyRing.
 * \retval retval  Byte from KeyRing.
 * \retval '\0'    Failure (key ring is empty).
 */
unsigned char GetKeyFromKeyRing(KeyRing *kr)
{
	unsigned char retval = '\0';

	kr->tail %= KEYRINGSIZE;

	if ((kr->head % KEYRINGSIZE) != kr->tail) {
		retval = kr->contents[kr->tail];
	        kr->tail = (kr->tail + 1) % KEYRINGSIZE;
	}

	return retval;
}
/** @} */



/**
 * Send message with arguments to the given handle.
 * \param fd    File handle to write to.
 * \param msg   Command byte to write.
 * \param len   Length (in bytes) of data following.
 * \param data  Pointer to command argument data.
 */
void send_bytes_message(int fd, unsigned char msg, int len, unsigned char *data)
{
	COMMAND_PACKET out;
	COMMAND_PACKET in;

	out.command = msg;
	out.data_length = (unsigned char) ((len > MAX_DATA_LENGTH) ? MAX_DATA_LENGTH : len);
	memcpy(out.data, data, out.data_length);

	/* send message & calc CRC */
	send_packet(fd, &out, &in);
}


/**
 * Send message with one byte argument to the given handle.
 * \param fd     File handle to write to.
 * \param msg    Command byte to write.
 * \param value  Command argument.
 */
void send_onebyte_message(int fd, unsigned char msg, unsigned char value)
{
	COMMAND_PACKET out;
	COMMAND_PACKET in;

	out.command = msg;
	out.data_length = 1;
	out.data[0] = value;

	/* send message & calc CRC */
	send_packet(fd, &out, &in);
}


/**
 * Send message without arguments to the given handle.
 * \param fd    File handle to write to.
 * \param msg   Command byte to write.
 */
void send_zerobyte_message(int fd, unsigned char msg)
{
	COMMAND_PACKET out;
	COMMAND_PACKET in;

	out.command = msg;
	out.data_length = 0;

	/* send message & calc CRC */
	send_packet(fd, &out, &in);
}


/**
 * Send out to the given handle; calc & send CRC when doing so.
 * \param fd    File handle to write to.
 * \param out   Pointer to COMMAND_PACKET structure to write.
 * \param in    Pointer to COMMAND_PACKET structure to read after write.
 */
static void
send_packet(int fd, COMMAND_PACKET *out, COMMAND_PACKET *in)
{
	unsigned char CRC[2];

	write(fd, &out->command, 1);
	write(fd, &out->data_length, 1);
	if (out->data_length > 0)
		write(fd, out->data, out->data_length);

	/* calculate & send the CRC: convert to bytes manually to avoid endianess issues */
	out->crc = get_crc((unsigned char *) out, out->data_length + 2, 0xFFFF);
	CRC[0] = out->crc & 0xFF;
	CRC[1] = (out->crc >> 8) & 0xFF;
	write(fd, CRC, 2);

	/**** TEST STUFF ****/
	//print_packet(out);

	/* Every time we send a message, we also check for an incoming one. */
	test_packet(fd, 0x40 | out->command, in);
}


/**
 * Calculate CRC over given buffer with given length.
 * \param buf   Byte buffer.
 * \param len   Length of byte buffer.
 * \param seed  CRC seed value.
 * \return  CRC calulated.
 */
/* According to the "Painless Guide to CRC error dectectin algorithmsi
 * (http://www.repairfaq.org/filipg/LINK/F_crc_v3.html) this is the table driven
 * implementation of a CRC with the following parameters:
 * - WIDTH:  16		(16 bit CRC)
 * - POLY:   0x1021	(generating polynomial)
 * - INIT:   0xFFFF	(seed value for the register when starting the algorithm)
 * - REFIN:  TRUE	(reflect [i.e. bit-swap] each input byte before being processed)
 * - REFOUT: TRUE	(reflect [i.e. bit-swap] the result before the XOROUT phase)
 * - XOROUT: 0xFFFF	(value to xor the result before returning it as crc)
 */
static int
get_crc(unsigned char *buf, int len, int seed)
{

	/* CRC lookup table to avoid bit-shifting loops. */
	static const word crcLookupTable[256] = {
		0x00000, 0x01189, 0x02312, 0x0329B, 0x04624, 0x057AD, 0x06536, 0x074BF,
		0x08C48, 0x09DC1, 0x0AF5A, 0x0BED3, 0x0CA6C, 0x0DBE5, 0x0E97E, 0x0F8F7,
		0x01081, 0x00108, 0x03393, 0x0221A, 0x056A5, 0x0472C, 0x075B7, 0x0643E,
		0x09CC9, 0x08D40, 0x0BFDB, 0x0AE52, 0x0DAED, 0x0CB64, 0x0F9FF, 0x0E876,
		0x02102, 0x0308B, 0x00210, 0x01399, 0x06726, 0x076AF, 0x04434, 0x055BD,
		0x0AD4A, 0x0BCC3, 0x08E58, 0x09FD1, 0x0EB6E, 0x0FAE7, 0x0C87C, 0x0D9F5,
		0x03183, 0x0200A, 0x01291, 0x00318, 0x077A7, 0x0662E, 0x054B5, 0x0453C,
		0x0BDCB, 0x0AC42, 0x09ED9, 0x08F50, 0x0FBEF, 0x0EA66, 0x0D8FD, 0x0C974,
		0x04204, 0x0538D, 0x06116, 0x0709F, 0x00420, 0x015A9, 0x02732, 0x036BB,
		0x0CE4C, 0x0DFC5, 0x0ED5E, 0x0FCD7, 0x08868, 0x099E1, 0x0AB7A, 0x0BAF3,
		0x05285, 0x0430C, 0x07197, 0x0601E, 0x014A1, 0x00528, 0x037B3, 0x0263A,
		0x0DECD, 0x0CF44, 0x0FDDF, 0x0EC56, 0x098E9, 0x08960, 0x0BBFB, 0x0AA72,
		0x06306, 0x0728F, 0x04014, 0x0519D, 0x02522, 0x034AB, 0x00630, 0x017B9,
		0x0EF4E, 0x0FEC7, 0x0CC5C, 0x0DDD5, 0x0A96A, 0x0B8E3, 0x08A78, 0x09BF1,
		0x07387, 0x0620E, 0x05095, 0x0411C, 0x035A3, 0x0242A, 0x016B1, 0x00738,
		0x0FFCF, 0x0EE46, 0x0DCDD, 0x0CD54, 0x0B9EB, 0x0A862, 0x09AF9, 0x08B70,
		0x08408, 0x09581, 0x0A71A, 0x0B693, 0x0C22C, 0x0D3A5, 0x0E13E, 0x0F0B7,
		0x00840, 0x019C9, 0x02B52, 0x03ADB, 0x04E64, 0x05FED, 0x06D76, 0x07CFF,
		0x09489, 0x08500, 0x0B79B, 0x0A612, 0x0D2AD, 0x0C324, 0x0F1BF, 0x0E036,
		0x018C1, 0x00948, 0x03BD3, 0x02A5A, 0x05EE5, 0x04F6C, 0x07DF7, 0x06C7E,
		0x0A50A, 0x0B483, 0x08618, 0x09791, 0x0E32E, 0x0F2A7, 0x0C03C, 0x0D1B5,
		0x02942, 0x038CB, 0x00A50, 0x01BD9, 0x06F66, 0x07EEF, 0x04C74, 0x05DFD,
		0x0B58B, 0x0A402, 0x09699, 0x08710, 0x0F3AF, 0x0E226, 0x0D0BD, 0x0C134,
		0x039C3, 0x0284A, 0x01AD1, 0x00B58, 0x07FE7, 0x06E6E, 0x05CF5, 0x04D7C,
		0x0C60C, 0x0D785, 0x0E51E, 0x0F497, 0x08028, 0x091A1, 0x0A33A, 0x0B2B3,
		0x04A44, 0x05BCD, 0x06956, 0x078DF, 0x00C60, 0x01DE9, 0x02F72, 0x03EFB,
		0x0D68D, 0x0C704, 0x0F59F, 0x0E416, 0x090A9, 0x08120, 0x0B3BB, 0x0A232,
		0x05AC5, 0x04B4C, 0x079D7, 0x0685E, 0x01CE1, 0x00D68, 0x03FF3, 0x02E7A,
		0x0E70E, 0x0F687, 0x0C41C, 0x0D595, 0x0A12A, 0x0B0A3, 0x08238, 0x093B1,
		0x06B46, 0x07ACF, 0x04854, 0x059DD, 0x02D62, 0x03CEB, 0x00E70, 0x01FF9,
		0x0F78F, 0x0E606, 0x0D49D, 0x0C514, 0x0B1AB, 0x0A022, 0x092B9, 0x08330,
		0x07BC7, 0x06A4E, 0x058D5, 0x0495C, 0x03DE3, 0x02C6A, 0x01EF1, 0x00F78
	};

    	/* initialize the new crc with the given seed */
	int newCrc = seed;

	/* This algorithm is based on the IrDA LAP example */
	while (len-- > 0)
		newCrc = (newCrc >> 8) ^ crcLookupTable[(newCrc ^ *buf++) & 0xFF];

	/* Make this crc match the one's complement that is sent in the packet */
	return ((~newCrc) & 0xFFFF);
}


/** \addtogroup CFA_ReceiveBuffer
 *
 * ReceiveBuffer handling functions.
 * The receive buffer is a circular buffer of bytes with the possibility to
 * 'peek' into received bytes.
 *
 *\verbatim
 * |                 v peek                           |
 * |--------------------------------------------------|
 * |          ^ tail              ^ head              |
 *\endverbatim
 * @{
 */

/* Global variable */
ReceiveBuffer receivebuffer;

/**
 * Initialize/empty receive buffer by resetting its pointers.
 * \param rb  Pointer to ReceiveBuffer structure.
 */
void EmptyReceiveBuffer(ReceiveBuffer *rb)
{
	rb->head = rb->tail = rb->peek = 0;
}


/**
 * Read given number of bytes from given file handle into receive buffer.
 * \param rb      Pointer to ReceiveBuffer structure.
 * \param fd      File handle to read from.
 * \param number  Max. number of bytes to read from file handle.
 */
void SyncReceiveBuffer(ReceiveBuffer *rb, int fd, unsigned int number)
{
	unsigned char buffer[MAX_DATA_LENGTH];
	int BytesRead;

#if defined(HAVE_SELECT) && defined(CFONTZ633_WRITE_DELAY) && (CFONTZ633_WRITE_DELAY > 0)
	fd_set rfds;
	struct timeval timeout;
	int retval;

	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);
	timeout.tv_sec = 0;
	timeout.tv_usec = CFONTZ633_WRITE_DELAY;
	retval = select(fd + 1, &rfds, NULL, NULL, &timeout);

	if (!retval)
		return;
#endif

	if (number > MAX_DATA_LENGTH)
		number = MAX_DATA_LENGTH;

	BytesRead = read(fd, buffer, number);

	if (BytesRead > 0) {
		int i;

		/* wrap write pointer to the receive buffer */
		rb->head %= RECEIVEBUFFERSIZE;

		/* store the bytes read wrapping at the buffer end */
		for (i = 0; i < BytesRead; i++) {
			rb->contents[rb->head] = buffer[i];
			rb->head = (rb->head + 1) % RECEIVEBUFFERSIZE;
		}
	}
}


/**
 * Get number of bytes available for reading in receive buffer.
 * \param rb  Pointer to ReceiveBuffer structure.
 * \return  Number of bytes available in receive buffer.
 */
int BytesAvail(ReceiveBuffer *rb)
{
	int avail_bytes = rb->head - rb->tail;

	if (avail_bytes < 0)
		avail_bytes += RECEIVEBUFFERSIZE;

	return(avail_bytes % RECEIVEBUFFERSIZE);
}


/**
 * Get next byte from receive buffer.
 * \param rb  Pointer to ReceiveBuffer structure.
 * \retval return_byte  Next byte in receive buffer.
 * \retval '\0'         Failure (receive buffer is empty).
 */
unsigned char GetByte(ReceiveBuffer *rb)
{
	unsigned char return_byte = '\0';

	/* wrap read pointer to the receive buffer */
	rb->tail %= RECEIVEBUFFERSIZE;

	/* See if there are any more bytes available. */
	if (rb->tail != (rb->head % RECEIVEBUFFERSIZE)) {
		/* There is at least one more byte. */
		return_byte = rb->contents[rb->tail];

		/* Increment read pointer (wrap if needed) */
		rb->tail = (rb->tail + 1) % RECEIVEBUFFERSIZE;
	}

	return(return_byte);
}


/**
 * Return number of bytes available for peeking in receive buffer.
 * \param rb  Pointer to ReceiveBuffer structure.
 * \return  Number of bytes available for peeking in receive buffer.
 */
int PeekBytesAvail(ReceiveBuffer *rb)
{
	int avail_bytes = rb->head - rb->peek;

	if (avail_bytes < 0)
		avail_bytes += RECEIVEBUFFERSIZE;

	return(avail_bytes % RECEIVEBUFFERSIZE);
}


/**
 * Sync peek pointer with read pointer.
 * \param rb  Pointer to ReceiveBuffer structure.
 */
void SyncPeekPointer(ReceiveBuffer *rb)
{
	rb->peek = rb->tail;
}


/**
 * Accept peeked data by syncing the read pointer to the peek pointer.
 * \param rb  Pointer to ReceiveBuffer structure.
 */
void AcceptPeekedData(ReceiveBuffer *rb)
{
	rb->tail = rb->peek;
}


/**
 * Peek next byte from receive buffer.
 * \param rb  Pointer to ReceiveBuffer structure.
 * \retval return_byte  Next byte in receive buffer.
 * \retval '\0'         Failure (receive buffer is empty).
 */
unsigned char PeekByte(ReceiveBuffer *rb)
{
	unsigned char return_byte = '\0';

	/* wrap peek pointer to the receive buffer */
	rb->peek %= RECEIVEBUFFERSIZE;

	/* See if there are any more bytes available. */
	if (rb->peek != (rb->head % RECEIVEBUFFERSIZE)) {
		/* There is at least one more byte. */
		return_byte = rb->contents[rb->peek];

		/* Increment the peek pointer (wrap if needed). */
		rb->peek = (rb->peek + 1) % RECEIVEBUFFERSIZE;
	}

	return(return_byte);
}
/** @} */


/**
 * Check for a response packet and try to identify it.
 * \param fd        File handle to read from.
 * \param response  Expected response command.
 * \param in        Pointer to COMMAND_PACKET structure to write the response to.
 * \retval 1  Expected response received.
 * \retval 0  Expected response not received.
 *
 * \todo check_for_packet is always called with MAX_DATA_LENGTH. This doesn't
 *       do any harm, but passing that parameter is useless then. Additionally
 *       one complete packet is MAX_DATA_LENGTH + 4 (command, length, CRC).
 */
static int
test_packet(int fd, unsigned char response, COMMAND_PACKET *in)
{
#if defined(HAVE_SELECT) && defined(CFONTZ633_WRITE_DELAY) && (CFONTZ633_WRITE_DELAY > 0)
	int response_received = 0;
	int loop;

	/* wait for answer packet but forever (LCDs should answer within max. 250ms) */
	for (loop = 250000/CFONTZ633_WRITE_DELAY; !response_received && loop > 0; loop--) {
		int is_msg = check_for_packet(fd, in, MAX_DATA_LENGTH);

		while (is_msg != GIVE_UP) {
			if (is_msg == GOOD_MSG) {
				/* key activity ? */
				if (in->command == 0x80)
					AddKeyToKeyRing(&keyring, in->data[0]);
				else if (in->command == response)
					response_received = 1;
			}

			is_msg = check_for_packet(fd, in, MAX_DATA_LENGTH);
		}
	}
	return(response_received);
#else
	int is_msg = check_for_packet(fd, in, MAX_DATA_LENGTH);
	while (is_msg != GIVE_UP) {
		if (is_msg == GOOD_MSG) {
			if (in->command == 0x80)
				AddKeyToKeyRing(&keyring, in->data[0]);
		}

		is_msg = check_for_packet(fd, in, MAX_DATA_LENGTH);
	}

	return 1;
#endif
}


/**
 * Check for a packet to read. If there is a valid packet in the input buffer
 * it will copy it into \c in and return GOOD_MSG. If there is no enough data
 * available for a valid packet it returns GIVE_UP.
 *
 * \param fd        File handle to read from.
 * \param in        Pointer to COMMAND_PACKET structure to write the response to.
 * \param expected_length  Expected response length.
 *
 * \retval GIVE_UP    No message and we should not retry until new input.
 * \retval TRY_AGAIN  No message but we should try again immediately.
 * \retval GOOD_MSG   Message correctly identified.
 */
static int
check_for_packet(int fd, COMMAND_PACKET *in, unsigned char expected_length)
{
	int i;
	int testcrc;

	SyncReceiveBuffer(&receivebuffer, fd, expected_length);

	/*
	 * There must be at least 4 bytes available in the input stream for
	 * there to be a valid command in it (command, length, no data, CRC).
	 */
	if (BytesAvail(&receivebuffer) < 4) {
		return(GIVE_UP);
	}

	/* Look into the buffer without removing the data. */
	SyncPeekPointer(&receivebuffer);

	/*
	 * Look at potential command byte. If it is not a valid command
	 * discard it and try again.
	 */
	in->command = PeekByte(&receivebuffer);
	if ((in->command & 0x3F) > MAX_COMMAND) {
		GetByte(&receivebuffer);
		return(TRY_AGAIN);
	}

	/*
	 * Get the data_length and check it is within valid range. If not
	 * discard it and try again.
	 */
	in->data_length = PeekByte(&receivebuffer);
	if (in->data_length > MAX_DATA_LENGTH) {
		GetByte(&receivebuffer);
		return(TRY_AGAIN);
	}

	/*
	 * Now there must be at least in->data_length + sizeof(CRC) bytes
	 * still available for us to continue. If not give up and try again
	 * if more bytes are available.
	 */
	if ((int) PeekBytesAvail(&receivebuffer) < (in->data_length + 2)) {
		return(GIVE_UP);
	}

	/* There is enough data to make a packet. Transfer over the data. */
	for (i = 0; i < in->data_length; i++)
		in->data[i] = PeekByte(&receivebuffer);

	/* Convert CRC bytes to CRC manually to avoid endianess issues */
	in->crc = PeekByte(&receivebuffer);
	in->crc |= PeekByte(&receivebuffer) << 8;

	/* Compute the expected checksum and compare it to the received one. */
	testcrc = get_crc((unsigned char *) in, in->data_length+2, 0xFFFF);
	if (in->crc == testcrc) {
		/* This is a good packet. Remove it from the serial buffer. */
		AcceptPeekedData(&receivebuffer);

		/**** TEST STUFF ****/
		//print_packet(in);

		return(GOOD_MSG);
	}

	/*
	 * The CRC did not match. Toss out one byte of garbage.  Next pass
	 * through should re-sync.
	 */
	GetByte(&receivebuffer);
	return(TRY_AGAIN);
}


#ifdef DEBUG
/*
 * This prints a hex dump of a packet to stderr.
 */
static void
print_packet(COMMAND_PACKET *packet)
{
	int i, cmd, top, len;

	top = (0xC0 & (packet->command)) >> 6;
	cmd = (0x3F & (packet->command));
	len = packet->data_length;

	fprintf(stderr, "Message (%d,%d) %d [", top, cmd, len);
	for (i = 0; i < packet->data_length; i++)
		fprintf(stderr, " %02x", packet->data[i]);
	fprintf(stderr, " ] %02x %02x .\n", packet->crc & 0xFF, (packet->crc >> 8) & 0xFF);
}
#endif