Android blueZ HCI (I): Implementation of hciconfig and common methods

Source: Internet
Author: User

Android blueZ HCI (I): Implementation of hciconfig and common methods

Keywords: hciconfighcitool hcidump
Author: xubin341719 (You are welcome to reprint it. Please indicate the author. Please respect copyright. Thank you !)
Correct the mistakes and learn and make progress together !!

Android blueZ HCI (I): Implementation of hciconfig and common methods
Android blueZ hci (II): Common hcitool hcidump Methods

1. Hciconfig
1. The bit of the hciconfig execution file in the adb shell
/System/xbin/hciconfig


The Android. mk file in the corresponding directory to 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
The main function has two main functions: main_options operation and command execution;
The following is an analysis in two parts:

Int main (int argc, char * argv []) {int opt, ctl, I, cmd = 0; // (1) and hciconfig, help and all commands; while (opt = getopt_long (argc, argv, "ah", main_options, NULL ))! =-1) {switch (opt) {case 'A': all = 1; break; case 'H': default: usage (); exit (0 );}} // (2). argc-= optind; argv + = optind; optind = 0;/* Open HCI socket */if (ctl = socket (AF_BLUETOOTH, 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 ++; 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, 1000); 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, main_options, help, and all commands

Int main (int argc, char * argv []) {int opt, ctl, I, cmd = 0; // 1), hciconfig, main_options, help, and all commands; while (opt = getopt_long (argc, argv, "ah", main_options, NULL ))! =-1) {// 1), main_options; switch (opt) {case 'A': all = 1; break; // 2). If it is all, execute the command; case 'H': default: usage (); // 3). If the help command is used to print out the command usage method, exit (0 );}}..................}

1 ),Main_options;
While (opt = getopt_long (argc, argv, "ah", main_options, NULL ))! =-1 ){
Getopt is used to parse the command line option parameter getopt_long, which supports long options. That is, a pair of dashes, a descriptive option name, can also contain a parameter that uses equal signs to connect to the option.

Http://blog.csdn.net/slmmlk2011_2/article/details/7964218.
That is, hciconfig-all

main_optionsstatic struct option main_options[] = {{ "help",0, 0, 'h' },{ "all",0, 0, 'a' },{ 0, 0, 0, 0 }};

2) ops parses the data. If it is a, the Bluetooth information is printed.

case 'a':all = 1;break;

This part is analyzed together with Command Parsing.

3) for the help command, print out the command usage method;

case 'h':default:usage();exit(0);

For help, run 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");printf("Usage:\n""\thciconfig\n""\thciconfig [-a] hciX [command]\n");printf("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, it does not print the command string in the command [] struct: The following two functions are clear at a glance:

The command execution result is as follows:


