' ########################################################################################
' Microsoft Windows
' File: ws2bth.inc
' Contents: Winsock 2 Bluetooth Annex definitions
' Copyright (c) 2011 Jos? Roca
' Portions Copyright (c) Microsoft Corporation.
' All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################
#IF NOT %DEF(PBX_ws2bth_PBI)
#DEFINE PBX_ws2bth_PBI
%EQUATE `WS2BTH_INC = 1
#INCLUDE ONCE "Legacy/Devices/bthdef.inc"
'#include <pshpack1.h>
%EQUATE `BT_PORT_ANY = -1
%EQUATE `BT_PORT_MIN = &H1
%EQUATE `BT_PORT_MAX = &Hffff
%EQUATE `BT_PORT_DYN_FIRST = &H1001
'//
'// These three definitions are duplicated in winsock2.h to reserve ordinals
'//
#IF NOT %DEF(%AF_BTH)
%EQUATE `AF_BTH = 32
#ENDIF
#IF NOT %DEF(%PF_BTH)
%EQUATE `PF_BTH = %AF_BTH
#ENDIF
#IF NOT %DEF(%NS_BTH)
%EQUATE `NS_BTH = 16
#ENDIF
' // Size = 30 bytes
TYPE SOCKADDR_BTH BYTE
 addressFamily AS WORD ' USHORT // Always AF_BTH
 btAddr AS QUAD ' BTH_ADDR // Bluetooth device address
 serviceClassId AS GUID ' GUID // [OPTIONAL] system will query SDP for port
 port AS DWORD ' ULONG // RFCOMM channel or L2CAP PSM
END TYPE
$SVCID_BTH_PROVIDER = GUID$("{06AA63E0-7D60-41FF-AFB2-3EE6D2D9392D}")
%EQUATE `BTH_ADDR_STRING_SIZE = 12 ' // max size from WSAAddressToString
'//
'// Bluetooth protocol #s are assigned according to the Bluetooth
'// Assigned Numbers portion of the Bluetooth Specification
'//
%EQUATE `BTHPROTO_RFCOMM = &H0003
%EQUATE `BTHPROTO_L2CAP = &H0100
%EQUATE `SOL_RFCOMM = %BTHPROTO_RFCOMM
%EQUATE `SOL_L2CAP = %BTHPROTO_L2CAP
%EQUATE `SOL_SDP = &H0101
'//
'// SOCKET OPTIONS
'//
%EQUATE `SO_BTH_AUTHENTICATE = &H80000001 ' // optlen=sizeof(ULONG), optval = &(ULONG)TRUE/FALSE
%EQUATE `SO_BTH_ENCRYPT = &H00000002 ' // optlen=sizeof(ULONG), optval = &(ULONG)TRUE/FALSE
%EQUATE `SO_BTH_MTU = &H80000007 ' // optlen=sizeof(ULONG), optval = &mtu
%EQUATE `SO_BTH_MTU_MAX = &H80000008 ' // optlen=sizeof(ULONG), optval = &max. mtu
%EQUATE `SO_BTH_MTU_MIN = &H8000000A ' // optlen=sizeof(ULONG), optval = &min. mtu
' //
' // Socket option parameters
' //
%EQUATE `RFCOMM_MAX_MTU = &H0000029A ' // L2CAP MTU (672) - RFCOMM header size (6)
%EQUATE `RFCOMM_MIN_MTU = &H00000017 ' // RFCOMM spec sec 5.3 table 5.1
'//
'// NAME SERVICE PROVIDER DEFINITIONS
'// For calling WSASetService
'// and WSALookupServiceBegin, WSALookupServiceNext, WSALookupServiceEnd
'// with Bluetooth-specific extensions
'//
%EQUATE `BTH_SDP_VERSION = 1
'//
'// [OPTIONAL] passed in BLOB member of WSAQUERYSET
'// QUERYSET and its lpBlob member are copied & converted
'// to unicode in the system for non-unicode applications.
'// However, nothing is copied back upon return. In
'// order for the system to return data such as pRecordHandle,
'// it much have an extra level of indirection from lpBlob
'//
'typedef struct _BTH_SET_SERVICE {
' //
' // This version number will change when/if the binary format of
' // SDP records change, affecting the format of pRecord.
' // Set to BTH_SDP_VERSION by client, and returned by system
' //
' PULONG pSdpVersion;
' //
' // Handle to SDP record. When BTH_SET_SERVICE structure is later
' // passed to WSASetService RNRSERVICE_DELETE, this handle identifies the
' // record to delete.
' //
' HANDLE *pRecordHandle;
' //
' // COD_SERVICE_* bit(s) associated with this SDP record, which will be
' // advertised when the local radio is found during device inquiry.
' // When the last SDP record associated with a bit is deleted, that
' // service bit is no longer reported in repsonse to inquiries
' //
' ULONG fCodService; // COD_SERVICE_* bits
' ULONG Reserved[5]; // Reserved by system. Must be zero.
' ULONG ulRecordLength; // length of pRecord which follows
' UCHAR pRecord[1]; // SDP record as defined by bluetooth spec
'} BTH_SET_SERVICE, *PBTH_SET_SERVICE;
' // Size = 37 bytes
TYPE BTH_SET_SERVICE BYTE
 pSdpVersion AS DWORD PTR
 pRecordHandle AS DWORD ' HANDLE *
 fCodService AS DWORD ' ULONG // COD_SERVICE_* bits
 Reserved(4) AS DWORD ' ULONG // Reserved by system. Must be zero.
 ulRecordLength AS DWORD ' ULONG // length of pRecord which follows
 pRecord(0) AS BYTE ' UCHAR // SDP record as defined by bluetooth spec
