[rygel] core: Move non-portable functions to C



commit 2e70dcffe79e022fb86eca9930680fe93b7c22f8
Author: Jens Georg <mail jensge org>
Date:   Fri Oct 31 23:27:50 2014 +0100

    core: Move non-portable functions to C
    
    This way, we can #ifdef properly for different OSs other than Linux.
    
    Signed-off-by: Jens Georg <mail jensge org>

 src/librygel-core/Makefile.am                      |    3 -
 src/librygel-core/filelist.am                      |    3 +-
 src/librygel-core/rygel-energy-management-helper.c |  115 ++++++++++++++++++++
 .../rygel-energy-management-helper.vapi            |    9 --
 src/librygel-core/rygel-energy-management.vala     |   52 +--------
 5 files changed, 121 insertions(+), 61 deletions(-)
---
diff --git a/src/librygel-core/Makefile.am b/src/librygel-core/Makefile.am
index 97f51c7..4f4af9b 100644
--- a/src/librygel-core/Makefile.am
+++ b/src/librygel-core/Makefile.am
@@ -16,9 +16,6 @@ librygel_core_2_4_la_VALAFLAGS = \
        --gir=Rygel-2.4.gir \
        --vapidir=$(srcdir) \
        --pkg uuid \
-       --pkg posix \
-       --pkg linux \
-       --pkg rygel-energy-management-helper \
        $(LIBRYGEL_CORE_DEPS_VALAFLAGS) \
        $(RYGEL_COMMON_VALAFLAGS)
 
diff --git a/src/librygel-core/filelist.am b/src/librygel-core/filelist.am
index 4ac48da..c743202 100644
--- a/src/librygel-core/filelist.am
+++ b/src/librygel-core/filelist.am
@@ -26,4 +26,5 @@ LIBRYGEL_CORE_VAPI_SOURCE_FILES = \
 LIBRYGEL_CORE_NONVAPI_SOURCE_FILES = \
        rygel-icon-info.vala \
        rygel-xml-utils.vala \
-       rygel-plugin-information.vala
+       rygel-plugin-information.vala \
+       rygel-energy-management-helper.c
diff --git a/src/librygel-core/rygel-energy-management-helper.c 
b/src/librygel-core/rygel-energy-management-helper.c
new file mode 100644
index 0000000..6e5c857
--- /dev/null
+++ b/src/librygel-core/rygel-energy-management-helper.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2013 Intel Corporation.
+ * Copyright (C) 2014 Jens Georg.
+ *
+ * Author: Jussi Kukkonen <jussi kukkonen intel com>
+ *         Jens Georg <mail jensge org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#ifdef G_OS_LINUX
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <linux/wireless.h>
+
+gboolean
+rygel_energy_management_get_mac_and_network_type (const char *iface,
+                                                  char      **mac,
+                                                  char      **type) {
+    int fd;
+    struct ifreq ifr;
+    gboolean retval = FALSE;
+
+    g_return_val_if_fail (mac != NULL, FALSE);
+    g_return_val_if_fail (type != NULL, FALSE);
+
+    *mac = NULL;
+    *type = NULL;
+
+    fd = socket (AF_INET, SOCK_STREAM, 0);
+    if (fd == -1) {
+        g_warning (_("Failed to get a socket: %s"), strerror (errno));
+
+        goto out;
+    }
+
+    strncpy (ifr.ifr_name, iface, IFNAMSIZ);
+    if (ioctl (fd, SIOCGIFHWADDR, &ifr) < 0) {
+        g_warning (_("Failed to get MAC address for %s: %s"),
+                   iface,
+                   strerror (errno));
+
+        goto out;
+    }
+
+    *mac = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
+                            ifr.ifr_hwaddr.sa_data[0],
+                            ifr.ifr_hwaddr.sa_data[1],
+                            ifr.ifr_hwaddr.sa_data[2],
+                            ifr.ifr_hwaddr.sa_data[3],
+                            ifr.ifr_hwaddr.sa_data[4],
+                            ifr.ifr_hwaddr.sa_data[5]);
+
+    if (ioctl (fd, SIOCGIWNAME, &ifr) < 0) {
+        *type = g_strdup ("Ethernet");
+    } else {
+        *type = g_strdup ("Wi-Fi");
+    }
+
+    retval = TRUE;
+out:
+    if (fd > 0) {
+        close (fd);
+    }
+
+    if (*mac == NULL) {
+        *mac = g_strdup ("00:00:00:00:00;00");
+    }
+
+    if (*type == NULL) {
+        *type = g_strdup ("Other");
+    }
+
+    return retval;
+}
+#else
+gboolean
+rygel_energy_management_get_mac_and_network_type (const char *iface,
+                                                  char      **mac,
+                                                  char      **type) {
+    g_warning (_("MAC and network type querying not implemented"));
+
+    if (mac != NULL) {
+        *mac = g_strdup ("00:00:00:00:00:00");
+    }
+
+    if (type != NULL) {
+        *type = g_strdup ("Other");
+    }
+
+    return TRUE;
+}
+#endif
diff --git a/src/librygel-core/rygel-energy-management.vala b/src/librygel-core/rygel-energy-management.vala
index ab8e113..e565cc4 100644
--- a/src/librygel-core/rygel-energy-management.vala
+++ b/src/librygel-core/rygel-energy-management.vala
@@ -116,53 +116,9 @@ public class Rygel.EnergyManagement : Service {
                      this.create_network_interface_info ());
     }
 
