蓝牙LMP剖析(二)

以下是 bt_lmp.h 源码:

/*
* This file is part of the LMP protocal.
* Data  :20160423
* Author: zhongjun
*
*/

#ifndef BT_LMP_H_H
#define BT_LMP_H_H

#include "bt_cfg.h"

#ifdef DEBUG_BT_LMP
#define DEBUG(x) {printf x;}
#define BT_LMP_DEBUG(x) DEBUG(x)
#else
#define BT_LMP_DEBUG(x) 
#endif

/*HDR INDEX*/
#define CMD_F_POS	0
#define CMD_S_POS	1

/*HDR SIZE*/
#define PDU_S_LENGTH 1
#define PDU_B_LENGTH 2

/*HDR DETAIL*/
#define CMD_TID_MASK	0x01
#define CMD_OPC_S_MASK	0xFE

/*FEATURE MASK*/
//0 index byte 0-7bit
#define three_slot_packets_mask 0x1
#define five_slot_packets_mask  0x2
#define Encryption_mask 0x4
#define Slot_offset_mask 0x8
#define Timing_accuracy_mask 0x10
#define Hold_mode_mask 0x40
#define Sniff_mode_mask 0x80
//1 index byte 0-7bit
#define Park_state_mask 0x100
#define Power_control_requests_mask  0x200
#define CQDDR_mask 0x400
#define SCO_link_mask 0x800
#define HV2_packets_mask 0x1000
#define HV3_packets_mask 0x2000
#define u_law_log_synchronous_data_mask 0x4000
#define A_law_log_synchronous_data_mask 0x8000
//2 index byte 0-7bit
#define CVSD_synchronous_data_mask 0x10000
#define Paging_parameter_negotiation_mask  0x20000
#define Power_control_mask 0x40000
#define Transparent_synchronous_data_mask 0x80000
#define Flow_control_least_lag_mask 0x100000
#define Flow_control_middle_lag_mask 0x200000
#define Flow_control_most_lag_mask 0x400000
#define Broadcast_Encryption_mask 0x800000
//3 index byte 0-7bit
#define Reserved_3_0_mask 0x1000000
#define EDR_ACL_2_M_mask  0x2000000
#define EDR_ACL_3_M_mask 0x4000000
#define Enhanced_inquiry_scan_mask 0x8000000
#define Interlaced_inquiry_scan_mask 0x10000000
#define Interlaced_page_scan_mask 0x20000000
#define RSSI_with_inquiry_results_mask 0x40000000
#define Extended_SCO_link_EV3_mask 0x80000000
//4 index byte 0-7bit
#define EV4_packets_mask 0x100000000
#define EV5_packets_mask  0x200000000
#define Reserved_4_2_mask 0x400000000
#define AFH_capable_slave_mask 0x800000000
#define AFH_classification_slave_mask  0x1000000000
#define BR_EDR_Not_Supported_mask 0x2000000000
#define LE_Supported_mask 0x4000000000
#define three_slot_EDR_ACL_mask 0x8000000000
//5 index byte 0-7bit
#define five_slot_EDR_ACL_mask 0x10000000000
#define Sniff_subrating_mask  0x20000000000
#define Pause_encryption_mask 0x40000000000
#define AFH_capable_master_mask 0x80000000000
#define AFH_classification_master_mask  0x100000000000
#define EDR_eSCO_2_M_mask 0x200000000000
#define EDR_eSCO_3_M_mask 0x400000000000
#define three_slot_EDR_eSCO_mask 0x800000000000
//6 index byte 0-7bit
#define Extended_Inquiry_Response_mask 0x1000000000000
#define Simultaneous_LE_and_BR_EDR_to_Same_Device_Capable_mask  0x2000000000000
#define Reserved_6_2_mask 0x4000000000000
#define Secure_Simple_Pairing_mask 0x8000000000000
#define Encapsulated_PDU_mask  0x10000000000000
#define Erroneous_Data_Reporting_mask 0x20000000000000
#define Non_flushable_Packet_Boundary_Flag_mask 0x40000000000000
#define Reserved_6_7_mask 0x80000000000000
//7 index byte 0-7bit
#define Link_Supervision_Timeout_Changed_Event_mask 0x1000000000000
#define Inquiry_TX_Power_Level_mask  0x2000000000000
#define Enhanced_Power_Control_mask 0x4000000000000
#define Reserved_7_3_mask 0x8000000000000
#define Reserved_7_4_mask  0x10000000000000
#define Reserved_7_5_mask 0x20000000000000
#define Reserved_7_6_mask 0x40000000000000
#define Extended_features_mask 0x80000000000000
//FEATURE SUPPORT
#define LMP_FEATURE_SUPPORT(src_feature,des_feature) ((src_feature & des_feature) == des_feature)

/* PDU HDR PARSE*/
#define LMP_GET_TID(buf)         	(((uint8_t *)buf)[CMD_F_POS] & CMD_TID_MASK)
#define LMP_GET_S_OPC(buf)         	((((uint8_t *)buf)[CMD_F_POS] & CMD_OPC_S_MASK) >> 1)
#define LMP_SET_S_HDR(buf,role,opc)	(((uint8_t *)buf)[CMD_F_POS]  = ((opc << 1) | role)) 

/*PDU HDR SIZE*/
typedef uint8_t LMP_PDU_HDR_S;
typedef uint8_t LMP_PDU_HDR_B[2];

/*ROLE*/
typedef enum 
{
  LMP_MASTER_ROLE = 0,
  LMP_SLAVE_ROLE = 1,
}LMP_ROLE;

/* ACCEPT RESULT*/
typedef enum 
{
  LMP_ACCEPT = 0,
  LMP_NOT_ACCEPT = 1,
}LMP_ACCEPT_RESULT;

/*VERSION DEFINE*/
typedef enum
{
  LMP_VERSION_4_0 = 0x6,
  LMP_RESERVED = 0x7,
}LMP_VERSION_DEF;

typedef enum
{
  BROADCOM = 0xF000,
  TI = 0xD000,
}LMP_COM_ID_DEF;

typedef enum
{
  LMP_SBUVERSION = 0x0,
}LMP_SUBVERSION_DEF;

