]> git.kernelconcepts.de Git - metawatch.git/blob - bt_helper.c
Break out Bluetooth functions in own module
[metawatch.git] / bt_helper.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <termios.h>
29 #include <ctype.h>
30
31 #include <errno.h>
32
33 #include <bluetooth/bluetooth.h>
34 #include <bluetooth/rfcomm.h>
35
36
37 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
38 {
39         struct sockaddr_rc addr;
40         int sk, opt;
41
42         sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
43         if (sk < 0) {
44                 fprintf(stderr, "Can't create socket: %s (%d)\n",
45                         strerror(errno), errno);
46                 return -1;
47         }
48
49         memset(&addr, 0, sizeof(addr));
50         addr.rc_family = AF_BLUETOOTH;
51         bacpy(&addr.rc_bdaddr, BDADDR_ANY);
52
53         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
54         fprintf(stderr, "Can't bind socket: %s (%d)\n", strerror(errno), errno);
55                 close(sk);
56                 return -1;
57         }
58
59         /* Set link mode */
60         opt = 0;
61         opt |= RFCOMM_LM_MASTER;
62         opt |= RFCOMM_LM_AUTH;
63 /*
64         opt |= RFCOMM_LM_ENCRYPT;
65         opt |= RFCOMM_LM_SECURE;
66 */
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);
69                 close(sk);
70                 return -1;
71         }
72
73         memset(&addr, 0, sizeof(addr));
74         addr.rc_family = AF_BLUETOOTH;
75         bacpy(&addr.rc_bdaddr, bdaddr);
76         addr.rc_channel = channel;
77
78         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
79                 fprintf(stderr, "Can't connect: %s (%d)\n", strerror(errno), errno);
80                 close(sk);
81                 return -1;
82         }
83
84         return sk;
85 }
86
87 static void mbaswap(bdaddr_t *dst, const bdaddr_t *src)
88 {
89         register unsigned char *d = (unsigned char *) dst;
90         register const unsigned char *s = (const unsigned char *) src;
91         register int i;
92
93         for (i = 0; i < 6; i++)
94                 d[i] = s[5-i];
95 }
96
97 static int mbachk(const char *str)
98 {
99         if (!str)
100                 return -1;
101
102         if (strlen(str) != 17)
103                 return -1;
104
105         while (*str) {
106                 if (!isxdigit(*str++))
107                         return -1;
108                 if (!isxdigit(*str++))
109                         return -1;
110                 if (*str == 0)
111                         break;
112                 if (*str++ != ':')
113                         return -1;
114         }
115
116         return 0;
117 }
118
119 int str2ba(const char *str, bdaddr_t *ba)
120 {
121         bdaddr_t b;
122         int i;
123
124         if (mbachk(str) < 0) {
125                 memset(ba, 0, sizeof(*ba));
126                 return -1;
127         }
128
129         for (i = 0; i < 6; i++, str += 3)
130                 b.b[i] = strtol(str, NULL, 16);
131
132         mbaswap(ba, &b);
133
134         return 0;
135 }
136