蓝牙HCI剖析(三)

关键字:bluetooth 蓝牙协议  HCI剖析 HCI概述 HCI笔记 LMP L2CAP SDP RFCOMM 


作者:zhongjun

本着互相学习的目的,来分享此一系列的文章,欢迎转载,请注明作者,尊重版权,谢谢

文章有不当处请指正,共同学习


此系列目前这样打算:

LMP剖析 + 模拟源码:

蓝牙LMP剖析(一)

蓝牙LMP剖析(二)

HCI剖析 + 模拟源码:

蓝牙HCI剖析(一)

蓝牙HCI剖析(二)

L2CAP剖析 + 模拟源码(未完成)
SDP剖析 + 模拟源码(未完成)

rfcomm剖析 + 模拟源码(未完成)


------------------------------------------------------------------------------------------------------------------------------------

华丽分割线,进入正题

------------------------------------------------------------------------------------------------------------------------------------

HCI源码:

bt_hci.h

/*
* This file is part of the HCI protocal.
* Data  :20160506
* Author: zhongjun
*
*/

#ifndef BT_HCI_H_H
#define BT_HCI_H_H

#include "bt_cfg.h"

#ifdef DEBUG_BT_HCI
#define DEBUG(x) {printf x;}
#define BT_HCI_DEBUG(x) DEBUG(x)
#else
#define BT_HCI_DEBUG(x) 
#endif

#define TRANSPORT_UART
#define TRANSPORT_USBx
#define TRANSPORT_SDIOx

#define SCAN_DISABLED		0x00
#define SCAN_INQUIRY		0x01
#define SCAN_PAGE		0x02

/* ACL flags */
#define ACL_CONT		0x01
#define ACL_START		0x02
#define ACL_ACTIVE_BCAST	0x04
#define ACL_PICO_BCAST		0x08

/* CMD/EVT/ACL/SCO types */
#define HCI_COMMAND_PKT		0x01
#define HCI_ACLDATA_PKT		0x02
#define HCI_SCODATA_PKT		0x03
#define HCI_EVENT_PKT		0x04
#define HCI_VENDOR_PKT		0xff

/* HCI Packet types */
#define HCI_2DH1	0x0002
#define HCI_3DH1	0x0004
#define HCI_DM1		0x0008
#define HCI_DH1		0x0010
#define HCI_2DH3	0x0100
#define HCI_3DH3	0x0200
#define HCI_DM3		0x0400
#define HCI_DH3		0x0800
#define HCI_2DH5	0x1000
#define HCI_3DH5	0x2000
#define HCI_DM5		0x4000
#define HCI_DH5		0x8000

#define HCI_HV1		0x0020
#define HCI_HV2		0x0040
#define HCI_HV3		0x0080

#define HCI_EV3		0x0008
#define HCI_EV4		0x0010
#define HCI_EV5		0x0020
#define HCI_2EV3	0x0040
#define HCI_3EV3	0x0080
#define HCI_2EV5	0x0100
#define HCI_3EV5	0x0200

/* HCI Error codes */
#define HCI_UNKNOWN_COMMAND			0x01
#define HCI_NO_CONNECTION			0x02
#define HCI_HARDWARE_FAILURE			0x03
#define HCI_PAGE_TIMEOUT			0x04
#define HCI_AUTHENTICATION_FAILURE		0x05
#define HCI_PIN_OR_KEY_MISSING			0x06
#define HCI_MEMORY_FULL				0x07
#define HCI_CONNECTION_TIMEOUT			0x08
#define HCI_MAX_NUMBER_OF_CONNECTIONS		0x09
#define HCI_MAX_NUMBER_OF_SCO_CONNECTIONS	0x0a
#define HCI_ACL_CONNECTION_EXISTS		0x0b
#define HCI_COMMAND_DISALLOWED			0x0c
#define HCI_REJECTED_LIMITED_RESOURCES		0x0d
#define HCI_REJECTED_SECURITY			0x0e
#define HCI_REJECTED_PERSONAL			0x0f
#define HCI_HOST_TIMEOUT			0x10
#define HCI_UNSUPPORTED_FEATURE			0x11
#define HCI_INVALID_PARAMETERS			0x12
#define HCI_OE_USER_ENDED_CONNECTION		0x13
#define HCI_OE_LOW_RESOURCES			0x14
#define HCI_OE_POWER_OFF			0x15
#define HCI_CONNECTION_TERMINATED		0x16
#define HCI_REPEATED_ATTEMPTS			0x17
#define HCI_PAIRING_NOT_ALLOWED			0x18
#define HCI_UNKNOWN_LMP_PDU			0x19
#define HCI_UNSUPPORTED_REMOTE_FEATURE		0x1a
#define HCI_SCO_OFFSET_REJECTED			0x1b
#define HCI_SCO_INTERVAL_REJECTED		0x1c
#define HCI_AIR_MODE_REJECTED			0x1d
#define HCI_INVALID_LMP_PARAMETERS		0x1e
#define HCI_UNSPECIFIED_ERROR			0x1f
#define HCI_UNSUPPORTED_LMP_PARAMETER_VALUE	0x20
#define HCI_ROLE_CHANGE_NOT_ALLOWED		0x21
#define HCI_LMP_RESPONSE_TIMEOUT		0x22
#define HCI_LMP_ERROR_TRANSACTION_COLLISION	0x23
#define HCI_LMP_PDU_NOT_ALLOWED			0x24
#define HCI_ENCRYPTION_MODE_NOT_ACCEPTED	0x25
#define HCI_UNIT_LINK_KEY_USED			0x26
#define HCI_QOS_NOT_SUPPORTED			0x27
#define HCI_INSTANT_PASSED			0x28
#define HCI_PAIRING_NOT_SUPPORTED		0x29
#define HCI_TRANSACTION_COLLISION		0x2a
#define HCI_QOS_UNACCEPTABLE_PARAMETER		0x2c
#define HCI_QOS_REJECTED			0x2d
#define HCI_CLASSIFICATION_NOT_SUPPORTED	0x2e
#define HCI_INSUFFICIENT_SECURITY		0x2f
#define HCI_PARAMETER_OUT_OF_RANGE		0x30
#define HCI_ROLE_SWITCH_PENDING			0x32
#define HCI_SLOT_VIOLATION			0x34
#define HCI_ROLE_SWITCH_FAILED			0x35
#define HCI_EIR_TOO_LARGE			0x36
#define HCI_SIMPLE_PAIRING_NOT_SUPPORTED	0x37
#define HCI_HOST_BUSY_PAIRING			0x38

