gmyth-upnp/src/gmyth_upnp.c
author rosfran
Thu May 24 01:26:10 2007 +0100 (2007-05-24)
branchtrunk
changeset 706 5cc97240e238
parent 704 5b6a77d85cf7
child 754 cb885ee44618
permissions -rw-r--r--
[svn r712] GObject's signal handling on receiving UPnP device notifications.
melunko@250
     1
/**
melunko@250
     2
 * GMyth Library
melunko@250
     3
 *
melunko@250
     4
 * @file gmyth/gmyth_upnp.c
melunko@250
     5
 * 
melunko@250
     6
 * @brief <p> GMythUPnP allows that a MythTV frontend discovers a 
melunko@250
     7
 * MythTV backend, using the UPnP architecture.
melunko@250
     8
 *
melunko@250
     9
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
melunko@250
    10
 * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
melunko@250
    11
 * 
melunko@250
    12
 * This program is free software; you can redistribute it and/or modify
melunko@250
    13
 * it under the terms of the GNU Lesser General Public License as published by
melunko@250
    14
 * the Free Software Foundation; either version 2 of the License, or
melunko@250
    15
 * (at your option) any later version.
melunko@250
    16
 *
melunko@250
    17
 * This program is distributed in the hope that it will be useful,
melunko@250
    18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
melunko@250
    19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
melunko@250
    20
 * GNU General Public License for more details.
melunko@250
    21
 *
melunko@250
    22
 * You should have received a copy of the GNU Lesser General Public License
melunko@250
    23
 * along with this program; if not, write to the Free Software
melunko@250
    24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
melunko@250
    25
 *   
melunko@250
    26
 */
melunko@250
    27
melunko@250
    28
#ifdef HAVE_CONFIG_H
melunko@250
    29
#include "config.h"
melunko@250
    30
#endif
melunko@250
    31
melunko@250
    32
#include "gmyth_upnp.h"
rosfran@696
    33
#include "gmyth_upnp_marshal.h"
melunko@250
    34
melunko@250
    35
#include <arpa/inet.h>
melunko@250
    36
#include <sys/types.h>
melunko@250
    37
#include <sys/socket.h>
melunko@250
    38
#include <netdb.h>
melunko@250
    39
#include <errno.h>
melunko@250
    40
#include <stdlib.h>
melunko@250
    41
rosfran@260
    42
#include <gmyth/gmyth_backendinfo.h>
rosfran@260
    43
#include <gmyth/gmyth_socket.h>
rosfran@260
    44
#include <gmyth/gmyth_uri.h>
rosfran@260
    45
#include <gmyth/gmyth_debug.h>
melunko@250
    46
rosfran@696
    47
/* Maximum number of searches in the synchronized search */
melunko@250
    48
#define GMYTH_UPNP_MAX_SEARCHS		10
melunko@250
    49
rosfran@696
    50
#define GMYTH_UPNP_GET_PRIVATE(obj) \
rosfran@696
    51
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_UPNP_TYPE, GMythUPnPPrivate))
rosfran@696
    52
rosfran@696
    53
struct _GMythUPnPPrivate {
rosfran@696
    54
	GHashTable *mythtv_servers;
rosfran@696
    55
	GMythUPnPDeviceStatus last_status;
rosfran@696
    56
	gboolean upnp_dev_found;
rosfran@696
    57
	gchar *udn;
rosfran@696
    58
	GMutex *mutex;
rosfran@696
    59
};
rosfran@696
    60
melunko@250
    61
static void gmyth_upnp_class_init          (GMythUPnPClass *klass);
melunko@250
    62
static void gmyth_upnp_init                (GMythUPnP *object);
melunko@250
    63
melunko@250
    64
static void gmyth_upnp_dispose  (GObject *object);
melunko@250
    65
static void gmyth_upnp_finalize (GObject *object);
melunko@250
    66
rosfran@700
    67
static void _mythtv_device_found ( GMythUPnP *gmyth_upnp, GMythUPnPDeviceStatus status, gchar *dev );
rosfran@696
    68
