ud.c 9.3 KB
Newer Older
Adrian's avatar
Adrian committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

#define S0(x) (x ? x : "")

//#define NO_INFORM_DOWNLINK
#ifndef NO_INFORM_DOWNLINK
#define INFORM_DOWNLINK 1
#endif

Adrian's avatar
:|    
Adrian committed
21
22
23
24
25
#define picX 640
#define picY 480
#define SIZE_PIC (picX * picY)

#define ORIGX 3280
Adrian's avatar
Adrian committed
26
#define ORIGY 2464
Adrian's avatar
:|    
Adrian committed
27
#define ORIGS 4128
Adrian's avatar
Adrian committed
28
29
30
31
32
33
34
35
36
37
38
39
40

int otrok_umrl = 0;
pid_t otrok_pid = 0;
int otrok_status = 0;
clock_t otrok_utime = 0;
clock_t otrok_stime = 0;
int samomor = 0;

uint64_t vrsI = 0;
uint64_t seg = 5;
char vrs[];

void handle_child(int s __attribute__((unused)), siginfo_t *i,
Adrian's avatar
Adrian committed
41
42
43
44
45
46
                  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;
Adrian's avatar
Adrian committed
47
48
49
50
51
}

void handle_me(int s __attribute__((unused))) { samomor++; }

int main(int argc, char **argv) {
Adrian's avatar
Adrian committed
52
53
54
55
56
57
58
  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;
  }
Adrian's avatar
Adrian committed
59
60
61
62
63
64
65

#define IMG argv[4]
#define BEG 'B'
#define END 'E'
#define RAW 'R'
#define PIC 'P'

Adrian's avatar
Adrian committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  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;
Adrian's avatar
Adrian committed
108
#define R(x)                                                                   \
Adrian's avatar
Adrian committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  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);
Adrian's avatar
Adrian committed
148
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
149
150
151
152
153
      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));
Adrian's avatar
Adrian committed
154
#endif
Adrian's avatar
Adrian committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    }
    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");
Adrian's avatar
Adrian committed
186
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
187
188
189
190
        char buf[64];
        buf[0] = BEG;
        sprintf(buf + 1, "A%d\n", c);
        write(u, buf, strlen(buf)); // this may eagain
