2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <sys/types.h>
33 #include <bluetooth/bluetooth.h>
34 #include <bluetooth/rfcomm.h>
37 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
39 struct sockaddr_rc addr;
42 sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
44 fprintf(stderr, "Can't create socket: %s (%d)\n",
45 strerror(errno), errno);
49 memset(&addr, 0, sizeof(addr));
50 addr.rc_family = AF_BLUETOOTH;
51 bacpy(&addr.rc_bdaddr, BDADDR_ANY);
53 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
54 fprintf(stderr, "Can't bind socket: %s (%d)\n", strerror(errno), errno);
61 opt |= RFCOMM_LM_MASTER;
62 opt |= RFCOMM_LM_AUTH;
64 opt |= RFCOMM_LM_ENCRYPT;
65 opt |= RFCOMM_LM_SECURE;
67 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
68 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)", strerror(errno), errno);
73 memset(&addr, 0, sizeof(addr));
74 addr.rc_family = AF_BLUETOOTH;
75 bacpy(&addr.rc_bdaddr, bdaddr);
76 addr.rc_channel = channel;
78 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
79 fprintf(stderr, "Can't connect: %s (%d)\n", strerror(errno), errno);
87 static void mbaswap(bdaddr_t *dst, const bdaddr_t *src)
89 register unsigned char *d = (unsigned char *) dst;
90 register const unsigned char *s = (const unsigned char *) src;
93 for (i = 0; i < 6; i++)
97 static int mbachk(const char *str)
102 if (strlen(str) != 17)
106 if (!isxdigit(*str++))
108 if (!isxdigit(*str++))
119 int str2ba(const char *str, bdaddr_t *ba)
124 if (mbachk(str) < 0) {
125 memset(ba, 0, sizeof(*ba));
129 for (i = 0; i < 6; i++, str += 3)
130 b.b[i] = strtol(str, NULL, 16);