summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_socket_win.cc
blob: 01b8eee02d26954737606361cfd3122b37d88805 (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
// 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.

#include "device/bluetooth/bluetooth_socket_win.h"

#include <string>

#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/sys_string_conversions.h"
#include "device/bluetooth/bluetooth_init_win.h"
#include "device/bluetooth/bluetooth_service_record_win.h"
#include "net/base/io_buffer.h"
#include "net/base/winsock_init.h"

namespace {

std::string FormatErrorMessage(DWORD error_code) {
  TCHAR error_msg[1024];
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                0,
                error_code,
                0,
                error_msg,
                1024,
                NULL);
  return base::SysWideToUTF8(error_msg);
}

}  // namespace

namespace device {

BluetoothSocketWin::BluetoothSocketWin(SOCKET fd) : fd_(fd) {
}

BluetoothSocketWin::~BluetoothSocketWin() {
  closesocket(fd_);
}

// static
scoped_refptr<BluetoothSocket> BluetoothSocketWin::CreateBluetoothSocket(
    const BluetoothServiceRecord& service_record) {
  BluetoothSocketWin* bluetooth_socket = NULL;
  if (service_record.SupportsRfcomm()) {
    net::EnsureWinsockInit();
    SOCKET socket_fd = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    SOCKADDR_BTH sa;
    ZeroMemory(&sa, sizeof(sa));
    sa.addressFamily = AF_BTH;
    sa.port = service_record.rfcomm_channel();
    const BluetoothServiceRecordWin* service_record_win =
        static_cast<const BluetoothServiceRecordWin*>(&service_record);
    sa.btAddr = service_record_win->bth_addr();

    int status = connect(socket_fd,
                         reinterpret_cast<SOCKADDR *>(&sa),
                         sizeof(sa));
    DWORD error_code = WSAGetLastError();
    if (status == 0 || error_code == WSAEINPROGRESS) {
      bluetooth_socket =
          new BluetoothSocketWin(socket_fd);
    } else {
      LOG(ERROR) << "Failed to connect bluetooth socket "
          << "(" << service_record.address() << "): "
          << "(" << error_code << ")" << FormatErrorMessage(error_code);
      closesocket(socket_fd);
    }
  }
  // TODO(youngki) add support for L2CAP sockets as well.

  return scoped_refptr<BluetoothSocketWin>(bluetooth_socket);
}

bool BluetoothSocketWin::Receive(net::GrowableIOBuffer* buffer) {
  buffer->SetCapacity(1024);
  int bytes_read;
  do {
    if (buffer->RemainingCapacity() == 0)
      buffer->SetCapacity(buffer->capacity() * 2);
    bytes_read = recv(fd_, buffer->data(), buffer->RemainingCapacity(), 0);
    if (bytes_read > 0)
      buffer->set_offset(buffer->offset() + bytes_read);
  } while (bytes_read > 0);

  DWORD error_code = WSAGetLastError();
  if (bytes_read < 0 && error_code != WSAEWOULDBLOCK) {
    error_message_ = FormatErrorMessage(error_code);
    return false;
  }
  return true;
}

bool BluetoothSocketWin::Send(net::DrainableIOBuffer* buffer) {
  int bytes_written;
  do {
    bytes_written = send(fd_, buffer->data(), buffer->BytesRemaining(), 0);
    if (bytes_written > 0)
      buffer->DidConsume(bytes_written);
  } while (buffer->BytesRemaining() > 0 && bytes_written > 0);

  DWORD error_code = WSAGetLastError();
  if (bytes_written < 0 && error_code != WSAEWOULDBLOCK) {
    error_message_ = FormatErrorMessage(error_code);
    return false;
  }
  return true;
}

std::string BluetoothSocketWin::GetLastErrorMessage() const {
  return error_message_;
}

}  // namespace device