The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2007-2008 The Android Open Source Project |
| 2 | ** |
| 3 | ** This software is licensed under the terms of the GNU General Public |
| 4 | ** License version 2, as published by the Free Software Foundation, and |
| 5 | ** may be copied, distributed, and modified under those terms. |
| 6 | ** |
| 7 | ** This program is distributed in the hope that it will be useful, |
| 8 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | ** GNU General Public License for more details. |
| 11 | */ |
| 12 | #include "android/hw-qemud.h" |
| 13 | #include "android/utils/debug.h" |
| 14 | #include "android/utils/misc.h" |
| 15 | #include "android/utils/system.h" |
| 16 | #include "android/utils/bufprint.h" |
| 17 | #include "qemu-char.h" |
| 18 | #include "charpipe.h" |
| 19 | #include "cbuffer.h" |
| 20 | |
| 21 | #define D(...) VERBOSE_PRINT(qemud,__VA_ARGS__) |
| 22 | #define D_ACTIVE VERBOSE_CHECK(qemud) |
| 23 | |
| 24 | /* the T(...) macro is used to dump traffic */ |
| 25 | #define T_ACTIVE 0 |
| 26 | |
| 27 | #if T_ACTIVE |
| 28 | #define T(...) VERBOSE_PRINT(qemud,__VA_ARGS__) |
| 29 | #else |
| 30 | #define T(...) ((void)0) |
| 31 | #endif |
| 32 | |
| 33 | /* max serial MTU. Don't change this without modifying |
| 34 | * development/emulator/qemud/qemud.c as well. |
| 35 | */ |
| 36 | #define MAX_SERIAL_PAYLOAD 4000 |
| 37 | |
| 38 | /* max framed data payload. Must be < (1 << 16) |
| 39 | */ |
| 40 | #define MAX_FRAME_PAYLOAD 65535 |
| 41 | |
| 42 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 43 | /* define SUPPORT_LEGACY_QEMUD to 1 if you want to support |
| 44 | * talking to a legacy qemud daemon. See docs/ANDROID-QEMUD.TXT |
| 45 | * for details. |
| 46 | */ |
| 47 | #define SUPPORT_LEGACY_QEMUD 1 |
| 48 | |
David Turner | fbcbf42 | 2009-04-15 06:50:16 -0700 | [diff] [blame] | 49 | #if SUPPORT_LEGACY_QEMUD |
| 50 | #include "telephony/android_modem.h" |
| 51 | #include "telephony/modem_driver.h" |
| 52 | #endif |
| 53 | |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 54 | /* |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 55 | * This implements support for the 'qemud' multiplexing communication |
| 56 | * channel between clients running in the emulated system and 'services' |
| 57 | * provided by the emulator. |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 58 | * |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 59 | * For additional details, please read docs/ANDROID-QEMUD.TXT |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 60 | * |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | /* |
| 64 | * IMPLEMENTATION DETAILS: |
| 65 | * |
| 66 | * We use one charpipe to connect the emulated serial port to the 'QemudSerial' |
| 67 | * object. This object is used to receive data from the serial port, and |
| 68 | * unframe messages (i.e. extract payload length + channel id from header, |
| 69 | * then the payload itself), before sending them to a generic receiver. |
| 70 | * |
| 71 | * The QemudSerial object can also be used to send messages to the daemon |
| 72 | * through the serial port (see qemud_serial_send()) |
| 73 | * |
| 74 | * The multiplexer is connected to one or more 'service' objects. |
| 75 | * are themselves connected through a charpipe to an emulated device or |
| 76 | * control sub-module in the emulator. |
| 77 | * |
| 78 | * tty <==charpipe==> QemudSerial ---> QemudMultiplexer ----> QemudClient |
| 79 | * ^ | |
| 80 | * | | |
| 81 | * +--------------------------------------+ |
| 82 | * |
| 83 | */ |
| 84 | |
| 85 | /** HANDLING INCOMING DATA FRAMES |
| 86 | **/ |
| 87 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 88 | /* A QemudSink is just a handly data structure that is used to |
| 89 | * read a fixed amount of bytes into a buffer |
| 90 | */ |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 91 | typedef struct QemudSink { |
| 92 | int len; |
| 93 | int size; |
| 94 | uint8_t* buff; |
| 95 | } QemudSink; |
| 96 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 97 | /* reset a QemudSink, i.e. provide a new destination buffer address |
| 98 | * and its size in bytes. |
| 99 | */ |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 100 | static void |
| 101 | qemud_sink_reset( QemudSink* ss, int size, uint8_t* buffer ) |
| 102 | { |
| 103 | ss->len = 0; |
| 104 | ss->size = size; |
| 105 | ss->buff = buffer; |
| 106 | } |
| 107 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 108 | /* try to fill the sink by reading bytes from the source buffer |
| 109 | * '*pmsg' which contains '*plen' bytes |
| 110 | * |
| 111 | * this functions updates '*pmsg' and '*plen', and returns |
| 112 | * 1 if the sink's destination buffer is full, or 0 otherwise. |
| 113 | */ |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 114 | static int |
| 115 | qemud_sink_fill( QemudSink* ss, const uint8_t* *pmsg, int *plen) |
| 116 | { |
| 117 | int avail = ss->size - ss->len; |
| 118 | |
| 119 | if (avail <= 0) |
| 120 | return 1; |
| 121 | |
| 122 | if (avail > *plen) |
| 123 | avail = *plen; |
| 124 | |
| 125 | memcpy(ss->buff + ss->len, *pmsg, avail); |
| 126 | *pmsg += avail; |
| 127 | *plen -= avail; |
| 128 | ss->len += avail; |
| 129 | |
| 130 | return (ss->len == ss->size); |
| 131 | } |
| 132 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 133 | /* returns the number of bytes needed to fill a sink's destination |
| 134 | * buffer. |
| 135 | */ |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 136 | static int |
| 137 | qemud_sink_needed( QemudSink* ss ) |
| 138 | { |
| 139 | return ss->size - ss->len; |
| 140 | } |
| 141 | |
| 142 | /** HANDLING SERIAL PORT CONNECTION |
| 143 | **/ |
| 144 | |
| 145 | /* The QemudSerial object receives data from the serial port charpipe. |
| 146 | * It parses the header to extract the channel id and payload length, |
| 147 | * then the message itself. |
| 148 | * |
| 149 | * Incoming messages are sent to a generic receiver identified by |
| 150 | * the 'recv_opaque' and 'recv_func' parameters to qemud_serial_init() |
| 151 | * |
| 152 | * It also provides qemud_serial_send() which can be used to send |
| 153 | * messages back through the serial port. |
| 154 | */ |
| 155 | |
| 156 | #define HEADER_SIZE 6 |
| 157 | |
| 158 | #define LENGTH_OFFSET 2 |
| 159 | #define LENGTH_SIZE 4 |
| 160 | |
| 161 | #define CHANNEL_OFFSET 0 |
| 162 | #define CHANNEL_SIZE 2 |
| 163 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 164 | #if SUPPORT_LEGACY_QEMUD |
| 165 | typedef enum { |
| 166 | QEMUD_VERSION_UNKNOWN, |
| 167 | QEMUD_VERSION_LEGACY, |
| 168 | QEMUD_VERSION_NORMAL |
| 169 | } QemudVersion; |
| 170 | |
| 171 | # define LEGACY_LENGTH_OFFSET 0 |
| 172 | # define LEGACY_CHANNEL_OFFSET 4 |
| 173 | #endif |
| 174 | |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 175 | /* length of the framed header */ |
| 176 | #define FRAME_HEADER_SIZE 4 |
| 177 | |
| 178 | #define BUFFER_SIZE MAX_SERIAL_PAYLOAD |
| 179 | |
| 180 | /* out of convenience, the incoming message is zero-terminated |
| 181 | * and can be modified by the receiver (e.g. for tokenization). |
| 182 | */ |
| 183 | typedef void (*QemudSerialReceive)( void* opaque, int channel, uint8_t* msg, int msglen); |
| 184 | |
| 185 | typedef struct QemudSerial { |
| 186 | CharDriverState* cs; /* serial charpipe endpoint */ |
| 187 | |
| 188 | /* managing incoming packets from the serial port */ |
| 189 | ABool need_header; |
| 190 | int overflow; |
| 191 | int in_size; |
| 192 | int in_channel; |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 193 | #if SUPPORT_LEGACY_QEMUD |
| 194 | QemudVersion version; |
| 195 | #endif |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 196 | QemudSink header[1]; |
| 197 | QemudSink payload[1]; |
| 198 | uint8_t data0[MAX_SERIAL_PAYLOAD+1]; |
| 199 | |
| 200 | /* receiver */ |
| 201 | QemudSerialReceive recv_func; /* receiver callback */ |
| 202 | void* recv_opaque; /* receiver user-specific data */ |
| 203 | } QemudSerial; |
| 204 | |
| 205 | |
| 206 | /* called by the charpipe to see how much bytes can be |
| 207 | * read from the serial port. |
| 208 | */ |
| 209 | static int |
| 210 | qemud_serial_can_read( void* opaque ) |
| 211 | { |
| 212 | QemudSerial* s = opaque; |
| 213 | |
| 214 | if (s->overflow > 0) { |
| 215 | return s->overflow; |
| 216 | } |
| 217 | |
| 218 | /* if in_size is 0, we're reading the header */ |
| 219 | if (s->need_header) |
| 220 | return qemud_sink_needed(s->header); |
| 221 | |
| 222 | /* otherwise, we're reading the payload */ |
| 223 | return qemud_sink_needed(s->payload); |
| 224 | } |
| 225 | |
| 226 | /* called by the charpipe to read data from the serial |
| 227 | * port. 'len' cannot be more than the value returned |
| 228 | * by 'qemud_serial_can_read'. |
| 229 | */ |
| 230 | static void |
| 231 | qemud_serial_read( void* opaque, const uint8_t* from, int len ) |
| 232 | { |
| 233 | QemudSerial* s = opaque; |
| 234 | |
| 235 | T("%s: received %3d bytes: '%s'", __FUNCTION__, len, quote_bytes((const void*)from, len)); |
| 236 | |
| 237 | while (len > 0) { |
| 238 | int avail; |
| 239 | |
| 240 | /* skip overflow bytes */ |
| 241 | if (s->overflow > 0) { |
| 242 | avail = s->overflow; |
| 243 | if (avail > len) |
| 244 | avail = len; |
| 245 | |
| 246 | from += avail; |
| 247 | len -= avail; |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | /* read header if needed */ |
| 252 | if (s->need_header) { |
| 253 | if (!qemud_sink_fill(s->header, (const uint8_t**)&from, &len)) |
| 254 | break; |
| 255 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 256 | #if SUPPORT_LEGACY_QEMUD |
| 257 | if (s->version == QEMUD_VERSION_UNKNOWN) { |
| 258 | /* if we receive "001200" as the first header, then we |
| 259 | * detected a legacy qemud daemon. See the comments |
| 260 | * in qemud_serial_send_legacy_probe() for details. |
| 261 | */ |
| 262 | if ( !memcmp(s->data0, "001200", 6) ) { |
| 263 | D("%s: legacy qemud detected.", __FUNCTION__); |
| 264 | s->version = QEMUD_VERSION_LEGACY; |
David Turner | fbcbf42 | 2009-04-15 06:50:16 -0700 | [diff] [blame] | 265 | /* tell the modem to use legacy emulation mode */ |
| 266 | amodem_set_legacy(android_modem); |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 267 | } else { |
| 268 | D("%s: normal qemud detected.", __FUNCTION__); |
| 269 | s->version = QEMUD_VERSION_NORMAL; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (s->version == QEMUD_VERSION_LEGACY) { |
| 274 | s->in_size = hex2int( s->data0 + LEGACY_LENGTH_OFFSET, LENGTH_SIZE ); |
| 275 | s->in_channel = hex2int( s->data0 + LEGACY_CHANNEL_OFFSET, CHANNEL_SIZE ); |
| 276 | } else { |
| 277 | s->in_size = hex2int( s->data0 + LENGTH_OFFSET, LENGTH_SIZE ); |
| 278 | s->in_channel = hex2int( s->data0 + CHANNEL_OFFSET, CHANNEL_SIZE ); |
| 279 | } |
| 280 | #else |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 281 | /* extract payload length + channel id */ |
| 282 | s->in_size = hex2int( s->data0 + LENGTH_OFFSET, LENGTH_SIZE ); |
| 283 | s->in_channel = hex2int( s->data0 + CHANNEL_OFFSET, CHANNEL_SIZE ); |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 284 | #endif |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 285 | s->header->len = 0; |
| 286 | |
| 287 | if (s->in_size <= 0 || s->in_channel < 0) { |
| 288 | D("%s: bad header: '%.*s'", __FUNCTION__, HEADER_SIZE, s->data0); |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | if (s->in_size > MAX_SERIAL_PAYLOAD) { |
| 293 | D("%s: ignoring huge serial packet: length=%d channel=%1", |
| 294 | __FUNCTION__, s->in_size, s->in_channel); |
| 295 | s->overflow = s->in_size; |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | /* prepare 'in_data' for payload */ |
| 300 | s->need_header = 0; |
| 301 | qemud_sink_reset(s->payload, s->in_size, s->data0); |
| 302 | } |
| 303 | |
| 304 | /* read payload bytes */ |
| 305 | if (!qemud_sink_fill(s->payload, &from, &len)) |
| 306 | break; |
| 307 | |
| 308 | /* zero-terminate payload, then send it to receiver */ |
| 309 | s->payload->buff[s->payload->size] = 0; |
| 310 | D("%s: channel=%2d len=%3d '%s'", __FUNCTION__, |
| 311 | s->in_channel, s->payload->size, |
| 312 | quote_bytes((const void*)s->payload->buff, s->payload->size)); |
| 313 | |
| 314 | s->recv_func( s->recv_opaque, s->in_channel, s->payload->buff, s->payload->size ); |
| 315 | |
| 316 | /* prepare for new header */ |
| 317 | s->need_header = 1; |
| 318 | } |
| 319 | } |
| 320 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 321 | |
| 322 | #if SUPPORT_LEGACY_QEMUD |
| 323 | static void |
| 324 | qemud_serial_send_legacy_probe( QemudSerial* s ) |
| 325 | { |
| 326 | /* we're going to send a specially crafted packet to the qemud |
| 327 | * daemon, this will help us determine whether we're talking |
| 328 | * to a legacy or a normal daemon. |
| 329 | * |
| 330 | * the trick is to known that a legacy daemon uses the following |
| 331 | * header: |
| 332 | * |
| 333 | * <length><channel><payload> |
| 334 | * |
| 335 | * while the normal one uses: |
| 336 | * |
| 337 | * <channel><length><payload> |
| 338 | * |
| 339 | * where <channel> is a 2-hexchar string, and <length> a 4-hexchar |
| 340 | * string. |
| 341 | * |
| 342 | * if we send a header of "000100", it is interpreted: |
| 343 | * |
| 344 | * - as the header of a 1-byte payload by the legacy daemon |
| 345 | * - as the header of a 256-byte payload by the normal one. |
| 346 | * |
| 347 | * we're going to send something that looks like: |
| 348 | * |
| 349 | * "000100" + "X" + |
| 350 | * "000b00" + "connect:gsm" + |
| 351 | * "000b00" + "connect:gps" + |
| 352 | * "000f00" + "connect:control" + |
| 353 | * "00c210" + "0"*194 |
| 354 | * |
| 355 | * the normal daemon will interpret this as a 256-byte payload |
| 356 | * for channel 0, with garbage content ("X000b00conn...") which |
| 357 | * will be silently ignored. |
| 358 | * |
| 359 | * on the other hand, the legacy daemon will see it as a |
| 360 | * series of packets: |
| 361 | * |
| 362 | * one message "X" on channel 0, which will force the daemon |
| 363 | * to send back "001200ko:unknown command" as its first answer. |
| 364 | * |
| 365 | * three "connect:<xxx>" messages used to receive the channel |
| 366 | * numbers of the three legacy services implemented by the daemon. |
| 367 | * |
| 368 | * a garbage packet of 194 zeroes for channel 16, which will be |
| 369 | * silently ignored. |
| 370 | */ |
| 371 | uint8_t tab[194]; |
| 372 | |
| 373 | memset(tab, 0, sizeof(tab)); |
| 374 | qemu_chr_write(s->cs, (uint8_t*)"000100X", 7); |
| 375 | qemu_chr_write(s->cs, (uint8_t*)"000b00connect:gsm", 17); |
| 376 | qemu_chr_write(s->cs, (uint8_t*)"000b00connect:gps", 17); |
| 377 | qemu_chr_write(s->cs, (uint8_t*)"000f00connect:control", 21); |
| 378 | qemu_chr_write(s->cs, (uint8_t*)"00c210", 6); |
| 379 | qemu_chr_write(s->cs, tab, sizeof(tab)); |
| 380 | } |
| 381 | #endif /* SUPPORT_LEGACY_QEMUD */ |
| 382 | |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 383 | /* intialize a QemudSerial object with a charpipe endpoint |
| 384 | * and a receiver. |
| 385 | */ |
| 386 | static void |
| 387 | qemud_serial_init( QemudSerial* s, |
| 388 | CharDriverState* cs, |
| 389 | QemudSerialReceive recv_func, |
| 390 | void* recv_opaque ) |
| 391 | { |
| 392 | s->cs = cs; |
| 393 | s->recv_func = recv_func; |
| 394 | s->recv_opaque = recv_opaque; |
| 395 | s->need_header = 1; |
| 396 | s->overflow = 0; |
| 397 | |
| 398 | qemud_sink_reset( s->header, HEADER_SIZE, s->data0 ); |
| 399 | s->in_size = 0; |
| 400 | s->in_channel = -1; |
| 401 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 402 | #if SUPPORT_LEGACY_QEMUD |
| 403 | s->version = QEMUD_VERSION_UNKNOWN; |
| 404 | qemud_serial_send_legacy_probe(s); |
| 405 | #endif |
| 406 | |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 407 | qemu_chr_add_handlers( cs, |
| 408 | qemud_serial_can_read, |
| 409 | qemud_serial_read, |
| 410 | NULL, |
| 411 | s ); |
| 412 | } |
| 413 | |
| 414 | /* send a message to the serial port. This will add the necessary |
| 415 | * header. |
| 416 | */ |
| 417 | static void |
| 418 | qemud_serial_send( QemudSerial* s, |
| 419 | int channel, |
| 420 | ABool framing, |
| 421 | const uint8_t* msg, |
| 422 | int msglen ) |
| 423 | { |
| 424 | uint8_t header[HEADER_SIZE]; |
| 425 | uint8_t frame[FRAME_HEADER_SIZE]; |
| 426 | int avail, len = msglen; |
| 427 | |
| 428 | if (msglen <= 0 || channel < 0) |
| 429 | return; |
| 430 | |
| 431 | D("%s: channel=%2d len=%3d '%s'", |
| 432 | __FUNCTION__, channel, msglen, |
| 433 | quote_bytes((const void*)msg, msglen)); |
| 434 | |
| 435 | if (framing) { |
| 436 | len += FRAME_HEADER_SIZE; |
| 437 | } |
| 438 | |
| 439 | /* packetize the payload for the serial MTU */ |
| 440 | while (len > 0) |
| 441 | { |
| 442 | avail = len; |
| 443 | if (avail > MAX_SERIAL_PAYLOAD) |
| 444 | avail = MAX_SERIAL_PAYLOAD; |
| 445 | |
| 446 | /* write this packet's header */ |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 447 | #if SUPPORT_LEGACY_QEMUD |
| 448 | if (s->version == QEMUD_VERSION_LEGACY) { |
| 449 | int2hex(header + LEGACY_LENGTH_OFFSET, LENGTH_SIZE, avail); |
| 450 | int2hex(header + LEGACY_CHANNEL_OFFSET, CHANNEL_SIZE, channel); |
| 451 | } else { |
| 452 | int2hex(header + LENGTH_OFFSET, LENGTH_SIZE, avail); |
| 453 | int2hex(header + CHANNEL_OFFSET, CHANNEL_SIZE, channel); |
| 454 | } |
| 455 | #else |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 456 | int2hex(header + LENGTH_OFFSET, LENGTH_SIZE, avail); |
| 457 | int2hex(header + CHANNEL_OFFSET, CHANNEL_SIZE, channel); |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 458 | #endif |
| 459 | T("%s: '%.*s'", __FUNCTION__, HEADER_SIZE, header); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 460 | qemu_chr_write(s->cs, header, HEADER_SIZE); |
| 461 | |
| 462 | /* insert frame header when needed */ |
| 463 | if (framing) { |
| 464 | int2hex(frame, FRAME_HEADER_SIZE, msglen); |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 465 | T("%s: '%.*s'", __FUNCTION__, FRAME_HEADER_SIZE, frame); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 466 | qemu_chr_write(s->cs, frame, FRAME_HEADER_SIZE); |
| 467 | avail -= FRAME_HEADER_SIZE; |
| 468 | len -= FRAME_HEADER_SIZE; |
| 469 | framing = 0; |
| 470 | } |
| 471 | |
| 472 | /* write message content */ |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 473 | T("%s: '%.*s'", __FUNCTION__, avail, msg); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 474 | qemu_chr_write(s->cs, msg, avail); |
| 475 | msg += avail; |
| 476 | len -= avail; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /** CLIENTS |
| 481 | **/ |
| 482 | |
| 483 | /* A QemudClient models a single client as seen by the emulator. |
| 484 | * Each client has its own channel id, and belongs to a given |
| 485 | * QemudService (see below). |
| 486 | * |
| 487 | * There is a global list of clients used to multiplex incoming |
| 488 | * messages from the channel id (see qemud_multiplexer_serial_recv()). |
| 489 | * |
| 490 | */ |
| 491 | |
| 492 | struct QemudClient { |
| 493 | int channel; |
| 494 | QemudSerial* serial; |
| 495 | void* clie_opaque; |
| 496 | QemudClientRecv clie_recv; |
| 497 | QemudClientClose clie_close; |
| 498 | QemudService* service; |
| 499 | QemudClient* next_serv; /* next in same service */ |
| 500 | QemudClient* next; |
| 501 | QemudClient** pref; |
| 502 | |
| 503 | /* framing support */ |
| 504 | int framing; |
| 505 | ABool need_header; |
| 506 | QemudSink header[1]; |
| 507 | uint8_t header0[FRAME_HEADER_SIZE]; |
| 508 | QemudSink payload[1]; |
| 509 | }; |
| 510 | |
| 511 | static void qemud_service_remove_client( QemudService* service, |
| 512 | QemudClient* client ); |
| 513 | |
| 514 | /* remove a QemudClient from global list */ |
| 515 | static void |
| 516 | qemud_client_remove( QemudClient* c ) |
| 517 | { |
| 518 | c->pref[0] = c->next; |
| 519 | if (c->next) |
| 520 | c->next->pref = c->pref; |
| 521 | |
| 522 | c->next = NULL; |
| 523 | c->pref = &c->next; |
| 524 | } |
| 525 | |
| 526 | /* add a QemudClient to global list */ |
| 527 | static void |
| 528 | qemud_client_prepend( QemudClient* c, QemudClient** plist ) |
| 529 | { |
| 530 | c->next = *plist; |
| 531 | c->pref = plist; |
| 532 | *plist = c; |
| 533 | if (c->next) |
| 534 | c->next->pref = &c->next; |
| 535 | } |
| 536 | |
| 537 | /* receive a new message from a client, and dispatch it to |
| 538 | * the real service implementation. |
| 539 | */ |
| 540 | static void |
| 541 | qemud_client_recv( void* opaque, uint8_t* msg, int msglen ) |
| 542 | { |
| 543 | QemudClient* c = opaque; |
| 544 | |
| 545 | /* no framing, things are simple */ |
| 546 | if (!c->framing) { |
| 547 | if (c->clie_recv) |
| 548 | c->clie_recv( c->clie_opaque, msg, msglen ); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | /* framing */ |
| 553 | |
| 554 | #if 1 |
| 555 | /* special case, in 99% of cases, everything is in |
| 556 | * the incoming message, and we can do all we need |
| 557 | * directly without dynamic allocation. |
| 558 | */ |
| 559 | if (msglen > FRAME_HEADER_SIZE && |
| 560 | c->need_header == 1 && |
| 561 | qemud_sink_needed(c->header) == 0) |
| 562 | { |
| 563 | int len = hex2int( msg, FRAME_HEADER_SIZE ); |
| 564 | |
| 565 | if (len >= 0 && msglen == len + FRAME_HEADER_SIZE) { |
| 566 | if (c->clie_recv) |
| 567 | c->clie_recv( c->clie_opaque, |
| 568 | msg+FRAME_HEADER_SIZE, |
| 569 | msglen-FRAME_HEADER_SIZE); |
| 570 | return; |
| 571 | } |
| 572 | } |
| 573 | #endif |
| 574 | |
| 575 | while (msglen > 0) { |
| 576 | /* read the header */ |
| 577 | if (c->need_header) { |
| 578 | int frame_size; |
| 579 | uint8_t* data; |
| 580 | |
| 581 | if (!qemud_sink_fill(c->header, (const uint8_t**)&msg, &msglen)) |
| 582 | break; |
| 583 | |
| 584 | frame_size = hex2int(c->header0, 4); |
| 585 | if (frame_size == 0) { |
| 586 | D("%s: ignoring empty frame", __FUNCTION__); |
| 587 | continue; |
| 588 | } |
| 589 | if (frame_size < 0) { |
| 590 | D("%s: ignoring corrupted frame header '.*s'", |
| 591 | __FUNCTION__, FRAME_HEADER_SIZE, c->header0 ); |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | AARRAY_NEW(data, frame_size+1); /* +1 for terminating zero */ |
| 596 | qemud_sink_reset(c->payload, frame_size, data); |
| 597 | c->need_header = 0; |
| 598 | c->header->len = 0; |
| 599 | } |
| 600 | |
| 601 | /* read the payload */ |
| 602 | if (!qemud_sink_fill(c->payload, (const uint8_t**)&msg, &msglen)) |
| 603 | break; |
| 604 | |
| 605 | c->payload->buff[c->payload->size] = 0; |
| 606 | |
| 607 | |
| 608 | if (c->clie_recv) |
| 609 | c->clie_recv( c->clie_opaque, c->payload->buff, c->payload->size ); |
| 610 | |
| 611 | AFREE(c->payload->buff); |
| 612 | c->need_header = 1; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* disconnect a client. this automatically frees the QemudClient. |
| 617 | * note that this also removes the client from the global list |
| 618 | * and from its service's list, if any. |
| 619 | */ |
| 620 | static void |
| 621 | qemud_client_disconnect( void* opaque ) |
| 622 | { |
| 623 | QemudClient* c = opaque; |
| 624 | |
| 625 | /* remove from current list */ |
| 626 | qemud_client_remove(c); |
| 627 | |
| 628 | /* send a disconnect command to the daemon */ |
| 629 | if (c->channel > 0) { |
| 630 | char tmp[128], *p=tmp, *end=p+sizeof(tmp); |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 631 | p = bufprint(tmp, end, "disconnect:%02x", c->channel); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 632 | qemud_serial_send(c->serial, 0, 0, (uint8_t*)tmp, p-tmp); |
| 633 | } |
| 634 | |
| 635 | /* call the client close callback */ |
| 636 | if (c->clie_close) { |
| 637 | c->clie_close(c->clie_opaque); |
| 638 | c->clie_close = NULL; |
| 639 | } |
| 640 | c->clie_recv = NULL; |
| 641 | |
| 642 | /* remove from service list, if any */ |
| 643 | if (c->service) { |
| 644 | qemud_service_remove_client(c->service, c); |
| 645 | c->service = NULL; |
| 646 | } |
| 647 | |
| 648 | AFREE(c); |
| 649 | } |
| 650 | |
| 651 | /* allocate a new QemudClient object */ |
| 652 | static QemudClient* |
| 653 | qemud_client_alloc( int channel_id, |
| 654 | void* clie_opaque, |
| 655 | QemudClientRecv clie_recv, |
| 656 | QemudClientClose clie_close, |
| 657 | QemudSerial* serial, |
| 658 | QemudClient** pclients ) |
| 659 | { |
| 660 | QemudClient* c; |
| 661 | |
| 662 | ANEW0(c); |
| 663 | |
| 664 | c->serial = serial; |
| 665 | c->channel = channel_id; |
| 666 | c->clie_opaque = clie_opaque; |
| 667 | c->clie_recv = clie_recv; |
| 668 | c->clie_close = clie_close; |
| 669 | |
| 670 | c->framing = 0; |
| 671 | c->need_header = 1; |
| 672 | qemud_sink_reset(c->header, FRAME_HEADER_SIZE, c->header0); |
| 673 | |
| 674 | qemud_client_prepend(c, pclients); |
| 675 | |
| 676 | return c; |
| 677 | } |
| 678 | |
| 679 | /** SERVICES |
| 680 | **/ |
| 681 | |
| 682 | /* A QemudService models a _named_ service facility implemented |
| 683 | * by the emulator, that clients in the emulated system can connect |
| 684 | * to. |
| 685 | * |
| 686 | * Each service can have a limit on the number of clients they |
| 687 | * accept (this number if unlimited if 'max_clients' is 0). |
| 688 | * |
| 689 | * Each service maintains a list of active QemudClients and |
| 690 | * can also be used to create new QemudClient objects through |
| 691 | * its 'serv_opaque' and 'serv_connect' fields. |
| 692 | */ |
| 693 | struct QemudService { |
| 694 | const char* name; |
| 695 | int max_clients; |
| 696 | int num_clients; |
| 697 | QemudClient* clients; |
| 698 | QemudServiceConnect serv_connect; |
| 699 | void* serv_opaque; |
| 700 | QemudService* next; |
| 701 | }; |
| 702 | |
| 703 | /* Create a new QemudService object */ |
| 704 | static QemudService* |
| 705 | qemud_service_new( const char* name, |
| 706 | int max_clients, |
| 707 | void* serv_opaque, |
| 708 | QemudServiceConnect serv_connect, |
| 709 | QemudService** pservices ) |
| 710 | { |
| 711 | QemudService* s; |
| 712 | |
| 713 | ANEW0(s); |
| 714 | s->name = ASTRDUP(name); |
| 715 | s->max_clients = max_clients; |
| 716 | s->num_clients = 0; |
| 717 | s->clients = NULL; |
| 718 | |
| 719 | s->serv_opaque = serv_opaque; |
| 720 | s->serv_connect = serv_connect; |
| 721 | |
| 722 | s->next = *pservices; |
| 723 | *pservices = s; |
| 724 | |
| 725 | return s; |
| 726 | } |
| 727 | |
| 728 | /* used internally to populate a QemudService object with a |
| 729 | * new QemudClient */ |
| 730 | static void |
| 731 | qemud_service_add_client( QemudService* s, QemudClient* c ) |
| 732 | { |
| 733 | c->service = s; |
| 734 | c->next_serv = s->clients; |
| 735 | s->clients = c; |
| 736 | s->num_clients += 1; |
| 737 | } |
| 738 | |
| 739 | /* used internally to remove a QemudClient from a QemudService */ |
| 740 | static void |
| 741 | qemud_service_remove_client( QemudService* s, QemudClient* c ) |
| 742 | { |
| 743 | QemudClient** pnode = &s->clients; |
| 744 | QemudClient* node; |
| 745 | |
| 746 | /* remove from clients linked-list */ |
| 747 | for (;;) { |
| 748 | node = *pnode; |
| 749 | if (node == NULL) { |
| 750 | D("%s: could not find client %d for service '%s'", |
| 751 | __FUNCTION__, c->channel, s->name); |
| 752 | return; |
| 753 | } |
| 754 | if (node == c) |
| 755 | break; |
| 756 | pnode = &node->next_serv; |
| 757 | } |
| 758 | |
| 759 | *pnode = node->next_serv; |
| 760 | s->num_clients -= 1; |
| 761 | } |
| 762 | |
| 763 | /** MULTIPLEXER |
| 764 | **/ |
| 765 | |
| 766 | /* A QemudMultiplexer object maintains the global state of the |
| 767 | * qemud service facility. It holds a QemudSerial object to |
| 768 | * maintain the state of the serial port connection. |
| 769 | * |
| 770 | * The QemudMultiplexer receives all incoming messages from |
| 771 | * the serial port, and dispatches them to the appropriate |
| 772 | * QemudClient. |
| 773 | * |
| 774 | * It also has a global list of clients, and a global list of |
| 775 | * services. |
| 776 | * |
| 777 | * Finally, the QemudMultiplexer has a special QemudClient used |
| 778 | * to handle channel 0, i.e. the control channel used to handle |
| 779 | * connections and disconnections of clients. |
| 780 | */ |
| 781 | typedef struct QemudMultiplexer QemudMultiplexer; |
| 782 | |
| 783 | struct QemudMultiplexer { |
| 784 | QemudSerial serial[1]; |
| 785 | QemudClient* clients; |
| 786 | QemudService* services; |
| 787 | }; |
| 788 | |
| 789 | /* this is the serial_recv callback that is called |
| 790 | * whenever an incoming message arrives through the serial port |
| 791 | */ |
| 792 | static void |
| 793 | qemud_multiplexer_serial_recv( void* opaque, |
| 794 | int channel, |
| 795 | uint8_t* msg, |
| 796 | int msglen ) |
| 797 | { |
| 798 | QemudMultiplexer* m = opaque; |
| 799 | QemudClient* c = m->clients; |
| 800 | |
| 801 | /* dispatch to an existing client if possible |
| 802 | * note that channel 0 is handled by a special |
| 803 | * QemudClient that is setup in qemud_multiplexer_init() |
| 804 | */ |
| 805 | for ( ; c != NULL; c = c->next ) { |
| 806 | if (c->channel == channel) { |
| 807 | qemud_client_recv(c, msg, msglen); |
| 808 | return; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | D("%s: ignoring %d bytes for unknown channel %d", |
| 813 | __FUNCTION__, msglen, channel); |
| 814 | } |
| 815 | |
| 816 | /* handle a new connection attempt. This returns 0 on |
| 817 | * success, -1 if the service name is unknown, or -2 |
| 818 | * if the service's maximum number of clients has been |
| 819 | * reached. |
| 820 | */ |
| 821 | static int |
| 822 | qemud_multiplexer_connect( QemudMultiplexer* m, |
| 823 | const char* service_name, |
| 824 | int channel_id ) |
| 825 | { |
| 826 | QemudService* sv; |
| 827 | QemudClient* client; |
| 828 | |
| 829 | /* find the corresponding registered service by name */ |
| 830 | for (sv = m->services; sv != NULL; sv = sv->next) { |
| 831 | if (!strcmp(sv->name, service_name)) { |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | if (sv == NULL) { |
| 837 | D("%s: no registered '%s' service", __FUNCTION__, service_name); |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | /* check service's client count */ |
| 842 | if (sv->max_clients > 0 && sv->num_clients >= sv->max_clients) { |
| 843 | D("%s: registration failed for '%s' service: too many clients (%d)", |
| 844 | __FUNCTION__, service_name, sv->num_clients); |
| 845 | return -2; |
| 846 | } |
| 847 | |
| 848 | /* ask the service to create a new QemudClient. Note that we |
| 849 | * assume that this calls qemud_client_new() which will add |
| 850 | * the client to the service's list automatically. |
| 851 | */ |
| 852 | client = sv->serv_connect( sv->serv_opaque, sv, channel_id ); |
| 853 | if (client == NULL) { |
| 854 | D("%s: registration failed for '%s' service", |
| 855 | __FUNCTION__, service_name); |
| 856 | return -1; |
| 857 | } |
| 858 | |
| 859 | D("%s: registered client channel %d for '%s' service", |
| 860 | __FUNCTION__, channel_id, service_name); |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /* disconnect a given client from its channel id */ |
| 865 | static void |
| 866 | qemud_multiplexer_disconnect( QemudMultiplexer* m, |
| 867 | int channel ) |
| 868 | { |
| 869 | QemudClient* c; |
| 870 | |
| 871 | /* find the client by its channel id, then disconnect it */ |
| 872 | for (c = m->clients; c; c = c->next) { |
| 873 | if (c->channel == channel) { |
| 874 | D("%s: disconnecting client %d", |
| 875 | __FUNCTION__, channel); |
| 876 | /* note thatt this removes the client from |
| 877 | * m->clients automatically. |
| 878 | */ |
| 879 | c->channel = -1; /* no need to send disconnect:<id> */ |
| 880 | qemud_client_disconnect(c); |
| 881 | return; |
| 882 | } |
| 883 | } |
| 884 | D("%s: disconnecting unknown channel %d", |
| 885 | __FUNCTION__, channel); |
| 886 | } |
| 887 | |
| 888 | /* handle control messages. This is used as the receive |
| 889 | * callback for the special QemudClient setup to manage |
| 890 | * channel 0. |
| 891 | * |
| 892 | * note that the message is zero-terminated for convenience |
| 893 | * (i.e. msg[msglen] is a valid memory read that returns '\0') |
| 894 | */ |
| 895 | static void |
| 896 | qemud_multiplexer_control_recv( void* opaque, |
| 897 | uint8_t* msg, |
| 898 | int msglen ) |
| 899 | { |
| 900 | QemudMultiplexer* mult = opaque; |
| 901 | uint8_t* msgend = msg + msglen; |
| 902 | char tmp[64], *p=tmp, *end=p+sizeof(tmp); |
| 903 | |
| 904 | /* handle connection attempts. |
| 905 | * the client message must be "connect:<service-name>:<id>" |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 906 | * where <id> is a 2-char hexadecimal string, which must be > 0 |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 907 | */ |
| 908 | if (msglen > 8 && !memcmp(msg, "connect:", 8)) |
| 909 | { |
| 910 | const char* service_name = (const char*)msg + 8; |
| 911 | int channel, ret; |
| 912 | char* q; |
| 913 | |
| 914 | q = strchr(service_name, ':'); |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 915 | if (q == NULL || q+3 != (char*)msgend) { |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 916 | D("%s: malformed connect message: '%.*s' (offset=%d)", |
| 917 | __FUNCTION__, msglen, (const char*)msg, q ? q-(char*)msg : -1); |
| 918 | return; |
| 919 | } |
| 920 | *q++ = 0; /* zero-terminate service name */ |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 921 | channel = hex2int((uint8_t*)q, 2); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 922 | if (channel <= 0) { |
| 923 | D("%s: malformed channel id '%.*s", |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 924 | __FUNCTION__, 2, q); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 925 | return; |
| 926 | } |
| 927 | |
| 928 | ret = qemud_multiplexer_connect(mult, service_name, channel); |
| 929 | /* the answer can be one of: |
| 930 | * ok:connect:<id> |
| 931 | * ko:connect:<id>:<reason-for-failure> |
| 932 | */ |
| 933 | if (ret < 0) { |
| 934 | if (ret == -1) { |
| 935 | /* could not connect */ |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 936 | p = bufprint(tmp, end, "ko:connect:%02x:unknown service", channel); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 937 | } else { |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 938 | p = bufprint(tmp, end, "ko:connect:%02x:service busy", channel); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | else { |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 942 | p = bufprint(tmp, end, "ok:connect:%02x", channel); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 943 | } |
| 944 | qemud_serial_send(mult->serial, 0, 0, (uint8_t*)tmp, p-tmp); |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | /* handle client disconnections, |
| 949 | * this message arrives when the client has closed the connection. |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 950 | * format: "disconnect:<id>" where <id> is a 2-hex channel id > 0 |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 951 | */ |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 952 | if (msglen == 13 && !memcmp(msg, "disconnect:", 11)) { |
| 953 | int channel_id = hex2int(msg+11, 2); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 954 | if (channel_id <= 0) { |
| 955 | D("%s: malformed disconnect channel id: '%.*s'", |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 956 | __FUNCTION__, 2, msg+11); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 957 | return; |
| 958 | } |
| 959 | qemud_multiplexer_disconnect(mult, channel_id); |
| 960 | return; |
| 961 | } |
| 962 | |
David Turner | 791d861 | 2009-04-13 18:01:32 -0700 | [diff] [blame] | 963 | #if SUPPORT_LEGACY_QEMUD |
| 964 | /* an ok:connect:<service>:<id> message can be received if we're |
| 965 | * talking to a legacy qemud daemon, i.e. one running in a 1.0 or |
| 966 | * 1.1 system image. |
| 967 | * |
| 968 | * we should treat is as a normal "connect:" attempt, except that |
| 969 | * we must not send back any acknowledgment. |
| 970 | */ |
| 971 | if (msglen > 11 && !memcmp(msg, "ok:connect:", 11)) { |
| 972 | const char* service_name = (const char*)msg + 11; |
| 973 | char* q = strchr(service_name, ':'); |
| 974 | int channel; |
| 975 | |
| 976 | if (q == NULL || q+3 != (char*)msgend) { |
| 977 | D("%s: malformed legacy connect message: '%.*s' (offset=%d)", |
| 978 | __FUNCTION__, msglen, (const char*)msg, q ? q-(char*)msg : -1); |
| 979 | return; |
| 980 | } |
| 981 | *q++ = 0; /* zero-terminate service name */ |
| 982 | channel = hex2int((uint8_t*)q, 2); |
| 983 | if (channel <= 0) { |
| 984 | D("%s: malformed legacy channel id '%.*s", |
| 985 | __FUNCTION__, 2, q); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | switch (mult->serial->version) { |
| 990 | case QEMUD_VERSION_UNKNOWN: |
| 991 | mult->serial->version = QEMUD_VERSION_LEGACY; |
| 992 | D("%s: legacy qemud daemon detected.", __FUNCTION__); |
| 993 | break; |
| 994 | |
| 995 | case QEMUD_VERSION_LEGACY: |
| 996 | /* nothing unusual */ |
| 997 | break; |
| 998 | |
| 999 | default: |
| 1000 | D("%s: weird, ignoring legacy qemud control message: '%.*s'", |
| 1001 | __FUNCTION__, msglen, msg); |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | /* "hw-control" was called "control" in 1.0/1.1 */ |
| 1006 | if (!strcmp(service_name,"control")) |
| 1007 | service_name = "hw-control"; |
| 1008 | |
| 1009 | qemud_multiplexer_connect(mult, service_name, channel); |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | /* anything else, don't answer for legacy */ |
| 1014 | if (mult->serial->version == QEMUD_VERSION_LEGACY) |
| 1015 | return; |
| 1016 | #endif /* SUPPORT_LEGACY_QEMUD */ |
| 1017 | |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 1018 | /* anything else is a problem */ |
| 1019 | p = bufprint(tmp, end, "ko:unknown command"); |
| 1020 | qemud_serial_send(mult->serial, 0, 0, (uint8_t*)tmp, p-tmp); |
| 1021 | } |
| 1022 | |
| 1023 | /* initialize the global QemudMultiplexer. |
| 1024 | */ |
| 1025 | static void |
| 1026 | qemud_multiplexer_init( QemudMultiplexer* mult, |
| 1027 | CharDriverState* serial_cs ) |
| 1028 | { |
| 1029 | QemudClient* control; |
| 1030 | |
| 1031 | /* initialize serial handler */ |
| 1032 | qemud_serial_init( mult->serial, |
| 1033 | serial_cs, |
| 1034 | qemud_multiplexer_serial_recv, |
| 1035 | mult ); |
| 1036 | |
| 1037 | /* setup listener for channel 0 */ |
| 1038 | control = qemud_client_alloc( 0, |
| 1039 | mult, |
| 1040 | qemud_multiplexer_control_recv, |
| 1041 | NULL, |
| 1042 | mult->serial, |
| 1043 | &mult->clients ); |
| 1044 | } |
| 1045 | |
| 1046 | /* the global multiplexer state */ |
| 1047 | static QemudMultiplexer _multiplexer[1]; |
| 1048 | |
| 1049 | /** HIGH-LEVEL API |
| 1050 | **/ |
| 1051 | |
| 1052 | /* this function must be used in the serv_connect callback |
| 1053 | * of a given QemudService object (see qemud_service_register() |
| 1054 | * below). It is used to register a new QemudClient to acknowledge |
| 1055 | * a new client connection. |
| 1056 | * |
| 1057 | * 'clie_opaque', 'clie_recv' and 'clie_close' are used to |
| 1058 | * send incoming client messages to the corresponding service |
| 1059 | * implementation, or notify the service that a client has |
| 1060 | * disconnected. |
| 1061 | */ |
| 1062 | QemudClient* |
| 1063 | qemud_client_new( QemudService* service, |
| 1064 | int channelId, |
| 1065 | void* clie_opaque, |
| 1066 | QemudClientRecv clie_recv, |
| 1067 | QemudClientClose clie_close ) |
| 1068 | { |
| 1069 | QemudMultiplexer* m = _multiplexer; |
| 1070 | QemudClient* c = qemud_client_alloc( channelId, |
| 1071 | clie_opaque, |
| 1072 | clie_recv, |
| 1073 | clie_close, |
| 1074 | m->serial, |
| 1075 | &m->clients ); |
| 1076 | |
| 1077 | qemud_service_add_client(service, c); |
| 1078 | return c; |
| 1079 | } |
| 1080 | |
| 1081 | /* this can be used by a service implementation to send an answer |
| 1082 | * or message to a specific client. |
| 1083 | */ |
| 1084 | void |
| 1085 | qemud_client_send ( QemudClient* client, const uint8_t* msg, int msglen ) |
| 1086 | { |
| 1087 | qemud_serial_send(client->serial, client->channel, client->framing != 0, msg, msglen); |
| 1088 | } |
| 1089 | |
| 1090 | /* enable framing for this client. When TRUE, this will |
| 1091 | * use internally a simple 4-hexchar header before each |
| 1092 | * message exchanged through the serial port. |
| 1093 | */ |
| 1094 | void |
| 1095 | qemud_client_set_framing( QemudClient* client, int framing ) |
| 1096 | { |
| 1097 | /* release dynamic buffer if we're disabling framing */ |
| 1098 | if (client->framing) { |
| 1099 | if (!client->need_header) { |
| 1100 | AFREE(client->payload->buff); |
| 1101 | client->need_header = 1; |
| 1102 | } |
| 1103 | } |
| 1104 | client->framing = !!framing; |
| 1105 | } |
| 1106 | |
| 1107 | /* this can be used by a service implementation to close a |
| 1108 | * specific client connection. |
| 1109 | */ |
| 1110 | void |
| 1111 | qemud_client_close( QemudClient* client ) |
| 1112 | { |
| 1113 | qemud_client_disconnect(client); |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | /* this is the end of the serial charpipe that must be passed |
| 1119 | * to the emulated tty implementation. The other end of the |
| 1120 | * charpipe must be passed to qemud_multiplexer_init(). |
| 1121 | */ |
| 1122 | static CharDriverState* android_qemud_cs; |
| 1123 | |
| 1124 | extern void |
| 1125 | android_qemud_init( void ) |
| 1126 | { |
| 1127 | CharDriverState* cs; |
| 1128 | |
| 1129 | if (android_qemud_cs != NULL) |
| 1130 | return; |
| 1131 | |
| 1132 | if (qemu_chr_open_charpipe( &android_qemud_cs, &cs ) < 0) { |
| 1133 | derror( "%s: can't create charpipe to serial port", |
| 1134 | __FUNCTION__ ); |
| 1135 | exit(1); |
| 1136 | } |
| 1137 | |
| 1138 | qemud_multiplexer_init(_multiplexer, cs); |
| 1139 | } |
| 1140 | |
| 1141 | /* return the serial charpipe endpoint that must be used |
| 1142 | * by the emulated tty implementation. |
| 1143 | */ |
| 1144 | CharDriverState* android_qemud_get_cs( void ) |
| 1145 | { |
| 1146 | if (android_qemud_cs == NULL) |
| 1147 | android_qemud_init(); |
| 1148 | |
| 1149 | return android_qemud_cs; |
| 1150 | } |
| 1151 | |
| 1152 | /* this function is used to register a new named qemud-based |
| 1153 | * service. You must provide 'serv_opaque' and 'serv_connect' |
| 1154 | * which will be called whenever a new client tries to connect |
| 1155 | * to the services. |
| 1156 | * |
| 1157 | * 'serv_connect' shall return NULL if the connection is refused, |
| 1158 | * or a handle to a new QemudClient otherwise. The latter can be |
| 1159 | * created through qemud_client_new() defined above. |
| 1160 | * |
| 1161 | * 'max_clients' is the maximum number of clients accepted by |
| 1162 | * the service concurrently. If this value is 0, then any number |
| 1163 | * of clients can connect. |
| 1164 | */ |
| 1165 | QemudService* |
| 1166 | qemud_service_register( const char* service_name, |
| 1167 | int max_clients, |
| 1168 | void* serv_opaque, |
| 1169 | QemudServiceConnect serv_connect ) |
| 1170 | { |
| 1171 | QemudMultiplexer* m = _multiplexer; |
| 1172 | QemudService* sv; |
| 1173 | |
| 1174 | if (android_qemud_cs == NULL) |
| 1175 | android_qemud_init(); |
| 1176 | |
| 1177 | sv = qemud_service_new(service_name, |
| 1178 | max_clients, |
| 1179 | serv_opaque, |
| 1180 | serv_connect, |
| 1181 | &m->services); |
| 1182 | |
| 1183 | return sv; |
| 1184 | } |
| 1185 | |
| 1186 | /* broadcast a given message to all clients of a given QemudService |
| 1187 | */ |
| 1188 | extern void |
| 1189 | qemud_service_broadcast( QemudService* sv, |
| 1190 | const uint8_t* msg, |
| 1191 | int msglen ) |
| 1192 | { |
| 1193 | QemudClient* c; |
| 1194 | |
| 1195 | for (c = sv->clients; c; c = c->next_serv) |
| 1196 | qemud_client_send(c, msg, msglen); |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | /* |
| 1202 | * The following code is used for backwards compatibility reasons. |
| 1203 | * It allows you to implement a given qemud-based service through |
| 1204 | * a charpipe. |
| 1205 | * |
| 1206 | * In other words, this implements a QemudService and corresponding |
| 1207 | * QemudClient that connects a qemud client running in the emulated |
| 1208 | * system, to a CharDriverState object implemented through a charpipe. |
| 1209 | * |
| 1210 | * QemudCharClient <===charpipe====> (char driver user) |
| 1211 | * |
| 1212 | * For example, this is used to implement the "gsm" service when the |
| 1213 | * modem emulation is provided through an external serial device. |
| 1214 | * |
| 1215 | * A QemudCharService can have only one client by definition. |
| 1216 | * There is no QemudCharClient object because we can store a single |
| 1217 | * CharDriverState handle in the 'opaque' field for simplicity. |
| 1218 | */ |
| 1219 | |
| 1220 | typedef struct { |
| 1221 | QemudService* service; |
| 1222 | CharDriverState* cs; |
| 1223 | } QemudCharService; |
| 1224 | |
| 1225 | /* called whenever a new message arrives from a qemud client. |
| 1226 | * this simply sends the message through the charpipe to the user. |
| 1227 | */ |
| 1228 | static void |
| 1229 | _qemud_char_client_recv( void* opaque, uint8_t* msg, int msglen ) |
| 1230 | { |
| 1231 | CharDriverState* cs = opaque; |
| 1232 | qemu_chr_write(cs, msg, msglen); |
| 1233 | } |
| 1234 | |
| 1235 | /* we don't expect clients of char. services to exit. Just |
| 1236 | * print an error to signal an unexpected situation. We should |
| 1237 | * be able to recover from these though, so don't panic. |
| 1238 | */ |
| 1239 | static void |
| 1240 | _qemud_char_client_close( void* opaque ) |
| 1241 | { |
| 1242 | derror("unexpected qemud char. channel close"); |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | /* called by the charpipe to know how much data can be read from |
| 1247 | * the user. Since we send everything directly to the serial port |
| 1248 | * we can return an arbitrary number. |
| 1249 | */ |
| 1250 | static int |
| 1251 | _qemud_char_service_can_read( void* opaque ) |
| 1252 | { |
| 1253 | return 8192; /* whatever */ |
| 1254 | } |
| 1255 | |
| 1256 | /* called to read data from the charpipe and send it to the client. |
| 1257 | * used qemud_service_broadcast() even if there is a single client |
| 1258 | * because we don't need a QemudCharClient object this way. |
| 1259 | */ |
| 1260 | static void |
| 1261 | _qemud_char_service_read( void* opaque, const uint8_t* from, int len ) |
| 1262 | { |
| 1263 | QemudService* sv = opaque; |
| 1264 | qemud_service_broadcast( sv, from, len ); |
| 1265 | } |
| 1266 | |
| 1267 | /* called when a qemud client tries to connect to a char. service. |
| 1268 | * we simply create a new client and open the charpipe to receive |
| 1269 | * data from it. |
| 1270 | */ |
| 1271 | static QemudClient* |
| 1272 | _qemud_char_service_connect( void* opaque, QemudService* sv, int channel ) |
| 1273 | { |
| 1274 | CharDriverState* cs = opaque; |
| 1275 | QemudClient* c = qemud_client_new( sv, channel, |
| 1276 | cs, |
| 1277 | _qemud_char_client_recv, |
| 1278 | _qemud_char_client_close); |
| 1279 | |
| 1280 | /* now we can open the gates :-) */ |
| 1281 | qemu_chr_add_handlers( cs, |
| 1282 | _qemud_char_service_can_read, |
| 1283 | _qemud_char_service_read, |
| 1284 | NULL, |
| 1285 | sv ); |
| 1286 | |
| 1287 | return c; |
| 1288 | } |
| 1289 | |
| 1290 | /* returns a charpipe endpoint that can be used by an emulated |
| 1291 | * device or external serial port to implement a char. service |
| 1292 | */ |
| 1293 | int |
| 1294 | android_qemud_get_channel( const char* name, CharDriverState* *pcs ) |
| 1295 | { |
| 1296 | CharDriverState* cs; |
| 1297 | |
| 1298 | if (qemu_chr_open_charpipe(&cs, pcs) < 0) { |
| 1299 | derror("can't open charpipe for '%s' qemud service", name); |
| 1300 | exit(2); |
| 1301 | } |
| 1302 | qemud_service_register(name, 1, cs, _qemud_char_service_connect); |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | /* set the character driver state for a given qemud communication channel. this |
| 1307 | * is used to attach the channel to an external char driver device directly. |
| 1308 | * returns 0 on success, -1 on error |
| 1309 | */ |
| 1310 | int |
| 1311 | android_qemud_set_channel( const char* name, CharDriverState* peer_cs ) |
| 1312 | { |
David Turner | fff1ae5 | 2009-04-05 14:22:28 -0700 | [diff] [blame] | 1313 | CharDriverState* char_buffer = qemu_chr_open_buffer(peer_cs); |
| 1314 | |
| 1315 | if (char_buffer == NULL) |
| 1316 | return -1; |
| 1317 | |
| 1318 | qemud_service_register(name, 1, char_buffer, _qemud_char_service_connect); |
The Android Open Source Project | 9877e2e | 2009-03-18 17:39:44 -0700 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |