summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-energy-management-helper.c
blob: 2f399729fde6ad101d4b7764ba00107d9e09492c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * 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.1 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 library; 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>

#if defined(__linux__)

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#include <linux/wireless.h>

gboolean
rygel_energy_management_get_mac_and_network_type (const char *iface,
                                                  char      **mac,
                                                  char      **type) {
    int fd = -1;
    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 - 1);
    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 > -1) {
        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