Example #1
0
int le_connect_result(s)
{
	char buffer[512];
	ng_hci_event_pkt_t *e;
	ng_hci_le_ep *lep;
	ng_hci_le_connection_complete_ep *cep;
	int n;
	char addrstring[50];
	int err;

	e = (ng_hci_event_pkt_t *)buffer;
	lep = (ng_hci_le_ep *)(((char *)e)+(sizeof(*e)));
	cep = (ng_hci_le_connection_complete_ep *)(((char *)lep)+(sizeof(*lep)));
	n = sizeof(buffer);
	if((err = hci_recv(s, buffer, &n))==ERROR){
		printf("RECV Error\n");
		return 0;
	}
	if(n < sizeof(*e)){
		errno = EMSGSIZE;
		return 0;
	}
	if(e->type != NG_HCI_EVENT_PKT){
		printf("Event%d\n", e->type);
		errno = EIO;
		return 0;
	}
	printf("%d\n", lep->subevent_code);
	if(lep->subevent_code != NG_HCI_LEEV_CON_COMPL){
		printf("SubEvent%d\n", lep->subevent_code);
		errno = EIO;
		return 0;
	}
	printf("Connection Event:Status%d, handle%d, role%d, address_type:%d\n",
	       cep->status, cep->handle, cep->role, cep->address_type);
	bt_ntoa(&cep->address, addrstring);
	printf("%s %d %d %d %d\n", addrstring, cep->interval, cep->latency,
	       cep->supervision_timeout, cep->master_clock_accracy);
	if(cep->status != 0){
		printf("REQUEST ERROR %d\n", cep->status);
		return 0;
	}

	return cep->handle;
}
Example #2
0
/* 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; 

	/* parse command parameters */
	switch (argc) {
	case 2:
		/* bdaddr */
		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));
		}

		/* role */
		if (sscanf(argv[1], "%d", &n0) != 1)
			return (USAGE);

		cp.role = n0? NG_HCI_ROLE_SLAVE : NG_HCI_ROLE_MASTER;
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	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);

	/* wait for event */
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);
} /* hci_switch_role */
Example #3
0
/* Send Read_Clock_Offset command to the unit */
static int
hci_read_clock_offset(int s, int argc, char **argv)
{
	int				 n;
	char				 b[512];
	ng_hci_read_clock_offset_cp	 cp;
	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b; 

	/* parse command parameters */
	switch (argc) {
	case 1:
		/* connecton handle */
		if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff)
			return (USAGE);

		cp.con_handle = (n & 0x0fff);
		cp.con_handle = htole16(cp.con_handle);
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_READ_CLOCK_OFFSET),
			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
again:
	n = sizeof(b);
	if (hci_recv(s, b, &n) == ERROR)
		return (ERROR);

	if (n < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}

	if (e->event == NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL) {
		ng_hci_read_clock_offset_compl_ep	*ep = 
				(ng_hci_read_clock_offset_compl_ep *)(e + 1);

		if (ep->status != 0x00) {
			fprintf(stdout, "Status: %s [%#02x]\n", 
				hci_status2str(ep->status), ep->status);
			return (FAILED);
		}

		fprintf(stdout, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "Clock offset: %#04x\n",
			le16toh(ep->clock_offset));
	} else
		goto again;

	return (OK);
} /* hci_read_clock_offset */
Example #4
0
/* Send Read_Remote_Version_Information command to the unit */
static int
hci_read_remote_version_information(int s, int argc, char **argv)
{
	int				 n;
	char				 b[512];
	ng_hci_read_remote_ver_info_cp	 cp;
	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b; 

	/* parse command parameters */
	switch (argc) {
	case 1:
		/* connecton handle */
		if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff)
			return (USAGE);

		cp.con_handle = (n & 0x0fff);
		cp.con_handle = htole16(cp.con_handle);
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_READ_REMOTE_VER_INFO), 
			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
