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