Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: device/bluetooth/bluetooth_socket_experimental_chromeos.cc

Issue 14487002: Bluetooth: Profile support for Chrome OS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Handle EINTR. Detect disconnection during Receive and Send Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "device/bluetooth/bluetooth_socket_experimental_chromeos.h"
6
7 #include <errno.h>
8 #include <poll.h>
9 #include <unistd.h>
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13
14 #include <string>
15
16 #include "base/logging.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/safe_strerror_posix.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "dbus/file_descriptor.h"
22 #include "device/bluetooth/bluetooth_socket.h"
23 #include "net/base/io_buffer.h"
24
25 namespace chromeos {
26
27 BluetoothSocketExperimentalChromeOS::BluetoothSocketExperimentalChromeOS(
28 int fd)
29 : fd_(fd) {
30 // Fetch the socket type so we read from it correctly.
31 int optval;
32 socklen_t opt_len = sizeof optval;
33 if (getsockopt(fd_, SOL_SOCKET, SO_TYPE, &optval, &opt_len) < 0) {
34 // Sequenced packet is the safest assumption since it won't result in
35 // truncated packets.
36 LOG(WARNING) << "Unable to get socket type: " << safe_strerror(errno);
37 optval = SOCK_SEQPACKET;
38 }
39
40 if (optval == SOCK_DGRAM || optval == SOCK_SEQPACKET) {
41 socket_type_ = L2CAP;
42 } else {
43 socket_type_ = RFCOMM;
44 }
45 }
46
47 BluetoothSocketExperimentalChromeOS::~BluetoothSocketExperimentalChromeOS() {
48 HANDLE_EINTR(close(fd_));
49 }
50
51 bool BluetoothSocketExperimentalChromeOS::Receive(
52 net::GrowableIOBuffer *buffer) {
53 base::ThreadRestrictions::AssertIOAllowed();
54
55 if (socket_type_ == L2CAP) {
56 int count;
57 if (ioctl(fd_, FIONREAD, &count) < 0) {
58 error_message_ = safe_strerror(errno);
59 LOG(WARNING) << "Unable to get waiting data size: " << error_message_;
60 return true;
61 }
62
63 // No bytes waiting can mean either nothing to read, or the other end has
64 // been closed, and reading zero bytes always returns zero.
65 //
66 // We can't do a short read for fear of a race where data arrives between
67 // calls and we trunctate it. So use poll() to check for the POLLHUP flag.
68 if (count == 0) {
69 struct pollfd pollfd;
70
71 pollfd.fd = fd_;
72 pollfd.events = 0;
73 pollfd.revents = 0;
74
75 if (HANDLE_EINTR(poll(&pollfd, 1, 0)) < 0) {
satorux1 2013/05/07 08:07:16 I need to check the manual to confirm that this wo
76 error_message_ = safe_strerror(errno);
77 LOG(WARNING) << "Unable to check whether socket is closed: "
78 << error_message_;
79 return false;
80 }
81
82 if (pollfd.revents & POLLHUP) {
83 // TODO(keybuk, youngki): Agree a common way to flag disconnected.
84 error_message_ = "Disconnected";
85 return false;
86 }
87 }
88
89 buffer->SetCapacity(count);
90 } else {
91 buffer->SetCapacity(1024);
92 }
93
94 ssize_t bytes_read;
95 do {
96 if (buffer->RemainingCapacity() == 0)
97 buffer->SetCapacity(buffer->capacity() * 2);
98 bytes_read =
99 HANDLE_EINTR(read(fd_, buffer->data(), buffer->RemainingCapacity()));
100 if (bytes_read > 0)
101 buffer->set_offset(buffer->offset() + bytes_read);
102 } while (socket_type_ == RFCOMM && bytes_read > 0);
103
104 // Ignore an error if at least one read() call succeeded; it'll be returned
105 // the next read() call.
106 if (buffer->offset() > 0)
107 return true;
108
109 if (bytes_read < 0) {
110 if (errno == ECONNRESET || errno == ENOTCONN) {
111 // TODO(keybuk, youngki): Agree a common way to flag disconnected.
112 error_message_ = "Disconnected";
113 return false;
114 } else if (errno != EAGAIN && errno != EWOULDBLOCK) {
115 error_message_ = safe_strerror(errno);
116 return false;
117 }
118 }
119
120 if (bytes_read == 0 && socket_type_ == RFCOMM) {
121 // TODO(keybuk, youngki): Agree a common way to flag disconnected.
122 error_message_ = "Disconnected";
123 return false;
124 }
125
126 return true;
127 }
128
129 bool BluetoothSocketExperimentalChromeOS::Send(
130 net::DrainableIOBuffer *buffer) {
131 base::ThreadRestrictions::AssertIOAllowed();
132
133 ssize_t bytes_written;
134 do {
135 bytes_written =
136 HANDLE_EINTR(write(fd_, buffer->data(), buffer->BytesRemaining()));
137 if (bytes_written > 0)
138 buffer->DidConsume(bytes_written);
139 } while (buffer->BytesRemaining() > 0 && bytes_written > 0);
140
141 if (bytes_written < 0) {
142 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
143 // TODO(keybuk, youngki): Agree a common way to flag disconnected.
144 error_message_ = "Disconnected";
145 return false;
146 } else if (errno != EAGAIN && errno != EWOULDBLOCK) {
147 error_message_ = safe_strerror(errno);
148 return false;
149 }
150 }
151
152 return true;
153 }
154
155 std::string BluetoothSocketExperimentalChromeOS::GetLastErrorMessage() const {
156 return error_message_;
157 }
158
159 // static
160 scoped_refptr<device::BluetoothSocket>
161 BluetoothSocketExperimentalChromeOS::Create(dbus::FileDescriptor* fd) {
162 DCHECK(fd->is_valid());
163
164 BluetoothSocketExperimentalChromeOS* bluetooth_socket =
165 new BluetoothSocketExperimentalChromeOS(fd->TakeValue());;
166 return scoped_refptr<BluetoothSocketExperimentalChromeOS>(bluetooth_socket);
167 }
168
169 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698