Adrian's avatar
Adrian committed
191
#endif
Adrian's avatar
Adrian committed
192
193
194
195
196
        continue;
      }
      pid_t c = fork();
      if (c == -1) {
        perror("fork");
Adrian's avatar
Adrian committed
197
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
198
199
200
201
        char buf[64];
        buf[0] = BEG;
        sprintf(buf + 1, "F%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
202
#endif
Adrian's avatar
Adrian committed
203
204
205
206
207
208
209
210
        continue;
      }
      if (!c) {
        if (execv(argv[3], a) == -1) {
          perror("execv");
          return 13; // starš prejme SIGCHLD
        }
      }
Adrian's avatar
Adrian committed
211
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
212
213
214
215
      char buf[64];
      buf[0] = BEG;
      sprintf(buf + 1, "O%d\n", c);
      write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
216
#endif
Adrian's avatar
Adrian committed
217
218
219
220
221
222
223
      continue;
    }
    if (i == END) {
      raw_offset = 0;
      if (c >= 1)
        if (kill(c, SIGUSR1) == -1) {
          perror("kill");
Adrian's avatar
Adrian committed
224
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
225
226
227
228
          char buf[64];
          buf[0] = END;
          sprintf(buf + 1, "K%d\n", errno);
          write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
229
#endif
Adrian's avatar
Adrian committed
230
231
          continue;
        }
Adrian's avatar
Adrian committed
232
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
233
234
235
236
      char buf[64];
      buf[0] = END;
      sprintf(buf + 1, "O\n");
      write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
237
#endif
Adrian's avatar
Adrian committed
238
239
240
241
242
243
244
      continue;
    }
    if (i == PIC) {
      char buf[132];
      int fd;
      if ((fd = open(IMG, O_RDONLY | O_NONBLOCK)) == -1) {
        perror("open");
Adrian's avatar
Adrian committed
245
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
246
247
        sprintf(buf + 4, "O%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
248
#endif
Adrian's avatar
Adrian committed
249
250
251
252
253
        continue;
      }
      struct stat statbuf;
      if (fstat(fd, &statbuf) == -1) {
        perror("fstat");
Adrian's avatar
Adrian committed
254
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
255
256
        sprintf(buf + 4, "F%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
257
#endif
Adrian's avatar
Adrian committed
258
259
260
261
262
263
264
265
        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");
Adrian's avatar
Adrian committed
266
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
267
          sprintf(buf + 4, "L%d\n", errno);
Adrian's avatar
Adrian committed
268
#endif
Adrian's avatar
Adrian committed
269
270
271
272
          goto e;
        }
        y = read(fd, vrs, ORIGX);
        if (y == -1) {
Adrian's avatar
Adrian committed
273
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
274
          sprintf(buf + 4, "R%d\n", errno);
Adrian's avatar
Adrian committed
275
#endif
Adrian's avatar
Adrian committed
276
277
278
          goto e;
        }
        if (y != ORIGX) {
Adrian's avatar
Adrian committed
279
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
280
          sprintf(buf + 4, "W%d\n", errno);
Adrian's avatar
Adrian committed
281
#endif
Adrian's avatar
Adrian committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
          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");
Adrian's avatar
Adrian committed
304
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
305
306
        sprintf(buf + 4, "O%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
307
#endif
Adrian's avatar
Adrian committed
308
309
310
311
312
        continue;
      }
      struct stat statbuf;
      if (fstat(fd, &statbuf) == -1) {
        perror("fstat");
Adrian's avatar
Adrian committed
313
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
314
315
        sprintf(buf + 4, "F%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
316
#endif
Adrian's avatar
Adrian committed
317
318
319
320
        goto e2;
      }
      if (statbuf.st_size < raw_offset * 128 + 128) {
        if (!raw_offset) {
Adrian's avatar
Adrian committed
321
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
322
323
          sprintf(buf + 4, "S%ld\n", statbuf.st_size);
          write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
324
#endif
Adrian's avatar
Adrian committed
325
326
327
328
329
330
          goto e2;
        }
        raw_offset = 0;
      }
      if (lseek(fd, raw_offset++ * 128, SEEK_SET) == -1) {
        perror("lseek");
Adrian's avatar
Adrian committed
331
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
332
333
        sprintf(buf + 4, "L%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
334
#endif
Adrian's avatar
Adrian committed
335
336
337
338
        goto e;
      }
      y = read(fd, buf + 4, 128);
      if (y == -1) {
Adrian's avatar
Adrian committed
339
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
340
341
        sprintf(buf + 4, "R%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
342
#endif
Adrian's avatar
Adrian committed
343
344
345
        goto e2;
      }
      if (y != 128) {
Adrian's avatar
Adrian committed
346
#ifdef INFORM_DOWNLINK
Adrian's avatar
Adrian committed
347
348
        sprintf(buf + 4, "W%d\n", errno);
        write(u, buf, strlen(buf));
Adrian's avatar
Adrian committed
349
#endif
Adrian's avatar
Adrian committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
        goto e2;
      }
      *((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);
    e2:
      if (close(fd) == -1)
        perror("close");
      continue;
    }
    fprintf(stderr, "houston je poslal neveljaven ukaz %d\n", i);
    continue;
  }
Adrian's avatar
Adrian committed
366
r:
Adrian's avatar
Adrian committed
367
368
369
370
371
372
  if (close(u) == -1)
    perror("close");
  if (c >= 1)
    if (kill(c, SIGHUP) == -1)
      perror("kill");
  return r;
Adrian's avatar
Adrian committed
373
}