again:
	n = sizeof(b);
	if (hci_recv(s, b, &n) == ERROR)
		return (ERROR);

	if (n < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}

	if (e->event == NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL) {
		ng_hci_read_remote_ver_info_compl_ep	*ep = 
				(ng_hci_read_remote_ver_info_compl_ep *)(e + 1);

		if (ep->status != 0x00) {
			fprintf(stdout, "Status: %s [%#02x]\n", 
				hci_status2str(ep->status), ep->status);
			return (FAILED);
		}

		ep->manufacturer = le16toh(ep->manufacturer);

		fprintf(stdout, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "LMP version: %s [%#02x]\n",
			hci_lmpver2str(ep->lmp_version), ep->lmp_version);
		fprintf(stdout, "LMP sub-version: %#04x\n",
			le16toh(ep->lmp_subversion));
		fprintf(stdout, "Manufacturer: %s [%#04x]\n",
			hci_manufacturer2str(ep->manufacturer),
			ep->manufacturer);
	} else
		goto again;

	return (OK);
} /* hci_read_remote_version_information */
Example #5
0
/* Send Read_Remote_Supported_Features command to the unit */
static int
hci_read_remote_supported_features(int s, int argc, char **argv)
{
	int				 n;
	char				 b[512];
	ng_hci_read_remote_features_cp	 cp;
	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b; 
	char				 buffer[1024];

	/* parse command parameters */
	switch (argc) {
	case 1:
		/* connecton handle */
		if (sscanf(argv[0], "%d", &n) != 1 || n < 0 || n > 0x0eff)
			return (USAGE);

		cp.con_handle = (n & 0x0fff);
		cp.con_handle = htole16(cp.con_handle);
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_READ_REMOTE_FEATURES), 
			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
again:
	n = sizeof(b);
	if (hci_recv(s, b, &n) == ERROR)
		return (ERROR);

	if (n < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}

	if (e->event == NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL) {
		ng_hci_read_remote_features_compl_ep	*ep = 
				(ng_hci_read_remote_features_compl_ep *)(e + 1);

		if (ep->status != 0x00) {
			fprintf(stdout, "Status: %s [%#02x]\n", 
				hci_status2str(ep->status), ep->status);
			return (FAILED);
		}

		fprintf(stdout, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "Features: ");
		for (n = 0; n < sizeof(ep->features); n++)
			fprintf(stdout, "%#02x ", ep->features[n]);
		fprintf(stdout, "\n%s\n", hci_features2str(ep->features, 
			buffer, sizeof(buffer)));
	} else
		goto again;

	return (OK);
} /* hci_read_remote_supported_features */
Example #6
0
/* Send Remote_Name_Request command to the unit */
static int
hci_remote_name_request(int s, int argc, char **argv)
{
	int				 n0;
	char				 b[512];
	ng_hci_remote_name_req_cp	 cp;
	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b; 

	memset(&cp, 0, sizeof(cp));
	cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0;
	cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE;

	/* parse command parameters */
	switch (argc) {
	case 4:
		/* clock_offset */
		if (sscanf(argv[3], "%x", &n0) != 1)
			return (USAGE);

		cp.clock_offset = (n0 & 0xffff);
		cp.clock_offset = htole16(cp.clock_offset);

	case 3:
		/* page_scan_mode */
		if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x03)
			return (USAGE);

		cp.page_scan_mode = (n0 & 0xff);

	case 2:
		/* page_scan_rep_mode */
		if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x00 || n0 > 0x02)
			return (USAGE);

		cp.page_scan_rep_mode = (n0 & 0xff);

	case 1:
		/* BD_ADDR */
		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));
		}
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n0 = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_REMOTE_NAME_REQ),
			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
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_REMOTE_NAME_REQ_COMPL) {
		ng_hci_remote_name_req_compl_ep	*ep = 
				(ng_hci_remote_name_req_compl_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, "Name: %s\n", ep->name);
	} else 
		goto again;

	return (OK);
} /* hci_remote_name_request */
Example #7
0
/* Send Change_Connection_Packet_Type command to the unit */
static int
hci_change_connection_packet_type(int s, int argc, char **argv)
{
	int				 n;
	char				 b[512];
	ng_hci_change_con_pkt_type_cp	 cp;
	ng_hci_event_pkt_t		*e = (ng_hci_event_pkt_t *) b; 

	switch (argc) {
	case 2:
		/* connection handle */
		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
			return (USAGE);

		cp.con_handle = (uint16_t) (n & 0x0fff);
		cp.con_handle = htole16(cp.con_handle);

		/* packet type */
		if (sscanf(argv[1], "%x", &n) != 1)
			return (USAGE);

		cp.pkt_type = (uint16_t) (n & 0xffff);
		cp.pkt_type = htole16(cp.pkt_type);
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_CHANGE_CON_PKT_TYPE),
			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
again:
	n = sizeof(b);
	if (hci_recv(s, b, &n) == ERROR)
		return (ERROR);
	if (n < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}

	if (e->event == NG_HCI_EVENT_CON_PKT_TYPE_CHANGED) {
		ng_hci_con_pkt_type_changed_ep	*ep = 
				(ng_hci_con_pkt_type_changed_ep *)(e + 1);

		if (ep->status != 0x00) {
			fprintf(stdout, "Status: %s [%#02x]\n", 
				hci_status2str(ep->status), ep->status);
			return (FAILED);
		}

		fprintf(stdout, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "Packet type: %#04x\n",
			le16toh(ep->pkt_type));
	} else
		goto again;

	return (OK);
} /* hci_change_connection_packet_type */
Example #8
0
/* Send Inquiry command to the unit */
static int
hci_inquiry(int s, int argc, char **argv)
{
	int			 n0, n1, n2, timo;
	char			 b[512];
	ng_hci_inquiry_cp	 cp;
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b;

	/* set defaults */
	cp.lap[2] = 0x9e;
	cp.lap[1] = 0x8b;
	cp.lap[0] = 0x33;
	cp.inquiry_length = 5;
	cp.num_responses = 8;

	/* parse command parameters */
	switch (argc) {
	case 3:
		/* number of responses, range 0x00 - 0xff */
		if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 0xff)
			return (USAGE);

		cp.num_responses = (n0 & 0xff);

	case 2:
		/* inquiry length (N * 1.28) sec, range 0x01 - 0x30 */
		if (sscanf(argv[1], "%d", &n0) != 1 || n0 < 0x1 || n0 > 0x30)
			return (USAGE);

		cp.inquiry_length = (n0 & 0xff);

	case 1:
		/* LAP */
		if (sscanf(argv[0], "%x:%x:%x", &n2, &n1, &n0) != 3)
			return (USAGE);

		cp.lap[0] = (n0 & 0xff);
		cp.lap[1] = (n1 & 0xff);
		cp.lap[2] = (n2 & 0xff);

	case 0:
		/* use defaults */
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status back */
	n0 = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_INQUIRY), (char const *) &cp, sizeof(cp),
			b, &n0) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	timo = timeout;
	timeout = cp.inquiry_length * 1.28 + 1;

