📄 tdi.cxx
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
//------------------------------------------------------------------------------
//
// Bluetooth TDI Layer
//
//
// Module Name:
//
// tdi.cxx
//
// Abstract:
//
// This file implements Bluetooth TDI Layer
//
//
//------------------------------------------------------------------------------
#if defined (UNDER_CE)
#include <windows.h>
#include <svsutil.hxx>
#if ! defined (UNDER_CE)
#include <stddef.h>
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <winsock.h>
#include <ws2bth.h>
#include <bt_debug.h>
#include <bt_os.h>
#include <bt_buffer.h>
#include <bt_hcip.h>
#include <bt_ddi.h>
#include <bt_api.h>
#include <memory.h>
#include <nspapi.h>
#include <cxport.h>
#include <ndis.h>
#include <tdi.h>
#include <tdistat.h>
#include <tdice.h>
#include <tdiinfo.h>
#include <ipexport.h>
#include <tcpinfo.h>
//#include <ndislink.h> // hmseo...
#include <bthapi.h>
#include <bt_sdp.h>
#include <sdpnode.h>
#include <sdplib.h>
extern "C" {
#include <cclib.h>
};
//#define DEBUG_CREDIT_FLOW 1
//#define DEBUG_PEER_SYNCHRONIZATION 1 // Both sides MUST be compiled with this
#if defined (DEBUG_PEER_SYNCHRONIZATION)
#define DEBUG_PEER_MAGIC 'gbdP'
struct PeerDebugData {
unsigned int magic;
#if defined (DEBUG_CREDIT_FLOW)
int iHaveCredits;
int iGaveCredits;
int iPacketsSent;
int iPacketsRecv;
int iCreditsSent;
int iCreditsRecv;
#endif
PeerDebugData () {
memset (this, 0, sizeof(*this));
magic = DEBUG_PEER_MAGIC;
}
};
#define DEBUG_OVERHEAD sizeof(PeerDebugData)
#else
#define DEBUG_OVERHEAD 0
#endif
#define CLIENTPORTSIG 0x4D534254 //"MSBT"
#define PIN_SIZE 16
#define KEY_SIZE 16
typedef void (* PBT_CONNECT_COMPLETE)(
PVOID Context,
TDI_STATUS FinalStatus,
unsigned long ByteCount);
typedef void (* PBT_DISCONNECT_COMPLETE)(
PVOID Context,
TDI_STATUS FinalStatus,
unsigned long ByteCount);
typedef void (* PBT_SEND_COMPLETE)(
PVOID Context,
TDI_STATUS TdiStatus,
DWORD BytesSent);
typedef void (* PBT_RECV_COMPLETE)(
PVOID Context,
TDI_STATUS TdiStatus,
DWORD BytesRecvd);
const WINSOCK_MAPPING BT_MAPPING = {1, 3, AF_BT, SOCK_STREAM, BTHPROTO_RFCOMM};
#define RFCOMM_TRANSPORT_NAME L"RFCOMM"
#define TDIR_V24_CONST 0x81 // +EA -FC -RTC -RTR 0 0 -IC +DV
#define TDIR_V24_FC 0x02 // +FC
#define TDIR_V24_RTC 0x04 // +RTC
#define TDIR_V24_RTR 0x08 // +RTR
#define TDIR_V24_IC 0x08 // +IC
#define TAI_SIZE (offsetof(TDI_ADDRESS_INFO, Address.Address[0].AddressType) + sizeof(SOCKADDR_BTH))
#define TA_SIZE (offsetof(TRANSPORT_ADDRESS, Address[0].AddressType) + sizeof(SOCKADDR_BTH))
enum CONNECT_STAGE {
ALLOCATED,
INITED,
OPENING,
OPENED,
CLOSED
};
#define OPT_CONFIG_AUTH 0x00000001
#define OPT_CONFIG_CRYPT 0x00000002
#define OPT_CONFIG_MTU 0x00000004
#define OPT_CONFIG_MTU_MAX 0x00000008
#define OPT_CONFIG_MTU_MIN 0x00000010
#define OPT_CONFIG_XON 0x00000020
#define OPT_CONFIG_XOFF 0x00000040
#define OPT_CONFIG_MAX_SEND 0x00000080
#define OPT_CONFIG_MAX_RECV 0x00000100
#define OPT_CONFIG_PIN 0x00000200
struct OPT {
unsigned int fAuthenticate : 1; // Authentication required
unsigned int fEncrypt : 1; // Encryption required
int mtu; // negotiated MTU
int imax_mtu; // Min MTU
int imin_mtu; // Max MTU
int xon_lim; // xon_lim
int xoff_lim; // xoff_lim
int imax_send; // max_send
int imax_recv; // max_recv
int cPinLength; // pin length or 0
unsigned char caPinData[PIN_SIZE]; // PIN
unsigned int uiConfigMask; // Non-default configuration mask
OPT (void) {
fAuthenticate = 0;
fEncrypt = 0;
mtu = TDIR_MTU_DESIRED;
imax_mtu = TDIR_MTUMAX;
imin_mtu = TDIR_MTUMIN;
xon_lim = TDIR_XONLIM;
xoff_lim = TDIR_XOFFLIM;
imax_send = TDIR_SENDMAX;
imax_recv = TDIR_RECVMAX;
cPinLength = 0;
memset (caPinData, 0, sizeof(caPinData));
uiConfigMask = 0;
}
};
struct RFCOMM_ADDRESS_OBJECT : public SVSRefObj, public OPT {
RFCOMM_ADDRESS_OBJECT *pNext; // Next in list
BD_ADDR b; // BD_ADDR
unsigned char ch; // server channel
unsigned int fListenOn : 1; // server?
unsigned int fAssigned : 1; // server channel dynamically reserved
PConnectEvent pEventConnect; // PConnectEvent
PVOID pEventConnectContext; // PVOID
PDisconnectEvent pEventDisconnect; // PDisconnectEvent
PVOID pEventDisconnectContext; // PVOID
PErrorEvent pEventError; // PErrorEvent
PVOID pEventErrorContext; // PVOID
PRcvEvent pEventReceive; // PRcvEvent
PVOID pEventReceiveContext; // PVOID
PRcvDGEvent pEventReceiveDatagram; // PRcvDGEvent
PVOID pEventReceiveDatagramContext; // PVOID
PRcvExpEvent pEventReceiveExpedited; // PRcvExpEvent
PVOID pEventReceiveExpeditedContext; // PVOID
RFCOMM_ADDRESS_OBJECT (void) : OPT () {
pNext = NULL;
memset (&b, 0, sizeof(b));
ch = 0;
fListenOn = 0;
fAssigned = 0;
pEventConnect = NULL;
pEventConnectContext = NULL;
pEventDisconnect = NULL;
pEventDisconnectContext = NULL;
pEventError = NULL;
pEventErrorContext = NULL;
pEventReceive = NULL;
pEventReceiveContext = NULL;
pEventReceiveDatagram = NULL;
pEventReceiveDatagramContext = NULL;
pEventReceiveExpedited = NULL;
pEventReceiveExpeditedContext = NULL;
}
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_CONNECTION_OBJECT;
struct RFCOMM_PACKET_OBJECT {
RFCOMM_PACKET_OBJECT *pNext; // Next in list
RFCOMM_CONNECTION_OBJECT *pConn; // Connection
int cBytesSent; // sent bytes
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_SEND_OBJECT {
RFCOMM_SEND_OBJECT *pNext; // Next in list
RFCOMM_CONNECTION_OBJECT *pConnect; // Connection
NDIS_BUFFER *pNdisBuffer; // NDIS buffer
int cBytesUsed; // Bytes used in buffer
int cBytesConfirmed; // Confirmed (signal if == used)
PBT_SEND_COMPLETE pSendCallback; // PBT_SEND_COMPLETE
void *pSendContext; // PVOID
RFCOMM_SEND_OBJECT (void) {
memset (this, 0, sizeof(*this));
}
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_RECEIVE_OBJECT {
RFCOMM_RECEIVE_OBJECT *pNext; // Next in list
BD_BUFFER *pBuffer; // BD_BUFFER (if pending data)
NDIS_BUFFER *pNdisBuffer; // NDIS_BUFFER (if waiting)
DWORD dwPerms; // Perms if waiting
PBT_RECV_COMPLETE pRecvEvent; // PBT_RECV_COMPLETE
void *pRecvContext; // PVOID
int iNdisUsed; // Used in NDIS buffer
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_CONNECTION_OBJECT : public OPT {
RFCOMM_CONNECTION_OBJECT *pNext; // Next in list
RFCOMM_ADDRESS_OBJECT *pAddr; // Address object
RFCOMM_SEND_OBJECT *pSendList; // List of sends
RFCOMM_PACKET_OBJECT *pPacketList; // List of packets
RFCOMM_RECEIVE_OBJECT *pRecvList; // List of recvs or data
HANDLE hConnect; // Connection (NULL = not connected)
SOCKADDR_BTH BTAddr;
CONNECT_STAGE eStage; // CONNECT_STAGE
int isent; // sent
int irecvd; // received
unsigned int v24in; // v24 signals
unsigned int brin; // breaks
unsigned int rlsin; // line status
unsigned int fc_credit : 1; // Credit-based flow control
unsigned int fc_local : 1; // 1 = don't send (MSC)
unsigned int fc_remote : 1; // 1 = don't send (MSC)
unsigned int fc_aggregate : 1; // 1 = don't send (FCON/FCOFF)
unsigned int fCloseHaveRecv : 1; // socket has closed down, but there's still more data to recv() on.
unsigned int fCloseHaveSend : 1; // socket has closed down, but there's still more data to send on.
unsigned int fNoDisconnect : 1; // opening state before we know if error occured
unsigned int fParity : 1; // just store; no effect on anything
unsigned int fSending : 1; // In the process of sending
int cbDataIndicated; // Bytes already indicated to TDI
int BaudRate; // just store; no effect on anything
int ByteSize; // just store; no effect on anything
int StopBits; // just store; no effect on anything
int Parity; // just store; no effect on anything
int iHaveCredits;
int iGaveCredits;
#if defined (DEBUG_CREDIT_FLOW)
int iPacketsSent;
int iPacketsRecv;
int iCreditsSent;
int iCreditsRecv;
#endif
PVOID pConnectionContext; // PVOID
PBT_CONNECT_COMPLETE pConnectCompleteEvent; // PBT_CONNECT_COMPLETE
void *pConnectCompleteContext; // PVOID
// SDP connection setup structures
unsigned short sdpCid;
RFCOMM_CONNECTION_OBJECT (void) : OPT() {
pNext = NULL;
pAddr = NULL;
pSendList = NULL;
pPacketList = NULL;
pRecvList = NULL;
hConnect = NULL;
memset (&BTAddr, 0, sizeof(BTAddr));
eStage = ALLOCATED;
isent = irecvd = 0;
v24in = brin = rlsin = 0;
fc_credit = FALSE;
fc_local = FALSE;
fc_remote = FALSE;
fc_aggregate = FALSE;
fCloseHaveRecv = FALSE;
fCloseHaveSend = FALSE;
fNoDisconnect = FALSE;
cbDataIndicated = 0;
iHaveCredits = 0;
iGaveCredits = 0;
pConnectionContext = NULL;
pConnectCompleteEvent = NULL;
pConnectCompleteContext = NULL;
sdpCid = 0;
fSending = FALSE;
// These are not accessible from the inside...
fParity = FALSE;
BaudRate = 115200;
ByteSize = 8;
StopBits = 0;
Parity = 0;
#if defined (DEBUG_CREDIT_FLOW)
iPacketsSent = 0;
iPacketsRecv = 0;
iCreditsSent = 0;
iCreditsRecv = 0;
#endif
}
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_SOCKET_OBJECT : OPT {
RFCOMM_SOCKET_OBJECT *pNext;
RFCOMM_ADDRESS_OBJECT *pao;
HANDLE hSocketHandle;
RFCOMM_SOCKET_OBJECT (void) : OPT() {
pNext = NULL;
pao = NULL;
hSocketHandle = NULL;
}
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_PN_OBJECT {
RFCOMM_PN_OBJECT *pNext;
HANDLE hConnection;
int imtu;
int fCreditFlow; // credit-based flow control
int iCredits; // credits granted
void *operator new (size_t size);
void operator delete (void *ptr);
};
struct RFCOMM_TDI : public SVSAllocClass, public SVSRefObj, public SVSSynch {
FixedMemDescr *pfmdAO; // FMD for Address Objects
FixedMemDescr *pfmdCO; // FMD for Connect Objects
FixedMemDescr *pfmdSO; // FMD for Socket Objects
FixedMemDescr *pfmdSEND; // FMD for sends
FixedMemDescr *pfmdRECV; // FMD for recvs
FixedMemDescr *pfmdPL; // FMD for data packets
FixedMemDescr *pfmdPN; // FMD for parameter negotiation packets
RFCOMM_ADDRESS_OBJECT *paolist; // List of addresses
RFCOMM_CONNECTION_OBJECT *pcolist; // List of connections
RFCOMM_SOCKET_OBJECT *psolist; // List of sockets
RFCOMM_PN_OBJECT *pnlist; // List of PN associations
HANDLE hRFCOMM; // RFCOMM handle
RFCOMM_INTERFACE rfcomm_if; // RFCOMM interface
unsigned int fIsConnected : 1; // 1 = connected
unsigned int fIsInitialized : 1; // 1 = initialized
HANDLE hSDP;
SDP_INTERFACE sdp_if;
int cDeviceHeader; // pre-allocate for front
int cDeviceTrailer; // pre-allocate for rear
BT_ADDR btAddr; // address of local device
HANDLE hHCI; // handles to HCI layer to read the address
HCI_INTERFACE hci_if;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -