📄 boss.c
字号:
* R e a d L i n e * * Read one complete record from the CASIO */voidReadLine (){ /* * Read the CASIO record header */ if (debugmode > 2) fprintf (dbg, "calling readheader\n"); ReadHeader (); if (debugmode > 2) fprintf (dbg, "\nDone ReadHeader\n"); if (MHeader.nbytes == 0x00) { if (debugmode > 1) fprintf (dbg, "\nsaw last data frame\n"); if (MHeader.address == 0x0100) { if (debugmode > 1) fprintf (dbg, "\nsending ack to continue tarnsmission \n"); WriteByte (ACK); } } /* * Read the data bytes in the record */ for (nbytes = 0; nbytes < MHeader.nbytes && !Stopped; nbytes++) { DataBuffer[nbytes] = ReadHex (); if (debugmode > 1) fprintf (dbg, "\nDatabytes ---> %c\n", DataBuffer[nbytes]); /* store the databyte into the data file */ if (tflag) fprintf (data, "%c", DataBuffer[nbytes]); } if (tflag) fprintf (data, "\n"); /* * Read the checksum */ DataBuffer[nbytes++] = ReadHex (); if (debugmode > 2) fprintf (dbg, "\nDone readline\n");}/* * W r i t e L i n e * * Write one complete record from * the file to the CASIO */voidWriteLine (FILE * infile){ int i; /* * Read the next record header from the file */ if (fread (&MHeader, sizeof (MHeader), 1, infile) != 1) { Stopped = 1; return; } /* * Read the databytes and the checksum from the file */ fread (DataBuffer, MHeader.nbytes + 1, 1, infile); /* * Convert record to something for CASIO */ WBuffer[0] = ':'; WBuffer[1] = htoa (MHeader.nbytes >> 4); WBuffer[2] = htoa (MHeader.nbytes); WBuffer[3] = htoa (MHeader.type >> 4); WBuffer[4] = htoa (MHeader.type); WBuffer[5] = htoa (MHeader.address >> 4); WBuffer[6] = htoa (MHeader.address); WBuffer[7] = htoa (MHeader.address >> 12); WBuffer[8] = htoa (MHeader.address >> 8); for (i = 0; i < MHeader.nbytes + 1; i++) { WBuffer[2 * i + 9] = htoa (DataBuffer[i] >> 4); WBuffer[2 * i + 10] = htoa (DataBuffer[i] & 0x0f); } WriteBuffer (2 * MHeader.nbytes + 11); /* * Wait for reply */ if (MHeader.nbytes == 0 && MHeader.address == 0x0100) { while (!Stopped) { i = ReadByte (WAIT); switch (i) { case NACK: fprintf (stderr, "\nTransmission error, stopped\n"); Stopped = 1; WriteByte (STOP); break; case ACK: return; case STOP: case XON: case XOFF: return; default: fprintf (stderr, "0x%02x\n", i); return; } } }}/* * W r i t e B u f f e r * * Write the data in the CASIO Write Buffer * to the CASIO */voidWriteBuffer (int num){ int i; for (i = 0; i < num; i++) { WriteByte (WBuffer[i]); }}/* * U p p e r C a s e * * Turn a string into all uppercase characters */voidUpperCase (char *s){ while (*s) { if (*s >= 'a' && *s <= 'z') { *s = *s - 'a' + 'A'; } s++; }}/* * R e a d B y t e * * Read a byte from the COM port */intReadByte (byte mode){ int i, j; long TimeOut; if (LastRead) { if (debugmode > 2) fprintf (dbg, "we did read %c before ==> just return \n", LastRead); i = LastRead; LastRead = 0; return (i); } while (1) { for (TimeOut = 0; TimeOut < TIMEOUT; TimeOut++) { if (Stopped) { return (-1); } /* * Check if the user pressed ESC to stop communication */ if (kbhit ()) { if (getchar () == 0x1b) { fprintf (stderr, "\n\nESC pressed, stop communication\n"); Stopped = 1; WriteByte (STOP); return (-1); } } if (debugmode > 2) fprintf (dbg, "\n trying to read\n "); /* read a char and store in i */ j = read (Port, &i, 1);#ifndef __i386__ NTOHL(i);#endif if (debugmode > 2) fprintf (dbg, "\n read\n "); if (j == 0) { if (debugmode > 2) fprintf (dbg, "\nEOF encountered\n"); } if (j > 0) { if (debugmode > 2) fprintf (dbg, "\nsuccesful READ\n"); } if (j == -1) { if (debugmode > 2) fprintf (dbg, "\nUnsuccessfull READ\n"); } sprintf (bufin, "received %x\n", i & 0xff); if (debugmode > 1) fprintf (dbg, "%s\n", bufin); i &= 0xff; if (i == STOP) { fprintf (stderr, "\nCASIO has stopped communication\n"); fprintf (dbg, "\nSTOP received \n"); Stopped = 1; } if (i == XOFF) { WriteStatus = XOFF; } if (i == XON) { WriteStatus = XON; } return (i); } fprintf (stderr, "\n timeout\n"); if (mode == NOWAIT) { return (-1); } }}/* * W r i t e B y t e * * Write a byte to the COM port */voidWriteByte (byte d){ int i; long TimeOut; if (debugmode > 2) fprintf (dbg, "\nWRITEBYTE: writestatus = %x\n", WriteStatus); if (!NOCHECK && !Stopped) { if ((i = ReadByte (NOWAIT)) == XOFF) { WriteStatus = XOFF; } else { LastRead = i; } if (WriteStatus == XOFF) { while (ReadByte (WAIT) != XON && !Stopped); WriteStatus = XON; } } TimeOut = 0; if (debugmode > 1) fprintf (dbg, "\n sending %x to serial port \n", d);#ifndef __i386__ HTONL(i);#endif /* write the character to the serial port */ if (write (Port, &d, 1) != 1) { fprintf (stderr, "\nERROR WriteByte: couldnt write the char %c to serial port \n", d); terminate (); } if (Direction == WRITE) { usleep (4000); }}/* * R e a d H e x * * Read two ASCII digits from the COM port * and convert these into a hexadecimal value */byteReadHex (){ return ((atoh (ReadByte (WAIT)) << 4) | (atoh (ReadByte (WAIT))));}/* * a t o h * * Convert an ASCII number to a hexadecimal value */byteatoh (char c){ if (c >= '0' && c <= '9') { return (c - '0'); } if (c >= 'a' && c <= 'f') { return (c - 'a' + 10); } if (c >= 'A' && c <= 'F') { return (c - 'A' + 10); } return (0);}/* * h t o a * * Convert a hexadecimal value to an ASCII number */charhtoa (byte c){ c &= 0xf; if (c <= 9) { return (c + '0'); } return (c - 10 + 'A');}/* * D i s p l a y S t a t u s */voidDisplayStatus (){ if (debugmode > 2) { fprintf (dbg, "\n Display status: Mheader,nbytes %x\n", MHeader.nbytes); fprintf (dbg, "\n Display status: DatadBuffer[0] %x\n", DataBuffer[0]); } if (MHeader.nbytes == 0x02) { switch (DataBuffer[0]) { case 0x90: { fprintf (stderr, "\nPhone: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> phone\n"); if (tflag) fprintf (data, "\n===================== > phone < ==========\n"); Record = 0; break; } case 0xa0: { fprintf (stderr, "\nMemo: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> memo\n"); if (tflag) fprintf (data, "\n===================== > MEMO < =============\n"); Record = 0; break; } case 0x91: { fprintf (stderr, "\nReminder: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> reminder\n"); if (tflag) fprintf (data, "\n===================== > REMINDER < =============\n"); Record = 0; break; } case 0xb0: { fprintf (stderr, "\nSchedule: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> schedule\n"); if (tflag) fprintf (data, "\n===================== > SCHEDULE < =============\n"); Record = 0; break; } case 0xb1: { fprintf (stderr, "\nFree File: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> Free File\n"); if (tflag) fprintf (data, "\n===================== > Free File < =============\n"); Record = 0; break; } case 0x92: { fprintf (stderr, "\nExpense: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> Expense \n"); if (tflag) fprintf (data, "\n===================== > Expense < =============\n"); Record = 0; break; } case 0xA1: { fprintf (stderr, "\nReminder 2: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> Reminder 2\n"); if (tflag) fprintf (data, "\n===================== > Reminder 2 < =============\n"); Record = 0; break; } case 0xC1: { fprintf (stderr, "\nToDo List: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> Todo list\n"); if (tflag) fprintf (data, "\n===================== > todo list < =============\n"); Record = 0; break; } case 0xC0: { fprintf (stderr, "\nBussines Card: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> Bussines Card\n"); if (tflag) fprintf (data, "\n===================== > Bussines Card < =============\n"); Record = 0; break; } case 0x80: { fprintf (stderr, "\nCalendar: "); if (debugmode > 1) fprintf (dbg, "\n Data ==> calendar\n"); if (tflag) fprintf (data, "\n===================== > Calendar < =============\n"); Record = 0; break; } default: { if (tflag) fprintf (data, "\n===================== > UNKNOWN? < =============\n"); fprintf (stderr, "\nSection 0x%02x: ", DataBuffer[0]); } Record = 0; } } else if (MHeader.nbytes == 0x00) { if (MHeader.address == 0x0100) { fprintf (stderr, "\b\b\b%3d", ++Record); } else if (MHeader.address == 0xff00) { fprintf (stderr, "\nDONE!!\n"); Stopped = 1; } }}/* poll the keyboard for a character *//* idea borrowed from The Linux Journal article of Sept 95 issue 17 */intkbhit (void){ struct timeval tv; fd_set read_fd;/*do not wait at all, not even a microsecond */ tv.tv_sec = 0; tv.tv_usec = 0;/* must be done first to initilize read_fd */ FD_ZERO (&read_fd);/* makes select() ask if input is ready : * 0 is the file descriptor stdin */ FD_SET (0, &read_fd);/* the first parameter is the number of the * largest file descriptor to check + 1. */ if (select (1, &read_fd, NULL, /* NO writes */ NULL, /* NO exceptions */ &tv) == -1) return 0; /* An error occured *//* read_fd now holds a bitmap of files that are * readable. We test the entry for the standard * input (file 0). */ if (FD_ISSET (0, &read_fd)) /* character pending on stdin */ return 1;/* no charcaters were pending */ return 0;}static voidsig_catch (int signo){ fprintf (stderr, "\nsignal caught %d\n", signo); fprintf (dbg, "\nsignal caught %d\n", signo); /*inform */ fprintf (stderr, "\nstopping contact with casio \n\n"); /* should also reset the serial port being used by casio and close all files */ terminate ();}inttty_reset (int fd) /* restore terminal's mode */{ if (fd == Port) { if (tcsetattr (fd, TCSAFLUSH, &oldterm) < 0) fprintf (stderr, "\nFailed to reset serial port\n"); return (-1); } else if (fd == STDIN_FILENO) { if (tcsetattr (fd, TCSAFLUSH, &save_stdin) < 0) fprintf (stderr, "\nFailed to reset stdin\n"); return (-1); } return (0);}/* the terminator */voidterminate (){ blkflg |= O_NONBLOCK; if (fcntl (Port, F_SETFL, blkflg) < 0) { fprintf (stderr, "\nexiting ..\n"); exit (-1); } if (debugmode > 1) fprintf (dbg, "\nterminate: writting stop to port\n"); Stopped = 1; WriteByte (STOP); /* reset terminals */ if (debugmode > 1) fprintf (dbg, "\n reseting stdin\n"); tty_reset (STDIN_FILENO); if (debugmode > 1) fprintf (dbg, "\n reseting port\n"); tty_reset (Port); /* close all open files */ if (debugmode > 1) fprintf (dbg, "\n closing casiofile\n"); fclose (casiofile); if (debugmode > 1) fprintf (dbg, "\n closing Port\n"); close (Port); if (tflag) { if (debugmode > 1) fprintf (dbg, "\n closing data file\n"); fclose (data); } if (debugmode > 1) fprintf (dbg, "\n closing debug file\n"); fclose (dbg); exit (0);}/* borrowed from W. Richard Stevens bookAdvanced Programming in the Unix Environment */inttty_cbreak (int fd) /* put terminal into a cbreak mode */{ struct termios buf; if (fd == STDIN_FILENO) { if (tcgetattr (fd, &save_stdin) < 0) return (-1); } buf = save_stdin; /* structure copy */ buf.c_lflag &= ~(ECHO | ICANON); /* echo off, canonical mode off */ buf.c_cc[VMIN] = 1; /* Case B: 1 byte at a time, no timer */ buf.c_cc[VTIME] = 0; if (tcsetattr (fd, TCSAFLUSH, &buf) < 0) return (-1); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -