gmyth-upnp/src/gmyth_upnp.c
author rosfran
Wed Jan 10 23:23:54 2007 +0000 (2007-01-10)
branchtrunk
changeset 260 9bab67afb6c2
parent 250 ac3ca128dbca
child 696 7afeec40ed62
permissions -rw-r--r--
[svn r261] A lot of fixes to the gmyth-0.1 library's external references.
     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 
    36 #include <arpa/inet.h>
    37 #include <sys/types.h>
    38 #include <sys/socket.h>
    39 #include <netdb.h>
    40 #include <errno.h>
    41 #include <stdlib.h>
    42 
    43 #include <gmyth/gmyth_backendinfo.h>
    44 #include <gmyth/gmyth_socket.h>
    45 #include <gmyth/gmyth_uri.h>
    46 #include <gmyth/gmyth_debug.h>
    47 
    48 #define GMYTH_UPNP_MAX_SEARCHS		10
    49 
    50 static void gmyth_upnp_class_init          (GMythUPnPClass *klass);
    51 static void gmyth_upnp_init                (GMythUPnP *object);
    52 
    53 static void gmyth_upnp_dispose  (GObject *object);
    54 static void gmyth_upnp_finalize (GObject *object);
    55 
    56 static gboolean gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info );
    57 
    58 static gboolean gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn, 
    59 						GList **mythtv_servers_lst );
    60 
    61 G_DEFINE_TYPE(GMythUPnP, gmyth_upnp, G_TYPE_OBJECT)
    62 
    63 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    64 
    65 static void
    66 gmyth_upnp_class_init (GMythUPnPClass *klass)
    67 {
    68 	GObjectClass *gobject_class;
    69 
    70 	gobject_class = (GObjectClass *) klass;
    71 
    72 	gobject_class->dispose  = gmyth_upnp_dispose;
    73 	gobject_class->finalize = gmyth_upnp_finalize;	
    74 }
    75 
    76 static void
    77 gmyth_upnp_init ( GMythUPnP *gmyth_upnp )
    78 {
    79 	gmyth_upnp->upnp_dev_found = FALSE;
    80 	
    81 	gmyth_upnp->uri = NULL;
    82 	gmyth_upnp->host = NULL;
    83 	gmyth_upnp->port = 0;
    84 	gmyth_upnp->protocol = NULL;
    85 	
    86 	gmyth_upnp->gmyth_backend_info = NULL;
    87 	
    88 	gmyth_upnp->control_point = NULL;
    89 	
    90 }
    91 
    92 /** Creates a new instance of GMythUPnP.
    93  * 
    94  * @return a new instance of GMythUPnP. 
    95  */
    96 GMythUPnP *
    97 gmyth_upnp_new ( GMythBackendInfo *gmyth_backend_info ) 
    98 {
    99     GMythUPnP *gmyth_upnp = GMYTH_UPNP (g_object_new (GMYTH_UPNP_TYPE, NULL));
   100     
   101     gmyth_upnp->gmyth_backend_info = gmyth_backend_info;
   102     
   103     if ( !gmyth_upnp_initialize ( gmyth_upnp, gmyth_backend_info ) )
   104     	return NULL;
   105     
   106     return gmyth_upnp;
   107 }
   108 
   109 static void
   110 gmyth_upnp_dispose  (GObject *object)
   111 {
   112 	GMythUPnP *gmyth_upnp = GMYTH_UPNP(object);
   113 
   114 	if ( gmyth_upnp->control_point != NULL ) {
   115 		cg_upnp_controlpoint_stop( gmyth_upnp->control_point );
   116 		cg_upnp_controlpoint_delete( gmyth_upnp->control_point );
   117 		gmyth_upnp->control_point = NULL;
   118 	}
   119 
   120 	if ( gmyth_upnp->uri != NULL ) {
   121 		g_free ( gmyth_upnp->uri );
   122 		gmyth_upnp->uri = NULL;
   123 	}
   124 	
   125 	if ( gmyth_upnp->host != NULL ) {
   126 		g_free ( gmyth_upnp->host );
   127 		gmyth_upnp->host = NULL;
   128 	}
   129 	
   130 	if ( gmyth_upnp->protocol != NULL ) {
   131 		g_free ( gmyth_upnp->protocol );
   132 		gmyth_upnp->protocol = NULL;
   133 	}
   134 	
   135 	if ( gmyth_upnp->mythtv_servers != NULL )	{
   136 		g_list_free( gmyth_upnp->mythtv_servers );
   137 		gmyth_upnp->mythtv_servers = NULL;
   138 	}
   139 	
   140 	G_OBJECT_CLASS (gmyth_upnp_parent_class)->dispose (object);
   141 }
   142 
   143 static void
   144 gmyth_upnp_finalize (GObject *object)
   145 {
   146 	g_signal_handlers_destroy (object);
   147 
   148 	G_OBJECT_CLASS (gmyth_upnp_parent_class)->finalize (object);
   149 }
   150 
   151 /**
   152  * Create a control point and start it.
   153  */
   154 static gboolean
   155 gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info )
   156 {
   157 	
   158 	gboolean ret = FALSE;
   159 	
   160 	GMythURI* uri = NULL;
   161 	
   162 	guint iter_count = GMYTH_UPNP_MAX_SEARCHS;
   163 	
   164 	g_return_val_if_fail( gmyth_backend_info != NULL, FALSE );
   165 	
   166 	/* Create the cybergarage control point */
   167 	gmyth_upnp->control_point = cg_upnp_controlpoint_new();
   168 	/* cg_upnp_controlpoint_setdevicelistener( gmyth_upnp->control_point, device_listener ); */
   169 	
   170 	/* Start the control point */
   171   if ( cg_upnp_controlpoint_start( gmyth_upnp->control_point ) == FALSE)
   172 	{
   173 	  gmyth_debug( "Unable to start UPnP control point!!!" );
   174 	  goto done;
   175   }
   176 	else
   177 	{
   178     gmyth_debug( "Control point started." );
   179 	}
   180 	
   181 	while ( gmyth_upnp->upnp_dev_found == FALSE && ( --iter_count > 0 ) ) {
   182 		
   183 		gmyth_debug( "UPnP MythTV Client control point is searching MythTV AV Device server...\n" );
   184        
   185 		if ( gmyth_upnp->control_point != NULL )
   186 			cg_upnp_controlpoint_search ( gmyth_upnp->control_point, 
   187 		        		"urn:schemas-upnp-org:service:ContentDirectory:1" );
   188 
   189 		/* just to avoid clinkc pthread concurrency faults */
   190 		cg_wait( 1000 );
   191 		
   192 		/* discover if it was found */
   193 		gmyth_upnp->upnp_dev_found = gmyth_upnp_print_cp_device_list( gmyth_upnp->control_point, &gmyth_upnp->udn,
   194 									&gmyth_upnp->mythtv_servers );
   195 		
   196 	}
   197 	
   198 	if ( gmyth_upnp->upnp_dev_found ) {
   199 		
   200 		gmyth_debug( "Found UPnP MythTV AV Device...\n" );
   201 		
   202 		if ( g_list_first( gmyth_upnp->mythtv_servers ) != NULL && (g_list_first( gmyth_upnp->mythtv_servers ))->data != NULL )
   203 		{
   204 			gmyth_upnp->uri = (gchar*) (g_list_first( gmyth_upnp->mythtv_servers ))->data;
   205 			uri = gmyth_uri_new_with_value( gmyth_upnp->uri );
   206 			
   207 			gmyth_upnp->host = gmyth_uri_get_host( uri );
   208 			gmyth_upnp->port = gmyth_uri_get_port( uri );
   209 			gmyth_upnp->protocol = gmyth_uri_get_protocol( uri );
   210 			
   211 			/* sets all the discovered data from the UPnP remote device, like host name, IP address, port,... */
   212 			if ( NULL != gmyth_upnp->host )
   213 				gmyth_backend_info_set_hostname ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->host );
   214 			
   215 			if ( gmyth_upnp->port > 0 )
   216 				 gmyth_backend_info_set_port ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->port );
   217 				 
   218 			ret = TRUE;
   219 		}
   220 	}	
   221 
   222 done:
   223 
   224   if ( uri != NULL )
   225   {
   226   	g_object_unref( uri );
   227   	uri = NULL;
   228   }  	
   229 
   230 	return ret;
   231 	
   232 }
   233 
   234 /** 
   235  * Prints the Control Point's device list
   236  */
   237 static gboolean
   238 gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn, GList **mythtv_servers_lst )
   239 {
   240 	
   241 	g_return_val_if_fail( mythtv_servers_lst != NULL, FALSE );	
   242 	g_return_val_if_fail( controlPt != NULL, FALSE );
   243 	
   244 	gchar* mythtvFriendlyName = "Myth";
   245 	/* begin assertion about the size of discovered devices */
   246 	gint numDevices = cg_upnp_controlpoint_getndevices(controlPt);
   247 	gint cntDevs = 0;	
   248 	//CgUpnpDeviceList *devList = NULL;
   249 	CgUpnpDevice *childDev;
   250 	gchar *devName = NULL, *dev_url = NULL;
   251 	gboolean upnp_dev_found = FALSE;
   252 	
   253 	gmyth_debug( "UPnP MythTV AV Device list size = %d\n", numDevices );
   254 	
   255 	for ( childDev = cg_upnp_controlpoint_getdevices(controlPt); childDev != NULL; 
   256 			childDev = cg_upnp_device_next(childDev) ) {
   257 		devName = cg_upnp_device_getfriendlyname(childDev);
   258 		dev_url = cg_upnp_device_getlocationfromssdppacket( childDev );
   259 		gmyth_debug( "Device's friendly name = %s, and  device's URL = %s\n", devName, dev_url );
   260 		if ( ( upnp_dev_found = ( g_strstr_len( devName, strlen( devName ), mythtvFriendlyName ) != NULL ) ) == TRUE ) 
   261 		{
   262 			*udn = cg_upnp_device_getudn( childDev );
   263 			*mythtv_servers_lst = g_list_append( *mythtv_servers_lst, dev_url ); 
   264 		}
   265 		++cntDevs;
   266 	}
   267 	
   268 	if ( upnp_dev_found == TRUE ) {
   269 		gmyth_debug( "MythTV AV Device found, from a total of %d devices.\n", cntDevs );
   270 	} else if ( numDevices == cntDevs ) {
   271 		gmyth_debug( "MythTV AV Device not found, from a total of %d devices.\n", cntDevs );
   272 	} else {
   273 		gmyth_debug( "Control Point's MythTV AV Device count is wrong: iterated over %d devices, but there are %d registered devices.\n", cntDevs, numDevices );
   274 	}
   275 	
   276 	return upnp_dev_found;
   277 
   278 }
   279 
   280 /** Gets the GMythBackendInfo host object associated to this upnp.
   281  * 
   282  * @return The string host object currently valid or NULL if the settings
   283  * were not opened.
   284  */
   285 gchar*
   286 gmyth_upnp_get_host ( GMythUPnP *gmyth_upnp )
   287 {
   288 	
   289 	if ( NULL == gmyth_upnp || NULL == gmyth_upnp->host ) 
   290 	{
   291 		gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__);
   292 		return NULL;
   293 	}
   294 
   295 	return gmyth_upnp->host;
   296 }
   297 
   298 /** Gets the GMythBackendInfo port object associated to this upnp.
   299  * 
   300  * @return The string object currently valid or NULL if the port number.
   301  */
   302 gint
   303 gmyth_upnp_get_port( GMythUPnP *gmyth_upnp )
   304 {
   305 
   306 	if ( NULL == gmyth_upnp || gmyth_upnp->port <= 0 ) 
   307 	{
   308 		gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__);
   309 		return 0;
   310 	}
   311 
   312 	return gmyth_upnp->port;
   313 }
   314 
   315 /** Gets the UPnP AV devices server's list associated to this upnp.
   316  * 
   317  * @return The GList* containing all the URI values for each recognized UPnP device,
   318  * 			or NULL if it couldn't recognize any MythTV AV device.
   319  */
   320 GList*
   321 gmyth_upnp_get_servers ( GMythUPnP *gmyth_upnp )
   322 {
   323 	
   324 	if ( NULL == gmyth_upnp || NULL == gmyth_upnp->mythtv_servers ) 
   325 	{
   326 		gmyth_debug ("[%s] GMythUPnP has no MythTV servers recognized.\n", __FUNCTION__);
   327 		return NULL;
   328 	}
   329 
   330 	return gmyth_upnp->mythtv_servers;
   331 }
   332 
   333 /** Gets the GMythBackendInfo object associated to this upnp.
   334  * 
   335  * @return The GMythBackendInfo object currently valid or NULL if the settings
   336  * were not opened.
   337  */
   338 GMythBackendInfo*
   339 gmyth_upnp_get_backend_info ( GMythUPnP *gmyth_upnp )
   340 {
   341 	
   342 	if ( NULL == gmyth_upnp || NULL == gmyth_upnp->gmyth_backend_info ) 
   343 	{
   344 		gmyth_debug ("[%s] GMythUPnP not initialized\n", __FUNCTION__);
   345 		return NULL;
   346 	}
   347 
   348 	return gmyth_upnp->gmyth_backend_info;
   349 }
   350