summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_adapter_win.cc
blob: 26a9fc5ae01d8ac3122d23b78662076c218d9f12 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// TODO(youngki): Implement this file.

#include "device/bluetooth/bluetooth_adapter_win.h"

#include <BluetoothAPIs.h>
#if defined(_WIN32_WINNT_WIN8)
// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h.
#undef FACILITY_VISUALCPP
#endif
#include <delayimp.h>
#include <string>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
#include "base/sys_string_conversions.h"
#include "base/threading/thread_restrictions.h"

#pragma comment(lib, "Bthprops.lib")
#pragma comment(lib, "delayimp.lib")

namespace {

const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param =
    { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };

// A frame-based exception handler filter function for a handler for exceptions
// generated by the Visual C++ delay loader helper function.
int FilterVisualCPPExceptions(DWORD exception_code) {
  return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ?
      EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
}

// Returns true if the machine has a bluetooth stack available. The first call
// to this function will involve file IO, so it should be done on an
// appropriate thread. This function is not threadsafe.
bool HasBluetoothStack() {
  static enum {
    HBS_UNKNOWN,
    HBS_YES,
    HBS_NO,
  } has_bluetooth_stack = HBS_UNKNOWN;

  if (has_bluetooth_stack == HBS_UNKNOWN) {
    base::ThreadRestrictions::AssertIOAllowed();
    HRESULT hr = E_FAIL;
    __try {
      hr = __HrLoadAllImportsForDll("bthprops.cpl");
    } __except(FilterVisualCPPExceptions(::GetExceptionCode())) {
      hr = E_FAIL;
    }
    has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO;
  }

  return has_bluetooth_stack == HBS_YES;
}

}  // namespace

namespace device {

const int BluetoothAdapterWin::kPollIntervalMs = 500;

BluetoothAdapterWin::BluetoothAdapterWin()
    : BluetoothAdapter(),
      powered_(false),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
}

BluetoothAdapterWin::~BluetoothAdapterWin() {
}

void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) {
  NOTIMPLEMENTED();
}

void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) {
  NOTIMPLEMENTED();
}

bool BluetoothAdapterWin::IsPresent() const {
  return !address_.empty();
}

bool BluetoothAdapterWin::IsPowered() const {
  return powered_;
}

void BluetoothAdapterWin::SetPowered(
    bool powered,
    const base::Closure& callback,
    const ErrorCallback& error_callback) {
  NOTIMPLEMENTED();
}

bool BluetoothAdapterWin::IsDiscovering() const {
  NOTIMPLEMENTED();
  return false;
}

void BluetoothAdapterWin::SetDiscovering(
    bool discovering,
    const base::Closure& callback,
    const ErrorCallback& error_callback) {
  NOTIMPLEMENTED();
}

BluetoothAdapter::ConstDeviceList BluetoothAdapterWin::GetDevices() const {
  NOTIMPLEMENTED();
  return BluetoothAdapter::ConstDeviceList();
}

BluetoothDevice* BluetoothAdapterWin::GetDevice(const std::string& address) {
  NOTIMPLEMENTED();
  return NULL;
}

const BluetoothDevice* BluetoothAdapterWin::GetDevice(
    const std::string& address) const {
  NOTIMPLEMENTED();
  return NULL;
}

void BluetoothAdapterWin::ReadLocalOutOfBandPairingData(
    const BluetoothOutOfBandPairingDataCallback& callback,
    const ErrorCallback& error_callback) {
  NOTIMPLEMENTED();
}

void BluetoothAdapterWin::UpdateAdapterState() {
  // TODO(youngki): Move this check to the earliest point reasonable so that no
  // attempts are made to do any bluetooth processing if no BT stack is present.
  if (!HasBluetoothStack())
    return;

  HBLUETOOTH_RADIO_FIND bluetooth_adapter_handle = NULL;
  BLUETOOTH_RADIO_INFO bluetooth_adapter_info =
      { sizeof(BLUETOOTH_RADIO_INFO), 0 };
  HBLUETOOTH_RADIO_FIND bluetooth_handle = BluetoothFindFirstRadio(
      &bluetooth_adapter_param, &bluetooth_adapter_handle);

  if (bluetooth_adapter_handle) {
    if (ERROR_SUCCESS == BluetoothGetRadioInfo(bluetooth_adapter_handle,
                                               &bluetooth_adapter_info)) {
      name_ = base::SysWideToUTF8(bluetooth_adapter_info.szName);
      address_ = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
          bluetooth_adapter_info.address.rgBytes[5],
          bluetooth_adapter_info.address.rgBytes[4],
          bluetooth_adapter_info.address.rgBytes[3],
          bluetooth_adapter_info.address.rgBytes[2],
          bluetooth_adapter_info.address.rgBytes[1],
          bluetooth_adapter_info.address.rgBytes[0]);
      powered_ = BluetoothIsConnectable(bluetooth_adapter_handle) ||
          BluetoothIsDiscoverable(bluetooth_adapter_handle);
    } else {
      name_.clear();
      address_.clear();
      powered_ = false;
    }
  }

  if (bluetooth_handle)
    BluetoothFindRadioClose(bluetooth_handle);
}

void BluetoothAdapterWin::TrackDefaultAdapter() {
  PollAdapterState();
}

void BluetoothAdapterWin::PollAdapterState() {
  UpdateAdapterState();

  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&BluetoothAdapterWin::PollAdapterState,
                 weak_ptr_factory_.GetWeakPtr()),
      base::TimeDelta::FromMilliseconds(kPollIntervalMs));
}

}  // namespace device