-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathptsrecv.c
184 lines (142 loc) · 3.55 KB
/
ptsrecv.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
/* ########################################################################
tty0tty - linux null modem emulator
########################################################################
Copyright (c) : 2013 Luis Claudio Gambôa Lopes and Maximiliano Pin [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : [email protected]
######################################################################## */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <errno.h>
#include <termio.h>
#include <time.h>
static char buffer[1024];
int
ptym_open(char *pts_name, char *pts_name_s , int pts_namesz)
{
char *ptr;
int fdm;
strncpy(pts_name, "/dev/ptmx", pts_namesz);
pts_name[pts_namesz - 1] = '\0';
fdm = posix_openpt(O_RDWR | O_NONBLOCK);
if (fdm < 0)
return(-1);
if (grantpt(fdm) < 0)
{
close(fdm);
return(-2);
}
if (unlockpt(fdm) < 0)
{
close(fdm);
return(-3);
}
if ((ptr = ptsname(fdm)) == NULL)
{
close(fdm);
return(-4);
}
strncpy(pts_name_s, ptr, pts_namesz);
pts_name[pts_namesz - 1] = '\0';
return(fdm);
}
int
conf_ser(int serialDev)
{
int rc;
struct termios params;
// Get terminal atributes
rc = tcgetattr(serialDev, ¶ms);
// Modify terminal attributes
cfmakeraw(¶ms);
rc = cfsetispeed(¶ms, B9600);
rc = cfsetospeed(¶ms, B9600);
// CREAD - Enable port to read data
// CLOCAL - Ignore modem control lines
params.c_cflag |= (B9600 |CS8 | CLOCAL | CREAD);
// Make Read Blocking
//fcntl(serialDev, F_SETFL, 0);
// Set serial attributes
rc = tcsetattr(serialDev, TCSANOW, ¶ms);
// Flush serial device of both non-transmitted
// output data and non-read input data....
tcflush(serialDev, TCIOFLUSH);
return EXIT_SUCCESS;
}
void
dump(char *p, ssize_t s)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("%09ld: ", ts.tv_nsec);
for (; s > 0; s--)
printf("%02x ", *p++);
putchar('\n');
}
void
copydata(int fdfrom)
{
ssize_t br;
br = read(fdfrom, buffer, 1024);
if (br < 0)
{
if (errno == EAGAIN || errno == EIO)
{
br = 0;
}
else
{
perror("read");
exit(1);
}
}
if (br > 0)
{
dump(buffer, br);
}
}
int main(int argc, char* argv[])
{
char master1[1024];
char slave1[1024];
int fd1, slave;
fd_set rfds;
int retval;
fd1=ptym_open(master1,slave1,1024);
printf("(%s)\n",slave1);
if ((slave = open(slave1, 0)) < 0)
perror("open slave");
conf_ser(fd1);
while(1)
{
FD_ZERO(&rfds);
FD_SET(fd1, &rfds);
retval = select(fd1 + 1, &rfds, NULL, NULL, NULL);
if (retval == -1)
{
perror("select");
return 1;
}
if (FD_ISSET(fd1, &rfds))
{
copydata(fd1);
}
}
close(fd1);
close(slave);
return EXIT_SUCCESS;
}