link_policy.c revision 128079
159243Sobrien/*
259243Sobrien * link_policy.c
359243Sobrien *
459243Sobrien * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
559243Sobrien * All rights reserved.
659243Sobrien *
759243Sobrien * Redistribution and use in source and binary forms, with or without
859243Sobrien * modification, are permitted provided that the following conditions
959243Sobrien * are met:
1059243Sobrien * 1. Redistributions of source code must retain the above copyright
1159243Sobrien *    notice, this list of conditions and the following disclaimer.
1259243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer in the
1459243Sobrien *    documentation and/or other materials provided with the distribution.
1559243Sobrien *
1659243Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1759243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1859243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1959243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2059243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2159243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2259243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2359243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2459243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2559243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2659243Sobrien * SUCH DAMAGE.
2759243Sobrien *
2859243Sobrien * $Id: link_policy.c,v 1.3 2003/08/18 19:19:54 max Exp $
2959243Sobrien * $FreeBSD: head/usr.sbin/bluetooth/hccontrol/link_policy.c 128079 2004-04-09 23:58:53Z emax $
3059243Sobrien */
3159243Sobrien
3259243Sobrien#include <bluetooth.h>
3359243Sobrien#include <errno.h>
3459243Sobrien#include <stdio.h>
3559243Sobrien#include <string.h>
3659243Sobrien#include "hccontrol.h"
3759243Sobrien
3859243Sobrien/* Send Role Discovery to the unit */
3959243Sobrienstatic int
4059243Sobrienhci_role_discovery(int s, int argc, char **argv)
4159243Sobrien{
4259243Sobrien	ng_hci_role_discovery_cp	cp;
4359243Sobrien	ng_hci_role_discovery_rp	rp;
4459243Sobrien	int				n;
4559243Sobrien
4659243Sobrien	/* parse command parameters */
4759243Sobrien	switch (argc) {
4859243Sobrien	case 1:
4959243Sobrien		/* connection handle */
5059243Sobrien		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
5159243Sobrien			return (USAGE);
5259243Sobrien
5359243Sobrien		cp.con_handle = (uint16_t) (n & 0x0fff);
5459243Sobrien		cp.con_handle = htole16(cp.con_handle);
5559243Sobrien		break;
5659243Sobrien
5759243Sobrien	default:
5859243Sobrien		return (USAGE);
5959243Sobrien	}
6059243Sobrien
6159243Sobrien	/* send request */
6259243Sobrien	n = sizeof(rp);
6359243Sobrien	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
6459243Sobrien			NG_HCI_OCF_ROLE_DISCOVERY),
6559243Sobrien			(char const *) &cp, sizeof(cp),
6659243Sobrien			(char *) &rp, &n) == ERROR)
6759243Sobrien		return (ERROR);
6859243Sobrien
6959243Sobrien	if (rp.status != 0x00) {
7059243Sobrien		fprintf(stdout, "Status: %s [%#02x]\n",
7159243Sobrien			hci_status2str(rp.status), rp.status);
7259243Sobrien		return (FAILED);
7359243Sobrien	}
7459243Sobrien
7559243Sobrien	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
7659243Sobrien	fprintf(stdout, "Role: %s [%#x]\n",
7759243Sobrien		(rp.role == NG_HCI_ROLE_MASTER)? "Master" : "Slave", rp.role);
7859243Sobrien
7959243Sobrien	return (OK);
8059243Sobrien} /* hci_role_discovery */
8159243Sobrien
8259243Sobrien/* Send Swith Role to the unit */
8359243Sobrienstatic int
8459243Sobrienhci_switch_role(int s, int argc, char **argv)
8559243Sobrien{
8659243Sobrien	int			 n0;
8759243Sobrien	char			 b[512];
8859243Sobrien	ng_hci_switch_role_cp	 cp;
8959243Sobrien	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;
9059243Sobrien
9159243Sobrien	/* parse command parameters */
9259243Sobrien	switch (argc) {
9359243Sobrien	case 2:
9459243Sobrien		/* bdaddr */
9559243Sobrien		if (!bt_aton(argv[0], &cp.bdaddr)) {
9659243Sobrien			struct hostent	*he = NULL;
9759243Sobrien
9859243Sobrien			if ((he = bt_gethostbyname(argv[0])) == NULL)
9959243Sobrien				return (USAGE);
10059243Sobrien
10159243Sobrien			memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr));
10259243Sobrien		}
10359243Sobrien
10459243Sobrien		/* role */
10559243Sobrien		if (sscanf(argv[1], "%d", &n0) != 1)
10659243Sobrien			return (USAGE);
10759243Sobrien
10859243Sobrien		cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
10959243Sobrien		break;
11059243Sobrien
11159243Sobrien	default:
11259243Sobrien		return (USAGE);
11359243Sobrien	}
11459243Sobrien
11559243Sobrien	/* send request and expect status response */
11659243Sobrien	n0 = sizeof(b);
11759243Sobrien	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
11859243Sobrien			NG_HCI_OCF_SWITCH_ROLE),
11959243Sobrien			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
12059243Sobrien		return (ERROR);
12159243Sobrien
12259243Sobrien	if (*b != 0x00)
12359243Sobrien		return (FAILED);
12459243Sobrien
12559243Sobrien	/* wait for event */
12659243Sobrienagain:
12759243Sobrien	n0 = sizeof(b);
12859243Sobrien	if (hci_recv(s, b, &n0) == ERROR)
12959243Sobrien		return (ERROR);
13059243Sobrien	if (n0 < sizeof(*e)) {
13159243Sobrien		errno = EIO;
13259243Sobrien		return (ERROR);
13359243Sobrien	}
13459243Sobrien
13559243Sobrien	if (e->event == NG_HCI_EVENT_ROLE_CHANGE) {
13659243Sobrien		ng_hci_role_change_ep	*ep = (ng_hci_role_change_ep *)(e + 1);
13759243Sobrien
13859243Sobrien		if (ep->status != 0x00) {
13959243Sobrien			fprintf(stdout, "Status: %s [%#02x]\n",
14059243Sobrien				hci_status2str(ep->status), ep->status);
14159243Sobrien			return (FAILED);
14259243Sobrien		}
14359243Sobrien
14459243Sobrien		fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr));
14559243Sobrien		fprintf(stdout, "Role: %s [%#x]\n",
14659243Sobrien			(ep->role == NG_HCI_ROLE_MASTER)? "Master" : "Slave",
14759243Sobrien			ep->role);
14859243Sobrien	} else
14959243Sobrien		goto again;
15059243Sobrien
15159243Sobrien	return (OK);
15259243Sobrien} /* hci_switch_role */
15359243Sobrien
15459243Sobrien/* Send Read_Link_Policy_Settings command to the unit */
15559243Sobrienstatic int
15659243Sobrienhci_read_link_policy_settings(int s, int argc, char **argv)
15759243Sobrien{
15859243Sobrien	ng_hci_read_link_policy_settings_cp	cp;
15959243Sobrien	ng_hci_read_link_policy_settings_rp	rp;
16059243Sobrien	int					n;
16159243Sobrien
16259243Sobrien	/* parse command parameters */
16359243Sobrien	switch (argc) {
16459243Sobrien	case 1:
16559243Sobrien		/* connection handle */
16659243Sobrien		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
16759243Sobrien			return (USAGE);
16859243Sobrien
16959243Sobrien		cp.con_handle = (uint16_t) (n & 0x0fff);
17059243Sobrien		cp.con_handle = htole16(cp.con_handle);
17159243Sobrien		break;
17259243Sobrien
17359243Sobrien	default:
17459243Sobrien		return (USAGE);
17559243Sobrien	}
17659243Sobrien
17759243Sobrien	/* send request */
17859243Sobrien	n = sizeof(rp);
17959243Sobrien	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
18059243Sobrien			NG_HCI_OCF_READ_LINK_POLICY_SETTINGS),
18159243Sobrien			(char const *) &cp, sizeof(cp),
18259243Sobrien			(char *) &rp, &n) == ERROR)
18359243Sobrien		return (ERROR);
18459243Sobrien
18559243Sobrien	if (rp.status != 0x00) {
18659243Sobrien		fprintf(stdout, "Status: %s [%#02x]\n",
18759243Sobrien			hci_status2str(rp.status), rp.status);
18859243Sobrien		return (FAILED);
18959243Sobrien	}
19059243Sobrien
19159243Sobrien	fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));
19259243Sobrien	fprintf(stdout, "Link policy settings: %#x\n", le16toh(rp.settings));
19359243Sobrien
19459243Sobrien	return (OK);
19559243Sobrien} /* hci_read_link_policy_settings */
19659243Sobrien
19759243Sobrien/* Send Write_Link_Policy_Settings command to the unit */
19859243Sobrienstatic int
19959243Sobrienhci_write_link_policy_settings(int s, int argc, char **argv)
20059243Sobrien{
20159243Sobrien	ng_hci_write_link_policy_settings_cp	cp;
20259243Sobrien	ng_hci_write_link_policy_settings_rp	rp;
20359243Sobrien	int					n;
20459243Sobrien
20559243Sobrien	/* parse command parameters */
20659243Sobrien	switch (argc) {
20759243Sobrien	case 2:
20859243Sobrien		/* connection handle */
20959243Sobrien		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
21059243Sobrien			return (USAGE);
21159243Sobrien
21259243Sobrien		cp.con_handle = (uint16_t) (n & 0x0fff);
21359243Sobrien		cp.con_handle = htole16(cp.con_handle);
21459243Sobrien
21559243Sobrien		/* link policy settings */
21659243Sobrien		if (sscanf(argv[1], "%x", &n) != 1)
21759243Sobrien			return (USAGE);
21859243Sobrien
21959243Sobrien		cp.settings = (uint16_t) (n & 0x0ffff);
22059243Sobrien		cp.settings = htole16(cp.settings);
22159243Sobrien		break;
22259243Sobrien
22359243Sobrien	default:
22459243Sobrien		return (USAGE);
22559243Sobrien	}
22659243Sobrien
22759243Sobrien	/* send request */
22859243Sobrien	n = sizeof(rp);
22959243Sobrien	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
23059243Sobrien			NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS),
23159243Sobrien			(char const *) &cp, sizeof(cp),
23259243Sobrien			(char *) &rp, &n) == ERROR)
23359243Sobrien		return (ERROR);
23459243Sobrien
23559243Sobrien	if (rp.status != 0x00) {
23659243Sobrien		fprintf(stdout, "Status: %s [%#02x]\n",
23759243Sobrien			hci_status2str(rp.status), rp.status);
23859243Sobrien		return (FAILED);
23959243Sobrien	}
24059243Sobrien
24159243Sobrien	return (OK);
24259243Sobrien} /* hci_write_link_policy_settings */
24359243Sobrien
24459243Sobrienstruct hci_command	link_policy_commands[] = {
24559243Sobrien{
24659243Sobrien"role_discovery <connection_handle>",
24759243Sobrien"\nThe Role_Discovery command is used for a Bluetooth device to determine\n" \
24859243Sobrien"which role the device is performing for a particular Connection Handle.\n" \
24959243Sobrien"The connection handle must be a connection handle for an ACL connection.\n\n" \
25059243Sobrien"\t<connection_handle> - dddd; connection handle",
25159243Sobrien&hci_role_discovery
25259243Sobrien},
25359243Sobrien{
25459243Sobrien"switch_role <bdaddr> <role>",
25559243Sobrien"\nThe Switch_Role command is used for a Bluetooth device to switch the\n" \
25659243Sobrien"current role the device is performing for a particular connection with\n" \
25759243Sobrien"another specified Bluetooth device. The BD_ADDR command parameter indicates\n"\
25859243Sobrien"for which connection the role switch is to be performed. The Role indicates\n"\
25959243Sobrien"the requested new role that the local device performs. Note: the BD_ADDR\n" \
26059243Sobrien"command parameter must specify a Bluetooth device for which a connection\n"
26159243Sobrien"already exists.\n\n" \
26259243Sobrien"\t<bdaddr> - xx:xx:xx:xx:xx:xx; device bdaddr\n" \
26359243Sobrien"\t<role>   - dd; role; 0 - Master, 1 - Slave",
26459243Sobrien&hci_switch_role
26559243Sobrien},
26659243Sobrien{
26759243Sobrien"read_link_policy_settings <connection_handle>",
26859243Sobrien"\nThis command will read the Link Policy setting for the specified connection\n"\
26959243Sobrien"handle. The link policy settings parameter determines the behavior of the\n" \
27059243Sobrien"local Link Manager when it receives a request from a remote device or it\n" \
27159243Sobrien"determines itself to change the master-slave role or to enter the hold,\n" \
27259243Sobrien"sniff, or park mode. The local Link Manager will automatically accept or\n" \
27359243Sobrien"reject such a request from the remote device, and may even autonomously\n" \
27459243Sobrien"request itself, depending on the value of the link policy settings parameter\n"\
27559243Sobrien"for the corresponding connection handle. The connection handle must be a\n" \
27659243Sobrien"connection handle for an ACL connection.\n\n" \
27759243Sobrien"\t<connection_handle> - dddd; connection handle",
27859243Sobrien&hci_read_link_policy_settings
27959243Sobrien},
28059243Sobrien{
28159243Sobrien"write_link_policy_settings <connection_handle> <settings>",
28259243Sobrien"\nThis command will write the Link Policy setting for the specified connection\n"\
28359243Sobrien"handle. The link policy settings parameter determines the behavior of the\n" \
28459243Sobrien"local Link Manager when it receives a request from a remote device or it\n" \
28559243Sobrien"determines itself to change the master-slave role or to enter the hold,\n" \
28659243Sobrien"sniff, or park mode. The local Link Manager will automatically accept or\n" \
28759243Sobrien"reject such a request from the remote device, and may even autonomously\n" \
28859243Sobrien"request itself, depending on the value of the link policy settings parameter\n"\
28959243Sobrien"for the corresponding connection handle. The connection handle must be a\n" \
29059243Sobrien"connection handle for an ACL connection. Multiple Link Manager policies may\n"\
29159243Sobrien"be specified for the link policy settings parameter by performing a bitwise\n"\
29259243Sobrien"OR operation of the different activity types.\n\n" \
29359243Sobrien"\t<connection_handle> - dddd; connection handle\n" \
29459243Sobrien"\t<settings>          - xxxx; settings\n" \
29559243Sobrien"\t\t0x0000 - Disable All LM Modes (Default)\n" \
29659243Sobrien"\t\t0x0001 - Enable Master Slave Switch\n" \
29759243Sobrien"\t\t0x0002 - Enable Hold Mode\n" \
29859243Sobrien"\t\t0x0004 - Enable Sniff Mode\n" \
29959243Sobrien"\t\t0x0008 - Enable Park Mode\n",
30059243Sobrien&hci_write_link_policy_settings
30159243Sobrien},
30259243Sobrien{
30359243SobrienNULL,
30459243Sobrien}};
30559243Sobrien
30659243Sobrien