wait_for_more:
	/* wait for inquiry events */
	n0 = sizeof(b);
	if (hci_recv(s, b, &n0) == ERROR) {
		timeout = timo;
		return (ERROR);
	}

	if (n0 < sizeof(*e)) {
		timeout = timo;
		errno = EIO;
		return (ERROR);
	}

	switch (e->event) {
	case NG_HCI_EVENT_INQUIRY_RESULT: {
		ng_hci_inquiry_result_ep	*ir = 
				(ng_hci_inquiry_result_ep *)(e + 1);
		uint8_t				*r = (uint8_t *)(ir + 1);

		fprintf(stdout, "Inquiry result, num_responses=%d\n",
			ir->num_responses);

		for (n0 = 0; n0 < ir->num_responses; n0++)
			hci_inquiry_response(n0, &r);

		goto wait_for_more;
		}

	case NG_HCI_EVENT_INQUIRY_COMPL:
		fprintf(stdout, "Inquiry complete. Status: %s [%#02x]\n",
			hci_status2str(*(b + sizeof(*e))), *(b + sizeof(*e)));
		break;

	default:
		goto wait_for_more;
	}

	timeout = timo;

	return (OK);
} /* hci_inquiry */
Example #9
0
/* Send Add_SCO_Connection command to the unit */
static int
hci_add_sco_connection(int s, int argc, char **argv)
{
	int			 n;
	char			 b[512];
	ng_hci_add_sco_con_cp	 cp;
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b; 

	/* Set defaults */
	memset(&cp, 0, sizeof(cp));
	cp.pkt_type = htole16(NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3);

	/* parse command parameters */
	switch (argc) {
	case 2:
		/* packet type */
		if (sscanf(argv[1], "%x", &n) != 1)
			return (USAGE);

		n &= (NG_HCI_PKT_HV1 | NG_HCI_PKT_HV2 | NG_HCI_PKT_HV3);
		if (n == 0)
			return (USAGE);

		cp.pkt_type = (uint16_t) (n & 0x0fff);
		cp.pkt_type = htole16(cp.pkt_type);

	case 1:
		/* acl connection handle */
		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
			return (USAGE);

		cp.con_handle = (uint16_t) (n & 0x0fff);
		cp.con_handle = htole16(cp.con_handle);
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_ADD_SCO_CON),
			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
again:
	n = sizeof(b);
	if (hci_recv(s, b, &n) == ERROR)
		return (ERROR);
	if (n < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}

	if (e->event == NG_HCI_EVENT_CON_COMPL) {
		ng_hci_con_compl_ep	*ep = (ng_hci_con_compl_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, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "Encryption mode: %s [%d]\n",
			hci_encrypt2str(ep->encryption_mode, 0),
			ep->encryption_mode);
	} else
		goto again;

	return (OK);
} /* Add_SCO_Connection */
Example #10
0
/* Send Disconnect command to the unit */
static int
hci_disconnect(int s, int argc, char **argv)
{
	int			 n;
	char			 b[512];
	ng_hci_discon_cp	 cp;
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b; 

	/* Set defaults */
	memset(&cp, 0, sizeof(cp));
	cp.reason = 0x13;

	/* parse command parameters */
	switch (argc) {
	case 2:
		/* reason */
		if (sscanf(argv[1], "%d", &n) != 1 || n <= 0x00 || n > 0xff)
			return (USAGE);

		cp.reason = (uint8_t) (n & 0xff);

	case 1:
		/* connection handle */
		if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)
			return (USAGE);

		cp.con_handle = (uint16_t) (n & 0x0fff);
		cp.con_handle = htole16(cp.con_handle);
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_DISCON),
			(char const *) &cp, sizeof(cp), b, &n) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
