aboutsummaryrefslogtreecommitdiffstats
path: root/android/qemud.c
blob: b127fc938834943021983b702b8930334b26058f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include "android/qemud.h"
#include "android/utils/debug.h"
#include "android/utils/misc.h"
#include "qemu-char.h"
#include "charpipe.h"
#include "cbuffer.h"

#define  D(...)    VERBOSE_PRINT(qemud,__VA_ARGS__)
#define  D_ACTIVE  VERBOSE_CHECK(qemud)

/* the T(...) macro is used to dump traffic */
#define  T_ACTIVE   0

#if T_ACTIVE
#define  T(...)    VERBOSE_PRINT(qemud,__VA_ARGS__)
#else
#define  T(...)    ((void)0)
#endif

#define  MAX_PAYLOAD   4000
#define  MAX_CHANNELS  8

#define  CHANNEL_CONTROL_INDEX 0

/** packets
 **/
#define  HEADER_SIZE  6

typedef struct Packet {
    struct Packet*  next;
    int             len;
    uint8_t         header[HEADER_SIZE];
    uint8_t         data[MAX_PAYLOAD];
} Packet;

static Packet*  _free_packets;

static void
packet_free( Packet*  p )
{
    p->next       = _free_packets;
    _free_packets = p;
}

static Packet*
packet_alloc( void )
{
    Packet*  p = _free_packets;
    if (p != NULL) {
        _free_packets = p->next;
    } else {
        p = malloc(sizeof(*p));
        if (p == NULL) {
            derror("%s: not enough memory", __FUNCTION__);
            exit(1);
        }
    }
    p->next = NULL;
    p->len  = 0;
    return p;
}

/** channels
 **/
typedef void (*EnqueueFunc)( void*  user, Packet*  p );

typedef struct {
    const char*      name;
    int              index;
    CharDriverState* cs;
    EnqueueFunc      enq_func;
    void*            enq_user;
} Channel;


static int
channel_can_read( void*  opaque )
{
    Channel*  c = opaque;

    return c->index < 0 ? 0 : MAX_PAYLOAD;
}


/* here, the data comes from the emulated device (e.g. GSM modem) through
 * a charpipe, we simply need to send it through the multiplexer */
static void
channel_read( void* opaque, const uint8_t*  from, int  len )
{
    Channel*  c = opaque;

    if (c->enq_func != NULL) {
        Packet*   p = packet_alloc();

        if (len > MAX_PAYLOAD)
            len = MAX_PAYLOAD;

        memcpy( p->data, from, len );
        p->len = len + HEADER_SIZE;
        int2hex( p->header+0, 4, len );
        int2hex( p->header+4, 2, c->index );

        c->enq_func( c->enq_user, p );
    }
    else
    {
        D("%s: discarding %d bytes for channel '%s'",
          __FUNCTION__, len, c->name);
    }
}

static void
channel_init( Channel*  c, const char*  name, CharDriverState* peer_cs )
{
    c->name     = name;
    c->index    = -1;
    c->enq_func = NULL;
    c->enq_user = NULL;
    c->cs       = peer_cs;
}


static void
channel_set_peer( Channel*  c, int  index, EnqueueFunc  enq_func, void*  enq_user )
{
    c->index = index;
    qemu_chr_add_handlers( c->cs,
                           channel_can_read,
                           channel_read,
                           NULL,
                           c );
    c->enq_func = enq_func;
    c->enq_user = enq_user;
}


static int
channel_write( Channel*c , const uint8_t*  buf, int  len )
{
    return qemu_chr_write( c->cs, buf, len );
}

/** multiplexer
 **/
#define  IN_BUFF_SIZE  (2*MAX_PAYLOAD)

typedef struct {
    CharDriverState*  cs;

    CBuffer  in_cbuffer[1];
    int      in_datalen;
    int      in_channel;

    int      count;
    Channel  channels[MAX_CHANNELS];
    uint8_t  in_buff[ IN_BUFF_SIZE + HEADER_SIZE ];
} Multiplexer;


/* called by channel_read when data comes from an emulated
 * device, and needs to be multiplexed through the serial
 * port
 */
static void
multiplexer_enqueue( Multiplexer*  m, Packet*  p )
{
    T("%s: sending %d bytes: '%s'", __FUNCTION__,
       p->len - HEADER_SIZE, quote_bytes( p->data, p->len - HEADER_SIZE ) );

    qemu_chr_write( m->cs, p->header, HEADER_SIZE );
    qemu_chr_write( m->cs, p->data, p->len - HEADER_SIZE );
    packet_free(p);
}

/* called when we received a channel registration from the
 * qemud daemon
 */
static void
multiplexer_register_channel( Multiplexer*  m,
                              const char*   name,
                              int           index )
{
    Channel*  c = m->channels;
    Channel*  c_end = c + m->count;

    for ( ; c < c_end; c++ ) {
        if ( !strcmp(c->name, name) )
            break;
    }

    if (c >= c_end) {
        D( "%s: unknown channel name '%s'",
            __FUNCTION__, name );
        return;
    }

    if (c->index >= 0) {
        D( "%s: channel '%s' re-assigned index %d",
            __FUNCTION__, name, index );
        c->index = index;
        return;
    }
    channel_set_peer( c, index, (EnqueueFunc) multiplexer_enqueue, m );
    D( "%s: channel '%s' registered as index %d",
       __FUNCTION__, c->name, c->index );
}


