aboutsummaryrefslogtreecommitdiffstats
path: root/telephony/sysdeps_qemu.c
blob: 6aa50380e73683e276de2fe37d911914cf96e62d (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
/* 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 "sockets.h"
#include "sysdeps.h"
#include "qemu-common.h"
#include "qemu-timer.h"
#include "qemu-char.h"
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#endif

#define  DEBUG  0

#define  D_ACTIVE  DEBUG

#if DEBUG
#define  D(...)  do { if (D_ACTIVE) fprintf(stderr, __VA_ARGS__); } while (0)
#else
#define  D(...)  ((void)0)
#endif

/** TIME
 **/

SysTime
sys_time_ms( void )
{
    return qemu_get_clock_ms(rt_clock);
}

/** TIMERS
 **/

typedef struct SysTimerRec_ {
    QEMUTimer*    timer;
    QEMUTimerCB*  callback;
    void*         opaque;
    SysTimer      next;
} SysTimerRec;

#define  MAX_TIMERS  32

static SysTimerRec  _s_timers0[ MAX_TIMERS ];
static SysTimer     _s_free_timers;

static void
sys_init_timers( void )
{
    int  nn;
    for (nn = 0; nn < MAX_TIMERS-1; nn++)
        _s_timers0[nn].next = _s_timers0 + (nn+1);

    _s_free_timers = _s_timers0;
}

static SysTimer
sys_timer_alloc( void )
{
    SysTimer  timer = _s_free_timers;

    if (timer != NULL) {
        _s_free_timers = timer->next;
        timer->next    = NULL;
        timer->timer   = NULL;
    }
    return timer;
}


static void
sys_timer_free( SysTimer  timer )
{
    if (timer->timer) {
        qemu_del_timer( timer->timer );
        qemu_free_timer( timer->timer );
        timer->timer = NULL;
    }
    timer->next    = _s_free_timers;
    _s_free_timers = timer;
}


SysTimer   sys_timer_create( void )
{
    SysTimer  timer = sys_timer_alloc();
    return timer;
}

void
sys_timer_set( SysTimer  timer, SysTime  when, SysCallback   _callback, void*  opaque )
{
    QEMUTimerCB*  callback = (QEMUTimerCB*)_callback;

    if (callback == NULL) {  /* unsetting the timer */
        if (timer->timer) {
            qemu_del_timer( timer->timer );
            qemu_free_timer( timer->timer );
            timer->timer = NULL;
        }
        timer->callback = callback;
        timer->opaque   = NULL;
        return;
    }

    if ( timer->timer ) {
         if ( timer->callback == callback && timer->opaque == opaque )
            goto ReuseTimer;

         /* need to replace the timer */
         qemu_free_timer( timer->timer );
    }

    timer->timer    = qemu_new_timer_ms( rt_clock, callback, opaque );
    timer->callback = callback;
    timer->opaque   = opaque;

ReuseTimer:
    qemu_mod_timer( timer->timer, when );
}

void
sys_timer_unset( SysTimer  timer )
{
    if (timer->timer) {
        qemu_del_timer( timer->timer );
    }
}

void
sys_timer_destroy( SysTimer  timer )
{
    sys_timer_free( timer );
}


/** CHANNELS
 **/

typedef struct SysChannelRec_ {
    int                 fd;
    SysChannelCallback  callback;
    void*               opaque;
    SysChannel          next;
} SysChannelRec;

#define  MAX_CHANNELS  16

static SysChannelRec  _s_channels0[ MAX_CHANNELS ];
static SysChannel     _s_free_channels;

static void
sys_init_channels( void )
{
    int  nn;

    for ( nn = 0; nn < MAX_CHANNELS-1; nn++ ) {
        _s_channels0[nn].next = _s_channels0 + (nn+1);
    }
    _s_free_channels = _s_channels0;
}

static SysChannel
sys_channel_alloc( )
{
    SysChannel  channel = _s_free_channels;
    if (channel != NULL) {
        _s_free_channels  = channel->next;
        channel->next     = NULL;
        channel->fd       = -1;
        channel->callback = NULL;
        channel->opaque   = NULL;
    }
    return channel;
}

static void
sys_channel_free( SysChannel  channel )
{
    if (channel->fd >= 0) {
        socket_close( channel->fd );
        channel->fd = -1;
    }
    channel->next    = _s_free_channels;
    _s_free_channels = channel;
}


static void
sys_channel_read_handler( void*  _channel )
{
    SysChannel  channel = _channel;
    D( "%s: read event for channel %p:%d\n", __FUNCTION__,
       channel, channel->fd );
    channel->callback( channel->opaque, SYS_EVENT_READ );
}

static void
sys_channel_write_handler( void*  _channel )
{
    SysChannel  channel = _channel;
    D( "%s: write event for channel %p:%d\n", __FUNCTION__, channel, channel->fd );
    channel->callback( channel->opaque, SYS_EVENT_WRITE );
}

void
sys_channel_on( SysChannel          channel,
                int                 events,
                SysChannelCallback  event_callback,
                void*               event_opaque )
{
    IOHandler*  read_handler  = NULL;
    IOHandler*  write_handler = NULL;

    if (events & SYS_EVENT_READ) {
        read_handler = sys_channel_read_handler;
    }
    if (events & SYS_EVENT_WRITE) {
        write_handler = sys_channel_write_handler;
    }
    channel->callback = event_callback;
    channel->opaque   = event_opaque;
    qemu_set_fd_handler( channel->fd, read_handler, write_handler, channel );
}

int
sys_channel_read( SysChannel  channel, void*  buffer, int  size )
{
    int   len = size;
    char* buf = (char*) buffer;

    while (len > 0) {
        int  ret = socket_recv(channel->fd, buf, len);
        if (ret < 0) {
            if (errno == EINTR)
                continue;
            if (errno == EWOULDBLOCK || errno == EAGAIN)
                break;
            D( "%s: after reading %d bytes, recv() returned error %d: %s\n",
                __FUNCTION__, size - len, errno, errno_str);
            return -1;
        } else if (ret == 0) {
            break;
        } else {
            buf += ret;
            len -= ret;
        }
    }
    return size - len;
}


int
sys_channel_write( SysChannel  channel, const void*  buffer, int  size )
{
    int         len = size;
    const char* buf = (const char*) buffer;

    while (len > 0) {
        int  ret = socket_send(channel->fd, buf, len);
        if (ret < 0) {
            if (errno == EINTR)
                continue;
            if (errno == EWOULDBLOCK || errno == EAGAIN)
                break;
            D( "%s: send() returned error %d: %s\n",
                __FUNCTION__, errno, errno_str);
            return -1;
        } else if (ret == 0) {
            break;
        } else {
            buf += ret;
            len -= ret;
        }
    }
    return size - len;
}

void  sys_channel_close( SysChannel  channel )
{
    qemu_set_fd_handler( channel->fd, NULL, NULL, NULL );
    sys_channel_free( channel );
}

void  sys_main_init( void )
{
    sys_init_channels();
    sys_init_timers();
}


int   sys_main_loop( void )
{
    /* no looping, qemu has its own event loop */
    return 0;
}




SysChannel
sys_channel_create_tcp_server( int port )
{
    SysChannel  channel = sys_channel_alloc();

    channel->fd = socket_anyaddr_server( port, SOCKET_STREAM );
    if (channel->fd < 0) {
        D( "%s: failed to created network socket on TCP:%d\n",
            __FUNCTION__, port );
        sys_channel_free( channel );
        return NULL;
    }

    D( "%s: server channel %p:%d now listening on port %d\n",
       __FUNCTION__, channel, channel->fd, port );

    return channel;
}


SysChannel
sys_channel_create_tcp_handler( SysChannel  server_channel )
{
    SysChannel  channel = sys_channel_alloc();

    D( "%s: creating handler from server channel %p:%d\n", __FUNCTION__,
       server_channel, server_channel->fd );

    channel->fd = socket_accept_any( server_channel->fd );
    if (channel->fd < 0) {
        perror( "accept" );
        sys_channel_free( channel );
        return NULL;
    }

    /* disable Nagle algorithm */
    socket_set_nodelay( channel->fd );

    D( "%s: handler %p:%d created from server %p:%d\n", __FUNCTION__,
        server_channel, server_channel->fd, channel, channel->fd );

     return channel;
}


SysChannel
sys_channel_create_tcp_client( const char*  hostname, int  port )
{
    SysChannel  channel = sys_channel_alloc();

    channel->fd = socket_network_client( hostname, port, SOCKET_STREAM );
    if (channel->fd < 0) {
        sys_channel_free(channel);
        return NULL;
    };

    /* set to non-blocking and disable Nagle algorithm */
    socket_set_nonblock( channel->fd );
    socket_set_nodelay( channel->fd );

    return channel;
}