Key words: Hciconfighcitool hcidump
Author: xubin341719 (Welcome reprint, please specify clearly, please respect copyright, thank you.) )
Welcome to correct mistakes, common learning, common progress!
。
Android BlueZ HCI (i): hciconfig implementation and regular usage
Android BlueZ HCI (ii): Hcitool hcidump Frequent usage
First, Hciconfig
1, under the ADB shell. Hciconfig the bits of the running file
/system/xbin/hciconfig
ANDROID.MK file under corresponding folder, Generate Hciconfig
# # Hciconfig#include $ (clear_vars) local_src_files:= csr.c csr_h4.c hciconfig.c ...... ..... Local_module_path: = $ (target_out_optional_executables) Local_module_tags: = Optionallocal_module:=hciconfiginclude $ (build_executable)
2, Hciconfig code implementation
idh.code\external\bluetooth\bluez\tools\hciconfig.c
main function has two functions: main_options operation. The run of the command;
the following two parts of our analysis
int main (int argc, char *argv[]) {int opt, CTL, I, cmd=0;//(1), hciconfig two life main_options,help and all two commands. while ((Opt=getopt_long (argc, argv, "Ah", Main_options, NULL))! =-1) {switch (opt) {case ' a ': all = 1;break;case ' h ':d efaul T:usage (); exit (0);}} (2), command run part argc-= optind;argv + = Optind;optind = 0;/* Open HCI socket */if (ctl = socket (Af_bluetooth, Sock_raw, Btpro TO_HCI)) < 0) {perror ("Can ' t open HCI socket."); Exit (1);} if (ARGC < 1) {print_dev_list (CTL, 0); exit (0);} di.dev_id = Atoi (argv[0] + 3); argc--; Argv++;if (IOCTL (CTL, Hcigetdevinfo, (void *) &di)) {perror ("Can ' t get device Info"); exit (1);} if (Hci_test_bit (Hci_raw, &di.flags) &&!bacmp (&di.bdaddr, Bdaddr_any)) {int dd = Hci_open_dev (di.dev_ ID); HCI_READ_BD_ADDR (DD, &di.bdaddr, +); Hci_close_dev (DD);} while (argc > 0) {for (i = 0; command[i].cmd; i++) {if (strncmp (Command[i].cmd, *ARGV, 5)) continue;if (command[i].opt) {argc--; argv++;} Command[i].func (CTL, di.dev_id, *argv); cmd = 1;break;} argc--; argv++;} if (!cmd) Print_dev_info (CTL, &di); close (CTL); return 0;}
(1), hciconfig two life main_options,help and all two commands
int main (int argc, char *argv[]) {int opt, CTL, I, CMD=0;//1), hciconfig two life main_options,help and all two commands. while ((Opt=getopt_long (argc, argv, "Ah", Main_options, NULL))! =-1) {//1), main_options. Switch (opt) {case ' a ': all = 1;BREAK;//2), assuming the all command is running, case ' H ':d efault:usage ();//3), assuming that the help command prints out the command usage. Exit (0);}} ..................}
1),main_options;
while ((Opt=getopt_long (argc, argv, "Ah", Main_options, NULL))! =-1) {
Getopt is used to parse command-line option parameters. Getopt_long. The parse command parameter supports the long option. a pair of dashes, a descriptive descriptive option name, and the ability to include a parameter that is connected to an option using the equal sign.
http://blog.csdn.net/slmmlk2011_2/article/details/7964218 is described in detail.
namely: Hciconfig–all
main_optionsstatic struct option main_options[] = {{"Help", 0, 0, ' h '},{"All", 0, 0, ' a '},{0, 0, 0, 0}};
2), Ops parse out the data, assuming it is a, print out Bluetooth related information
Case ' a ': all = 1;break;
This section is analyzed together with the command parsing.
3), assuming the help command, print out the command usage.
Case ' h ':d efault:usage (); exit (0);
Assume that the help runs the usage function. The following analysis of this function
Idh.code\external\bluetooth\bluez\tools\hciconfig.c
static void usage (void) {int i;printf ("HCICONFIG-HCI device Configuration utility\n");p rintf ("usage:\n" "\thciconfig\n "\thciconfig [-a] hcix [command]\n");p rintf ("commands:\n"); for (i=0; Command[i].cmd; i++) printf ("\t%-10s%-8s\t%s\n", command[i].cmd,command[i].opt?Command[i].opt: "", Command[i].doc);}
This function is relatively simple, that is, not command[] The command string in the struct is printed out: The following two are at a glance:
The command runs the results such as the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehviaw4znde3mtk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>
static struct {char *cmd;//command. For example Hciconfig up;void (*func) (int ctl, int hdev, char *opt);//command run function. Char *opt;//char *doc;//command descriptive narration} command[] = {{"Up", cmd_up,0, "Open and initialize HCI device"},{"down", cmd_down,0, "Close HCI Device "},{" reset ", cmd_reset,0," Reset HCI Device "},..................}
This section takes: {"Up", cmd_up, 0, "Open and Initialize Hcidevice"}, for example
When you use the UP command. Will call to, cmd_up this function:
Idh.code\external\bluetooth\bluez\tools\hciconfig.c
static void cmd_up (int ctl, int hdev, char *opt) {/* Start HCI device */if (IOCTL (CTL, Hcidevup, Hdev) < 0) {if (errno = = Ealready) return;fprintf (stderr, "Can ' t init device hci%d:%s (%d) \ n", Hdev, Strerror (errno), errno); exit (1);}}
(2), command run part
Main () {.... argc-= optind;argv + = Optind;optind = 0;/* Open HCI socket *///1), open HCI socket communication if ((CTL = socket (Af_blueto OTH, Sock_raw, BTPROTO_HCI)) < 0) {/perror ("Can ' t open HCI socket."); Exit (1);} if (ARGC < 1) {print_dev_list (CTL, 0); exit (0);} di.dev_id = Atoi (argv[0] + 3); argc--; ARGV++;//2), obtain the HCI drive information via the IOCTL if (IOCTL (CTL, Hcigetdevinfo, (void *) &di)) {perror ("Can ' t get device Info"); exit (1);} 3), Hci_test_bit bacmpif (Hci_test_bit (Hci_raw, &di.flags) &&!bacmp (&di.bdaddr, BDADDR_ANY)) {int DD = Hci_open_dev (di.dev_id); Hci_read_bd_addr (DD, &di.bdaddr, +); Hci_close_dev (DD);} 4) command runs while (argc > 0) {for (i = 0; command[i].cmd; i++) {if (strncmp (Command[i].cmd, *ARGV, 5)) Continue;if (command[ i].opt) {argc--; argv++;} Command[i].func (CTL, di.dev_id, *argv); cmd = 1;break;} argc--; argv++;} if (!cmd)//There is no corresponding command to print out Print_dev_info (CTL, &di),//5), close CTL, finish operation Close (CTL); return 0;}
1), open HCI socket communication
/* Open HCI Socket */if ((ctl = socket (Af_bluetooth, Sock_raw, BTPROTO_HCI)) < 0) {int socket (int domain, int type, int protocol);
Description of the parameters:
Domain: indicates the protocol family used, typically pf_inet, that represents the TCP/IP protocol.
Type : parameters Specify the type of socket, basically there are three kinds: data flow socket, datagram socket, raw socket
protocol: usually assigns a value of "0".
Bluetooth setup in the program:
Damain: Using af_bluetooth;idh.code\3rdparty\bluetooth\trout_bt\special\android\kernel\include\net\bluetooth\ Bluetooth.h#define Af_bluetooth31type:soc_seqpacket, read in packet unit. Soc_raw: Original Socketenum sock_type {sock_stream= 1,sock_dgram= 2,sock_raw= 3,sock_rdm= 4,sock_seqpacket= 5,SOCK_DCCP= 6, sock_packet=,};p rotocol: Using the corresponding established socket protocol, different types of into L2cap, HCI, SCO ... #define Btproto_l2cap0#define btproto_hci1 #define Btproto_sco2#define btproto_rfcomm3#define btproto_bnep4#define btproto_cmtp5#define BTPROTO_HIDP6#define Btproto_avdtp7
2), obtain HCI drive information via IOCTL, write to struct hci_dev_info di structure
if (IOCTL (CTL, Hcigetdevinfo, (void *) &di))
idh.code\3rdparty\bluetooth\trout_bt\special\android\kernel\include\net\bluetooth\hci.h definition of RELATED commands
#define Hcigetdevlist_ior (' h ', hcigetdevinfo_ior, int) #define (' H ', 211, int) #define HCIGETCONNLIST_IOR (' h ', 212, int) #define HCIGETCONNINFO_IOR (' H ', 213, int) #define HCIGETAUTHINFO_IOR (' h ', 215, int) di corresponding data structure static struct HCI_DEV_ Info di;struct hci_dev_info {__u16 dev_id;char name[8];bdaddr_t bdaddr;__u32 flags;__u8 type;__u8 Features[8];__u32 pkt_type;__u32 link_policy;__u32 link_mode;__u16 acl_mtu;__u16 acl_pkts;__u16 sco_mtu;__u16 sco_ Pkts;struct hci_dev_stats Stat;};
3), Hci_test_bit bacmp
if (Hci_test_bit (Hci_raw, &di.flags) &&!bacmp (&di.bdaddr, Bdaddr_any)) {int dd = Hci_open_dev (di.dev_ ID); HCI_READ_BD_ADDR (DD, &di.bdaddr, +); Hci_close_dev (DD);}
Infer the relevant parameters in Di.
hci_test_bit Check if the NR bit of the *ADDR is 1 (the lowest *addr right starting position is No. 0)
static inline int hci_test_bit (int nr, void *addr) {return * ((__U32 *) addr + (nr >> 5)) & ((__U32) 1 << (n R & 31));}
4), command run, which is part of the command implementation
while (argc > 0) {for (i = 0; command[i].cmd; i++) {if (strncmp (Command[i].cmd, *ARGV, 5))//The second parameter through the For Loop continue;if ( command[i].opt) {argc--; argv++;} Command[i].func (CTL, di.dev_id, *ARGV);//Assuming the parameters correspond, run the corresponding command function cmd = 1;break;} argc--; argv++;} if (!cmd)//There is no corresponding command to print out Print_dev_info (CTL, &di);
A, such as:if (strncmp (Command[i].cmd, *ARGV, 5))//The second parameter is compared with a for loop
| such as command: HCICONFIGHCI0 commands
The value of argv is: commands string, assuming 5 characters typeface and so on.
B, the assumption corresponds . Just run the corresponding command function
Command[i].func (CTL, di.dev_id, *ARGV);
C, assuming that the commands command: Corresponds to an array in the command[] struct
{"Commands", cmd_commands,0, "Display supported Commands"},
Command[i].func = Cmd_commands
The implementation of the implementation function Cmd_commands function corresponding to commands:
static void Cmd_commands (int ctl, int hdev, char *opt) {uint8_t Cmds[64];char *str;int i, n, dd;dd = Hci_open_dev (Hdev); if (DD < 0) {fprintf (stderr, "Can ' t Open device hci%d:%s (%d) \ n", Hdev, Strerror (errno), errno); exit (1);} if (Hci_read_local_commands (DD, CMDS, £) < 0) {fprintf (stderr, "Can ' t read support commands on hci%d:%s (%d) \ n", Hde V, strerror (errno), errno); exit (1);} PRINT_DEV_HDR (&di); for (i = 0; i <; i++) {if (!cmds[i]) continue;printf ("%s Octet%-2d = 0x%02x (Bit", I? "\t\t": "\tcommands:", I, cmds[i]); for (n = 0; n < 8; n++) if (Cmds[i] & (1 << N)) printf ("%d", N);p rintf (") \ n");} str = HCI_COMMANDSTOSTR (Cmds, "\ T", ();p rintf ("%s\n", str); Bt_free (str); Hci_close_dev (DD);} This is the specific function that is implemented for each command. 5), close CTL, complete operation close (CTL); return 0;
3. Hciconfig commands frequently used:
(1), help command: View Hciconfig supported commands and usage
Hciconfig–h or Hciconfig--hlep
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehviaw4znde3mtk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>
(2), view Bluetooth related information
Hciconfig–a
[email protected]:/# HCICONFIG-AHCICONFIG-AHCI0:TYPE:BR/EDR bus:uart//The interface type of Bluetooth. This is the UART. There are also USB, PCI ..... BD address:00:16:53:96:22:53 ACL mtu:1021:8 SCO mtu:120:10//bluetooth address up RUNNING pscan//command status RX bytes:2846 a cl:0 sco:0 events:67 errors:0 TX bytes:2034 acl:0 sco:0 commands:80 errors:0 Features:0xff 0xff 0x8d 0xfe 0x9b 0xbf 0x79 0x83 Packet type:dm1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 hv3//support Packet Li NK Policy:rswitch hold SNIFF PARK link mode:slave The type of accept//connection. is the main device, from device name: ' SP8825C1 '//Bluetooth name class:0x5a020c//Bluetooth type Service classes:networking, capturing, Object Transfer, telephony//supported type Device class:phone, Smart Phone HCI version:2.1 (0x4) REVISION:0X1250//HCI version Number LMP version:2.1 (0x4) SUBVERSION:0X1250//LMP version number Manufacturer:ericsson technology Licensing (0)//author
(3), start Bluetooth
Hciconfig hci0 up
(4), turn off Bluetooth
Hciconfig Hci0 Down
(5), View HCI command
Hciconfighci0 commands
(6), display OOB data
Hciconfig Hci0 Oobdata
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android BlueZ HCI (one): Hciconfig implemented and frequently used