again:
	n = sizeof(b);
	if (hci_recv(s, b, &n) == ERROR)
		return (ERROR);
	if (n < sizeof(*e)) {
		errno = EIO;
		return (ERROR);
	}

	if (e->event == NG_HCI_EVENT_DISCON_COMPL) {
		ng_hci_discon_compl_ep	*ep = (ng_hci_discon_compl_ep *)(e + 1);

		if (ep->status != 0x00) {
			fprintf(stdout, "Status: %s [%#02x]\n", 
				hci_status2str(ep->status), ep->status);
			return (FAILED);
		}

		fprintf(stdout, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "Reason: %s [%#02x]\n",
			hci_status2str(ep->reason), ep->reason);
	} else
		goto again;

	return (OK);
} /* hci_disconnect */
Example #11
0
/* Send Create_Connection command to the unit */
static int
hci_create_connection(int s, int argc, char **argv)
{
	int			 n0;
	char			 b[512];
	ng_hci_create_con_cp	 cp;
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) b; 

	/* Set defaults */
	memset(&cp, 0, sizeof(cp));
	cp.pkt_type = htole16(	NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 |
				NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 |
				NG_HCI_PKT_DM5);
	cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0;
	cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE;
	cp.clock_offset = 0;
	cp.accept_role_switch = 1;

	/* parse command parameters */
	switch (argc) {
	case 6:
		/* accept role switch */
		if (sscanf(argv[5], "%d", &n0) != 1)
			return (USAGE);

		cp.accept_role_switch = n0 ? 1 : 0;

	case 5:
		/* clock offset */
		if (sscanf(argv[4], "%d", &n0) != 1)
			return (USAGE);

		cp.clock_offset = (n0 & 0xffff);
		cp.clock_offset = htole16(cp.clock_offset);

	case 4:
		/* page scan mode */
		if (sscanf(argv[3], "%d", &n0) != 1 || n0 < 0 || n0 > 3)
			return (USAGE);

		cp.page_scan_mode = (n0 & 0xff);

	case 3:
		/* page scan rep mode */
		if (sscanf(argv[2], "%d", &n0) != 1 || n0 < 0 || n0 > 2)
			return (USAGE);

		cp.page_scan_rep_mode = (n0 & 0xff);

	case 2:
		/* packet type */
		if (sscanf(argv[1], "%x", &n0) != 1)
			return (USAGE);

		n0 &= (	NG_HCI_PKT_DM1 | NG_HCI_PKT_DH1 |
			NG_HCI_PKT_DM3 | NG_HCI_PKT_DH3 |
			NG_HCI_PKT_DM5);
		if (n0 == 0)
			return (USAGE);

		cp.pkt_type = (n0 & 0xffff);
		cp.pkt_type = htole16(cp.pkt_type);

	case 1:
		/* BD_ADDR */
		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));
		}
		break;

	default:
		return (USAGE);
	}

	/* send request and expect status response */
	n0 = sizeof(b);
	if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL,
			NG_HCI_OCF_CREATE_CON),
			(char const *) &cp, sizeof(cp), b, &n0) == ERROR)
		return (ERROR);

	if (*b != 0x00)
		return (FAILED);

	/* wait for event */
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_CON_COMPL) {
		ng_hci_con_compl_ep	*ep = (ng_hci_con_compl_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, "Connection handle: %d\n",
			le16toh(ep->con_handle));
		fprintf(stdout, "Encryption mode: %s [%d]\n",
			hci_encrypt2str(ep->encryption_mode, 0),
			ep->encryption_mode);
	} else
		goto again;

	return (OK);
} /* hci_create_connection */
Example #12
0
/* Send HCI request to the unit */
int
hci_request(int s, int opcode, char const *cp, int cp_size, char *rp, int *rp_size)
{
	char			 buffer[512];
	int			 n;
	ng_hci_cmd_pkt_t	*c = (ng_hci_cmd_pkt_t *) buffer;
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) buffer;

	assert(rp != NULL);
	assert(rp_size != NULL);
	assert(*rp_size > 0);

	c->type = NG_HCI_CMD_PKT;
	c->opcode = (uint16_t) opcode;
	c->opcode = htole16(c->opcode);

	if (cp != NULL) {
		assert(0 < cp_size && cp_size <= NG_HCI_CMD_PKT_SIZE);

		c->length = (uint8_t) cp_size;
		memcpy(buffer + sizeof(*c), cp, cp_size);
	} else
		c->length = 0;

	if (hci_send(s, buffer, sizeof(*c) + cp_size) == ERROR)
		return (ERROR);

