aboutsummaryrefslogtreecommitdiff
path: root/nimbluez/msbt/ms_ws2bth.nim
blob: 869ec3730b3851444912be3f9384fe0677c6e151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#++
#
#Copyright (c) 2000  Microsoft Corporation
#
#Module Name:
#
#        ws2bth.h
#
#Abstract:
#
#        Winsock 2 Bluetooth Annex definitions.
#
#Notes:
#
#        Change BT_* to BTH_*
#
#--
{.passC: "-mno-ms-bitfields".}

import ms_bthdef, ms_bthsdpdef, ms_bluetoothapis

const
  AF_BTH* = 32
  PF_BTH* = AF_BTH

  IOC_UNIX* = 0x00000000
  IOC_WS2* = 0x08000000
  IOC_PROTOCOL* = 0x10000000
  IOC_VENDOR* = 0x18000000

  IOCPARM_MASK* = 0x0000007F
  IOC_VOID* = 0x20000000
  IOC_OUT* = 0x40000000
  IOC_IN* = 0x80000000
  IOC_INOUT* = (IOC_IN or IOC_OUT)

template WSAIO*(x, y: expr): expr =
  (IOC_VOID or (x) or (y))

template WSAIOR*(x, y: expr): expr =
  (IOC_OUT or (x) or (y))

template WSAIOW*(x, y: expr): expr =
  (IOC_IN or (x) or (y))

template WSAIORW*(x, y: expr): expr =
  (IOC_INOUT or (x) or (y))

const
  BT_PORT_ANY* = ULONG(-1)
  BT_PORT_MIN* = 0x00000001
  BT_PORT_MAX* = 0x0000FFFF
  BT_PORT_DYN_FIRST* = 0x00001001
#
# These three definitions are duplicated in winsock2.h to reserve ordinals
#
type
  SOCKADDR_BTH* {.packed.} = object
    addressFamily*: USHORT # Always AF_BTH
    btAddr*: BTH_ADDR     # Bluetooth device address
    serviceClassId*: GUID # [OPTIONAL] system will query SDP for port
    port*: ULONG          # RFCOMM channel or L2CAP PSM

  PSOCKADDR_BTH* = ptr SOCKADDR_BTH

DEFINE_GUID(SVCID_BTH_PROVIDER, 0x06AA63E0, 0x00007D60, 0x000041FF,
            0x000000AF, 0x000000B2, 0x0000003E, 0x000000E6, 0x000000D2,
            0x000000D9, 0x00000039, 0x0000002D)
const
  BTH_ADDR_STRING_SIZE* = 12
#
# Bluetooth protocol #s are assigned according to the Bluetooth
# Assigned Numbers portion of the Bluetooth Specification
#
const
  BTHPROTO_RFCOMM* = 0x00000003
  BTHPROTO_L2CAP* = 0x00000100

  SOL_RFCOMM* = BTHPROTO_RFCOMM
  SOL_L2CAP* = BTHPROTO_L2CAP
  SOL_SDP* = 0x00000101
#
# SOCKET OPTIONS
#
const
  SO_BTH_AUTHENTICATE* = 0x80000001
  SO_BTH_ENCRYPT* = 0x00000002
  SO_BTH_MTU* = 0x80000007
  SO_BTH_MTU_MAX* = 0x80000008
  SO_BTH_MTU_MIN* = 0x8000000A
#
# Socket option parameters
#
# 3-DH5 => payload of 1021 => L2cap payload of 1017 => RFComm payload of 1011
const
  RFCOMM_MAX_MTU* = 0x000003F3
  RFCOMM_MIN_MTU* = 0x00000017
#
# NAME SERVICE PROVIDER DEFINITIONS
# For calling WSASetService
# and WSALookupServiceBegin, WSALookupServiceNext, WSALookupServiceEnd
# with Bluetooth-specific extensions
#
const
  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
#
type
  BTH_SET_SERVICE* = object
    pSdpVersion*: PULONG #
                         # 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
                         #
    #
    # Handle to SDP record.  When BTH_SET_SERVICE structure is later
    # passed to WSASetService RNRSERVICE_DELETE, this handle identifies the
    # record to delete.
    #
    pRecordHandle*: ptr HANDLE #
                               # 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
                               #
    fCodService*: ULONG   # COD_SERVICE_* bits
    Reserved*: array[5, ULONG] # Reserved by system.  Must be zero.
    ulRecordLength*: ULONG # length of pRecord which follows
    pRecord*: array[1, UCHAR] # SDP record as defined by bluetooth spec

  PBTH_SET_SERVICE* = ptr BTH_SET_SERVICE
#
# 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
#
const
  SDP_DEFAULT_INQUIRY_SECONDS* = 6
  SDP_MAX_INQUIRY_SECONDS* = 60