/* Command opcode pack/unpack */
#define cmd_opcode_pack(ogf, ocf)	(uint16_t)((ocf & 0x03ff)|(ogf << 10))
#define cmd_opcode_ogf(op)		(op >> 10)
#define cmd_opcode_ocf(op)		(op & 0x03ff)
/* ACL handle and flags pack/unpack */
#define acl_handle_pack(h, f)	(uint16_t)((h & 0x0fff)|(f << 12))
#define acl_get_handle(h)		(h & 0x0fff)
#define acl_get_flags(h)		(h >> 12)


/* -----  HCI Commands ----- */
/* Link Control */
#define OGF_LINK_CTL		0x01
#define OCF_INQUIRY			0x0001
#define OCF_INQUIRY_CANCEL		0x0002
#define OCF_PERIODIC_INQUIRY		0x0003
#define OCF_EXIT_PERIODIC_INQUIRY	0x0004
#define OCF_CREATE_CONN			0x0005
#define OCF_DISCONNECT			0x0006
#define OCF_ADD_SCO			0x0007
#define OCF_CREATE_CONN_CANCEL		0x0008
#define OCF_ACCEPT_CONN_REQ		0x0009
#define OCF_REJECT_CONN_REQ		0x000A
#define OCF_LINK_KEY_REPLY		0x000B
#define OCF_LINK_KEY_NEG_REPLY		0x000C
#define OCF_PIN_CODE_REPLY		0x000D
#define OCF_PIN_CODE_NEG_REPLY		0x000E
#define OCF_SET_CONN_PTYPE		0x000F
#define OCF_AUTH_REQUESTED		0x0011
#define OCF_SET_CONN_ENCRYPT		0x0013
#define OCF_CHANGE_CONN_LINK_KEY	0x0015
#define OCF_MASTER_LINK_KEY		0x0017
#define OCF_REMOTE_NAME_REQ		0x0019
#define OCF_REMOTE_NAME_REQ_CANCEL	0x001A
#define OCF_READ_REMOTE_FEATURES	0x001B
#define OCF_READ_REMOTE_EXT_FEATURES	0x001C
#define OCF_READ_REMOTE_VERSION		0x001D
#define OCF_READ_CLOCK_OFFSET		0x001F
#define OCF_READ_LMP_HANDLE		0x0020
#define OCF_SETUP_SYNC_CONN		0x0028
#define OCF_ACCEPT_SYNC_CONN_REQ	0x0029
#define OCF_REJECT_SYNC_CONN_REQ	0x002A
#define OCF_IO_CAPABILITY_REPLY		0x002B
#define OCF_USER_CONFIRM_REPLY		0x002C
#define OCF_USER_CONFIRM_NEG_REPLY	0x002D
#define OCF_USER_PASSKEY_REPLY		0x002E
#define OCF_USER_PASSKEY_NEG_REPLY	0x002F
#define OCF_REMOTE_OOB_DATA_REPLY	0x0030
#define OCF_REMOTE_OOB_DATA_NEG_REPLY	0x0033
#define OCF_IO_CAPABILITY_NEG_REPLY	0x0034

/* Link Policy */
#define OGF_LINK_POLICY		0x02
#define OCF_HOLD_MODE			0x0001
#define OCF_SNIFF_MODE			0x0003
#define OCF_EXIT_SNIFF_MODE		0x0004
#define OCF_PARK_MODE			0x0005
#define OCF_EXIT_PARK_MODE		0x0006
#define OCF_QOS_SETUP			0x0007
#define OCF_ROLE_DISCOVERY		0x0009
#define OCF_SWITCH_ROLE			0x000B
#define OCF_READ_LINK_POLICY		0x000C
#define OCF_WRITE_LINK_POLICY		0x000D
#define OCF_READ_DEFAULT_LINK_POLICY	0x000E
#define OCF_WRITE_DEFAULT_LINK_POLICY	0x000F
#define OCF_FLOW_SPECIFICATION		0x0010
#define OCF_SNIFF_SUBRATING		0x0011

