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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* tp_test.c - telepathy-sofiasip test utility (modified from
* libtelepathy's proto.c)
*
* Copyright (C) 2005-2006 Nokia Corporation.
*
* This work is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This work 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef DBUS_API_SUBJECT_TO_CHANGE
#define DBUS_API_SUBJECT_TO_CHANGE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <dbus/dbus-glib.h>
#include <glib.h>
#include <libtelepathy/tp-conn.h>
#include <libtelepathy/tp-connmgr.h>
#include <libtelepathy/tp-chan.h>
#include <libtelepathy/tp-chan-gen.h>
#include <libtelepathy/tp-ch-gen.h>
#include <libtelepathy/tp-chan-iface-group-gen.h>
#include <libtelepathy/tp-chan-type-text-gen.h>
#include <libtelepathy/tp-chan-type-streamed-media-gen.h>
#include <libtelepathy/tp-props-iface.h>
#include <libtelepathy/tp-constants.h>
#include <libtelepathy/tp-interfaces.h>
/*
* Test connection manager and account
*/
#define CONNMGR_NAME "sofiasip"
#define CONNMGR_BUS "org.freedesktop.Telepathy.ConnectionManager.sofiasip"
#define CONNMGR_PATH "/org/freedesktop/Telepathy/ConnectionManager/sofiasip"
#define PROTOCOL "sip"
enum {
STATE_START = 0,
STATE_CHAN,
STATE_STREAMS,
STATE_RUNNING,
STATE_COMPLETED,
};
static gboolean global_connected = FALSE;
static guint global_remote_handle = 0;
static GMainLoop *global_mainloop = NULL;
static DBusGProxy *global_streamengine = NULL;
static DBusGConnection *global_dbus_connection = NULL;
static gint global_exit_request = 0;
static char* global_conn_path = NULL;
static char* global_chan_path = NULL;
static guint global_chan_handle = 0;
static guint global_call_state = STATE_START;
static void newconnection_handler(DBusGProxy *proxy, const char *s1,
const char *o, const char *s2,
gpointer user_data);
static void new_channel_handler(DBusGProxy *proxy, const char *object_path,
const char *channel_type, guint handle_type,
guint handle, gboolean suppress_handle,
gpointer user_data);
static gboolean status_changed_cb(DBusGProxy *proxy,
guint status, guint reason,
gpointer user_data);
static void request_handles_reply_cb(DBusGProxy *proxy, GArray *OUT_arg2, GError *error, gpointer userdata);
static void request_chan_cb(DBusGProxy *proxy,
gchar *channel_path,
GError *error,
gpointer data);
static void request_streams_cb(DBusGProxy *proxy,
GPtrArray *streams,
GError *error,
gpointer data);
static void tpcaller_signal_handler(int signo);
static void check_conn_properties(TpConn *conn);
static TpConn *action_login(const char* sip_address, const char* sip_password, const char* sip_proxy)
{
DBusGConnection *connection;
TpConn *conn;
DBusGProxy *streamengine = NULL;
TpConnMgr *connmgr;
guint status = 1;
GError *error = NULL;
GValue val_acc, val_auth_usr, val_pwd, val_proxy;
GHashTable *connection_parameters = g_hash_table_new(g_str_hash, NULL);
connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
if (connection == NULL) {
g_printerr("Failed to open connection to bus: %s\n",
error->message);
g_error_free(error);
exit(1);
}
printf("connected to DBus with connection %p\n", connection);
global_dbus_connection = connection;
/* Create a connection manager object */
g_print("Attempting to create a connection manager object.\n");
connmgr = tp_connmgr_new(connection, CONNMGR_BUS, CONNMGR_PATH,
TP_IFACE_CONN_MGR_INTERFACE);
if (connmgr == NULL) {
g_error("Failed to create a connection manager, skipping manager startup.");
}
else {
g_print("Creating a connection manager succeeded.\n");
}
g_print("Attempting to register a signal handler for NewConnection signal.\n");
dbus_g_proxy_connect_signal(DBUS_G_PROXY(connmgr), "NewConnection",
G_CALLBACK(newconnection_handler),
NULL, NULL);
/* Setting "g_type" is a hack since GValue is broken */
val_acc.g_type = 0;
val_auth_usr.g_type = 0;
val_proxy.g_type = 0;
val_pwd.g_type = 0;
g_value_init(&val_acc, G_TYPE_STRING);
g_value_init(&val_auth_usr, G_TYPE_STRING);
g_value_init(&val_proxy, G_TYPE_STRING);
g_value_init(&val_pwd, G_TYPE_STRING);
/* Connection parameters are dummy: fill in suitable replacements */
g_value_set_string(&val_acc, sip_address);
g_value_set_string(&val_pwd, sip_password);
g_hash_table_insert(connection_parameters, "account",
(gpointer) &val_acc);
g_hash_table_insert(connection_parameters, "password",
(gpointer) &val_pwd);
if (sip_proxy != NULL) {
g_value_set_string(&val_proxy, sip_proxy);
g_hash_table_insert(connection_parameters, "proxy",
(gpointer) &val_proxy);
}
/* Create a new actual connection with the connection manager */
g_print("Attempting to create a connection object.\n");
conn = tp_connmgr_new_connection(connmgr,
connection_parameters,
PROTOCOL);
g_assert(conn != NULL);
/* step: connection creation succesful */
dbus_g_proxy_connect_signal(DBUS_G_PROXY(conn), "NewChannel",
G_CALLBACK(new_channel_handler),
NULL, NULL);
g_print("Creation of a connection object succeeded.\n");
streamengine = dbus_g_proxy_new_for_name (connection,
"org.freedesktop.Telepathy.StreamEngine",
"/org/freedesktop/Telepathy/StreamEngine",
"org.freedesktop.Telepathy.ChannelHandler");
g_assert(streamengine != NULL);
global_streamengine = streamengine;
/* Check if connection is active; if not, add a callback
* for StatusChange signal */
if (!tp_conn_get_status(DBUS_G_PROXY(conn), &status, &error) || status != 0) {
g_print("GetStatus did not work synchronously, trying async version.\n");
dbus_g_proxy_connect_signal(DBUS_G_PROXY(conn), "StatusChanged",
G_CALLBACK(status_changed_cb),
NULL, NULL);
if (error)
g_error_free(error);
while (global_connected != TRUE && global_exit_request == 0) {
g_main_context_iteration(NULL, TRUE);
}
} else {
check_conn_properties(conn);
}
g_hash_table_destroy(connection_parameters);
return conn;
}
static int action_make_call(TpConn *conn, const char* to_uri)
{
DBusGProxy *streamengine = global_streamengine;
GError *error = NULL;
TpChan *channel;
DBusGProxy *stream = NULL;
GArray *types = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
guint mediatype = TP_MEDIA_STREAM_TYPE_AUDIO;
const char *urilist[2] = { NULL, NULL };
int result = 0;
guint old_state = STATE_START;
urilist[0] = to_uri;
/* state machine for setting up a call */
while (global_call_state != STATE_COMPLETED) {
switch (global_call_state)
{
case STATE_START:
g_print("Requesting handle for SIP URI %s.\n", to_uri);
tp_conn_request_handles_async(DBUS_G_PROXY(conn), TP_CONN_HANDLE_TYPE_CONTACT, (const char**)urilist, request_handles_reply_cb, NULL);
break;
case STATE_CHAN:
/* request for a channel for outbound call */
g_print("Attempting to make an outbound call to %s (%d).\n", to_uri, global_remote_handle);
tp_conn_request_channel_async(DBUS_G_PROXY(conn),
TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
TP_CONN_HANDLE_TYPE_CONTACT, global_remote_handle, TRUE,
request_chan_cb, NULL);
break;
case STATE_STREAMS:
g_debug("Calling HandleChannel on %p, connection='%s', chan='%s'.\n",
streamengine, global_conn_path, global_chan_path);
error = NULL;
tp_ch_handle_channel (streamengine,
CONNMGR_BUS,
global_conn_path,
TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
global_chan_path,
TP_CONN_HANDLE_TYPE_CONTACT,
global_remote_handle,
&error);
if (error) {
g_print ("ERROR: %s", error->message);
g_error_free (error);
g_object_unref (streamengine);
result = -1;
}
else
g_print ("Succesful HandleChannel with streamengine.\n");
channel = tp_chan_new (global_dbus_connection,
CONNMGR_BUS,
global_chan_path,
TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
TP_CONN_HANDLE_TYPE_CONTACT,
global_remote_handle);
g_assert (channel);
stream = tp_chan_get_interface (channel,
TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
if (stream) {
g_array_append_val (types, mediatype);
g_debug("%s: calling RequestStream with types->len=%d", G_STRFUNC, types->len);
tp_chan_type_streamed_media_request_streams_async (stream,
global_chan_handle /*global_remote_handle*/,
types,
request_streams_cb,
NULL);
}
break;
case STATE_RUNNING:
/* stream ready, call setup completed */
global_call_state = STATE_COMPLETED;
break;
}
/* run the mainloop */
while (global_call_state == old_state &&
global_exit_request == 0) {
g_main_context_iteration(NULL, TRUE);
}
old_state = global_call_state;
if (global_exit_request)
global_call_state = STATE_COMPLETED;
}
return result;
}
int main(int argc, char **argv)
{
TpConn *conn;
GError *error = NULL;
GMainLoop *mainloop;
char* sip_proxy = getenv("SIP_PROXY");
#ifndef _WIN32
/* see: http://www.opengroup.org/onlinepubs/007908799/xsh/sigaction.html */
struct sigaction sigact;
memset(&sigact, 0, sizeof(sigact));
sigact.sa_handler = tpcaller_signal_handler;
sigaction(SIGINT, &sigact, NULL); /* ctrl-c */
sigaction(SIGABRT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
#endif
if (argc < 3) {
g_message("Usage: tp_caller <sip-aor> <password> [<sip-address-to-call> <sip-outbound-proxy>]");
exit(1);
}
g_type_init();
global_mainloop = mainloop = g_main_loop_new (NULL, FALSE);
if (sip_proxy == NULL)
sip_proxy = argc > 4 ? argv[4] : NULL;
conn = action_login(argv[1], argv[2], sip_proxy);
if (conn) {
if (argc > 3 &&
strncmp(argv[3], "sip:", 4) == 0) {
/* only call if 3rd param is a valid SIP URI */
action_make_call(conn, argv[3]);
};
g_print("Entering tp_caller mainloop.\n");
g_main_loop_run (mainloop);
g_print("Disconnecting from network.\n");
tp_conn_disconnect (DBUS_G_PROXY(conn), &error);
if (error)
g_error_free (error);
g_object_unref (conn);
dbus_g_connection_unref (global_dbus_connection);
}
else
g_warning("Unable to login with the requested account.");
g_print("Closing connection to SIP network.\n");
return 0;
}
static void newconnection_handler(DBusGProxy *proxy, const char *bus,
const char *path, const char *proto,
gpointer user_data)
{
g_print("NewConnection callback:\n\tbus=%s\n\tpath=%s\n\tproto=%s\n", bus, path, proto);
global_conn_path = g_strdup(path);
}
static void handle_incoming_call (DBusGConnection *proxy, const char *chan_path, guint handle)
{
GError *error = NULL;
GArray *array;
TpChan *channel;
DBusGProxy *chgroup = NULL;
channel = tp_chan_new (proxy,
CONNMGR_BUS,
chan_path,
TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
TP_CONN_HANDLE_TYPE_CONTACT,
handle);
g_assert (channel);
chgroup = tp_chan_get_interface (channel,
TELEPATHY_CHAN_IFACE_GROUP_QUARK);
g_assert (chgroup);
g_print("\tInbound call, passing the media channel to stream engine.\n");
/* step 1: pass the channel to the stream engine */
tp_ch_handle_channel (global_streamengine,
CONNMGR_BUS,
global_conn_path,
TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
chan_path,
TP_CONN_HANDLE_TYPE_CONTACT,
handle,
&error);
if (error) {
g_print ("ERROR: %s", error->message);
g_error_free (error);
}
/* step 2: inform connection manager we accept the session */
tp_chan_iface_group_get_self_handle (chgroup, &global_chan_handle, &error);
if (error) {
g_warning ("cannot get self handle: %s", error->message);
g_error_free (error), error = NULL;
}
g_print("\tAccepting the call (self handle %d).\n", global_chan_handle);
array = g_array_new (FALSE, FALSE, sizeof (guint));
g_array_append_val (array, global_chan_handle);
tp_chan_iface_group_add_members (chgroup, array, "", &error);
if (error) {
g_warning ("cannot add member %u to media channel: %s", handle, error->message);
g_error_free (error), error = NULL;
}
g_array_free(array, TRUE);
}
static void new_channel_handler(DBusGProxy *proxy, const char *object_path,
const char *channel_type, guint handle_type,
guint handle, gboolean suppress_handler,
gpointer user_data)
{
g_print("NewChannel callback\n\tpath=%s\n\ttype=%s, handle=%u\n",
object_path, channel_type, handle);
if (strcmp(channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
global_chan_path = g_strdup(object_path);
/* note: handle == 0 for inbound calls */
global_chan_handle = handle;
if (suppress_handler == 0) {
/* an inbound session, let's answer it */
handle_incoming_call (global_dbus_connection, global_chan_path, handle);
}
}
}
static void request_chan_cb(DBusGProxy *proxy,
gchar *channel_path,
GError *error,
gpointer data)
{
g_print("RequestChan callback:\n\tchanpath=%s\n", channel_path);
if (error != NULL) {
g_warning("%s: error in callback - %s\n", G_STRFUNC, error->message);
g_error_free(error);
global_exit_request = 1;
return;
}
if (channel_path == NULL) {
global_exit_request = 1;
return;
}
global_call_state = STATE_STREAMS;
}
static void request_streams_cb(DBusGProxy *proxy,
GPtrArray *streams,
GError *error,
gpointer data)
{
g_print("RequestStreams callback with %d streams\n", streams ? streams->len : 0);
if (error != NULL) {
g_warning("%s: error in callback - %s\n", G_STRFUNC, error->message);
g_error_free(error);
return;
}
global_call_state = STATE_RUNNING;
}
static gboolean status_changed_cb(DBusGProxy *proxy,
guint status, guint reason,
gpointer user_data)
{
g_print("StatusChanged signal\n");
if (status == 0) {
g_print("\tConnected!\n");
global_connected = TRUE;
check_conn_properties(TELEPATHY_CONN(proxy));
}
else {
global_connected = FALSE;
}
return TRUE;
}
static void request_handles_reply_cb(DBusGProxy *proxy, GArray *handles, GError *error, gpointer userdata)
{
guint i;
g_print("RequestHandles callback\n");
if (error) {
g_warning("%s: error in callback - %s\n", G_STRFUNC, error->message);
g_error_free(error);
global_exit_request = 1;
return;
}
for(i = 0; handles && i < handles->len; i++) {
guint ret_handle = g_array_index(handles, guint, i);
if (i == 0) {
global_remote_handle = ret_handle;
}
g_print("\tRequested handle received: %u (item %u)\n", ret_handle, i);
}
global_call_state = STATE_CHAN;
}
static void tpcaller_signal_handler(int signo)
{
global_exit_request = 1;
if (global_mainloop)
g_main_loop_quit(global_mainloop);
}
static void check_conn_properties(TpConn *conn)
{
TpPropsIface *conn_props;
conn_props = TELEPATHY_PROPS_IFACE (tp_conn_get_interface (
conn, TELEPATHY_PROPS_IFACE_QUARK));
if (conn_props == NULL) {
g_warning ("The connection object does not support " TP_IFACE_PROPERTIES);
}
}
|