From c3aa33d95fa0cc6be2734381d039b5ae0f150cef Mon Sep 17 00:00:00 2001 From: dschmenk Date: Fri, 31 May 2013 14:27:43 -0700 Subject: [PATCH] Add tty device command line option Also broke out common a2 io routines into a2lib.c --- src/a2lib.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/a2mon.c | 89 +------------------------------------------------- src/a2pid.c | 80 +++++++++++++++++++++++++++------------------ src/dskread.c | 89 +------------------------------------------------- src/dskwrite.c | 89 +------------------------------------------------- 5 files changed, 140 insertions(+), 296 deletions(-) create mode 100755 src/a2lib.c diff --git a/src/a2lib.c b/src/a2lib.c new file mode 100755 index 0000000..6b674c9 --- /dev/null +++ b/src/a2lib.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int a2open(char *ipaddr) +{ + struct sockaddr_in piaddr; + int res; + int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (fd < 0) + { + perror("Cannot create socket"); + return -1; + } + memset(&piaddr, 0, sizeof(piaddr)); + piaddr.sin_family = AF_INET; + piaddr.sin_port = htons(6502); + res = inet_pton(AF_INET, ipaddr, &piaddr.sin_addr); + if (res < 0) + { + perror("First parameter is not a valid address family"); + close(fd); + return -1; + } + else if (res == 0) + { + perror("Char string (second parameter does not contain valid ipaddress)"); + close(fd); + return -1; + } + if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0) + { + perror("Connect failed"); + close(fd); + return -1; + } + return fd; +} +void a2close(int fd) +{ + char closepkt; + closepkt = 0xFF; + write(fd, &closepkt, 1); + shutdown(fd, SHUT_RDWR); + close(fd); +} +int a2read(int fd, int address, int count, char *buffer) +{ + char readpkt[8]; + readpkt[0] = 0x90; // read + readpkt[1] = address; + readpkt[2] = address >> 8; + readpkt[3] = count; + readpkt[4] = count >> 8; + write(fd, readpkt, 5); + read(fd, buffer, count); + read(fd, readpkt, 2); + return ((unsigned char)readpkt[0] == 0x9E); +} +int a2write(int fd, int address, int count, char *buffer) +{ + char writepkt[8]; + writepkt[0] = 0x92; // write + writepkt[1] = address; + writepkt[2] = address >> 8; + writepkt[3] = count; + writepkt[4] = count >> 8; + write(fd, writepkt, 5); + write(fd, buffer, count); + read(fd, writepkt, 2); + return ((unsigned char)writepkt[0] == 0x9E); +} +int a2call(int fd, int address, int *result) +{ + char callpkt[4]; + callpkt[0] = 0x94; // call + callpkt[1] = address; + callpkt[2] = address >> 8; + write(fd, callpkt, 3); + read(fd, callpkt, 2); + if (result) + *result = (unsigned char)callpkt[1]; + return ((unsigned char)callpkt[0] == 0x9E); +} diff --git a/src/a2mon.c b/src/a2mon.c index 2473c80..b1232c7 100755 --- a/src/a2mon.c +++ b/src/a2mon.c @@ -1,92 +1,5 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include "a2lib.c" -int a2open(char *ipaddr) -{ - struct sockaddr_in piaddr; - int res; - int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (fd < 0) - { - perror("Cannot create socket"); - return -1; - } - memset(&piaddr, 0, sizeof(piaddr)); - piaddr.sin_family = AF_INET; - piaddr.sin_port = htons(6502); - res = inet_pton(AF_INET, ipaddr, &piaddr.sin_addr); - if (res < 0) - { - perror("First parameter is not a valid address family"); - close(fd); - return -1; - } - else if (res == 0) - { - perror("Char string (second parameter does not contain valid ipaddress)"); - close(fd); - return -1; - } - if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0) - { - perror("Connect failed"); - close(fd); - return -1; - } - return fd; -} -void a2close(int fd) -{ - char closepkt; - closepkt = 0xFF; - write(fd, &closepkt, 1); - shutdown(fd, SHUT_RDWR); - close(fd); -} -int a2read(int fd, int address, int count, char *buffer) -{ - char readpkt[8]; - readpkt[0] = 0x90; // read - readpkt[1] = address; - readpkt[2] = address >> 8; - readpkt[3] = count; - readpkt[4] = count >> 8; - write(fd, readpkt, 5); - read(fd, buffer, count); - read(fd, readpkt, 2); - return ((unsigned char)readpkt[0] == 0x9E); -} -int a2write(int fd, int address, int count, char *buffer) -{ - char writepkt[8]; - writepkt[0] = 0x92; // write - writepkt[1] = address; - writepkt[2] = address >> 8; - writepkt[3] = count; - writepkt[4] = count >> 8; - write(fd, writepkt, 5); - write(fd, buffer, count); - read(fd, writepkt, 2); - return ((unsigned char)writepkt[0] == 0x9E); -} -int a2call(int fd, int address, int *result) -{ - char callpkt[4]; - callpkt[0] = 0x94; // call - callpkt[1] = address; - callpkt[2] = address >> 8; - write(fd, callpkt, 3); - read(fd, callpkt, 2); - if (result) - *result = (unsigned char)callpkt[1]; - return ((unsigned char)callpkt[0] == 0x9E); -} void prbytes(int address, int count, char *data) { int i; diff --git a/src/a2pid.c b/src/a2pid.c index 812b109..821b46d 100755 --- a/src/a2pid.c +++ b/src/a2pid.c @@ -15,7 +15,6 @@ #include #define BAUDRATE B115200 -#define A2DEVICE "/dev/ttyAMA0" #define _POSIX_SOURCE 1 /* POSIX compliant source */ #define FALSE 0 #define TRUE 1 @@ -491,44 +490,61 @@ void main(int argc, char **argv) { struct uinput_user_dev uidev; struct termios oldtio,newtio; - char iopkt[16]; + unsigned char iopkt[16]; int i, c, lastbtn; int a2fd, kbdfd, moufd, srvfd, reqfd, maxfd; struct sockaddr_in servaddr; fd_set readset, openset; + char *devtty = "/dev/ttyAMA0"; /* default for Raspberry Pi */ /* - * Are we running as a daemon? + * Parse arguments */ - if ((argc > 1) && (strcmp(argv[1], "--daemon") == 0)) + if (argc > 1) { - pid_t pid, sid; /* our process ID and Session ID */ + /* + * Are we running as a daemon? + */ + if (strcmp(argv[1], "--daemon") == 0) + { + pid_t pid, sid; /* our process ID and Session ID */ - pid = fork(); /* fork off the parent process */ - if (pid < 0) - die("a2pid: fork() failure"); - /* - * If we got a good PID, then - * we can exit the parent process - */ - if (pid > 0) - exit(EXIT_SUCCESS); - umask(0); /* change the file mode mask */ - /* - * Open any logs here - */ - sid = setsid(); /* create a new SID for the child process */ - if (sid < 0) - die("a2pid: setsid() failure"); - if ((chdir("/")) < 0) /* change the current working directory */ - die("a2pid: chdir() failure"); - /* - * Close out the standard file descriptors - */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - isdaemon = TRUE; + pid = fork(); /* fork off the parent process */ + if (pid < 0) + die("a2pid: fork() failure"); + /* + * If we got a good PID, then + * we can exit the parent process + */ + if (pid > 0) + exit(EXIT_SUCCESS); + umask(0); /* change the file mode mask */ + /* + * Open any logs here + */ + sid = setsid(); /* create a new SID for the child process */ + if (sid < 0) + die("a2pid: setsid() failure"); + if ((chdir("/")) < 0) /* change the current working directory */ + die("a2pid: chdir() failure"); + /* + * Close out the standard file descriptors + */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + isdaemon = TRUE; + /* + * Another argument must be tty device + */ + if (argc > 2) + devtty = argv[2]; + } + else + /* + * Must be tty device + */ + devtty = argv[1]; } /* * Create keyboard input device @@ -605,7 +621,7 @@ void main(int argc, char **argv) * Open serial port. */ prlog("a2pid: Open serial port\n"); - a2fd = open(A2DEVICE, O_RDWR | O_NOCTTY); + a2fd = open(devtty, O_RDWR | O_NOCTTY); if (a2fd < 0) die("error: serial port open"); tcgetattr(a2fd, &oldtio); /* save current port settings */ @@ -891,4 +907,4 @@ void main(int argc, char **argv) ioctl(kbdfd, UI_DEV_DESTROY); close(moufd); close(kbdfd); -} \ No newline at end of file +} diff --git a/src/dskread.c b/src/dskread.c index ec2175f..0303f27 100755 --- a/src/dskread.c +++ b/src/dskread.c @@ -1,92 +1,5 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include "a2lib.c" -int a2open(char *ipaddr) -{ - struct sockaddr_in piaddr; - int res; - int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (fd < 0) - { - perror("Cannot create socket"); - return -1; - } - memset(&piaddr, 0, sizeof(piaddr)); - piaddr.sin_family = AF_INET; - piaddr.sin_port = htons(6502); - res = inet_pton(AF_INET, ipaddr, &piaddr.sin_addr); - if (res < 0) - { - perror("First parameter is not a valid address family"); - close(fd); - return -1; - } - else if (res == 0) - { - perror("Char string (second parameter does not contain valid ipaddress)"); - close(fd); - return -1; - } - if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0) - { - perror("Connect failed"); - close(fd); - return -1; - } - return fd; -} -void a2close(int fd) -{ - char closepkt; - closepkt = 0xFF; - write(fd, &closepkt, 1); - shutdown(fd, SHUT_RDWR); - close(fd); -} -int a2read(int fd, int address, int count, char *buffer) -{ - char readpkt[8]; - readpkt[0] = 0x90; // read - readpkt[1] = address; - readpkt[2] = address >> 8; - readpkt[3] = count; - readpkt[4] = count >> 8; - write(fd, readpkt, 5); - read(fd, buffer, count); - read(fd, readpkt, 2); - return ((unsigned char)readpkt[0] == 0x9E); -} -int a2write(int fd, int address, int count, char *buffer) -{ - char writepkt[8]; - writepkt[0] = 0x92; // write - writepkt[1] = address; - writepkt[2] = address >> 8; - writepkt[3] = count; - writepkt[4] = count >> 8; - write(fd, writepkt, 5); - write(fd, buffer, count); - read(fd, writepkt, 2); - return ((unsigned char)writepkt[0] == 0x9E); -} -int a2call(int fd, int address, int *result) -{ - char callpkt[4]; - callpkt[0] = 0x94; // call - callpkt[1] = address; - callpkt[2] = address >> 8; - write(fd, callpkt, 3); - read(fd, callpkt, 2); - if (result) - *result = (unsigned char)callpkt[1]; - return ((unsigned char)callpkt[0] == 0x9E); -} char online[] = { // ORG $300 0x20, 0x00, 0xBF, // JSR $BF00 (PRODOS) diff --git a/src/dskwrite.c b/src/dskwrite.c index dcde389..74b4aa2 100755 --- a/src/dskwrite.c +++ b/src/dskwrite.c @@ -1,92 +1,5 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include "a2lib.c" -int a2open(char *ipaddr) -{ - struct sockaddr_in piaddr; - int res; - int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (fd < 0) - { - perror("Cannot create socket"); - return -1; - } - memset(&piaddr, 0, sizeof(piaddr)); - piaddr.sin_family = AF_INET; - piaddr.sin_port = htons(6502); - res = inet_pton(AF_INET, ipaddr, &piaddr.sin_addr); - if (res < 0) - { - perror("First parameter is not a valid address family"); - close(fd); - return -1; - } - else if (res == 0) - { - perror("Char string (second parameter does not contain valid ipaddress)"); - close(fd); - return -1; - } - if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0) - { - perror("Connect failed"); - close(fd); - return -1; - } - return fd; -} -void a2close(int fd) -{ - char closepkt; - closepkt = 0xFF; - write(fd, &closepkt, 1); - shutdown(fd, SHUT_RDWR); - close(fd); -} -int a2read(int fd, int address, int count, char *buffer) -{ - char readpkt[8]; - readpkt[0] = 0x90; // read - readpkt[1] = address; - readpkt[2] = address >> 8; - readpkt[3] = count; - readpkt[4] = count >> 8; - write(fd, readpkt, 5); - read(fd, buffer, count); - read(fd, readpkt, 2); - return ((unsigned char)readpkt[0] == 0x9E); -} -int a2write(int fd, int address, int count, char *buffer) -{ - char writepkt[8]; - writepkt[0] = 0x92; // write - writepkt[1] = address; - writepkt[2] = address >> 8; - writepkt[3] = count; - writepkt[4] = count >> 8; - write(fd, writepkt, 5); - write(fd, buffer, count); - read(fd, writepkt, 2); - return ((unsigned char)writepkt[0] == 0x9E); -} -int a2call(int fd, int address, int *result) -{ - char callpkt[4]; - callpkt[0] = 0x94; // call - callpkt[1] = address; - callpkt[2] = address >> 8; - write(fd, callpkt, 3); - read(fd, callpkt, 2); - if (result) - *result = (unsigned char)callpkt[1]; - return ((unsigned char)callpkt[0] == 0x9E); -} char online[] = { // ORG $300 0x20, 0x00, 0xBF, // JSR $BF00 (PRODOS)