7 #include <bluetooth/bluetooth.h>
8 #include <bluetooth/hci.h>
9 #include <bluetooth/hci_lib.h>
11 int chosen_dev_id = -1; /* BB device id*/
13 static int dev_info(int socket, int dev_id, long arg)
15 struct hci_dev_info di;
19 /* Ziskej informace o BT zarizeni */
20 err = hci_devinfo(dev_id, &di);
24 ba2str(&di.bdaddr, addr);
25 printf("\t%s\t%s", di.name, addr);
29 if (hci_test_bit(HCI_RAW, &di.flags)) printf("RAW ");
32 if (chosen_dev_id == -1) {
33 /* Uloz prvni nalezene dev_id */
34 chosen_dev_id = dev_id;
43 /* Projed vsechny lokalni BT zarizeni a pro kazde z nich zavolej fci
49 printf("Nalezene lokalni BT zarizeni:\n");
50 ret = hci_for_each_dev(HCI_UP, dev_info, 0);
52 /* v knihovne je chyba - vraci to -1 i kdyz bylo volani uspesne) */
53 if (ret && chosen_dev_id == -1) {
54 perror("hledani zarizeni");
59 /* Otevri BT zarizeni */
62 int dd; /* BT device descriptor se kterym
65 dd = hci_open_dev(chosen_dev_id);
67 perror("hci_open_dev");
71 /* Nasta RAW rezim, aby data nebyly zpracovavany l2cap vrstvou. */
72 if (ioctl(dd, HCISETRAW, 1) < 0) {
73 perror("Can't set RAW mode");
80 /* Najdi okolni zarizeni */
83 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
84 int inq_length = 10; /* sekund */
85 inquiry_info *info = NULL; /* informace o zarizenich */
91 //flags |= IREQ_CACHE_FLUSH; /* Vycisti cache */
93 printf("Scanuji... (%d sekund)\n", inq_length);
94 num_rsp = hci_inquiry(chosen_dev_id,
95 (int)(inq_length / 1.28),
96 0, /* Najdi vsechna zarizeni */
98 &info, /* sem uloz ukazatel na nalezena
101 char addr[18], name[249];
103 /* Zjisti jmena nalezenych zarizeni a vse vypis */
104 for (i = 0; i < num_rsp; i++) {
106 ba2str(&info[i].bdaddr, addr);
108 ret = hci_read_remote_name(dd,
111 100000); /* timeout v milisekundach*/
113 strcpy(name, "n/a"); /* Jmeno neexistuje */
115 printf("\t%s\t%s\n", addr, name);
117 bt_free(info); /* uvolni pamet */
121 /* Spoj se s protejskem */
122 uint16_t spojse(int dd, bdaddr_t bdaddr)
124 uint8_t allow_role_switch = 0x01;
125 uint16_t ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;
127 struct hci_filter flt;
130 /* Nastav filter, aby nam chodily vsechny HCI pakety */
131 hci_filter_clear(&flt);
132 hci_filter_all_ptypes(&flt);
133 hci_filter_all_events(&flt);
134 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
135 perror("Can't set filter");
141 printf("Navazuji spojeni\n");
142 ret = hci_create_connection(dd,
144 ptype, htobs(0x0000),
147 10000); /* timeout */
149 perror("Spojeni nelze navazat");
155 /* Ukazka jak posilat HCI pakety. Paket je slozen ze 3 casti: typ paketu, hlavicka a data */
156 int sendstr(int dd, uint16_t handle, char *buf)
158 uint8_t type = HCI_ACLDATA_PKT;
162 acl_hdr.handle = htobs(handle);
163 acl_hdr.dlen = htobs(strlen(buf));
165 /* sloz paket ze tri casti (typ, hlavicka, data) */
166 iv[0].iov_base = &type;
168 iv[1].iov_base = &acl_hdr;
169 iv[1].iov_len = HCI_ACL_HDR_SIZE;
171 iv[2].iov_base = buf;
172 iv[2].iov_len = acl_hdr.dlen;
174 printf("Sending %d\n", strlen(buf));
175 while (writev(dd, iv, 3) < 0) {
176 if (errno == EAGAIN || errno == EINTR)
183 static void hex_dump(char *pref, int width, unsigned char *buf, int len)
187 for (i = 0, n = 1; i < len; i++, n++) {
190 printf("%2.2X ", buf[i]);
202 int recvstr(int dd, uint16_t handle, char *str, int len)
204 unsigned char buf[200];
205 struct hci_acl_hdr *acl_hdr;
207 while ((len = read(dd, buf, sizeof(buf))) < 0) {
208 if (errno == EAGAIN || errno == EINTR)
214 case HCI_ACLDATA_PKT:
215 printf("ACL DATA: ");
216 acl_hdr = (struct hci_acl_hdr *)(buf+1);
218 case HCI_SCODATA_PKT:
219 printf("SCO DATA: ");
226 hex_dump("content: ", 16, &buf[1], len - 1);
232 void komunikuj(int dd, uint16_t handle)
239 p[0].fd = fileno(stdin);
240 p[0].events = POLLIN;
242 p[1].events = POLLIN;
245 ret = poll(p, 2, 1000);
249 if (p[0].revents & POLLIN) {
250 if (fgets(buf, 99, stdin) != NULL)
251 sendstr(dd, handle, buf); /* posli data */
252 else end = 1; /* konec (Ctrl-D) */
254 if (p[1].revents & POLLIN) {
255 recvstr(dd, handle, buf, 99);
258 } else if (ret < 0) {
266 int main(int argc, char *argv[])
276 printf("Ocekavam jako parametr BD adresu\n");
282 strncpy(bdstr, argv[1], sizeof(bdstr));
283 str2ba(bdstr, &bdaddr);
285 /* Navaz ACL spojeni */
286 handle = spojse(dd, bdaddr);
288 /* Posilej pakety - nefunguje to. Je potreba nastavit dalsi veci pomoci HCI prikazu */
289 komunikuj(dd, handle);
291 printf("Rusim spojeni\n");
292 hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);
295 if (ioctl(dd, HCISETRAW, 0) < 0) {
296 perror("Can't reset RAW mode");
300 /* Zavri BT zarizeni */