Android BlueZ HCI (i): hciconfig implementation and common methods

Source: Internet
Author: User

Key words: Hciconfighcitool hcidump
xubin341719 (Welcome reprint, please specify the author, please respect copyright, thank you!) )
Welcome to correct mistakes, common learning, common progress!!

Android BlueZ HCI (i): hciconfig implementation and common methods
Android BlueZ HCI (ii): Hcitool Hcidump Common methods

First, Hciconfig
1, adb shell, Hciconfig execute the file bit
/system/xbin/hciconfig


ANDROID.MK file under corresponding directory, 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 parts: main_options operation, command execution;
Here we analyze in two parts

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 efault:usage (); exit (0);} }//(2), command execution part argc-= optind;argv + = Optind;optind = 0;/* Open HCI socket */if ((ctl = socket (Af_bluetooth, Sock_raw, BTPR OTO_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), if the all command is executed Case ' h ':d efault:usage ();//3), if the help command prints out the command use method; 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, and parse command parameters to support long options. a pair of dashes, a descriptive option name, and a parameter that you can use to connect to an option with an 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, if it is a, print out Bluetooth related information

Case ' a ': all = 1;break;

This section is analyzed together with the command parsing.

3), if the Help command, print out the command to use the method;

Case ' h ':d efault:usage (); exit (0);

If it is help, perform the usage function. This function is analyzed below
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 is clear:

The command execution results are as follows:


The static struct {char *cmd;//command, such as Hciconfig up;void (*func) (int ctl, int hdev, char *opt);//command execution function; char *opt;//char *doc;//life Order description} Command[] = {{"Up", cmd_up,0, "Open and initialize HCI device"},{"down", cmd_down,0, "Close HCI Device"},{"reset", CM d_reset,0, "Reset HCI Device"},..................}
This section takes: {"Up", cmd_up, 0, "Open and Initialize Hcidevice"}, for example

When using the UP command, this function is called to CMD_UP:
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 execution 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 execution 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, complete 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);

Parameter description:
Domain:
indicates the protocol family used, usually pf_inet, which represents the TCP/IP protocol;
  Type : The parameter specifies the types of sockets, basically there are three kinds: data flow sockets, datagram sockets, raw sockets
  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= 10,}; Protocol: 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);}

Determine the relevant parameters in Di.
hci_test_bit detects 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 execution, which is part of the command implementation

while (argc > 0) {for (i = 0; command[i].cmd; i++) {if (strncmp (Command[i].cmd, *ARGV, 5))//compares the second parameter with a For loop continue;if ( command[i].opt) {argc--; argv++;} Command[i].func (CTL, di.dev_id, *ARGV);//If the parameters correspond, execute 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))//Compare the second parameter with a for loop
| such as command: HCICONFIGHCI0 commands
The value of argv is: commands string, if 5 characters typeface, etc.

b, if corresponding , execute the corresponding command function

Command[i].func (CTL, di.dev_id, *ARGV);
C, if 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 implemented for each command. 5), close the CTL, complete operation close (CTL); return 0;

3, Hciconfig command common methods:
(1), help command: View Hciconfig supported commands and usage
Hciconfig–h or Hciconfig--hlep

(2), view Bluetooth related information

Hciconfig–a


[email         protected]:/# HCICONFIG-AHCICONFIG-AHCI0:TYPE:BR/EDR bus:uart//Bluetooth interface type, this is the UART, as well as 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 accept//The type of connection that is the primary device, slave name: ' SP8825C1 '//bluetooth names Cla ss: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 LMP version:2.1 (0x4) subversion:0x125 0//LMP version 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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.