keytag.c
Go to the documentation of this file.00001
00045 #include "keytag.h"
00046
00047 #include <kern/kfile.h>
00048
00049 #include <drv/timer.h>
00050 #include <drv/ser.h>
00051
00052 #include <cfg/macros.h>
00053 #include <cfg/debug.h>
00054
00055 static void keytag_clearPkt(struct TagPacket *pkt)
00056 {
00057 pkt->sync = false;
00058 pkt->len = 0;
00059 }
00060
00061 void keytag_init(struct TagPacket *pkt, struct KFile *comm, struct KFile *tag)
00062 {
00063 keytag_clearPkt(pkt);
00064 pkt->host = comm;
00065 pkt->tag = tag;
00066 }
00067
00068 void keytag_poll(struct TagPacket *pkt)
00069 {
00070 int c;
00071
00072
00073 while ((c = kfile_getc(pkt->tag)) != EOF)
00074 {
00075
00076 if (c == TAG_STX)
00077 {
00078
00079 if (pkt->sync)
00080 kprintf("TAG double sync!\n");
00081 keytag_clearPkt(pkt);
00082 pkt->sync = true;
00083 }
00084 else if (pkt->sync)
00085 {
00086
00087 if (c == TAG_ETX)
00088 {
00089 pkt->buf[TAG_MAX_PRINT_CHARS] = '\x0';
00090
00091 kfile_printf(pkt->host, "tag %s", pkt->buf);
00092 pkt->sync = false;
00093 }
00094 else
00095 {
00096
00097 if (pkt->len >= TAG_MAX_LEN)
00098 {
00099 kprintf("TAG buffer overflow\n");
00100 pkt->sync = false;
00101 }
00102 else
00103 {
00104
00105 if (pkt->sync)
00106 {
00107 pkt->buf[pkt->len] = c;
00108 pkt->len++;
00109 }
00110 }
00111 }
00112 }
00113 }
00114 if (kfile_error(pkt->tag) != 0)
00115 {
00116 kprintf("Error %08x\n", kfile_error(pkt->tag));
00117 kfile_clearerr(pkt->tag);
00118 }
00119
00120 }