static void _clinkc_mythtv_device_found ( gchar *udn, GMythUPnPDeviceStatus status );
melunko@250
    69
rosfran@696
    70
static gboolean gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info,
rosfran@696
    71
		GMythUPnPDeviceListener listener);
rosfran@696
    72
rosfran@696
    73
static gboolean gmyth_upnp_got_mythtv_service( CgUpnpControlPoint* controlPt, gchar **udn, 
rosfran@696
    74
						GHashTable **mythtv_servers_lst );
melunko@250
    75
melunko@250
    76
G_DEFINE_TYPE(GMythUPnP, gmyth_upnp, G_TYPE_OBJECT)
melunko@250
    77
rosfran@696
    78
static GMythUPnP *gmyth_upnp_static = NULL;
melunko@250
    79
melunko@250
    80
static void
melunko@250
    81
gmyth_upnp_class_init (GMythUPnPClass *klass)
melunko@250
    82
{
melunko@250
    83
	GObjectClass *gobject_class;
rosfran@696
    84
	GMythUPnPClass *gupnp_class;
melunko@250
    85
melunko@250
    86
	gobject_class = (GObjectClass *) klass;
rosfran@696
    87
	gupnp_class = (GMythUPnPClass*) gobject_class;
melunko@250
    88
melunko@250
    89
	gobject_class->dispose  = gmyth_upnp_dispose;
rosfran@696
    90
	gobject_class->finalize = gmyth_upnp_finalize;
rosfran@696
    91
	
rosfran@696
    92
	g_type_class_add_private( gobject_class, sizeof( GMythUPnPPrivate ) );
rosfran@696
    93
rosfran@696
    94
	gupnp_class->device_found_handler = _mythtv_device_found;
rosfran@696
    95
rosfran@696
    96
	gupnp_class->device_found_handler_signal_id =
rosfran@696
    97
		g_signal_new ("device-found",
rosfran@696
    98
				G_TYPE_FROM_CLASS (gupnp_class),
rosfran@696
    99
				G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE |
rosfran@696
   100
				G_SIGNAL_NO_HOOKS, 0, NULL, NULL,
rosfran@696
   101
				gmyth_upnp_marshal_VOID__INT_STRING, G_TYPE_NONE, 2,
rosfran@696
   102
				G_TYPE_INT, G_TYPE_STRING);
rosfran@696
   103
melunko@250
   104
}
melunko@250
   105
melunko@250
   106
static void
melunko@250
   107
gmyth_upnp_init ( GMythUPnP *gmyth_upnp )
melunko@250
   108
{
rosfran@696
   109
rosfran@696
   110
	gmyth_upnp->backend_info = NULL;
rosfran@696
   111
melunko@250
   112
	gmyth_upnp->control_point = NULL;
rosfran@696
   113
rosfran@696
   114
	gmyth_upnp->priv = GMYTH_UPNP_GET_PRIVATE(gmyth_upnp);
rosfran@696
   115
	gmyth_upnp->priv->mutex = g_mutex_new ();
rosfran@696
   116
	gmyth_upnp->priv->upnp_dev_found = FALSE;
rosfran@696
   117
rosfran@696
   118
	g_signal_connect (G_OBJECT (gmyth_upnp), "device-found",
rosfran@696
   119
			(GCallback) (GMYTH_UPNP_GET_CLASS (gmyth_upnp)->
rosfran@696
   120
				device_found_handler), NULL);
rosfran@696
   121
rosfran@696
   122
	gmyth_upnp_static = gmyth_upnp;
rosfran@696
   123
melunko@250
   124
}
melunko@250
   125
melunko@250
   126
/** Creates a new instance of GMythUPnP.
melunko@250
   127
 * 
melunko@250
   128
 * @return a new instance of GMythUPnP. 
melunko@250
   129
 */
melunko@250
   130
GMythUPnP *
rosfran@696
   131