/*PDU ID*/
typedef enum 
{
  LMP_name_req = 1,
  LMP_name_rsp = 2,
  LMP_accepted = 3,
  LMP_not_accepted = 4,
  LMP_clkoffset_req = 5,
  LMP_clkoffset_rsp = 6,
  LMP_detach = 7,
  LMP_in_rand = 8,
  LMP_comb_key = 9,
  LMP_au_rand = 11,
  LMP_sres = 12,
  LMP_switch_req = 19,
  LMP_hold = 20,
  LMP_hold_req = 21,
  LMP_sniff_req = 23,
  LMP_park_req = 25,
  LMP_incr_power_req = 31,
  LMP_decr_power_req = 32,
  LMP_max_power = 33,
  LMP_min_power = 34,
  LMP_auto_rate = 35,
  LMP_preferred_rate = 36,
  LMP_version_req = 37,
  LMP_version_rsp = 38,
  LMP_features_req = 39,
  LMP_features_rsp = 40,
  
  LMP_quality_of_service = 41,
  LMP_quality_of_service_req = 42,
  LMP_SCO_link_req = 43,
  LMP_remove_SCO_link_req = 44,
  LMP_max_slot = 45,
  LMP_max_slot_req = 46,

  LMP_setup_complete = 49,
  LMP_host_connection_req = 51,
  LMP_slot_offset = 52,
  LMP_page_mode_req = 53,
  LMP_page_scan_mode_req = 54,
  LMP_supervision_timeout = 55,
  LMP_set_AFH = 60,
  LMP_Simple_Pairing_Confirm = 63,
  LMP_Simple_Pairing_Number = 64,
}LMP_OPCODE_ID_S;

/*DETAIL PDU FORMAT*/
#pragma pack (1)
typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t name_offset;
}LMP_name_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t name_offset;
  uint8_t name_length;
  uint8_t name[14];
}LMP_name_rsp_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint64_t feature;
}LMP_features_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint64_t feature;
}LMP_features_rsp_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
}LMP_host_connection_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t opcode;
}LMP_accepted_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t opcode;
  uint8_t errcode;
}LMP_not_accepted_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t versnr;
  uint16_t compid;
  uint16_t subvernr;
}LMP_version_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t versnr;
  uint16_t compid;
  uint16_t subvernr;
}LMP_version_rsp_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint32_t AFH_instant;
  uint8_t AFH_mode;
  uint8_t AFH_channel_map[10];
}LMP_set_AFH_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
}LMP_setup_complete_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t random_nub[16];
}LMP_in_rand_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t random_nub[16];
}LMP_comb_key_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t random_nub[16];
}LMP_au_rand_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t auth_rsp[4];
}LMP_sres_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
}LMP_auto_rate_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
}LMP_clkoffset_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t clock_offset;
}LMP_clkoffset_rsp_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t reserved;
}LMP_incr_power_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t reserved;
}LMP_decr_power_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
}LMP_max_power_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
}LMP_min_power_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t errcode;
}LMP_detach_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t hold_time;
  uint32_t hold_instant;
}LMP_hold_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t hold_time;
  uint32_t hold_instant;
}LMP_hold_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t max_slot;
}LMP_max_slot_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t max_slot;
}LMP_max_slot_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t paging_scheme;
  uint8_t paging_scheme_settings;
}LMP_page_mode_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t paging_scheme;
  uint8_t paging_scheme_settings;
}LMP_page_scan_mode_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t timing_control_flags;
  uint16_t DB;
  uint16_t TB;
  uint16_t NB;
  uint16_t xB;
  uint8_t PM_ADDR;
  uint8_t AR_ADDR;
  uint8_t NBsleep;
  uint8_t DBsleep;
  uint8_t Daccess;
  uint8_t Taccess;
  uint8_t Nacc_slots;
  uint8_t Npol;
  uint8_t Maccess:4;
  uint8_t access_scheme:4; 
}LMP_park_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t data_rate;
}LMP_preferred_rate_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t poll_interval;
  uint8_t NBC;
}LMP_quality_of_service_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t poll_interval;
  uint8_t NBC;
}LMP_quality_of_service_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t SCO_handle;
  uint8_t errcode;
}LMP_remove_SCO_link_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t SCO_handle;
  uint8_t timing_control_flags;
  uint8_t Dsco;
  uint8_t Tsco;
  uint8_t SCO_packe;
  uint8_t air_mode;
}LMP_SCO_link_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t Commitment_Value[16]; 
}LMP_Simple_Pairing_Confirm_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t Nonce_Value[16]; 
}LMP_Simple_Pairing_Number_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint8_t timing_control_flags;
  uint16_t Dsniff;
  uint16_t Tsniffe;
  uint16_t sniff_attempt;
  uint16_t sniff_timeout;
}LMP_sniff_req_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t slot_offset;
  uint8_t BD_ADDR[6];
}LMP_slot_offset_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint16_t supervision_timeout;
}LMP_supervision_timeout_format;

typedef struct
{
  LMP_PDU_HDR_S hdr;
  uint32_t switch_instant;
}LMP_switch_req_format;

typedef struct
{
  uint8_t has_commite_setup_complete;
  uint8_t local_name[14];
  uint8_t remote_name[14];
  uint8_t K_init[16];
  uint8_t link_key[16];
  uint8_t local_role;
  uint8_t remote_role;
  uint64_t local_feature; 
  uint64_t remote_feature; 
  
  uint8_t AFH_mode;
  uint32_t local_clock;
  uint32_t remote_clock;
  uint32_t offset;
  uint8_t CQDDR;
  uint8_t max_power;
  uint8_t min_power;
  uint8_t now_power;
  
  uint8_t hold_status;
  uint16_t hold_time;
  uint32_t hold_instant;
  
  uint8_t max_slot;
}LMP_Core;
#pragma pack ()


