This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
71 lines (60 loc) · 1.61 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Main file of the serial port project.
// NOTE: This file must not be changed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "application_layer.h"
#define N_TRIES 3
#define TIMEOUT 4
// Arguments:
// $1: /dev/ttySxx
// $2: baud rate
// $3: tx | rx
// $4: filename
int main(int argc, char *argv[])
{
if (argc < 5) {
printf("Usage: %s /dev/ttySxx baudrate tx|rx filename\n", argv[0]);
exit(1);
}
const char *serialPort = argv[1];
const int baudrate = atoi(argv[2]);
const char *role = argv[3];
const char *filename = argv[4];
// Validate baud rate
switch (baudrate) {
case 1200:
case 1800:
case 2400:
case 4800:
case 9600:
case 19200:
case 38400:
case 57600:
case 115200:
break;
default:
printf("Unsupported baud rate (must be one of 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200)\n");
exit(2);
}
// Validate role
if (strcmp("tx", role) != 0 && strcmp("rx", role) != 0) {
printf("ERROR: Role must be \"tx\" or \"rx\"\n");
exit(3);
}
printf("Starting link-layer protocol application\n"
" - Serial port: %s\n"
" - Role: %s\n"
" - Baudrate: %d\n"
" - Number of tries: %d\n"
" - Timeout: %d\n"
" - Filename: %s\n",
serialPort,
role,
baudrate,
N_TRIES,
TIMEOUT,
filename);
applicationLayer(serialPort, role, baudrate, N_TRIES, TIMEOUT, filename);
return 0;
}