3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
35 #include <bluetooth/bluetooth.h>
36 #include <bluetooth/hci.h>
37 #include <bluetooth/hci_lib.h>
41 static int transient = 0;
43 static int generic_reset_device(int dd)
48 err = hci_send_cmd(dd, 0x03, 0x0003, 0, NULL);
52 return hci_read_bd_addr(dd, &bdaddr, 10000);
55 #define OCF_ERICSSON_WRITE_BD_ADDR 0x000d
58 } __attribute__ ((packed)) ericsson_write_bd_addr_cp;
59 #define ERICSSON_WRITE_BD_ADDR_CP_SIZE 6
61 static int ericsson_write_bd_addr(int dd, bdaddr_t *bdaddr)
63 struct hci_request rq;
64 ericsson_write_bd_addr_cp cp;
66 memset(&cp, 0, sizeof(cp));
67 bacpy(&cp.bdaddr, bdaddr);
69 memset(&rq, 0, sizeof(rq));
70 rq.ogf = OGF_VENDOR_CMD;
71 rq.ocf = OCF_ERICSSON_WRITE_BD_ADDR;
73 rq.clen = ERICSSON_WRITE_BD_ADDR_CP_SIZE;
77 if (hci_send_req(dd, &rq, 1000) < 0)
83 #define OCF_ERICSSON_STORE_IN_FLASH 0x0022
87 uint8_t flash_data[253];
88 } __attribute__ ((packed)) ericsson_store_in_flash_cp;
89 #define ERICSSON_STORE_IN_FLASH_CP_SIZE 255
91 static int ericsson_store_in_flash(int dd, uint8_t user_id, uint8_t flash_length, uint8_t *flash_data)
93 struct hci_request rq;
94 ericsson_store_in_flash_cp cp;
96 memset(&cp, 0, sizeof(cp));
98 cp.flash_length = flash_length;
100 memcpy(cp.flash_data, flash_data, flash_length);
102 memset(&rq, 0, sizeof(rq));
103 rq.ogf = OGF_VENDOR_CMD;
104 rq.ocf = OCF_ERICSSON_STORE_IN_FLASH;
106 rq.clen = ERICSSON_STORE_IN_FLASH_CP_SIZE;
110 if (hci_send_req(dd, &rq, 1000) < 0)
116 static int csr_write_bd_addr(int dd, bdaddr_t *bdaddr)
118 unsigned char cmd[] = { 0x02, 0x00, 0x0c, 0x00, 0x11, 0x47, 0x03, 0x70,
119 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
122 unsigned char cp[254], rp[254];
123 struct hci_request rq;
128 cmd[16] = bdaddr->b[2];
130 cmd[18] = bdaddr->b[0];
131 cmd[19] = bdaddr->b[1];
132 cmd[20] = bdaddr->b[3];
134 cmd[22] = bdaddr->b[4];
135 cmd[23] = bdaddr->b[5];
137 memset(&cp, 0, sizeof(cp));
139 memcpy(cp + 1, cmd, sizeof(cmd));
141 memset(&rq, 0, sizeof(rq));
142 rq.ogf = OGF_VENDOR_CMD;
144 rq.event = EVT_VENDOR;
146 rq.clen = sizeof(cmd) + 1;
148 rq.rlen = sizeof(rp);
150 if (hci_send_req(dd, &rq, 2000) < 0)
158 if ((rp[9] + (rp[10] << 8)) != 0) {
166 static int csr_reset_device(int dd)
168 unsigned char cmd[] = { 0x02, 0x00, 0x09, 0x00,
169 0x00, 0x00, 0x01, 0x40, 0x00, 0x00,
170 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
172 unsigned char cp[254], rp[254];
173 struct hci_request rq;
178 memset(&cp, 0, sizeof(cp));
180 memcpy(cp + 1, cmd, sizeof(cmd));
182 memset(&rq, 0, sizeof(rq));
183 rq.ogf = OGF_VENDOR_CMD;
185 rq.event = EVT_VENDOR;
187 rq.clen = sizeof(cmd) + 1;
189 rq.rlen = sizeof(rp);
191 if (hci_send_req(dd, &rq, 2000) < 0)
197 #define OCF_TI_WRITE_BD_ADDR 0x0006
200 } __attribute__ ((packed)) ti_write_bd_addr_cp;
201 #define TI_WRITE_BD_ADDR_CP_SIZE 6
203 static int ti_write_bd_addr(int dd, bdaddr_t *bdaddr)
205 struct hci_request rq;
206 ti_write_bd_addr_cp cp;
208 memset(&cp, 0, sizeof(cp));
209 bacpy(&cp.bdaddr, bdaddr);
211 memset(&rq, 0, sizeof(rq));
212 rq.ogf = OGF_VENDOR_CMD;
213 rq.ocf = OCF_TI_WRITE_BD_ADDR;
215 rq.clen = TI_WRITE_BD_ADDR_CP_SIZE;
219 if (hci_send_req(dd, &rq, 1000) < 0)
225 #define OCF_BCM_WRITE_BD_ADDR 0x0001
228 } __attribute__ ((packed)) bcm_write_bd_addr_cp;
229 #define BCM_WRITE_BD_ADDR_CP_SIZE 6
231 static int bcm_write_bd_addr(int dd, bdaddr_t *bdaddr)
233 struct hci_request rq;
234 bcm_write_bd_addr_cp cp;
236 memset(&cp, 0, sizeof(cp));
237 bacpy(&cp.bdaddr, bdaddr);
239 memset(&rq, 0, sizeof(rq));
240 rq.ogf = OGF_VENDOR_CMD;
241 rq.ocf = OCF_BCM_WRITE_BD_ADDR;
243 rq.clen = BCM_WRITE_BD_ADDR_CP_SIZE;
247 if (hci_send_req(dd, &rq, 1000) < 0)
253 #define OCF_ZEEVO_WRITE_BD_ADDR 0x0001
256 } __attribute__ ((packed)) zeevo_write_bd_addr_cp;
257 #define ZEEVO_WRITE_BD_ADDR_CP_SIZE 6
259 static int zeevo_write_bd_addr(int dd, bdaddr_t *bdaddr)
261 struct hci_request rq;
262 zeevo_write_bd_addr_cp cp;
264 memset(&cp, 0, sizeof(cp));
265 bacpy(&cp.bdaddr, bdaddr);
267 memset(&rq, 0, sizeof(rq));
268 rq.ogf = OGF_VENDOR_CMD;
269 rq.ocf = OCF_ZEEVO_WRITE_BD_ADDR;
271 rq.clen = ZEEVO_WRITE_BD_ADDR_CP_SIZE;
275 if (hci_send_req(dd, &rq, 1000) < 0)
281 static int st_write_bd_addr(int dd, bdaddr_t *bdaddr)
283 return ericsson_store_in_flash(dd, 0xfe, 6, (uint8_t *) bdaddr);
288 int (*write_bd_addr)(int dd, bdaddr_t *bdaddr);
289 int (*reset_device)(int dd);
291 { 0, ericsson_write_bd_addr, NULL },
292 { 10, csr_write_bd_addr, csr_reset_device },
293 { 13, ti_write_bd_addr, NULL },
294 { 15, bcm_write_bd_addr, generic_reset_device },
295 { 18, zeevo_write_bd_addr, NULL },
296 { 48, st_write_bd_addr, generic_reset_device },
297 { 57, ericsson_write_bd_addr, generic_reset_device },
298 { 65535, NULL, NULL },
301 static void usage(void)
303 printf("bdaddr - Utility for changing the Bluetooth device address\n\n");
305 "\tbdaddr [-i <dev>] [-r] [-t] [new bdaddr]\n");
308 static struct option main_options[] = {
309 { "device", 1, 0, 'i' },
310 { "reset", 0, 0, 'r' },
311 { "transient", 0, 0, 't' },
312 { "help", 0, 0, 'h' },
316 int main(int argc, char *argv[])
318 struct hci_dev_info di;
319 struct hci_version ver;
321 char addr[18], oui[9], *comp;
322 int i, dd, opt, dev = 0, reset = 0;
324 bacpy(&bdaddr, BDADDR_ANY);
326 while ((opt=getopt_long(argc, argv, "+i:rth", main_options, NULL)) != -1) {
329 dev = hci_devid(optarg);
331 perror("Invalid device");
355 dd = hci_open_dev(dev);
357 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
358 dev, strerror(errno), errno);
362 if (hci_devinfo(dev, &di) < 0) {
363 fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
364 dev, strerror(errno), errno);
369 if (hci_read_local_version(dd, &ver, 1000) < 0) {
370 fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
371 dev, strerror(errno), errno);
376 if (!bacmp(&di.bdaddr, BDADDR_ANY)) {
377 if (hci_read_bd_addr(dd, &bdaddr, 1000) < 0) {
378 fprintf(stderr, "Can't read address for hci%d: %s (%d)\n",
379 dev, strerror(errno), errno);
384 bacpy(&bdaddr, &di.bdaddr);
386 printf("Manufacturer: %s (%d)\n",
387 bt_compidtostr(ver.manufacturer), ver.manufacturer);
389 ba2oui(&bdaddr, oui);
390 comp = ouitocomp(oui);
392 ba2str(&bdaddr, addr);
393 printf("Device address: %s", addr);
396 printf(" (%s)\n", comp);
406 str2ba(argv[0], &bdaddr);
407 if (!bacmp(&bdaddr, BDADDR_ANY)) {
412 for (i = 0; vendor[i].compid != 65535; i++)
413 if (ver.manufacturer == vendor[i].compid) {
414 ba2oui(&bdaddr, oui);
415 comp = ouitocomp(oui);
417 ba2str(&bdaddr, addr);
418 printf("New BD address: %s", addr);
421 printf(" (%s)\n\n", comp);
427 if (vendor[i].write_bd_addr(dd, &bdaddr) < 0) {
428 fprintf(stderr, "Can't write new address\n");
433 printf("Address changed - ");
435 if (reset && vendor[i].reset_device) {
436 if (vendor[i].reset_device(dd) < 0) {
437 printf("Reset device manually\n");
439 ioctl(dd, HCIDEVRESET, dev);
440 printf("Device reset successfully\n");
443 printf("Reset device now\n");
446 //ioctl(dd, HCIDEVRESET, dev);
447 //ioctl(dd, HCIDEVDOWN, dev);
448 //ioctl(dd, HCIDEVUP, dev);
457 fprintf(stderr, "Unsupported manufacturer\n");