1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-upnp/src/gmyth_upnp.c Wed Jan 10 17:19:07 2007 +0000
1.3 @@ -0,0 +1,350 @@
1.4 +/**
1.5 + * GMyth Library
1.6 + *
1.7 + * @file gmyth/gmyth_upnp.c
1.8 + *
1.9 + * @brief <p> GMythUPnP allows that a MythTV frontend discovers a
1.10 + * MythTV backend, using the UPnP architecture.
1.11 + *
1.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
1.13 + * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
1.14 + *
1.15 + *//*
1.16 + *
1.17 + * This program is free software; you can redistribute it and/or modify
1.18 + * it under the terms of the GNU Lesser General Public License as published by
1.19 + * the Free Software Foundation; either version 2 of the License, or
1.20 + * (at your option) any later version.
1.21 + *
1.22 + * This program is distributed in the hope that it will be useful,
1.23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.25 + * GNU General Public License for more details.
1.26 + *
1.27 + * You should have received a copy of the GNU Lesser General Public License
1.28 + * along with this program; if not, write to the Free Software
1.29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.30 + *
1.31 + */
1.32 +
1.33 +#ifdef HAVE_CONFIG_H
1.34 +#include "config.h"
1.35 +#endif
1.36 +
1.37 +#include "gmyth_upnp.h"
1.38 +
1.39 +#include <arpa/inet.h>
1.40 +#include <sys/types.h>
1.41 +#include <sys/socket.h>
1.42 +#include <netdb.h>
1.43 +#include <errno.h>
1.44 +#include <stdlib.h>
1.45 +
1.46 +#include "gmyth_backendinfo.h"
1.47 +#include "gmyth_socket.h"
1.48 +#include "gmyth_uri.h"
1.49 +#include "gmyth_debug.h"
1.50 +
1.51 +#define GMYTH_UPNP_MAX_SEARCHS 10
1.52 +
1.53 +static void gmyth_upnp_class_init (GMythUPnPClass *klass);
1.54 +static void gmyth_upnp_init (GMythUPnP *object);
1.55 +
1.56 +static void gmyth_upnp_dispose (GObject *object);
1.57 +static void gmyth_upnp_finalize (GObject *object);
1.58 +
1.59 +static gboolean gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info );
1.60 +
1.61 +static gboolean gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn,
1.62 + GList **mythtv_servers_lst );
1.63 +
1.64 +G_DEFINE_TYPE(GMythUPnP, gmyth_upnp, G_TYPE_OBJECT)
1.65 +
1.66 +static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
1.67 +
1.68 +static void
1.69 +gmyth_upnp_class_init (GMythUPnPClass *klass)
1.70 +{
1.71 + GObjectClass *gobject_class;
1.72 +
1.73 + gobject_class = (GObjectClass *) klass;
1.74 +
1.75 + gobject_class->dispose = gmyth_upnp_dispose;
1.76 + gobject_class->finalize = gmyth_upnp_finalize;
1.77 +}
1.78 +
1.79 +static void
1.80 +gmyth_upnp_init ( GMythUPnP *gmyth_upnp )
1.81 +{
1.82 + gmyth_upnp->upnp_dev_found = FALSE;
1.83 +
1.84 + gmyth_upnp->uri = NULL;
1.85 + gmyth_upnp->host = NULL;
1.86 + gmyth_upnp->port = 0;
1.87 + gmyth_upnp->protocol = NULL;
1.88 +
1.89 + gmyth_upnp->gmyth_backend_info = NULL;
1.90 +
1.91 + gmyth_upnp->control_point = NULL;
1.92 +
1.93 +}
1.94 +
1.95 +/** Creates a new instance of GMythUPnP.
1.96 + *
1.97 + * @return a new instance of GMythUPnP.
1.98 + */
1.99 +GMythUPnP *
1.100 +gmyth_upnp_new ( GMythBackendInfo *gmyth_backend_info )
1.101 +{
1.102 + GMythUPnP *gmyth_upnp = GMYTH_UPNP (g_object_new (GMYTH_UPNP_TYPE, NULL));
1.103 +
1.104 + gmyth_upnp->gmyth_backend_info = gmyth_backend_info;
1.105 +
1.106 + if ( !gmyth_upnp_initialize ( gmyth_upnp, gmyth_backend_info ) )
1.107 + return NULL;
1.108 +
1.109 + return gmyth_upnp;
1.110 +}
1.111 +
1.112 +static void
1.113 +gmyth_upnp_dispose (GObject *object)
1.114 +{
1.115 + GMythUPnP *gmyth_upnp = GMYTH_UPNP(object);
1.116 +
1.117 + if ( gmyth_upnp->control_point != NULL ) {
1.118 + cg_upnp_controlpoint_stop( gmyth_upnp->control_point );
1.119 + cg_upnp_controlpoint_delete( gmyth_upnp->control_point );
1.120 + gmyth_upnp->control_point = NULL;
1.121 + }
1.122 +
1.123 + if ( gmyth_upnp->uri != NULL ) {
1.124 + g_free ( gmyth_upnp->uri );
1.125 + gmyth_upnp->uri = NULL;
1.126 + }
1.127 +
1.128 + if ( gmyth_upnp->host != NULL ) {
1.129 + g_free ( gmyth_upnp->host );
1.130 + gmyth_upnp->host = NULL;
1.131 + }
1.132 +
1.133 + if ( gmyth_upnp->protocol != NULL ) {
1.134 + g_free ( gmyth_upnp->protocol );
1.135 + gmyth_upnp->protocol = NULL;
1.136 + }
1.137 +
1.138 + if ( gmyth_upnp->mythtv_servers != NULL ) {
1.139 + g_list_free( gmyth_upnp->mythtv_servers );
1.140 + gmyth_upnp->mythtv_servers = NULL;
1.141 + }
1.142 +
1.143 + G_OBJECT_CLASS (gmyth_upnp_parent_class)->dispose (object);
1.144 +}
1.145 +
1.146 +static void
1.147 +gmyth_upnp_finalize (GObject *object)
1.148 +{
1.149 + g_signal_handlers_destroy (object);
1.150 +
1.151 + G_OBJECT_CLASS (gmyth_upnp_parent_class)->finalize (object);
1.152 +}
1.153 +
1.154 +/**
1.155 + * Create a control point and start it.
1.156 + */
1.157 +static gboolean
1.158 +gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info )
1.159 +{
1.160 +
1.161 + gboolean ret = FALSE;
1.162 +
1.163 + GMythURI* uri = NULL;
1.164 +
1.165 + guint iter_count = GMYTH_UPNP_MAX_SEARCHS;
1.166 +
1.167 + g_return_val_if_fail( gmyth_backend_info != NULL, FALSE );
1.168 +
1.169 + /* Create the cybergarage control point */
1.170 + gmyth_upnp->control_point = cg_upnp_controlpoint_new();
1.171 + /* cg_upnp_controlpoint_setdevicelistener( gmyth_upnp->control_point, device_listener ); */
1.172 +
1.173 + /* Start the control point */
1.174 + if ( cg_upnp_controlpoint_start( gmyth_upnp->control_point ) == FALSE)
1.175 + {
1.176 + gmyth_debug( "Unable to start UPnP control point!!!" );
1.177 + goto done;
1.178 + }
1.179 + else
1.180 + {
1.181 + gmyth_debug( "Control point started." );
1.182 + }
1.183 +
1.184 + while ( gmyth_upnp->upnp_dev_found == FALSE && ( --iter_count > 0 ) ) {
1.185 +
1.186 + gmyth_debug( "UPnP MythTV Client control point is searching MythTV AV Device server...\n" );
1.187 +
1.188 + if ( gmyth_upnp->control_point != NULL )
1.189 + cg_upnp_controlpoint_search ( gmyth_upnp->control_point,
1.190 + "urn:schemas-upnp-org:service:ContentDirectory:1" );
1.191 +
1.192 + /* just to avoid clinkc pthread concurrency faults */
1.193 + cg_wait( 1000 );
1.194 +
1.195 + /* discover if it was found */
1.196 + gmyth_upnp->upnp_dev_found = gmyth_upnp_print_cp_device_list( gmyth_upnp->control_point, &gmyth_upnp->udn,
1.197 + &gmyth_upnp->mythtv_servers );
1.198 +
1.199 + }
1.200 +
1.201 + if ( gmyth_upnp->upnp_dev_found ) {
1.202 +
1.203 + gmyth_debug( "Found UPnP MythTV AV Device...\n" );
1.204 +
1.205 + if ( g_list_first( gmyth_upnp->mythtv_servers ) != NULL && (g_list_first( gmyth_upnp->mythtv_servers ))->data != NULL )
1.206 + {
1.207 + gmyth_upnp->uri = (gchar*) (g_list_first( gmyth_upnp->mythtv_servers ))->data;
1.208 + uri = gmyth_uri_new_with_value( gmyth_upnp->uri );
1.209 +
1.210 + gmyth_upnp->host = gmyth_uri_get_host( uri );
1.211 + gmyth_upnp->port = gmyth_uri_get_port( uri );
1.212 + gmyth_upnp->protocol = gmyth_uri_get_protocol( uri );
1.213 +
1.214 + /* sets all the discovered data from the UPnP remote device, like host name, IP address, port,... */
1.215 + if ( NULL != gmyth_upnp->host )
1.216 + gmyth_backend_info_set_hostname ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->host );
1.217 +
1.218 + if ( gmyth_upnp->port > 0 )
1.219 + gmyth_backend_info_set_port ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->port );
1.220 +
1.221 + ret = TRUE;
1.222 + }
1.223 + }
1.224 +
1.225 +done:
1.226 +
1.227 + if ( uri != NULL )
1.228 + {
1.229 + g_object_unref( uri );
1.230 + uri = NULL;
1.231 + }
1.232 +
1.233 + return ret;
1.234 +
1.235 +}
1.236 +
1.237 +/**
1.238 + * Prints the Control Point's device list
1.239 + */
1.240 +static gboolean
1.241 +gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn, GList **mythtv_servers_lst )
1.242 +{
1.243 +
1.244 + g_return_val_if_fail( mythtv_servers_lst != NULL, FALSE );
1.245 + g_return_val_if_fail( controlPt != NULL, FALSE );
1.246 +
1.247 + gchar* mythtvFriendlyName = "Myth";
1.248 + /* begin assertion about the size of discovered devices */
1.249 + gint numDevices = cg_upnp_controlpoint_getndevices(controlPt);
1.250 + gint cntDevs = 0;
1.251 + //CgUpnpDeviceList *devList = NULL;
1.252 + CgUpnpDevice *childDev;
1.253 + gchar *devName = NULL, *dev_url = NULL;
1.254 + gboolean upnp_dev_found = FALSE;
1.255 +
1.256 + gmyth_debug( "UPnP MythTV AV Device list size = %d\n", numDevices );
1.257 +
1.258 + for ( childDev = cg_upnp_controlpoint_getdevices(controlPt); childDev != NULL;
1.259 + childDev = cg_upnp_device_next(childDev) ) {
1.260 + devName = cg_upnp_device_getfriendlyname(childDev);
1.261 + dev_url = cg_upnp_device_getlocationfromssdppacket( childDev );
1.262 + gmyth_debug( "Device's friendly name = %s, and device's URL = %s\n", devName, dev_url );
1.263 + if ( ( upnp_dev_found = ( g_strstr_len( devName, strlen( devName ), mythtvFriendlyName ) != NULL ) ) == TRUE )
1.264 + {
1.265 + *udn = cg_upnp_device_getudn( childDev );
1.266 + *mythtv_servers_lst = g_list_append( *mythtv_servers_lst, dev_url );
1.267 + }
1.268 + ++cntDevs;
1.269 + }
1.270 +
1.271 + if ( upnp_dev_found == TRUE ) {
1.272 + gmyth_debug( "MythTV AV Device found, from a total of %d devices.\n", cntDevs );
1.273 + } else if ( numDevices == cntDevs ) {
1.274 + gmyth_debug( "MythTV AV Device not found, from a total of %d devices.\n", cntDevs );
1.275 + } else {
1.276 + gmyth_debug( "Control Point's MythTV AV Device count is wrong: iterated over %d devices, but there are %d registered devices.\n", cntDevs, numDevices );
1.277 + }
1.278 +
1.279 + return upnp_dev_found;
1.280 +
1.281 +}
1.282 +
1.283 +/** Gets the GMythBackendInfo host object associated to this upnp.
1.284 + *
1.285 + * @return The string host object currently valid or NULL if the settings
1.286 + * were not opened.
1.287 + */
1.288 +gchar*
1.289 +gmyth_upnp_get_host ( GMythUPnP *gmyth_upnp )
1.290 +{
1.291 +
1.292 + if ( NULL == gmyth_upnp || NULL == gmyth_upnp->host )
1.293 + {
1.294 + gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__);
1.295 + return NULL;
1.296 + }
1.297 +
1.298 + return gmyth_upnp->host;
1.299 +}
1.300 +
1.301 +/** Gets the GMythBackendInfo port object associated to this upnp.
1.302 + *
1.303 + * @return The string object currently valid or NULL if the port number.
1.304 + */
1.305 +gint
1.306 +gmyth_upnp_get_port( GMythUPnP *gmyth_upnp )
1.307 +{
1.308 +
1.309 + if ( NULL == gmyth_upnp || gmyth_upnp->port <= 0 )
1.310 + {
1.311 + gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__);
1.312 + return 0;
1.313 + }
1.314 +
1.315 + return gmyth_upnp->port;
1.316 +}
1.317 +
1.318 +/** Gets the UPnP AV devices server's list associated to this upnp.
1.319 + *
1.320 + * @return The GList* containing all the URI values for each recognized UPnP device,
1.321 + * or NULL if it couldn't recognize any MythTV AV device.
1.322 + */
1.323 +GList*
1.324 +gmyth_upnp_get_servers ( GMythUPnP *gmyth_upnp )
1.325 +{
1.326 +
1.327 + if ( NULL == gmyth_upnp || NULL == gmyth_upnp->mythtv_servers )
1.328 + {
1.329 + gmyth_debug ("[%s] GMythUPnP has no MythTV servers recognized.\n", __FUNCTION__);
1.330 + return NULL;
1.331 + }
1.332 +
1.333 + return gmyth_upnp->mythtv_servers;
1.334 +}
1.335 +
1.336 +/** Gets the GMythBackendInfo object associated to this upnp.
1.337 + *
1.338 + * @return The GMythBackendInfo object currently valid or NULL if the settings
1.339 + * were not opened.
1.340 + */
1.341 +GMythBackendInfo*
1.342 +gmyth_upnp_get_backend_info ( GMythUPnP *gmyth_upnp )
1.343 +{
1.344 +
1.345 + if ( NULL == gmyth_upnp || NULL == gmyth_upnp->gmyth_backend_info )
1.346 + {
1.347 + gmyth_debug ("[%s] GMythUPnP not initialized\n", __FUNCTION__);
1.348 + return NULL;
1.349 + }
1.350 +
1.351 + return gmyth_upnp->gmyth_backend_info;
1.352 +}
1.353 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/gmyth-upnp/src/gmyth_upnp.h Wed Jan 10 17:19:07 2007 +0000
2.3 @@ -0,0 +1,96 @@
2.4 +/**
2.5 + * GMyth Library
2.6 + *
2.7 + * @file gmyth/gmyth_upnp.h
2.8 + *
2.9 + * @brief <p> GMythUPnP allows that a MythTV frontend discovers a
2.10 + * MythTV backend, using the UPnP architecture.
2.11 + *
2.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
2.13 + * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
2.14 + *
2.15 + *//*
2.16 + *
2.17 + * This program is free software; you can redistribute it and/or modify
2.18 + * it under the terms of the GNU Lesser General Public License as published by
2.19 + * the Free Software Foundation; either version 2 of the License, or
2.20 + * (at your option) any later version.
2.21 + *
2.22 + * This program is distributed in the hope that it will be useful,
2.23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.25 + * GNU General Public License for more details.
2.26 + *
2.27 + * You should have received a copy of the GNU Lesser General Public License
2.28 + * along with this program; if not, write to the Free Software
2.29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2.30 + *
2.31 + */
2.32 +
2.33 +#ifndef _GMYTH_UPNP_H_
2.34 +#define _GMYTH_UPNP_H_
2.35 +
2.36 +#include <glib.h>
2.37 +#include <glib-object.h>
2.38 +
2.39 +#include <stdlib.h>
2.40 +#include <stdio.h>
2.41 +#include <string.h>
2.42 +
2.43 +#include <cybergarage/upnp/cupnp.h>
2.44 +#include "gmyth_backendinfo.h"
2.45 +
2.46 +G_BEGIN_DECLS
2.47 +
2.48 +#define GMYTH_UPNP_TYPE (gmyth_upnp_get_type ())
2.49 +#define GMYTH_UPNP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_UPNP_TYPE, GMythUPnP))
2.50 +#define GMYTH_UPNP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_UPNP_TYPE, GMythUPnPClass))
2.51 +#define IS_GMYTH_UPNP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_UPNP_TYPE))
2.52 +#define IS_GMYTH_UPNP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_UPNP_TYPE))
2.53 +#define GMYTH_UPNP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GMYTH_UPNP_TYPE, GMythUPnPClass))
2.54 +
2.55 +typedef struct _GMythUPnP GMythUPnP;
2.56 +typedef struct _GMythUPnPClass GMythUPnPClass;
2.57 +
2.58 +/****************************************
2.59 +* Data Type
2.60 +****************************************/
2.61 +
2.62 +struct _GMythUPnPClass
2.63 +{
2.64 + GObjectClass parent_class;
2.65 +
2.66 + /* callbacks */
2.67 + /* no one for now */
2.68 +};
2.69 +
2.70 +struct _GMythUPnP {
2.71 +
2.72 + GObject parent;
2.73 +
2.74 + gchar *uri;
2.75 + gchar *host;
2.76 + gint port;
2.77 + gchar *protocol;
2.78 +
2.79 + CgUpnpControlPoint* control_point;
2.80 +
2.81 + gchar *udn;
2.82 +
2.83 + GMythBackendInfo *gmyth_backend_info;
2.84 +
2.85 + GList* mythtv_servers;
2.86 +
2.87 + gboolean upnp_dev_found;
2.88 +
2.89 +};
2.90 +
2.91 +GType gmyth_upnp_get_type (void);
2.92 +GMythUPnP * gmyth_upnp_new ( GMythBackendInfo *gmyth_backend_info );
2.93 +gchar* gmyth_upnp_get_host ( GMythUPnP *gmyth_upnp );
2.94 +gint gmyth_upnp_get_port ( GMythUPnP *gmyth_upnp );
2.95 +GMythBackendInfo* gmyth_upnp_get_backend_info ( GMythUPnP *gmyth_upnp );
2.96 +
2.97 +G_END_DECLS
2.98 +
2.99 +#endif /* _GMYTH_UPNP_H_ */