#
# Default maximum number of devices to search for
#
const
  SDP_DEFAULT_INQUIRY_MAX_RESPONSES* = 255
  SDP_SERVICE_SEARCH_REQUEST* = 1
  SDP_SERVICE_ATTRIBUTE_REQUEST* = 2
  SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST* = 3
#
# [OPTIONAL] input restrictions on device inquiry
# Passed in BLOB of LUP_CONTAINERS (device) search
#
type
  BTH_QUERY_DEVICE* = object
    LAP*: ULONG           # reserved: must be 0 (GIAC inquiry only)
    length*: UCHAR        # requested length of inquiry (seconds)

  PBTH_QUERY_DEVICE* = ptr BTH_QUERY_DEVICE
#
# [OPTIONAL] Restrictions on searching for a particular service
# Passed in BLOB of !LUP_CONTAINERS (service) search
#
type
  BTH_QUERY_SERVICE* = object
    `type`*: ULONG        # one of SDP_SERVICE_*
    serviceHandle*: ULONG
    uuids*: array[MAX_UUIDS_IN_QUERY, SdpQueryUuid]
    numRange*: ULONG
    pRange*: array[1, SdpAttributeRange]

  PBTH_QUERY_SERVICE* = ptr BTH_QUERY_SERVICE
#
# BTHNS_RESULT_*
#
# Bluetooth specific flags returned from WSALookupServiceNext
# in WSAQUERYSET.dwOutputFlags in response to device inquiry
#
#
# Local device is paired with remote device
#
const
  BTHNS_RESULT_DEVICE_CONNECTED* = 0x00010000
  BTHNS_RESULT_DEVICE_REMEMBERED* = 0x00020000
  BTHNS_RESULT_DEVICE_AUTHENTICATED* = 0x00040000
#
# SOCKET IOCTLs
#
const
  SIO_RFCOMM_SEND_COMMAND* = WSAIORW(IOC_VENDOR, 101)
  SIO_RFCOMM_WAIT_COMMAND* = WSAIORW(IOC_VENDOR, 102)
#
# These IOCTLs are for test/validation/conformance and may only be
# present in debug/checked builds of the system
#
const
  SIO_BTH_PING* = WSAIORW(IOC_VENDOR, 8)
  SIO_BTH_INFO* = WSAIORW(IOC_VENDOR, 9)
  SIO_RFCOMM_SESSION_FLOW_OFF* = WSAIORW(IOC_VENDOR, 103)
  SIO_RFCOMM_TEST* = WSAIORW(IOC_VENDOR, 104)
  SIO_RFCOMM_USECFC* = WSAIORW(IOC_VENDOR, 105)
#      RESERVED                          _WSAIORW (IOC_VENDOR, 106)
#
# SOCKET IOCTL DEFINITIONS
#
when not defined(BIT):
  template BIT*(b: expr): expr =
    (1 shl (b))

#
# Structure definition from Bluetooth RFCOMM spec, TS 07.10 5.4.6.3.7
#
# Signals field values
const
#  MSC_EA_BIT* = EA_BIT
  MSC_FC_BIT* = BIT(1)    # Flow control, clear if we can receive
  MSC_RTC_BIT* = BIT(2)   # Ready to communicate, set when ready
  MSC_RTR_BIT* = BIT(3)   # Ready to receive, set when ready
  MSC_RESERVED* = (BIT(4) or BIT(5)) # Reserved by spec, must be 0
  MSC_IC_BIT* = BIT(6)    # Incoming call
  MSC_DV_BIT* = BIT(7)    # Data valid
                          # Break field values
  MSC_BREAK_BIT* = BIT(1) # Set if sending break
template MSC_SET_BREAK_LENGTH*(b, l: expr): expr =
  ((b) = ((b) and 0x00000003) or (((l) and 0x0000000F) shl 4))

type
  RFCOMM_MSC_DATA* = object
    Signals*: UCHAR
    Break*: UCHAR

  PRFCOMM_MSC_DATA* = ptr RFCOMM_MSC_DATA
#
# Structure definition from Bluetooth RFCOMM spec, TS 07.10 5.4.6.3.10
#
const
  RLS_ERROR* = 0x00000001
  RLS_OVERRUN* = 0x00000002
  RLS_PARITY* = 0x00000004
  RLS_FRAMING* = 0x00000008
type
  RFCOMM_RLS_DATA* = object
    LineStatus*: UCHAR

  PRFCOMM_RLS_DATA* = ptr RFCOMM_RLS_DATA
