Add tty device command line option

Also broke out common a2 io routines into a2lib.c
This commit is contained in:
dschmenk 2013-05-31 14:27:43 -07:00
parent 28c563341f
commit c3aa33d95f
5 changed files with 140 additions and 296 deletions

89
src/a2lib.c Executable file
View File

@ -0,0 +1,89 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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);
}

View File

@ -1,92 +1,5 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;

View File

@ -15,7 +15,6 @@
#include <linux/uinput.h>
#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);
}
}

View File

@ -1,92 +1,5 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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)

View File

@ -1,92 +1,5 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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)