gmyth_upnp_new ( GMythBackendInfo *gmyth_backend_info, GMythUPnPDeviceListener handler ) 
melunko@250
   132
{
melunko@250
   133
    GMythUPnP *gmyth_upnp = GMYTH_UPNP (g_object_new (GMYTH_UPNP_TYPE, NULL));
melunko@250
   134
    
rosfran@696
   135
    g_object_ref( gmyth_backend_info );
rosfran@696
   136
rosfran@696
   137
    gmyth_upnp->backend_info = gmyth_backend_info;
rosfran@696
   138
rosfran@696
   139
    if ( !gmyth_upnp_initialize ( gmyth_upnp, gmyth_backend_info, handler ) )
rosfran@696
   140
    {
rosfran@696
   141
    	gmyth_debug( "Error initializing the GMythUPnP!!!" );
rosfran@696
   142
    }
melunko@250
   143
    
melunko@250
   144
    return gmyth_upnp;
melunko@250
   145
}
melunko@250
   146
melunko@250
   147
static void
melunko@250
   148
gmyth_upnp_dispose  (GObject *object)
melunko@250
   149
{
melunko@250
   150
	GMythUPnP *gmyth_upnp = GMYTH_UPNP(object);
melunko@250
   151
melunko@250
   152
	if ( gmyth_upnp->control_point != NULL ) {
melunko@250
   153
		cg_upnp_controlpoint_stop( gmyth_upnp->control_point );
melunko@250
   154
		cg_upnp_controlpoint_delete( gmyth_upnp->control_point );
melunko@250
   155
		gmyth_upnp->control_point = NULL;
melunko@250
   156
	}
rosfran@696
   157
	
rosfran@696
   158
	if ( gmyth_upnp->priv->mythtv_servers != NULL )	{
rosfran@696
   159
		g_hash_table_destroy( gmyth_upnp->priv->mythtv_servers );
rosfran@696
   160
		gmyth_upnp->priv->mythtv_servers = NULL;
rosfran@696
   161
	}
melunko@250
   162
rosfran@696
   163
	if ( gmyth_upnp->backend_info != NULL )	{
rosfran@696
   164
		g_object_unref( gmyth_upnp->backend_info );
rosfran@696
   165
		gmyth_upnp->backend_info = NULL;
melunko@250
   166
	}
rosfran@696
   167
melunko@250
   168
	G_OBJECT_CLASS (gmyth_upnp_parent_class)->dispose (object);
melunko@250
   169
}
melunko@250
   170
melunko@250
   171
static void
melunko@250
   172
gmyth_upnp_finalize (GObject *object)
melunko@250
   173
{
melunko@250
   174
	g_signal_handlers_destroy (object);
melunko@250
   175
melunko@250
   176
	G_OBJECT_CLASS (gmyth_upnp_parent_class)->finalize (object);
melunko@250
   177
}
melunko@250
   178
rosfran@706
   179
gchar *
rosfran@706
   180
gmyth_upnp_device_status_to_string( GMythUPnPDeviceStatus status )
rosfran@706
   181
{
rosfran@706
   182
	if ( status == CgUpnpDeviceStatusAdded )
rosfran@706
   183
		return "Added";
rosfran@706
   184
	else if ( status == CgUpnpDeviceStatusUpdated )
rosfran@706
   185
		return "Updated";
rosfran@706
   186
	else if ( status == CgUpnpDeviceStatusInvalid )
rosfran@706
   187
		return "Invalid";
rosfran@706
   188
	else if ( status == CgUpnpDeviceStatusRemoved )
rosfran@706
   189
		return "Removed";
rosfran@706
   190
	
rosfran@706
   191
	return "";
rosfran@706
   192
}
rosfran@706
   193
melunko@250
   194
/**
rosfran@696
   195
 * GObject's signal handler
rosfran@696
   196
 */ 
rosfran@696
   197
static void
rosfran@700
   198
_mythtv_device_found( GMythUPnP *gmyth_upnp, GMythUPnPDeviceStatus status, gchar* udn )
rosfran@696
   199
{
rosfran@706
   200
	g_debug( "Device: [status = %s, UDN = %s]\n", 
rosfran@706
   201
		gmyth_upnp_device_status_to_string( status ), udn );
rosfran@696
   202
}
rosfran@696
   203
rosfran@696
   204
