~mdz/network-manager/ubuntu.0.7

« back to all changes in this revision

Viewing changes to libnm-glib/libnm-glib-test.c

  • Committer: tambeti
  • Date: 2007-02-12 09:23:43 UTC
  • Revision ID: vcs-imports@canonical.com-20070212092343-96d46863e2821ee2
2007-02-12  Tambet Ingo  <tambet@ximian.com>

        Totally break NetworkManager. Please use 0.6 branch until futher notice.

        * src/:
                - Remove old low-level dbus interface implementations and replace them
                  with dbus-glib one.

        * configure.in:
                - Require dbus-glib >= 0.72.
                - Plug in new sources to build.

        * libnm-glib/:
                - Implement GObject wrappers on top of DBUS glib auto-generated bindings
                  to make it more convenient to use from GObject based programs.

        * introspection/:
                - Implement DBUS XML introspection files, used by both NM and libnm-glib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include "nm-client.h"
 
3
#include "nm-device.h"
 
4
#include "nm-device-802-3-ethernet.h"
 
5
#include "nm-device-802-11-wireless.h"
 
6
 
 
7
static gboolean
 
8
test_wireless_enabled (NMClient *client)
 
9
{
 
10
        gboolean wireless;
 
11
 
 
12
        wireless = nm_client_wireless_get_enabled (client);
 
13
        g_print ("Wireless enabled? %s\n", wireless ? "yes" : "no");
 
14
 
 
15
        nm_client_wireless_set_enabled (client, !wireless);
 
16
 
 
17
        wireless = nm_client_wireless_get_enabled (client);
 
18
        g_print ("Wireless enabled? %s\n", wireless ? "yes" : "no");
 
19
 
 
20
        nm_client_wireless_set_enabled (client, !wireless);
 
21
 
 
22
        return TRUE;
 
23
}
 
24
 
 
25
static gboolean
 
26
test_get_state (NMClient *client)
 
27
{
 
28
        guint state;
 
29
 
 
30
        state = nm_client_get_state (client);
 
31
        g_print ("Current state: %d\n", state);
 
32
 
 
33
        return TRUE;
 
34
}
 
35
 
 
36
 
 
37
static void
 
38
dump_device (NMDevice *device)
 
39
{
 
40
        char *str;
 
41
        gboolean b;
 
42
        guint32 u;
 
43
        NMDeviceState state;
 
44
 
 
45
        str = nm_device_get_iface (device);
 
46
        g_print ("Interface: %s\n", str);
 
47
        g_free (str);
 
48
 
 
49
        str = nm_device_get_udi (device);
 
50
        g_print ("Udi: %s\n", str);
 
51
        g_free (str);
 
52
 
 
53
        str = nm_device_get_driver (device);
 
54
        g_print ("Driver: %s\n", str);
 
55
        g_free (str);
 
56
 
 
57
        b = nm_device_get_use_dhcp (device);
 
58
        g_print ("Use DHCP: %s\n", b ? "yes" : "no");
 
59
 
 
60
        u = nm_device_get_ip4_address (device);
 
61
        g_print ("IP address: %d\n", u);
 
62
 
 
63
        state = nm_device_get_state (device);
 
64
        g_print ("State: %d\n", state);
 
65
 
 
66
        if (NM_IS_DEVICE_802_3_ETHERNET (device)) {
 
67
                int speed = nm_device_802_3_ethernet_get_speed (NM_DEVICE_802_3_ETHERNET (device));
 
68
                g_print ("Speed: %d\n", speed);
 
69
        } else if (NM_IS_DEVICE_802_11_WIRELESS (device)) {
 
70
                GSList *iter;
 
71
                GSList *networks = nm_device_802_11_wireless_get_networks (NM_DEVICE_802_11_WIRELESS (device));
 
72
 
 
73
                g_print ("Networks:\n");
 
74
                for (iter = networks; iter; iter = iter->next) {
 
75
                        NMAccessPoint *ap = NM_ACCESS_POINT (iter->data);
 
76
 
 
77
                        str = nm_access_point_get_essid (ap);
 
78
                        g_print ("\tEssid: %s\n", str);
 
79
                        g_free (str);
 
80
                }
 
81
                g_slist_foreach (networks, (GFunc) g_object_unref, NULL);
 
82
                g_slist_free (networks);
 
83
        }
 
84
}
 
