-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.c
198 lines (177 loc) · 5.48 KB
/
serial.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#define STR_MAX_SIZE 50
// Comunication Status
const char AVAILABLE = 'a', SENDER = 's', RECIEVER = 'r';
const char man_file[] = "/tmp/man_file";
// Global Variables
char command[STR_MAX_SIZE];
// Status
char status = 'a';
void man_maker(){
char man_word[STR_MAX_SIZE] = "man ";
char end_word[STR_MAX_SIZE] = "\n";
strcat(man_word, command);
strcat(man_word, end_word);
strcpy(command, man_word);
printf("\n%s\n", command);
int z=0;
for(z=0; z<strlen(man_word);z++)
{
man_word[z] = 0;
}
}
void do_command() {
FILE *command_resul, *man_resul;
command_resul = popen(command, "r");
man_resul = fopen(man_file, "w");
do fprintf(man_resul, "%c", fgetc(command_resul)); while(!feof(command_resul));
pclose(command_resul);
fclose(man_resul);
}
int main(int argc, char **argv) {
int fd, kbd, aux, remover;
int i = 0;
char letra, *portname;
char check_status = 0;
char msg[80];
struct termios mytty, kbdios;
fd_set descritores; /* Set of i/o handles */
struct timeval timeout;
/* Open serial port */
portname="/dev/ttyS0"; /* Use /dev/ttyS0 by default */
/* or get the name of the serial port device from the command line */
if(argc>1 && *argv[1]=='/') portname=argv[1];
/* Open the serial port */
fd=open(portname, O_RDWR | O_NDELAY);
if(fd==-1) {
sprintf(msg, "\nUnable to open \"%s\"", portname);
perror(msg);
return -1;
}
/* Set attributes of the serial interface to raw mode */
if(tcgetattr(fd, &mytty)==-1) {
perror(NULL);
close(fd);
return -1;
}
mytty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
mytty.c_oflag &= ~OPOST;
mytty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
mytty.c_cflag &= ~(CSIZE|PARENB);
mytty.c_cflag |= CS8; /* 8 bit data bytes */
/* Set Baud Rate to 9600 baud */
cfsetispeed(&mytty, B9600);
cfsetospeed(&mytty, B9600);
if(tcsetattr(fd, TCSANOW, &mytty)==-1) {
perror(NULL);
close(fd);
return -1;
}
/* Get keyboard handle (assuming that stdin is the keyboard and video) */
kbd=fileno(stdin);
/* Set keyboard to raw mode */
tcgetattr(kbd, &kbdios);
memcpy(&mytty, &kbdios, sizeof(struct termios));
mytty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tcsetattr(kbd, TCSANOW, &mytty);
/* These calls make read to return imedately even when there is
no data in the input stream */
fcntl(fd, F_SETFL, FNDELAY);
fcntl(kbd, F_SETFL, FNDELAY);
/* Now we may use the serial interface and keyboard for sending
just one character at a time */
printf("\nSerial tty terminal on port \"%s\"\n", portname);
printf("Type ctrl-X (meta-X) to exit.\n");
do {
/* Wait for any activitiy in the serial port (fd) or keyboard (kbd) */
FD_ZERO(&descritores); /* Inicializa a lista de handles */
FD_SET(fd, &descritores);
FD_SET(kbd, &descritores);
timeout.tv_sec=5;
timeout.tv_usec=0;
select(FD_SETSIZE, &descritores, NULL, NULL, &timeout);
// Custom Stuff
// Verify first input
while(read(fd, &letra, 1) >= 0) { /* Get a char from serial port "fd"*/
if(status == AVAILABLE) { //change to 2
if(letra == RECIEVER) {
status = letra;
letra = ' ';
printf("Receiver\n");
}
} else
if(status == RECIEVER) {
//Receiver logic
command[i] = letra;
if(command[i] == '\n') {
do_command();
aux = open(man_file, O_RDONLY);
while(read(aux, &letra, 1)){
// write(fd, &letra, 1);
printf("%c", letra);
status = AVAILABLE;
remover = remove(man_file);
int g=0;
for(g=0; g<strlen(command);g++)
{
command[g] = ' ';
}
}
close(aux);
i = 0;
}
i++;
}
}
while(read(kbd, &letra, 1) >= 0) { /* Get a char from "kbd" (keyboard)*/
if(status == AVAILABLE) {
if(letra == '\n') {
status = SENDER;
printf("Sender\n");
write(fd, &RECIEVER, 1); /* send it to the serial port "fd"*/
printf("Press enter to send a command\n");
}
}
else {
if(letra == '\n') {
//write to serial port
man_maker();
int count = 0;
while(command[count] != '\n') {
write(fd, &command[count], 1);
count++;
}
write(fd, "\n", 1);
i = 0;
status = AVAILABLE;
int z=0;
for(z=0; z<strlen(command);z++)
{
command[z] = ' ';
}
} else {
command[i] = letra;
write(kbd, &letra, 1);
i++;
}
}
fflush(stdin);
}
} while(letra!='X'-64); /* if the key was Ctrl-X exit*/
close(fd);
/* Set console back to normal */
fcntl(kbd, 0, FNDELAY);
tcsetattr(kbd, TCSANOW, &kbdios);
putchar('\n');
return 0;
}
//Command
//socat -d -d pty,raw,echo=0 pty,raw,echo=0