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 GSList *drivers = NULL;
52 int connman_iface_register(struct connman_iface_driver *driver)
54 DBG("driver %p", driver);
56 drivers = g_slist_append(drivers, driver);
61 void connman_iface_unregister(struct connman_iface_driver *driver)
63 DBG("driver %p", driver);
65 drivers = g_slist_remove(drivers, driver);
68 static GSList *interfaces = NULL;
70 struct connman_iface *__connman_iface_find(int index)
74 for (list = interfaces; list; list = list->next) {
75 struct connman_iface *iface = list->data;
77 if (iface->index == index)
84 void __connman_iface_list(DBusMessageIter *iter)
90 for (list = interfaces; list; list = list->next) {
91 struct connman_iface *iface = list->data;
93 dbus_message_iter_append_basic(iter,
94 DBUS_TYPE_OBJECT_PATH, &iface->path);
98 static void append_entry(DBusMessageIter *dict,
99 const char *key, int type, void *val)
101 DBusMessageIter entry, value;
102 const char *signature;
104 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
107 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
110 case DBUS_TYPE_STRING:
111 signature = DBUS_TYPE_STRING_AS_STRING;
113 case DBUS_TYPE_UINT16:
114 signature = DBUS_TYPE_UINT16_AS_STRING;
117 signature = DBUS_TYPE_VARIANT_AS_STRING;
121 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
123 dbus_message_iter_append_basic(&value, type, val);
124 dbus_message_iter_close_container(&entry, &value);
126 dbus_message_iter_close_container(dict, &entry);
129 static void state_changed(struct connman_iface *iface)
131 const char *str = __connman_iface_state2string(iface->state);
132 enum connman_iface_state state = iface->state;
134 DBG("iface %p state %s", iface, str);
136 g_dbus_emit_signal(connection, iface->path,
137 CONNMAN_IFACE_INTERFACE, "StateChanged",
138 DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
140 switch (iface->state) {
141 case CONNMAN_IFACE_STATE_OFF:
142 __connman_dhcp_release(iface);
145 case CONNMAN_IFACE_STATE_ENABLED:
146 __connman_dhcp_release(iface);
147 connman_iface_clear_ipv4(iface);
148 if (iface->type == CONNMAN_IFACE_TYPE_80211) {
149 state = CONNMAN_IFACE_STATE_SCANNING;
150 if (iface->driver->connect) {
151 iface->driver->connect(iface, NULL);
152 state = CONNMAN_IFACE_STATE_CONNECT;
157 case CONNMAN_IFACE_STATE_CARRIER:
158 if (iface->policy == CONNMAN_IFACE_POLICY_AUTO)
159 state = CONNMAN_IFACE_STATE_CONFIGURE;
162 case CONNMAN_IFACE_STATE_CONFIGURE:
163 __connman_dhcp_request(iface);
166 case CONNMAN_IFACE_STATE_SHUTDOWN:
167 __connman_dhcp_release(iface);
168 if (iface->driver->disconnect)
169 iface->driver->disconnect(iface);
170 if (iface->policy != CONNMAN_IFACE_POLICY_AUTO)
171 state = CONNMAN_IFACE_STATE_OFF;
178 if (iface->state != state) {
179 iface->state = state;
180 state_changed(iface);
184 static void switch_policy(struct connman_iface *iface)
186 DBG("iface %p", iface);
188 switch (iface->policy) {
189 case CONNMAN_IFACE_POLICY_OFF:
190 iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
191 state_changed(iface);
192 connman_iface_clear_ipv4(iface);
193 __connman_iface_down(iface);
196 case CONNMAN_IFACE_POLICY_IGNORE:
199 case CONNMAN_IFACE_POLICY_AUTO:
200 __connman_iface_up(iface);
201 state_changed(iface);
209 void connman_iface_indicate_enabled(struct connman_iface *iface)
211 DBG("iface %p state %d", iface, iface->state);
213 switch (iface->state) {
214 case CONNMAN_IFACE_STATE_OFF:
215 case CONNMAN_IFACE_STATE_CARRIER:
216 iface->state = CONNMAN_IFACE_STATE_ENABLED;
217 state_changed(iface);
224 void connman_iface_indicate_disabled(struct connman_iface *iface)
226 DBG("iface %p state %d", iface, iface->state);
228 iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
229 state_changed(iface);
232 void connman_iface_indicate_connected(struct connman_iface *iface)
234 DBG("iface %p state %d", iface, iface->state);
236 switch (iface->state) {
237 case CONNMAN_IFACE_STATE_CONNECT:
238 iface->state = CONNMAN_IFACE_STATE_CONNECTED;
239 state_changed(iface);
246 void connman_iface_indicate_carrier_on(struct connman_iface *iface)
248 DBG("iface %p state %d", iface, iface->state);
250 switch (iface->state) {
251 case CONNMAN_IFACE_STATE_ENABLED:
252 case CONNMAN_IFACE_STATE_CONNECT:
253 case CONNMAN_IFACE_STATE_CONNECTED:
254 iface->state = CONNMAN_IFACE_STATE_CARRIER;
255 state_changed(iface);
262 void connman_iface_indicate_carrier_off(struct connman_iface *iface)
264 DBG("iface %p state %d", iface, iface->state);
266 switch (iface->state) {
267 case CONNMAN_IFACE_STATE_CARRIER:
268 case CONNMAN_IFACE_STATE_CONFIGURE:
269 case CONNMAN_IFACE_STATE_READY:
270 iface->state = CONNMAN_IFACE_STATE_ENABLED;
271 state_changed(iface);
278 void connman_iface_indicate_configured(struct connman_iface *iface)
280 DBG("iface %p state %d", iface, iface->state);
282 switch (iface->state) {
283 case CONNMAN_IFACE_STATE_CONFIGURE:
284 iface->state = CONNMAN_IFACE_STATE_READY;
285 state_changed(iface);
292 static void append_station(DBusMessage *reply, const char *name)
294 DBusMessageIter array, dict;
296 dbus_message_iter_init_append(reply, &array);
298 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
299 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
300 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
301 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
303 append_entry(&dict, "ESSID", DBUS_TYPE_STRING, &name);
305 dbus_message_iter_close_container(&array, &dict);
308 void connman_iface_indicate_station(struct connman_iface *iface,
313 DBG("iface %p name %s", iface, name);
315 signal = dbus_message_new_signal(iface->path,
316 CONNMAN_IFACE_INTERFACE, "NetworkFound");
320 append_station(signal, name);
322 dbus_connection_send(connection, signal, NULL);
323 dbus_message_unref(signal);
326 int connman_iface_get_ipv4(struct connman_iface *iface,
327 struct connman_ipv4 *ipv4)
334 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
337 DBG("iface %p ipv4 %p", iface, ipv4);
339 memset(&req, 0, sizeof(req));
340 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
341 req.hdr.nlmsg_type = RTM_GETADDR;
342 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
343 req.hdr.nlmsg_pid = 0;
344 req.hdr.nlmsg_seq = 4711;
345 req.msg.rtgen_family = AF_INET;
347 __connman_rtnl_send(&req, sizeof(req));
352 int connman_iface_set_ipv4(struct connman_iface *iface,
353 struct connman_ipv4 *ipv4)
357 struct sockaddr_in *addr;
361 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
364 DBG("iface %p ipv4 %p", iface, ipv4);
366 sk = socket(PF_INET, SOCK_DGRAM, 0);
370 memset(&ifr, 0, sizeof(ifr));
371 ifr.ifr_ifindex = iface->index;
373 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
378 DBG("ifname %s", ifr.ifr_name);
380 addr = (struct sockaddr_in *) &ifr.ifr_addr;
381 addr->sin_family = AF_INET;
382 addr->sin_addr = ipv4->address;
384 err = ioctl(sk, SIOCSIFADDR, &ifr);
387 DBG("address setting failed (%s)", strerror(errno));
389 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
390 addr->sin_family = AF_INET;
391 addr->sin_addr = ipv4->netmask;
393 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
396 DBG("netmask setting failed (%s)", strerror(errno));
398 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
399 addr->sin_family = AF_INET;
400 addr->sin_addr = ipv4->broadcast;
402 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
405 DBG("broadcast setting failed (%s)", strerror(errno));
407 memset(&rt, 0, sizeof(rt));
408 rt.rt_flags = RTF_UP | RTF_GATEWAY;
410 addr = (struct sockaddr_in *) &rt.rt_dst;
411 addr->sin_family = AF_INET;
412 addr->sin_addr.s_addr = INADDR_ANY;
414 addr = (struct sockaddr_in *) &rt.rt_gateway;
415 addr->sin_family = AF_INET;
416 addr->sin_addr = ipv4->gateway;
418 addr = (struct sockaddr_in *) &rt.rt_genmask;
419 addr->sin_family = AF_INET;
420 addr->sin_addr.s_addr = INADDR_ANY;
422 err = ioctl(sk, SIOCADDRT, &rt);
427 DBG("default route failed (%s)", strerror(errno));
431 sprintf(cmd, "echo \"nameserver %s\" | resolvconf -a %s",
432 inet_ntoa(ipv4->nameserver), ifr.ifr_name);
441 int connman_iface_clear_ipv4(struct connman_iface *iface)
444 struct sockaddr_in *addr;
448 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
451 DBG("iface %p", iface);
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.s_addr = INADDR_ANY;
471 //err = ioctl(sk, SIOCDIFADDR, &ifr);
472 err = ioctl(sk, SIOCSIFADDR, &ifr);
476 if (err < 0 && errno != EADDRNOTAVAIL) {
477 DBG("address removal failed (%s)", strerror(errno));
481 sprintf(cmd, "resolvconf -d %s", ifr.ifr_name);
490 static DBusMessage *scan_iface(DBusConnection *conn,
491 DBusMessage *msg, void *data)
493 struct connman_iface *iface = data;
494 struct connman_iface_driver *driver = iface->driver;
497 DBG("conn %p", conn);
499 reply = dbus_message_new_method_return(msg);
506 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
511 static DBusMessage *get_properties(DBusConnection *conn,
512 DBusMessage *msg, void *data)
514 struct connman_iface *iface = data;
516 DBusMessageIter array, dict;
519 DBG("conn %p", conn);
521 reply = dbus_message_new_method_return(msg);
525 dbus_message_iter_init_append(reply, &array);
527 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
528 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
529 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
530 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
532 str = __connman_iface_type2string(iface->type);
533 append_entry(&dict, "Type", DBUS_TYPE_STRING, &str);
535 str = __connman_iface_state2string(iface->state);
536 append_entry(&dict, "State", DBUS_TYPE_STRING, &str);
538 if (iface->type == CONNMAN_IFACE_TYPE_80211) {
539 dbus_uint16_t signal = 75;
540 append_entry(&dict, "Signal", DBUS_TYPE_UINT16, &signal);
543 str = __connman_iface_policy2string(iface->policy);
544 append_entry(&dict, "Policy", DBUS_TYPE_STRING, &str);
546 if (iface->device.driver != NULL)
547 append_entry(&dict, "Driver",
548 DBUS_TYPE_STRING, &iface->device.driver);
550 if (iface->device.vendor != NULL)
551 append_entry(&dict, "Vendor",
552 DBUS_TYPE_STRING, &iface->device.vendor);
554 if (iface->device.product != NULL)
555 append_entry(&dict, "Product",
556 DBUS_TYPE_STRING, &iface->device.product);
558 dbus_message_iter_close_container(&array, &dict);
563 static DBusMessage *get_state(DBusConnection *conn,
564 DBusMessage *msg, void *data)
566 struct connman_iface *iface = data;
570 DBG("conn %p", conn);
572 reply = dbus_message_new_method_return(msg);
576 state = __connman_iface_state2string(iface->state);
578 dbus_message_append_args(reply, DBUS_TYPE_STRING, &state,
584 static DBusMessage *get_signal(DBusConnection *conn,
585 DBusMessage *msg, void *data)
587 struct connman_iface *iface = data;
589 dbus_uint16_t signal;
591 DBG("conn %p", conn);
593 reply = dbus_message_new_method_return(msg);
597 if (iface->type == CONNMAN_IFACE_TYPE_80211)
602 dbus_message_append_args(reply, DBUS_TYPE_UINT16, &signal,
608 static DBusMessage *get_policy(DBusConnection *conn,
609 DBusMessage *msg, void *data)
611 struct connman_iface *iface = data;
615 DBG("conn %p", conn);
617 reply = dbus_message_new_method_return(msg);
621 policy = __connman_iface_policy2string(iface->policy);
623 dbus_message_append_args(reply, DBUS_TYPE_STRING, &policy,
629 static DBusMessage *set_policy(DBusConnection *conn,
630 DBusMessage *msg, void *data)
632 struct connman_iface *iface = data;
634 enum connman_iface_policy new_policy;
637 DBG("conn %p", conn);
639 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &policy,
642 new_policy = __connman_iface_string2policy(policy);
644 reply = dbus_message_new_method_return(msg);
648 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
650 if (iface->policy != new_policy) {
651 iface->policy = new_policy;
652 __connman_iface_store(iface);
654 switch_policy(iface);
655 policy = __connman_iface_policy2string(new_policy);
657 g_dbus_emit_signal(conn, iface->path, CONNMAN_IFACE_INTERFACE,
658 "PolicyChanged", DBUS_TYPE_STRING, &policy,
665 static void append_network(DBusMessage *reply,
666 struct connman_iface *iface, gboolean secrets)
668 DBusMessageIter array, dict;
670 dbus_message_iter_init_append(reply, &array);
672 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
673 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
674 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
675 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
677 switch (iface->type) {
678 case CONNMAN_IFACE_TYPE_80211:
679 if (iface->network.essid != NULL)
680 append_entry(&dict, "ESSID",
681 DBUS_TYPE_STRING, &iface->network.essid);
682 if (secrets == TRUE && iface->network.psk != NULL)
683 append_entry(&dict, "PSK",
684 DBUS_TYPE_STRING, &iface->network.psk);
690 dbus_message_iter_close_container(&array, &dict);
693 static DBusMessage *get_network(DBusConnection *conn,
694 DBusMessage *msg, void *data)
696 struct connman_iface *iface = data;
699 DBG("conn %p", conn);
701 reply = dbus_message_new_method_return(msg);
705 append_network(reply, iface, TRUE);
710 static DBusMessage *set_network(DBusConnection *conn,
711 DBusMessage *msg, void *data)
713 struct connman_iface *iface = data;
714 DBusMessage *reply, *signal;
715 DBusMessageIter array, dict;
716 gboolean changed = FALSE;
718 DBG("conn %p", conn);
720 dbus_message_iter_init(msg, &array);
722 dbus_message_iter_recurse(&array, &dict);
724 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
725 DBusMessageIter entry, value;
726 const char *key, *val;
728 dbus_message_iter_recurse(&dict, &entry);
729 dbus_message_iter_get_basic(&entry, &key);
731 dbus_message_iter_next(&entry);
733 dbus_message_iter_recurse(&entry, &value);
735 //type = dbus_message_iter_get_arg_type(&value);
736 dbus_message_iter_get_basic(&value, &val);
738 if (g_strcasecmp(key, "ESSID") == 0) {
739 g_free(iface->network.essid);
740 iface->network.essid = g_strdup(val);
741 if (iface->driver->set_network)
742 iface->driver->set_network(iface, val);
746 if (g_strcasecmp(key, "PSK") == 0) {
747 g_free(iface->network.psk);
748 iface->network.psk = g_strdup(val);
749 if (iface->driver->set_network)
750 iface->driver->set_passphrase(iface, val);
754 dbus_message_iter_next(&dict);
757 reply = dbus_message_new_method_return(msg);
761 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
763 if (changed == TRUE) {
764 __connman_iface_store(iface);
766 signal = dbus_message_new_signal(iface->path,
767 CONNMAN_IFACE_INTERFACE, "NetworkChanged");
768 if (signal != NULL) {
769 append_network(signal, iface, FALSE);
770 dbus_connection_send(conn, signal, NULL);
771 dbus_message_unref(signal);
778 static void append_ipv4(DBusMessage *reply, struct connman_iface *iface)
780 DBusMessageIter array, dict;
783 dbus_message_iter_init_append(reply, &array);
785 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
786 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
787 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
788 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
790 str = __connman_ipv4_method2string(iface->ipv4.method);
791 append_entry(&dict, "Method", DBUS_TYPE_STRING, &str);
793 if (iface->ipv4.address.s_addr != INADDR_ANY) {
794 str = inet_ntoa(iface->ipv4.address);
795 append_entry(&dict, "Address", DBUS_TYPE_STRING, &str);
798 if (iface->ipv4.netmask.s_addr != INADDR_ANY) {
799 str = inet_ntoa(iface->ipv4.netmask);
800 append_entry(&dict, "Netmask", DBUS_TYPE_STRING, &str);
803 if (iface->ipv4.gateway.s_addr != INADDR_ANY) {
804 str = inet_ntoa(iface->ipv4.gateway);
805 append_entry(&dict, "Gateway", DBUS_TYPE_STRING, &str);
808 dbus_message_iter_close_container(&array, &dict);
811 static DBusMessage *get_ipv4(DBusConnection *conn,
812 DBusMessage *msg, void *data)
814 struct connman_iface *iface = data;
817 DBG("conn %p", conn);
819 reply = dbus_message_new_method_return(msg);
823 append_ipv4(reply, iface);
828 static DBusMessage *set_ipv4(DBusConnection *conn,
829 DBusMessage *msg, void *data)
831 struct connman_iface *iface = data;
832 DBusMessage *reply, *signal;
833 DBusMessageIter array, dict;
834 gboolean changed = FALSE;
836 DBG("conn %p", conn);
838 dbus_message_iter_init(msg, &array);
840 dbus_message_iter_recurse(&array, &dict);
842 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
843 DBusMessageIter entry, value;
844 const char *key, *val;
845 enum connman_ipv4_method method;
848 dbus_message_iter_recurse(&dict, &entry);
849 dbus_message_iter_get_basic(&entry, &key);
851 dbus_message_iter_next(&entry);
853 dbus_message_iter_recurse(&entry, &value);
855 //type = dbus_message_iter_get_arg_type(&value);
856 dbus_message_iter_get_basic(&value, &val);
858 if (g_strcasecmp(key, "Method") == 0) {
859 method = __connman_ipv4_string2method(val);
860 if (iface->ipv4.method != method) {
861 iface->ipv4.method = method;
866 if (g_strcasecmp(key, "Address") == 0) {
867 addr = inet_addr(val);
868 if (iface->ipv4.address.s_addr != addr) {
869 iface->ipv4.address.s_addr = addr;
874 if (g_strcasecmp(key, "Netmask") == 0) {
875 addr = inet_addr(val);
876 if (iface->ipv4.netmask.s_addr != addr) {
877 iface->ipv4.netmask.s_addr = addr;
882 if (g_strcasecmp(key, "Gateway") == 0) {
883 addr = inet_addr(val);
884 if (iface->ipv4.gateway.s_addr != addr) {
885 iface->ipv4.gateway.s_addr = addr;
890 dbus_message_iter_next(&dict);
893 reply = dbus_message_new_method_return(msg);
897 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
899 if (changed == TRUE) {
900 __connman_iface_store(iface);
902 signal = dbus_message_new_signal(iface->path,
903 CONNMAN_IFACE_INTERFACE, "IPv4Changed");
904 if (signal != NULL) {
905 append_ipv4(signal, iface);
906 dbus_connection_send(conn, signal, NULL);
907 dbus_message_unref(signal);
914 static GDBusMethodTable iface_methods[] = {
915 { "Scan", "", "", scan_iface },
916 { "GetProperties", "", "a{sv}", get_properties },
917 { "GetState", "", "s", get_state },
918 { "GetSignal", "", "q", get_signal },
919 { "GetPolicy", "", "s", get_policy },
920 { "SetPolicy", "s", "", set_policy },
921 { "GetNetwork", "", "a{sv}", get_network },
922 { "SetNetwork", "a{sv}", "", set_network },
923 { "GetIPv4", "", "a{sv}", get_ipv4 },
924 { "SetIPv4", "a{sv}", "", set_ipv4 },
928 static GDBusSignalTable iface_signals[] = {
929 { "StateChanged", "s" },
930 { "SignalChanged", "q" },
931 { "PolicyChanged", "s" },
932 { "NetworkFound", "a{sv}" },
933 { "NetworkChanged", "a{sv}" },
934 { "IPv4Changed", "a{sv}" },
938 static void device_free(void *data)
940 struct connman_iface *iface = data;
942 DBG("iface %p", iface);
944 connman_iface_clear_ipv4(iface);
946 if (iface->driver && iface->driver->remove)
947 iface->driver->remove(iface);
951 g_free(iface->sysfs);
952 g_free(iface->identifier);
953 g_free(iface->network.essid);
954 g_free(iface->device.driver);
955 g_free(iface->device.vendor);
956 g_free(iface->device.product);
960 static void detect_device_info(LibHalContext *ctx, struct connman_iface *iface)
962 char *parent, *subsys, *value;
964 parent = libhal_device_get_property_string(ctx, iface->udi,
965 "info.parent", NULL);
967 subsys = libhal_device_get_property_string(ctx, iface->udi,
968 "linux.subsystem", NULL);
970 value = libhal_device_get_property_string(ctx, iface->udi,
971 "info.linux.driver", NULL);
973 value = libhal_device_get_property_string(ctx, parent,
974 "info.linux.driver", NULL);
976 iface->device.driver = g_strdup(value);
979 if (strcmp(subsys, "net") == 0) {
980 value = libhal_device_get_property_string(ctx, parent,
981 "info.vendor", NULL);
983 iface->device.vendor = g_strdup(value);
985 value = libhal_device_get_property_string(ctx, parent,
986 "info.product", NULL);
988 iface->device.product = g_strdup(value);
992 static int probe_device(LibHalContext *ctx,
993 struct connman_iface_driver *driver, const char *udi)
995 DBusConnection *conn;
996 struct connman_iface *iface;
1000 DBG("ctx %p driver %p udi %s", ctx, driver, udi);
1005 iface = g_try_new0(struct connman_iface, 1);
1009 temp = g_path_get_basename(udi);
1010 iface->path = g_strdup_printf("%s/%s", CONNMAN_IFACE_BASEPATH, temp);
1013 iface->udi = g_strdup(udi);
1015 DBG("iface %p path %s", iface, iface->path);
1017 sysfs = libhal_device_get_property_string(ctx, udi,
1018 "linux.sysfs_path", NULL);
1020 iface->sysfs = g_strdup(sysfs);
1022 detect_device_info(ctx, iface);
1026 if (g_str_has_prefix(driver->capability, "net") == TRUE)
1027 iface->index = libhal_device_get_property_int(ctx, udi,
1028 "net.linux.ifindex", NULL);
1030 iface->type = CONNMAN_IFACE_TYPE_UNKNOWN;
1032 iface->state = CONNMAN_IFACE_STATE_UNKNOWN;
1033 iface->policy = CONNMAN_IFACE_POLICY_UNKNOWN;
1035 err = driver->probe(iface);
1041 __connman_iface_create_identifier(iface);
1043 __connman_iface_init_via_inet(iface);
1045 iface->driver = driver;
1047 __connman_iface_load(iface);
1049 conn = libhal_ctx_get_dbus_connection(ctx);
1051 g_dbus_register_object(conn, iface->path, iface, device_free);
1053 interfaces = g_slist_append(interfaces, iface);
1055 if (iface->flags & CONNMAN_IFACE_FLAG_IPV4) {
1056 connman_iface_get_ipv4(iface, &iface->ipv4);
1058 DBG("address %s", inet_ntoa(iface->ipv4.address));
1061 g_dbus_register_interface(conn, iface->path,
1062 CONNMAN_IFACE_INTERFACE,
1063 iface_methods, iface_signals, NULL);
1065 DBG("iface %p identifier %s", iface, iface->identifier);
1067 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
1068 CONNMAN_MANAGER_INTERFACE,
1070 DBUS_TYPE_OBJECT_PATH, &iface->path,
1073 switch_policy(iface);
1078 static void device_added(LibHalContext *ctx, const char *udi)
1082 DBG("ctx %p udi %s", ctx, udi);
1084 for (list = drivers; list; list = list->next) {
1085 struct connman_iface_driver *driver = list->data;
1087 if (driver->capability == NULL)
1090 if (libhal_device_query_capability(ctx, udi,
1091 driver->capability, NULL) == TRUE) {
1092 if (probe_device(ctx, driver, udi) == 0)
1098 static void device_removed(LibHalContext *ctx, const char *udi)
1100 DBusConnection *conn;
1103 DBG("ctx %p udi %s", ctx, udi);
1105 conn = libhal_ctx_get_dbus_connection(ctx);
1107 for (list = interfaces; list; list = list->next) {
1108 struct connman_iface *iface = list->data;
1110 if (strcmp(udi, iface->udi) == 0) {
1111 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
1112 CONNMAN_MANAGER_INTERFACE,
1114 DBUS_TYPE_OBJECT_PATH, &iface->path,
1116 interfaces = g_slist_remove(interfaces, iface);
1117 g_dbus_unregister_interface(conn, iface->path,
1118 CONNMAN_IFACE_INTERFACE);
1119 g_dbus_unregister_object(conn, iface->path);
1125 static void probe_driver(LibHalContext *ctx,
1126 struct connman_iface_driver *driver)
1131 DBG("ctx %p driver %p", ctx, driver);
1133 list = libhal_find_device_by_capability(ctx,
1134 driver->capability, &num, NULL);
1139 probe_device(ctx, driver, *tmp);
1143 libhal_free_string_array(list);
1147 static void find_devices(LibHalContext *ctx)
1153 for (list = drivers; list; list = list->next) {
1154 struct connman_iface_driver *driver = list->data;
1156 DBG("driver %p", driver);
1158 if (driver->capability == NULL)
1161 probe_driver(ctx, driver);
1165 static LibHalContext *hal_ctx = NULL;
1167 static void hal_init(void *data)
1169 DBusConnection *conn = data;
1171 DBG("conn %p", conn);
1173 if (hal_ctx != NULL)
1176 hal_ctx = libhal_ctx_new();
1177 if (hal_ctx == NULL)
1180 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
1181 libhal_ctx_free(hal_ctx);
1185 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
1186 libhal_ctx_free(hal_ctx);
1190 libhal_ctx_set_device_added(hal_ctx, device_added);
1191 libhal_ctx_set_device_removed(hal_ctx, device_removed);
1193 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
1194 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
1196 find_devices(hal_ctx);
1199 static void hal_cleanup(void *data)
1201 DBusConnection *conn = data;
1204 DBG("conn %p", conn);
1206 if (hal_ctx == NULL)
1209 for (list = interfaces; list; list = list->next) {
1210 struct connman_iface *iface = list->data;
1212 DBG("path %s", iface->path);
1214 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
1215 CONNMAN_MANAGER_INTERFACE,
1217 DBUS_TYPE_OBJECT_PATH, &iface->path,
1220 g_dbus_unregister_interface(conn, iface->path,
1221 CONNMAN_IFACE_INTERFACE);
1223 g_dbus_unregister_object(conn, iface->path);
1226 g_slist_free(interfaces);
1230 libhal_ctx_shutdown(hal_ctx, NULL);
1232 libhal_ctx_free(hal_ctx);
1237 static guint hal_watch = 0;
1239 int __connman_iface_init(DBusConnection *conn)
1241 DBG("conn %p", conn);
1243 connection = dbus_connection_ref(conn);
1244 if (connection == NULL)
1247 hal_init(connection);
1249 hal_watch = g_dbus_add_watch(connection, "org.freedesktop.Hal",
1250 hal_init, hal_cleanup, connection, NULL);
1255 void __connman_iface_cleanup(void)
1257 DBG("conn %p", connection);
1259 g_dbus_remove_watch(connection, hal_watch);
1261 hal_cleanup(connection);
1263 dbus_connection_unref(connection);