Analysis of Bluetooth l2cap (II.)

Source: Internet
Author: User
Tags define null

Keywords: Bluetooth protocol HCI Anatomy HCI Overview HCI Note LMP l2cap SDP RFCOMM


Zhongjun

In the spirit of mutual learning purposes, to share this series of articles, welcome reprint, please specify the author, respect for copyright, thank you

Please correct me if the article is not in the wrong place, learn together


This series is currently intended to:

LMP anatomy + Analog Source code:

Analysis of Bluetooth LMP (i.)

Analysis of Bluetooth LMP (II.)

HCI anatomy + Analog Source code:

Analysis of Bluetooth HCI (i.)

Analysis of Bluetooth HCI (II.)

Analysis of Bluetooth HCI (III.)

L2CAP Analysis + Analog source code

Analysis of Bluetooth l2cap (i.)

SDP profiling + analog Source (unfinished)

Rfcomm Anatomy + Analog Source (not completed)

You can also access my csdn from the Bluetooth section: http://blog.csdn.net/XiaoXiaoPengBo/article/category/5998687

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

Gorgeous split line, go to the chase

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


One, part of the code is not completed, just simulate a connection-oriented from the establishment to the disconnection process, in addition, L2CAP's key state machine and unpacking reorganization no simulation

Bt_l2cap.h

/** This file was part of the L2cap protocal.* data:20160510* author:zhongjun**/#ifndef bt_l2cap_h_h#define bt_l2cap_h_h #include "bt_cfg.h" #ifdef debug_bt_l2cap#define DEBUG (x) {printf x;} #define BT_L2CAP_DEBUG (x) DEBUG (x) #else # define BT_L2CAP_DEBUG (x) #endif/* l2cap CHANNEL */#define L2CAP_SIG_CH 0x0001# Define L2CAP_CONLESS_CH 0x0002/* l2cap defaults * * #define L2CAP_DEFAULT_MTU672#DEFINE l2cap_default_flush_to0xffff/* UPPER LAYER PSM */#define SDP_PSM 0x0001/* L2CAP command codes */#define L2cap_command_rej0x01#define l2cap_conn_req0x02# Define L2cap_conn_rsp0x03#define l2cap_conf_req0x04#define L2cap_conf_rsp0x05#define L2CAP_DISCONN_REQ0x06#define L2cap_disconn_rsp0x07#define l2cap_echo_req0x08#define l2cap_echo_rsp0x09#define L2CAP_INFO_REQ0x0a#define L2CAP_ info_rsp0x0b/* Connect result */#define L2cap_cr_success0x0000#define L2cap_cr_pend0x0001#define L2cap_cr_nosup_ Psm0x0002#define l2cap_cr_sec_block0x0003#define l2cap_cr_no_rsr0x0004/* Config result */#define L2cap_conf_success0x0000#define l2cap_conf_unaccept0x0001#define l2cap_conf_reject0x0002#define L2CAP_CONF_UNKNOWN0x0003/* Config Option */#define L2cap_conf_mtu0x01#define l2cap_conf_flush_to0x02#define l2cap_conf_qos0x03#define L2CAP_CONF_ Rfc0x04#define L2cap_conf_rfc_mode0x04#pragma Pack (1)/* L2CAP structures hdr*/typedef struct {uint16_tlen;uint16_tcid ;} l2cap_pdu_hdr_format;typedef struct {L2cap_pdu_hdr_format sig_hdr;uint8_tcode;uint8_tident;uint16_tlen;} l2cap_sig_hdr_format;/* l2cap Structures detail*/typedef struct {l2cap_sig_hdr_format com_hdr;uint16_tpsm;uint16_ Tscid;} l2cap_con_req;typedef struct {L2cap_sig_hdr_format com_hdr;uint16_tdst_cid;uint16_tsrc_cid;uint16_tresult;uint16_ Tstatus;} l2cap_con_rsp;typedef struct {l2cap_sig_hdr_format com_hdr;uint16_tdst_cid;uint16_tflags;uint8_tdata[0];} l2cap_conf_req;typedef struct {L2cap_sig_hdr_format com_hdr;uint16_tsrc_cid;uint16_tflags;uint16_tresult;uint8_ TDATA[0];} l2cap_conf_rsp;typedef struct {l2cap_pdu_hdr_format com_hdr;uint8_tdata[0];} L2cap_upper_layer_data_format;typedef struct {L2cap_sig_hdr_format com_hdr;uint16_tdcid;uint16_tscid;} l2cap_discon_req;typedef struct {L2cap_sig_hdr_format com_hdr;uint16_tdcid;uint16_tscid;} L2CAP_DISCON_RSP; #pragma pack ()/* API *//* send PDU */int l2cap_send_sig_con_req (uint8_t ident,uint16_t psm,uint16_t SCID); int l2cap_send_sig_conf_req (uint8_t ident,uint16_t dcid,uint16_t flags,uint8_t *conf_opt,uint8_t opt_len); int L2cap_send_upper_layer_data (uint16_t cid,uint8_t *data,uint16_t length); int L2cap_send_sig_discon_req (uint8_t ident , uint16_t dcid,uint16_t SCID); int L2cap_send_pdu (uint8_t *pdu,uint32_t length);/* Reve PDU */int L2CAP_PARSE_PDU (uint8_ T *pdu,uint32_t length); int L2cap_parse_sig_con_rsp_pdu (uint8_t *pdu,uint32_t length); int l2cap_parse_sig_conf_rsp_ PDU (uint8_t *pdu,uint32_t length); int L2cap_parse_sig_discon_rsp_pdu (uint8_t *pdu,uint32_t length); #endif