/**
rosfran@696
   205
 * GObject's signal handler
rosfran@696
   206
 */
rosfran@696
   207
static void 
rosfran@696
   208
_clinkc_mythtv_device_found ( gchar *udn, GMythUPnPDeviceStatus status )
rosfran@696
   209
{
rosfran@699
   210
	if ( gmyth_upnp_static != NULL && udn != NULL )
rosfran@696
   211
		g_signal_emit ( gmyth_upnp_static, 
rosfran@696
   212
			GMYTH_UPNP_GET_CLASS (gmyth_upnp_static)->device_found_handler_signal_id, 0, /* details */
rosfran@696
   213
			status, udn );
rosfran@696
   214
}
rosfran@696
   215
rosfran@696
   216
/**
melunko@250
   217
 * Create a control point and start it.
melunko@250
   218
 */
melunko@250
   219
static gboolean
rosfran@696
   220
gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info, 
rosfran@696
   221
	GMythUPnPDeviceListener device_found_handler )
melunko@250
   222
{
rosfran@696
   223
	gboolean ret = TRUE;
rosfran@696
   224
melunko@250
   225
	g_return_val_if_fail( gmyth_backend_info != NULL, FALSE );
rosfran@696
   226
melunko@250
   227
	/* Create the cybergarage control point */
melunko@250
   228
	gmyth_upnp->control_point = cg_upnp_controlpoint_new();
rosfran@696
   229
rosfran@696
   230
	if ( device_found_handler != NULL )
rosfran@696
   231
	{
rosfran@696
   232
		GMYTH_UPNP_GET_CLASS(gmyth_upnp)->device_found_handler = device_found_handler; 
rosfran@696
   233
rosfran@696
   234
		cg_upnp_controlpoint_setdevicelistener( gmyth_upnp->control_point, _clinkc_mythtv_device_found );
rosfran@696
   235
	}
rosfran@696
   236
melunko@250
   237
	/* Start the control point */
rosfran@696
   238
	if ( cg_upnp_controlpoint_start( gmyth_upnp->control_point ) == FALSE)
melunko@250
   239
	{
rosfran@696
   240
		gmyth_debug( "Unable to start UPnP control point!!!" );
rosfran@696
   241
		ret = FALSE;
rosfran@696
   242
		goto done;
rosfran@696
   243
	}
melunko@250
   244
	else
melunko@250
   245
	{
rosfran@696
   246
		gmyth_debug( "Control point started." );
melunko@250
   247
	}
rosfran@696
   248
rosfran@696
   249
done:
rosfran@696
   250
rosfran@696
   251
	return ret;
rosfran@696
   252
}
rosfran@696
   253
rosfran@696
   254
static void
rosfran@696
   255
_gmyth_foreach_key_value( gchar *udn, gchar *dev, GList *upnp_servers_list )
rosfran@696
   256
{
rosfran@696
   257
	GMythUPnPDevice *gmyth_upnp = g_malloc0( sizeof( GMythUPnPDevice ) );
rosfran@696
   258
rosfran@696
   259
	GMythURI* uri = NULL;
rosfran@696
   260
	gmyth_upnp->uri = (gchar*) (dev);
rosfran@696
   261
	uri = gmyth_uri_new_with_value( gmyth_upnp->uri );
rosfran@696
   262
rosfran@696
   263
	gmyth_upnp->host = gmyth_uri_get_host( uri );
rosfran@696
   264
	gmyth_upnp->port = gmyth_uri_get_port( uri );
rosfran@696
   265
	gmyth_upnp->protocol = gmyth_uri_get_protocol( uri );
rosfran@696
   266
rosfran@706
   267
	gmyth_debug("MythTV UPnP service [ %s, %d ].", gmyth_upnp->host, gmyth_upnp->port);
rosfran@706
   268
rosfran@696
   269
	upnp_servers_list = g_list_append( upnp_servers_list, gmyth_upnp );
rosfran@696
   270
rosfran@696
   271
	if ( uri != NULL )
rosfran@696
   272
	{
rosfran@696
   273
		g_object_unref( uri );
rosfran@696
   274
		uri = NULL;
rosfran@696
   275
	}
rosfran@696
   276
rosfran@696
   277
}
rosfran@696
   278