Static struct {char * cmd; // command, such as hciconfig up; void (* func) (int ctl, int hdev, char * opt); // command execution function; char * opt; // char * doc; // command description} 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"} as an example.

When you use the up command, it will call the following 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 Execution

Main (){.................. Argc-= optind; argv + = optind; optind = 0;/* Open HCI socket * // 1), Open HCI socket communication if (ctl = socket (AF_BLUETOOTH, 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 HCI driver information through ioctl if (ioctl (ctl, HCIGETDEVINFO, (void *) & di) {perror ("Can't get device info"); exit (1) ;}// 3), hci_test_ B It 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, 1000); hci_close_dev (dd) ;}// 4) run the 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) // no command is provided to print print_dev_info (ctl, & di); // 5). close ctl and complete the close (ctl); return 0;} operation ;}

1) enable 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 in use, usually PF_INET, indicating the TCP/IP protocol;
  Type:The parameter specifies the socket type. There are basically three types: data stream socket, datagram socket, and original socket.
  Protocol:The value is usually 0 ".

Bluetooth setup in the program:

Damain: Use AF_BLUETOOTH; idh. code \ 3rdparty \ bluetooth \ Trout_BT \ special \ android \ kernel \ include \ net \ bluetooth. h # define af_rjth31type: bytes, read in Packet, 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: Use the protocol of the Socket to establish a Socket ...... # 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 driver information through ioctl and write the struct hci_dev_info di struct.

If (ioctl (ctl, HCIGETDEVINFO, (void *) & di ))
Definition of commands related to idh. code \ 3rdparty \ bluetooth \ Trout_BT \ special \ android \ kernel \ include \ net \ bluetooth \ hci. h

# Define HCIGETDEVLIST_IOR ('h', 210, int) # define HCIGETDEVINFO_IOR ('h', 211, int) # define HCIGETCONNLIST_IOR ('h', 212, int) # define HCIGETCONNINFO_IOR ('h', 213, int) # define HCIGETAUTHINFO_IOR ('h', 215, int) the data structure of di is static struct hci_dev_info di; struct hci_dev_info {__ b2dev_id; char name [8]; bdaddr_t bdaddr ;__ u32 flags ;__ u8 type ;__ u8 features [8] ;__ u32 pkt_type ;__ u32 link_policy; __u32 link_mode ;__ 2010acl_mtu ;__ 2010acl_pkts ;__ 2010sco_mtu ;__ 2010sco_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, 1000);hci_close_dev(dd);}

Determine the relevant parameters in di.
Hci_test_bitCheck whether the nr-th bit of * addr is 1 (* The right-start detection bit of addr is 0th)

static inline int hci_test_bit(int nr, void *addr){return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));}

4) command execution. This part is the part of command implementation.

While (argc> 0) {for (I = 0; command [I]. cmd; I ++) {if (strncmp (command [I]. cmd, * argv, 5) // compare the second parameter continue through the for loop; if (command [I]. opt) {argc --; argv ++;} command [I]. func (ctl, di. dev_id, * argv); // if the parameter corresponds, run the command function cmd = 1; break;} argc --; argv ++;} if (! Cmd) // no command is provided to print print_dev_info (ctl, & di );

A. For example:If (strncmp (command [I]. cmd, * argv, 5) // compare the second parameter through the for Loop
|Command: hciconfighci0 commands
The value of argv is a commands string, if five characters are equal.

B. If <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqOsvs3WtNDQz + DTprXEw/zB7rqvyv08YnIgLz48cHJlIGNsYXNzPQ = "brush: java;"> command [I]. func (ctl, di. dev_id, * argv ); C. For commands: Corresponds to the array in the command [] struct.

{ "commands",cmd_commands,0,"Display supported commands" },

Command [I]. func = cmd_commands
Corresponding to the commands implementation function cmd_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, 1000) <0) {fprintf (stderr, "Can't read support commands on hci % d: % s (% d) \ n ", hdev, strerror (errno), errno); exit (1) ;} print_dev_hdr (& di); (I = 0; I <64; I ++) {if (! Cmds [I]) continue; printf ("% s Octet %-2d = 0x % 02x (Bit", I? "\ T": "\ tCommands:", I, cmds [I]); for (n = 0; n <8; n ++) if (cmds [I] & (1 <n) printf ("% d", n); printf (") \ n") ;}str = hci_commandstostr (cmds, "\ t", 71); printf ("% s \ n", str); bt_free (str); hci_close_dev (dd );} this is a specific function implemented for each command. // 5) close ctl and complete close (ctl); return 0;

3. Common hciconfig commands:
(1) HELP command: View commands and usage supported by hciconfig
Hciconfig-h or hciconfig -- hlep

(2) view Bluetooth Information

Hciconfig-


Root @ android:/# hciconfig-ahciconfig-ahci0: Type: BR/EDR Bus: UART/Bluetooth interface Type, which is UART, 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 acl: 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 // supports data Packet Link policy: rswitch hold sniff park Link mode: slave accept // connection type, which is the master device, SLAVE Device Name: 'sp8825c1 '// Bluetooth Name Class: 0x5a020c // Bluetooth type Service Classes: Networking, Capturing, Object Transfer, telephony // supported Device Class: Phone, Smart phone HCI Version: 2.1 (0x4) Revision: 0x1250 // HCI Version LMP Version: 2.1 (0x4) subversion: 0x1250 // LMP Manufacturer: Ericsson Technology Licensing (0) // prepared

(3) Start Bluetooth
Hciconfig hci0 up
(4) Disable Bluetooth
Hciconfig hci0 down
(5) view the hci command
Hciconfighci0 commands

(6) Displaying OOB data
Hciconfig hci0 oobdata



Related Article

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.