Bt_l2cap.c

#include "bt_l2cap.h" int l2cap_send_sig_con_req (uint8_t ident,uint16_t psm,uint16_t SCID) {l2cap_con_req PDU; PDU.com_hdr.sig_hdr.cid = l2cap_sig_ch; PDU.com_hdr.sig_hdr.len = sizeof (l2cap_con_req)-sizeof (L2cap_pdu_hdr_format); PDU.com_hdr.code = L2cap_conn_req; PDU.com_hdr.ident = ident; PDU.com_hdr.len = sizeof (l2cap_con_req)-sizeof (L2cap_sig_hdr_format); PDU.PSM = PSM; Pdu.scid = SCID; L2CAP_SEND_PDU ((uint8_t *) &pdu,sizeof (l2cap_con_req));} int L2cap_send_sig_conf_req (uint8_t ident,uint16_t dcid,uint16_t flags,uint8_t *conf_opt,uint8_t opt_len) {L2CAP_Conf _req *PDU = (L2cap_conf_req *) malloc (sizeof (L2CAP_CONF_REQ) + Opt_len);(PD u->com_hdr). Sig_hdr.cid = L2cap_sig_ch; ( PDU-&GT;COM_HDR). Sig_hdr.len = sizeof (l2cap_conf_req)-sizeof (L2cap_pdu_hdr_format) + Opt_len; (PDU-&GT;COM_HDR). Code = L2cap_conf_req; (PDU-&GT;COM_HDR). ident = ident; (PDU-&GT;COM_HDR). len = sizeof (l2cap_conf_req)-sizeof (L2CAP_ Sig_hdr_format) + Opt_len; Pdu->dst_cid = Dcid; Pdu->flags = flags;memcpy (pdu->data,cOnf_opt,opt_len); L2CAP_SEND_PDU (pdu,sizeof (l2cap_conf_req) + Opt_len);} int L2cap_send_upper_layer_data (uint16_t cid,uint8_t *data,uint16_t length) {L2cap_upper_layer_data_format *PDU = ( L2cap_upper_layer_data_format *) malloc (sizeof (L2cap_upper_layer_data_format) + length);(PD u->com_hdr). CID = CID ;(PD u->com_hdr). Len = sizeof (L2cap_upper_layer_data_format) + length;memcpy (pdu->data,data,length); L2CAP_SEND_PDU (pdu,sizeof (l2cap_upper_layer_data_format) + length);} int L2cap_send_sig_discon_req (uint8_t ident,uint16_t dcid,uint16_t SCID) {l2cap_discon_req PDU; PDU.com_hdr.sig_hdr.cid = l2cap_sig_ch; PDU.com_hdr.sig_hdr.len = sizeof (l2cap_discon_req)-sizeof (L2cap_pdu_hdr_format); PDU.com_hdr.code = L2cap_disconn_req; PDU.com_hdr.ident = ident; PDU.com_hdr.len = sizeof (l2cap_con_req)-sizeof (L2cap_sig_hdr_format); Pdu.dcid = Dcid; Pdu.scid = SCID; L2CAP_SEND_PDU ((uint8_t *) &pdu,sizeof (l2cap_con_req));} int L2CAP_SEND_PDU (uint8_t *pdu,uint32_t length) {int index = 0; Bt_l2cap_debug (">> Send l2cap REQ PDU: ")); for (index = 0; Index < length; index++) {Bt_l2cap_debug (("0x%02x", Pdu[index]));} Bt_l2cap_debug (("\ n")); int L2CAP_PARSE_PDU (uint8_t *pdu,uint32_t length) {L2cap_pdu_hdr_format *TMP_PDU = (L2cap_pdu_hdr_format *) PDU;if ((TMP _PDU-&GT;CID) = = l2cap_sig_ch) {uint8_t tmp_code; L2cap_sig_hdr_format *PDU = (L2cap_sig_hdr_format *) PDU; Bt_l2cap_debug (("SIG pdu\n")), Tmp_code = Pdu->code;switch (tmp_code) {case L2cap_conn_rsp:{bt_l2cap_debug ("L2CAP _conn_rsp\n ")); L2CAP_PARSE_SIG_CON_RSP_PDU (pdu,length); break;} Case L2cap_conf_rsp:{bt_l2cap_debug (("l2cap_conf_rsp\n")); L2CAP_PARSE_SIG_CONF_RSP_PDU (pdu,length); break;} Case L2cap_disconn_rsp:{bt_l2cap_debug (("l2cap_disconn_rsp\n")); L2CAP_PARSE_SIG_DISCON_RSP_PDU (pdu,length); break;} Default:{break;}}} Else{//todo}}int L2CAP_PARSE_SIG_CON_RSP_PDU (uint8_t *pdu,uint32_t length) {l2cap_con_rsp *TMP_PDU = (L2CAP_CON_RSP *) Pdu;if (Tmp_pdu->result = = l2cap_cr_success) {bt_l2cap_debug (("Connect successful\n");} else if (tmp_pdu->result= = L2cap_cr_pend) {bt_l2cap_debug (("Connect pending\n");}} int L2CAP_PARSE_SIG_CONF_RSP_PDU (uint8_t *pdu,uint32_t length) {l2cap_conf_rsp *TMP_PDU = (L2CAP_CONF_RSP *) PDU;if (tmp _pdu->result = = l2cap_conf_success) {bt_l2cap_debug (("config successful\n");} else if (Tmp_pdu->result = = l2cap_conf_unaccept) {bt_l2cap_debug (("config unaccept\n"));}} int L2CAP_PARSE_SIG_DISCON_RSP_PDU (uint8_t *pdu,uint32_t length) {}

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_l2captypedef unsigned char uint8_t;typedef unsigned short uint16_t;typedef unsigned int uint32_t;typedef  Char int8_t;typedef short  int16_t;typedef  int int32_t, #undef null #if defined (__cplusplus) #define NULL 0# Else#define NULL ((void *) 0) #endif #endif