int LMP_Init(uint8_t role,uint8_t local_name[14],uint64_t local_feature,uint32_t local_clock,uint8_t min_power,uint8_t max_power,uint8_t default_power);
/*send PDU_ID */
int LMP_Send_Feature_req(uint8_t role,uint64_t feature);
int LMP_Send_Feature_rsp(uint8_t role,uint64_t feature);
int LMP_Send_host_connection_req(uint8_t role);
int LMP_Send_accepted(uint8_t role,uint8_t accept,uint8_t opcode);
int LMP_Send_not_accepted(uint8_t role,uint8_t accept,uint8_t opcode,uint8_t errcode);
int LMP_Send_LMP_version_req(uint8_t role,uint8_t vernr,uint16_t comid,uint16_t subvernr);
int LMP_Send_LMP_version_rsp(uint8_t role,uint8_t vernr,uint16_t comid,uint16_t subvernr);
int LMP_Send_set_AFH(uint8_t role,uint32_t AFH_instant,uint8_t AFH_mode,uint8_t AFH_channel_map[10]);
int LMP_Send_setup_complete(uint8_t role);
int LMP_Send_in_rand(uint8_t role,uint8_t random[16]);
int LMP_Send_comb_key(uint8_t role,uint8_t random[16]);
int LMP_Send_au_rand(uint8_t role,uint8_t random[16]);
int LMP_Send_sres(uint8_t role,uint8_t auth_rsp[4]);
int LMP_Send_auto_rate(uint8_t role);
int LMP_Send_clkoffset_req(uint8_t role);
int LMP_Send_clkoffset_rsp(uint8_t role,uint16_t clock_offset);
int LMP_Send_incr_power_req(uint8_t role);
int LMP_Send_decr_power_req(uint8_t role);
int LMP_Send_min_power(uint8_t role);
int LMP_Send_max_power(uint8_t role);
int LMP_Send_detach(uint8_t role,uint8_t errcode);
int LMP_Send_hold(uint8_t role,uint16_t hold_time,uint32_t hold_instant);
int LMP_Send_hold_req(uint8_t role,uint16_t hold_time,uint32_t hold_instant);
int LMP_Send_max_slot(uint8_t role,uint8_t max_slot);
int LMP_Send_max_slot_req(uint8_t role,uint8_t max_slot);
int LMP_Send_name_req(uint8_t role,uint8_t name_offset);
int LMP_Send_name_rsp(uint8_t role,uint8_t name_offset,uint8_t name_length,uint8_t name[14]);
int LMP_Send_page_mode_req(uint8_t role,uint8_t paging_scheme,uint8_t paging_scheme_settings);
int LMP_Send_page_scan_mode_req(uint8_t role,uint8_t paging_scheme,uint8_t paging_scheme_settings);
int LMP_Send_preferred_rate(uint8_t role,uint8_t data_rate);
int LMP_Send_quality_of_service(uint8_t role,uint16_t poll_interval,uint8_t NBC);
int LMP_Send_quality_of_service_req(uint8_t role,uint16_t poll_interval,uint8_t NBC);
int LMP_Send_remove_SCO_link_req(uint8_t role,uint8_t SCO_handle,uint8_t errcode);
int LMP_Send_SCO_link_req(uint8_t role,uint8_t SCO_handle,uint8_t timing_control_flags,uint8_t Dsco,uint8_t Tsco,uint8_t SCO_packe,uint8_t air_mode);
int LMP_Send_Simple_Pairing_Confirm(uint8_t role,uint8_t Commitment_Value[16]);
int LMP_Send_Simple_Pairing_Number(uint8_t role,uint8_t Nonce_Value[16]);
int LMP_Send_slot_offset(uint8_t role,uint16_t slot_offset,uint8_t BD_ADDR[6]);
int LMP_Send_sniff_req(uint8_t role,uint8_t timing_control_flags,uint16_t Dsniff,uint16_t Tsniffe,uint16_t sniff_attempt,uint16_t sniff_timeout);
int LMP_Send_supervision_timeout(uint8_t role,uint16_t supervision_timeout);
int LMP_Send_switch_req(uint8_t role,uint32_t switch_instant);
  
  
  
  
int LMP_Send_PDU(uint8_t *PDU,uint32_t length);

/*parse PDU*/
int LMP_Parse_PDU(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_Feature_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_Feature_rsp(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_host_connection_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_accepted(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_not_accepted(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_version_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_version_rsp(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_set_AFH(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_in_rand(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_comb_key(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_au_rand(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_auto_rate(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_clkoffset_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_clkoffset_rsp(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_incr_power_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_decr_power_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_detach(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_hold(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_hold_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_max_slot(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_max_slot_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_name_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_name_rsq(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_page_mode_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_page_scan_mode_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_preferred_rate(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_quality_of_service(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_quality_of_service_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_remove_SCO_link_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_SCO_link_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_Simple_Pairing_Confirm(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_Simple_Pairing_Number(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_slot_offset(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_sniff_req(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_supervision_timeout(uint8_t *PDU,uint32_t length);
int LMP_Parse_Send_switch_req(uint8_t *PDU,uint32_t length);



#endif

以下是 bt_lmp.c 源码:

#include "bt_lmp.h"


LMP_Core *LMP_Core_M;
int LMP_Init(uint8_t role,uint8_t local_name[14],uint64_t local_feature,uint32_t local_clock,uint8_t min_power,uint8_t max_power,uint8_t default_power)
{
  if(LMP_Core_M == NULL)
  {
    LMP_Core_M = malloc(sizeof(LMP_Core));
  }
  else
  {
    BT_LMP_DEBUG(("WARNING:LMP_Core_M already has malloc memory\n"));
    return -1;
  }
  
  if(LMP_Core_M == NULL)
  {
    BT_LMP_DEBUG(("WARING:LMP INIT FAULT\n"));
    return -1;
  }
  memset(LMP_Core_M,0,sizeof(LMP_Core));
  
  LMP_Core_M->has_commite_setup_complete = 0;
  memset(LMP_Core_M->K_init,0,16);
  memset(LMP_Core_M->link_key,0,16);
  LMP_Core_M->local_role = role;
  LMP_Core_M->local_feature = local_feature;
  
  LMP_Core_M->AFH_mode = 0;
  LMP_Core_M->local_clock = local_clock;
  LMP_Core_M->remote_clock = 0;
  LMP_Core_M->offset = 0;  
  LMP_Core_M->CQDDR = 0;
  LMP_Core_M->min_power = min_power;
  LMP_Core_M->max_power = max_power;
  LMP_Core_M->now_power = default_power;
  LMP_Core_M->hold_status = 0;
  LMP_Core_M->hold_time = 0;
  LMP_Core_M->hold_instant = 0;
  LMP_Core_M->max_slot = 0;
  memcpy(LMP_Core_M->local_name,local_name,14);

}


/* SEND PDU*/
int LMP_Send_Feature_req(uint8_t role,uint64_t feature)
{
  LMP_features_req_format PDU;
  memset(&PDU,0,sizeof(LMP_features_req_format));

  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_features_req);
  PDU.feature = feature;

  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_features_req_format));
}
int LMP_Send_Feature_rsp(uint8_t role,uint64_t feature)
{
  LMP_features_rsp_format PDU;
  memset(&PDU,0,sizeof(LMP_features_rsp_format));

  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_features_rsp);
  PDU.feature = feature;

  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_features_rsp));
}
int LMP_Send_host_connection_req(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_host_connection_req\n"));
  LMP_host_connection_req_format PDU;
  memset(&PDU,0,sizeof(LMP_host_connection_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_host_connection_req);
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_host_connection_req_format));
}
int LMP_Send_accepted(uint8_t role,uint8_t accept,uint8_t opcode)
{
  BT_LMP_DEBUG(("LMP_Send_accepted\n"));
  LMP_accepted_format PDU;
  memset(&PDU,0,sizeof(LMP_accepted_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_accepted);
  
  PDU.opcode = opcode;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_accepted_format));
}
int LMP_Send_not_accepted(uint8_t role,uint8_t accept,uint8_t opcode,uint8_t errcode)
{
  BT_LMP_DEBUG(("LMP_Send_not_accepted\n"));
  LMP_not_accepted_format PDU;
  memset(&PDU,0,sizeof(LMP_not_accepted_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_not_accepted);
  
  PDU.opcode = opcode;
  PDU.errcode = errcode;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_not_accepted_format));
}
int LMP_Send_LMP_version_req(uint8_t role,uint8_t vernr,uint16_t comid,uint16_t subvernr)
{
  BT_LMP_DEBUG(("LMP_Send_LMP_version_req\n"));
  LMP_version_req_format PDU;
  memset(&PDU,0,sizeof(LMP_version_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_version_req);
  PDU.versnr = vernr;
  PDU.compid = comid;
  PDU.subvernr = subvernr;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_version_req_format));
}
int LMP_Send_LMP_version_rsp(uint8_t role,uint8_t vernr,uint16_t comid,uint16_t subvernr)
{
  BT_LMP_DEBUG(("LMP_Send_LMP_version_rsp\n"));
  LMP_version_rsp_format PDU;
  memset(&PDU,0,sizeof(LMP_version_rsp_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_version_rsp);
  PDU.versnr = vernr;
  PDU.compid = comid;
  PDU.subvernr = subvernr;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_version_rsp_format));
}
int LMP_Send_set_AFH(uint8_t role,uint32_t AFH_instant,uint8_t AFH_mode,uint8_t AFH_channel_map[10])
{
  BT_LMP_DEBUG(("LMP_Send_set_AFH\n"));
  LMP_set_AFH_format PDU;
  memset(&PDU,0,sizeof(LMP_set_AFH_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_set_AFH);
  PDU.AFH_instant = AFH_instant;
  PDU.AFH_mode = AFH_mode;
  memcpy(&(PDU.AFH_channel_map),AFH_channel_map,10);
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_set_AFH_format));
}
int LMP_Send_setup_complete(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_setup_complete\n"));
  LMP_setup_complete_format PDU;
  memset(&PDU,0,sizeof(LMP_setup_complete_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_setup_complete);
  
  LMP_Core_M->has_commite_setup_complete = 1;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_setup_complete_format));
}
int LMP_Send_in_rand(uint8_t role,uint8_t random[16])
{
  BT_LMP_DEBUG(("LMP_Send_in_rand\n"));
  LMP_in_rand_format PDU;
  memset(&PDU,0,sizeof(LMP_in_rand_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_in_rand);
  memcpy(&(PDU.random_nub),random,16);
  memcpy(LMP_Core_M->K_init,random,16);
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_in_rand_format));
}
int LMP_Send_comb_key(uint8_t role,uint8_t random[16])
{
  BT_LMP_DEBUG(("LMP_Send_comb_key\n"));
  LMP_comb_key_format PDU;
  memset(&PDU,0,sizeof(LMP_comb_key_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_comb_key);
  memcpy(&(PDU.random_nub),random,16);
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_comb_key_format));
}

int LMP_Send_au_rand(uint8_t role,uint8_t random[16])
{
  BT_LMP_DEBUG(("LMP_Send_au_rand\n"));
  LMP_au_rand_format PDU;
  memset(&PDU,0,sizeof(LMP_au_rand_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_au_rand);
  memcpy(&(PDU.random_nub),random,16);
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_au_rand_format));
}
int LMP_Send_sres(uint8_t role,uint8_t auth_rsp[4])
{
  BT_LMP_DEBUG(("LMP_Send_sres\n"));
  LMP_sres_format PDU;
  memset(&PDU,0,sizeof(LMP_sres_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_sres);
  memcpy(&(PDU.auth_rsp),auth_rsp,4);
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_sres_format));
}
int LMP_Send_auto_rate(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_auto_rate\n"));
  LMP_auto_rate_format PDU;
  memset(&PDU,0,sizeof(LMP_auto_rate_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_auto_rate);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_auto_rate_format));
}
int LMP_Send_clkoffset_req(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_clkoffset_req\n"));
  LMP_clkoffset_req_format PDU;
  memset(&PDU,0,sizeof(LMP_clkoffset_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_clkoffset_req);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_clkoffset_req_format));
}
int LMP_Send_clkoffset_rsp(uint8_t role,uint16_t clock_offset)
{
  BT_LMP_DEBUG(("LMP_Send_clkoffset_rsp\n"));
  LMP_clkoffset_rsp_format PDU;
  memset(&PDU,0,sizeof(LMP_clkoffset_rsp_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_clkoffset_rsp);
  PDU.clock_offset = clock_offset;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_clkoffset_rsp_format));
}

int LMP_Send_incr_power_req(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_incr_power_req\n"));
  LMP_incr_power_req_format PDU;
  memset(&PDU,0,sizeof(LMP_incr_power_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_incr_power_req);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_incr_power_req_format));
}
int LMP_Send_decr_power_req(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_decr_power_req\n"));
  LMP_decr_power_req_format PDU;
  memset(&PDU,0,sizeof(LMP_decr_power_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_decr_power_req);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_decr_power_req_format));
}

