reduce number of global/static variables;

make KeyRing and ReceiveBuffer functions take the KeyRing/ReceiveBuffer as an argument;
adapt CFontz633.c CFontzPacket.c accordingly;
remove superfluous functions; more effective writing; correct max. packet size
This commit is contained in:
marschap
2005-05-30 16:56:29 +00:00
parent a1adadfe8d
commit b4d557123c
4 changed files with 180 additions and 171 deletions
+18 -19
View File
@@ -93,7 +93,7 @@ typedef struct driver_private_data {
int fd; int fd;
//int model; int model;
int newfirmware; int newfirmware;
/* dimensions */ /* dimensions */
@@ -160,8 +160,8 @@ CFontz633_init (Driver *drvthis, char *args)
debug(RPT_INFO, "CFontz633: init(%p,%s)", drvthis, args ); debug(RPT_INFO, "CFontz633: init(%p,%s)", drvthis, args );
EmptyKeyRing(); EmptyKeyRing(&keyring);
EmptyReceiveBuffer(); EmptyReceiveBuffer(&receivebuffer);
/* Read config file */ /* Read config file */
/* Which device should be used */ /* Which device should be used */
@@ -362,15 +362,14 @@ CFontz633_flush (Driver *drvthis)
int i; int i;
#if defined(CF635_FLUSH) #if defined(CF635_FLUSH)
int len = width * height; int len = p->width * p->height;
char out[4]; unsigned char out[3];
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (p->framebuf[i] != p->backingstore[i]) { if (p->framebuf[i] != p->backingstore[i]) {
out[1] = i / width; // line out[0] = (unsigned char) (i % p->width); // column
out[0] = i - (out[1] * width); // column out[1] = (unsigned char) (i / p->width); // line
out[2] = p->framebuf[i]; // character out[2] = p->framebuf[i]; // character
out[2] = '\0';
send_bytes_message(fd, 3, CF633_Send_Data_to_LCD, out); send_bytes_message(fd, 3, CF633_Send_Data_to_LCD, out);
p->backingstore[i] = p->framebuf[i]; p->backingstore[i] = p->framebuf[i];
} }
@@ -385,19 +384,19 @@ CFontz633_flush (Driver *drvthis)
for (i = 0; i < p->width; i++) { for (i = 0; i < p->width; i++) {
if (*xp++ != *xq++) { if (*xp++ != *xq++) {
send_bytes_message(p->fd, 16, CF633_Set_LCD_Contents_Line_One, &(p->framebuf[0])); send_bytes_message(p->fd, 16, CF633_Set_LCD_Contents_Line_One, p->framebuf);
memcpy(p->backingstore, p->framebuf, p->width); memcpy(p->backingstore, p->framebuf, p->width);
break; break;
} }
} }
xp = &(p->framebuf[p->width]); xp = p->framebuf + p->width;
xq = &(p->backingstore[p->width]); xq = p->backingstore + p->width;
for (i = 0; i < p->width; i++) { for (i = 0; i < p->width; i++) {
if (*xp++ != *xq++) { if (*xp++ != *xq++) {
send_bytes_message(p->fd, 16, CF633_Set_LCD_Contents_Line_Two, &(p->framebuf[p->width])); send_bytes_message(p->fd, 16, CF633_Set_LCD_Contents_Line_Two, p->framebuf + p->width);
memcpy(&(p->backingstore[p->width]), &(p->framebuf[p->width]), p->width); memcpy(p->backingstore + p->width, p->framebuf + p->width, p->width);
break; break;
} }
} }
@@ -414,7 +413,7 @@ CFontz633_get_key (Driver *drvthis)
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
unsigned char key; unsigned char key;
key = GetKeyFromKeyRing(); key = GetKeyFromKeyRing(&keyring);
switch (key) { switch (key) {
case CF633_KEY_LEFT: case CF633_KEY_LEFT:
@@ -534,7 +533,7 @@ static void
CFontz633_no_live_report (Driver *drvthis) CFontz633_no_live_report (Driver *drvthis)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[2] = { 0, 0 }; unsigned char out[2] = { 0, 0 };
for (out[0] = 0; out[0] < 8; out[0]++) { for (out[0] = 0; out[0] < 8; out[0]++) {
send_bytes_message(p->fd, 2, CF633_Set_Up_Live_Fan_or_Temperature_Display, out); send_bytes_message(p->fd, 2, CF633_Set_Up_Live_Fan_or_Temperature_Display, out);
@@ -561,7 +560,7 @@ static void
CFontz633_no_temp_report (Driver *drvthis) CFontz633_no_temp_report (Driver *drvthis)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[4] = { 0, 0, 0, 0 }; unsigned char out[4] = { 0, 0, 0, 0 };
send_bytes_message(p->fd, 4, CF633_Set_Up_Temperature_Reporting, out); send_bytes_message(p->fd, 4, CF633_Set_Up_Temperature_Reporting, out);
} }
@@ -574,7 +573,7 @@ static void
CFontz633_reboot (Driver *drvthis) CFontz633_reboot (Driver *drvthis)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[3] = { 8, 18, 99 }; unsigned char out[3] = { 8, 18, 99 };
send_bytes_message(p->fd, 3, CF633_Reboot, out); send_bytes_message(p->fd, 3, CF633_Reboot, out);
sleep(2); sleep(2);
@@ -811,7 +810,7 @@ CFontz633_num (Driver *drvthis, int x, int num)
{ {
/* /*
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[5]; unsigned char out[5];
snprintf (out, sizeof(out), "%c%c%c", 28, x, num); snprintf (out, sizeof(out), "%c%c%c", 28, x, num);
write (p->fd, out, 3); write (p->fd, out, 3);
@@ -830,7 +829,7 @@ MODULE_EXPORT void
CFontz633_set_char (Driver *drvthis, int n, char *dat) CFontz633_set_char (Driver *drvthis, int n, char *dat)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[9]; unsigned char out[9];
int row, col; int row, col;
if ((n < 0) || (n >= NUM_CCs)) if ((n < 0) || (n >= NUM_CCs))
+97 -116
View File
@@ -17,10 +17,7 @@
#include "CFontz633io.h" #include "CFontz633io.h"
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
/*#include <errno.h>*/
/*extern int errono;*/
/*#include <string.h>*/
#define TRY_AGAIN 0 #define TRY_AGAIN 0
@@ -29,134 +26,122 @@
/* static local fuinctions */ /* static local fuinctions */
static void send_packet(int fd); static void send_packet(int fd, COMMAND_PACKET *out);
static void SendByte(int fd, unsigned char datum);
static int get_crc(char * bufptr, int len, int seed); static int get_crc(char * bufptr, int len, int seed);
static int check_for_packet(int fd, unsigned char expected_length); static int check_for_packet(int fd, unsigned char expected_length);
static void treat_packet(void); static void treat_packet(void);
static void print_packet(COMMAND_PACKET *packet); static void print_packet(COMMAND_PACKET *packet);
static COMMAND_PACKET outgoing_response; /* local variables */
static COMMAND_PACKET incoming_command; static COMMAND_PACKET incoming_command;
/* variables for circular receive buffer */ /* global variables */
#define RECEIVEBUFFERSIZE 512 KeyRing keyring;
static unsigned char SerialReceiveBuffer[RECEIVEBUFFERSIZE]; ReceiveBuffer receivebuffer;
static int ReceiveBufferHead = 0;
static int ReceiveBufferTail = 0;
static int ReceiveBufferTailPeek = 0;
/* /*
* KeyRing handling function. * KeyRing handling functions.
* This separate the producer from the consumer. * This separates the producer from the consumer.
* It is just a small fifo of unsigned char. * It is just a small fifo of unsigned char.
*/ */
#define KEYRINGSIZE 16
static unsigned char KeyRing[KEYRINGSIZE];
static int KeyHead = 0;
static int KeyTail = 0;
/** initialize/empty key ring by resetting its read & write pointers */ /** initialize/empty key ring by resetting its read & write pointers */
void EmptyKeyRing(void) void EmptyKeyRing(KeyRing *kr)
{ {
KeyHead = KeyTail = 0; kr->head = kr->tail = 0;
} }
/** add byte to key ring; return success (byte added) / failure (key ring is full) */ /** add byte to key ring; return success (byte added) / failure (key ring is full) */
int AddKeyToKeyRing(unsigned char key) int AddKeyToKeyRing(KeyRing *kr, unsigned char key)
{ {
if (((KeyHead + 1) % KEYRINGSIZE) != (KeyTail % KEYRINGSIZE)) { if (((kr->head + 1) % KEYRINGSIZE) != (kr->tail % KEYRINGSIZE)) {
/* printf("We add key: %d\n", key); */ /* printf("We add key: %d\n", key); */
KeyRing[KeyHead % KEYRINGSIZE] = key; kr->contents[kr->head % KEYRINGSIZE] = key;
KeyHead = (KeyHead + 1) % KEYRINGSIZE; kr->head = (kr->head + 1) % KEYRINGSIZE;
return 1; return 1;
} }
/* KeyRing overflow: do not accept extra key */ /* KeyRing overflow: do not accept extra key */
return 0; return 0;
} }
/** get byte from key ring (or '\0' if key ring is empty) */ /** get byte from key ring (or '\0' if key ring is empty) */
unsigned char GetKeyFromKeyRing(void) unsigned char GetKeyFromKeyRing(KeyRing *kr)
{ {
unsigned char retval = '\0'; unsigned char retval = '\0';
KeyTail %= KEYRINGSIZE; kr->tail %= KEYRINGSIZE;
if ((KeyHead % KEYRINGSIZE) != KeyTail) { if ((kr->head % KEYRINGSIZE) != kr->tail) {
retval = KeyRing[KeyTail]; retval = kr->contents[kr->tail];
KeyTail = (KeyTail + 1) % KEYRINGSIZE; kr->tail = (kr->tail + 1) % KEYRINGSIZE;
} }
/* if (retval) printf("We remove key: %d\n", retval); */ /* if (retval) printf("We remove key: %d\n", retval); */
return retval; return retval;
} }
/** send message with arguments to the given handle */ /** send message with arguments to the given handle */
void send_bytes_message(int fd, int len, int msg, char *framebuf) void send_bytes_message(int fd, int len, int msg, unsigned char *data)
{ {
int i; COMMAND_PACKET out;
outgoing_response.command = msg; out.command = msg;
outgoing_response.data_length = len; out.data_length = len;
for (i = 0; i < outgoing_response.data_length; i++) memcpy(out.data, data, len);
outgoing_response.data[i] = framebuf[i];
/* send message & calc CRC */ /* send message & calc CRC */
send_packet(fd); send_packet(fd, &out);
} }
/** send message with one byte argument to the given handle */ /** send message with one byte argument to the given handle */
void send_onebyte_message(int fd, int msg, int value) void send_onebyte_message(int fd, int msg, unsigned char value)
{ {
outgoing_response.command = msg; COMMAND_PACKET out;
outgoing_response.data_length = 1;
outgoing_response.data[0] = value; out.command = msg;
out.data_length = 1;
out.data[0] = value;
/* send message & calc CRC */ /* send message & calc CRC */
send_packet(fd); send_packet(fd, &out);
} }
/** send message without data to the given handle */ /** send message without data to the given handle */
void send_zerobyte_message(int fd, int msg) void send_zerobyte_message(int fd, int msg)
{ {
outgoing_response.command = msg; COMMAND_PACKET out;
outgoing_response.data_length = 0;
out.command = msg;
out.data_length = 0;
/* send message & calc CRC */ /* send message & calc CRC */
send_packet(fd); send_packet(fd, &out);
} }
/** send outgoing_response to the given handle; calc & send CRC when doing so */ /** send outgoing_response to the given handle; calc & send CRC when doing so */
static void static void
send_packet(int fd) send_packet(int fd, COMMAND_PACKET *out)
{ {
unsigned char i; write(fd, &out->command, 1);
write(fd, &out->data_length, 1);
SendByte(fd, outgoing_response.command); write(fd, out->data, out->data_length);
SendByte(fd, outgoing_response.data_length);
for (i = 0; i < outgoing_response.data_length; i++) {
SendByte(fd, outgoing_response.data[i]);
}
/* calculate & send the CRC */ /* calculate & send the CRC */
outgoing_response.CRC.as_word = get_crc((unsigned char *) &outgoing_response, out->crc.as_word = get_crc((unsigned char *) out, out->data_length + 2, 0xFFFF);
outgoing_response.data_length + 2, 0xFFFF); write(fd, out->crc.as_bytes, 2);
SendByte(fd, outgoing_response.CRC.as_bytes[0]);
SendByte(fd, outgoing_response.CRC.as_bytes[1]);
/**** TEST STUF ****/ /**** TEST STUF ****/
// print_packet(&outgoing_response); // print_packet(&outgoing_response);
@@ -219,14 +204,6 @@ get_crc(char *buf, int len, int seed)
} }
/** send one byte to the given handle */
static void
SendByte(int fd, unsigned char datum)
{
write(fd, &datum, 1);
}
/*============================================================================ /*============================================================================
@@ -239,7 +216,7 @@ SendByte(int fd, unsigned char datum)
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* This is some code that kind of makes the windows stuff look a little /* This is some code that kind of makes the windows stuff look a little
* like the DOS/633 interrupt driven serial stuff. Basically there is a * like the DOS/633 interrupt driven serial stuff. Basically there is a
* circular buffer, and Sync_Read_Buffer() uses ReadFile() to put data * circular buffer, and SyncReceiveBuffer() uses ReadFile() to put data
* into the circular buffer much like the DOS stuff uses an ISR to put * into the circular buffer much like the DOS stuff uses an ISR to put
* the data into the buffer. Then the rest of the functions work like * the data into the buffer. Then the rest of the functions work like
* the counterparts in the 633, * the counterparts in the 633,
@@ -253,20 +230,22 @@ SendByte(int fd, unsigned char datum)
/** initialize/empty receive buffer by resetting its pointers */ /** initialize/empty receive buffer by resetting its pointers */
void EmptyReceiveBuffer(void) void EmptyReceiveBuffer(ReceiveBuffer *rb)
{ {
ReceiveBufferHead = ReceiveBufferTail = ReceiveBufferTailPeek = 0; rb->head = rb->tail = rb->peek = 0;
} }
/** read given number of bytes from given file handle into receive buffer */ /** read given number of bytes from given file handle into receive buffer */
void Sync_Read_Buffer(int fd, unsigned char expected_bytes) void SyncReceiveBuffer(int fd, ReceiveBuffer *rb, unsigned int number)
{ {
unsigned char Incoming[512]; unsigned char buffer[MAX_DATA_LENGTH];
int BytesRead; int BytesRead;
// ToDo: check that expected_bytes < sizeof(Incoming) if (number > MAX_DATA_LENGTH)
BytesRead = read(fd, Incoming, expected_bytes); number = MAX_DATA_LENGTH;
BytesRead = read(fd, buffer, number);
if (BytesRead == -1) { if (BytesRead == -1) {
/* printf("~~~Problem reading: %s .\n", strerror(errno)); */ /* printf("~~~Problem reading: %s .\n", strerror(errno)); */
} }
@@ -276,15 +255,15 @@ void Sync_Read_Buffer(int fd, unsigned char expected_bytes)
/* printf("Read %d Bytes:", BytesRead); */ /* printf("Read %d Bytes:", BytesRead); */
/* wrap write pointer to the receive buffer */ /* wrap write pointer to the receive buffer */
ReceiveBufferHead %= RECEIVEBUFFERSIZE; rb->head %= RECEIVEBUFFERSIZE;
/* Read the incoming byte and store it, */ /* store the bytes read */
for (i = 0; i < BytesRead; i++) { for (i = 0; i < BytesRead; i++) {
/* printf(" %02x", Incoming[i]); */ /* printf(" %02x", buffer[i]); */
SerialReceiveBuffer[ReceiveBufferHead] = Incoming[i]; rb->contents[rb->head] = buffer[i];
/* increment write pointer (wrap if needed) */ /* increment write pointer (wrap if needed) */
ReceiveBufferHead = (ReceiveBufferHead + 1) % RECEIVEBUFFERSIZE; rb->head = (rb->head + 1) % RECEIVEBUFFERSIZE;
} }
/* printf("\n"); */ /* printf("\n"); */
} }
@@ -292,9 +271,9 @@ void Sync_Read_Buffer(int fd, unsigned char expected_bytes)
/** return number of bytes available for reading in receive buffer */ /** return number of bytes available for reading in receive buffer */
int BytesAvail(void) int BytesAvail(ReceiveBuffer *rb)
{ {
int avail_bytes = ReceiveBufferHead - ReceiveBufferTail; int avail_bytes = rb->head - rb->tail;
if (avail_bytes < 0) if (avail_bytes < 0)
avail_bytes += RECEIVEBUFFERSIZE; avail_bytes += RECEIVEBUFFERSIZE;
@@ -304,20 +283,20 @@ int BytesAvail(void)
/** get next byte from receive buffer (return '\0' if buffer is empty) */ /** get next byte from receive buffer (return '\0' if buffer is empty) */
unsigned char GetByte(void) unsigned char GetByte(ReceiveBuffer *rb)
{ {
unsigned char return_byte = '\0'; unsigned char return_byte = '\0';
/* wrap read pointer to the receive buffer */ /* wrap read pointer to the receive buffer */
ReceiveBufferTail %= RECEIVEBUFFERSIZE; rb->tail %= RECEIVEBUFFERSIZE;
/* See if there are any more bytes available. */ /* See if there are any more bytes available. */
if (ReceiveBufferTail != (ReceiveBufferHead % RECEIVEBUFFERSIZE)) { if (rb->tail != (rb->head % RECEIVEBUFFERSIZE)) {
/* There is at least one more byte. */ /* There is at least one more byte. */
return_byte = SerialReceiveBuffer[ReceiveBufferTail]; return_byte = rb->contents[rb->tail];
/* Increment read pointer (wrap if needed) */ /* Increment read pointer (wrap if needed) */
ReceiveBufferTail = (ReceiveBufferTail + 1) % RECEIVEBUFFERSIZE; rb->tail = (rb->tail + 1) % RECEIVEBUFFERSIZE;
} }
return(return_byte); return(return_byte);
@@ -325,9 +304,9 @@ unsigned char GetByte(void)
/** return number of bytes available for peeking in receive buffer */ /** return number of bytes available for peeking in receive buffer */
int PeekBytesAvail(void) int PeekBytesAvail(ReceiveBuffer *rb)
{ {
int avail_bytes = ReceiveBufferHead - ReceiveBufferTailPeek; int avail_bytes = rb->head - rb->peek;
if (avail_bytes < 0) if (avail_bytes < 0)
avail_bytes += RECEIVEBUFFERSIZE; avail_bytes += RECEIVEBUFFERSIZE;
@@ -337,40 +316,41 @@ int PeekBytesAvail(void)
/** sync peek pointer with read pointer */ /** sync peek pointer with read pointer */
void Sync_Peek_Pointer(void) void SyncPeekPointer(ReceiveBuffer *rb)
{ {
ReceiveBufferTailPeek = ReceiveBufferTail; rb->peek = rb->tail;
} }
/** accept ppeked data by syncing the read pointer to the peek pointer */ /** accept ppeked data by syncing the read pointer to the peek pointer */
void AcceptPeekedData(void) void AcceptPeekedData(ReceiveBuffer *rb)
{ {
ReceiveBufferTail = ReceiveBufferTailPeek; rb->tail = rb->peek;
} }
/** peek next byte from receive buffer (return '\0' if buffer is empty) */ /** peek next byte from receive buffer (return '\0' if buffer is empty) */
unsigned char PeekByte(void) unsigned char PeekByte(ReceiveBuffer *rb)
{ {
unsigned char return_byte = '\0'; unsigned char return_byte = '\0';
/* wrap peek pointer to the receive buffer */ /* wrap peek pointer to the receive buffer */
ReceiveBufferTailPeek %= RECEIVEBUFFERSIZE; rb->peek %= RECEIVEBUFFERSIZE;
/* See if there are any more bytes available. */ /* See if there are any more bytes available. */
if (ReceiveBufferTailPeek != (ReceiveBufferHead % RECEIVEBUFFERSIZE)) { if (rb->peek != (rb->head % RECEIVEBUFFERSIZE)) {
/* There is at least one more byte. */ /* There is at least one more byte. */
return_byte = SerialReceiveBuffer[ReceiveBufferTailPeek]; return_byte = rb->contents[rb->peek];
/* Increment the peek pointer (wrap if needed). */ /* Increment the peek pointer (wrap if needed). */
ReceiveBufferTailPeek = (ReceiveBufferTailPeek + 1) % RECEIVEBUFFERSIZE; rb->peek = (rb->peek + 1) % RECEIVEBUFFERSIZE;
} }
return(return_byte); return(return_byte);
} }
/* I should use the value GIVE_UP and not reenter if there is no extra /* I should use the value GIVE_UP and not reenter if there is no extra
* byte read from the serial port * byte read from the serial port
*/ */
@@ -394,7 +374,7 @@ static void
treat_packet(void) treat_packet(void)
{ {
if (incoming_command.command == 0x80) { if (incoming_command.command == 0x80) {
AddKeyToKeyRing(incoming_command.data[0]); AddKeyToKeyRing(&keyring, incoming_command.data[0]);
} }
} }
@@ -423,43 +403,43 @@ check_for_packet(int fd, unsigned char expected_length)
int i; int i;
int testcrc; int testcrc;
Sync_Read_Buffer(fd, expected_length); SyncReceiveBuffer(fd, &receivebuffer, expected_length);
//First off, there must be at least 4 bytes available in the input stream //First off, 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). //for there to be a valid command in it (command, length, no data, CRC).
if (BytesAvail() < 4) { if (BytesAvail(&receivebuffer) < 4) {
/* printf("Not enough byte available for even the smallest message.\n"); */ /* printf("Not enough byte available for even the smallest message.\n"); */
return(GIVE_UP); /* We don't need to retry before more byte are received */ return(GIVE_UP); /* We don't need to retry before more byte are received */
} }
/* Look into the buffer without removing the data. */ /* Look into the buffer without removing the data. */
Sync_Peek_Pointer(); SyncPeekPointer(&receivebuffer);
/* look at potential command byte */ /* look at potential command byte */
incoming_command.command = PeekByte(); incoming_command.command = PeekByte(&receivebuffer);
/* Only commands 0 through MAX_COMMAND are valid */ /* Only commands 0 through MAX_COMMAND are valid */
if (MAX_COMMAND < (0x3F & incoming_command.command)) { if (MAX_COMMAND < (0x3F & incoming_command.command)) {
/* Throw out one byte of garbage. Next pass through should re-sync. */ /* Throw out one byte of garbage. Next pass through should re-sync. */
GetByte(); GetByte(&receivebuffer);
/* printf("###: Unknown command.\n"); */ /* printf("###: Unknown command.\n"); */
return(TRY_AGAIN); return(TRY_AGAIN);
} }
/* There is a valid command byte. Get the data_length. */ /* There is a valid command byte. Get the data_length. */
incoming_command.data_length = PeekByte(); incoming_command.data_length = PeekByte(&receivebuffer);
/* The data length must be within reason. */ /* The data length must be within reason. */
if (MAX_DATA_LENGTH < incoming_command.data_length) { if (MAX_DATA_LENGTH < incoming_command.data_length) {
//Throw out one byte of garbage. Next pass through should re-sync. //Throw out one byte of garbage. Next pass through should re-sync.
GetByte(); GetByte(&receivebuffer);
/* printf("###: Too long packet: %d.\n", incoming_command.data_length); */ /* printf("###: Too long packet: %d.\n", incoming_command.data_length); */
return(TRY_AGAIN); return(TRY_AGAIN);
} }
// Now there must be at least incoming_command.data_length + sizeof(CRC) bytes // Now there must be at least incoming_command.data_length + sizeof(CRC) bytes
// still available for us to continue. // still available for us to continue.
if ((int) PeekBytesAvail() < (incoming_command.data_length + 2)) { if ((int) PeekBytesAvail(&receivebuffer) < (incoming_command.data_length + 2)) {
//It looked like a valid start of a packet, but it does not look //It looked like a valid start of a packet, but it does not look
//like the complete packet has been received yet. //like the complete packet has been received yet.
/* printf("Not enough read to check the complete message.\n"); */ /* printf("Not enough read to check the complete message.\n"); */
@@ -468,11 +448,11 @@ check_for_packet(int fd, unsigned char expected_length)
/* There is enough data to make a packet. Transfer over the data. */ /* There is enough data to make a packet. Transfer over the data. */
for (i = 0; i < incoming_command.data_length; i++) for (i = 0; i < incoming_command.data_length; i++)
incoming_command.data[i] = PeekByte(); incoming_command.data[i] = PeekByte(&receivebuffer);
//Now move over the CRC. //Now move over the CRC.
incoming_command.CRC.as_bytes[0] = PeekByte(); incoming_command.crc.as_bytes[0] = PeekByte(&receivebuffer);
incoming_command.CRC.as_bytes[1] = PeekByte(); incoming_command.crc.as_bytes[1] = PeekByte(&receivebuffer);
//Now check the CRC. //Now check the CRC.
//Compute the expected CheckSum //Compute the expected CheckSum
@@ -480,10 +460,10 @@ check_for_packet(int fd, unsigned char expected_length)
incoming_command.data_length+2, 0xFFFF); incoming_command.data_length+2, 0xFFFF);
testcrc = testcrc & 0xFFFF; /* This is TRICKY */ testcrc = testcrc & 0xFFFF; /* This is TRICKY */
if (incoming_command.CRC.as_word == testcrc) { if (incoming_command.crc.as_word == testcrc) {
//This is a good packet. I'll be horn swaggled. Remove the packet //This is a good packet. I'll be horn swaggled. Remove the packet
//from the serial buffer. //from the serial buffer.
AcceptPeekedData(); AcceptPeekedData(&receivebuffer);
//Let our caller know that incoming_command has good stuff in it. //Let our caller know that incoming_command has good stuff in it.
/* print_packet(&outgoing_response); */ /* print_packet(&outgoing_response); */
@@ -493,9 +473,9 @@ check_for_packet(int fd, unsigned char expected_length)
/* The CRC did not match. Toss out one byte of garbage. /* The CRC did not match. Toss out one byte of garbage.
* Next pass through should re-sync. * Next pass through should re-sync.
*/ */
GetByte(); GetByte(&receivebuffer);
/* printf("###: Wrong CheckSum. computed/real %04x:%04x \n", /* printf("###: Wrong CheckSum. computed/real %04x:%04x \n",
testcrc, incoming_command.CRC.as_word); */ testcrc, incoming_command.crc.as_word); */
return(TRY_AGAIN); return(TRY_AGAIN);
} }
@@ -505,7 +485,8 @@ check_for_packet(int fd, unsigned char expected_length)
* It should be removed or compiled in conditionally. * It should be removed or compiled in conditionally.
* Currently it is still using printf for debugging. * Currently it is still using printf for debugging.
*/ */
void print_packet(COMMAND_PACKET *packet) static void
print_packet(COMMAND_PACKET *packet)
{ {
int i, cmd, top, len; int i, cmd, top, len;
@@ -519,6 +500,6 @@ void print_packet(COMMAND_PACKET *packet)
for (i = 0; i < packet->data_length; i++) for (i = 0; i < packet->data_length; i++)
printf(" %02x", packet->data[i]); printf(" %02x", packet->data[i]);
printf(" ] %02x %02x .\n", packet->CRC.as_bytes[0], packet->CRC.as_bytes[1]); printf(" ] %02x %02x .\n", packet->crc.as_bytes[0], packet->crc.as_bytes[1]);
} }
+50 -22
View File
@@ -61,40 +61,68 @@ typedef unsigned char ubyte;
typedef signed char sbyte; typedef signed char sbyte;
typedef unsigned short word; typedef unsigned short word;
typedef unsigned long dword; typedef unsigned long dword;
typedef union {
unsigned char as_bytes[2];
word as_word;
} WORD_UNION;
/* KeyRing management */ /* KeyRing management */
void EmptyKeyRing(void); #define KEYRINGSIZE 16
int AddKeyToKeyRing(unsigned char key);
unsigned char GetKeyFromKeyRing(void);
void send_bytes_message(int fd, int len, int msg, char *framebuf); typedef struct {
void send_onebyte_message(int fd, int msg, int value); unsigned char contents[KEYRINGSIZE];
void send_zerobyte_message(int fd, int msg); int head;
int tail;
} KeyRing;
void EmptyReceiveBuffer(void);
void Sync_Read_Buffer(int fd, unsigned char expected_bytes);
int BytesAvail(void);
unsigned char GetByte(void);
int PeekBytesAvail(void);
void Sync_Peek_Pointer(void);
void AcceptPeekedData(void);
unsigned char PeekByte(void);
int test_packet(int fd); /* receive buffer management */
#define RECEIVEBUFFERSIZE 512
#define MAX_DATA_LENGTH 16 typedef struct _reveivebuffer {
unsigned char contents[RECEIVEBUFFERSIZE];
int head;
int tail;
int peek;
} ReceiveBuffer;
/* command management */
#define MAX_DATA_LENGTH 22 /* CF635 spec says 0..22 */
#define MAX_COMMAND 32 #define MAX_COMMAND 32
typedef struct { typedef struct {
ubyte command; ubyte command;
ubyte data_length; ubyte data_length;
ubyte data[MAX_DATA_LENGTH]; ubyte data[MAX_DATA_LENGTH+1];
WORD_UNION CRC; union {
unsigned char as_bytes[2];
word as_word;
} crc;
} COMMAND_PACKET; } COMMAND_PACKET;
/* KeyRing management */
void EmptyKeyRing(KeyRing *kr);
int AddKeyToKeyRing(KeyRing *kr, unsigned char key);
unsigned char GetKeyFromKeyRing(KeyRing *kr);
void send_bytes_message(int fd, int len, int msg, unsigned char *data);
void send_onebyte_message(int fd, int msg, unsigned char value);
void send_zerobyte_message(int fd, int msg);
void EmptyReceiveBuffer(ReceiveBuffer *rb);
void SyncReceiveBuffer(int fd, ReceiveBuffer *rb, unsigned int number);
int BytesAvail(ReceiveBuffer *rb);
unsigned char GetByte(ReceiveBuffer *rb);
int PeekBytesAvail(ReceiveBuffer *rb);
void SyncPeekPointer(ReceiveBuffer *rb);
void AcceptPeekedData(ReceiveBuffer *rb);
unsigned char PeekByte(ReceiveBuffer *rb);
int test_packet(int fd);
/* global variables */
extern KeyRing keyring;
extern ReceiveBuffer receivebuffer;
#endif /* CFONTZ633IO_H */ #endif /* CFONTZ633IO_H */
+13 -12
View File
@@ -53,6 +53,7 @@
* + Stopping the live reporting (of temperature) * + Stopping the live reporting (of temperature)
* + Stopping the reporting of temp and fan (is it necessary after reboot) * + Stopping the reporting of temp and fan (is it necessary after reboot)
* + Use of library for hbar and vbar (good but library could be better) * + Use of library for hbar and vbar (good but library could be better)
* + Support for keypad (Using a KeyRing)
* *
* THINGS TO DO: * THINGS TO DO:
* + Make the caching at least for heartbeat icon * + Make the caching at least for heartbeat icon
@@ -112,7 +113,7 @@
typedef enum { typedef enum {
standard, /* only char 0 is used for heartbeat */ standard, /* only char 0 is used for heartbeat */
vbar, /* vertical bars */ vbar, /* vertical bars */
hbar, /* horizontaln bars */ hbar, /* horizontal bars */
custom, /* custom settings */ custom, /* custom settings */
bignum, /* big numbers */ bignum, /* big numbers */
bigchar /* big characters */ bigchar /* big characters */
@@ -197,8 +198,8 @@ CFontz633_init (Driver *drvthis, char *args)
debug(RPT_INFO, "CFontz633: init(%p,%s)", drvthis, args ); debug(RPT_INFO, "CFontz633: init(%p,%s)", drvthis, args );
EmptyKeyRing(); EmptyKeyRing(&keyring);
EmptyReceiveBuffer(); EmptyReceiveBuffer(&receivebuffer);
/* Read config file */ /* Read config file */
/* Which model is it (CF633 or CF631)? */ /* Which model is it (CF633 or CF631)? */
@@ -469,8 +470,8 @@ CFontz633_flush (Driver *drvthis)
; ;
// deal with the differences // deal with the differences
if ( j < p->width ) { if (j < p->width) {
char out[23]; unsigned char out[23];
int diff_length; int diff_length;
int first_diff = j; int first_diff = j;
@@ -480,8 +481,8 @@ CFontz633_flush (Driver *drvthis)
// send the difference to the screen // send the difference to the screen
diff_length = j - first_diff; diff_length = j - first_diff;
out[1] = i; // line
out[0] = first_diff; // column out[0] = first_diff; // column
out[1] = i; // line
debug (RPT_INFO,"WriteDiff: l=%d c=%d count=%d string='%.*s'", debug (RPT_INFO,"WriteDiff: l=%d c=%d count=%d string='%.*s'",
out[0], out[1], diff_length, diff_length, out[0], out[1], diff_length, diff_length,
@@ -506,7 +507,7 @@ CFontz633_get_key (Driver *drvthis)
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
unsigned char key; unsigned char key;
key = GetKeyFromKeyRing(); key = GetKeyFromKeyRing(&keyring);
switch (key) { switch (key) {
case CF633_KEY_LEFT: case CF633_KEY_LEFT:
@@ -654,7 +655,7 @@ static void
CFontz633_no_live_report (Driver *drvthis) CFontz633_no_live_report (Driver *drvthis)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[2] = { 0, 0 }; unsigned char out[2] = { 0, 0 };
if (p->model == 633) { if (p->model == 633) {
for (out[0] = 0; out[0] < 8; out[0]++) for (out[0] = 0; out[0] < 8; out[0]++)
@@ -683,7 +684,7 @@ static void
CFontz633_no_temp_report (Driver *drvthis) CFontz633_no_temp_report (Driver *drvthis)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[4] = { 0, 0, 0, 0 }; unsigned char out[4] = { 0, 0, 0, 0 };
if (p->model == 633) if (p->model == 633)
send_bytes_message(p->fd, 4, CF633_Set_Up_Temperature_Reporting, out); send_bytes_message(p->fd, 4, CF633_Set_Up_Temperature_Reporting, out);
@@ -697,7 +698,7 @@ static void
CFontz633_reboot (Driver *drvthis) CFontz633_reboot (Driver *drvthis)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[3] = { 8, 18, 99 }; unsigned char out[3] = { 8, 18, 99 };
send_bytes_message(p->fd, 3, CF633_Reboot, out); send_bytes_message(p->fd, 3, CF633_Reboot, out);
sleep(2); sleep(2);
@@ -934,7 +935,7 @@ CFontz633_num (Driver *drvthis, int x, int num)
{ {
/* /*
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[5]; unsigned char out[5];
snprintf (out, sizeof(out), "%c%c%c", 28, x, num); snprintf (out, sizeof(out), "%c%c%c", 28, x, num);
write (p->fd, out, 3); write (p->fd, out, 3);
@@ -953,7 +954,7 @@ MODULE_EXPORT void
CFontz633_set_char (Driver *drvthis, int n, char *dat) CFontz633_set_char (Driver *drvthis, int n, char *dat)
{ {
PrivateData *p = drvthis->private_data; PrivateData *p = drvthis->private_data;
char out[9]; unsigned char out[9];
int row, col; int row, col;
if ((n < 0) || (n >= NUM_CCs)) if ((n < 0) || (n >= NUM_CCs))