-    private bool get_mac_and_network_type (string iface,
-                                           out string mac,
-                                           out string type) {
-        var success = true;
-
-        var sock = Posix.socket (Posix.AF_INET, Posix.SOCK_STREAM, 0);
-        if (sock == -1) {
-            warning ("Failed to get a socket: %s\n", strerror (errno));
-            mac = "00:00:00:00:00:00";
-            type = "Other";
-
-            return false;
-        }
-
-        var ifreq = Linux.Network.IfReq ();
-        var ifreqp = (Linux.Network.IfReq*)(&ifreq);
-        Posix.strncpy ((string)ifreqp->ifr_name,
-                       iface,
-                       Linux.Network.INTERFACE_NAME_SIZE);
-
-        if (Posix.ioctl (sock, Linux.Network.SIOCGIFHWADDR, &ifreq) < 0) {
-            warning ("Failed to get mac address: %s\n",
-                     strerror (errno));
-            mac = "00:00:00:00:00:00";
-            success = false;
-        } else {
-            /* workaround for https://bugzilla.gnome.org/show_bug.cgi?id=707180 */
-            EnergyManagementHelper.SockAddr *addr =
-                                        (EnergyManagementHelper.SockAddr*)(&ifreq.ifr_hwaddr);
-
-            mac = "%02X:%02X:%02X:%02X:%02X:%02X".printf
-                                        ((uchar)addr.sa_data[0], (uchar)addr.sa_data[1],
-                                         (uchar)addr.sa_data[2], (uchar)addr.sa_data[3],
-                                         (uchar)addr.sa_data[4], (uchar)addr.sa_data[5]);
-        }
-
-        /* Note that this call really takes a struct IwReq, but this
-         * works since we only test if the call fails or not */
-        var ret_val = Posix.ioctl (sock, Linux.WirelessExtensions.SIOCGIWNAME, &ifreq);
-        if (ret_val == -1) {
-            type = "Ethernet";
-        } else {
-            type = "Wi-Fi";
-        }
-
-        return success;
-    }
+    extern static bool get_mac_and_network_type (string iface,
+                                                 out string mac,
+                                                 out string type);
 
     private string create_network_interface_info () {
 
@@ -172,7 +128,7 @@ public class Rygel.EnergyManagement : Service {
         var iface = this.root_device.context.interface;
         var config_section ="EnergyManagement-%s".printf (iface);
 
-        success = this.get_mac_and_network_type (iface, out mac_address, out type);
+        success = EnergyManagement.get_mac_and_network_type (iface, out mac_address, out type);
 
         var mac = mac_address.replace (":", "");
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]