int LMP_Send_min_power(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_min_power\n"));
  LMP_min_power_format PDU;
  memset(&PDU,0,sizeof(LMP_min_power_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_min_power);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_min_power_format));
}
int LMP_Send_max_power(uint8_t role)
{
  BT_LMP_DEBUG(("LMP_Send_max_power\n"));
  LMP_max_power_format PDU;
  memset(&PDU,0,sizeof(LMP_max_power_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_max_power);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_max_power_format));
}

int LMP_Send_detach(uint8_t role,uint8_t errcode)
{
  BT_LMP_DEBUG(("LMP_Send_detach\n"));
  LMP_detach_format PDU;
  memset(&PDU,0,sizeof(LMP_detach_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_detach);
  PDU.errcode = errcode;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_detach_format));
}
int LMP_Send_hold(uint8_t role,uint16_t hold_time,uint32_t hold_instant)
{
  BT_LMP_DEBUG(("LMP_Send_LMP_hold\n"));
  LMP_hold_format PDU;
  memset(&PDU,0,sizeof(LMP_hold_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_hold);
  PDU.hold_time = hold_time;
  PDU.hold_instant = hold_instant;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_hold_format));
}
int LMP_Send_hold_req(uint8_t role,uint16_t hold_time,uint32_t hold_instant)
{
  BT_LMP_DEBUG(("LMP_Send_LMP_hold_req\n"));
  LMP_hold_req_format PDU;
  memset(&PDU,0,sizeof(LMP_hold_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_hold_req);
  PDU.hold_time = hold_time;
  PDU.hold_instant = hold_instant;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_hold_req_format));
}
int LMP_Send_max_slot(uint8_t role,uint8_t max_slot)
{
  BT_LMP_DEBUG(("LMP_max_slot\n"));
  LMP_max_slot_format PDU;
  memset(&PDU,0,sizeof(LMP_max_slot_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_max_slot);
  PDU.max_slot = max_slot;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_max_slot_format));
}
int LMP_Send_max_slot_req(uint8_t role,uint8_t max_slot)
{
  BT_LMP_DEBUG(("LMP_Send_max_slot_req\n"));
  LMP_max_slot_req_format PDU;
  memset(&PDU,0,sizeof(LMP_max_slot_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_max_slot_req);
  PDU.max_slot = max_slot;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_max_slot_req_format));
}
int LMP_Send_name_req(uint8_t role,uint8_t name_offset)
{
  BT_LMP_DEBUG(("LMP_Send_name_req\n"));
  LMP_name_req_format PDU;
  memset(&PDU,0,sizeof(LMP_name_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_name_req);
  PDU.name_offset = name_offset;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_name_req_format));
}
int LMP_Send_name_rsp(uint8_t role,uint8_t name_offset,uint8_t name_length,uint8_t name[14])
{
  BT_LMP_DEBUG(("LMP_Send_name_rsp\n"));
  LMP_name_rsp_format PDU;
  memset(&PDU,0,sizeof(LMP_name_rsp_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_name_rsp);
  PDU.name_offset = name_offset;
  PDU.name_length = name_length;
  if(name_length > 13)
  {
    memcpy(&(PDU.name),name,13);
  }
  else
  {
    memcpy(&(PDU.name),name,name_length);
  }
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_name_req_format));
}
int LMP_Send_page_mode_req(uint8_t role,uint8_t paging_scheme,uint8_t paging_scheme_settings)
{
  BT_LMP_DEBUG(("LMP_Send_page_mode_req\n"));
  LMP_page_mode_req_format PDU;
  memset(&PDU,0,sizeof(LMP_page_mode_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_page_mode_req);
  PDU.paging_scheme = paging_scheme;
  PDU.paging_scheme_settings = paging_scheme_settings;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_page_mode_req_format));
}
int LMP_Send_page_scan_mode_req(uint8_t role,uint8_t paging_scheme,uint8_t paging_scheme_settings)
{
  BT_LMP_DEBUG(("LMP_Send_page_scan_mode_req\n"));
  LMP_page_scan_mode_req_format PDU;
  memset(&PDU,0,sizeof(LMP_page_scan_mode_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_page_scan_mode_req);
  PDU.paging_scheme = paging_scheme;
  PDU.paging_scheme_settings = paging_scheme_settings;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_page_scan_mode_req_format));
}
int LMP_Send_preferred_rate(uint8_t role,uint8_t data_rate)
{
  BT_LMP_DEBUG(("LMP_Send_preferred_rate\n"));
  LMP_preferred_rate_format PDU;
  memset(&PDU,0,sizeof(LMP_preferred_rate_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_preferred_rate);
  PDU.data_rate = data_rate;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_preferred_rate_format));
}
int LMP_Send_quality_of_service(uint8_t role,uint16_t poll_interval,uint8_t NBC)
{
  BT_LMP_DEBUG(("LMP_Send_quality_of_service\n"));
  LMP_quality_of_service_format PDU;
  memset(&PDU,0,sizeof(LMP_quality_of_service_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_quality_of_service);
  PDU.poll_interval = poll_interval;
  PDU.NBC = NBC;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_quality_of_service_format));
}
int LMP_Send_quality_of_service_req(uint8_t role,uint16_t poll_interval,uint8_t NBC)
{
  BT_LMP_DEBUG(("LMP_Send_quality_of_service_req\n"));
  LMP_quality_of_service_req_format PDU;
  memset(&PDU,0,sizeof(LMP_quality_of_service_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_quality_of_service_req);
  PDU.poll_interval = poll_interval;
  PDU.NBC = NBC;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_quality_of_service_req_format));
}