/* Host Controller and Baseband */
#define OGF_HOST_CTL		0x03
#define OCF_SET_EVENT_MASK		0x0001
#define OCF_RESET			0x0003
#define OCF_SET_EVENT_FLT		0x0005
#define OCF_FLUSH			0x0008
#define OCF_READ_PIN_TYPE		0x0009
#define OCF_WRITE_PIN_TYPE		0x000A
#define OCF_CREATE_NEW_UNIT_KEY		0x000B
#define OCF_READ_STORED_LINK_KEY	0x000D
#define OCF_WRITE_STORED_LINK_KEY	0x0011
#define OCF_DELETE_STORED_LINK_KEY	0x0012
#define OCF_CHANGE_LOCAL_NAME		0x0013
#define OCF_READ_LOCAL_NAME		0x0014
#define OCF_READ_CONN_ACCEPT_TIMEOUT	0x0015
#define OCF_WRITE_CONN_ACCEPT_TIMEOUT	0x0016
#define OCF_READ_PAGE_TIMEOUT		0x0017
#define OCF_WRITE_PAGE_TIMEOUT		0x0018
#define OCF_READ_SCAN_ENABLE		0x0019
#define OCF_WRITE_SCAN_ENABLE		0x001A
#define OCF_READ_PAGE_ACTIVITY		0x001B
#define OCF_WRITE_PAGE_ACTIVITY		0x001C
#define OCF_READ_INQ_ACTIVITY		0x001D
#define OCF_WRITE_INQ_ACTIVITY		0x001E
#define OCF_READ_AUTH_ENABLE		0x001F
#define OCF_WRITE_AUTH_ENABLE		0x0020
#define OCF_READ_ENCRYPT_MODE		0x0021
#define OCF_WRITE_ENCRYPT_MODE		0x0022
#define OCF_READ_CLASS_OF_DEV		0x0023
#define OCF_WRITE_CLASS_OF_DEV		0x0024
#define OCF_READ_VOICE_SETTING		0x0025
#define OCF_WRITE_VOICE_SETTING		0x0026
#define OCF_READ_AUTOMATIC_FLUSH_TIMEOUT	0x0027
#define OCF_WRITE_AUTOMATIC_FLUSH_TIMEOUT	0x0028
#define OCF_READ_NUM_BROADCAST_RETRANS	0x0029
#define OCF_WRITE_NUM_BROADCAST_RETRANS	0x002A
#define OCF_READ_HOLD_MODE_ACTIVITY	0x002B
#define OCF_WRITE_HOLD_MODE_ACTIVITY	0x002C
#define OCF_READ_TRANSMIT_POWER_LEVEL	0x002D
#define OCF_READ_SYNC_FLOW_ENABLE	0x002E
#define OCF_WRITE_SYNC_FLOW_ENABLE	0x002F
#define OCF_SET_CONTROLLER_TO_HOST_FC	0x0031
#define OCF_HOST_BUFFER_SIZE		0x0033
#define OCF_HOST_NUM_COMP_PKTS		0x0035
#define OCF_READ_LINK_SUPERVISION_TIMEOUT	0x0036
#define OCF_WRITE_LINK_SUPERVISION_TIMEOUT	0x0037
#define OCF_READ_NUM_SUPPORTED_IAC	0x0038
#define OCF_READ_CURRENT_IAC_LAP	0x0039
#define OCF_WRITE_CURRENT_IAC_LAP	0x003A
#define OCF_READ_PAGE_SCAN_PERIOD_MODE	0x003B
#define OCF_WRITE_PAGE_SCAN_PERIOD_MODE	0x003C
#define OCF_READ_PAGE_SCAN_MODE		0x003D
#define OCF_WRITE_PAGE_SCAN_MODE	0x003E
#define OCF_SET_AFH_CLASSIFICATION	0x003F
#define OCF_READ_INQUIRY_SCAN_TYPE	0x0042
#define OCF_WRITE_INQUIRY_SCAN_TYPE	0x0043
#define OCF_READ_INQUIRY_MODE		0x0044
#define OCF_WRITE_INQUIRY_MODE		0x0045
#define OCF_READ_PAGE_SCAN_TYPE		0x0046
#define OCF_WRITE_PAGE_SCAN_TYPE	0x0047
#define OCF_READ_AFH_MODE		0x0048
#define OCF_WRITE_AFH_MODE		0x0049
#define OCF_READ_EXT_INQUIRY_RESPONSE	0x0051
#define OCF_WRITE_EXT_INQUIRY_RESPONSE	0x0052
#define OCF_REFRESH_ENCRYPTION_KEY	0x0053
#define OCF_READ_SIMPLE_PAIRING_MODE	0x0055
#define OCF_WRITE_SIMPLE_PAIRING_MODE	0x0056
#define OCF_READ_LOCAL_OOB_DATA		0x0057
#define OCF_READ_INQUIRY_TRANSMIT_POWER_LEVEL	0x0058
#define OCF_WRITE_INQUIRY_TRANSMIT_POWER_LEVEL	0x0059
#define OCF_READ_DEFAULT_ERROR_DATA_REPORTING	0x005A
#define OCF_WRITE_DEFAULT_ERROR_DATA_REPORTING	0x005B
#define OCF_ENHANCED_FLUSH		0x005F
#define OCF_SEND_KEYPRESS_NOTIFY	0x0060

/* Informational Parameters */
#define OGF_INFO_PARAM		0x04
#define OCF_READ_LOCAL_VERSION		0x0001
#define OCF_READ_LOCAL_COMMANDS		0x0002
#define OCF_READ_LOCAL_FEATURES		0x0003
#define OCF_READ_LOCAL_EXT_FEATURES	0x0004
#define OCF_READ_BUFFER_SIZE		0x0005
#define OCF_READ_COUNTRY_CODE		0x0007
#define OCF_READ_BD_ADDR		0x0009

/* Status params */
#define OGF_STATUS_PARAM	0x05
#define OCF_READ_FAILED_CONTACT_COUNTER		0x0001
#define OCF_RESET_FAILED_CONTACT_COUNTER	0x0002
#define OCF_READ_LINK_QUALITY		0x0003
#define OCF_READ_RSSI			0x0005
#define OCF_READ_AFH_MAP		0x0006
#define OCF_READ_CLOCK			0x0007

/* Testing commands */
#define OGF_TESTING_CMD		0x06
#define OCF_READ_LOOPBACK_MODE			0x0001
#define OCF_WRITE_LOOPBACK_MODE			0x0002
#define OCF_ENABLE_DEVICE_UNDER_TEST_MODE	0x0003
#define OCF_WRITE_SIMPLE_PAIRING_DEBUG_MODE	0x0004

/* Vendor specific commands */
#define OGF_VENDOR_CMD		0x3f


/* ---- HCI Events ---- */

