blob: ff0f3bdc82ccef9acd2cacd3d60402aefab7fc37 [file] [log] [blame]
Szymon Starzycki2a656c32013-09-05 14:26:28 -07001/*
2 * Copyright (c) 2009-2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <stdlib.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <unistd.h>
36#include <cutils/sockets.h>
37
38#include "debug.h"
39#include "transport.h"
40#include "utils.h"
41
42
43#define container_of(ptr, type, member) \
44 ((type*)((char*)(ptr) - offsetof(type, member)))
45
46#define SOCKET_WORKING 0
47#define SOCKET_STOPPED -1
48
49
50struct socket_transport {
51 struct transport transport;
52
53 int fd;
54};
55
56struct socket_handle {
57 struct transport_handle handle;
58
59 int fd;
60};
61
62void socket_close(struct transport_handle *thandle)
63{
64 struct socket_handle * handle = container_of(thandle, struct socket_handle, handle);
65 close(handle->fd);
66}
67
68struct transport_handle *socket_connect(struct transport *transport)
69{
70 struct socket_handle *handle = calloc(sizeof(struct socket_handle), 1);
71 struct socket_transport *socket_transport = container_of(transport, struct socket_transport, transport);
72 struct sockaddr addr;
73 socklen_t alen = sizeof(addr);
74
75 handle->fd = accept(socket_transport->fd, &addr, &alen);
76
77 if (handle->fd < 0) {
78 D(WARN, "socket connect error");
79 return NULL;
80 }
81
82 D(DEBUG, "[ socket_thread - registering device ]");
83 return &handle->handle;
84}
85
86ssize_t socket_write(struct transport_handle *thandle, const void *data, size_t len)
87{
88 ssize_t ret;
89 struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
90
91 D(DEBUG, "about to write (fd=%d, len=%d)", handle->fd, len);
92 ret = bulk_write(handle->fd, data, len);
93 if (ret < 0) {
94 D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
95 return -1;
96 }
97 D(DEBUG, "[ socket_write done fd=%d ]", handle->fd);
98 return ret;
99}
100
101ssize_t socket_read(struct transport_handle *thandle, void *data, size_t len)
102{
103 ssize_t ret;
104 struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
105
106 D(DEBUG, "about to read (fd=%d, len=%d)", handle->fd, len);
107 ret = bulk_read(handle->fd, data, len);
108 if (ret < 0) {
109 D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
110 return -1;
111 }
112 D(DEBUG, "[ socket_read done fd=%d ret=%zd]", handle->fd, ret);
113 return ret;
114}
115
116static int listen_socket_init(struct socket_transport *socket_transport)
117{
118 int s = android_get_control_socket("fastbootd");
119
120 if (s < 0) {
121 D(WARN, "android_get_control_socket(fastbootd): %s\n", strerror(errno));
122 return 0;
123 }
124
125 if (listen(s, 4) < 0) {
126 D(WARN, "listen(control socket): %s\n", strerror(errno));
127 return 0;
128 }
129
130 socket_transport->fd = s;
131
132 return 1;
133}
134
135
136int transport_socket_init()
137{
138 struct socket_transport *socket_transport = malloc(sizeof(struct socket_transport));
Szymon Starzycki55b61f42013-10-03 11:06:45 -0700139
Szymon Starzycki2a656c32013-09-05 14:26:28 -0700140 socket_transport->transport.connect = socket_connect;
141 socket_transport->transport.close = socket_close;
142 socket_transport->transport.read = socket_read;
143 socket_transport->transport.write = socket_write;
Szymon Starzycki55b61f42013-10-03 11:06:45 -0700144
Szymon Starzycki2a656c32013-09-05 14:26:28 -0700145 if (!listen_socket_init(socket_transport)) {
146 D(ERR, "socket transport init failed");
147 free(socket_transport);
148 return 0;
149 }
150
151 transport_register(&socket_transport->transport);
152 return 1;
153}
154