aboutsummaryrefslogtreecommitdiff
path: root/nimbluez/bluetoothmsbt.nim
blob: 4d10392087f7ce9749906840c8c30318285d44c5 (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
# Copyright (c) 2016, Maxim V. Abramov
# All rights reserved.
# Look at license.txt for more info.

## This module is used for discovery and managing Bluetooth devices and
## services. It is based on Microsoft Bluetooth protocol stack implementation
## for Windows.

import os, strutils, algorithm, sequtils, winlean
import msbt/ms_bluetoothapis, msbt/ms_bthsdpdef, msbt/ms_bthdef

export BLUETOOTH_ADDRESS, BLUETOOTH_RADIO_INFO, BLUETOOTH_DEVICE_INFO

const
  ERROR_NO_MORE_ITEMS* = OSErrorCode(259)

type
  BluetoothDeviceLocalImpl* = object of RootObj ## Local Bluetooth device -
                                                ## radio adapter.
    fRadioInfo*: BLUETOOTH_RADIO_INFO

  BluetoothDeviceLocal* = ref BluetoothDeviceLocalImpl ## Local Bluetooth
                                                       ## device - radio
                                                       ## adapter reference.

  BluetoothDeviceRemoteImpl* = object of RootObj ## Remote Bluetooth device.
    fDeviceInfo*: BLUETOOTH_DEVICE_INFO

  BluetoothDeviceRemote* = ref BluetoothDeviceRemoteImpl ## Remote Bluetooth
                                                         ## device reference.


proc CloseHandle(hObject: HANDLE): WINBOOL{.stdcall, dynlib: "kernel32",
     importc: "CloseHandle".}


proc `$`*(ba: BLUETOOTH_ADDRESS): string =
  ## Returns Bluetooth device address as a string.
  let ab = map(ba.ano_116103095.rgBytes,
               proc(b: byte): string = toHex(BiggestInt(b), 2))
  result = ab.reversed.join(":")


proc parseBluetoothAddress*(str: string): BLUETOOTH_ADDRESS =
  ## Converts a string to a Bluetooth device address.
  var intVals = map(str.split(':'),
                    proc(s: string): int = parseHexInt(s)).reversed()
  for i in 0..5:
    result.ano_116103095.rgBytes[i] = byte(intVals[i])


proc `==`*(x, y: BLUETOOTH_ADDRESS): bool =
  ## Converts a string to a Bluetooth device address.
  x.ano_116103095.ullLong == y.ano_116103095.ullLong


iterator localDeviceHandles*():
  tuple[handle: int , info: BLUETOOTH_RADIO_INFO] {.inline.} =
  ## Iterates through the local Bluetooth devices. Closes current device
  ## ''handle'' at the end of the each iteration.
  var
    findRadioParams: BLUETOOTH_FIND_RADIO_PARAMS
    findRadioHandle: HBLUETOOTH_RADIO_FIND
    radioHandle: int
    errorCode: OSErrorCode

  findRadioParams.dwSize = DWORD(sizeof(findRadioParams))
  try:
    findRadioHandle = BluetoothFindFirstRadio(addr(findRadioParams),
                                              addr(radioHandle))
    if findRadioHandle == 0:
      errorCode = osLastError()
      if errorCode != ERROR_NO_MORE_ITEMS:
        raiseOSError(errorCode)
    else:
      while true:
        var radioInfo: BLUETOOTH_RADIO_INFO
        radioInfo.dwSize = DWORD(sizeof(radioInfo))

        errorCode = BluetoothGetRadioInfo(radioHandle, addr(radioInfo))
          .OSErrorCode
        if errorCode != OSErrorCode(NO_ERROR):
          raiseOSError(errorCode)

        yield (handle: radioHandle, info: radioInfo)

        if CloseHandle(radioHandle) == 0:
          raiseOSError(osLastError())
        radioHandle = 0;

        if BluetoothFindNextRadio(findRadioHandle, addr(radioHandle)) == 0:
          errorCode = osLastError()
          if errorCode != ERROR_NO_MORE_ITEMS:
            raiseOSError(errorCode)
          break
  finally:
    if findRadioHandle != 0:
      if radioHandle != 0:
        if CloseHandle(radioHandle) == 0:
          raiseOSError(osLastError())
      if BluetoothFindRadioClose(findRadioHandle) == 0:
        raiseOSError(osLastError())


iterator getLocalDevices*(): BluetoothDeviceLocal {.inline.} =
  ## Returns all local Blutooth radio devices.
  for item in localDeviceHandles():
    yield BluetoothDeviceLocal(fRadioInfo: item.info)


proc getLocalDevice*(): BluetoothDeviceLocal =
  ## Returns default local Bluetooth radio device.
  for item in getLocalDevices():
    return item
  raiseOSError(ERROR_NO_MORE_ITEMS)


proc getLocalDevice*(address: string): BluetoothDeviceLocal =
  ## Returns local Bluetooth radio device with a specified address.
  let ba = parseBluetoothAddress(address)
  for item in getLocalDevices():
    if item.fRadioInfo.address == ba:
      return item
  raiseOSError(ERROR_NO_MORE_ITEMS)


proc getRemoteDevice*(address: string): BluetoothDeviceRemote =
  ## Returns remote Bluetooth device with a specified address.
  new(result)
  result.fDeviceInfo.Address = address.parseBluetoothAddress


iterator remoteDeviceInfos*(radioHandle: HANDLE;
                            timeoutMultiplier: UCHAR;
                            issueInquiry: BOOL):
                            BLUETOOTH_DEVICE_INFO {.inline.} =
  ## Iterates through the all remote Bluetooth devices visible for the
  ## specified local Bluetooth radio device.
  var
    deviceSearchParams: BLUETOOTH_DEVICE_SEARCH_PARAMS
    deviceFindHandle: HBLUETOOTH_DEVICE_FIND
    errorCode: OSErrorCode
    deviceInfo: BLUETOOTH_DEVICE_INFO

  deviceSearchParams.dwSize = DWORD(sizeof(deviceSearchParams))
  deviceSearchParams.fReturnAuthenticated = BOOL(true)
  deviceSearchParams.fReturnRemembered = BOOL(true)
  deviceSearchParams.fReturnUnknown = BOOL(true)
  deviceSearchParams.fReturnConnected = BOOL(true)
  deviceSearchParams.fIssueInquiry = issueInquiry
  deviceSearchParams.cTimeoutMultiplier = timeoutMultiplier
  deviceSearchParams.hRadio = radioHandle

  deviceInfo.dwSize = DWORD(sizeof(deviceInfo))

  try:
    deviceFindHandle = BluetoothFindFirstDevice(addr(deviceSearchParams),
                                                addr(deviceInfo))
    if deviceFindHandle == 0:
      errorCode = osLastError()
      if errorCode != ERROR_NO_MORE_ITEMS:
        raiseOSError(errorCode)
    else:
      yield deviceInfo

      while true:
        var deviceInfoLoop: BLUETOOTH_DEVICE_INFO
        deviceInfoLoop.dwSize = DWORD(sizeof(deviceInfoLoop))

        if BluetoothFindNextDevice(deviceFindHandle, addr(deviceInfoLoop)) == 0:
          errorCode = osLastError()
          if errorCode != ERROR_NO_MORE_ITEMS:
            raiseOSError(errorCode)
          break

        yield deviceInfoLoop
  finally:
    if deviceFindHandle != 0:
      if BluetoothFindDeviceClose(deviceFindHandle) == 0:
        raiseOSError(errorCode)


proc getRemoteDevices*(device: BluetoothDeviceLocal = nil;
                       duration: int = 8;
                       flushCache: bool = true): seq[BluetoothDeviceRemote] =
  ## Returns all remote Bluetooth devices visible for the specified or default
  ## local Bluetooth radio device.
  result = @[]

  if device != nil:
    for item in localDeviceHandles():
      if item.info.address == device.fRadioInfo.address:
        for deviceInfo in remoteDeviceInfos(item.handle, UCHAR(duration),
                                            BOOL(flushCache)):
          result.add(BluetoothDeviceRemote(fDeviceInfo: deviceInfo))
  else:
    for deviceInfo in remoteDeviceInfos(0, UCHAR(duration), BOOL(flushCache)):
      result.add(BluetoothDeviceRemote(fDeviceInfo: deviceInfo))


proc address*(device: BluetoothDeviceLocal): string =
  ## Returns local Bluetooth device address as a string.
  $device.fRadioInfo.address


proc address*(device: BluetoothDeviceRemote): string =
  ## Returns remote Bluetooth device address as a string.
  $device.fDeviceInfo.Address


proc name*(device: BluetoothDeviceLocal): string =
  ## Returns local Bluetooth device name.
  var buf: array[BLUETOOTH_MAX_NAME_SIZE + 1, WCHAR]
  copyMem(addr(buf), addr(device.fRadioInfo.szName), BLUETOOTH_MAX_NAME_SIZE)
  return cast[WideCString](addr(buf)) $ BLUETOOTH_MAX_NAME_SIZE


proc name*(device: BluetoothDeviceRemote): string =
  ## Returns remote Bluetooth device name.
  var buf: array[BLUETOOTH_MAX_NAME_SIZE + 1, WCHAR]
  copyMem(addr(buf), addr(device.fDeviceInfo.szName), BLUETOOTH_MAX_NAME_SIZE)
  return cast[WideCString](addr(buf)) $ BLUETOOTH_MAX_NAME_SIZE