fix ipv4 info retrieving, add ethernet.
[framework/uifw/edbus.git] / src / lib / connman / e_connman.c
1 #include "e_connman_private.h"
2 #include <stdlib.h>
3 #include <string.h>
4
5 static E_DBus_Signal_Handler *cb_name_owner_changed = NULL;
6 static DBusPendingCall *pending_get_name_owner = NULL;
7 static unsigned int init_count = 0;
8 static char *unique_name = NULL;
9
10 static const char bus_name[] = "org.moblin.connman";
11 static const char fdo_bus_name[] = "org.freedesktop.DBus";
12 static const char fdo_interface[] = "org.freedesktop.DBus";
13 static const char fdo_path[] = "/org/freedesktop/DBus";
14
15 E_DBus_Connection *e_connman_conn = NULL;
16
17 EAPI int E_CONNMAN_EVENT_MANAGER_IN = 0;
18 EAPI int E_CONNMAN_EVENT_MANAGER_OUT = 0;
19 EAPI int E_CONNMAN_EVENT_ELEMENT_ADD = 0;
20 EAPI int E_CONNMAN_EVENT_ELEMENT_DEL = 0;
21 EAPI int E_CONNMAN_EVENT_ELEMENT_UPDATED = 0;
22
23 const char *e_connman_iface_manager = NULL;
24 const char *e_connman_iface_network = NULL;
25 const char *e_connman_iface_profile = NULL;
26 const char *e_connman_iface_service = NULL;
27 const char *e_connman_iface_device = NULL;
28 const char *e_connman_iface_connection = NULL;
29
30 const char *e_connman_prop_available = NULL;
31 const char *e_connman_prop_connected = NULL;
32 const char *e_connman_prop_connections = NULL;
33 const char *e_connman_prop_default = NULL;
34 const char *e_connman_prop_device = NULL;
35 const char *e_connman_prop_devices = NULL;
36 const char *e_connman_prop_interface = NULL;
37 const char *e_connman_prop_ipv4 = NULL;
38 const char *e_connman_prop_ipv4_configuration = NULL;
39 const char *e_connman_prop_ethernet = NULL;
40 const char *e_connman_prop_method = NULL;
41 const char *e_connman_prop_address = NULL;
42 const char *e_connman_prop_gateway = NULL;
43 const char *e_connman_prop_netmask = NULL;
44 const char *e_connman_prop_mtu = NULL;
45 const char *e_connman_prop_name = NULL;
46 const char *e_connman_prop_network = NULL;
47 const char *e_connman_prop_networks = NULL;
48 const char *e_connman_prop_offline_mode = NULL;
49 const char *e_connman_prop_policy = NULL;
50 const char *e_connman_prop_powered = NULL;
51 const char *e_connman_prop_priority = NULL;
52 const char *e_connman_prop_profiles = NULL;
53 const char *e_connman_prop_profile_active = NULL;
54 const char *e_connman_prop_services = NULL;
55 const char *e_connman_prop_remember = NULL;
56 const char *e_connman_prop_scan_interval = NULL;
57 const char *e_connman_prop_scanning = NULL;
58 const char *e_connman_prop_state = NULL;
59 const char *e_connman_prop_strength = NULL;
60 const char *e_connman_prop_frequency = NULL;
61 const char *e_connman_prop_type = NULL;
62 const char *e_connman_prop_wifi_mode = NULL;
63 const char *e_connman_prop_wifi_passphrase = NULL;
64 const char *e_connman_prop_wifi_security = NULL;
65 const char *e_connman_prop_wifi_ssid = NULL;
66 const char *e_connman_prop_wifi_channel = NULL;
67 const char *e_connman_prop_wifi_eap = NULL;
68 const char *e_connman_prop_error = NULL;
69 const char *e_connman_prop_mode = NULL;
70 const char *e_connman_prop_security = NULL;
71 const char *e_connman_prop_passphrase = NULL;
72 const char *e_connman_prop_passphrase_required = NULL;
73 const char *e_connman_prop_favorite = NULL;
74 const char *e_connman_prop_immutable = NULL;
75 const char *e_connman_prop_auto_connect = NULL;
76 const char *e_connman_prop_setup_required = NULL;
77 const char *e_connman_prop_apn = NULL;
78 const char *e_connman_prop_mcc = NULL;
79 const char *e_connman_prop_mnc = NULL;
80 const char *e_connman_prop_roaming = NULL;
81 const char *e_connman_prop_technology_default = NULL;
82 const char *e_connman_prop_technologies_available = NULL;
83 const char *e_connman_prop_technologies_enabled= NULL;
84 const char *e_connman_prop_technologies_connected = NULL;
85
86
87 int _e_dbus_connman_log_dom = -1;
88
89 const char *
90 e_connman_system_bus_name_get(void)
91 {
92    return unique_name ? unique_name : bus_name;
93 }
94
95
96 /***********************************************************************
97  * Manager
98  ***********************************************************************/
99
100 /**
101  * Synchronize elements with server.
102  *
103  * This will call Manager.GetProperties() on server, retrieve properties
104  * and some element paths and then request their properties.
105  *
106  * This call will add events E_CONNMAN_EVENT_ELEMENT_ADD and
107  * E_CONNMAN_EVENT_ELEMENT_UPDATED to the main loop.
108  *
109  * This will not remove stale elements.
110  *
111  * @return 1 on success, 0 otherwise.
112  */
113 bool
114 e_connman_manager_sync_elements(void)
115 {
116    E_Connman_Element *manager;
117
118    if (!unique_name)
119      return FALSE;
120    manager = e_connman_element_register(manager_path, e_connman_iface_manager);
121    if (manager)
122      e_connman_element_properties_sync(manager);
123    else
124      return FALSE;
125
126    DBG("sync_manager: %s (%s)", unique_name, bus_name);
127
128    return TRUE;
129 }
130
131 static void
132 _e_connman_system_name_owner_exit(void)
133 {
134    e_connman_manager_clear_elements();
135    ecore_event_add(E_CONNMAN_EVENT_MANAGER_OUT, NULL, NULL, NULL);
136
137    free(unique_name);
138    unique_name = NULL;
139 }
140
141 static void
142 _e_connman_system_name_owner_enter(const char *uid)
143 {
144    DBG("enter connman at %s (old was %s)", uid, unique_name);
145    if (unique_name && strcmp(unique_name, uid) == 0)
146      {
147         DBG("same unique_name for connman, ignore.");
148         return;
149      }
150
151    if (unique_name)
152      _e_connman_system_name_owner_exit();
153
154    unique_name = strdup(uid);
155
156    ecore_event_add(E_CONNMAN_EVENT_MANAGER_IN, NULL, NULL, NULL);
157    e_connman_manager_sync_elements();
158 }
159
160 static void
161 _e_connman_system_name_owner_changed(void *data __UNUSED__, DBusMessage *msg)
162 {
163    DBusError err;
164    const char *name, *from, *to;
165
166    dbus_error_init(&err);
167    if (!dbus_message_get_args(msg, &err,
168                               DBUS_TYPE_STRING, &name,
169                               DBUS_TYPE_STRING, &from,
170                               DBUS_TYPE_STRING, &to,
171                               DBUS_TYPE_INVALID))
172      {
173         ERR("could not get NameOwnerChanged arguments: %s: %s",
174             err.name, err.message);
175         dbus_error_free(&err);
176         return;
177      }
178
179    if (strcmp(name, bus_name) != 0)
180      return;
181
182    DBG("NameOwnerChanged from=[%s] to=[%s]", from, to);
183
184    if (from[0] == '\0' && to[0] != '\0')
185      _e_connman_system_name_owner_enter(to);
186    else if (from[0] != '\0' && to[0] == '\0')
187      {
188         DBG("exit connman at %s", from);
189         if (strcmp(unique_name, from) != 0)
190           DBG("%s was not the known name %s, ignored.", from, unique_name);
191         else
192           _e_connman_system_name_owner_exit();
193      }
194    else
195      DBG("unknow change from %s to %s", from, to);
196 }
197
198 static void
199 _e_connman_get_name_owner(void *data __UNUSED__, DBusMessage *msg, DBusError *err)
200 {
201    DBusMessageIter itr;
202    int t;
203    const char *uid;
204
205    pending_get_name_owner = NULL;
206
207    if (!_dbus_callback_check_and_init(msg, &itr, err))
208      return;
209
210    t = dbus_message_iter_get_arg_type(&itr);
211    if (!_dbus_iter_type_check(t, DBUS_TYPE_STRING))
212      return;
213
214    dbus_message_iter_get_basic(&itr, &uid);
215    if (!uid)
216      {
217         ERR("no name owner!");
218         return;
219      }
220
221    _e_connman_system_name_owner_enter(uid);
222    return;
223 }
224
225 /**
226  * Initialize E Connection Manager (E_Connman) system.
227  *
228  * This will connect and watch org.moblin.connman.Manager and Element
229  * events and translate to Ecore main loop events, also provide a
230  * proxy for method invocation on server.
231  *
232  * Interesting events are:
233  *   - E_CONNMAN_EVENT_MANAGER_IN: issued when connman is avaiable.
234  *   - E_CONNMAN_EVENT_MANAGER_OUT: issued when connman connection is lost.
235  *   - E_CONNMAN_EVENT_ELEMENT_ADD: element was added.
236  *   - E_CONNMAN_EVENT_ELEMENT_DEL: element was deleted.
237  *   - E_CONNMAN_EVENT_ELEMENT_UPDATED: element was updated (properties
238  *     or state changed).
239  *
240  * Manager IN/OUT events do not provide any event information, just
241  * tells you that system is usable or not. After manager is out, all
242  * elements will be removed, so after this event do not use the system anymore.
243  *
244  * Element events will give you an element object. After DEL event callback
245  * returns, that element will not be valid anymore.
246  */
247 unsigned int
248 e_connman_system_init(E_DBus_Connection *edbus_conn)
249 {
250    init_count++;
251
252    if (init_count > 1)
253      return init_count;
254
255    _e_dbus_connman_log_dom = eina_log_domain_register
256      ("e_dbus_connman", EINA_LOG_DEFAULT_COLOR);
257
258    if(_e_dbus_connman_log_dom < 0)
259      {
260         EINA_LOG_ERR
261           ("impossible to create a log domain for edbus_connman module");
262         return -1;
263      }
264
265    if (E_CONNMAN_EVENT_MANAGER_IN == 0)
266      E_CONNMAN_EVENT_MANAGER_IN = ecore_event_type_new();
267    if (E_CONNMAN_EVENT_MANAGER_OUT == 0)
268      E_CONNMAN_EVENT_MANAGER_OUT = ecore_event_type_new();
269    if (E_CONNMAN_EVENT_ELEMENT_ADD == 0)
270      E_CONNMAN_EVENT_ELEMENT_ADD = ecore_event_type_new();
271    if (E_CONNMAN_EVENT_ELEMENT_DEL == 0)
272      E_CONNMAN_EVENT_ELEMENT_DEL = ecore_event_type_new();
273    if (E_CONNMAN_EVENT_ELEMENT_UPDATED == 0)
274      E_CONNMAN_EVENT_ELEMENT_UPDATED = ecore_event_type_new();
275
276    if (e_connman_iface_manager == NULL)
277      e_connman_iface_manager = eina_stringshare_add("org.moblin.connman.Manager");
278    if (e_connman_iface_network == NULL)
279      e_connman_iface_network = eina_stringshare_add("org.moblin.connman.Network");
280    if (e_connman_iface_profile == NULL)
281      e_connman_iface_profile = eina_stringshare_add("org.moblin.connman.Profile");
282    if (e_connman_iface_service == NULL)
283      e_connman_iface_service = eina_stringshare_add("org.moblin.connman.Service");
284    if (e_connman_iface_device == NULL)
285      e_connman_iface_device = eina_stringshare_add("org.moblin.connman.Device");
286    if (e_connman_iface_connection == NULL)
287      e_connman_iface_connection = eina_stringshare_add("org.moblin.connman.Connection");
288
289    if (e_connman_prop_available == NULL)
290      e_connman_prop_available = eina_stringshare_add("Available");
291    if (e_connman_prop_connected == NULL)
292      e_connman_prop_connected = eina_stringshare_add("Connected");
293    if (e_connman_prop_connections == NULL)
294      e_connman_prop_connections = eina_stringshare_add("Connections");
295    if (e_connman_prop_default == NULL)
296      e_connman_prop_default = eina_stringshare_add("Default");
297    if (e_connman_prop_device == NULL)
298      e_connman_prop_device = eina_stringshare_add("Device");
299    if (e_connman_prop_devices == NULL)
300      e_connman_prop_devices = eina_stringshare_add("Devices");
301    if (e_connman_prop_interface == NULL)
302      e_connman_prop_interface = eina_stringshare_add("Interface");
303    if (e_connman_prop_ipv4 == NULL)
304      e_connman_prop_ipv4 = eina_stringshare_add("IPv4");
305    if (e_connman_prop_ipv4_configuration == NULL)
306      e_connman_prop_ipv4_configuration = eina_stringshare_add("IPv4.Configuration");
307    if (e_connman_prop_ethernet == NULL)
308      e_connman_prop_ethernet = eina_stringshare_add("Ethernet");
309    if (e_connman_prop_method == NULL)
310      e_connman_prop_method = eina_stringshare_add("Method");
311    if (e_connman_prop_address == NULL)
312      e_connman_prop_address = eina_stringshare_add("Address");
313    if (e_connman_prop_gateway == NULL)
314      e_connman_prop_gateway = eina_stringshare_add("Gateway");
315    if (e_connman_prop_netmask == NULL)
316      e_connman_prop_netmask = eina_stringshare_add("Netmask");
317    if (e_connman_prop_mtu == NULL)
318      e_connman_prop_mtu = eina_stringshare_add("MTU");
319    if (e_connman_prop_name == NULL)
320      e_connman_prop_name = eina_stringshare_add("Name");
321    if (e_connman_prop_network == NULL)
322      e_connman_prop_network = eina_stringshare_add("Network");
323    if (e_connman_prop_networks == NULL)
324      e_connman_prop_networks = eina_stringshare_add("Networks");
325    if (e_connman_prop_offline_mode == NULL)
326      e_connman_prop_offline_mode = eina_stringshare_add("OfflineMode");
327    if (e_connman_prop_policy == NULL)
328      e_connman_prop_policy = eina_stringshare_add("Policy");
329    if (e_connman_prop_powered == NULL)
330      e_connman_prop_powered = eina_stringshare_add("Powered");
331    if (e_connman_prop_priority == NULL)
332      e_connman_prop_priority = eina_stringshare_add("Priority");
333    if (e_connman_prop_profiles == NULL)
334      e_connman_prop_profiles = eina_stringshare_add("Profiles");
335    if (e_connman_prop_profile_active == NULL)
336      e_connman_prop_profile_active = eina_stringshare_add("ActiveProfile");
337    if (e_connman_prop_services == NULL)
338      e_connman_prop_services = eina_stringshare_add("Services");
339    if (e_connman_prop_remember == NULL)
340      e_connman_prop_remember = eina_stringshare_add("Remember");
341    if (e_connman_prop_scan_interval == NULL)
342      e_connman_prop_scan_interval = eina_stringshare_add("ScanInterval");
343    if (e_connman_prop_scanning == NULL)
344      e_connman_prop_scanning = eina_stringshare_add("Scanning");
345    if (e_connman_prop_state == NULL)
346      e_connman_prop_state = eina_stringshare_add("State");
347    if (e_connman_prop_strength == NULL)
348      e_connman_prop_strength = eina_stringshare_add("Strength");
349    if (e_connman_prop_frequency == NULL)
350      e_connman_prop_frequency = eina_stringshare_add("Frequency");
351    if (e_connman_prop_type == NULL)
352      e_connman_prop_type = eina_stringshare_add("Type");
353    if (e_connman_prop_wifi_mode == NULL)
354      e_connman_prop_wifi_mode = eina_stringshare_add("WiFi.Mode");
355    if (e_connman_prop_wifi_passphrase == NULL)
356      e_connman_prop_wifi_passphrase = eina_stringshare_add("WiFi.Passphrase");
357    if (e_connman_prop_wifi_security == NULL)
358      e_connman_prop_wifi_security = eina_stringshare_add("WiFi.Security");
359    if (e_connman_prop_wifi_ssid == NULL)
360      e_connman_prop_wifi_ssid = eina_stringshare_add("WiFi.SSID");
361    if (e_connman_prop_wifi_channel == NULL)
362      e_connman_prop_wifi_channel = eina_stringshare_add("WiFi.Channel");
363    if (e_connman_prop_wifi_eap == NULL)
364      e_connman_prop_wifi_eap = eina_stringshare_add("WiFi.EAP");
365    if (e_connman_prop_error == NULL)
366      e_connman_prop_error = eina_stringshare_add("Error");
367    if (e_connman_prop_mode == NULL)
368      e_connman_prop_mode = eina_stringshare_add("Mode");
369    if (e_connman_prop_security == NULL)
370      e_connman_prop_security = eina_stringshare_add("Security");
371    if (e_connman_prop_passphrase == NULL)
372      e_connman_prop_passphrase = eina_stringshare_add("Passphrase");
373    if (e_connman_prop_passphrase_required == NULL)
374      e_connman_prop_passphrase_required = eina_stringshare_add("PassphraseRequired");
375    if (e_connman_prop_favorite == NULL)
376      e_connman_prop_favorite = eina_stringshare_add("Favorite");
377    if (e_connman_prop_immutable == NULL)
378      e_connman_prop_immutable = eina_stringshare_add("Immutable");
379    if (e_connman_prop_auto_connect == NULL)
380      e_connman_prop_auto_connect = eina_stringshare_add("AutoConnect");
381    if (e_connman_prop_setup_required == NULL)
382      e_connman_prop_setup_required = eina_stringshare_add("SetupRequired");
383    if (e_connman_prop_apn == NULL)
384      e_connman_prop_apn = eina_stringshare_add("APN");
385    if (e_connman_prop_mcc == NULL)
386      e_connman_prop_mcc = eina_stringshare_add("MCC");
387    if (e_connman_prop_mnc == NULL)
388      e_connman_prop_mnc = eina_stringshare_add("MCN");
389    if (e_connman_prop_roaming == NULL)
390      e_connman_prop_roaming = eina_stringshare_add("Roaming");
391    if (e_connman_prop_technology_default == NULL)
392      e_connman_prop_technology_default = eina_stringshare_add("DefaultTechnology");
393    if (e_connman_prop_technologies_available == NULL)
394      e_connman_prop_technologies_available = eina_stringshare_add("AvailableTechnologies");
395    if (e_connman_prop_technologies_enabled == NULL)
396      e_connman_prop_technologies_enabled = eina_stringshare_add("EnabledTechnologies");
397    if (e_connman_prop_technologies_connected == NULL)
398      e_connman_prop_technologies_connected = eina_stringshare_add("ConnectedTechnologies");
399
400    e_connman_conn = edbus_conn;
401    cb_name_owner_changed = e_dbus_signal_handler_add
402      (e_connman_conn, fdo_bus_name, fdo_path, fdo_interface, "NameOwnerChanged",
403       _e_connman_system_name_owner_changed, NULL);
404
405    if (pending_get_name_owner)
406      dbus_pending_call_cancel(pending_get_name_owner);
407
408    pending_get_name_owner = e_dbus_get_name_owner
409      (e_connman_conn, bus_name, _e_connman_get_name_owner, NULL);
410
411    e_connman_elements_init();
412
413    return init_count;
414 }
415
416 static inline void
417 _stringshare_del(const char **str)
418 {
419    if (!*str)
420      return;
421    eina_stringshare_del(*str);
422    *str = NULL;
423 }
424
425 /**
426  * Shutdown connman system.
427  *
428  * When count drops to 0 resources will be released and no calls should be
429  * made anymore.
430  */
431 unsigned int
432 e_connman_system_shutdown(void)
433 {
434    if (init_count == 0)
435      {
436         ERR("connman system already shut down.");
437         return 0;
438      }
439    init_count--;
440    if (init_count > 0)
441      return init_count;
442
443    _stringshare_del(&e_connman_iface_manager);
444    _stringshare_del(&e_connman_iface_network);
445    _stringshare_del(&e_connman_iface_profile);
446    _stringshare_del(&e_connman_iface_service);
447    _stringshare_del(&e_connman_iface_device);
448    _stringshare_del(&e_connman_iface_connection);
449
450    _stringshare_del(&e_connman_prop_available);
451    _stringshare_del(&e_connman_prop_connected);
452    _stringshare_del(&e_connman_prop_connections);
453    _stringshare_del(&e_connman_prop_default);
454    _stringshare_del(&e_connman_prop_device);
455    _stringshare_del(&e_connman_prop_devices);
456    _stringshare_del(&e_connman_prop_interface);
457    _stringshare_del(&e_connman_prop_ipv4);
458    _stringshare_del(&e_connman_prop_ipv4_configuration);
459    _stringshare_del(&e_connman_prop_ethernet);
460    _stringshare_del(&e_connman_prop_method);
461    _stringshare_del(&e_connman_prop_address);
462    _stringshare_del(&e_connman_prop_gateway);
463    _stringshare_del(&e_connman_prop_netmask);
464    _stringshare_del(&e_connman_prop_mtu);
465    _stringshare_del(&e_connman_prop_name);
466    _stringshare_del(&e_connman_prop_network);
467    _stringshare_del(&e_connman_prop_networks);
468    _stringshare_del(&e_connman_prop_offline_mode);
469    _stringshare_del(&e_connman_prop_policy);
470    _stringshare_del(&e_connman_prop_powered);
471    _stringshare_del(&e_connman_prop_priority);
472    _stringshare_del(&e_connman_prop_profiles);
473    _stringshare_del(&e_connman_prop_profile_active);
474    _stringshare_del(&e_connman_prop_services);
475    _stringshare_del(&e_connman_prop_remember);
476    _stringshare_del(&e_connman_prop_scan_interval);
477    _stringshare_del(&e_connman_prop_scanning);
478    _stringshare_del(&e_connman_prop_state);
479    _stringshare_del(&e_connman_prop_strength);
480    _stringshare_del(&e_connman_prop_frequency);
481    _stringshare_del(&e_connman_prop_type);
482    _stringshare_del(&e_connman_prop_wifi_mode);
483    _stringshare_del(&e_connman_prop_wifi_passphrase);
484    _stringshare_del(&e_connman_prop_wifi_security);
485    _stringshare_del(&e_connman_prop_wifi_ssid);
486    _stringshare_del(&e_connman_prop_wifi_channel);
487    _stringshare_del(&e_connman_prop_wifi_eap);
488    _stringshare_del(&e_connman_prop_error);
489    _stringshare_del(&e_connman_prop_mode);
490    _stringshare_del(&e_connman_prop_security);
491    _stringshare_del(&e_connman_prop_passphrase);
492    _stringshare_del(&e_connman_prop_passphrase_required);
493    _stringshare_del(&e_connman_prop_favorite);
494    _stringshare_del(&e_connman_prop_immutable);
495    _stringshare_del(&e_connman_prop_auto_connect);
496    _stringshare_del(&e_connman_prop_setup_required);
497    _stringshare_del(&e_connman_prop_apn);
498    _stringshare_del(&e_connman_prop_mcc);
499    _stringshare_del(&e_connman_prop_mnc);
500    _stringshare_del(&e_connman_prop_roaming);
501    _stringshare_del(&e_connman_prop_technology_default);
502    _stringshare_del(&e_connman_prop_technologies_available);
503    _stringshare_del(&e_connman_prop_technologies_enabled);
504    _stringshare_del(&e_connman_prop_technologies_connected);
505
506    if (pending_get_name_owner)
507      {
508         dbus_pending_call_cancel(pending_get_name_owner);
509         pending_get_name_owner = NULL;
510      }
511
512    if (cb_name_owner_changed)
513      {
514         e_dbus_signal_handler_del(e_connman_conn, cb_name_owner_changed);
515         cb_name_owner_changed = NULL;
516      }
517
518    if (unique_name)
519      _e_connman_system_name_owner_exit();
520
521    e_connman_elements_shutdown();
522    eina_log_domain_unregister(_e_dbus_connman_log_dom);
523    e_connman_conn = NULL;
524
525    return init_count;
526 }