END TYPE
'//
'// Default device inquiry duration in seconds
'//
'// The application thread will be blocked in WSALookupServiceBegin
'// for the duration of the device inquiry, so this value needs to
'// be balanced against the chance that a device that is actually
'// present might not being found by Bluetooth in this time
'//
'// Paging improvements post-1.1 will cause devices to be
'// found generally uniformly in the 0-6 sec timeperiod
'//
%EQUATE `SDP_DEFAULT_INQUIRY_SECONDS = 6
%EQUATE `SDP_MAX_INQUIRY_SECONDS = 60
'//
'// Default maximum number of devices to search for
'//
%EQUATE `SDP_DEFAULT_INQUIRY_MAX_RESPONSES = 255
%EQUATE `SDP_SERVICE_SEARCH_REQUEST = 1
%EQUATE `SDP_SERVICE_ATTRIBUTE_REQUEST = 2
%EQUATE `SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST = 3
'//
'// [OPTIONAL] input restrictions on device inquiry
'// Passed in BLOB of LUP_CONTAINERS (device) search
'//
' // Size = 5 bytes
TYPE BTH_QUERY_DEVICE BYTE
 LAP AS DWORD ' ULONG // reserved: must be 0 (GIAC inquiry only)
 length AS BYTE ' UCHAR // requested length of inquiry (seconds)
END TYPE
'//
'// [OPTIONAL] Restrictions on searching for a particular service
'// Passed in BLOB of !LUP_CONTAINERS (service) search
'//
' // Size = 256 bytes
TYPE BTH_QUERY_SERVICE BYTE
 type AS DWORD ' ULONG // one of SDP_SERVICE_*
 serviceHandle AS DWORD ' ULONG
 uuids(%MAX_UUIDS_IN_QUERY - 1) AS SdpQueryUuid ' SdpQueryUuid
 numRange AS DWORD ' ULONG
 pRange(0) AS SdpAttributeRange ' SdpAttributeRange