rosfran@696
   279
GList *
rosfran@696
   280
gmyth_upnp_do_search_sync( GMythUPnP* gmyth_upnp )
rosfran@696
   281
{
rosfran@696
   282
	GList *upnp_servers_list = NULL;
rosfran@696
   283
	guint iter_count = GMYTH_UPNP_MAX_SEARCHS;
rosfran@696
   284
	/* gmyth_upnp->priv = GMYTH_UPNP_GET_PRIVATE( gmyth_upnp ); */
rosfran@696
   285
rosfran@696
   286
	while ( gmyth_upnp->priv->upnp_dev_found == FALSE && ( --iter_count > 0 ) ) {
rosfran@696
   287
melunko@250
   288
		gmyth_debug( "UPnP MythTV Client control point is searching MythTV AV Device server...\n" );
rosfran@696
   289
melunko@250
   290
		if ( gmyth_upnp->control_point != NULL )
melunko@250
   291
			cg_upnp_controlpoint_search ( gmyth_upnp->control_point, 
rosfran@696
   292
					"urn:schemas-upnp-org:service:ContentDirectory:1" );
melunko@250
   293
melunko@250
   294
		/* just to avoid clinkc pthread concurrency faults */
melunko@250
   295
		cg_wait( 1000 );
rosfran@696
   296
melunko@250
   297
		/* discover if it was found */
rosfran@696
   298
		gmyth_upnp->priv->upnp_dev_found = gmyth_upnp_got_mythtv_service( gmyth_upnp->control_point, &gmyth_upnp->priv->udn,
rosfran@696
   299
				&gmyth_upnp->priv->mythtv_servers );
rosfran@696
   300
rosfran@696
   301
	} /* while */
rosfran@696
   302
rosfran@696
   303
	if ( gmyth_upnp->priv->upnp_dev_found ) {
rosfran@696
   304
melunko@250
   305
		gmyth_debug( "Found UPnP MythTV AV Device...\n" );
melunko@250
   306
rosfran@696
   307
		g_hash_table_foreach( gmyth_upnp->priv->mythtv_servers, (GHFunc)_gmyth_foreach_key_value, upnp_servers_list );
melunko@250
   308
rosfran@696
   309
	} /* if - found UPnP device  */
melunko@250
   310
rosfran@696
   311
	return upnp_servers_list;
melunko@250
   312
}
melunko@250
   313
melunko@250
   314
/** 
rosfran@696
   315
 * Checks if got the MythTV service in the Control Point's device list.
melunko@250
   316
 */
melunko@250
   317
static gboolean
rosfran@696
   318
gmyth_upnp_got_mythtv_service( CgUpnpControlPoint* controlPt, gchar **udn, 
rosfran@696
   319
	GHashTable **mythtv_servers_lst )