Main.c

#include <stdio.h> #include "bt_l2cap.h" int main () {uint8_t con_rsp_pdu[] = {0x0c,0x00,0x01,0x00,0x03,0x79,0x08, 0X00,0X06,0X08,0X40,0X00,0X00,0X00,0X00,0X00}; uint8_t conf_option[] = {0x01,0x02,0xf4,0x01,0x02,0x02,0xff,0xff};uint8_t conf_rsp_pdu[] = {0x0E,0x00,0x01,0x00,0x05 , 0x7a,0x0a,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0xf4,0x01};uint8_t upper_layer_data[] = {0x1,0x1,0x1,0x1, 0X1,0X1}; L2cap_send_sig_con_req (0x79,sdp_psm,0x40); L2CAP_PARSE_PDU (con_rsp_pdu,sizeof (CON_RSP_PDU)); L2cap_send_sig_conf_req (0x79,0x0806,0x0,conf_option,sizeof (conf_option)); L2CAP_PARSE_PDU (conf_rsp_pdu,sizeof (CONF_RSP_PDU)); L2cap_send_upper_layer_data (0x40,upper_layer_data,sizeof (upper_layer_data)); L2cap_send_sig_discon_req (0x79,0x40,0x0806);}


Analysis of Bluetooth l2cap (II.)

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.