END TYPE
'//
'// BTHNS_RESULT_*
'//
'// Bluetooth specific flags returned from WSALookupServiceNext
'// in WSAQUERYSET.dwOutputFlags in response to device inquiry
'//
'//
'// Local device is paired with remote device
'//
%EQUATE `BTHNS_RESULT_DEVICE_CONNECTED = &H00010000
%EQUATE `BTHNS_RESULT_DEVICE_REMEMBERED = &H00020000
%EQUATE `BTHNS_RESULT_DEVICE_AUTHENTICATED = &H00040000
'//
'// SOCKET IOCTLs
'//
'#define SIO_RFCOMM_SEND_COMMAND _WSAIORW (IOC_VENDOR, 101)
'#define SIO_RFCOMM_WAIT_COMMAND _WSAIORW (IOC_VENDOR, 102)
%EQUATE `SIO_RFCOMM_SEND_COMMAND = &HD8000065
%EQUATE `SIO_RFCOMM_WAIT_COMMAND = &HD8000066
'//
'// These IOCTLs are for test/validation/conformance and may only be
'// present in debug/checked builds of the system
'//
'#define SIO_BTH_PING _WSAIORW (IOC_VENDOR, 8)
'#define SIO_BTH_INFO _WSAIORW (IOC_VENDOR, 9)
'#define SIO_RFCOMM_SESSION_FLOW_OFF _WSAIORW (IOC_VENDOR, 103)
'#define SIO_RFCOMM_TEST _WSAIORW (IOC_VENDOR, 104)
'#define SIO_RFCOMM_USECFC _WSAIORW (IOC_VENDOR, 105)
%EQUATE `SIO_BTH_PING = &HD8000008
%EQUATE `SIO_BTH_INFO = &HD8000009
%EQUATE `SIO_RFCOMM_SESSION_FLOW_OFF = &HD8000067
%EQUATE `SIO_RFCOMM_TEST = &HD8000068
%EQUATE `SIO_RFCOMM_USECFC = &HD8000069
'//
'// SOCKET IOCTL DEFINITIONS
'//
'#ifndef BIT
'#define BIT(b) (1<<(b))
'#endif
'//
'// Structure definition from Bluetooth RFCOMM spec, TS 07.10 5.4.6.3.7
'//
'typedef struct _RFCOMM_MSC_DATA {
' UCHAR Signals;
' #define MSC_EA_BIT EA_BIT
' #define MSC_FC_BIT BIT(1) // Flow control, clear if we can receive
' #define MSC_RTC_BIT BIT(2) // Ready to communicate, set when ready
' #define MSC_RTR_BIT BIT(3) // Ready to receive, set when ready
' #define MSC_RESERVED (BIT(4)|BIT(5))// Reserved by spec, must be 0
' #define MSC_IC_BIT BIT(6) // Incoming call
' #define MSC_DV_BIT BIT(7) // Data valid
' UCHAR Break;
' #define MSC_BREAK_BIT BIT(1) // Set if sending break
' #define MSC_SET_BREAK_LENGTH(b, l) ((b) = ((b)&0x3) | (((l)&0xf) << 4))
'} RFCOMM_MSC_DATA, *PRFCOMM_MSC_DATA;
'%MSC_EA_BIT = %EA_BIT ' Note: EA_BIT isn't defined in any header
%EQUATE `MSC_EA_BIT = &H01
%EQUATE `MSC_FC_BIT = &H02 ' // Flow control, clear if we can receive
%EQUATE `MSC_RTC_BIT = &H04 ' // Ready to communicate, set when ready
%EQUATE `MSC_RTR_BIT = &H08 ' // Ready to receive, set when ready
%EQUATE `MSC_RESERVED = &H10 OR &H20 ' // Reserved by spec, must be 0
%EQUATE `MSC_IC_BIT = &H40 ' // Incoming call
%EQUATE `MSC_DV_BIT = &H80 ' // Data valid
' // Size = 2 bytes
TYPE RFCOMM_MSC_DATA BYTE
 Signals AS BYTE ' UCHAR
 Break AS BYTE ' UCHAR
END TYPE
'//
'// Structure definition from Bluetooth RFCOMM spec, TS 07.10 5.4.6.3.10
'//
%EQUATE `RLS_ERROR = &H01
%EQUATE `RLS_OVERRUN = &H02
%EQUATE `RLS_PARITY = &H04
%EQUATE `RLS_FRAMING = &H08
' // Size = 1 byte
TYPE RFCOMM_RLS_DATA BYTE
 LineStatus AS BYTE ' UCHAR
END TYPE
'//
'// Structure definition from Bluetooth RFCOMM spec, TS 07.10 5.4.6.3.9
'//
%EQUATE `RPN_BAUD_2400 = 0
%EQUATE `RPN_BAUD_4800 = 1
%EQUATE `RPN_BAUD_7200 = 2
%EQUATE `RPN_BAUD_9600 = 3
%EQUATE `RPN_BAUD_19200 = 4
%EQUATE `RPN_BAUD_38400 = 5
%EQUATE `RPN_BAUD_57600 = 6
%EQUATE `RPN_BAUD_115200 = 7
%EQUATE `RPN_BAUD_230400 = 8
%EQUATE `RPN_DATA_5 = &H0
%EQUATE `RPN_DATA_6 = &H1
%EQUATE `RPN_DATA_7 = &H2
%EQUATE `RPN_DATA_8 = &H3
%EQUATE `RPN_STOP_1 = &H0
%EQUATE `RPN_STOP_1_5 = &H4
%EQUATE `RPN_PARITY_NONE = &H00
%EQUATE `RPN_PARITY_ODD = &H08
%EQUATE `RPN_PARITY_EVEN = &H18
%EQUATE `RPN_PARITY_MARK = &H28
%EQUATE `RPN_PARITY_SPACE = &H38
%EQUATE `RPN_FLOW_X_IN = &H01
%EQUATE `RPN_FLOW_X_OUT = &H02
%EQUATE `RPN_FLOW_RTR_IN = &H04
%EQUATE `RPN_FLOW_RTR_OUT = &H08
%EQUATE `RPN_FLOW_RTC_IN = &H10
%EQUATE `RPN_FLOW_RTC_OUT = &H20
%EQUATE `RPN_PARAM_BAUD = &H01
%EQUATE `RPN_PARAM_DATA = &H02
%EQUATE `RPN_PARAM_STOP = &H04
%EQUATE `RPN_PARAM_PARITY = &H08
%EQUATE `RPN_PARAM_P_TYPE = &H10
%EQUATE `RPN_PARAM_XON = &H20
%EQUATE `RPN_PARAM_XOFF = &H40
%EQUATE `RPN_PARAM_X_IN = &H01
%EQUATE `RPN_PARAM_X_OUT = &H02
%EQUATE `RPN_PARAM_RTR_IN = &H04
%EQUATE `RPN_PARAM_RTR_OUT = &H08
%EQUATE `RPN_PARAM_RTC_IN = &H10
%EQUATE `RPN_PARAM_RTC_OUT = &H20
' // Size = 7 bytes
TYPE RFCOMM_RPN_DATA BYTE
 Baud AS BYTE ' UCHAR
 Data AS BYTE ' UCHAR
 FlowControl AS BYTE ' UCHAR
 XonChar AS BYTE ' UCHAR
 XoffChar AS BYTE ' UCHAR
 ParameterMask1 AS BYTE ' UCHAR
 ParameterMask2 AS BYTE ' UCHAR
