|
Lines 67-72
Link Here
|
| 67 |
static int le_clear_white_list(int s, int argc, char *argv[]); |
67 |
static int le_clear_white_list(int s, int argc, char *argv[]); |
| 68 |
static int le_add_device_to_white_list(int s, int argc, char *argv[]); |
68 |
static int le_add_device_to_white_list(int s, int argc, char *argv[]); |
| 69 |
static int le_remove_device_from_white_list(int s, int argc, char *argv[]); |
69 |
static int le_remove_device_from_white_list(int s, int argc, char *argv[]); |
|
|
70 |
static int le_connect(int s, int argc, char *argv[]); |
| 71 |
static void handle_le_connection_event(ng_hci_event_pkt_t* e, bool verbose); |
| 70 |
|
72 |
|
| 71 |
static int |
73 |
static int |
| 72 |
le_set_scan_param(int s, int argc, char *argv[]) |
74 |
le_set_scan_param(int s, int argc, char *argv[]) |
|
Lines 933-938
Link Here
|
| 933 |
return (OK); |
935 |
return (OK); |
| 934 |
} |
936 |
} |
| 935 |
|
937 |
|
|
|
938 |
static int |
| 939 |
le_connect(int s, int argc, char *argv[]) |
| 940 |
{ |
| 941 |
ng_hci_le_create_connection_cp cp; |
| 942 |
ng_hci_status_rp rp; |
| 943 |
char b[512]; |
| 944 |
ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) b; |
| 945 |
|
| 946 |
int n, scancount, bufsize; |
| 947 |
char ch; |
| 948 |
bool addr_set = false; |
| 949 |
bool verbose = false; |
| 950 |
|
| 951 |
optreset = 1; |
| 952 |
optind = 0; |
| 953 |
|
| 954 |
/* minimal scan interval (2.5ms) */ |
| 955 |
cp.scan_interval = htole16(4); |
| 956 |
cp.scan_window = htole16(4); |
| 957 |
|
| 958 |
/* Don't use the whitelist */ |
| 959 |
cp.filter_policy = 0x00; |
| 960 |
|
| 961 |
/* Default to public peer address */ |
| 962 |
cp.peer_addr_type = 0x00; |
| 963 |
|
| 964 |
/* Own address type public */ |
| 965 |
cp.own_address_type = 0x00; |
| 966 |
|
| 967 |
/* 18.75ms min connection interval */ |
| 968 |
cp.conn_interval_min = htole16(0x000F); |
| 969 |
/* 18.75ms max connection interval */ |
| 970 |
cp.conn_interval_max = htole16(0x000F); |
| 971 |
|
| 972 |
/* 0 events connection latency */ |
| 973 |
cp.conn_latency = htole16(0x0000); |
| 974 |
|
| 975 |
/* 32s supervision timeout */ |
| 976 |
cp.supervision_timeout = htole16(0x0C80); |
| 977 |
|
| 978 |
/* Min CE Length 0.625 ms */ |
| 979 |
cp.min_ce_length = htole16(1); |
| 980 |
/* Max CE Length 0.625 ms */ |
| 981 |
cp.max_ce_length = htole16(1); |
| 982 |
|
| 983 |
while ((ch = getopt(argc, argv , "a:t:v")) != -1) { |
| 984 |
switch(ch) { |
| 985 |
case 't': |
| 986 |
if (strcmp(optarg, "public") == 0) |
| 987 |
cp.peer_addr_type = 0x00; |
| 988 |
else if (strcmp(optarg, "random") == 0) |
| 989 |
cp.peer_addr_type = 0x01; |
| 990 |
else |
| 991 |
return (USAGE); |
| 992 |
break; |
| 993 |
case 'a': |
| 994 |
addr_set = true; |
| 995 |
if (!bt_aton(optarg, &cp.peer_addr)) { |
| 996 |
struct hostent *he = NULL; |
| 997 |
|
| 998 |
if ((he = bt_gethostbyname(optarg)) == NULL) |
| 999 |
return (USAGE); |
| 1000 |
|
| 1001 |
memcpy(&cp.peer_addr, he->h_addr, |
| 1002 |
sizeof(cp.peer_addr)); |
| 1003 |
} |
| 1004 |
break; |
| 1005 |
case 'v': |
| 1006 |
verbose = true; |
| 1007 |
break; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (addr_set == false) |
| 1012 |
return (USAGE); |
| 1013 |
|
| 1014 |
n = sizeof(rp); |
| 1015 |
if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_LE, |
| 1016 |
NG_HCI_OCF_LE_CREATE_CONNECTION), |
| 1017 |
(void *)&cp, sizeof(cp), (void *)&rp, &n) == ERROR) |
| 1018 |
return (ERROR); |
| 1019 |
|
| 1020 |
if (rp.status != 0x00) { |
| 1021 |
fprintf(stdout, |
| 1022 |
"Create connection failed. Status: %s [%#02x]\n", |
| 1023 |
hci_status2str(rp.status), rp.status); |
| 1024 |
return (FAILED); |
| 1025 |
} |
| 1026 |
|
| 1027 |
scancount = 0; |
| 1028 |
while (scancount < 3) { |
| 1029 |
/* wait for connection events */ |
| 1030 |
bufsize = sizeof(b); |
| 1031 |
if (hci_recv(s, b, &bufsize) == ERROR) { |
| 1032 |
return (ERROR); |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (bufsize < sizeof(*e)) { |
| 1036 |
errno = EIO; |
| 1037 |
return (ERROR); |
| 1038 |
} |
| 1039 |
scancount++; |
| 1040 |
if (e->event == NG_HCI_EVENT_LE) { |
| 1041 |
handle_le_connection_event(e, verbose); |
| 1042 |
break; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
return (OK); |
| 1047 |
} |
| 1048 |
|
| 1049 |
static void handle_le_connection_event(ng_hci_event_pkt_t* e, bool verbose) |
| 1050 |
{ |
| 1051 |
ng_hci_le_ep *ev_pkt; |
| 1052 |
ng_hci_le_connection_complete_ep *conn_event; |
| 1053 |
|
| 1054 |
ev_pkt = (ng_hci_le_ep *)(e + 1); |
| 1055 |
|
| 1056 |
if (ev_pkt->subevent_code == NG_HCI_LEEV_CON_COMPL) { |
| 1057 |
conn_event =(ng_hci_le_connection_complete_ep *)(ev_pkt + 1); |
| 1058 |
fprintf(stdout, "Handle: %d\n", le16toh(conn_event->handle)); |
| 1059 |
if (verbose) { |
| 1060 |
fprintf(stdout, |
| 1061 |
"Status: %s\n", |
| 1062 |
hci_status2str(conn_event->status)); |
| 1063 |
fprintf(stdout, |
| 1064 |
"Role: %s\n", |
| 1065 |
hci_role2str(conn_event->role)); |
| 1066 |
fprintf(stdout, |
| 1067 |
"Address Type: %s\n", |
| 1068 |
hci_addrtype2str(conn_event->address_type)); |
| 1069 |
fprintf(stdout, |
| 1070 |
"Address: %s\n", |
| 1071 |
hci_bdaddr2str(&conn_event->address)); |
| 1072 |
fprintf(stdout, |
| 1073 |
"Interval: %.2fms\n", |
| 1074 |
6.25 * le16toh(conn_event->interval)); |
| 1075 |
fprintf(stdout, |
| 1076 |
"Latency: %d events\n", conn_event->latency); |
| 1077 |
fprintf(stdout, |
| 1078 |
"Supervision timeout: %dms\n", |
| 1079 |
10 * le16toh(conn_event->supervision_timeout)); |
| 1080 |
fprintf(stdout, |
| 1081 |
"Master clock accuracy: %sn", |
| 1082 |
hci_mc_accuracy2str( |
| 1083 |
conn_event->master_clock_accuracy)); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
return; |
| 1087 |
} |
| 1088 |
|
| 936 |
struct hci_command le_commands[] = { |
1089 |
struct hci_command le_commands[] = { |
| 937 |
{ |
1090 |
{ |
| 938 |
"le_enable", |
1091 |
"le_enable", |
|
Lines 1037-1040
Link Here
|
| 1037 |
"Remove device from the white list", |
1190 |
"Remove device from the white list", |
| 1038 |
&le_remove_device_from_white_list |
1191 |
&le_remove_device_from_white_list |
| 1039 |
}, |
1192 |
}, |
|
|
1193 |
{ |
| 1194 |
"le_connect", |
| 1195 |
"le_connect -a address [-t public|random] [-v]\n" |
| 1196 |
"Connect to an LE device", |
| 1197 |
&le_connect |
| 1198 |
}, |
| 1040 |
}; |
1199 |
}; |