5 * Copyright (C) 2007 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include <net/route.h>
38 #include <linux/netlink.h>
39 #include <linux/rtnetlink.h>
44 #include <hal/libhal.h>
48 static DBusConnection *connection = NULL;
50 static gchar *ifname_filter = NULL;
52 static GSList *drivers = NULL;
54 int connman_iface_register(struct connman_iface_driver *driver)
56 DBG("driver %p", driver);
58 drivers = g_slist_append(drivers, driver);
63 void connman_iface_unregister(struct connman_iface_driver *driver)
65 DBG("driver %p", driver);
67 drivers = g_slist_remove(drivers, driver);
70 static GSList *interfaces = NULL;
72 struct connman_iface *__connman_iface_find(int index)
76 for (list = interfaces; list; list = list->next) {
77 struct connman_iface *iface = list->data;
79 if (iface->index == index)
86 void __connman_iface_list(DBusMessageIter *iter)
92 for (list = interfaces; list; list = list->next) {
93 struct connman_iface *iface = list->data;
95 dbus_message_iter_append_basic(iter,
96 DBUS_TYPE_OBJECT_PATH, &iface->path);
100 static void append_entry(DBusMessageIter *dict,
101 const char *key, int type, void *val)
103 DBusMessageIter entry, value;
104 const char *signature;
106 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
109 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
112 case DBUS_TYPE_STRING:
113 signature = DBUS_TYPE_STRING_AS_STRING;
115 case DBUS_TYPE_UINT16:
116 signature = DBUS_TYPE_UINT16_AS_STRING;
119 signature = DBUS_TYPE_VARIANT_AS_STRING;
123 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
125 dbus_message_iter_append_basic(&value, type, val);
126 dbus_message_iter_close_container(&entry, &value);
128 dbus_message_iter_close_container(dict, &entry);
131 static gboolean scan_timeout(gpointer user_data)
133 struct connman_iface *iface = user_data;
135 switch (iface->state) {
136 case CONNMAN_IFACE_STATE_SCANNING:
137 if (iface->driver->scan)
138 iface->driver->scan(iface);
147 static void state_changed(struct connman_iface *iface)
149 const char *str = __connman_iface_state2string(iface->state);
150 enum connman_iface_state state = iface->state;
152 DBG("iface %p state %s", iface, str);
154 g_dbus_emit_signal(connection, iface->path,
155 CONNMAN_IFACE_INTERFACE, "StateChanged",
156 DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
158 switch (iface->state) {
159 case CONNMAN_IFACE_STATE_OFF:
160 __connman_dhcp_release(iface);
163 case CONNMAN_IFACE_STATE_ENABLED:
164 __connman_dhcp_release(iface);
165 connman_iface_clear_ipv4(iface);
166 if (iface->driver->disconnect)
167 iface->driver->disconnect(iface);
168 if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING)
169 state = CONNMAN_IFACE_STATE_SCANNING;
172 case CONNMAN_IFACE_STATE_SCANNING:
173 if (iface->driver->scan)
174 iface->driver->scan(iface);
175 g_timeout_add(8000, scan_timeout, iface);
178 case CONNMAN_IFACE_STATE_CARRIER:
179 if (iface->policy == CONNMAN_IFACE_POLICY_AUTO)
180 state = CONNMAN_IFACE_STATE_CONFIGURE;
183 case CONNMAN_IFACE_STATE_CONFIGURE:
184 __connman_dhcp_request(iface);
187 case CONNMAN_IFACE_STATE_SHUTDOWN:
188 __connman_dhcp_release(iface);
189 if (iface->driver->disconnect)
190 iface->driver->disconnect(iface);
191 if (iface->policy != CONNMAN_IFACE_POLICY_AUTO)
192 state = CONNMAN_IFACE_STATE_OFF;
195 case CONNMAN_IFACE_STATE_READY:
202 if (iface->state != state) {
203 iface->state = state;
204 state_changed(iface);
208 static void switch_policy(struct connman_iface *iface)
210 DBG("iface %p", iface);
212 switch (iface->policy) {
213 case CONNMAN_IFACE_POLICY_OFF:
214 iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
215 state_changed(iface);
216 connman_iface_clear_ipv4(iface);
217 if (iface->driver->stop)
218 iface->driver->stop(iface);
220 __connman_iface_down(iface);
223 case CONNMAN_IFACE_POLICY_IGNORE:
226 case CONNMAN_IFACE_POLICY_AUTO:
227 if (iface->driver->start)
228 iface->driver->start(iface);
230 __connman_iface_up(iface);
231 state_changed(iface);
239 void connman_iface_indicate_enabled(struct connman_iface *iface)
241 DBG("iface %p state %d", iface, iface->state);
243 switch (iface->state) {
244 case CONNMAN_IFACE_STATE_OFF:
245 case CONNMAN_IFACE_STATE_CARRIER:
246 iface->state = CONNMAN_IFACE_STATE_ENABLED;
247 state_changed(iface);
254 void connman_iface_indicate_disabled(struct connman_iface *iface)
256 DBG("iface %p state %d", iface, iface->state);
258 if (iface->policy == CONNMAN_IFACE_POLICY_AUTO) {
259 iface->state = CONNMAN_IFACE_STATE_ENABLED;
260 if (iface->driver->start)
261 iface->driver->start(iface);
263 __connman_iface_up(iface);
265 iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
267 state_changed(iface);
270 void connman_iface_indicate_connected(struct connman_iface *iface)
272 DBG("iface %p state %d", iface, iface->state);
274 switch (iface->state) {
275 case CONNMAN_IFACE_STATE_CONNECT:
276 iface->state = CONNMAN_IFACE_STATE_CONNECTED;
277 state_changed(iface);
284 void connman_iface_indicate_carrier_on(struct connman_iface *iface)
286 DBG("iface %p state %d", iface, iface->state);
288 switch (iface->state) {
289 case CONNMAN_IFACE_STATE_ENABLED:
290 case CONNMAN_IFACE_STATE_CONNECT:
291 case CONNMAN_IFACE_STATE_CONNECTED:
292 iface->state = CONNMAN_IFACE_STATE_CARRIER;
293 state_changed(iface);
300 void connman_iface_indicate_carrier_off(struct connman_iface *iface)
302 DBG("iface %p state %d", iface, iface->state);
304 switch (iface->state) {
305 case CONNMAN_IFACE_STATE_CARRIER:
306 case CONNMAN_IFACE_STATE_CONFIGURE:
307 case CONNMAN_IFACE_STATE_READY:
309 if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING) {
310 if (iface->driver->disconnect)
311 iface->driver->disconnect(iface);
312 iface->state = CONNMAN_IFACE_STATE_SCANNING;
315 iface->state = CONNMAN_IFACE_STATE_ENABLED;
316 state_changed(iface);
323 void connman_iface_indicate_configured(struct connman_iface *iface)
325 DBG("iface %p state %d", iface, iface->state);
327 switch (iface->state) {
328 case CONNMAN_IFACE_STATE_CONFIGURE:
329 iface->state = CONNMAN_IFACE_STATE_READY;
330 state_changed(iface);
337 static void append_station(DBusMessage *reply, const char *name,
338 int signal, int security)
340 DBusMessageIter array, dict;
341 const char *wpa = "WPA";
343 dbus_message_iter_init_append(reply, &array);
345 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
346 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
347 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
348 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
350 append_entry(&dict, "ESSID", DBUS_TYPE_STRING, &name);
351 append_entry(&dict, "Signal", DBUS_TYPE_UINT16, &signal);
354 append_entry(&dict, "Security", DBUS_TYPE_STRING, &wpa);
356 dbus_message_iter_close_container(&array, &dict);
359 void connman_iface_indicate_station(struct connman_iface *iface,
360 const char *name, int strength, int security)
363 char *ssid, *passphrase;
366 DBG("iface %p security %d name %s", iface, security, name);
368 if (name == NULL || strlen(name) == 0)
371 signal = dbus_message_new_signal(iface->path,
372 CONNMAN_IFACE_INTERFACE, "NetworkFound");
376 append_station(signal, name, strength, security);
378 dbus_connection_send(connection, signal, NULL);
379 dbus_message_unref(signal);
381 if (iface->state != CONNMAN_IFACE_STATE_SCANNING)
389 /* The D-Link access points return a 0x05 at the end of the SSID */
390 if (ssid[len - 1] == '\05')
391 ssid[len - 1] = '\0';
393 passphrase = __connman_iface_find_passphrase(iface, ssid);
394 if (passphrase != NULL) {
395 DBG("network %s passphrase %s", ssid, passphrase);
397 g_free(iface->network.identifier);
398 iface->network.identifier = g_strdup(ssid);
399 g_free(iface->network.passphrase);
400 iface->network.passphrase = passphrase;
402 if (iface->driver->connect) {
403 iface->driver->connect(iface, &iface->network);
404 iface->state = CONNMAN_IFACE_STATE_CONNECT;
405 state_changed(iface);
412 int connman_iface_get_ipv4(struct connman_iface *iface,
413 struct connman_ipv4 *ipv4)
421 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
424 DBG("iface %p ipv4 %p", iface, ipv4);
426 memset(&req, 0, sizeof(req));
427 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
428 req.hdr.nlmsg_type = RTM_GETADDR;
429 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
430 req.hdr.nlmsg_pid = 0;
431 req.hdr.nlmsg_seq = 4711;
432 req.msg.rtgen_family = AF_INET;
434 __connman_rtnl_send(&req, sizeof(req));
440 int connman_iface_set_ipv4(struct connman_iface *iface,
441 struct connman_ipv4 *ipv4)
445 struct sockaddr_in *addr;
448 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
451 DBG("iface %p ipv4 %p", iface, ipv4);
453 sk = socket(PF_INET, SOCK_DGRAM, 0);
457 memset(&ifr, 0, sizeof(ifr));
458 ifr.ifr_ifindex = iface->index;
460 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
465 DBG("ifname %s", ifr.ifr_name);
467 addr = (struct sockaddr_in *) &ifr.ifr_addr;
468 addr->sin_family = AF_INET;
469 addr->sin_addr = ipv4->address;
471 err = ioctl(sk, SIOCSIFADDR, &ifr);
474 DBG("address setting failed (%s)", strerror(errno));
476 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
477 addr->sin_family = AF_INET;
478 addr->sin_addr = ipv4->netmask;
480 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
483 DBG("netmask setting failed (%s)", strerror(errno));
485 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
486 addr->sin_family = AF_INET;
487 addr->sin_addr = ipv4->broadcast;
489 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
492 DBG("broadcast setting failed (%s)", strerror(errno));
494 memset(&rt, 0, sizeof(rt));
495 rt.rt_flags = RTF_UP | RTF_GATEWAY;
497 addr = (struct sockaddr_in *) &rt.rt_dst;
498 addr->sin_family = AF_INET;
499 addr->sin_addr.s_addr = INADDR_ANY;
501 addr = (struct sockaddr_in *) &rt.rt_gateway;
502 addr->sin_family = AF_INET;
503 addr->sin_addr = ipv4->gateway;
505 addr = (struct sockaddr_in *) &rt.rt_genmask;
506 addr->sin_family = AF_INET;
507 addr->sin_addr.s_addr = INADDR_ANY;
509 err = ioctl(sk, SIOCADDRT, &rt);
514 DBG("default route failed (%s)", strerror(errno));
518 __connman_resolver_append(iface, inet_ntoa(ipv4->nameserver));
523 int connman_iface_clear_ipv4(struct connman_iface *iface)
526 struct sockaddr_in *addr;
529 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
532 DBG("iface %p", iface);
534 sk = socket(PF_INET, SOCK_DGRAM, 0);
538 memset(&ifr, 0, sizeof(ifr));
539 ifr.ifr_ifindex = iface->index;
541 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
546 DBG("ifname %s", ifr.ifr_name);
548 addr = (struct sockaddr_in *) &ifr.ifr_addr;
549 addr->sin_family = AF_INET;
550 addr->sin_addr.s_addr = INADDR_ANY;
552 //err = ioctl(sk, SIOCDIFADDR, &ifr);
553 err = ioctl(sk, SIOCSIFADDR, &ifr);
557 if (err < 0 && errno != EADDRNOTAVAIL) {
558 DBG("address removal failed (%s)", strerror(errno));
562 __connman_resolver_remove(iface);
567 static DBusMessage *scan_iface(DBusConnection *conn,
568 DBusMessage *msg, void *data)
570 struct connman_iface *iface = data;
571 struct connman_iface_driver *driver = iface->driver;
574 DBG("conn %p", conn);
576 reply = dbus_message_new_method_return(msg);
583 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
588 static DBusMessage *get_properties(DBusConnection *conn,
589 DBusMessage *msg, void *data)
591 struct connman_iface *iface = data;
593 DBusMessageIter array, dict;
596 DBG("conn %p", conn);
598 reply = dbus_message_new_method_return(msg);
602 dbus_message_iter_init_append(reply, &array);
604 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
605 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
606 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
607 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
609 str = __connman_iface_type2string(iface->type);
610 append_entry(&dict, "Type", DBUS_TYPE_STRING, &str);
612 str = __connman_iface_state2string(iface->state);
613 append_entry(&dict, "State", DBUS_TYPE_STRING, &str);
615 if (iface->type == CONNMAN_IFACE_TYPE_80211) {
616 dbus_uint16_t signal = 75;
617 append_entry(&dict, "Signal", DBUS_TYPE_UINT16, &signal);
620 str = __connman_iface_policy2string(iface->policy);
621 append_entry(&dict, "Policy", DBUS_TYPE_STRING, &str);
623 if (iface->device.driver != NULL)
624 append_entry(&dict, "Driver",
625 DBUS_TYPE_STRING, &iface->device.driver);
627 if (iface->device.vendor != NULL)
628 append_entry(&dict, "Vendor",
629 DBUS_TYPE_STRING, &iface->device.vendor);
631 if (iface->device.product != NULL)
632 append_entry(&dict, "Product",
633 DBUS_TYPE_STRING, &iface->device.product);
635 dbus_message_iter_close_container(&array, &dict);
640 static DBusMessage *get_state(DBusConnection *conn,
641 DBusMessage *msg, void *data)
643 struct connman_iface *iface = data;
647 DBG("conn %p", conn);
649 reply = dbus_message_new_method_return(msg);
653 state = __connman_iface_state2string(iface->state);
655 dbus_message_append_args(reply, DBUS_TYPE_STRING, &state,
661 static DBusMessage *get_signal(DBusConnection *conn,
662 DBusMessage *msg, void *data)
664 struct connman_iface *iface = data;
666 dbus_uint16_t signal;
668 DBG("conn %p", conn);
670 reply = dbus_message_new_method_return(msg);
674 if (iface->type == CONNMAN_IFACE_TYPE_80211)
679 dbus_message_append_args(reply, DBUS_TYPE_UINT16, &signal,
685 static DBusMessage *get_policy(DBusConnection *conn,
686 DBusMessage *msg, void *data)
688 struct connman_iface *iface = data;
692 DBG("conn %p", conn);
694 reply = dbus_message_new_method_return(msg);
698 policy = __connman_iface_policy2string(iface->policy);
700 dbus_message_append_args(reply, DBUS_TYPE_STRING, &policy,
706 static DBusMessage *set_policy(DBusConnection *conn,
707 DBusMessage *msg, void *data)
709 struct connman_iface *iface = data;
711 enum connman_iface_policy new_policy;
714 DBG("conn %p", conn);
716 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &policy,
719 new_policy = __connman_iface_string2policy(policy);
721 reply = dbus_message_new_method_return(msg);
725 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
727 if (iface->policy != new_policy) {
728 iface->policy = new_policy;
729 __connman_iface_store(iface);
731 switch_policy(iface);
732 policy = __connman_iface_policy2string(new_policy);
734 g_dbus_emit_signal(conn, iface->path, CONNMAN_IFACE_INTERFACE,
735 "PolicyChanged", DBUS_TYPE_STRING, &policy,
742 static void append_network(DBusMessage *reply,
743 struct connman_iface *iface, gboolean secrets)
745 DBusMessageIter array, dict;
747 dbus_message_iter_init_append(reply, &array);
749 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
750 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
751 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
752 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
754 switch (iface->type) {
755 case CONNMAN_IFACE_TYPE_80211:
756 if (iface->network.identifier != NULL)
757 append_entry(&dict, "ESSID",
758 DBUS_TYPE_STRING, &iface->network.identifier);
759 if (secrets == TRUE && iface->network.passphrase != NULL)
760 append_entry(&dict, "PSK",
761 DBUS_TYPE_STRING, &iface->network.passphrase);
767 dbus_message_iter_close_container(&array, &dict);
770 static DBusMessage *get_network(DBusConnection *conn,
771 DBusMessage *msg, void *data)
773 struct connman_iface *iface = data;
776 DBG("conn %p", conn);
778 reply = dbus_message_new_method_return(msg);
782 append_network(reply, iface, TRUE);
787 static DBusMessage *set_network(DBusConnection *conn,
788 DBusMessage *msg, void *data)
790 struct connman_iface *iface = data;
791 DBusMessage *reply, *signal;
792 DBusMessageIter array, dict;
793 gboolean changed = FALSE;
795 DBG("conn %p", conn);
797 dbus_message_iter_init(msg, &array);
799 dbus_message_iter_recurse(&array, &dict);
801 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
802 DBusMessageIter entry, value;
803 const char *key, *val;
805 dbus_message_iter_recurse(&dict, &entry);
806 dbus_message_iter_get_basic(&entry, &key);
808 dbus_message_iter_next(&entry);
810 dbus_message_iter_recurse(&entry, &value);
812 //type = dbus_message_iter_get_arg_type(&value);
813 dbus_message_iter_get_basic(&value, &val);
815 if (g_strcasecmp(key, "ESSID") == 0) {
816 g_free(iface->network.identifier);
817 iface->network.identifier = g_strdup(val);
821 if (g_strcasecmp(key, "PSK") == 0) {
822 g_free(iface->network.passphrase);
823 iface->network.passphrase = g_strdup(val);
827 dbus_message_iter_next(&dict);
830 reply = dbus_message_new_method_return(msg);
834 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
836 if (changed == TRUE) {
837 __connman_iface_store(iface);
839 signal = dbus_message_new_signal(iface->path,
840 CONNMAN_IFACE_INTERFACE, "NetworkChanged");
841 if (signal != NULL) {
842 append_network(signal, iface, FALSE);
843 dbus_connection_send(conn, signal, NULL);
844 dbus_message_unref(signal);
847 if (iface->driver->disconnect)
848 iface->driver->disconnect(iface);
850 if (iface->driver->connect)
851 iface->driver->connect(iface, &iface->network);
857 static DBusMessage *select_network(DBusConnection *conn,
858 DBusMessage *msg, void *data)
860 struct connman_iface *iface = data;
864 DBG("conn %p", conn);
866 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &network,
869 g_free(iface->network.identifier);
870 iface->network.identifier = g_strdup(network);
872 if (iface->driver->disconnect)
873 iface->driver->disconnect(iface);
875 if (iface->driver->connect)
876 iface->driver->connect(iface, &iface->network);
878 reply = dbus_message_new_method_return(msg);
882 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
887 static void append_ipv4(DBusMessage *reply, struct connman_iface *iface)
889 DBusMessageIter array, dict;
892 dbus_message_iter_init_append(reply, &array);
894 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
895 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
896 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
897 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
899 str = __connman_ipv4_method2string(CONNMAN_IPV4_METHOD_DHCP);
900 append_entry(&dict, "Method", DBUS_TYPE_STRING, &str);
902 if (iface->ipv4.address.s_addr != INADDR_ANY) {
903 str = inet_ntoa(iface->ipv4.address);
904 append_entry(&dict, "Address", DBUS_TYPE_STRING, &str);
907 if (iface->ipv4.netmask.s_addr != INADDR_ANY) {
908 str = inet_ntoa(iface->ipv4.netmask);
909 append_entry(&dict, "Netmask", DBUS_TYPE_STRING, &str);
912 if (iface->ipv4.gateway.s_addr != INADDR_ANY) {
913 str = inet_ntoa(iface->ipv4.gateway);
914 append_entry(&dict, "Gateway", DBUS_TYPE_STRING, &str);
917 dbus_message_iter_close_container(&array, &dict);
920 static DBusMessage *get_ipv4(DBusConnection *conn,
921 DBusMessage *msg, void *data)
923 struct connman_iface *iface = data;
926 DBG("conn %p", conn);
928 switch (iface->policy) {
929 case CONNMAN_IFACE_POLICY_OFF:
930 case CONNMAN_IFACE_POLICY_IGNORE:
931 return dbus_message_new_error(msg, CONNMAN_ERROR_INTERFACE
932 ".NotAvailable", "");
937 reply = dbus_message_new_method_return(msg);
941 append_ipv4(reply, iface);
946 static DBusMessage *set_ipv4(DBusConnection *conn,
947 DBusMessage *msg, void *data)
949 struct connman_iface *iface = data;
950 DBusMessage *reply, *signal;
951 DBusMessageIter array, dict;
952 gboolean changed = FALSE;
954 DBG("conn %p", conn);
956 return dbus_message_new_error(msg, CONNMAN_ERROR_INTERFACE
957 ".NotImplemented", "");
959 dbus_message_iter_init(msg, &array);
961 dbus_message_iter_recurse(&array, &dict);
963 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
964 DBusMessageIter entry, value;
965 const char *key, *val;
966 enum connman_ipv4_method method;
969 dbus_message_iter_recurse(&dict, &entry);
970 dbus_message_iter_get_basic(&entry, &key);
972 dbus_message_iter_next(&entry);
974 dbus_message_iter_recurse(&entry, &value);
976 //type = dbus_message_iter_get_arg_type(&value);
977 dbus_message_iter_get_basic(&value, &val);
979 if (g_strcasecmp(key, "Method") == 0) {
980 method = __connman_ipv4_string2method(val);
981 if (iface->ipv4.method != method) {
982 iface->ipv4.method = method;
987 if (g_strcasecmp(key, "Address") == 0) {
988 addr = inet_addr(val);
989 if (iface->ipv4.address.s_addr != addr) {
990 iface->ipv4.address.s_addr = addr;
995 if (g_strcasecmp(key, "Netmask") == 0) {
996 addr = inet_addr(val);
997 if (iface->ipv4.netmask.s_addr != addr) {
998 iface->ipv4.netmask.s_addr = addr;
1003 if (g_strcasecmp(key, "Gateway") == 0) {
1004 addr = inet_addr(val);
1005 if (iface->ipv4.gateway.s_addr != addr) {
1006 iface->ipv4.gateway.s_addr = addr;
1011 dbus_message_iter_next(&dict);
1014 reply = dbus_message_new_method_return(msg);
1018 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
1020 if (changed == TRUE) {
1021 __connman_iface_store(iface);
1023 signal = dbus_message_new_signal(iface->path,
1024 CONNMAN_IFACE_INTERFACE, "IPv4Changed");
1025 if (signal != NULL) {
1026 append_ipv4(signal, iface);
1027 dbus_connection_send(conn, signal, NULL);
1028 dbus_message_unref(signal);
1035 static GDBusMethodTable iface_methods[] = {
1036 { "Scan", "", "", scan_iface },
1037 { "GetProperties", "", "a{sv}", get_properties },
1038 { "GetState", "", "s", get_state },
1039 { "GetSignal", "", "q", get_signal },
1040 { "GetPolicy", "", "s", get_policy },
1041 { "SetPolicy", "s", "", set_policy },
1042 { "GetNetwork", "", "a{sv}", get_network },
1043 { "SetNetwork", "a{sv}", "", set_network },
1044 { "SelectNetwork", "s", "", select_network },
1045 { "GetIPv4", "", "a{sv}", get_ipv4 },
1046 { "SetIPv4", "a{sv}", "", set_ipv4 },
1050 static GDBusSignalTable iface_signals[] = {
1051 { "StateChanged", "s" },
1052 { "SignalChanged", "q" },
1053 { "PolicyChanged", "s" },
1054 { "NetworkFound", "a{sv}" },
1055 { "NetworkChanged", "a{sv}" },
1056 { "IPv4Changed", "a{sv}" },
1060 static void device_free(void *data)
1062 struct connman_iface *iface = data;
1064 DBG("iface %p", iface);
1066 connman_iface_clear_ipv4(iface);
1068 if (iface->driver && iface->driver->remove)
1069 iface->driver->remove(iface);
1071 g_free(iface->path);
1073 g_free(iface->sysfs);
1074 g_free(iface->identifier);
1075 g_free(iface->network.identifier);
1076 g_free(iface->network.passphrase);
1077 g_free(iface->device.driver);
1078 g_free(iface->device.vendor);
1079 g_free(iface->device.product);
1083 static void detect_device_info(LibHalContext *ctx, struct connman_iface *iface)
1085 char *parent, *subsys, *value;
1087 parent = libhal_device_get_property_string(ctx, iface->udi,
1088 "info.parent", NULL);
1090 subsys = libhal_device_get_property_string(ctx, iface->udi,
1091 "linux.subsystem", NULL);
1093 value = libhal_device_get_property_string(ctx, iface->udi,
1094 "info.linux.driver", NULL);
1095 if (value == NULL) {
1096 value = libhal_device_get_property_string(ctx, parent,
1097 "info.linux.driver", NULL);
1099 iface->device.driver = g_strdup(value);
1102 if (strcmp(subsys, "net") == 0) {
1103 value = libhal_device_get_property_string(ctx, parent,
1104 "info.vendor", NULL);
1106 iface->device.vendor = g_strdup(value);
1108 value = libhal_device_get_property_string(ctx, parent,
1109 "info.product", NULL);
1111 iface->device.product = g_strdup(value);
1115 static int probe_device(LibHalContext *ctx,
1116 struct connman_iface_driver *driver, const char *udi)
1118 DBusConnection *conn;
1119 struct connman_iface *iface;
1120 char *temp, *sysfs, *ifname;
1123 DBG("ctx %p driver %p udi %s", ctx, driver, udi);
1128 iface = g_try_new0(struct connman_iface, 1);
1132 temp = g_path_get_basename(udi);
1133 iface->path = g_strdup_printf("%s/%s", CONNMAN_IFACE_BASEPATH, temp);
1136 iface->udi = g_strdup(udi);
1138 DBG("iface %p path %s", iface, iface->path);
1140 sysfs = libhal_device_get_property_string(ctx, udi,
1141 "linux.sysfs_path", NULL);
1143 iface->sysfs = g_strdup(sysfs);
1145 detect_device_info(ctx, iface);
1149 if (g_str_has_prefix(driver->capability, "net") == TRUE) {
1150 iface->index = libhal_device_get_property_int(ctx, udi,
1151 "net.linux.ifindex", NULL);
1153 ifname = libhal_device_get_property_string(ctx, udi,
1154 "net.interface", NULL);
1155 if (ifname != NULL && ifname_filter != NULL &&
1156 *ifname_filter != '\0' &&
1157 g_str_equal(ifname, ifname_filter) == FALSE) {
1163 iface->type = CONNMAN_IFACE_TYPE_UNKNOWN;
1165 iface->state = CONNMAN_IFACE_STATE_UNKNOWN;
1166 iface->policy = CONNMAN_IFACE_POLICY_UNKNOWN;
1168 err = driver->probe(iface);
1174 __connman_iface_create_identifier(iface);
1176 __connman_iface_init_via_inet(iface);
1178 iface->driver = driver;
1180 __connman_iface_load(iface);
1182 DBG("iface %p network %s secret %s", iface,
1183 iface->network.identifier,
1184 iface->network.passphrase);
1186 conn = libhal_ctx_get_dbus_connection(ctx);
1188 g_dbus_register_object(conn, iface->path, iface, device_free);
1190 interfaces = g_slist_append(interfaces, iface);
1192 if (iface->flags & CONNMAN_IFACE_FLAG_IPV4) {
1193 connman_iface_get_ipv4(iface, &iface->ipv4);
1195 DBG("address %s", inet_ntoa(iface->ipv4.address));
1198 g_dbus_register_interface(conn, iface->path,
1199 CONNMAN_IFACE_INTERFACE,
1200 iface_methods, iface_signals, NULL);
1202 DBG("iface %p identifier %s", iface, iface->identifier);
1204 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
1205 CONNMAN_MANAGER_INTERFACE,
1207 DBUS_TYPE_OBJECT_PATH, &iface->path,
1210 switch_policy(iface);
1215 static void device_added(LibHalContext *ctx, const char *udi)
1219 DBG("ctx %p udi %s", ctx, udi);
1221 for (list = drivers; list; list = list->next) {
1222 struct connman_iface_driver *driver = list->data;
1224 if (driver->capability == NULL)
1227 if (libhal_device_query_capability(ctx, udi,
1228 driver->capability, NULL) == TRUE) {
1229 if (probe_device(ctx, driver, udi) == 0)
1235 static void device_removed(LibHalContext *ctx, const char *udi)
1237 DBusConnection *conn;
1240 DBG("ctx %p udi %s", ctx, udi);
1242 conn = libhal_ctx_get_dbus_connection(ctx);
1244 for (list = interfaces; list; list = list->next) {
1245 struct connman_iface *iface = list->data;
1247 if (strcmp(udi, iface->udi) == 0) {
1248 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
1249 CONNMAN_MANAGER_INTERFACE,
1251 DBUS_TYPE_OBJECT_PATH, &iface->path,
1253 interfaces = g_slist_remove(interfaces, iface);
1254 g_dbus_unregister_interface(conn, iface->path,
1255 CONNMAN_IFACE_INTERFACE);
1256 g_dbus_unregister_object(conn, iface->path);
1262 static void probe_driver(LibHalContext *ctx,
1263 struct connman_iface_driver *driver)
1268 DBG("ctx %p driver %p", ctx, driver);
1270 list = libhal_find_device_by_capability(ctx,
1271 driver->capability, &num, NULL);
1276 probe_device(ctx, driver, *tmp);
1280 libhal_free_string_array(list);
1284 static void find_devices(LibHalContext *ctx)
1290 for (list = drivers; list; list = list->next) {
1291 struct connman_iface_driver *driver = list->data;
1293 DBG("driver %p", driver);
1295 if (driver->capability == NULL)
1298 probe_driver(ctx, driver);
1302 static LibHalContext *hal_ctx = NULL;
1304 static void hal_init(void *data)
1306 DBusConnection *conn = data;
1308 DBG("conn %p", conn);
1310 if (hal_ctx != NULL)
1313 hal_ctx = libhal_ctx_new();
1314 if (hal_ctx == NULL)
1317 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
1318 libhal_ctx_free(hal_ctx);
1322 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
1323 libhal_ctx_free(hal_ctx);
1327 libhal_ctx_set_device_added(hal_ctx, device_added);
1328 libhal_ctx_set_device_removed(hal_ctx, device_removed);
1330 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
1331 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
1333 find_devices(hal_ctx);
1336 static void hal_cleanup(void *data)
1338 DBusConnection *conn = data;
1341 DBG("conn %p", conn);
1343 if (hal_ctx == NULL)
1346 for (list = interfaces; list; list = list->next) {
1347 struct connman_iface *iface = list->data;
1349 DBG("path %s", iface->path);
1351 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
1352 CONNMAN_MANAGER_INTERFACE,
1354 DBUS_TYPE_OBJECT_PATH, &iface->path,
1357 g_dbus_unregister_interface(conn, iface->path,
1358 CONNMAN_IFACE_INTERFACE);
1360 g_dbus_unregister_object(conn, iface->path);
1363 g_slist_free(interfaces);
1367 libhal_ctx_shutdown(hal_ctx, NULL);
1369 libhal_ctx_free(hal_ctx);
1374 static guint hal_watch = 0;
1376 int __connman_iface_init(DBusConnection *conn, const char *interface)
1378 DBG("conn %p", conn);
1380 connection = dbus_connection_ref(conn);
1381 if (connection == NULL)
1384 if (interface != NULL)
1385 ifname_filter = g_strdup(interface);
1387 hal_init(connection);
1389 hal_watch = g_dbus_add_watch(connection, "org.freedesktop.Hal",
1390 hal_init, hal_cleanup, connection, NULL);
1395 void __connman_iface_cleanup(void)
1397 DBG("conn %p", connection);
1399 g_dbus_remove_watch(connection, hal_watch);
1401 hal_cleanup(connection);
1403 g_free(ifname_filter);
1405 dbus_connection_unref(connection);