int LMP_Send_remove_SCO_link_req(uint8_t role,uint8_t SCO_handle,uint8_t errcode)
{
  BT_LMP_DEBUG(("LMP_Send_remove_SCO_link_req\n"));
  LMP_remove_SCO_link_req_format PDU;
  memset(&PDU,0,sizeof(LMP_remove_SCO_link_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_remove_SCO_link_req);
  PDU.SCO_handle = SCO_handle;
  PDU.errcode = errcode;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_remove_SCO_link_req_format));
}
int LMP_Send_SCO_link_req(uint8_t role,uint8_t SCO_handle,uint8_t timing_control_flags,uint8_t Dsco,uint8_t Tsco,uint8_t SCO_packe,uint8_t air_mode)
{
  BT_LMP_DEBUG(("LMP_Send_SCO_link_req\n"));
  LMP_SCO_link_req_format PDU;
  memset(&PDU,0,sizeof(LMP_SCO_link_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_SCO_link_req);
  PDU.SCO_handle = SCO_handle;
  PDU.timing_control_flags = timing_control_flags;
  PDU.Dsco = Dsco;
  PDU.SCO_packe = SCO_packe;
  PDU.air_mode = air_mode;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_SCO_link_req_format));
}
int LMP_Send_Simple_Pairing_Confirm(uint8_t role,uint8_t Commitment_Value[16])
{
  BT_LMP_DEBUG(("LMP_Send_Simple_Pairing_Confirm\n"));
  LMP_Simple_Pairing_Confirm_format PDU;
  memset(&PDU,0,sizeof(LMP_Simple_Pairing_Confirm_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_Simple_Pairing_Confirm);
  memcpy(&(PDU.Commitment_Value),Commitment_Value,16);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_Simple_Pairing_Confirm_format));
}
int LMP_Send_Simple_Pairing_Number(uint8_t role,uint8_t Nonce_Value[16])
{
  BT_LMP_DEBUG(("LMP_Send_Simple_Pairing_Number\n"));
  LMP_Simple_Pairing_Number_format PDU;
  memset(&PDU,0,sizeof(LMP_Simple_Pairing_Number_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_Simple_Pairing_Number);
  memcpy(&(PDU.Nonce_Value),Nonce_Value,16);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_Simple_Pairing_Number_format));
}

int LMP_Send_slot_offset(uint8_t role,uint16_t slot_offset,uint8_t BD_ADDR[6])
{
  BT_LMP_DEBUG(("LMP_Send_slot_offset\n"));
  LMP_slot_offset_format PDU;
  memset(&PDU,0,sizeof(LMP_slot_offset_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_slot_offset);
  PDU.slot_offset = slot_offset;
  memcpy(PDU.BD_ADDR,BD_ADDR,6);
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_slot_offset_format));
}

int LMP_Send_sniff_req(uint8_t role,uint8_t timing_control_flags,uint16_t Dsniff,uint16_t Tsniffe,uint16_t sniff_attempt,uint16_t sniff_timeout)
{
  BT_LMP_DEBUG(("LMP_Send_sniff_req\n"));
  LMP_sniff_req_format PDU;
  memset(&PDU,0,sizeof(LMP_sniff_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_sniff_req);
  PDU.timing_control_flags = timing_control_flags;
  PDU.Dsniff = Dsniff;
  PDU.Tsniffe = Tsniffe;
  PDU.sniff_attempt = sniff_attempt;
  PDU.sniff_timeout = sniff_timeout;
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_sniff_req_format));
}

int LMP_Send_supervision_timeout(uint8_t role,uint16_t supervision_timeout)
{
  BT_LMP_DEBUG(("LMP_Send_supervision_timeout\n"));
  LMP_supervision_timeout_format PDU;
  memset(&PDU,0,sizeof(LMP_supervision_timeout_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_supervision_timeout);
  PDU.supervision_timeout = supervision_timeout;
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_supervision_timeout_format));
}

