3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-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
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
37 #include <linux/sockios.h>
39 #include <bluetooth/bluetooth.h>
40 #include <bluetooth/l2cap.h>
41 #include <bluetooth/bnep.h>
42 #include <bluetooth/uuid.h>
52 const char *name; /* Friendly name */
53 const char *uuid128; /* UUID 128 */
54 uint16_t id; /* Service class identifier */
56 { "panu", PANU_UUID, BNEP_SVC_PANU },
57 { "gn", GN_UUID, BNEP_SVC_GN },
58 { "nap", NAP_UUID, BNEP_SVC_NAP },
62 uint16_t bnep_service_id(const char *svc)
67 /* Friendly service name */
68 for (i = 0; __svc[i].name; i++)
69 if (!strcasecmp(svc, __svc[i].name)) {
74 for (i = 0; __svc[i].uuid128; i++)
75 if (!strcasecmp(svc, __svc[i].uuid128)) {
79 /* Try convert to HEX */
80 id = strtol(svc, NULL, 16);
81 if ((id < BNEP_SVC_PANU) || (id > BNEP_SVC_GN))
87 const char *bnep_uuid(uint16_t id)
91 for (i = 0; __svc[i].uuid128; i++)
92 if (__svc[i].id == id)
93 return __svc[i].uuid128;
97 const char *bnep_name(uint16_t id)
101 for (i = 0; __svc[i].name; i++)
102 if (__svc[i].id == id)
103 return __svc[i].name;
109 ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
113 error("Failed to open control socket: %s (%d)",
114 strerror(-err), -err);
121 int bnep_cleanup(void)
127 int bnep_kill_connection(bdaddr_t *dst)
129 struct bnep_conndel_req req;
131 memset(&req, 0, sizeof(req));
132 baswap((bdaddr_t *)&req.dst, dst);
134 if (ioctl(ctl, BNEPCONNDEL, &req)) {
136 error("Failed to kill connection: %s (%d)",
137 strerror(-err), -err);
143 int bnep_kill_all_connections(void)
145 struct bnep_connlist_req req;
146 struct bnep_conninfo ci[7];
150 memset(&req, 0, sizeof(req));
153 if (ioctl(ctl, BNEPGETCONNLIST, &req)) {
155 error("Failed to get connection list: %s (%d)",
156 strerror(-err), -err);
160 for (i = 0; i < req.cnum; i++) {
161 struct bnep_conndel_req del;
163 memset(&del, 0, sizeof(del));
164 memcpy(del.dst, ci[i].dst, ETH_ALEN);
166 ioctl(ctl, BNEPCONNDEL, &del);
171 int bnep_connadd(int sk, uint16_t role, char *dev)
173 struct bnep_connadd_req req;
175 memset(&req, 0, sizeof(req));
176 strncpy(req.device, dev, 16);
177 req.device[15] = '\0';
180 if (ioctl(ctl, BNEPCONNADD, &req) < 0) {
182 error("Failed to add device %s: %s(%d)",
183 dev, strerror(-err), -err);
187 strncpy(dev, req.device, 16);
191 int bnep_if_up(const char *devname)
196 sk = socket(AF_INET, SOCK_DGRAM, 0);
198 memset(&ifr, 0, sizeof(ifr));
199 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
201 ifr.ifr_flags |= IFF_UP;
202 ifr.ifr_flags |= IFF_MULTICAST;
204 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr);
209 error("Could not bring up %s", devname);
216 int bnep_if_down(const char *devname)
221 sk = socket(AF_INET, SOCK_DGRAM, 0);
223 memset(&ifr, 0, sizeof(ifr));
224 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
226 ifr.ifr_flags &= ~IFF_UP;
228 /* Bring down the interface */
229 err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr);
234 error("Could not bring down %s", devname);
241 int bnep_add_to_bridge(const char *devname, const char *bridge)
247 if (!devname || !bridge)
250 ifindex = if_nametoindex(devname);
252 sk = socket(AF_INET, SOCK_STREAM, 0);
256 #ifdef __TIZEN_PATCH__
257 err = ioctl(sk, SIOCBRADDBR, bridge);
260 info("bridge create err: %d", err);
266 memset(&ifr, 0, sizeof(ifr));
267 strncpy(ifr.ifr_name, bridge, IFNAMSIZ - 1);
268 ifr.ifr_ifindex = ifindex;
270 err = ioctl(sk, SIOCBRADDIF, &ifr);
277 info("bridge %s: interface %s added", bridge, devname);