#define EVT_INQUIRY_COMPLETE		0x01
#define EVT_INQUIRY_RESULT		0x02
#define EVT_CONN_COMPLETE		0x03
#define EVT_CONN_REQUEST		0x04
#define EVT_DISCONN_COMPLETE		0x05
#define EVT_AUTH_COMPLETE		0x06
#define EVT_REMOTE_NAME_REQ_COMPLETE	0x07
#define EVT_ENCRYPT_CHANGE		0x08
#define EVT_CHANGE_CONN_LINK_KEY_COMPLETE	0x09
#define EVT_MASTER_LINK_KEY_COMPLETE		0x0A
#define EVT_READ_REMOTE_FEATURES_COMPLETE	0x0B
#define EVT_READ_REMOTE_VERSION_COMPLETE	0x0C
#define EVT_QOS_SETUP_COMPLETE		0x0D
#define EVT_CMD_COMPLETE 		0x0E
#define EVT_CMD_STATUS 			0x0F
#define EVT_HARDWARE_ERROR		0x10
#define EVT_FLUSH_OCCURRED		0x11
#define EVT_ROLE_CHANGE			0x12
#define EVT_NUM_COMP_PKTS		0x13
#define EVT_MODE_CHANGE			0x14
#define EVT_RETURN_LINK_KEYS		0x15
#define EVT_PIN_CODE_REQ		0x16
#define EVT_LINK_KEY_REQ		0x17
#define EVT_LINK_KEY_NOTIFY		0x18
#define EVT_LOOPBACK_COMMAND		0x19
#define EVT_DATA_BUFFER_OVERFLOW	0x1A
#define EVT_MAX_SLOTS_CHANGE		0x1B
#define EVT_READ_CLOCK_OFFSET_COMPLETE	0x1C
#define EVT_CONN_PTYPE_CHANGED		0x1D
#define EVT_QOS_VIOLATION		0x1E
#define EVT_PSCAN_REP_MODE_CHANGE	0x20
#define EVT_FLOW_SPEC_COMPLETE		0x21
#define EVT_INQUIRY_RESULT_WITH_RSSI	0x22
#define EVT_READ_REMOTE_EXT_FEATURES_COMPLETE	0x23
#define EVT_SYNC_CONN_COMPLETE		0x2C
#define EVT_SYNC_CONN_CHANGED		0x2D
#define EVT_SNIFF_SUBRATING		0x2E
#define EVT_EXTENDED_INQUIRY_RESULT	0x2F
#define EVT_ENCRYPTION_KEY_REFRESH_COMPLETE	0x30
#define EVT_IO_CAPABILITY_REQUEST	0x31
#define EVT_IO_CAPABILITY_RESPONSE	0x32
#define EVT_USER_CONFIRM_REQUEST	0x33
#define EVT_USER_PASSKEY_REQUEST	0x34
#define EVT_REMOTE_OOB_DATA_REQUEST	0x35
#define EVT_SIMPLE_PAIRING_COMPLETE	0x36
#define EVT_LINK_SUPERVISION_TIMEOUT_CHANGED	0x38
#define EVT_ENHANCED_FLUSH_COMPLETE	0x39
#define EVT_USER_PASSKEY_NOTIFY		0x3B
#define EVT_KEYPRESS_NOTIFY		0x3C
#define EVT_REMOTE_HOST_FEATURES_NOTIFY	0x3D

#define HCI_COMMAND_HDR_SIZE 3
#define HCI_EVENT_HDR_SIZE 3
#define HCI_ACL_HDR_SIZE 4
#define HCI_SCO_HDR_SIZE 3
#pragma pack(1)
/*PDU HDR*/
typedef struct
{
  uint16_t opcode;
  uint8_t para_total_len;
}HCI_Command_HDR_Format;

typedef struct
{
  uint8_t evecode;
  uint8_t para_total_len;
}HCI_Event_HDR_Format;

typedef struct {
  uint16_t handle;		/* Handle & Flags(PB, BC) */
  uint16_t dlen;
}HCI_ACL_HDR_Format;

typedef struct {
 uint16_t handle;
 uint8_t dlen;
}HCI_SCO_HDR_Format;

/*DEATIL COMMAND PDU FORMAT*/
/* Link Control */
typedef struct
{
  HCI_Command_HDR_Format HDR;
  uint8_t lap[3];
  uint8_t inquiry_time;
  uint8_t num_limit;
}COM_Inquiay_Format;

typedef struct
{
  HCI_Command_HDR_Format HDR;
  uint8_t BD_ADDR[6];
  uint16_t pkt_type;
  uint8_t pscan_rep_mode;
  uint8_t reserved;
  uint16_t clock_offset;
  uint8_t role_switch;
}COM_Create_Connect_Format;

typedef struct
{
  HCI_Command_HDR_Format HDR;
  uint16_t con_handle;
  uint8_t reason;
}COM_Disconnect_Format;
/* Link Policy */
/* Host Controller and Baseband */
typedef struct
{
  HCI_Command_HDR_Format HDR;
}COM_Reset_Format;

typedef struct 
{
  uint8_t status;
}Reset_rsp_Format;

typedef struct
{
  HCI_Command_HDR_Format HDR;
  uint8_t scan_enable;
}COM_Write_Scan_Enable_Format;

typedef struct 
{
  uint8_t status;
}Write_Scan_Enable_rsp_Format;
/* Informational Parameters */
typedef struct
{
  HCI_Command_HDR_Format HDR;
}COM_Read_Buffer_Size_Format;

typedef struct
{
  uint8_t status;
  uint16_t acl_mtu;
  uint8_t sco_mtu;
  uint16_t acl_max_pkt;
  uint16_t sco_max_pkt;
}Read_Buffer_Size_rsp_Format;

typedef struct
{
  HCI_Command_HDR_Format HDR;
}COM_Read_BD_ADDR_Format;

typedef struct
{
  uint8_t status;
  uint8_t BD_ADDR[6];
}Read_BD_ADDR_rsp_Format;
/* Status params */
/* Testing commands */
/* Vendor specific commands */
/* Vendor specific commands */

/*DEATIL EVENT PDU FORMAT*/
typedef struct {
  HCI_Event_HDR_Format HDR;
  uint8_t ncmd;
  uint16_t opcode;
}EVT_Com_Complete_HDR_Format;