END TYPE
%EQUATE `RFCOMM_CMD_NONE = 0
%EQUATE `RFCOMM_CMD_MSC = 1
%EQUATE `RFCOMM_CMD_RLS = 2
%EQUATE `RFCOMM_CMD_RPN = 3
%EQUATE `RFCOMM_CMD_RPN_REQUEST = 4
%EQUATE `RFCOMM_CMD_RPN_RESPONSE = 5
' // Size = 7 bytes
UNION RFCOMM_COMMAND_DATA_UNION BYTE
 MSC AS RFCOMM_MSC_DATA
 RLS AS RFCOMM_RLS_DATA
 RPN AS RFCOMM_RPN_DATA
END UNION
' // Size = 11 bytes
TYPE RFCOMM_COMMAND BYTE
 CmdType AS DWORD ' ULONG // one of RFCOMM_CMD_*
 Data AS RFCOMM_COMMAND_DATA_UNION
END TYPE
'//
'// These structures are for test/validation/conformance and may only be
'// present in debug/checked builds of the system
'//
' // Size = 53 bytes
TYPE BTH_PING_REQ BYTE
 btAddr AS QUAD ' BTH_ADDR
 dataLen AS BYTE ' UCHAR
 data(%MAX_L2CAP_PING_DATA_LENGTH - 1) AS BYTE ' UCHAR
END TYPE
' // Size = 45 bytes
TYPE BTH_PING_RSP BYTE
 dataLen AS BYTE ' UCHAR
 data(%MAX_L2CAP_PING_DATA_LENGTH - 1) AS BYTE ' UCHAR
END TYPE
' // Size = 10 bytes
TYPE BTH_INFO_REQ BYTE
 btAddr AS QUAD ' BTH_ADDR
 infoType AS WORD ' USHORT
END TYPE
' // Size = 44 bytes
UNION BTH_INFO_RSP_UNION BYTE
 connectionlessMTU AS WORD ' USHORT
 data(%MAX_L2CAP_INFO_DATA_LENGTH - 1) AS BYTE ' UCHAR
END UNION
' // Size = 47 bytes
TYPE BTH_INFO_RSP BYTE
 result AS WORD ' WORD
 dataLen AS BYTE ' UCHAR
 BTH_INFO_RSP_UNION
END TYPE
'//
'#include <poppack.h>
#ENDIF
