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: * 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" rosfran@696: #include "gmyth_upnp_marshal.h" melunko@250: melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: #include melunko@250: rosfran@260: #include rosfran@260: #include rosfran@260: #include rosfran@260: #include melunko@250: renatofilho@754: /* renatofilho@754: * Maximum number of searches in the synchronized search renatofilho@754: */ melunko@250: #define GMYTH_UPNP_MAX_SEARCHS 10 melunko@250: rosfran@696: #define GMYTH_UPNP_GET_PRIVATE(obj) \ rosfran@696: (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_UPNP_TYPE, GMythUPnPPrivate)) rosfran@696: rosfran@696: struct _GMythUPnPPrivate { renatofilho@754: GHashTable *mythtv_servers; renatofilho@754: GMythUPnPDeviceStatus last_status; renatofilho@754: gboolean upnp_dev_found; renatofilho@754: gchar *udn; renatofilho@754: GMutex *mutex; rosfran@696: }; rosfran@696: renatofilho@754: static void gmyth_upnp_class_init(GMythUPnPClass * klass); renatofilho@754: static void gmyth_upnp_init(GMythUPnP * object); melunko@250: renatofilho@754: static void gmyth_upnp_dispose(GObject * object); renatofilho@754: static void gmyth_upnp_finalize(GObject * object); melunko@250: renatofilho@754: static void _mythtv_device_found(GMythUPnP * gmyth_upnp, renatofilho@754: GMythUPnPDeviceStatus status, renatofilho@754: gchar * dev); renatofilho@754: static void _clinkc_mythtv_device_found(gchar * udn, renatofilho@754: GMythUPnPDeviceStatus status); melunko@250: renatofilho@754: static gboolean gmyth_upnp_initialize(GMythUPnP * gmyth_upnp, renatofilho@754: GMythBackendInfo * renatofilho@754: gmyth_backend_info, renatofilho@754: GMythUPnPDeviceListener listener); rosfran@696: renatofilho@754: static gboolean gmyth_upnp_got_mythtv_service(CgUpnpControlPoint * renatofilho@754: controlPt, gchar ** udn, renatofilho@754: GHashTable ** renatofilho@754: mythtv_servers_lst); melunko@250: melunko@250: G_DEFINE_TYPE(GMythUPnP, gmyth_upnp, G_TYPE_OBJECT) melunko@250: renatofilho@754: static GMythUPnP *gmyth_upnp_static = NULL; melunko@250: renatofilho@754: static void renatofilho@754: gmyth_upnp_class_init(GMythUPnPClass * klass) melunko@250: { renatofilho@754: GObjectClass *gobject_class; renatofilho@754: GMythUPnPClass *gupnp_class; melunko@250: renatofilho@754: gobject_class = (GObjectClass *) klass; renatofilho@754: gupnp_class = (GMythUPnPClass *) gobject_class; melunko@250: renatofilho@754: gobject_class->dispose = gmyth_upnp_dispose; renatofilho@754: gobject_class->finalize = gmyth_upnp_finalize; rosfran@696: renatofilho@754: g_type_class_add_private(gobject_class, sizeof(GMythUPnPPrivate)); rosfran@696: renatofilho@754: gupnp_class->device_found_handler = _mythtv_device_found; renatofilho@754: renatofilho@754: gupnp_class->device_found_handler_signal_id = renatofilho@754: g_signal_new("device-found", renatofilho@754: G_TYPE_FROM_CLASS(gupnp_class), renatofilho@754: G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | renatofilho@754: G_SIGNAL_NO_HOOKS, 0, NULL, NULL, renatofilho@754: gmyth_upnp_marshal_VOID__INT_STRING, G_TYPE_NONE, 2, renatofilho@754: G_TYPE_INT, G_TYPE_STRING); rosfran@696: melunko@250: } melunko@250: melunko@250: static void renatofilho@754: gmyth_upnp_init(GMythUPnP * gmyth_upnp) melunko@250: { rosfran@696: renatofilho@754: gmyth_upnp->backend_info = NULL; rosfran@696: renatofilho@754: gmyth_upnp->control_point = NULL; rosfran@696: renatofilho@754: gmyth_upnp->priv = GMYTH_UPNP_GET_PRIVATE(gmyth_upnp); renatofilho@754: gmyth_upnp->priv->mutex = g_mutex_new(); renatofilho@754: gmyth_upnp->priv->upnp_dev_found = FALSE; rosfran@696: renatofilho@754: g_signal_connect(G_OBJECT(gmyth_upnp), "device-found", renatofilho@754: (GCallback) (GMYTH_UPNP_GET_CLASS(gmyth_upnp)-> renatofilho@754: device_found_handler), NULL); rosfran@696: renatofilho@754: gmyth_upnp_static = gmyth_upnp; rosfran@696: melunko@250: } melunko@250: melunko@250: /** Creates a new instance of GMythUPnP. melunko@250: * melunko@250: * @return a new instance of GMythUPnP. melunko@250: */ renatofilho@754: GMythUPnP * renatofilho@754: gmyth_upnp_new(GMythBackendInfo * gmyth_backend_info, renatofilho@754: GMythUPnPDeviceListener handler) melunko@250: { renatofilho@754: GMythUPnP *gmyth_upnp = renatofilho@754: GMYTH_UPNP(g_object_new(GMYTH_UPNP_TYPE, NULL)); renatofilho@754: renatofilho@754: g_object_ref(gmyth_backend_info); rosfran@696: rosfran@696: gmyth_upnp->backend_info = gmyth_backend_info; rosfran@696: renatofilho@754: if (!gmyth_upnp_initialize(gmyth_upnp, gmyth_backend_info, handler)) { renatofilho@754: gmyth_debug("Error initializing the GMythUPnP!!!"); rosfran@696: } renatofilho@754: melunko@250: return gmyth_upnp; melunko@250: } melunko@250: melunko@250: static void renatofilho@754: gmyth_upnp_dispose(GObject * object) melunko@250: { renatofilho@754: GMythUPnP *gmyth_upnp = GMYTH_UPNP(object); melunko@250: renatofilho@754: if (gmyth_upnp->control_point != NULL) { renatofilho@754: cg_upnp_controlpoint_stop(gmyth_upnp->control_point); renatofilho@754: cg_upnp_controlpoint_delete(gmyth_upnp->control_point); renatofilho@754: gmyth_upnp->control_point = NULL; renatofilho@754: } melunko@250: renatofilho@754: if (gmyth_upnp->priv->mythtv_servers != NULL) { renatofilho@754: g_hash_table_destroy(gmyth_upnp->priv->mythtv_servers); renatofilho@754: gmyth_upnp->priv->mythtv_servers = NULL; renatofilho@754: } rosfran@696: renatofilho@754: if (gmyth_upnp->backend_info != NULL) { renatofilho@754: g_object_unref(gmyth_upnp->backend_info); renatofilho@754: gmyth_upnp->backend_info = NULL; renatofilho@754: } renatofilho@754: renatofilho@754: G_OBJECT_CLASS(gmyth_upnp_parent_class)->dispose(object); melunko@250: } melunko@250: melunko@250: static void renatofilho@754: gmyth_upnp_finalize(GObject * object) melunko@250: { renatofilho@754: g_signal_handlers_destroy(object); melunko@250: renatofilho@754: G_OBJECT_CLASS(gmyth_upnp_parent_class)->finalize(object); melunko@250: } melunko@250: renatofilho@754: gchar * renatofilho@754: gmyth_upnp_device_status_to_string(GMythUPnPDeviceStatus status) rosfran@706: { renatofilho@754: if (status == CgUpnpDeviceStatusAdded) renatofilho@754: return "Added"; renatofilho@754: else if (status == CgUpnpDeviceStatusUpdated) renatofilho@754: return "Updated"; renatofilho@754: else if (status == CgUpnpDeviceStatusInvalid) renatofilho@754: return "Invalid"; renatofilho@754: else if (status == CgUpnpDeviceStatusRemoved) renatofilho@754: return "Removed"; rosfran@706: renatofilho@754: return ""; rosfran@696: } rosfran@696: rosfran@696: /** rosfran@696: * GObject's signal handler rosfran@696: */ renatofilho@754: static void renatofilho@754: _mythtv_device_found(GMythUPnP * gmyth_upnp, GMythUPnPDeviceStatus status, renatofilho@754: gchar * udn) rosfran@696: { renatofilho@754: g_debug("Device: [status = %s, UDN = %s]\n", renatofilho@754: gmyth_upnp_device_status_to_string(status), udn); renatofilho@754: } renatofilho@754: renatofilho@754: /** renatofilho@754: * GObject's signal handler renatofilho@754: */ renatofilho@754: static void renatofilho@754: _clinkc_mythtv_device_found(gchar * udn, GMythUPnPDeviceStatus status) renatofilho@754: { renatofilho@754: if (gmyth_upnp_static != NULL && udn != NULL) renatofilho@754: g_signal_emit(gmyth_upnp_static, GMYTH_UPNP_GET_CLASS(gmyth_upnp_static)->device_found_handler_signal_id, 0, /* details renatofilho@754: */ renatofilho@754: status, udn); rosfran@696: } rosfran@696: rosfran@696: /** melunko@250: * Create a control point and start it. melunko@250: */ renatofilho@754: static gboolean renatofilho@754: gmyth_upnp_initialize(GMythUPnP * gmyth_upnp, renatofilho@754: GMythBackendInfo * gmyth_backend_info, renatofilho@754: GMythUPnPDeviceListener device_found_handler) melunko@250: { renatofilho@754: gboolean ret = TRUE; rosfran@696: renatofilho@754: g_return_val_if_fail(gmyth_backend_info != NULL, FALSE); rosfran@696: renatofilho@754: /* renatofilho@754: * Create the cybergarage control point renatofilho@754: */ renatofilho@754: gmyth_upnp->control_point = cg_upnp_controlpoint_new(); rosfran@696: renatofilho@754: if (device_found_handler != NULL) { renatofilho@754: GMYTH_UPNP_GET_CLASS(gmyth_upnp)->device_found_handler = renatofilho@754: device_found_handler; rosfran@696: renatofilho@754: cg_upnp_controlpoint_setdevicelistener(gmyth_upnp->control_point, renatofilho@754: _clinkc_mythtv_device_found); renatofilho@754: } rosfran@696: renatofilho@754: /* renatofilho@754: * Start the control point renatofilho@754: */ renatofilho@754: if (cg_upnp_controlpoint_start(gmyth_upnp->control_point) == FALSE) { renatofilho@754: gmyth_debug("Unable to start UPnP control point!!!"); renatofilho@754: ret = FALSE; renatofilho@754: goto done; renatofilho@754: } else { renatofilho@754: gmyth_debug("Control point started."); renatofilho@754: } rosfran@696: renatofilho@754: done: rosfran@696: renatofilho@754: return ret; rosfran@696: } rosfran@696: rosfran@696: static void renatofilho@754: _gmyth_foreach_key_value(gchar * udn, gchar * dev, renatofilho@754: GList * upnp_servers_list) rosfran@696: { renatofilho@754: GMythUPnPDevice *gmyth_upnp = g_malloc0(sizeof(GMythUPnPDevice)); rosfran@696: renatofilho@754: GMythURI *uri = NULL; renatofilho@754: gmyth_upnp->uri = (gchar *) (dev); renatofilho@754: uri = gmyth_uri_new_with_value(gmyth_upnp->uri); rosfran@696: renatofilho@754: gmyth_upnp->host = gmyth_uri_get_host(uri); renatofilho@754: gmyth_upnp->port = gmyth_uri_get_port(uri); renatofilho@754: gmyth_upnp->protocol = gmyth_uri_get_protocol(uri); rosfran@696: renatofilho@754: gmyth_debug("MythTV UPnP service [ %s, %d ].", gmyth_upnp->host, renatofilho@754: gmyth_upnp->port); rosfran@706: renatofilho@754: upnp_servers_list = g_list_append(upnp_servers_list, gmyth_upnp); rosfran@696: renatofilho@754: if (uri != NULL) { renatofilho@754: g_object_unref(uri); renatofilho@754: uri = NULL; renatofilho@754: } rosfran@696: rosfran@696: } rosfran@696: renatofilho@754: GList * renatofilho@754: gmyth_upnp_do_search_sync(GMythUPnP * gmyth_upnp) rosfran@696: { renatofilho@754: GList *upnp_servers_list = NULL; renatofilho@754: guint iter_count = GMYTH_UPNP_MAX_SEARCHS; renatofilho@754: /* renatofilho@754: * gmyth_upnp->priv = GMYTH_UPNP_GET_PRIVATE( gmyth_upnp ); renatofilho@754: */ rosfran@696: renatofilho@754: while (gmyth_upnp->priv->upnp_dev_found == FALSE && (--iter_count > 0)) { rosfran@696: renatofilho@754: gmyth_debug renatofilho@754: ("UPnP MythTV Client control point is searching MythTV AV Device server...\n"); rosfran@696: renatofilho@754: if (gmyth_upnp->control_point != NULL) renatofilho@754: cg_upnp_controlpoint_search(gmyth_upnp->control_point, renatofilho@754: "urn:schemas-upnp-org:service:ContentDirectory:1"); melunko@250: renatofilho@754: /* renatofilho@754: * just to avoid clinkc pthread concurrency faults renatofilho@754: */ renatofilho@754: cg_wait(1000); rosfran@696: renatofilho@754: /* renatofilho@754: * discover if it was found renatofilho@754: */ renatofilho@754: gmyth_upnp->priv->upnp_dev_found = renatofilho@754: gmyth_upnp_got_mythtv_service(gmyth_upnp->control_point, renatofilho@754: &gmyth_upnp->priv->udn, renatofilho@754: &gmyth_upnp->priv-> renatofilho@754: mythtv_servers); rosfran@696: renatofilho@754: } /* while */ rosfran@696: renatofilho@754: if (gmyth_upnp->priv->upnp_dev_found) { rosfran@696: renatofilho@754: gmyth_debug("Found UPnP MythTV AV Device...\n"); melunko@250: renatofilho@754: g_hash_table_foreach(gmyth_upnp->priv->mythtv_servers, renatofilho@754: (GHFunc) _gmyth_foreach_key_value, renatofilho@754: upnp_servers_list); melunko@250: renatofilho@754: } renatofilho@754: /* renatofilho@754: * if - found UPnP device renatofilho@754: */ renatofilho@754: return upnp_servers_list; melunko@250: } melunko@250: melunko@250: /** rosfran@696: * Checks if got the MythTV service in the Control Point's device list. melunko@250: */ renatofilho@754: static gboolean renatofilho@754: gmyth_upnp_got_mythtv_service(CgUpnpControlPoint * controlPt, gchar ** udn, renatofilho@754: GHashTable ** mythtv_servers_lst) melunko@250: { rosfran@696: renatofilho@754: g_return_val_if_fail(mythtv_servers_lst != NULL, FALSE); renatofilho@754: g_return_val_if_fail(controlPt != NULL, FALSE); renatofilho@754: renatofilho@754: *mythtv_servers_lst = g_hash_table_new(g_str_hash, g_str_equal); renatofilho@754: renatofilho@754: const gchar *mythtvFriendlyName = "Myth"; renatofilho@754: /* renatofilho@754: * begin assertion about the size of discovered devices renatofilho@754: */ renatofilho@754: gint numDevices = renatofilho@754: cg_upnp_controlpoint_getndevices(controlPt); renatofilho@754: gint cntDevs = 0; renatofilho@754: CgUpnpDevice *childDev; renatofilho@754: gchar *devName = NULL, renatofilho@754: *dev_url = NULL; renatofilho@754: gboolean upnp_dev_found = FALSE; renatofilho@754: renatofilho@754: gmyth_debug("UPnP MythTV AV Device list size = %d\n", numDevices); renatofilho@754: renatofilho@754: for (childDev = cg_upnp_controlpoint_getdevices(controlPt); renatofilho@754: childDev != NULL; childDev = cg_upnp_device_next(childDev)) { renatofilho@754: devName = cg_upnp_device_getfriendlyname(childDev); renatofilho@754: dev_url = cg_upnp_device_getlocationfromssdppacket(childDev); renatofilho@754: gmyth_debug renatofilho@754: ("Device's friendly name = %s, and device's URL = %s\n", renatofilho@754: devName, dev_url); renatofilho@754: if ((g_strstr_len(devName, strlen(devName), mythtvFriendlyName) != renatofilho@754: NULL) == TRUE) { renatofilho@754: upnp_dev_found = TRUE; renatofilho@754: /* renatofilho@754: * stores the last UDN number ID renatofilho@754: */ renatofilho@754: *udn = cg_upnp_device_getudn(childDev); renatofilho@754: /* renatofilho@754: *mythtv_servers_lst = g_list_append( *mythtv_servers_lst, dev_url ); */ renatofilho@754: g_hash_table_insert(*mythtv_servers_lst, (gpointer) * udn, renatofilho@754: (gpointer) dev_url); renatofilho@754: } renatofilho@754: ++cntDevs; renatofilho@754: } renatofilho@754: renatofilho@754: if (upnp_dev_found == TRUE) { renatofilho@754: gmyth_debug renatofilho@754: ("MythTV AV Device found, from a total of %d devices.\n", renatofilho@754: cntDevs); renatofilho@754: } else if (numDevices == cntDevs) { renatofilho@754: gmyth_debug renatofilho@754: ("MythTV AV Device not found, from a total of %d devices.\n", renatofilho@754: cntDevs); renatofilho@754: } else { renatofilho@754: gmyth_debug renatofilho@754: ("Control Point's MythTV AV Device count is wrong: iterated over %d devices, but there are %d registered devices.\n", renatofilho@754: cntDevs, numDevices); renatofilho@754: } renatofilho@754: renatofilho@754: return upnp_dev_found; melunko@250: melunko@250: } melunko@250: melunko@250: /** Gets the UPnP AV devices server's list associated to this upnp. melunko@250: * rosfran@696: * @return The GHashTable* 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: */ renatofilho@754: GHashTable * renatofilho@754: gmyth_upnp_get_servers(GMythUPnP * gmyth_upnp) melunko@250: { melunko@250: renatofilho@754: if (NULL == gmyth_upnp || NULL == gmyth_upnp->priv->mythtv_servers) { renatofilho@754: gmyth_debug("[%s] GMythUPnP has no MythTV servers recognized.\n", renatofilho@754: __FUNCTION__); renatofilho@754: return NULL; renatofilho@754: } renatofilho@754: renatofilho@754: return gmyth_upnp->priv->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: */ renatofilho@754: GMythBackendInfo * renatofilho@754: gmyth_upnp_get_backend_info(GMythUPnP * gmyth_upnp) melunko@250: { melunko@250: renatofilho@754: if (NULL == gmyth_upnp || NULL == gmyth_upnp->backend_info) { renatofilho@754: gmyth_debug("[%s] GMythUPnP not initialized\n", __FUNCTION__); renatofilho@754: return NULL; renatofilho@754: } renatofilho@754: renatofilho@754: return gmyth_upnp->backend_info; melunko@250: }