chromeos: bluetooth: add BluetoothInputClient
[chromium-blink-merge.git] / tools / android / common / adb_connection.cc
blob4739d1d761dd188b1d137d6c1e6960701aa6a9a3
1 // Copyright (c) 2012 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.
5 #include "tools/android/common/adb_connection.h"
7 #include <arpa/inet.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include <unistd.h>
14 #include "base/eintr_wrapper.h"
15 #include "base/logging.h"
16 #include "tools/android/common/net.h"
18 namespace tools {
20 int ConnectAdbHostSocket(const char* forward_to) {
21 // ADB port forward request format: HHHHtcp:port:address.
22 // HHHH is the hexidecimal length of the "tcp:port:address" part.
23 const size_t kBufferMaxLength = 30;
24 const size_t kLengthOfLength = 4;
25 const size_t kAddressMaxLength = kBufferMaxLength - kLengthOfLength;
27 const char kAddressPrefix[] = { 't', 'c', 'p', ':' };
28 size_t address_length = arraysize(kAddressPrefix) + strlen(forward_to);
29 if (address_length > kBufferMaxLength - kLengthOfLength) {
30 LOG(ERROR) << "Forward to address is too long: " << forward_to;
31 return -1;
34 char request[kBufferMaxLength];
35 memcpy(request + kLengthOfLength, kAddressPrefix, arraysize(kAddressPrefix));
36 memcpy(request + kLengthOfLength + arraysize(kAddressPrefix),
37 forward_to, strlen(forward_to));
39 char length_buffer[kLengthOfLength + 1];
40 snprintf(length_buffer, arraysize(length_buffer), "%04X",
41 static_cast<int>(address_length));
42 memcpy(request, length_buffer, kLengthOfLength);
44 int host_socket = socket(AF_INET, SOCK_STREAM, 0);
45 if (host_socket < 0) {
46 LOG(ERROR) << "Failed to create adb socket: " << strerror(errno);
47 return -1;
50 DisableNagle(host_socket);
52 const int kAdbPort = 5037;
53 sockaddr_in addr;
54 memset(&addr, 0, sizeof(addr));
55 addr.sin_family = AF_INET;
56 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
57 addr.sin_port = htons(kAdbPort);
58 if (HANDLE_EINTR(connect(host_socket, reinterpret_cast<sockaddr*>(&addr),
59 sizeof(addr))) < 0) {
60 LOG(ERROR) << "Failed to connect adb socket: " << strerror(errno);
61 HANDLE_EINTR(close(host_socket));
62 return -1;
65 size_t bytes_remaining = address_length + kLengthOfLength;
66 size_t bytes_sent = 0;
67 while (bytes_remaining > 0) {
68 int ret = HANDLE_EINTR(send(host_socket, request + bytes_sent,
69 bytes_remaining, 0));
70 if (ret < 0) {
71 LOG(ERROR) << "Failed to send request: " << strerror(errno);
72 HANDLE_EINTR(close(host_socket));
73 return -1;
76 bytes_sent += ret;
77 bytes_remaining -= ret;
80 const size_t kAdbStatusLength = 4;
81 char response[kBufferMaxLength];
82 int response_length = HANDLE_EINTR(recv(host_socket, response,
83 kBufferMaxLength, 0));
84 if (response_length < kAdbStatusLength ||
85 strncmp("OKAY", response, kAdbStatusLength) != 0) {
86 LOG(ERROR) << "Bad response from ADB: length: " << response_length
87 << " data: " << DumpBinary(response, response_length);
88 HANDLE_EINTR(close(host_socket));
89 return -1;
92 return host_socket;
95 } // namespace tools