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