typedef struct {
  HCI_Event_HDR_Format HDR;
  uint8_t status;
  uint8_t ncmd;
  uint16_t opcode;
}EVT_CMD_Status_Format;

typedef struct {
  HCI_Event_HDR_Format HDR;
  uint8_t ncmd;
  uint8_t bdaddr[6];
  uint8_t page_scan_mode;
  uint8_t reserved[2];
  uint8_t dev_class[3];
  uint16_t clock_offset;
}EVT_CMD_Inquiry_result_Format;

typedef struct {
  HCI_Event_HDR_Format HDR;
  uint8_t status;
  uint16_t con_handle;
  uint8_t bdaddr[6];
  uint8_t link_type;
  uint8_t encryption_enabled;
}EVT_CMD_Connect_complete_Format;

typedef struct {
  HCI_Event_HDR_Format HDR;
  uint8_t status;
  uint16_t con_handle;
  uint8_t reason;
}EVT_CMD_Disconnect_complete_Format;

typedef struct {
  HCI_Event_HDR_Format HDR;
  uint8_t status;
}EVT_CMD_Inquiry_complete_Format;


#pragma pack ()

/*HCI API*/
int hci_reset();
int hci_read_buffer_size();
int hci_write_scan_enable(uint8_t scan_enable);
int hci_read_bd_addr();
int hci_connect_req(uint8_t BD_ADDR[6],uint16_t pkt_type,uint8_t pscan_rep_mode,uint8_t allow_role_switch);
int hci_disconnect(uint16_t con_handle,uint8_t reason);
int hci_inquiry(uint8_t lap[3],uint8_t inquiry_time,uint8_t num_limit);
int hci_send_acl_data(uint16_t handle,uint8_t *acl_data,uint16_t acl_length);
int hci_send_cmd(uint8_t *PDU,uint32_t length);

int hci_receive_pdu(uint8_t *PDU,uint32_t length);
int hci_parse_cmd(uint8_t *PDU,uint32_t length);
int hci_parse_evt(uint8_t *PDU,uint32_t length);
int hci_parse_acl(uint8_t *PDU,uint32_t length);
int hci_parse_sco(uint8_t *PDU,uint32_t length);
int hci_parse_evt_complete(uint8_t *PDU,uint32_t length);
int hci_parse_evt_cmd_status(uint8_t *PDU,uint32_t length);
int hci_parse_evt_inquiry_result(uint8_t *PDU,uint32_t length);
int hci_parse_evt_inquiry_complete(uint8_t *PDU,uint32_t length);
int hci_parse_evt_connect_complete(uint8_t *PDU,uint32_t length);
int hci_parse_evt_disconnect_complete(uint8_t *PDU,uint32_t length);
int hci_parse_reset_rsp(uint8_t *PDU,uint32_t length);
int hci_parse_read_buffer_size_rsp(uint8_t *PDU,uint32_t length);
int hci_parse_write_scan_enable_rsp(uint8_t *PDU,uint32_t length);
int hci_parse_read_BD_ADDR_rsp(uint8_t *PDU,uint32_t length);
#endif

bt_hci.c

#include "bt_hci.h"

int hci_reset()
{
  COM_Reset_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_HOST_CTL,OCF_RESET);
  PDU.HDR.para_total_len = sizeof(COM_Reset_Format) - HCI_COMMAND_HDR_SIZE;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Reset_Format));
}
int hci_read_buffer_size()
{
  COM_Read_Buffer_Size_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_INFO_PARAM,OCF_READ_BUFFER_SIZE);
  PDU.HDR.para_total_len = sizeof(COM_Read_Buffer_Size_Format) - HCI_COMMAND_HDR_SIZE;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Read_Buffer_Size_Format));
}
int hci_write_scan_enable(uint8_t scan_enable)
{
  COM_Write_Scan_Enable_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_HOST_CTL,OCF_WRITE_SCAN_ENABLE);
  PDU.HDR.para_total_len = sizeof(COM_Write_Scan_Enable_Format) - HCI_COMMAND_HDR_SIZE;
  PDU.scan_enable = scan_enable;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Write_Scan_Enable_Format));
}
int hci_read_bd_addr()
{
  COM_Read_BD_ADDR_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_INFO_PARAM,OCF_READ_BD_ADDR);
  PDU.HDR.para_total_len = sizeof(COM_Read_BD_ADDR_Format) - HCI_COMMAND_HDR_SIZE;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Read_BD_ADDR_Format));
}
int hci_connect_req(uint8_t BD_ADDR[6],uint16_t pkt_type,uint8_t pscan_rep_mode,uint8_t allow_role_switch)
{
  COM_Create_Connect_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_LINK_CTL,OCF_CREATE_CONN);
  PDU.HDR.para_total_len = sizeof(COM_Create_Connect_Format) - HCI_COMMAND_HDR_SIZE;
  memcpy(PDU.BD_ADDR,BD_ADDR,6);
  PDU.pkt_type = pkt_type;
  PDU.pscan_rep_mode = pscan_rep_mode;
  PDU.reserved = 0;
  PDU.clock_offset = 0;
  PDU.role_switch = allow_role_switch;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Create_Connect_Format)); 
}
int hci_disconnect(uint16_t con_handle,uint8_t reason)
{
  COM_Disconnect_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_LINK_CTL,OCF_DISCONNECT);
  PDU.HDR.para_total_len = sizeof(COM_Disconnect_Format) - HCI_COMMAND_HDR_SIZE;
  PDU.con_handle = con_handle;
  PDU.reason = reason;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Disconnect_Format)); 
}
int hci_inquiry(uint8_t lap[3],uint8_t inquiry_time,uint8_t num_limit)
{
  COM_Inquiay_Format PDU;
  PDU.HDR.opcode = cmd_opcode_pack(OGF_LINK_CTL,OCF_INQUIRY);
  PDU.HDR.para_total_len = sizeof(COM_Inquiay_Format) - HCI_COMMAND_HDR_SIZE;
  memcpy(PDU.lap,lap,3);
  PDU.inquiry_time = inquiry_time;
  PDU.num_limit = num_limit;
  hci_send_cmd((uint8_t *)&PDU,sizeof(COM_Inquiay_Format));
}
int hci_send_acl_data(uint16_t handle,uint8_t *acl_data,uint16_t acl_length)
{
  
}
int hci_send_cmd(uint8_t *PDU,uint32_t length)
{
  int index = 0;
#if defined(TRANSPORT_UART)
  uint8_t HCI_PDU[length+1];
  HCI_PDU[0] = HCI_COMMAND_PKT;
  memcpy(HCI_PDU+1,PDU,length);
  
  BT_HCI_DEBUG((">> SEDN HCI COMMAND:"));
  for(index = 0;index < length+1;index++)
  {
    BT_HCI_DEBUG(("0x%x ",HCI_PDU[index]));
  }
  BT_HCI_DEBUG(("\n"));
  
  //NEED TO DO PASS THE DATA TO UART PORT
#elif defined(TRANSPORT_USB)
  //TODO
#elif defined(TRANSPORT_SDIO)
  //TODO
#endif
}