melunko@250
   320
{
melunko@250
   321
	
melunko@250
   322
	g_return_val_if_fail( mythtv_servers_lst != NULL, FALSE );	
melunko@250
   323
	g_return_val_if_fail( controlPt != NULL, FALSE );
rosfran@696
   324
rosfran@696
   325
	*mythtv_servers_lst = g_hash_table_new( g_str_hash, g_str_equal );
melunko@250
   326
	
rosfran@699
   327
	const gchar* mythtvFriendlyName = "Myth";
melunko@250
   328
	/* begin assertion about the size of discovered devices */
melunko@250
   329
	gint numDevices = cg_upnp_controlpoint_getndevices(controlPt);
melunko@250
   330
	gint cntDevs = 0;	
melunko@250
   331
	CgUpnpDevice *childDev;
melunko@250
   332
	gchar *devName = NULL, *dev_url = NULL;
melunko@250
   333
	gboolean upnp_dev_found = FALSE;
melunko@250
   334
	
melunko@250
   335
	gmyth_debug( "UPnP MythTV AV Device list size = %d\n", numDevices );
melunko@250
   336
	
melunko@250
   337
	for ( childDev = cg_upnp_controlpoint_getdevices(controlPt); childDev != NULL; 
melunko@250
   338
			childDev = cg_upnp_device_next(childDev) ) {
melunko@250
   339
		devName = cg_upnp_device_getfriendlyname(childDev);
melunko@250
   340
		dev_url = cg_upnp_device_getlocationfromssdppacket( childDev );
melunko@250
   341
		gmyth_debug( "Device's friendly name = %s, and  device's URL = %s\n", devName, dev_url );
rosfran@704
   342
		if ( ( g_strstr_len( devName, strlen( devName ), mythtvFriendlyName ) != NULL ) == TRUE ) 
melunko@250
   343
		{
rosfran@704
   344
			upnp_dev_found = TRUE;
rosfran@696
   345
			/* stores the last UDN number ID */
melunko@250
   346
			*udn = cg_upnp_device_getudn( childDev );
rosfran@696
   347
			/* *mythtv_servers_lst = g_list_append( *mythtv_servers_lst, dev_url );  */
rosfran@696
   348
			g_hash_table_insert( *mythtv_servers_lst, (gpointer)*udn, (gpointer)dev_url );
melunko@250
   349
		}
melunko@250
   350
		++cntDevs;
melunko@250
   351
	}
melunko@250
   352
	
melunko@250
   353
	if ( upnp_dev_found == TRUE ) {
melunko@250
   354
		gmyth_debug( "MythTV AV Device found, from a total of %d devices.\n", cntDevs );
melunko@250
   355
	} else if ( numDevices == cntDevs ) {
melunko@250
   356
		gmyth_debug( "MythTV AV Device not found, from a total of %d devices.\n", cntDevs );
melunko@250
   357
	} else {
melunko@250
   358
		gmyth_debug( "Control Point's MythTV AV Device count is wrong: iterated over %d devices, but there are %d registered devices.\n", cntDevs, numDevices );
melunko@250
   359
	}
melunko@250
   360
	
melunko@250
   361
	return upnp_dev_found;
melunko@250
   362
melunko@250
   363
}
melunko@250
   364
melunko@250
   365
/** Gets the UPnP AV devices server's list associated to this upnp.
melunko@250
   366
 * 
rosfran@696
   367
 * @return The GHashTable* containing all the URI values for each recognized UPnP device,
melunko@250
   368
 * 			or NULL if it couldn't recognize any MythTV AV device.
melunko@250
   369
 */
rosfran@696
   370
GHashTable*
melunko@250
   371
gmyth_upnp_get_servers ( GMythUPnP *gmyth_upnp )
melunko@250
   372
{
melunko@250
   373
	
rosfran@696
   374
	if ( NULL == gmyth_upnp || NULL == gmyth_upnp->priv->mythtv_servers ) 
melunko@250
   375
	{
melunko@250
   376
		gmyth_debug ("[%s] GMythUPnP has no MythTV servers recognized.\n", __FUNCTION__);
melunko@250
   377
		return NULL;
melunko@250
   378
	}
melunko@250
   379
rosfran@696
   380
	return gmyth_upnp->priv->mythtv_servers;
melunko@250
   381
}
melunko@250
   382
melunko@250
   383
/** Gets the GMythBackendInfo object associated to this upnp.
melunko@250
   384
 * 
melunko@250
   385
 * @return The GMythBackendInfo object currently valid or NULL if the settings
melunko@250
   386
 * were not opened.
melunko@250
   387
 */
melunko@250
   388
GMythBackendInfo*
melunko@250
   389
gmyth_upnp_get_backend_info ( GMythUPnP *gmyth_upnp )
melunko@250
   390
{
melunko@250
   391
	
rosfran@696
   392
	if ( NULL == gmyth_upnp || NULL == gmyth_upnp->backend_info ) 
melunko@250
   393
	{
melunko@250
   394
		gmyth_debug ("[%s] GMythUPnP not initialized\n", __FUNCTION__);
melunko@250
   395
		return NULL;
melunko@250
   396
	}
melunko@250
   397
rosfran@696
   398
	return gmyth_upnp->backend_info;
melunko@250
   399
}
melunko@250
   400