forked from pedrorfernandes/serial_transfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
167 lines (141 loc) · 5.86 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
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
108
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "main.h"
LinkLayer linkLayerConf;
ApplicationLayer applicationLayerConf;
int main(int argc, char** argv)
{
if ( (argc < 3) ||
((strcmp("/dev/ttyS0", argv[1])!=0) &&
(strcmp("/dev/ttyS1", argv[1])!=0) ) ||
((strcmp("transmitter",argv[2])!=0) &&
(strcmp("receiver", argv[2])!=0) ) ) {
printf("Usage:\tnserial SerialPort transmitter|receiver filename [-b baudrate] [-s size of data per packet] [-r retry number] [-t timeout seconds]\n");
printf("Example:\tnserial /dev/ttyS0 transmitter ./pinguim.gif -b B115200 -s 1024 -r 5 -t 20\n");
exit(1);
}
int port = 0, role = 0;
if (strcmp(argv[1], COM1_PORT) == 0 )
port = COM1;
else if (strcmp(argv[1], COM2_PORT) == 0)
port = COM2;
if ( strcmp("transmitter", argv[2]) == 0 )
role = TRANSMITTER;
if ( strcmp("receiver", argv[2]) == 0 )
role = RECEIVER;
// default settings
applicationLayerConf.maxPacketSize = DEFAULT_DATA_SIZE;
applicationLayerConf.maxDataFieldSize = applicationLayerConf.maxPacketSize - BASE_DATA_PACKET_SIZE;
linkLayerConf.maxInformationSize = applicationLayerConf.maxPacketSize * 2 + 4;
linkLayerConf.frameSize = linkLayerConf.maxInformationSize + BASE_FRAME_SIZE;
linkLayerConf.frameBCC2Index = FBCC2(linkLayerConf.maxInformationSize);
linkLayerConf.frameTrailerIndex = FTRAILERFLAG(linkLayerConf.maxInformationSize);
linkLayerConf.numTransmissions = 1 + DEFAULT_RETRY; //first transmission + retries
linkLayerConf.sendTimeout = DEFAULT_TIMEOUT; //seconds until timeout
linkLayerConf.receiveTimeout = RECEIVE_INFO_TIMEOUT;
linkLayerConf.baudRate = B9600;
linkLayerConf.testMode = 0;
char fileNameArgument[FILENAME_LEN] = "";
if ( argc > 3 ) {
// the transmitter will send this file
if ( role == TRANSMITTER && fileExists(argv[3]) )
strcpy(fileNameArgument, argv[3]);
else if ( role == TRANSMITTER && fileExists(DEFAULT_FILENAME) )
strcpy(fileNameArgument, DEFAULT_FILENAME);
else if ( role == TRANSMITTER ) {
printf("Filename not specified / doesn't exist and default file '%s' doesn't exist! Terminating.\n", DEFAULT_FILENAME);
return -1;
}
// the receiver will write to this filename (the third argument mustn't be a flag)
if ( role == RECEIVER && !fileExists(argv[3]) && argv[3][0] != '-' )
strcpy(fileNameArgument, argv[3]);
}
int c; opterr = 0;
int result;
while ((c = getopt (argc, argv, "b:s:r:t:m:")) != -1)
switch (c)
{
case 'b':
result = setBaudrate(optarg);
if ( result == 0 )
printf("Baud rate set to %s\n", optarg);
else
printf("Error setting baud rate! Reverted to default %s\n", DEFAULT_BAUDRATE_STRING);
break;
case 's':
result = setDataSize(optarg);
if ( result == 0)
printf("Maximum information field size set to %s\n", optarg);
else
printf("Error setting maximum information field size! Reverting to default %i\n", DEFAULT_DATA_SIZE);
break;
case 'r':
result = setRetry(optarg);
if ( result == 0)
printf("Number of retries set to %s\n", optarg);
else
printf("Error setting number of retries! Reverting to default %i\n", DEFAULT_RETRY);
break;
case 't':
result = setTimeout(optarg);
if ( result == 0)
printf("Timeout set to %s seconds\n", optarg);
else
printf("Error setting timeout seconds! Reverting to default %i\n", DEFAULT_TIMEOUT);
break;
case 'm':
result = setTestMode(optarg);
if( result == 0)
if(strcmp(optarg, "1") == 0)
printf("Test mode is ON\n");
else
printf("Test mode is OFF\n");
else
printf("Error setting test mode, reverting to OFF\n");
break;
case '?':
break;
default:
return -1;
}
int fd;
fd = llopen(port, role);
if(role == TRANSMITTER && fd != -1) {
size_t fileSize;
printf("The file '%s' will be sent.\n", fileNameArgument);
unsigned char* file = readFile(fileNameArgument, &fileSize);
if(sendFile(file, fileSize, fileNameArgument) == 0)
printf("Sent file.\n");
if(closeLink() != 0)
printf("Problem restoring serial port definitions.\n");
printf("File sent!");
}
else if(role == RECEIVER && fd != -1) {
size_t size;
char* fileNameReceived = NULL;
unsigned char* file = receiveFile(&size, &fileNameReceived);
if(file != NULL) {
if(waitCloseLink() != 0) {
llclose(applicationLayerConf.fileDescriptor);
}
if ( strcmp(fileNameArgument, "") != 0 ) {
// use the file name from the commandline
strcpy(fileNameReceived, fileNameArgument);
}
if( writeFile(file, fileNameReceived, size) == 0 )
printf("Success! File '%s' should be created!\n", fileNameReceived);
else {
printf("Failed to create file\n");
}
}
else {
printf("Failed to correctly receive file\n");
confirmCloseLink();
return 0;
}
}
return 0;
}
int fileExists(char * filename)
{
struct stat buffer;
return (stat (filename, &buffer) == 0);
}