Chinaunix首页 | 论坛 | 博客
  • 博客访问: 310025
  • 博文数量: 92
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 960
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-21 19:38






分类: 嵌入式

2009-09-19 20:35:27

1. Open an HCI Socket --- int hci_open_dev (int dev_id):
This function is used to open an HCI Socket. It first opened an HCI protocol of the Socket (room), and the Socket and the device ID = parameter dev_id bind up the Dongle. Only bind, it will be Socket handle only correspond with the Dongle.
Note that all of the HCI Command before sending, we need to use hci_open_dev opened and binding.
2. Close an HCI Socket: --- int hci_close_dev (int dd)
 simple closure of the use of hci_open_dev open Socket.
3. To the HCI Socket (corresponding to a Dongle) send the request:
int hci_send_req (int dd, struct hci_request * r, int to)
BlueZ provides this function which is very useful, it can achieve that all Host can send Command function to Modules.
Parameter 1: HCI Socket.
Parameter 2: the content of Command.
Parameter 3: milliseconds as the unit of timeout.
The following is the explanation of this function and usage:
When an application needs to Dongle (corresponds to a bind after the Socket) to send Command, the call to this function.
Among them, one parameter corresponds to a dd using hci_open_dev () to open a Socket (Dongle).
Parameters, compared with three to wait for Dongle results of the implementation of and respond to the command timeout. In milliseconds.
Parameter 2 hci_request * r is the most important, first of all to look at its structure:
struct hci_request (
  uint16_t ogf; / / Opcode Group
  uint16_t ocf; / / Opcode Command
  int event; / / This Command generated by Event Type.
  void * cparam; / / Command Parameters
  int clen; / / Command Parameter Length
  void * rparam; / / Response Parameters
  int rlen; / / Response length parameter
ogf, ocf Needless to say, they are
Group Code the Command Code. The two first determined, then you can check HCI Spec. View input parameters (cparam) and output parameters (rparam) meaning. As to their structure and parameters of length, then the ~ / include / net / bluetooth / hci.h have defined.
As for the event. If set, it will be setsockopt installed in Socket.
Example 1: get a connection Policy Setting.
HCI Spec as well as ~ / include / net / bluetooth / hci.h can all see, OGF = OGF_LINK_POLICY (0x02). OCF = OCF_READ_LINK_POLICY (0x0C).
Because of this Command is used to read an ACL connection Policy Setting. So, that is, the input parameters for this connection Handle.
Return parameter contains three parts, status (Command is the smooth implementation), handle (connected Handle). policy (to be the policy value)
It also introduces a new problem, how to get an ACL connection Handle.
You can use ioctl HCIGETCONNINFO get ACL connection Handle.
ioctl (dd, HCIGETCONNINFO, (unsigned long) cr);
Connect_handle = htobs (cr-> conn_info-> handle);
Therefore, the complete process is as follows:
struct hci_request HCI_Request;
  read_link_policy_cp Command_Param;
  read_link_policy_rp Response_Param;
/ / 1. Get ACL Connect Handle
if (ioctl (dd, HCIGETCONNINFO, (unsigned long) cr) <0)
   return -1;
  Connect_handle = htobs (cr-> conn_info-> handle);
memset (& HCI_Request, 0, sizeof (HCI_Request));
  memset (& Command_Param, 0, sizeof (Command_Param));
  memset (& Response_Param, 0, sizeof (Response_Param));
/ / 2. Fill Command input parameters
  Command_Param.handle = Connect_handle;
HCI_Request.ogf = OGF_LINK_POLICY; / / Command Unit ID
  HCI_Request.ocf = OCF_READ_LINK_POLICY; / / Command ID
  HCI_Request.cparam = & Command_Param;
  HCI_Request.rparam = & Response_Param;
if (hci_send_req (dd, & HCI_Request, to) <0)
   perror ( "\ nhci_send_req ()");
   return -1;
/ / If the state does not return value
  if (Response_Param.status) (
   return -1;
/ / Get the current policy
  * policy = Response_Param.policy;
4: a few more basic function:
static inline void bacpy (bdaddr_t * dst, const bdaddr_t * src) / / bdaddr copy

static inline int bacmp (const bdaddr_t * ba1, const bdaddr_t * ba2) / / bdaddr Comparison
5: to get designated Dongle BDAddr:
int hci_read_bd_addr (int dd, bdaddr_t * bdaddr, int to);
Parameter 1: HCI Socket, use hci_open_dev () to open a Socket (Dongle).
Parameter 2: Output parameters, which will be placed bdaddr.
Parameter 3: milliseconds as the unit timeout.
6: read and write Dongle Name:
int hci_read_local_name (int dd, int len, char * name, int to)
int hci_write_local_name (int dd, const char * name, int to)
Parameter 1: HCI Socket, use hci_open_dev () to open a Socket (Dongle).
Parameter 2: read or set the Name.
Parameter 3: milliseconds as the unit timeout.
Note: Name and IOCTL HCIGETDEVINFO here are different hci_dev_info in the name.
7: Get HCI Version:
int hci_read_local_version (int dd, struct hci_version * ver, int to)
8: Get UP's Dongle BDaddr: +-
int hci_devba (int dev_id, bdaddr_t * bdaddr);

dev_id: Dongle Device ID.
bdaddr: output parameter to specify Dongle if the UP, then placed his BDAddr.
9: Get Dongle Info:
int hci_devinfo (int dev_id, struct hci_dev_info * di)
dev_id: Dongle Device ID.
di: The Dongle information.
Error returns -1.
Note that this Function approach is fully consistent with the 3.0 method.
10: Get X from the hciX:
int hci_devid (const char * str)

str: like hci0 this string.
If the hciX corresponding Device ID (X) is real and UP. This device is returned Device ID.
11: Get BDADDR parameters bdaddr is not equal to the Dongle Device ID:
int hci_get_route (bdaddr_t * bdaddr)

Find Dongle, found Dongle Bdaddr parameters bdaddr is not equal to the first one Dongle, then return to this Dongle Device ID.
So, if: int hci_get_route (NULL), then get the first available Dongle Device ID.
12: Convert BDADDR to a string:
int ba2str (const bdaddr_t * ba, char * str)
13: Convert a string to BDADDR:
int str2ba (const char * str, bdaddr_t * ba)


阅读(1858) | 评论(0) | 转发(0) |