/*
|
/*
|
* loadserv.c -- serial line load server
|
* loadserv.c -- serial line load server
|
*/
|
*/
|
|
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <stdarg.h>
|
#include <stdarg.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <termios.h>
|
#include <termios.h>
|
|
|
|
|
#define SYN ((unsigned char) 's')
|
#define SYN ((unsigned char) 's')
|
#define ACK ((unsigned char) 'a')
|
#define ACK ((unsigned char) 'a')
|
|
|
#define LINE_SIZE 520
|
#define LINE_SIZE 520
|
|
|
|
|
static int debugCmds = 1;
|
static int debugCmds = 1;
|
static int debugData = 0;
|
static int debugData = 0;
|
|
|
static FILE *loadFile = NULL;
|
static FILE *loadFile = NULL;
|
static int sfd = 0;
|
static int sfd = 0;
|
static struct termios origOptions;
|
static struct termios origOptions;
|
static struct termios currOptions;
|
static struct termios currOptions;
|
|
|
|
|
void serialClose(void);
|
void serialClose(void);
|
|
|
|
|
void error(char *fmt, ...) {
|
void error(char *fmt, ...) {
|
va_list ap;
|
va_list ap;
|
|
|
va_start(ap, fmt);
|
va_start(ap, fmt);
|
printf("Error: ");
|
printf("Error: ");
|
vprintf(fmt, ap);
|
vprintf(fmt, ap);
|
printf("\n");
|
printf("\n");
|
va_end(ap);
|
va_end(ap);
|
if (loadFile != NULL) {
|
if (loadFile != NULL) {
|
fclose(loadFile);
|
fclose(loadFile);
|
loadFile = NULL;
|
loadFile = NULL;
|
}
|
}
|
if (sfd != 0) {
|
if (sfd != 0) {
|
serialClose();
|
serialClose();
|
sfd = 0;
|
sfd = 0;
|
}
|
}
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
|
|
void serialOpen(char *serialPort) {
|
void serialOpen(char *serialPort) {
|
sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
|
sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
|
if (sfd == -1) {
|
if (sfd == -1) {
|
error("cannot open serial port '%s'", serialPort);
|
error("cannot open serial port '%s'", serialPort);
|
}
|
}
|
tcgetattr(sfd, &origOptions);
|
tcgetattr(sfd, &origOptions);
|
currOptions = origOptions;
|
currOptions = origOptions;
|
cfsetispeed(&currOptions, B38400);
|
cfsetispeed(&currOptions, B38400);
|
cfsetospeed(&currOptions, B38400);
|
cfsetospeed(&currOptions, B38400);
|
currOptions.c_cflag |= (CLOCAL | CREAD);
|
currOptions.c_cflag |= (CLOCAL | CREAD);
|
currOptions.c_cflag &= ~PARENB;
|
currOptions.c_cflag &= ~PARENB;
|
currOptions.c_cflag &= ~CSTOPB;
|
currOptions.c_cflag &= ~CSTOPB;
|
currOptions.c_cflag &= ~CSIZE;
|
currOptions.c_cflag &= ~CSIZE;
|
currOptions.c_cflag |= CS8;
|
currOptions.c_cflag |= CS8;
|
currOptions.c_cflag &= ~CRTSCTS;
|
currOptions.c_cflag &= ~CRTSCTS;
|
currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
|
currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
|
currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
|
currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
|
currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
|
currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
|
currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
|
currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
|
currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
|
currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
|
tcsetattr(sfd, TCSANOW, &currOptions);
|
tcsetattr(sfd, TCSANOW, &currOptions);
|
}
|
}
|
|
|
|
|
void serialClose(void) {
|
void serialClose(void) {
|
tcsetattr(sfd, TCSANOW, &origOptions);
|
tcsetattr(sfd, TCSANOW, &origOptions);
|
close(sfd);
|
close(sfd);
|
}
|
}
|
|
|
|
|
int serialSnd(unsigned char b) {
|
int serialSnd(unsigned char b) {
|
int n;
|
int n;
|
|
|
n = write(sfd, &b, 1);
|
n = write(sfd, &b, 1);
|
return n == 1;
|
return n == 1;
|
}
|
}
|
|
|
|
|
int serialRcv(unsigned char *bp) {
|
int serialRcv(unsigned char *bp) {
|
int n;
|
int n;
|
|
|
n = read(sfd, bp, 1);
|
n = read(sfd, bp, 1);
|
return n == 1;
|
return n == 1;
|
}
|
}
|
|
|
|
|
void connect(void) {
|
void connect(void) {
|
unsigned char b;
|
unsigned char b;
|
|
|
printf("SYN... ");
|
printf("SYN... ");
|
fflush(stdout);
|
fflush(stdout);
|
while (!serialSnd(ACK)) ;
|
while (!serialSnd(ACK)) ;
|
tcdrain(sfd);
|
tcdrain(sfd);
|
printf("ACK... ");
|
printf("ACK... ");
|
fflush(stdout);
|
fflush(stdout);
|
while (!serialRcv(&b)) ;
|
while (!serialRcv(&b)) ;
|
if (b != ACK) {
|
if (b != ACK) {
|
error("cannot synchronize with client");
|
error("cannot synchronize with client");
|
}
|
}
|
printf("connected\n");
|
printf("connected\n");
|
}
|
}
|
|
|
|
|
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
|
char *serialPort;
|
char *loadName;
|
char *loadName;
|
unsigned char b;
|
unsigned char b;
|
unsigned char cmd;
|
unsigned char cmd;
|
char line[LINE_SIZE];
|
char line[LINE_SIZE];
|
int n, i;
|
int n, i;
|
|
|
if (argc != 3) {
|
if (argc != 3) {
|
printf("Usage: %s <serial port> <file to be loaded>\n", argv[0]);
|
printf("Usage: %s <serial port> <file to be loaded>\n", argv[0]);
|
exit(1);
|
exit(1);
|
}
|
}
|
|
serialPort = argv[1];
|
loadName = argv[2];
|
loadName = argv[2];
|
loadFile = fopen(loadName, "rt");
|
loadFile = fopen(loadName, "rt");
|
if (loadFile == NULL) {
|
if (loadFile == NULL) {
|
error("cannot open file to be loaded '%s'", loadName);
|
error("cannot open file to be loaded '%s'", loadName);
|
}
|
}
|
/* open serial interface */
|
/* open serial interface */
|
serialOpen(argv[1]);
|
serialOpen(serialPort);
|
/* wait for client to connect */
|
/* wait for client to connect */
|
printf("Waiting for client...\n");
|
printf("Waiting for client...\n");
|
while (1) {
|
while (1) {
|
if (serialRcv(&b) && b == SYN) {
|
if (serialRcv(&b) && b == SYN) {
|
break;
|
break;
|
}
|
}
|
}
|
}
|
connect();
|
connect();
|
fseek(loadFile, 0, SEEK_SET);
|
fseek(loadFile, 0, SEEK_SET);
|
/* connected, now handle requests */
|
/* connected, now handle requests */
|
while (1) {
|
while (1) {
|
while (!serialRcv(&cmd)) ;
|
while (!serialRcv(&cmd)) ;
|
if (cmd == 'q') {
|
if (cmd == 'q') {
|
/* quit */
|
/* quit */
|
if (debugCmds) {
|
if (debugCmds) {
|
printf("quit\n");
|
printf("quit\n");
|
}
|
}
|
break;
|
break;
|
}
|
}
|
if (cmd == SYN) {
|
if (cmd == SYN) {
|
/* this happens if the client has been reset */
|
/* this happens if the client has been reset */
|
connect();
|
connect();
|
fseek(loadFile, 0, SEEK_SET);
|
fseek(loadFile, 0, SEEK_SET);
|
continue;
|
continue;
|
}
|
}
|
if (cmd != 'r') {
|
if (cmd != 'r') {
|
/* unknown command */
|
/* unknown command */
|
if (debugCmds) {
|
if (debugCmds) {
|
printf("unknown... UNCMD\n");
|
printf("unknown... UNCMD\n");
|
}
|
}
|
continue;
|
continue;
|
}
|
}
|
/* only read requests get here */
|
/* only read requests get here */
|
if (debugCmds) {
|
if (debugCmds) {
|
printf("reading record... ");
|
printf("reading record... ");
|
fflush(stdout);
|
fflush(stdout);
|
}
|
}
|
if (fgets(line, LINE_SIZE, loadFile) == NULL) {
|
if (fgets(line, LINE_SIZE, loadFile) == NULL) {
|
if (debugCmds) {
|
if (debugCmds) {
|
printf("RDERR\n");
|
printf("RDERR\n");
|
}
|
}
|
} else {
|
} else {
|
n = strlen(line);
|
n = strlen(line);
|
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
while (!serialSnd(line[i])) ;
|
while (!serialSnd(line[i])) ;
|
}
|
}
|
tcdrain(sfd);
|
tcdrain(sfd);
|
if (debugCmds) {
|
if (debugCmds) {
|
printf("OK\n");
|
printf("OK\n");
|
}
|
}
|
if (debugData) {
|
if (debugData) {
|
printf("%s", line);
|
printf("%s", line);
|
}
|
}
|
}
|
}
|
}
|
}
|
if (loadFile != NULL) {
|
if (loadFile != NULL) {
|
fclose(loadFile);
|
fclose(loadFile);
|
loadFile = NULL;
|
loadFile = NULL;
|
}
|
}
|
if (sfd != 0) {
|
if (sfd != 0) {
|
serialClose();
|
serialClose();
|
sfd = 0;
|
sfd = 0;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|