#
# Structure definition from Bluetooth RFCOMM spec, TS 07.10 5.4.6.3.9
#
const
  RPN_BAUD_2400* = 0
  RPN_BAUD_4800* = 1
  RPN_BAUD_7200* = 2
  RPN_BAUD_9600* = 3
  RPN_BAUD_19200* = 4
  RPN_BAUD_38400* = 5
  RPN_BAUD_57600* = 6
  RPN_BAUD_115200* = 7
  RPN_BAUD_230400* = 8
  RPN_DATA_5* = 0x00000000
  RPN_DATA_6* = 0x00000001
  RPN_DATA_7* = 0x00000002
  RPN_DATA_8* = 0x00000003
  RPN_STOP_1* = 0x00000000
  RPN_STOP_1_5* = 0x00000004
  RPN_PARITY_NONE* = 0x00000000
  RPN_PARITY_ODD* = 0x00000008
  RPN_PARITY_EVEN* = 0x00000018
  RPN_PARITY_MARK* = 0x00000028
  RPN_PARITY_SPACE* = 0x00000038
  RPN_FLOW_X_IN* = 0x00000001
  RPN_FLOW_X_OUT* = 0x00000002
  RPN_FLOW_RTR_IN* = 0x00000004
  RPN_FLOW_RTR_OUT* = 0x00000008
  RPN_FLOW_RTC_IN* = 0x00000010
  RPN_FLOW_RTC_OUT* = 0x00000020
  RPN_PARAM_BAUD* = 0x00000001
  RPN_PARAM_DATA* = 0x00000002
  RPN_PARAM_STOP* = 0x00000004
  RPN_PARAM_PARITY* = 0x00000008
  RPN_PARAM_P_TYPE* = 0x00000010
  RPN_PARAM_XON* = 0x00000020
  RPN_PARAM_XOFF* = 0x00000040
  RPN_PARAM_X_IN* = 0x00000001
  RPN_PARAM_X_OUT* = 0x00000002
  RPN_PARAM_RTR_IN* = 0x00000004
  RPN_PARAM_RTR_OUT* = 0x00000008
  RPN_PARAM_RTC_IN* = 0x00000010
  RPN_PARAM_RTC_OUT* = 0x00000020
type
  RFCOMM_RPN_DATA* = object
    Baud*: UCHAR
    Data*: UCHAR
    FlowControl*: UCHAR
    XonChar*: UCHAR
    XoffChar*: UCHAR
    ParameterMask1*: UCHAR
    ParameterMask2*: UCHAR

  PRFCOMM_RPN_DATA* = ptr RFCOMM_RPN_DATA
const
  RFCOMM_CMD_NONE* = 0
  RFCOMM_CMD_MSC* = 1
  RFCOMM_CMD_RLS* = 2
  RFCOMM_CMD_RPN* = 3
  RFCOMM_CMD_RPN_REQUEST* = 4
  RFCOMM_CMD_RPN_RESPONSE* = 5
#      RESERVED_CMD                6
type
  INNER_C_UNION_112492126* = object  {.union.}
    MSC*: RFCOMM_MSC_DATA
    RLS*: RFCOMM_RLS_DATA
    RPN*: RFCOMM_RPN_DATA

type
  RFCOMM_COMMAND* = object
    CmdType*: ULONG       # one of RFCOMM_CMD_*
    Data*: INNER_C_UNION_112492126

  PRFCOMM_COMMAND* = ptr RFCOMM_COMMAND
#
# These structures are for test/validation/conformance and may only be
# present in debug/checked builds of the system
#
type
  INNER_C_UNION_301228124* = object  {.union.}
    connectionlessMTU*: USHORT
    data*: array[MAX_L2CAP_INFO_DATA_LENGTH, UCHAR]

type
  BTH_PING_REQ* = object
    btAddr*: BTH_ADDR
    dataLen*: UCHAR
    data*: array[MAX_L2CAP_PING_DATA_LENGTH, UCHAR]

  PBTH_PING_REQ* = ptr BTH_PING_REQ
  BTH_PING_RSP* = object
    dataLen*: UCHAR
    data*: array[MAX_L2CAP_PING_DATA_LENGTH, UCHAR]

  PBTH_PING_RSP* = ptr BTH_PING_RSP
  BTH_INFO_REQ* = object
    btAddr*: BTH_ADDR
    infoType*: USHORT

  PBTH_INFO_REQ* = ptr BTH_INFO_REQ
  BTH_INFO_RSP* = object
    result*: USHORT
    dataLen*: UCHAR
    ano_2541354110*: INNER_C_UNION_301228124

  PBTH_INFO_RSP* = ptr BTH_INFO_RSP
#
# WinCE compatible struct names
#
type
  BTHNS_SETBLOB* = BTH_SET_SERVICE
  PBTHNS_SETBLOB* = ptr BTH_SET_SERVICE
  BTHNS_INQUIRYBLOB* = BTH_QUERY_DEVICE
  PBTHNS_INQUIRYBLOB* = ptr BTH_QUERY_DEVICE
  BTHNS_RESTRICTIONBLOB* = BTH_QUERY_SERVICE
  PBTHNS_RESTRICTIONBLOB* = ptr BTH_QUERY_SERVICE