/* handle answers from the control channel */
static void
multiplexer_handle_control( Multiplexer*  m, Packet*  p )
{
    int  len = p->len - HEADER_SIZE;

    /* for now, the only supported answer is 'ok:connect:<name>:<XX>' where
     * <XX> is a hexdecimal channel numner */
    D( "%s: received '%s'", __FUNCTION__, quote_bytes( (const void*)p->data, (unsigned)len ) );
    if ( !memcmp( p->data, "ok:connect:", 11 ) ) do {
        char*  name = (char*)p->data + 11;
        char*  q    = strchr( name, ':' );
        int    index;

        if (q == NULL)
            break;

        q[0] = 0;
        if (q + 3 > (char*)p->data + len)
            break;

        index = hex2int( (uint8_t*)q+1, 2 );
        if (index < 0)
            break;

        multiplexer_register_channel( m, name, index );
        goto Exit;
    }
    while(0);

    D( "%s: unsupported message !!", __FUNCTION__ );
Exit:
    packet_free(p);
}


static int
multiplexer_can_read( void*  opaque )
{
    Multiplexer* m = opaque;

    return cbuffer_write_avail( m->in_cbuffer );
}

/* the data comes from the serial port, we need to reconstruct packets then
 * dispatch them to the appropriate channel */
static void
multiplexer_read( void*  opaque, const uint8_t* from, int  len )
{
    Multiplexer*  m  = opaque;
    CBuffer*      cb = m->in_cbuffer;
    int           ret = 0;

    T("%s: received %d bytes from serial: '%s'",
      __FUNCTION__, len, quote_bytes( from, len ));

    ret = cbuffer_write( cb, from, len );
    if (ret == 0)
        return;

    for (;;) {
        int   len = cbuffer_read_avail( cb );

        if (m->in_datalen == 0) {
            uint8_t  header[HEADER_SIZE];

            if (len < HEADER_SIZE)
                break;

            cbuffer_read( cb, header, HEADER_SIZE );
            m->in_datalen = hex2int( header+0, 4 );
            m->in_channel = hex2int( header+4, 2 );
        }
        else
        {
            Packet*  p;

            if (len < m->in_datalen)
                break;

            /* a full packet was received */
            p = packet_alloc();
            cbuffer_read( cb, p->data, m->in_datalen );
            p->len = HEADER_SIZE + m->in_datalen;

            /* find the channel for this packet */
            if (m->in_channel == CHANNEL_CONTROL_INDEX)
                multiplexer_handle_control( m, p );
            else {
                Channel*  c = m->channels;
                Channel*  c_end = c + m->count;

                for ( ; c < c_end; c++ ) {
                    if (c->index == m->in_channel) {
                        channel_write( c, p->data, m->in_datalen );
                        break;
                    }
                }
                packet_free(p);
            }
            m->in_datalen = 0;
        }

    }
    return;
}

static void
multiplexer_query_channel( Multiplexer*  m, const char*  name )
{
    Packet*  p = packet_alloc();
    int      len;

    len = snprintf( (char*)p->data, MAX_PAYLOAD, "connect:%s", name );

    int2hex( p->header+0, 4, len );
    int2hex( p->header+4, 2, CHANNEL_CONTROL_INDEX );
    p->len = HEADER_SIZE + len;

    multiplexer_enqueue( m, p );
}


static Channel*
multiplexer_find_channel( Multiplexer*  m, const char*  name )
{
    int  n;
    for (n = 0; n < m->count; n++)
        if ( !strcmp(m->channels[n].name, name) )
            return m->channels + n;

    return NULL;
}


static Multiplexer       _multiplexer[1];
static CharDriverState*  android_qemud_cs;

extern void
android_qemud_init( void )
{
    Multiplexer*      m = _multiplexer;

    if (android_qemud_cs != NULL)
        return;

    m->count = 0;

    cbuffer_reset( m->in_cbuffer, m->in_buff, sizeof(m->in_buff) );
    m->in_datalen = 0;
    m->in_channel = 0;

    if (qemu_chr_open_charpipe( &android_qemud_cs, &m->cs ) < 0) {
        derror( "%s: can't create charpipe to serial port",
                __FUNCTION__ );
        exit(1);
    }

    qemu_chr_add_handlers( m->cs, multiplexer_can_read,
                           multiplexer_read, NULL, m );
}


CharDriverState*  android_qemud_get_cs( void )
{
    if (android_qemud_cs == NULL)
        android_qemud_init();

    return android_qemud_cs;
}


extern int
android_qemud_get_channel( const char*  name, CharDriverState**  pcs )
{
    Multiplexer*      m = _multiplexer;
    Channel*          c;
    CharDriverState*  peer_cs;
    int               ret;

    if (m->cs == NULL)
        android_qemud_init();

    c = multiplexer_find_channel( m, name );
    if (c) {
        derror( "%s: trying to get already-opened qemud channel '%s'",
                __FUNCTION__, name );
        return -1;
    }

    if (m->count >= MAX_CHANNELS) {
        derror( "%s: too many registered channels (%d)",
                __FUNCTION__, m->count );
        return -1;
    }

    c = m->channels + m->count;

    ret = qemu_chr_open_charpipe( &peer_cs, pcs );
    if (ret == 0) {
        channel_init(c, name, peer_cs);
        m->count += 1;
        multiplexer_query_channel( m, c->name );
    }

    return ret;
}

extern int
android_qemud_set_channel( const char*  name, CharDriverState*  peer_cs )
{
    Multiplexer*  m = _multiplexer;
    Channel*      c;

    if (m->cs == NULL)
        android_qemud_init();

    c = multiplexer_find_channel(m, name);
    if (c != NULL) {
        derror( "%s: trying to set opened qemud channel '%s'",
                __FUNCTION__, name );
        return -1;
    }

    if (m->count >= MAX_CHANNELS) {
        derror( "%s: too many registered channels (%d)",
                __FUNCTION__, m->count );
        return -1;
    }

    c = m->channels + m->count;
    channel_init(c, name, peer_cs);
    m->count += 1;
    multiplexer_query_channel( m, c->name );

    return 0;
}