85
 
 
86
static gboolean
 
87
test_devices (NMClient *client)
 
88
{
 
89
        GSList *list, *iter;
 
90
 
 
91
        list = nm_client_get_devices (client);
 
92
        g_print ("Got devices:\n");
 
93
        for (iter = list; iter; iter = iter->next) {
 
94
                NMDevice *device = NM_DEVICE (iter->data);
 
95
                dump_device (device);
 
96
                g_print ("\n");
 
97
        }
 
98
 
 
99
        g_slist_foreach (list, (GFunc) g_object_unref, NULL);
 
100
        g_slist_free (list);
 
101
 
 
102
        return TRUE;
 
103
}
 
104
 
 
105
static void
 
106
device_added_cb (NMClient *client, NMDevice *device, gpointer user_data)
 
107
{
 
108
        g_print ("New device added\n");
 
109
        dump_device (device);
 
110
}
 
111
 
 
112
static void
 
113
device_removed_cb (NMClient *client, NMDevice *device, gpointer user_data)
 
114
{
 
115
        g_print ("Device removed\n");
 
116
        dump_device (device);
 
117
}
 
118
 
 
119
 
 
120
static gboolean
 
121
device_deactivate (gpointer user_data)
 
122
{
 
123
        NMDevice *device = NM_DEVICE (user_data);
 
124
 
 
125
        nm_device_deactivate (device);
 
126
 
 
127
        return FALSE;
 
128
}
 
129
 
 
130
static void
 
131
device_state_changed (NMDevice *device, NMDeviceState state, gpointer user_data)
 
132
{
 
133
        char *str;
 
134
 
 
135
        str = nm_device_get_iface (device);
 
136
        g_print ("Device state changed: %s %d\n", str, state);
 
137
        g_free (str);
 
138
 
 
139
        if (state == NM_DEVICE_STATE_ACTIVATED) {
 
140
                g_print ("Scheduling device deactivation\n");
 
141
                g_timeout_add (5 * 1000,
 
142
                                           device_deactivate,
 
143
                                           device);
 
144
        }
 
145
}
 
146
 
 
147
static gboolean
 
148
do_stuff (gpointer user_data)
 
149
{
 
150
        NMClient *client = NM_CLIENT (user_data);
 
151
        GSList *list, *iter;
 
152
 
 
153
        list = nm_client_get_devices (client);
 
154
        for (iter = list; iter; iter = iter->next) {
 
155
                if (NM_IS_DEVICE_802_3_ETHERNET (iter->data)) {
 
156
                        NMDevice8023Ethernet *device = NM_DEVICE_802_3_ETHERNET (iter->data);
 
157
 
 
158
                        g_signal_connect (device, "state-changed",
 
159
                                                          G_CALLBACK (device_state_changed),
 
160
                                                          NULL);
 
161
                        /* FIXME: This ref is never released */
 
162
                        g_object_ref (device);
 
163
 
 
164
                        nm_device_802_3_ethernet_activate (device, TRUE);
 
165
                        break;
 
166
                }
 
167
        }
 
168
 
 
169
        g_slist_foreach (list, (GFunc) g_object_unref, NULL);
 
170
        g_slist_free (list);
 
171
 
 
172
        return FALSE;
 
173
}
 
174
 
 
175
int
 
176
main (int argc, char *argv[])
 
177
{
 
178
        NMClient *client;
 
179
 
 
180
        g_type_init ();
 
181
 
 
182
        client = nm_client_new ();
 
183
        if (!client) {
 
184
                exit (1);
 
185
        }
 
186
 
 
187
        test_wireless_enabled (client);
 
188
        test_get_state (client);
 
189
        test_devices (client);
 
190
 
 
191
        g_signal_connect (client, "device-added",
 
192
                                          G_CALLBACK (device_added_cb), NULL);
 
193
        g_signal_connect (client, "device-removed",
 
194
                                          G_CALLBACK (device_removed_cb), NULL);
 
195
 
 
196
/*      g_idle_add (do_stuff, client); */
 
197
 
 
198
        g_main_loop_run (g_main_loop_new (NULL, FALSE));
 
199
 
 
200
        g_object_unref (client);
 
201
 
 
202
        return 0;
 
203
}