2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* this file contains various functions used by all libhardware modules
18 * that support QEMU emulation
21 #define LOG_TAG "hardware-qemu"
22 #include <cutils/log.h>
23 #include <cutils/properties.h>
24 #include <cutils/sockets.h>
34 # define D(...) LOGD(__VA_ARGS__)
36 # define D(...) ((void)0)
43 static int in_qemu = -1;
45 if (__builtin_expect(in_qemu < 0,0)) {
46 char propBuf[PROPERTY_VALUE_MAX];
47 property_get("ro.kernel.qemu", propBuf, "");
48 in_qemu = (propBuf[0] == '1');
54 qemu_fd_write( int fd, const char* cmd, int len )
58 len2 = write(fd, cmd, len);
59 } while (len2 < 0 && errno == EINTR);
64 qemu_fd_read( int fd, char* buff, int len )
68 len2 = read(fd, buff, len);
69 } while (len2 < 0 && errno == EINTR);
75 qemu_channel_open_qemud( QemuChannel* channel,
78 int fd, ret, namelen = strlen(name);
81 fd = socket_local_client( "qemud",
82 ANDROID_SOCKET_NAMESPACE_RESERVED,
85 D("no qemud control socket: %s", strerror(errno));
89 /* send service name to connect */
90 if (qemu_fd_write(fd, name, namelen) != namelen) {
91 D("can't send service name to qemud: %s",
97 /* read answer from daemon */
98 if (qemu_fd_read(fd, answer, 2) != 2 ||
99 answer[0] != 'O' || answer[1] != 'K') {
100 D("cant' connect to %s service through qemud", name);
105 channel->is_qemud = 1;
112 qemu_channel_open_qemud_old( QemuChannel* channel,
117 snprintf(channel->device, sizeof channel->device,
120 fd = socket_local_client( channel->device,
121 ANDROID_SOCKET_NAMESPACE_RESERVED,
124 D("no '%s' control socket available: %s",
125 channel->device, strerror(errno));
130 channel->is_qemud_old = 1;
136 qemu_channel_open_tty( QemuChannel* channel,
140 char key[PROPERTY_KEY_MAX];
141 char prop[PROPERTY_VALUE_MAX];
144 ret = snprintf(key, sizeof key, "ro.kernel.android.%s", name);
145 if (ret >= (int)sizeof key)
148 if (property_get(key, prop, "") == 0) {
149 D("no kernel-provided %s device name", name);
153 ret = snprintf(channel->device, sizeof channel->device,
155 if (ret >= (int)sizeof channel->device) {
156 D("%s device name too long: '%s'", name, prop);
160 channel->is_tty = !memcmp("/dev/tty", channel->device, 8);
165 qemu_channel_open( QemuChannel* channel,
171 /* initialize the channel is needed */
172 if (!channel->is_inited)
174 channel->is_inited = 1;
177 if (qemu_channel_open_qemud(channel, name) == 0)
180 if (qemu_channel_open_qemud_old(channel, name) == 0)
183 if (qemu_channel_open_tty(channel, name, mode) == 0)
186 channel->is_available = 0;
190 channel->is_available = 1;
193 /* try to open the file */
194 if (!channel->is_available) {
199 if (channel->is_qemud) {
200 return dup(channel->fd);
203 if (channel->is_qemud_old) {
205 fd = socket_local_client( channel->device,
206 ANDROID_SOCKET_NAMESPACE_RESERVED,
208 } while (fd < 0 && errno == EINTR);
210 else /* /dev/ttySn ? */
213 fd = open(channel->device, mode);
214 } while (fd < 0 && errno == EINTR);
216 /* disable ECHO on serial lines */
217 if (fd >= 0 && channel->is_tty) {
219 tcgetattr( fd, &ios );
220 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
221 tcsetattr( fd, TCSANOW, &ios );
229 qemu_command_vformat( char* buffer,
240 len = vsnprintf(buffer+4, buffer_size-4, format, args);
241 if (len >= buffer_size-4)
244 snprintf(header, sizeof header, "%04x", len);
245 memcpy(buffer, header, 4);
250 qemu_command_format( char* buffer,
258 va_start(args, format);
259 ret = qemu_command_format(buffer, buffer_size, format, args);
266 qemu_control_fd(void)
268 static QemuChannel channel[1];
271 fd = qemu_channel_open( channel, "hw-control", O_RDWR );
273 D("%s: could not open control channel: %s", __FUNCTION__,
280 qemu_control_send(const char* cmd, int len)
289 fd = qemu_control_fd();
293 len2 = qemu_fd_write(fd, cmd, len);
296 D("%s: could not send everything %d < %d",
297 __FUNCTION__, len2, len);
305 qemu_control_command( const char* fmt, ... )
312 len = qemu_command_vformat( command, sizeof command, fmt, args );
315 if (len < 0 || len >= (int)sizeof command) {
317 D("%s: could not send: %s", __FUNCTION__, strerror(errno));
319 D("%s: too large %d > %d", __FUNCTION__, len, (int)(sizeof command));
325 return qemu_control_send( command, len );
328 extern int qemu_control_query( const char* question, int questionlen,
329 char* answer, int answersize )
331 int ret, fd, len, result = -1;
332 char header[5], *end;
334 if (questionlen <= 0) {
339 fd = qemu_control_fd();
343 ret = qemu_fd_write( fd, question, questionlen );
344 if (ret != questionlen) {
345 D("%s: could not write all: %d < %d", __FUNCTION__,
350 /* read a 4-byte header giving the length of the following content */
351 ret = qemu_fd_read( fd, header, 4 );
353 D("%s: could not read header (%d != 4)",
359 len = strtol( header, &end, 16 );
360 if ( len < 0 || end == NULL || end != header+4 || len > answersize ) {
361 D("%s: could not parse header: '%s'",
362 __FUNCTION__, header);
366 /* read the answer */
367 ret = qemu_fd_read( fd, answer, len );
369 D("%s: could not read all of answer %d < %d",
370 __FUNCTION__, ret, len);