1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16#ifndef _libs_hardware_qemu_h
17#define _libs_hardware_qemu_h
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#ifdef QEMU_HARDWARE
24
25/* returns 1 iff we're running in the emulator */
26extern int  qemu_check(void);
27
28/* a structure used to hold enough state to connect to a given
29 * QEMU communication channel, either through a qemud socket or
30 * a serial port.
31 *
32 * initialize the structure by zero-ing it out
33 */
34typedef struct {
35    char   is_inited;
36    char   is_available;
37    char   is_qemud;
38    char   is_qemud_old;
39    char   is_tty;
40    int    fd;
41    char   device[32];
42} QemuChannel;
43
44/* try to open a qemu communication channel.
45 * returns a file descriptor on success, or -1 in case of
46 * error.
47 *
48 * 'channel' must be a QemuChannel structure that is empty
49 * on the first call. You can call this function several
50 * time to re-open the channel using the same 'channel'
51 * object to speed things a bit.
52 */
53extern int  qemu_channel_open( QemuChannel*  channel,
54                               const char*   name,
55                               int           mode );
56
57/* create a command made of a 4-hexchar prefix followed
58 * by the content. the prefix contains the content's length
59 * in hexadecimal coding.
60 *
61 * 'buffer' must be at last 6 bytes
62 * returns -1 in case of overflow, or the command's total length
63 * otherwise (i.e. content length + 4)
64 */
65extern int  qemu_command_format( char*        buffer,
66                                 int          buffer_size,
67                                 const char*  format,
68                                 ... );
69
70/* directly sends a command through the 'hw-control' channel.
71 * this will open the channel, send the formatted command, then
72 * close the channel automatically.
73 * returns 0 on success, or -1 on error.
74 */
75extern int  qemu_control_command( const char*  fmt, ... );
76
77/* sends a question to the hw-control channel, then receive an answer in
78 * a user-allocated buffer. returns the length of the answer, or -1
79 * in case of error.
80 *
81 * 'question' *must* have been formatted through qemu_command_format
82 */
83extern int  qemu_control_query( const char*  question, int  questionlen,
84                                char*        answer,   int  answersize );
85
86#endif /* QEMU_HARDWARE */
87
88/* use QEMU_FALLBACK(call) to call a QEMU-specific callback  */
89/* use QEMU_FALLBACK_VOID(call) if the function returns void */
90#ifdef QEMU_HARDWARE
91#  define  QEMU_FALLBACK(x)  \
92    do { \
93        if (qemu_check()) \
94            return qemu_ ## x ; \
95    } while (0)
96#  define  QEMU_FALLBACK_VOID(x)  \
97    do { \
98        if (qemu_check()) { \
99            qemu_ ## x ; \
100            return; \
101        } \
102    } while (0)
103#else
104#  define  QEMU_FALLBACK(x)       ((void)0)
105#  define  QEMU_FALLBACK_VOID(x)  ((void)0)
106#endif
107
108#ifdef __cplusplus
109}
110#endif
111
112#endif /* _libs_hardware_qemu_h */
113