int LMP_Send_switch_req(uint8_t role,uint32_t switch_instant)
{
  BT_LMP_DEBUG(("LMP_Send_switch_req_\n"));
  LMP_switch_req_format PDU;
  memset(&PDU,0,sizeof(LMP_switch_req_format));
  
  LMP_SET_S_HDR(&(PDU.hdr),role,LMP_switch_req);
  PDU.switch_instant = switch_instant;
  
  LMP_Send_PDU((uint8_t *)&PDU,sizeof(LMP_switch_req_format));
}

int LMP_Send_PDU(uint8_t *PDU,uint32_t length)
{
  LMP_Parse_PDU(PDU,length);
}


int LMP_Parse_PDU(uint8_t *PDU,uint32_t length)
{
  uint32_t PDU_ID;
  if(LMP_GET_TID(PDU) == LMP_MASTER_ROLE || LMP_GET_TID(PDU) == LMP_SLAVE_ROLE);
  else
  {
     BT_LMP_DEBUG(("WARING:INVALID ROLE\n"));
     return -1;
  }
  
  PDU_ID = LMP_GET_S_OPC(PDU);
  BT_LMP_DEBUG(("PDU ID = %d\n",PDU_ID));
  switch(PDU_ID)
  {
    case LMP_accepted:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_accepted\n"));
      LMP_Parse_Send_accepted(PDU,length);
      //TODO
      break;
    }
    case LMP_not_accepted:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_not_accepted\n"));
      break;
    }
    case LMP_comb_key:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_comb_key\n"));
      LMP_Parse_Send_comb_key(PDU,length);
      break;
    }
    case LMP_version_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_version_req\n"));
      LMP_Send_LMP_version_rsp(LMP_Core_M->remote_role,LMP_VERSION_4_0,TI,LMP_SBUVERSION);
      break;
    }
    case LMP_version_rsp:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_version_rsp\n"));
      LMP_Parse_Send_version_req(PDU,length);
      LMP_Parse_Send_version_rsp(PDU,length);
      break;
    }
    case LMP_features_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_features_req\n"));
      LMP_Parse_Send_Feature_req(PDU,length);
      LMP_Send_Feature_rsp(LMP_Core_M->remote_role,LMP_Core_M->local_feature);
      break;
    }
    case LMP_features_rsp:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_features_rsp\n"));
      LMP_Parse_Send_Feature_rsp(PDU,length);
      //TODO
      break;
    }
    case LMP_host_connection_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_host_connection_req\n"));
      LMP_Parse_Send_host_connection_req(PDU,length);
      LMP_Send_accepted(LMP_Core_M->remote_role,LMP_ACCEPT,LMP_host_connection_req);
      break;
    }
    case LMP_setup_complete:
    {
       BT_LMP_DEBUG(("PDU_ID is LMP_setup_complete\n"));
       if(LMP_Core_M->has_commite_setup_complete == 1)
       {
	 BT_LMP_DEBUG(("REMOTE RSP\n"));
       }
       else
       {
	 BT_LMP_DEBUG(("REMOTE REQ\n"));
	 LMP_Send_setup_complete(LMP_Core_M->remote_role);
       }
      break;
    }
    case LMP_set_AFH:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_set_AFH\n"));
      LMP_Parse_Send_set_AFH(PDU,length);
      break;
    }
    case LMP_in_rand:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_in_rand\n"));
      LMP_Parse_Send_in_rand(PDU,length);
      LMP_Send_accepted(LMP_Core_M->remote_role,LMP_ACCEPT,LMP_in_rand);
      break;
    }
    case LMP_au_rand:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_au_rand\n"));
      
      break;
    }
    case LMP_sres:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_sres\n"));
      break;
    }
    case LMP_auto_rate:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_auto_rate\n"));
      LMP_Parse_Send_auto_rate(PDU,length);
      break;
    }
    case LMP_clkoffset_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_clkoffset_req\n"));
      LMP_Parse_Send_clkoffset_req(PDU,length);
      break;
    }
    case LMP_clkoffset_rsp:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_clkoffset_rsp\n"));
      LMP_Parse_Send_clkoffset_rsp(PDU,length);
      break;
    }
    case LMP_incr_power_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_incr_power_req\n"));
      LMP_Parse_Send_incr_power_req(PDU,length);
      break;
    }
    case LMP_decr_power_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_decr_power_req\n"));
      LMP_Parse_Send_decr_power_req(PDU,length);
      break;
    }
    case LMP_max_power:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_max_power\n"));
      break;
    }
    case LMP_min_power:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_min_power\n"));
      break;
    }
    case LMP_detach:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_detach\n"));
      break;
    }
    case LMP_hold:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_hold\n"));
      LMP_Parse_Send_hold(PDU,length);
      break;
    }
    case LMP_hold_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_hold_req\n"));
      break;
    }
    case LMP_max_slot:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_max_slot\n"));
      break;
    }
    case LMP_max_slot_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_max_slot_req\n"));
      break;
    }
    case LMP_name_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_name_req\n"));
      LMP_Parse_Send_name_req(PDU,length);
      break;
    }
    case LMP_name_rsp:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_name_rsp\n"));
      LMP_Parse_Send_name_rsq(PDU,length);
      break;
    }
    case LMP_page_mode_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_page_mode_req\n"));
      LMP_Parse_Send_page_mode_req(PDU,length);
      break;
    }
    case LMP_page_scan_mode_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_page_scan_mode_req\n"));
      LMP_Parse_Send_page_scan_mode_req(PDU,length);
      break;
    }
    case LMP_preferred_rate:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_preferred_rate\n"));
      LMP_Parse_Send_preferred_rate(PDU,length);
      break;
    }
    case LMP_quality_of_service:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_quality_of_service\n"));
      LMP_Parse_Send_quality_of_service(PDU,length);
      break;
    }
    case LMP_quality_of_service_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_quality_of_service_req\n"));
      LMP_Parse_Send_quality_of_service_req(PDU,length);
      break;
    }
    case LMP_remove_SCO_link_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_remove_SCO_link_req\n"));
      LMP_Parse_Send_remove_SCO_link_req(PDU,length);
      break;
    }
    case LMP_SCO_link_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_SCO_link_req\n"));
      LMP_Parse_Send_SCO_link_req(PDU,length);
      break;
    }
    case LMP_Simple_Pairing_Confirm:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_Simple_Pairing_Confirm\n"));
      LMP_Parse_Send_Simple_Pairing_Confirm(PDU,length);
      break;
    }
    case LMP_Simple_Pairing_Number:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_Simple_Pairing_Number\n"));
      LMP_Parse_Send_Simple_Pairing_Number(PDU,length);
      break;
    }
    case LMP_slot_offset:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_slot_offset\n"));
      LMP_Parse_Send_slot_offset(PDU,length);
      break;
    }
    case LMP_sniff_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_sniff_req\n"));
      LMP_Parse_Send_sniff_req(PDU,length);
      break;
    }
    case LMP_supervision_timeout:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_supervision_timeout\n"));
      LMP_Parse_Send_supervision_timeout(PDU,length);
      break;
    }
    case LMP_switch_req:
    {
      BT_LMP_DEBUG(("PDU_ID is LMP_switch_req\n"));
      LMP_Parse_Send_switch_req(PDU,length);
      break;
    }
    default:
    {
      BT_LMP_DEBUG(("WARNING:UNKNOW PDU ID\n"));
      break;
    }
  }
}
int LMP_Parse_Send_Feature_req(uint8_t *PDU,uint32_t length)
{
  LMP_features_req_format *tmp_PDU_ptr = (LMP_features_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  LMP_Core_M->remote_feature = tmp_PDU_ptr->feature;

}
int LMP_Parse_Send_Feature_rsp(uint8_t *PDU,uint32_t length)
{
  LMP_features_rsp_format *tmp_PDU_ptr = (LMP_features_rsp_format *)PDU;
  
  LMP_Core_M->remote_feature = tmp_PDU_ptr->feature;
  
}
int LMP_Parse_Send_host_connection_req(uint8_t *PDU,uint32_t length)
{
  LMP_host_connection_req_format *tmp_PDU_ptr = (LMP_host_connection_req_format *)PDU;
}
int LMP_Parse_Send_accepted(uint8_t *PDU,uint32_t length)
{
  LMP_accepted_format *tmp_PDU_ptr = (LMP_accepted_format *)PDU;
  uint8_t tmp_accept_opcode = tmp_PDU_ptr->opcode;
 
}
int LMP_Parse_Send_not_accepted(uint8_t *PDU,uint32_t length)
{
  LMP_not_accepted_format *tmp_PDU_ptr = (LMP_not_accepted_format *)PDU;
  LMP_Core_M->local_role = LMP_GET_TID(PDU);
  //TODO
}
int LMP_Parse_Send_version_req(uint8_t *PDU,uint32_t length)
{
  LMP_version_req_format *tmp_PDU_ptr = (LMP_version_req_format *)PDU;
}
int LMP_Parse_Send_version_rsp(uint8_t *PDU,uint32_t length)
{
  LMP_version_rsp_format *tmp_PDU_ptr = (LMP_version_rsp_format *)PDU;
}
int LMP_Parse_Send_set_AFH(uint8_t *PDU,uint32_t length)
{
  LMP_set_AFH_format *tmp_PDU_ptr = (LMP_set_AFH_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  LMP_Core_M->AFH_mode = tmp_PDU_ptr->AFH_mode;
  LMP_Core_M->remote_clock = tmp_PDU_ptr->AFH_instant;
  LMP_Core_M->offset =  LMP_Core_M->remote_clock - LMP_Core_M->local_clock;
}
int LMP_Parse_Send_in_rand(uint8_t *PDU,uint32_t length)
{
  LMP_in_rand_format *tmp_PDU_ptr = (LMP_in_rand_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  memcpy(LMP_Core_M->K_init,tmp_PDU_ptr->random_nub,16);
}
int LMP_Parse_Send_au_rand(uint8_t *PDU,uint32_t length)
{
  LMP_au_rand_format *tmp_PDU_ptr = (LMP_au_rand_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}

int LMP_Parse_Send_comb_key(uint8_t *PDU,uint32_t length)
{
  LMP_comb_key_format *tmp_PDU_ptr = (LMP_comb_key_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_auto_rate(uint8_t *PDU,uint32_t length)
{
  LMP_auto_rate_format *tmp_PDU_ptr = (LMP_auto_rate_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  LMP_Core_M->CQDDR = 1;
}

int LMP_Parse_Send_clkoffset_req(uint8_t *PDU,uint32_t length)
{
  LMP_clkoffset_req_format *tmp_PDU_ptr = (LMP_clkoffset_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_clkoffset_rsp(uint8_t *PDU,uint32_t length)
{
  LMP_clkoffset_rsp_format *tmp_PDU_ptr = (LMP_clkoffset_rsp_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  LMP_Core_M->offset = tmp_PDU_ptr->clock_offset;
}
int LMP_Parse_Send_incr_power_req(uint8_t *PDU,uint32_t length)
{
  LMP_incr_power_req_format *tmp_PDU_ptr = (LMP_incr_power_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  if(LMP_Core_M->now_power < LMP_Core_M->max_power)
  {
    (LMP_Core_M->now_power)++;
  }
  else
  {
    LMP_Send_max_power(LMP_Core_M->remote_role);
  }
}
int LMP_Parse_Send_decr_power_req(uint8_t *PDU,uint32_t length)
{
  LMP_decr_power_req_format *tmp_PDU_ptr = (LMP_decr_power_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  if(LMP_Core_M->now_power > LMP_Core_M->min_power)
  {
    (LMP_Core_M->now_power)--;
  }
  else
  {
    LMP_Send_min_power(LMP_Core_M->remote_role);
  }
}
int LMP_Parse_Send_detach(uint8_t *PDU,uint32_t length)
{
  LMP_detach_format *tmp_PDU_ptr = (LMP_detach_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  
}
int LMP_Parse_Send_hold(uint8_t *PDU,uint32_t length)
{
  LMP_hold_format *tmp_PDU_ptr = (LMP_hold_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  LMP_Core_M->hold_status = 1;
  LMP_Core_M->hold_time = tmp_PDU_ptr->hold_time;
  LMP_Core_M->hold_instant = tmp_PDU_ptr->hold_instant;
}

int LMP_Parse_Send_hold_req(uint8_t *PDU,uint32_t length)
{
  LMP_hold_req_format *tmp_PDU_ptr = (LMP_hold_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_max_slot(uint8_t *PDU,uint32_t length)
{
  LMP_max_slot_format *tmp_PDU_ptr = (LMP_max_slot_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  LMP_Core_M->max_slot = tmp_PDU_ptr->max_slot;
}
int LMP_Parse_Send_max_slot_req(uint8_t *PDU,uint32_t length)
{
  LMP_max_slot_req_format *tmp_PDU_ptr = (LMP_max_slot_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  LMP_Core_M->max_slot = tmp_PDU_ptr->max_slot;
}
int LMP_Parse_Send_name_req(uint8_t *PDU,uint32_t length)
{
  LMP_max_slot_req_format *tmp_PDU_ptr = (LMP_max_slot_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_name_rsq(uint8_t *PDU,uint32_t length)
{
  LMP_name_rsp_format *tmp_PDU_ptr = (LMP_name_rsp_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
  if(tmp_PDU_ptr->name_length >13)
  {
    memcpy(LMP_Core_M->remote_name,tmp_PDU_ptr->name,13);
  }
  else
  {
    memcpy(LMP_Core_M->remote_name,tmp_PDU_ptr->name,tmp_PDU_ptr->name_length);
  }
}
int LMP_Parse_Send_page_mode_req(uint8_t *PDU,uint32_t length)
{
  LMP_page_mode_req_format *tmp_PDU_ptr = (LMP_page_mode_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_page_scan_mode_req(uint8_t *PDU,uint32_t length)
{
  LMP_page_scan_mode_req_format *tmp_PDU_ptr = (LMP_page_scan_mode_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}

int LMP_Parse_Send_preferred_rate(uint8_t *PDU,uint32_t length)
{
  LMP_preferred_rate_format *tmp_PDU_ptr = (LMP_preferred_rate_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_quality_of_service(uint8_t *PDU,uint32_t length)
{
  LMP_quality_of_service_format *tmp_PDU_ptr = (LMP_quality_of_service_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_quality_of_service_req(uint8_t *PDU,uint32_t length)
{
  LMP_quality_of_service_req_format *tmp_PDU_ptr = (LMP_quality_of_service_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_remove_SCO_link_req(uint8_t *PDU,uint32_t length)
{
  LMP_remove_SCO_link_req_format *tmp_PDU_ptr = (LMP_remove_SCO_link_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_SCO_link_req(uint8_t *PDU,uint32_t length)
{
  LMP_remove_SCO_link_req_format *tmp_PDU_ptr = (LMP_remove_SCO_link_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_Simple_Pairing_Confirm(uint8_t *PDU,uint32_t length)
{
  LMP_Simple_Pairing_Confirm_format *tmp_PDU_ptr = (LMP_Simple_Pairing_Confirm_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_Simple_Pairing_Number(uint8_t *PDU,uint32_t length)
{
  LMP_Simple_Pairing_Number_format *tmp_PDU_ptr = (LMP_Simple_Pairing_Number_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
}
int LMP_Parse_Send_slot_offset(uint8_t *PDU,uint32_t length)
{
  LMP_slot_offset_format *tmp_PDU_ptr = (LMP_slot_offset_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_sniff_req(uint8_t *PDU,uint32_t length)
{
  LMP_sniff_req_format *tmp_PDU_ptr = (LMP_sniff_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}
int LMP_Parse_Send_supervision_timeout(uint8_t *PDU,uint32_t length)
{
  LMP_supervision_timeout_format *tmp_PDU_ptr = (LMP_supervision_timeout_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
  
}
int LMP_Parse_Send_switch_req(uint8_t *PDU,uint32_t length)
{
  LMP_switch_req_format *tmp_PDU_ptr = (LMP_switch_req_format *)PDU;
  LMP_Core_M->remote_role = LMP_GET_TID(PDU);
}


以下是 bt_cfg.h 源码:

#ifndef BT_LMP_CFG_H
#define BT_LMP_CFG_H

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

#define DEBUG_BT_LMP

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_lmp.h"


extern LMP_Core *LMP_Core_M;
void SYS_Test()
{
  printf("char size = %d\n",sizeof(char));
  printf("short size = %d\n",sizeof(short));
  printf("int size = %d\n",sizeof(int));
  printf("long size = %d\n",sizeof(long));

  printf("float size = %d\n",sizeof(float));
  printf("double size = %d\n",sizeof(long));

  printf("long long size = %d\n",sizeof(unsigned long long));
  
  printf("enum LMP_VERSION_DEF = %d\n",sizeof(LMP_VERSION_DEF));
  printf("enum LMP_COM_ID_DEF = %d\n",sizeof(LMP_COM_ID_DEF));
}

int main()
{
  uint64_t feature = 0xFFFFFFFFFFFFFFFF;
  uint32_t AFH_instant = 0x00435d62;
  uint8_t AFH_channel_map[10] = {0x0};
  uint8_t random[16] = {0x1};
  uint8_t auth_rsp[4] = {0x1};
  uint16_t offset = 0x16;
  uint8_t name[14] = "yuzhongjun";
  uint8_t BD_ADDR[6] = {0x1};
  SYS_Test();
  LMP_Init(LMP_MASTER_ROLE,name,feature,AFH_instant,1,5,5);
  LMP_Send_Feature_req(LMP_Core_M->local_role,LMP_Core_M->local_feature);
  LMP_Send_host_connection_req(LMP_Core_M->local_role);
  LMP_Send_LMP_version_req(LMP_Core_M->local_role,LMP_VERSION_4_0,TI,LMP_SBUVERSION);
  LMP_Send_set_AFH(LMP_Core_M->local_role,AFH_instant,0x1,AFH_channel_map);
  LMP_Send_setup_complete(LMP_Core_M->local_role);
  LMP_Send_in_rand(LMP_Core_M->local_role,random);
  LMP_Send_not_accepted(LMP_Core_M->local_role,LMP_NOT_ACCEPT,LMP_in_rand,0);
  LMP_Send_comb_key(LMP_Core_M->local_role,random);
  LMP_Send_au_rand(LMP_Core_M->local_role,random);
  LMP_Send_sres(LMP_Core_M->local_role,auth_rsp);
  LMP_Send_auto_rate(LMP_Core_M->local_role);
  LMP_Send_clkoffset_req(LMP_Core_M->local_role);
  LMP_Send_clkoffset_rsp(LMP_Core_M->local_role,0x16);
  LMP_Send_decr_power_req(LMP_Core_M->local_role);
  LMP_Send_detach(LMP_Core_M->local_role,0);
  LMP_Send_hold(LMP_Core_M->local_role,0,0);
  LMP_Send_hold_req(LMP_Core_M->local_role,0,0);
  LMP_Send_incr_power_req(LMP_Core_M->local_role);
  LMP_Send_incr_power_req(LMP_Core_M->local_role);
  LMP_Send_max_slot(LMP_Core_M->local_role,1);
  LMP_Send_max_slot_req(LMP_Core_M->local_role,1);
  LMP_Send_name_req(LMP_Core_M->local_role,0);
  LMP_Send_name_rsp(LMP_Core_M->local_role,0,9,name);
  LMP_Send_page_mode_req(LMP_Core_M->local_role,0,1);
  LMP_Send_page_scan_mode_req(LMP_Core_M->local_role,0,1);
  LMP_Send_preferred_rate(LMP_Core_M->local_role,1);
  LMP_Send_quality_of_service(LMP_Core_M->local_role,1,1);
  LMP_Send_quality_of_service_req(LMP_Core_M->local_role,1,1);
  LMP_Send_remove_SCO_link_req(LMP_Core_M->local_role,1,1);
  LMP_Send_SCO_link_req(LMP_Core_M->local_role,1,1,1,1,1,1);
  LMP_Send_Simple_Pairing_Confirm(LMP_Core_M->local_role,random);
  LMP_Send_Simple_Pairing_Number(LMP_Core_M->local_role,random);
  LMP_Send_slot_offset(LMP_Core_M->local_role,1,BD_ADDR);
  LMP_Send_sniff_req(LMP_Core_M->local_role,1,1,1,1,1);
  LMP_Send_supervision_timeout(LMP_Core_M->local_role,1);
  LMP_Send_switch_req(LMP_Core_M->local_role,1);
}

一下是大makefile
objects = bt_lmp.o main.o
LMP : $(objects)
	cc $(objects) -o LMP
main.o : main.c 
bt_lmp.o : bt_lmp.c bt_lmp.h bt_cfg.h
clean :
	rm LMP $(objects)




你可能感兴趣的:(源码,蓝牙,LMP)