3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37
#include <sys/ioctl.h>
38
#include <sys/socket.h>
40
#include <bluetooth/bluetooth.h>
41
#include <bluetooth/rfcomm.h>
44
static volatile sig_atomic_t __io_canceled;
46
static inline void io_init(void)
51
static inline void io_cancel(void)
56
/* Signal functions */
57
static void sig_hup(int sig)
62
static void sig_term(int sig)
64
syslog(LOG_INFO, "Closing RFCOMM channel");
68
/* Read exactly len bytes (Signal safe)*/
69
static inline int read_n(int fd, char *buf, int len)
71
register int t = 0, w;
73
while (!__io_canceled && len > 0) {
74
if ((w = read(fd, buf, len)) < 0) {
75
if (errno == EINTR || errno == EAGAIN)
89
/* Write exactly len bytes (Signal safe)*/
90
static inline int write_n(int fd, char *buf, int len)
92
register int t = 0, w;
94
while (!__io_canceled && len > 0) {
95
if ((w = write(fd, buf, len)) < 0) {
96
if (errno == EINTR || errno == EAGAIN)
110
/* Create the RFCOMM connection */
111
static int create_connection(bdaddr_t *bdaddr, uint8_t channel)
113
struct sockaddr_rc remote_addr, local_addr;
116
if ((fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
119
memset(&local_addr, 0, sizeof(local_addr));
120
local_addr.rc_family = AF_BLUETOOTH;
121
bacpy(&local_addr.rc_bdaddr, BDADDR_ANY);
122
if ((err = bind(fd, (struct sockaddr *)&local_addr, sizeof(local_addr))) < 0) {
127
memset(&remote_addr, 0, sizeof(remote_addr));
128
remote_addr.rc_family = AF_BLUETOOTH;
129
bacpy(&remote_addr.rc_bdaddr, bdaddr);
130
remote_addr.rc_channel = channel;
131
if ((err = connect(fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr))) < 0) {
136
syslog(LOG_INFO, "RFCOMM channel %d connected", channel);
141
/* Process the data from socket and pseudo tty */
142
static int process_data(int fd)
149
p[0].events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
152
p[1].events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
156
while (!__io_canceled) {
160
err = poll(p, 2, -1);
167
if (p[0].revents & (POLLERR | POLLHUP | POLLNVAL))
169
r = read(0, buf, sizeof(buf));
171
if (errno != EINTR && errno != EAGAIN) {
177
err = write_n(fd, buf, r);
183
if (p[1].revents & (POLLERR | POLLHUP | POLLNVAL))
185
r = read(fd, buf, sizeof(buf));
187
if (errno != EINTR && errno != EAGAIN) {
193
err = write_n(1, buf, r);
202
static void usage(void)
204
printf("Usage:\tppporc <bdaddr> [channel]\n");
207
int main(int argc, char** argv)
215
/* Parse command line options */
216
while ((opt = getopt(argc, argv, "h")) != EOF) {
229
str2ba(argv[0], &bdaddr);
233
str2ba(argv[0], &bdaddr);
234
channel = atoi(argv[1]);
241
/* Initialize syslog */
242
openlog("ppporc", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
243
syslog(LOG_INFO, "PPP over RFCOMM");
245
/* Initialize signals */
246
memset(&sa, 0, sizeof(sa));
247
sa.sa_flags = SA_NOCLDSTOP;
248
sa.sa_handler = SIG_IGN;
249
sigaction(SIGCHLD, &sa, NULL);
250
sigaction(SIGPIPE, &sa, NULL);
252
sa.sa_handler = sig_term;
253
sigaction(SIGTERM, &sa, NULL);
254
sigaction(SIGINT, &sa, NULL);
256
sa.sa_handler = sig_hup;
257
sigaction(SIGHUP, &sa, NULL);
259
syslog(LOG_INFO, "Connecting to %s", argv[0]);
261
if ((fd = create_connection(&bdaddr, channel)) < 0) {
262
syslog(LOG_ERR, "Can't connect to remote device (%s)", strerror(errno));
266
err = process_data(fd);