FreeBSD/usr.sbin/bluetooth/hccontrol/link_policy.c
/* Send Swith Role to the unit */
Send Swith Role to the unit
Send Swith Role to the unit
static int
hci_switch_role(int s, int argc, char **argv)
{
	int			 n0;
	char			 b[512];
	ng_hci_switch_role_cp	 cp;
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b; 
	switch (argc) {
	case 2:
		if (!bt_aton(argv[0], &cp.bdaddr)) {
			struct hostent	*he = NULL;
			if ((he = bt_gethostbyname(argv[0])) == NULL)
				return (USAGE);
			memcpy(&cp.bdaddr, he->h_addr, sizeof(cp.bdaddr));
		}
		if (sscanf(argv[1], "%d", &n0) != 1)
			return (USAGE);
		cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
		break;
	default:
		return (USAGE);
	}
	n0 = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_POLICY,
			NG_HCI_OCF_SWITCH_ROLE),
			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
		return (ERROR);
	if (*b != 0x00)
		return (FAILED);
again:
	n0 = sizeof(b);
	if (hci_recv(s, b, &n0) == ERROR)
		return (ERROR);
	if (n0 < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}
	if (e->event == NG_HCI_EVENT_ROLE_CHANGE) {
		ng_hci_role_change_ep	*ep = (ng_hci_role_change_ep *)(e + 1);
		if (ep->status != 0x00) {
			fprintf(stdout, "Status: %s [%#02x]\n", 
				hci_status2str(ep->status), ep->status);
			return (FAILED);
		}
		fprintf(stdout, "BD_ADDR: %s\n", hci_bdaddr2str(&ep->bdaddr));
		fprintf(stdout, "Role: %s [%#x]\n",
			(ep->role == NG_HCI_ROLE_MASTER)? "Master" : "Slave",
			ep->role);
	} else
		goto again;
	return (OK);
}
static int hci_switch_role ( int s , int argc , char * * argv ) { int n0 ; char b [ 512 ] ; ng_hci_switch_role_cp cp ; ng_hci_event_pkt_t * e = ( ng_hci_event_pkt_t * ) b ; switch ( argc ) { case 2 : if ( ! bt_aton ( argv [ 0 ] , & cp . bdaddr ) ) { struct hostent * he = NULL ; if ( ( he = bt_gethostbyname ( argv [ 0 ] ) ) == NULL ) return ( USAGE ) ; memcpy ( & cp . bdaddr , he -> h_addr , sizeof ( cp . bdaddr ) ) ; } if ( sscanf ( argv [ 1 ] , " " , & n0 ) != 1 ) return ( USAGE ) ; cp . role = n0 ? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER ; break ; default : return ( USAGE ) ; } n0 = sizeof ( b ) ; if ( hci_request ( s , NG_HCI_OPCODE ( NG_HCI_OGF_LINK_POLICY , NG_HCI_OCF_SWITCH_ROLE ) , ( char const * ) & cp , sizeof ( cp ) , b , & n0 ) == ERROR ) return ( ERROR ) ; if ( * b != 0x00 ) return ( FAILED ) ; again : n0 = sizeof ( b ) ; if ( hci_recv ( s , b , & n0 ) == ERROR ) return ( ERROR ) ; if ( n0 < sizeof ( * e ) ) { errno = EIO ; return ( ERROR ) ; } if ( e -> event == NG_HCI_EVENT_ROLE_CHANGE ) { ng_hci_role_change_ep * ep = ( ng_hci_role_change_ep * ) ( e + 1 ) ; if ( ep -> status != 0x00 ) { fprintf ( stdout , " \n " , hci_status2str ( ep -> status ) , ep -> status ) ; return ( FAILED ) ; } fprintf ( stdout , " \n " , hci_bdaddr2str ( & ep -> bdaddr ) ) ; fprintf ( stdout , " \n " , ( ep -> role == NG_HCI_ROLE_MASTER ) ? " " : " " , ep -> role ) ; } else goto again ; return ( OK ) ; }
Send Swith Role to the unit
Send Swith Role to the unit
/* parse command parameters */ /* bdaddr */ /* role */ /* send request and expect status response */ /* wait for event */