int hci_receive_pdu(uint8_t *PDU,uint32_t length)
{
#if defined(TRANSPORT_UART)
  uint8_t pck_type = *PDU;
  switch(pck_type)
  {
    case HCI_COMMAND_PKT:
    {
      BT_HCI_DEBUG(("<< COMMAND:"));
      hci_parse_cmd(PDU+1,length-1);
      break;
    }
    case HCI_ACLDATA_PKT:
    {
      BT_HCI_DEBUG(("<< ACL:"));
      hci_parse_acl(PDU+1,length-1);
      break;
    }
    case HCI_SCODATA_PKT:
    {
      BT_HCI_DEBUG(("<< SCO:"));
      hci_parse_sco(PDU+1,length-1);
      break;
    }
    case HCI_EVENT_PKT:
    {
      BT_HCI_DEBUG(("<< HCI_EVENT_PKT:"));
      hci_parse_evt(PDU+1,length-1);
      break;
    }
    default:
    {
      BT_HCI_DEBUG(("WARNING:UNKNOW PDU\n"));
      break;
    }
  }
#elif defined(TRANSPORT_USB)
    
  //TODO
#elif defined(TRANSPORT_SDIO)
  //TODO
#endif  
}

int hci_parse_cmd(uint8_t *PDU,uint32_t length)
{
  HCI_Command_HDR_Format *COM_PDU = (HCI_Command_HDR_Format *)PDU;
}
int hci_parse_evt(uint8_t *PDU,uint32_t length)
{
  HCI_Event_HDR_Format *EVT_PDU = (HCI_Event_HDR_Format *)PDU;
  switch(EVT_PDU->evecode)
  {
    case EVT_CMD_COMPLETE:
    {
      BT_HCI_DEBUG(("EVT_CMD_COMPLETE "));
      hci_parse_evt_complete(PDU,length);
      break;
    }
    case EVT_CMD_STATUS:
    {
      BT_HCI_DEBUG(("EVT_CMD_STATUS \n"));
      hci_parse_evt_cmd_status(PDU,length);
      break;
    }
    case EVT_INQUIRY_RESULT:
    {
      BT_HCI_DEBUG(("EVT_INQUIRY_RESULT"));
      hci_parse_evt_inquiry_result(PDU,length);
      break;
    }
    case EVT_INQUIRY_COMPLETE:
    {
      BT_HCI_DEBUG(("EVT_INQUIRY_COMPLETE"));
      hci_parse_evt_inquiry_complete(PDU,length);
      break;
    }
    case EVT_CONN_COMPLETE:
    {
      BT_HCI_DEBUG(("EVT_CONN_COMPLETE"));
      hci_parse_evt_connect_complete(PDU,length);
      break;
    }
    case EVT_DISCONN_COMPLETE:
    {
      BT_HCI_DEBUG(("EVT_DISCONN_COMPLETE"));
      hci_parse_evt_disconnect_complete(PDU,length);
      break;
    }
    default:
    {
      int index = 0;
      BT_HCI_DEBUG(("UNKNOW EVENT:"));
      for(index = 0;index < length;index++)
      {
	BT_HCI_DEBUG(("0x%02x ",PDU[index]));
      }
      BT_HCI_DEBUG(("\n"));
      break;
    }
  }
}
int hci_parse_acl(uint8_t *PDU,uint32_t length)
{
  HCI_ACL_HDR_Format *ACL_PDU = (HCI_ACL_HDR_Format *)PDU;
}
int hci_parse_sco(uint8_t *PDU,uint32_t length)
{
  HCI_SCO_HDR_Format *SCO_PDU = (HCI_SCO_HDR_Format *)PDU;
}
int hci_parse_evt_complete(uint8_t *PDU,uint32_t length)
{
  EVT_Com_Complete_HDR_Format *EVT_PDU = (EVT_Com_Complete_HDR_Format*)PDU;
  switch(cmd_opcode_ogf(EVT_PDU->opcode))
  {
    case OGF_LINK_CTL:
    {
      BT_HCI_DEBUG(("OGF_LINK_CTL\n"));
      break;
    }
    case OGF_LINK_POLICY:
    {
      BT_HCI_DEBUG(("OGF_LINK_POLICY\n"));
      break;
    }
    case OGF_HOST_CTL:
    {
      BT_HCI_DEBUG(("OGF_HOST_CTL\n"));
      switch(cmd_opcode_ocf(EVT_PDU->opcode))
      {
	case OCF_RESET:
	{
	  hci_parse_reset_rsp((uint8_t *)EVT_PDU+sizeof(EVT_Com_Complete_HDR_Format),length-sizeof(EVT_Com_Complete_HDR_Format));
	  break;
	}
	case OCF_WRITE_SCAN_ENABLE:
	{
	  hci_parse_write_scan_enable_rsp((uint8_t *)EVT_PDU+sizeof(EVT_Com_Complete_HDR_Format),length-sizeof(EVT_Com_Complete_HDR_Format));
	}
	default:
	{
	  break;
	}
      }
      break;
    }
    case OGF_INFO_PARAM:
    {
      BT_HCI_DEBUG(("OGF_INFO_PARAM\n"));
      switch(cmd_opcode_ocf(EVT_PDU->opcode))
      {
	case OCF_READ_BUFFER_SIZE:
	{
	  hci_parse_read_buffer_size_rsp((uint8_t *)EVT_PDU+sizeof(EVT_Com_Complete_HDR_Format),length-sizeof(EVT_Com_Complete_HDR_Format));
	  break;
	}
	case OCF_READ_BD_ADDR:
	{
	  hci_parse_read_BD_ADDR_rsp((uint8_t *)EVT_PDU+sizeof(EVT_Com_Complete_HDR_Format),length-sizeof(EVT_Com_Complete_HDR_Format));
	}
	default:
	{
	  break;
	}
      }
      break;
    }
    case OGF_STATUS_PARAM:
    {
      BT_HCI_DEBUG(("OGF_STATUS_PARAM\n"));
      break;
    }
    case OGF_TESTING_CMD:
    {
      BT_HCI_DEBUG(("OGF_TESTING_CMD\n"));
      break;
    }
    case OGF_VENDOR_CMD:
    {
      BT_HCI_DEBUG(("OGF_VENDOR_CMD\n"));
      break;
    }
    default:
    {
      break;
    }
  }
   
}
int hci_parse_evt_cmd_status(uint8_t *PDU,uint32_t length)
{
  EVT_CMD_Status_Format *EVT_PDU = (EVT_CMD_Status_Format*)PDU;
  
}
int hci_parse_evt_inquiry_result(uint8_t *PDU,uint32_t length)
{
  int index = 0;
  EVT_CMD_Inquiry_result_Format *EVT_PDU = (EVT_CMD_Inquiry_result_Format*)PDU;
  BT_HCI_DEBUG(("Inquiry rep num %d\n",EVT_PDU->ncmd));
  BT_HCI_DEBUG(("Inquiry ADDR:"));
  for(index = 0;index < 6;index++)
  {
    BT_HCI_DEBUG(("0x%02x ",EVT_PDU->bdaddr[index]));
  }
  BT_HCI_DEBUG(("\n"));
  BT_HCI_DEBUG(("Inquiry page_scan_mode %d\n",EVT_PDU->page_scan_mode));
  BT_HCI_DEBUG(("Inquiry COD:"));
  for(index = 0;index < 3;index++)
  {
    BT_HCI_DEBUG(("0x%02x ",EVT_PDU->dev_class[index]));
  }
  BT_HCI_DEBUG(("\n"));
  BT_HCI_DEBUG(("Inquiry clock_offset %d\n",EVT_PDU->clock_offset));
}
int hci_parse_evt_inquiry_complete(uint8_t *PDU,uint32_t length)
{
  EVT_CMD_Inquiry_complete_Format *EVT_PDU = (EVT_CMD_Inquiry_complete_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",EVT_PDU->status));
}
int hci_parse_evt_connect_complete(uint8_t *PDU,uint32_t length)
{
  EVT_CMD_Connect_complete_Format *EVT_PDU = (EVT_CMD_Connect_complete_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",EVT_PDU->status));
  BT_HCI_DEBUG(("con_handle is %d\n",EVT_PDU->con_handle));
  BT_HCI_DEBUG(("link_type is %d\n",EVT_PDU->link_type));
}
int hci_parse_evt_disconnect_complete(uint8_t *PDU,uint32_t length)
{
  EVT_CMD_Disconnect_complete_Format *EVT_PDU = (EVT_CMD_Disconnect_complete_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",EVT_PDU->status));
  BT_HCI_DEBUG(("con_handle is %d\n",EVT_PDU->con_handle));
  BT_HCI_DEBUG(("reason is %d\n",EVT_PDU->reason));
}
int hci_parse_reset_rsp(uint8_t *PDU,uint32_t length)
{
  Reset_rsp_Format *RSP = (Reset_rsp_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",RSP->status));
}
int hci_parse_read_buffer_size_rsp(uint8_t *PDU,uint32_t length)
{
  Read_Buffer_Size_rsp_Format *RSP = (Read_Buffer_Size_rsp_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",RSP->status));
  BT_HCI_DEBUG(("acl_mtu is %d\n",RSP->acl_mtu));
  BT_HCI_DEBUG(("sco_mtu is %d\n",RSP->sco_mtu));
  BT_HCI_DEBUG(("acl_max_pkt is %d\n",RSP->acl_max_pkt));
  BT_HCI_DEBUG(("sco_max_pkt is %d\n",RSP->sco_max_pkt));
}
int hci_parse_write_scan_enable_rsp(uint8_t *PDU,uint32_t length)
{
  Write_Scan_Enable_rsp_Format *RSP = (Write_Scan_Enable_rsp_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",RSP->status));
}
int hci_parse_read_BD_ADDR_rsp(uint8_t *PDU,uint32_t length)
{
  int index = 0;
  Read_BD_ADDR_rsp_Format *RSP = (Read_BD_ADDR_rsp_Format *)PDU;
  BT_HCI_DEBUG(("status is %d\n",RSP->status));
  BT_HCI_DEBUG(("ADDR:"));
  for(index = 0;index<6;index++)
  {
    BT_HCI_DEBUG(("0x%02x ",(RSP->BD_ADDR)[index]));
  }
  BT_HCI_DEBUG(("\n"));
}

bt_cfg.h

#ifndef BT_HCI_CFG_H
#define BT_HCI_CFG_H

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define DEBUG_BT_HCI

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;


//typedef  char int8_t;
typedef  short int16_t;
typedef  int int32_t;
typedef long long int64_t;

#undef NULL 
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif

#endif

main.c

#include <stdio.h>
#include "bt_hci.h"
#include "memory_pool.h"

void HDR_PAR_UNPAR_Test()
{
  uint16_t Reset_opcode_2 = 0x0c03; 
  uint16_t Read_Buffer_Size = 0x1005;
  uint16_t acl_header = 0x204d;
  uint16_t handle = 0x004d;
  uint8_t flag = ACL_START;
  
  printf("COMMAND HDR SIZE is %d\n",sizeof(HCI_Command_HDR_Format));
  printf("EVENT HDR SIZE is %d\n",sizeof(HCI_Event_HDR_Format));
  printf("ACL HDR SIZE is %d\n",sizeof(HCI_ACL_HDR_Format));
  printf("SCO HDR SIZE is %d\n",sizeof(HCI_SCO_HDR_Format));
  
  printf("OGF is %d\n",cmd_opcode_ogf(Read_Buffer_Size));
  printf("OCF is %d\n",cmd_opcode_ocf(Read_Buffer_Size));
  printf("Rset opcode is 0x%04x\n",cmd_opcode_pack(3,3));
  
  printf("HANDLE is 0x%04x\n",acl_get_handle(acl_header));
  printf("FLAG is %d\n",acl_get_flags(acl_header));
  printf("ACL HDR is 0x%04x\n",acl_handle_pack(handle,flag));
}
int main()
{
  BT_HCI_DEBUG(("HCI START\n"));
  uint8_t BD_ADDR[6] = {0x1,0x1,0x1,0x1,0x1,0x1};
  uint8_t inquiry_access[3] = {0x9e,0x8b,0x33};
  uint8_t receive_reset_complete_evt[7] = {0x4,0xe,0x4,0x1,0x3,0xc,0x0};
  uint8_t receive_read_buffer_size_complete_evt[14] = {0x4,0xe,0xb,0x1,0x5,0x10,0x0,0x80,0x01,0x40,0x04,0x00,0x08,0x00};
  uint8_t receive_write_scan_enable_complete_evt[7] = {0x4,0xe,0x4,0x1,0x1a,0xc,0x0};
  uint8_t receive_read_BD_ADDR_complete_evt[13] = {0x04,0x0e,0x0a,0x1,0x9,0x10,0x00,0x4e,0xa8,0x00,0x4e,0x49,0x4a};
  uint8_t receive_cmd_status_evt_inquiry[7] = {0x4,0xf,0x4,0x0,0x1,0x1,0x4};
  uint8_t receive_inquiry_result_evt[18] = {0x4,0x2,0xf,0x1,0x1,0x01,0x1,0x1,0x1,0x1,0x1,0x0,0x0,0x1,0x52,0x2e,0x0,0x0};
  uint8_t receive_inquiry_complete_evt[4] = {0x4,0x1,0x1,0x0};
  uint8_t receive_cmd_status_evt_connect[7] = {0x4,0xf,0x4,0x0,0x1,0x5,0x4};
  uint8_t receive_create_connect_complete_evt[14] = {0x4,0x3,0xb,0x0,0x29,0x0,0x5f,0x21,0xa3,0x60,0x10,0x0,0x1,0x0};
  uint8_t receive_cmd_status_evt_disconnect[7] = {0x4,0xf,0x4,0x0,0x1,0x6,0x4};
  uint8_t receive_cmd_disconnect_complete_evt[7] = {0x4,0x5,0x4,0x0,0x29,0x0,0x16};
  HDR_PAR_UNPAR_Test();
  
  /*HCI TEST*/
  hci_reset();
  hci_receive_pdu(receive_reset_complete_evt,sizeof(receive_reset_complete_evt));
  
  hci_read_buffer_size();
  hci_receive_pdu(receive_read_buffer_size_complete_evt,sizeof(receive_read_buffer_size_complete_evt));
  
  hci_write_scan_enable(SCAN_INQUIRY|SCAN_PAGE);
  hci_receive_pdu(receive_write_scan_enable_complete_evt,sizeof(receive_write_scan_enable_complete_evt));
  
  hci_read_bd_addr();
  hci_receive_pdu(receive_read_BD_ADDR_complete_evt,sizeof(receive_read_BD_ADDR_complete_evt));
  
  hci_inquiry(inquiry_access,6,1);
  hci_receive_pdu(receive_cmd_status_evt_inquiry,sizeof(receive_cmd_status_evt_inquiry));
  hci_receive_pdu(receive_inquiry_result_evt,sizeof(receive_inquiry_result_evt));
  hci_receive_pdu(receive_inquiry_complete_evt,sizeof(receive_inquiry_complete_evt));
  
  hci_connect_req(BD_ADDR,HCI_DM5|HCI_DH5|HCI_DM3|HCI_DH3|HCI_DH1|HCI_DM1,0,0);
  hci_receive_pdu(receive_cmd_status_evt_connect,sizeof(receive_cmd_status_evt_connect));
  hci_receive_pdu(receive_create_connect_complete_evt,sizeof(receive_create_connect_complete_evt));
  
  hci_disconnect(0x0029,0x13);
  hci_receive_pdu(receive_cmd_status_evt_disconnect,sizeof(receive_cmd_status_evt_disconnect));
  hci_receive_pdu(receive_cmd_disconnect_complete_evt,sizeof(receive_cmd_disconnect_complete_evt));
}

makefile

objects = bt_hci.o main.o memory_pool.o
LMP : $(objects)
	cc $(objects) -o HCI
main.o : main.c 
bt_lmp.o : bt_hci.c bt_cfg.h bt_hci.h
memory_pool.o : memory_pool.h memory_pool.c 
clean :
	rm HCI $(objects)


你可能感兴趣的:(BlueTooth,蓝牙协议,HCI概述,HCI剖析,HCI笔记)