Commit c28cc2d7 authored by Adrian's avatar Adrian
Browse files

Should werk!

parent 01fc9b99
......@@ -23,7 +23,7 @@
#define SIZE_PIC (picX * picY)
#define ORIGX 3280
#define ORIGY 2464
#define ORIGY 2464
#define ORIGS 4128
int otrok_umrl = 0;
......@@ -38,337 +38,336 @@ uint64_t seg = 5;
char vrs[];
void handle_child(int s __attribute__((unused)), siginfo_t *i,
void *ctx __attribute__((unused))) {
otrok_umrl++;
otrok_pid = i->si_pid;
otrok_status = i->si_status;
otrok_utime = i->si_utime;
otrok_stime = i->si_stime;
void *ctx __attribute__((unused))) {
otrok_umrl++;
otrok_pid = i->si_pid;
otrok_status = i->si_status;
otrok_utime = i->si_utime;
otrok_stime = i->si_stime;
}
void handle_me(int s __attribute__((unused))) { samomor++; }
int main(int argc, char **argv) {
int r = 0;
pid_t c = -1;
int r = 0;
pid_t c = -1;
if (argc != 1 + 4) {
fprintf(stderr, "uporaba: %s /dev/s 9600 bin/d img\n", S0(argv[0]));
return 1;
}
if (argc != 1 + 4) {
fprintf(stderr, "uporaba: %s /dev/s 9600 bin/d img\n", S0(argv[0]));
return 1;
}
#define IMG argv[4]
#define BEG 'B'
#define END 'E'
#define RAW 'R'
#define PIC 'P'
struct sigaction act = {.sa_sigaction = handle_child,
.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART |
SA_SIGINFO};
if (sigaction(SIGCHLD, &act, NULL) == -1) {
perror("sigaction");
return 3;
}
act.sa_handler = handle_me;
act.sa_sigaction = NULL;
act.sa_flags = SA_RESTART;
if (sigaction(SIGINT, &act, NULL) == -1) {
perror("sigaction");
return 4;
}
if (sigaction(SIGHUP, &act, NULL) == -1) {
perror("sigaction");
return 5;
}
char *d = argv[1];
int b = atoi(argv[2]);
int u = open(d, O_RDWR | O_NOCTTY | O_NDELAY | O_CLOEXEC);
if (u == -1) {
perror("open");
return 6;
}
struct termios o;
if (tcgetattr(u, &o) == -1) {
perror("tcgetattr");
r = 7;
goto r;
}
o.c_iflag = IGNPAR;
o.c_oflag = 0;
o.c_cflag = CS8 | CREAD | CLOCAL;
o.c_lflag = 0; // brez ICANON (privzeto je 1, kar medpomni vrstično)
if (tcflush(u, TCIOFLUSH) == -1) { // briše prejete, a ne prebrane (I), in
// napisane, a neposlane (O), podatke.
perror("tcflush");
r = 8;
goto r;
}
int y = 0;
struct sigaction act = {.sa_sigaction = handle_child,
.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART |
SA_SIGINFO};
if (sigaction(SIGCHLD, &act, NULL) == -1) {
perror("sigaction");
return 3;
}
act.sa_handler = handle_me;
act.sa_sigaction = NULL;
act.sa_flags = SA_RESTART;
if (sigaction(SIGINT, &act, NULL) == -1) {
perror("sigaction");
return 4;
}
if (sigaction(SIGHUP, &act, NULL) == -1) {
perror("sigaction");
return 5;
}
char *d = argv[1];
int b = atoi(argv[2]);
int u = open(d, O_RDWR | O_NOCTTY | O_NDELAY | O_CLOEXEC);
if (u == -1) {
perror("open");
return 6;
}
struct termios o;
if (tcgetattr(u, &o) == -1) {
perror("tcgetattr");
r = 7;
goto r;
}
o.c_iflag = IGNPAR;
o.c_oflag = 0;
o.c_cflag = CS8 | CREAD | CLOCAL;
o.c_lflag = 0; // brez ICANON (privzeto je 1, kar medpomni vrstično)
if (tcflush(u, TCIOFLUSH) == -1) { // briše prejete, a ne prebrane (I), in
// napisane, a neposlane (O), podatke.
perror("tcflush");
r = 8;
goto r;
}
int y = 0;
#define R(x) \
if (b == x) { \
cfsetospeed(&o, B##x); \
cfsetispeed(&o, B##x); \
y++; \
}
R(50)
R(75)
R(110) R(134) R(150) R(200) R(300) R(600) R(1200) R(1800) R(2400) R(4800)
R(9600) R(19200) R(38400) R(57600) R(115200) R(230400) R(460800) R(500000)
R(576000) R(921600) R(1000000) R(1152000) R(1500000)
R(2000000) if (!y) {
fprintf(stderr, "podana hitrost pošiljanja ni podprta\nglej Line speed v "
"tcgetattr(3) za možne hitrosti\n");
r = 9;
goto r;
}
if (tcsetattr(u, TCSANOW, &o) == -1) {
perror("tcsetattr");
r = 10;
goto r;
}
uint32_t raw_offset = 0; // ob RAW ukazu pošlješ 128 bajtov datoteke in ++
uint32_t picture_number __attribute__((unused)) =
0; // ob PICTURE ukazu pošlješ picture_number sliko, ++ // TODO
struct pollfd p = {.fd = u, .events = POLLIN | POLLPRI | POLLOUT | POLLERR};
while (1) {
if (otrok_umrl) {
otrok_umrl = 0;
c = -1;
fprintf(stderr,
"otrok umrl. pid %d status %d utime %ld stime %ld CLOCKS_PER_SEC "
"%ld\n",
otrok_pid, otrok_status, otrok_utime, otrok_stime,
CLOCKS_PER_SEC);
#ifdef INFORM_DOWNLINK
char buf[128];
buf[0] = 'c';
sprintf(buf + 1, "%dS%dU%ldS%ldC%ld\n", otrok_pid, otrok_status,
otrok_utime, otrok_stime, CLOCKS_PER_SEC);
write(u, buf, strlen(buf));
#endif
}
if (samomor)
goto r;
if (poll(&p, 1, -1) == -1) {
if (errno != EINTR) {
perror("poll");
r = 11;
goto r;
} else
continue;
}
char i = '\0';
if (read(u, &i, 1) == -1) {
if (errno != EAGAIN && errno != EINTR) {
perror("read");
r = 12;
goto r;
} else
continue;
}
if (!i) // instead of doing this it's better to check if return value of
// read is zero
continue;
if (i == BEG) {
picture_number = 0;
raw_offset = 0;
char *const a[] = {argv[3], "--camera", "0", "--rawfull", "-o",
IMG, "-n", "--flush", NULL};
if (c >= 1) {
fprintf(stderr,
"houston requested BEGIN, process was already created\n");
if (b == x) { \
cfsetospeed(&o, B##x); \
cfsetispeed(&o, B##x); \
y++; \
}
R(50)
R(75)
R(110)
R(134)
R(150)
R(200)
R(300)
R(600)
R(1200) R(1800) R(2400) R(4800) R(9600) R(19200) R(38400) R(57600) R(115200)
R(230400) R(460800) R(500000) R(576000) R(921600) R(1000000) R(1152000)
R(1500000) R(2000000) if (!y) {
fprintf(stderr, "podana hitrost pošiljanja ni podprta\nglej Line speed v "
"tcgetattr(3) za možne hitrosti\n");
r = 9;
goto r;
}
if (tcsetattr(u, TCSANOW, &o) == -1) {
perror("tcsetattr");
r = 10;
goto r;
}
uint32_t raw_offset = 0; // ob RAW ukazu pošlješ 128 bajtov datoteke in ++
uint32_t picture_number __attribute__((unused)) =
0; // ob PICTURE ukazu pošlješ picture_number sliko, ++ // TODO
struct pollfd p = {.fd = u, .events = POLLIN | POLLPRI | POLLOUT | POLLERR};
while (1) {
if (otrok_umrl) {
otrok_umrl = 0;
c = -1;
fprintf(stderr,
"otrok umrl. pid %d status %d utime %ld stime %ld CLOCKS_PER_SEC "
"%ld\n",
otrok_pid, otrok_status, otrok_utime, otrok_stime,
CLOCKS_PER_SEC);
#ifdef INFORM_DOWNLINK
char buf[64];
buf[0] = BEG;
sprintf(buf + 1, "A%d\n", c);
write(u, buf, strlen(buf)); // this may eagain
char buf[128];
buf[0] = 'c';
sprintf(buf + 1, "%dS%dU%ldS%ldC%ld\n", otrok_pid, otrok_status,
otrok_utime, otrok_stime, CLOCKS_PER_SEC);
write(u, buf, strlen(buf));
#endif
continue;
}
pid_t c = fork();
if (c == -1) {
perror("fork");
}
if (samomor)
goto r;
if (poll(&p, 1, -1) == -1) {
if (errno != EINTR) {
perror("poll");
r = 11;
goto r;
} else
continue;
}
char i = '\0';
if (read(u, &i, 1) == -1) {
if (errno != EAGAIN && errno != EINTR) {
perror("read");
r = 12;
goto r;
} else
continue;
}
if (!i) // instead of doing this it's better to check if return value of
// read is zero
continue;
if (i == BEG) {
picture_number = 0;
raw_offset = 0;
char *const a[] = {argv[3], "--camera", "0", "--rawfull", "-o",
IMG, "-n", "--flush", NULL};
if (c >= 1) {
fprintf(stderr,
"houston requested BEGIN, process was already created\n");
#ifdef INFORM_DOWNLINK
char buf[64];
buf[0] = BEG;
sprintf(buf + 1, "F%d\n", errno);
write(u, buf, strlen(buf));
char buf[64];
buf[0] = BEG;
sprintf(buf + 1, "A%d\n", c);
write(u, buf, strlen(buf)); // this may eagain
#endif
continue;
}
if (!c) {
if (execv(argv[3], a) == -1) {
perror("execv");
return 13; // starš prejme SIGCHLD
}
}
continue;
}
pid_t c = fork();
if (c == -1) {
perror("fork");
#ifdef INFORM_DOWNLINK
char buf[64];
buf[0] = BEG;
sprintf(buf + 1, "O%d\n", c);
write(u, buf, strlen(buf));
char buf[64];
buf[0] = BEG;
sprintf(buf + 1, "F%d\n", errno);
write(u, buf, strlen(buf));
#endif
continue;
}
if (i == END) {
raw_offset = 0;
if (c >= 1)
if (kill(c, SIGUSR1) == -1) {
perror("kill");
continue;
}
if (!c) {
if (execv(argv[3], a) == -1) {
perror("execv");
return 13; // starš prejme SIGCHLD
}
}
#ifdef INFORM_DOWNLINK
char buf[64];
buf[0] = END;
sprintf(buf + 1, "K%d\n", errno);
write(u, buf, strlen(buf));
char buf[64];
buf[0] = BEG;
sprintf(buf + 1, "O%d\n", c);
write(u, buf, strlen(buf));
#endif
continue;
}
continue;
}
if (i == END) {
raw_offset = 0;
if (c >= 1)
if (kill(c, SIGUSR1) == -1) {
perror("kill");
#ifdef INFORM_DOWNLINK
char buf[64];
buf[0] = END;
sprintf(buf + 1, "O\n");
write(u, buf, strlen(buf));
char buf[64];
buf[0] = END;
sprintf(buf + 1, "K%d\n", errno);
write(u, buf, strlen(buf));
#endif
continue;
}
if (i == PIC) {
char buf[132];
int fd;
if ((fd = open(IMG, O_RDONLY | O_NONBLOCK)) == -1) {
perror("open");
continue;
}
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "O%d\n", errno);
write(u, buf, strlen(buf));
char buf[64];
buf[0] = END;
sprintf(buf + 1, "O\n");
write(u, buf, strlen(buf));
#endif
continue;
}
struct stat statbuf;
if (fstat(fd, &statbuf) == -1) {
perror("fstat");
continue;
}
if (i == PIC) {
char buf[132];
int fd;
if ((fd = open(IMG, O_RDONLY | O_NONBLOCK)) == -1) {
perror("open");
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "F%d\n", errno);
write(u, buf, strlen(buf));
sprintf(buf + 4, "O%d\n", errno);
write(u, buf, strlen(buf));
#endif
goto e;
}
if (statbuf.st_size < raw_offset * 128 + 128) {
if (!raw_offset) {
continue;
}
struct stat statbuf;
if (fstat(fd, &statbuf) == -1) {
perror("fstat");
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "S%ld\n", statbuf.st_size);
write(u, buf, strlen(buf));
sprintf(buf + 4, "F%d\n", errno);
write(u, buf, strlen(buf));
#endif
goto e;
}
raw_offset = 0;
}
if (seg==5) {
if (lseek(fd, vrsI*ORIGS, SEEK_SET) == -1) {
perror("lseek");
goto e;
}
if (statbuf.st_size < raw_offset * 128 + 128) {
raw_offset = 0;
}
if (seg == 5) {
if (lseek(fd, vrsI * ORIGY / picY * ORIGS, SEEK_SET) == -1) {
perror("lseek");
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "L%d\n", errno);
sprintf(buf + 4, "L%d\n", errno);
#endif
goto e;
}
y = read(fd, vrs, ORIGX);
if (y == -1) {
goto e;
}
y = read(fd, vrs, ORIGX);
if (y == -1) {
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "R%d\n", errno);
sprintf(buf + 4, "R%d\n", errno);
#endif
goto e;
}
if (y != 128) {
goto e;
}
if (y != ORIGX) {
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "W%d\n", errno);
sprintf(buf + 4, "W%d\n", errno);
#endif
goto e;
}
}
*((uint32_t *)(buf)) = htonl(raw_offset);
y = write(u, buf, 128 + 4);
if (y == -1)
perror("write");
if (y != 128 + 4)
fprintf(stderr, "RAW wrote only %d\n", y);
e:
if (close(fd) == -1)
perror("close");
continue;
}
if (i == RAW) {
char buf[132];
memset(buf, 0xFF, 4);
int fd;
if ((fd = open(IMG, O_RDONLY | O_NONBLOCK)) == -1) {
perror("open");
goto e;
}
}
*((uint32_t *)(buf)) = htonl(raw_offset);
for (int x = 0; x < 128; x++)
buf[x] = vrs[4 + (128 * seg + x * ORIGX / picX) * 5 / 4];
y = write(u, buf, 128 + 4);
if (y == -1)
perror("write");
if (y != 128 + 4)
fprintf(stderr, "RAW wrote only %d\n", y);
e:
if (close(fd) == -1)
perror("close");
continue;
}
if (i == RAW) {
char buf[132];
memset(buf, 0xFF, 4);
int fd;
if ((fd = open(IMG, O_RDONLY | O_NONBLOCK)) == -1) {
perror("open");
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "O%d\n", errno);
write(u, buf, strlen(buf));
sprintf(buf + 4, "O%d\n", errno);
write(u, buf, strlen(buf));
#endif
continue;
}
struct stat statbuf;
if (fstat(fd, &statbuf) == -1) {
perror("fstat");
continue;
}
struct stat statbuf;
if (fstat(fd, &statbuf) == -1) {
perror("fstat");
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "F%d\n", errno);
write(u, buf, strlen(buf));
sprintf(buf + 4, "F%d\n", errno);
write(u, buf, strlen(buf));
#endif
goto e;
}
if (statbuf.st_size < raw_offset * 128 + 128) {
if (!raw_offset) {
goto e2;
}
if (statbuf.st_size < raw_offset * 128 + 128) {
if (!raw_offset) {
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "S%ld\n", statbuf.st_size);
write(u, buf, strlen(buf));
sprintf(buf + 4, "S%ld\n", statbuf.st_size);
write(u, buf, strlen(buf));
#endif
goto e;
}
raw_offset = 0;
}
if (lseek(fd, raw_offset++ * 128, SEEK_SET) == -1) {
perror("lseek");
goto e2;
}
raw_offset = 0;
}
if (lseek(fd, raw_offset++ * 128, SEEK_SET) == -1) {
perror("lseek");
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "L%d\n", errno);
write(u, buf, strlen(buf));
sprintf(buf + 4, "L%d\n", errno);
write(u, buf, strlen(buf));
#endif
goto e;
}
y = read(fd, buf + 4, 128);
if (y == -1) {
goto e;
}
y = read(fd, buf + 4, 128);
if (y == -1) {
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "R%d\n", errno);
write(u, buf, strlen(buf));
sprintf(buf + 4, "R%d\n", errno);
write(u, buf, strlen(buf));
#endif
goto e;
}
if (y != 128) {
goto e2;
}
if (y != 128) {
#ifdef INFORM_DOWNLINK
sprintf(buf + 4, "W%d\n", errno);
write(u, buf, strlen(buf));
sprintf(buf + 4, "W%d\n", errno);
write(u, buf, strlen(buf));
#endif
goto e;
}
*((uint32_t *)(buf)) = htonl(raw_offset);
y = write(u, buf, 128 + 4);
if (y == -1)
perror("write");
if (y != 128 + 4)
fprintf(stderr, "RAW wrote only %d\n", y);
e:
if (close(fd) == -1)
perror("close");
continue;
}
fprintf(stderr, "houston je poslal neveljaven ukaz %d\n", i);
continue;
}