melunko@250: /** melunko@250: * GMyth Library melunko@250: * melunko@250: * @file gmyth/gmyth_upnp.c melunko@250: * melunko@250: * @brief

GMythUPnP allows that a MythTV frontend discovers a melunko@250: * MythTV backend, using the UPnP architecture. melunko@250: * melunko@250: * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. melunko@250: * @author Rosfran Lins Borges melunko@250: * melunko@250: *//* melunko@250: * melunko@250: * This program is free software; you can redistribute it and/or modify melunko@250: * it under the terms of the GNU Lesser General Public License as published by melunko@250: * the Free Software Foundation; either version 2 of the License, or melunko@250: * (at your option) any later version. melunko@250: * melunko@250: * This program is distributed in the hope that it will be useful, melunko@250: * but WITHOUT ANY WARRANTY; without even the implied warranty of melunko@250: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the melunko@250: * GNU General Public License for more details. melunko@250: * melunko@250: * You should have received a copy of the GNU Lesser General Public License melunko@250: * along with this program; if not, write to the Free Software melunko@250: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA melunko@250: * melunko@250: */ melunko@250: melunko@250: #ifdef HAVE_CONFIG_H melunko@250: #include "config.h" melunko@250: #endif melunko@250: melunko@250: #include "gmyth_upnp.h" melunko@250: melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: melunko@250: #include "gmyth_backendinfo.h" melunko@250: #include "gmyth_socket.h" melunko@250: #include "gmyth_uri.h" melunko@250: #include "gmyth_debug.h" melunko@250: melunko@250: #define GMYTH_UPNP_MAX_SEARCHS 10 melunko@250: melunko@250: static void gmyth_upnp_class_init (GMythUPnPClass *klass); melunko@250: static void gmyth_upnp_init (GMythUPnP *object); melunko@250: melunko@250: static void gmyth_upnp_dispose (GObject *object); melunko@250: static void gmyth_upnp_finalize (GObject *object); melunko@250: melunko@250: static gboolean gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info ); melunko@250: melunko@250: static gboolean gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn, melunko@250: GList **mythtv_servers_lst ); melunko@250: melunko@250: G_DEFINE_TYPE(GMythUPnP, gmyth_upnp, G_TYPE_OBJECT) melunko@250: melunko@250: static GStaticMutex mutex = G_STATIC_MUTEX_INIT; melunko@250: melunko@250: static void melunko@250: gmyth_upnp_class_init (GMythUPnPClass *klass) melunko@250: { melunko@250: GObjectClass *gobject_class; melunko@250: melunko@250: gobject_class = (GObjectClass *) klass; melunko@250: melunko@250: gobject_class->dispose = gmyth_upnp_dispose; melunko@250: gobject_class->finalize = gmyth_upnp_finalize; melunko@250: } melunko@250: melunko@250: static void melunko@250: gmyth_upnp_init ( GMythUPnP *gmyth_upnp ) melunko@250: { melunko@250: gmyth_upnp->upnp_dev_found = FALSE; melunko@250: melunko@250: gmyth_upnp->uri = NULL; melunko@250: gmyth_upnp->host = NULL; melunko@250: gmyth_upnp->port = 0; melunko@250: gmyth_upnp->protocol = NULL; melunko@250: melunko@250: gmyth_upnp->gmyth_backend_info = NULL; melunko@250: melunko@250: gmyth_upnp->control_point = NULL; melunko@250: melunko@250: } melunko@250: melunko@250: /** Creates a new instance of GMythUPnP. melunko@250: * melunko@250: * @return a new instance of GMythUPnP. melunko@250: */ melunko@250: GMythUPnP * melunko@250: gmyth_upnp_new ( GMythBackendInfo *gmyth_backend_info ) melunko@250: { melunko@250: GMythUPnP *gmyth_upnp = GMYTH_UPNP (g_object_new (GMYTH_UPNP_TYPE, NULL)); melunko@250: melunko@250: gmyth_upnp->gmyth_backend_info = gmyth_backend_info; melunko@250: melunko@250: if ( !gmyth_upnp_initialize ( gmyth_upnp, gmyth_backend_info ) ) melunko@250: return NULL; melunko@250: melunko@250: return gmyth_upnp; melunko@250: } melunko@250: melunko@250: static void melunko@250: gmyth_upnp_dispose (GObject *object) melunko@250: { melunko@250: GMythUPnP *gmyth_upnp = GMYTH_UPNP(object); melunko@250: melunko@250: if ( gmyth_upnp->control_point != NULL ) { melunko@250: cg_upnp_controlpoint_stop( gmyth_upnp->control_point ); melunko@250: cg_upnp_controlpoint_delete( gmyth_upnp->control_point ); melunko@250: gmyth_upnp->control_point = NULL; melunko@250: } melunko@250: melunko@250: if ( gmyth_upnp->uri != NULL ) { melunko@250: g_free ( gmyth_upnp->uri ); melunko@250: gmyth_upnp->uri = NULL; melunko@250: } melunko@250: melunko@250: if ( gmyth_upnp->host != NULL ) { melunko@250: g_free ( gmyth_upnp->host ); melunko@250: gmyth_upnp->host = NULL; melunko@250: } melunko@250: melunko@250: if ( gmyth_upnp->protocol != NULL ) { melunko@250: g_free ( gmyth_upnp->protocol ); melunko@250: gmyth_upnp->protocol = NULL; melunko@250: } melunko@250: melunko@250: if ( gmyth_upnp->mythtv_servers != NULL ) { melunko@250: g_list_free( gmyth_upnp->mythtv_servers ); melunko@250: gmyth_upnp->mythtv_servers = NULL; melunko@250: } melunko@250: melunko@250: G_OBJECT_CLASS (gmyth_upnp_parent_class)->dispose (object); melunko@250: } melunko@250: melunko@250: static void melunko@250: gmyth_upnp_finalize (GObject *object) melunko@250: { melunko@250: g_signal_handlers_destroy (object); melunko@250: melunko@250: G_OBJECT_CLASS (gmyth_upnp_parent_class)->finalize (object); melunko@250: } melunko@250: melunko@250: /** melunko@250: * Create a control point and start it. melunko@250: */ melunko@250: static gboolean melunko@250: gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info ) melunko@250: { melunko@250: melunko@250: gboolean ret = FALSE; melunko@250: melunko@250: GMythURI* uri = NULL; melunko@250: melunko@250: guint iter_count = GMYTH_UPNP_MAX_SEARCHS; melunko@250: melunko@250: g_return_val_if_fail( gmyth_backend_info != NULL, FALSE ); melunko@250: melunko@250: /* Create the cybergarage control point */ melunko@250: gmyth_upnp->control_point = cg_upnp_controlpoint_new(); melunko@250: /* cg_upnp_controlpoint_setdevicelistener( gmyth_upnp->control_point, device_listener ); */ melunko@250: melunko@250: /* Start the control point */ melunko@250: if ( cg_upnp_controlpoint_start( gmyth_upnp->control_point ) == FALSE) melunko@250: { melunko@250: gmyth_debug( "Unable to start UPnP control point!!!" ); melunko@250: goto done; melunko@250: } melunko@250: else melunko@250: { melunko@250: gmyth_debug( "Control point started." ); melunko@250: } melunko@250: melunko@250: while ( gmyth_upnp->upnp_dev_found == FALSE && ( --iter_count > 0 ) ) { melunko@250: melunko@250: gmyth_debug( "UPnP MythTV Client control point is searching MythTV AV Device server...\n" ); melunko@250: melunko@250: if ( gmyth_upnp->control_point != NULL ) melunko@250: cg_upnp_controlpoint_search ( gmyth_upnp->control_point, melunko@250: "urn:schemas-upnp-org:service:ContentDirectory:1" ); melunko@250: melunko@250: /* just to avoid clinkc pthread concurrency faults */ melunko@250: cg_wait( 1000 ); melunko@250: melunko@250: /* discover if it was found */ melunko@250: gmyth_upnp->upnp_dev_found = gmyth_upnp_print_cp_device_list( gmyth_upnp->control_point, &gmyth_upnp->udn, melunko@250: &gmyth_upnp->mythtv_servers ); melunko@250: melunko@250: } melunko@250: melunko@250: if ( gmyth_upnp->upnp_dev_found ) { melunko@250: melunko@250: gmyth_debug( "Found UPnP MythTV AV Device...\n" ); melunko@250: melunko@250: if ( g_list_first( gmyth_upnp->mythtv_servers ) != NULL && (g_list_first( gmyth_upnp->mythtv_servers ))->data != NULL ) melunko@250: { melunko@250: gmyth_upnp->uri = (gchar*) (g_list_first( gmyth_upnp->mythtv_servers ))->data; melunko@250: uri = gmyth_uri_new_with_value( gmyth_upnp->uri ); melunko@250: melunko@250: gmyth_upnp->host = gmyth_uri_get_host( uri ); melunko@250: gmyth_upnp->port = gmyth_uri_get_port( uri ); melunko@250: gmyth_upnp->protocol = gmyth_uri_get_protocol( uri ); melunko@250: melunko@250: /* sets all the discovered data from the UPnP remote device, like host name, IP address, port,... */ melunko@250: if ( NULL != gmyth_upnp->host ) melunko@250: gmyth_backend_info_set_hostname ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->host ); melunko@250: melunko@250: if ( gmyth_upnp->port > 0 ) melunko@250: gmyth_backend_info_set_port ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->port ); melunko@250: melunko@250: ret = TRUE; melunko@250: } melunko@250: } melunko@250: melunko@250: done: melunko@250: melunko@250: if ( uri != NULL ) melunko@250: { melunko@250: g_object_unref( uri ); melunko@250: uri = NULL; melunko@250: } melunko@250: melunko@250: return ret; melunko@250: melunko@250: } melunko@250: melunko@250: /** melunko@250: * Prints the Control Point's device list melunko@250: */ melunko@250: static gboolean melunko@250: gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn, GList **mythtv_servers_lst ) melunko@250: { melunko@250: melunko@250: g_return_val_if_fail( mythtv_servers_lst != NULL, FALSE ); melunko@250: g_return_val_if_fail( controlPt != NULL, FALSE ); melunko@250: melunko@250: gchar* mythtvFriendlyName = "Myth"; melunko@250: /* begin assertion about the size of discovered devices */ melunko@250: gint numDevices = cg_upnp_controlpoint_getndevices(controlPt); melunko@250: gint cntDevs = 0; melunko@250: //CgUpnpDeviceList *devList = NULL; melunko@250: CgUpnpDevice *childDev; melunko@250: gchar *devName = NULL, *dev_url = NULL; melunko@250: gboolean upnp_dev_found = FALSE; melunko@250: melunko@250: gmyth_debug( "UPnP MythTV AV Device list size = %d\n", numDevices ); melunko@250: melunko@250: for ( childDev = cg_upnp_controlpoint_getdevices(controlPt); childDev != NULL; melunko@250: childDev = cg_upnp_device_next(childDev) ) { melunko@250: devName = cg_upnp_device_getfriendlyname(childDev); melunko@250: dev_url = cg_upnp_device_getlocationfromssdppacket( childDev ); melunko@250: gmyth_debug( "Device's friendly name = %s, and device's URL = %s\n", devName, dev_url ); melunko@250: if ( ( upnp_dev_found = ( g_strstr_len( devName, strlen( devName ), mythtvFriendlyName ) != NULL ) ) == TRUE ) melunko@250: { melunko@250: *udn = cg_upnp_device_getudn( childDev ); melunko@250: *mythtv_servers_lst = g_list_append( *mythtv_servers_lst, dev_url ); melunko@250: } melunko@250: ++cntDevs; melunko@250: } melunko@250: melunko@250: if ( upnp_dev_found == TRUE ) { melunko@250: gmyth_debug( "MythTV AV Device found, from a total of %d devices.\n", cntDevs ); melunko@250: } else if ( numDevices == cntDevs ) { melunko@250: gmyth_debug( "MythTV AV Device not found, from a total of %d devices.\n", cntDevs ); melunko@250: } else { melunko@250: 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: } melunko@250: melunko@250: return upnp_dev_found; melunko@250: melunko@250: } melunko@250: melunko@250: /** Gets the GMythBackendInfo host object associated to this upnp. melunko@250: * melunko@250: * @return The string host object currently valid or NULL if the settings melunko@250: * were not opened. melunko@250: */ melunko@250: gchar* melunko@250: gmyth_upnp_get_host ( GMythUPnP *gmyth_upnp ) melunko@250: { melunko@250: melunko@250: if ( NULL == gmyth_upnp || NULL == gmyth_upnp->host ) melunko@250: { melunko@250: gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__); melunko@250: return NULL; melunko@250: } melunko@250: melunko@250: return gmyth_upnp->host; melunko@250: } melunko@250: melunko@250: /** Gets the GMythBackendInfo port object associated to this upnp. melunko@250: * melunko@250: * @return The string object currently valid or NULL if the port number. melunko@250: */ melunko@250: gint melunko@250: gmyth_upnp_get_port( GMythUPnP *gmyth_upnp ) melunko@250: { melunko@250: melunko@250: if ( NULL == gmyth_upnp || gmyth_upnp->port <= 0 ) melunko@250: { melunko@250: gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__); melunko@250: return 0; melunko@250: } melunko@250: melunko@250: return gmyth_upnp->port; melunko@250: } melunko@250: melunko@250: /** Gets the UPnP AV devices server's list associated to this upnp. melunko@250: * melunko@250: * @return The GList* containing all the URI values for each recognized UPnP device, melunko@250: * or NULL if it couldn't recognize any MythTV AV device. melunko@250: */ melunko@250: GList* melunko@250: gmyth_upnp_get_servers ( GMythUPnP *gmyth_upnp ) melunko@250: { melunko@250: melunko@250: if ( NULL == gmyth_upnp || NULL == gmyth_upnp->mythtv_servers ) melunko@250: { melunko@250: gmyth_debug ("[%s] GMythUPnP has no MythTV servers recognized.\n", __FUNCTION__); melunko@250: return NULL; melunko@250: } melunko@250: melunko@250: return gmyth_upnp->mythtv_servers; melunko@250: } melunko@250: melunko@250: /** Gets the GMythBackendInfo object associated to this upnp. melunko@250: * melunko@250: * @return The GMythBackendInfo object currently valid or NULL if the settings melunko@250: * were not opened. melunko@250: */ melunko@250: GMythBackendInfo* melunko@250: gmyth_upnp_get_backend_info ( GMythUPnP *gmyth_upnp ) melunko@250: { melunko@250: melunko@250: if ( NULL == gmyth_upnp || NULL == gmyth_upnp->gmyth_backend_info ) melunko@250: { melunko@250: gmyth_debug ("[%s] GMythUPnP not initialized\n", __FUNCTION__); melunko@250: return NULL; melunko@250: } melunko@250: melunko@250: return gmyth_upnp->gmyth_backend_info; melunko@250: } melunko@250: