[Lcdproc] Crystalfontz 633/635 Packet Driver Endianness Issue
Russell Krayer
rkrayer@msn.com
Sat Sep 9 15:23:01 2006
diff included here ...
Index: server/drivers/CFontz633io.c
===================================================================
--- server/drivers/CFontz633io.c (revision 16)
+++ server/drivers/CFontz633io.c (working copy)
@@ -63,7 +63,21 @@
#define GOOD_MSG 1
#define GIVE_UP 2
+#define LITTLE_ENDIAN 0
+#define BIG_ENDIAN 1
+int machineEndianness()
+{
+ long int i = 1;
+ const char *p = (const char *) &i;
+ if (p[0] == 1) // Lowest address contains the least significant byte
+ return LITTLE_ENDIAN;
+ else
+ return BIG_ENDIAN;
+}
+
+
+
/* 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);
@@ -180,6 +194,12 @@
/* calculate & send the CRC */
out->crc.as_word = get_crc((unsigned char *) out, out->data_length +
2, 0xFFFF);
+ if (machineEndianness()==BIG_ENDIAN)
+ {
+ unsigned char tmp = out->crc.as_bytes[0];
+ out->crc.as_bytes[0] = out->crc.as_bytes[1];
+ out->crc.as_bytes[1] = tmp;
+ }
write(fd, out->crc.as_bytes, 2);
/**** TEST STUFF ****/
@@ -513,8 +533,16 @@
in->data[i] = PeekByte(&receivebuffer);
//Now move over the CRC.
- in->crc.as_bytes[0] = PeekByte(&receivebuffer);
- in->crc.as_bytes[1] = PeekByte(&receivebuffer);
+ if (machineEndianness()==LITTLE_ENDIAN)
+ {
+ in->crc.as_bytes[0] = PeekByte(&receivebuffer);
+ in->crc.as_bytes[1] = PeekByte(&receivebuffer);
+ }
+ else
+ {
+ in->crc.as_bytes[1] = PeekByte(&receivebuffer);
+ in->crc.as_bytes[0] = PeekByte(&receivebuffer);
+ }
//Now check the CRC.
//Compute the expected CheckSum
>From: "Russell Krayer" <rkrayer@msn.com>
>To: lcdproc@lists.omnipotent.net
>Subject: [Lcdproc] Crystalfontz 633/635 Packet Driver Endianness Issue
>Date: Sat, 09 Sep 2006 11:10:12 -0400
>
>Hi,
>
>I stumbled onto an issue with endianness running LCDproc 0.5.0 on a ppc box
>(a big endian machine). The problem is in CFontz633io.c when working with
>the CRC. I have a fix that is working for me and should still work with
>little endian machines. Should I send a patch somewhere? Note, the bug is
>also in the Crystalfontz example program from Crystalfontz vendor.
>
>Thanks
>Russ
>
>
>_______________________________________________
>LCDproc mailing list
>LCDproc@lists.omnipotent.net
>http://lists.omnipotent.net/mailman/listinfo/lcdproc