again:
	n = sizeof(buffer);
	if (hci_recv(s, buffer, &n) == ERROR)
		return (ERROR);

	if (n < sizeof(*e)) {
		errno = EMSGSIZE;
		return (ERROR);
	}

	if (e->type != NG_HCI_EVENT_PKT) {
		errno = EIO;
		return (ERROR);
	}

	switch (e->event) {
	case NG_HCI_EVENT_COMMAND_COMPL: {
		ng_hci_command_compl_ep	*cc = 
				(ng_hci_command_compl_ep *)(e + 1);

		cc->opcode = le16toh(cc->opcode);

		if (cc->opcode == 0x0000 || cc->opcode != opcode)
			goto again; 

		n -= (sizeof(*e) + sizeof(*cc));
		if (n < *rp_size)
			*rp_size = n;

		memcpy(rp, buffer + sizeof(*e) + sizeof(*cc), *rp_size);
		} break;

	case NG_HCI_EVENT_COMMAND_STATUS: {
		ng_hci_command_status_ep	*cs = 
				(ng_hci_command_status_ep *)(e + 1);

		cs->opcode = le16toh(cs->opcode);

		if (cs->opcode == 0x0000 || cs->opcode != opcode)
			goto again; 

		*rp_size = 1;
		*rp = cs->status;
		} break;

	default:
		goto again;
	}

	return (OK);
} /* hci_request */
Example #13
0
static int
le_scan_result(int s)
{
  unsigned char buffer[512];
  ng_hci_event_pkt_t  *e = (ng_hci_event_pkt_t *) buffer;
  int i,j,k,l;
  int n;
  int err;
  int numrecord;
  int sig_str;

  printf("* Scanning....\n");
  n = sizeof(buffer);
  if ((err = hci_recv(s, (char *)buffer, &n)) == ERROR){
    return (ERROR);
  }

  if (n < sizeof(*e)) {
    printf("Size: %d\n", n);
    errno = EMSGSIZE;
    return (ERROR);
  }

  if (e->type != NG_HCI_EVENT_PKT) {
    printf("Event: %d\n", e->type);
    errno = EIO;
    return (ERROR);
  }

  printf("  Result: %x %x\n", e->event, e->length);
  printf("  Subevent: %d\n", buffer[3]);
  numrecord = buffer[4];
  printf("  NumRecord: %d\n", numrecord);
  j = 5;
  for(i=0; i < numrecord; i++){
    int length_data;
    printf("  Eventtype: %d\n", buffer[j]);
    j++;
    printf("  AddrType: %d\n", buffer[j]);
    j++;
    printf("  Addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
     buffer[j+5],buffer[j+4],buffer[j+3],
     buffer[j+2],buffer[j+1],buffer[j]);
    j += 6;
    length_data = buffer[j];
    printf("  Length_Data %d\n", length_data);
    j++;
    printf("  Data:");
    l = 0;
    for(k=0; k<length_data;k++){
      if(l==0){
        printf("\n");
        l = buffer[j];
        switch(buffer[j+1]){
        case 0x1: // Vol.3 Part C 18.1
          printf("    Flags: ");
          break;
        case 0x2: // Vol.3 Part C 18.2
        case 0x3:
        case 0x4:
        case 0x5:
        case 0x6:
        case 0x7:
          printf("    UUID: ");
          break;
        case 0x8:
        case 0x9:
          printf("    Local Name: ");
          break;
        case 0xa:
          printf("    TX Power: ");
          break;
        default:
          printf("    %02x: ", buffer[j+1]);
          break;
        }
      }else{
        l--;
        printf("%02x ", buffer[j]);
      }
      j++;
    }
    sig_str = ((char*)buffer)[j];
    printf("\n  ");

    printf("RSSI: %x (%d db)\n", buffer[j], sig_str);
  }
  return 0;
}
Example #14
0
int le_scan_result(int s)
{
	unsigned char buffer[512];
	ng_hci_event_pkt_t	*e = (ng_hci_event_pkt_t *) buffer;
	int i,j,k,l;
	int n;
	int err;
	int numrecord; 
	int sig_str;

	printf("START SCANNING\n");
	n = sizeof(buffer);
	if ((err =hci_recv(s, (char *)buffer, &n)) == ERROR){
	  printf("%d %d %s \n", err, n, strerror(errno));
		return (ERROR);
	}
	printf("HOGEHOGE\n");

	if (n < sizeof(*e)) {
		printf("SIZE%d\n", n);
		errno = EMSGSIZE;
		return (ERROR);
	}

	if (e->type != NG_HCI_EVENT_PKT) {
		printf("Event%d\n", e->type);
		errno = EIO;
		return (ERROR);
	}
	
	printf("SCAN_RESULT %x %x\n", e->event, e->length);
	printf("Subevent  %d\n", buffer[3]);
	numrecord = buffer[4];
	printf("NumRecord %d\n", numrecord);
	j = 5;
	for(i=0; i < numrecord; i++){
	  int length_data;
	  printf("Eventtype %d\n", buffer[j]);
	  j++;
	  printf("AddrType %d\n", buffer[j]);
	  j++;
	  printf("Addr:[%02x:%02x:%02x:%02x:%02x:%02x]\n",
		 buffer[j+5],buffer[j+4],buffer[j+3],
		 buffer[j+2],buffer[j+1],buffer[j]);
	  j+=6;
	  length_data=buffer[j];
	  printf("length_data %d\n", length_data);
	  j++;
	  printf("DATA:");
	  l=0;
	  for(k=0; k<length_data;k++){
		  if(l==0){
			  printf("\n");
			  l = buffer[j];
		  }else{
			  l--;
		  }
		  printf("%02x ", buffer[j]);
		  j++;
	  }
	  sig_str = ((char*)buffer)[j];
	  printf("\n");

	  printf("RSSI: %x (%d db)\n", buffer[j], sig_str);
	}
	return 0;
	
}