-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrfcomm-server.c
More file actions
311 lines (284 loc) · 7.46 KB
/
Copy pathrfcomm-server.c
File metadata and controls
311 lines (284 loc) · 7.46 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <stdlib.h> // for exit(EXIT_FAILURE);
#include <unistd.h>
#include <errno.h>
#include <sys/select.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <sys/ioctl.h>
#include "rfcomm-server.h"
#define STDIN 0 // for selecting stdin input in select()
int dynamic_bind_rc(int sock, struct sockaddr_rc *sockaddr)
{
int err;
int port = 0;
for( port = 1; port <= 31; port++ ) {
sockaddr->rc_channel = port;
err = bind(sock, (struct sockaddr *)sockaddr, sizeof(*(sockaddr)));
if( ! err || errno == EINVAL ) break;
}
if( port == 31 ) {
err = -1;
errno = EINVAL;
}
return err;
}
int rfcomm_server(uint8_t *port)
{
struct sockaddr_rc loc_addr = { 0 };
int s;
// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (s < 0)
{
perror("socket() failed");
exit(EXIT_FAILURE);
}
int on = 1;
// Allow socket descriptor to be reuseable.
int rc_opt = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
if (rc_opt < 0)
{
perror("setsockopt() failed");
close(s);
exit(EXIT_FAILURE);
}
// Set socket to be nonblocking. All of the sockets for
// the incoming connections will also be nonblocking since
// they will inherit that state from the listening socket.
int rc_ioctl = ioctl(s, FIONBIO, (char *)&on);
if (rc_ioctl < 0)
{
perror("ioctl() failed");
close(s);
exit(EXIT_FAILURE);
}
// bind socket to first available port of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;;
loc_addr.rc_bdaddr = *BDADDR_ANY;
int rc_bind = dynamic_bind_rc(s, &loc_addr);
if (rc_bind < 0)
{
perror("bind() failed");
close(s);
exit(EXIT_FAILURE);
}
*port = loc_addr.rc_channel;
// Print binded information.
char bdaddr_str[32] = { 0 };
ba2str( &loc_addr.rc_bdaddr, bdaddr_str );
printf("Binded to address \"%s\" and channel %d.\n", bdaddr_str, loc_addr.rc_channel);
// put socket into listening mode accepting only one connection.
int rc = listen(s, 1);
if (rc < 0)
{
perror("listen() failed");
close(s);
exit(EXIT_FAILURE);
}
printf("Listening.\n");
return s;
}
int process_connection(int *max_sd, fd_set *master_set, int s)
{
int end_server = 0;
int client = 0; //, bytes_read;
do
{
struct sockaddr_rc rem_addr = { 0 };
socklen_t opt = sizeof(rem_addr);
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
if (client < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" accept() failed");
end_server = 1;
}
break;
}
printf(" New incoming connection - %d\n", client);
char buf[32] = {0};
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
memset(buf, 0, sizeof(buf));
FD_SET(client, master_set);
if (client > *max_sd)
*max_sd = client;
}
while (client != -1);
return end_server;
}
int read_connection(int i)
{
int close_conn = 0;
// Receive all incoming data on this socket
// before we loop back and call select again.
//do
//{
// Receive data on this connection until the
// recv fails with EWOULDBLOCK. If any other
// failure occurs, we will close the
//connection.
char buf[1024] = {0};
int rc_recv = recv(i, buf, sizeof(buf), 0);
if (rc_recv < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" recv() failed");
close_conn = 1;
}
//break;
}
// Check to see if the connection has been
// closed by the client
if (rc_recv == 0)
{
printf(" Connection closed\n");
close_conn = 1;
//break;
}
// Data was received
int len = rc_recv;
printf(" %d bytes received \"%s\"\n", len, buf);
//} while (1);
return close_conn;
}
// If the close_conn flag was turned on, we need
// to clean up this active connection. This
// clean up process includes removing the
// descriptor from the master set and
// determining the new maximum descriptor value
// based on the bits that are still turned on in
// the master set.
void close_connection(int i, int *max_sd, fd_set *master_set)
{
close(i);
FD_CLR(i, master_set);
if (i == *max_sd)
{
while (FD_ISSET(*max_sd, master_set) == 0)
*max_sd -= 1;
}
}
int loop_descriptors(int rc_select, int *max_sd, fd_set *master_set, fd_set *working_set, int s)
{
int end_server = 0;
int desc_ready = rc_select;
int i;
for (i=0; i <= *max_sd && desc_ready > 0; ++i)
{
printf("Checkin FD_ISSET %d socket.\n", i);
// Check to see if this descriptor is ready.
if (FD_ISSET(i, working_set))
{
desc_ready -= 1;
// Check to see if this is the listening socket.
if (i == s)
{
printf(" Listening socket is readable\n");
end_server = process_connection(max_sd, master_set, s);
}
// Check stdin input.
else if(i == STDIN)
{
char buf[1024] = {0};
// Read data from stdin using fgets.
fgets(buf, sizeof(buf), stdin);
// Remove trailing newline character from the input buffer if needed.
int len = strlen(buf) - 1;
if (buf[len] == '\n') buf[len] = '\0';
printf("'%s' was read from stdin.\n", buf);
// Echo the data back to the client
int j;
for (j=0; j <= *max_sd; ++j)
{
// Check to see if this descriptor is ready.
if (FD_ISSET(j, master_set))
{
// Send in all nonlistenig sockets.
if ( j > 0 && j != s)
{
int rc_send = send(j, buf, len, 0);
if (rc_send < 0)
{
perror(" send() failed");
//close_conn = 1;
break;
}
printf("Sended \"%s\" in %d socket\n", buf, j);
}
}
}
}
// This is not the listening socket, therefore an
// existing connection must be readable
else
{
printf(" Descriptor %d is readable\n", i);
int close_conn = read_connection(i);
if (close_conn)
{
close_connection(i, max_sd, master_set);
}
} /* End of existing connection is readable */
} /* End of if (FD_ISSET(i, &working_set)) */
} /* End of loop through selectable descriptors */
return end_server;
}
void accept_connection(int s)
{
int max_sd;
struct timeval timeout;
fd_set master_set, working_set;
// Initialize the master fd_set
FD_ZERO(&master_set);
max_sd = s;
FD_SET(s, &master_set);
// Read from STDIN also.
FD_SET(STDIN, &master_set);
int end_server = 0;
// Loop waiting for incoming connects or for incoming data
// on any of the connected sockets.
do
{
// Copy the master fd_set over to the working fd_set.
memcpy(&working_set, &master_set, sizeof(master_set));
// Initialize the timeval struct to 3 minutes. If no
// activity after 3 minutes this program will end.
timeout.tv_sec = 3 * 60;
timeout.tv_usec = 0;
// Call select() and wait 5 minutes for it to complete.
printf("Waiting on select() %ld sec...\n", timeout.tv_sec);
int rc_select = select(max_sd + 1, &working_set, NULL, NULL, &timeout);
// Check to see if the select call failed.
if (rc_select < 0)
{
perror(" select() failed");
break;
}
// One or more descriptors are readable. Need to
// determine which ones they are.
else if (rc_select > 0)
{
end_server = loop_descriptors(rc_select, &max_sd, &master_set, &working_set, s);
}
// Else if rc_select == 0 then the 5 minute time out expired.
else
{
printf(" select() timed out. End program.\n");
break;
}
}
while (end_server == 0);
// Clean up all of the sockets that are open.
int i;
for (i=0; i <= max_sd; ++i)
{
if (FD_ISSET(i, &master_set))
close(i);
}
}