e_dbus/connman: add getters for Proxy.Configuration
[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[] = "net.connman";
11
12 E_DBus_Connection *e_connman_conn = NULL;
13
14 EAPI int E_CONNMAN_EVENT_MANAGER_IN = 0;
15 EAPI int E_CONNMAN_EVENT_MANAGER_OUT = 0;
16 EAPI int E_CONNMAN_EVENT_ELEMENT_ADD = 0;
17 EAPI int E_CONNMAN_EVENT_ELEMENT_DEL = 0;
18 EAPI int E_CONNMAN_EVENT_ELEMENT_UPDATED = 0;
19
20 const char *e_connman_iface_manager = NULL;
21 const char *e_connman_iface_profile = NULL;
22 const char *e_connman_iface_service = NULL;
23 const char *e_connman_iface_connection = NULL;
24 const char *e_connman_iface_technology = NULL;
25
26 const char *e_connman_prop_available = NULL;
27 const char *e_connman_prop_connections = NULL;
28 const char *e_connman_prop_default = NULL;
29 const char *e_connman_prop_ipv4 = NULL;
30 const char *e_connman_prop_ipv4_configuration = NULL;
31 const char *e_connman_prop_ethernet = NULL;
32 const char *e_connman_prop_interface = NULL;
33 const char *e_connman_prop_speed = NULL;
34 const char *e_connman_prop_duplex = NULL;
35 const char *e_connman_prop_method = NULL;
36 const char *e_connman_prop_address = NULL;
37 const char *e_connman_prop_gateway = NULL;
38 const char *e_connman_prop_netmask = NULL;
39 const char *e_connman_prop_mtu = NULL;
40 const char *e_connman_prop_name = NULL;
41 const char *e_connman_prop_offline_mode = NULL;
42 const char *e_connman_prop_policy = NULL;
43 const char *e_connman_prop_priority = NULL;
44 const char *e_connman_prop_profiles = NULL;
45 const char *e_connman_prop_profile_active = NULL;
46 const char *e_connman_prop_services = NULL;
47 const char *e_connman_prop_technologies = NULL;
48 const char *e_connman_prop_remember = NULL;
49 const char *e_connman_prop_state = NULL;
50 const char *e_connman_prop_strength = NULL;
51 const char *e_connman_prop_type = NULL;
52 const char *e_connman_prop_error = NULL;
53 const char *e_connman_prop_security = NULL;
54 const char *e_connman_prop_passphrase = NULL;
55 const char *e_connman_prop_passphrase_required = NULL;
56 const char *e_connman_prop_login_required = NULL;
57 const char *e_connman_prop_favorite = NULL;
58 const char *e_connman_prop_immutable = NULL;
59 const char *e_connman_prop_auto_connect = NULL;
60 const char *e_connman_prop_roaming = NULL;
61 const char *e_connman_prop_technology_default = NULL;
62 const char *e_connman_prop_technologies_available = NULL;
63 const char *e_connman_prop_technologies_enabled = NULL;
64 const char *e_connman_prop_technologies_connected = NULL;
65 const char *e_connman_prop_nameservers = NULL;
66 const char *e_connman_prop_domains = NULL;
67 const char *e_connman_prop_proxy = NULL;
68 const char *e_connman_prop_proxy_configuration = NULL;
69 const char *e_connman_prop_url = NULL;
70 const char *e_connman_prop_servers = NULL;
71 const char *e_connman_prop_excludes = NULL;
72
73 int _e_dbus_connman_log_dom = -1;
74
75 const char *
76 e_connman_system_bus_name_get(void)
77 {
78    return unique_name ? unique_name : bus_name;
79 }
80
81 /***********************************************************************
82 * Manager
83 ***********************************************************************/
84
85 /**
86  * Synchronize elements with server.
87  *
88  * This will call Manager.GetProperties() on server, retrieve properties
89  * and some element paths and then request their properties.
90  *
91  * This call will add events E_CONNMAN_EVENT_ELEMENT_ADD and
92  * E_CONNMAN_EVENT_ELEMENT_UPDATED to the main loop.
93  *
94  * This will not remove stale elements.
95  *
96  * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
97  */
98 Eina_Bool
99 e_connman_manager_sync_elements(void)
100 {
101    E_Connman_Element *manager;
102
103    if (!unique_name)
104       return EINA_FALSE;
105
106    manager = e_connman_element_register(manager_path, e_connman_iface_manager);
107    if (manager)
108       e_connman_element_properties_sync(manager);
109    else
110       return EINA_FALSE;
111
112    DBG("sync_manager: %s (%s)", unique_name, bus_name);
113
114    return EINA_TRUE;
115 }
116
117 static void
118 _e_connman_system_name_owner_exit(void)
119 {
120    e_connman_manager_clear_elements();
121    ecore_event_add(E_CONNMAN_EVENT_MANAGER_OUT, NULL, NULL, NULL);
122
123    free(unique_name);
124    unique_name = NULL;
125 }
126
127 static void
128 _e_connman_system_name_owner_enter(const char *uid)
129 {
130    DBG("enter connman at %s (old was %s)", uid, unique_name);
131    if (unique_name && strcmp(unique_name, uid) == 0)
132      {
133         DBG("same unique_name for connman, ignore.");
134         return;
135      }
136
137    if (unique_name)
138       _e_connman_system_name_owner_exit();
139
140    unique_name = strdup(uid);
141
142    ecore_event_add(E_CONNMAN_EVENT_MANAGER_IN, NULL, NULL, NULL);
143    e_connman_manager_sync_elements();
144 }
145
146 static void
147 _e_connman_system_name_owner_changed(void *data __UNUSED__, DBusMessage *msg)
148 {
149    DBusError err;
150    const char *name, *from, *to;
151
152    dbus_error_init(&err);
153    if (!dbus_message_get_args(msg, &err,
154                               DBUS_TYPE_STRING, &name,
155                               DBUS_TYPE_STRING, &from,
156                               DBUS_TYPE_STRING, &to,
157                               DBUS_TYPE_INVALID))
158      {
159         ERR("could not get NameOwnerChanged arguments: %s: %s",
160             err.name, err.message);
161         dbus_error_free(&err);
162         return;
163      }
164
165    if (strcmp(name, bus_name) != 0)
166       return;
167
168    DBG("NameOwnerChanged from=[%s] to=[%s]", from, to);
169
170    if (from[0] == '\0' && to[0] != '\0')
171      {
172         _e_connman_system_name_owner_enter(to);
173      }
174    else if (from[0] != '\0' && to[0] == '\0')
175      {
176         DBG("exit connman at %s", from);
177         if (strcmp(unique_name, from) != 0)
178            DBG("%s was not the known name %s, ignored.", from, unique_name);
179         else
180            _e_connman_system_name_owner_exit();
181      }
182    else
183      {
184         DBG("unknow change from %s to %s", from, to);
185      }
186 }
187
188 static void
189 _e_connman_get_name_owner(void *data __UNUSED__, DBusMessage *msg, DBusError *err)
190 {
191    DBusMessageIter itr;
192    int t;
193    const char *uid;
194
195    pending_get_name_owner = NULL;
196
197    if (!_dbus_callback_check_and_init(msg, &itr, err))
198       return;
199
200    t = dbus_message_iter_get_arg_type(&itr);
201    if (!_dbus_iter_type_check(t, DBUS_TYPE_STRING))
202       return;
203
204    dbus_message_iter_get_basic(&itr, &uid);
205    if (!uid)
206      {
207         ERR("no name owner!");
208         return;
209      }
210
211    _e_connman_system_name_owner_enter(uid);
212    return;
213 }
214
215 /**
216  * Initialize E Connection Manager (E_Connman) system.
217  *
218  * This will connect and watch net.connman.Manager and Element
219  * events and translate to Ecore main loop events, also provide a
220  * proxy for method invocation on server.
221  *
222  * Interesting events are:
223  *   - E_CONNMAN_EVENT_MANAGER_IN: issued when connman is avaiable.
224  *   - E_CONNMAN_EVENT_MANAGER_OUT: issued when connman connection is lost.
225  *   - E_CONNMAN_EVENT_ELEMENT_ADD: element was added.
226  *   - E_CONNMAN_EVENT_ELEMENT_DEL: element was deleted.
227  *   - E_CONNMAN_EVENT_ELEMENT_UPDATED: element was updated (properties
228  *     or state changed).
229  *
230  * Manager IN/OUT events do not provide any event information, just
231  * tells you that system is usable or not. After manager is out, all
232  * elements will be removed, so after this event do not use the system anymore.
233  *
234  * Element events will give you an element object. After DEL event callback
235  * returns, that element will not be valid anymore.
236  */
237 unsigned int
238 e_connman_system_init(E_DBus_Connection *edbus_conn)
239 {
240    init_count++;
241
242    if (init_count > 1)
243       return init_count;
244
245    _e_dbus_connman_log_dom = eina_log_domain_register
246          ("e_dbus_connman", EINA_LOG_DEFAULT_COLOR);
247
248    if (_e_dbus_connman_log_dom < 0)
249      {
250         EINA_LOG_ERR
251            ("impossible to create a log domain for edbus_connman module");
252         return -1;
253      }
254
255    if (E_CONNMAN_EVENT_MANAGER_IN == 0)
256       E_CONNMAN_EVENT_MANAGER_IN = ecore_event_type_new();
257
258    if (E_CONNMAN_EVENT_MANAGER_OUT == 0)
259       E_CONNMAN_EVENT_MANAGER_OUT = ecore_event_type_new();
260
261    if (E_CONNMAN_EVENT_ELEMENT_ADD == 0)
262       E_CONNMAN_EVENT_ELEMENT_ADD = ecore_event_type_new();
263
264    if (E_CONNMAN_EVENT_ELEMENT_DEL == 0)
265       E_CONNMAN_EVENT_ELEMENT_DEL = ecore_event_type_new();
266
267    if (E_CONNMAN_EVENT_ELEMENT_UPDATED == 0)
268       E_CONNMAN_EVENT_ELEMENT_UPDATED = ecore_event_type_new();
269
270 #define ADD_STRINGSHARE(name, s)       \
271    if (!name)                          \
272       name = eina_stringshare_add(s)
273
274    ADD_STRINGSHARE(e_connman_iface_manager, "net.connman.Manager");
275    ADD_STRINGSHARE(e_connman_iface_profile, "net.connman.Profile");
276    ADD_STRINGSHARE(e_connman_iface_service, "net.connman.Service");
277    ADD_STRINGSHARE(e_connman_iface_connection, "net.connman.Connection");
278    ADD_STRINGSHARE(e_connman_iface_technology, "net.connman.Technology");
279    ADD_STRINGSHARE(e_connman_prop_available, "Available");
280    ADD_STRINGSHARE(e_connman_prop_connections, "Connections");
281    ADD_STRINGSHARE(e_connman_prop_default, "Default");
282    ADD_STRINGSHARE(e_connman_prop_ipv4, "IPv4");
283    ADD_STRINGSHARE(e_connman_prop_ipv4_configuration, "IPv4.Configuration");
284    ADD_STRINGSHARE(e_connman_prop_ethernet, "Ethernet");
285    ADD_STRINGSHARE(e_connman_prop_interface, "Interface");
286    ADD_STRINGSHARE(e_connman_prop_speed, "Speed");
287    ADD_STRINGSHARE(e_connman_prop_duplex, "Duplex");
288    ADD_STRINGSHARE(e_connman_prop_method, "Method");
289    ADD_STRINGSHARE(e_connman_prop_address, "Address");
290    ADD_STRINGSHARE(e_connman_prop_gateway, "Gateway");
291    ADD_STRINGSHARE(e_connman_prop_netmask, "Netmask");
292    ADD_STRINGSHARE(e_connman_prop_mtu, "MTU");
293    ADD_STRINGSHARE(e_connman_prop_name, "Name");
294    ADD_STRINGSHARE(e_connman_prop_offline_mode, "OfflineMode");
295    ADD_STRINGSHARE(e_connman_prop_policy, "Policy");
296    ADD_STRINGSHARE(e_connman_prop_priority, "Priority");
297    ADD_STRINGSHARE(e_connman_prop_profiles, "Profiles");
298    ADD_STRINGSHARE(e_connman_prop_profile_active, "ActiveProfile");
299    ADD_STRINGSHARE(e_connman_prop_services, "Services");
300    ADD_STRINGSHARE(e_connman_prop_technologies, "Technologies");
301    ADD_STRINGSHARE(e_connman_prop_remember, "Remember");
302    ADD_STRINGSHARE(e_connman_prop_state, "State");
303    ADD_STRINGSHARE(e_connman_prop_strength, "Strength");
304    ADD_STRINGSHARE(e_connman_prop_type, "Type");
305    ADD_STRINGSHARE(e_connman_prop_error, "Error");
306    ADD_STRINGSHARE(e_connman_prop_security, "Security");
307    ADD_STRINGSHARE(e_connman_prop_passphrase, "Passphrase");
308    ADD_STRINGSHARE(e_connman_prop_passphrase_required, "PassphraseRequired");
309    ADD_STRINGSHARE(e_connman_prop_login_required, "LoginRequired");
310    ADD_STRINGSHARE(e_connman_prop_favorite, "Favorite");
311    ADD_STRINGSHARE(e_connman_prop_immutable, "Immutable");
312    ADD_STRINGSHARE(e_connman_prop_auto_connect, "AutoConnect");
313    ADD_STRINGSHARE(e_connman_prop_roaming, "Roaming");
314    ADD_STRINGSHARE(e_connman_prop_technology_default, "DefaultTechnology");
315    ADD_STRINGSHARE(e_connman_prop_technologies_available,
316                    "AvailableTechnologies");
317    ADD_STRINGSHARE(e_connman_prop_technologies_enabled, "EnabledTechnologies");
318    ADD_STRINGSHARE(e_connman_prop_technologies_connected,
319                    "ConnectedTechnologies");
320    ADD_STRINGSHARE(e_connman_prop_nameservers, "Nameservers");
321    ADD_STRINGSHARE(e_connman_prop_domains, "Domains");
322    ADD_STRINGSHARE(e_connman_prop_proxy, "Proxy");
323    ADD_STRINGSHARE(e_connman_prop_proxy_configuration, "Proxy.Configuration");
324    ADD_STRINGSHARE(e_connman_prop_url, "URL");
325    ADD_STRINGSHARE(e_connman_prop_servers, "Servers");
326    ADD_STRINGSHARE(e_connman_prop_excludes, "Excludes");
327
328 #undef ADD_STRINGSHARE
329
330    e_connman_conn = edbus_conn;
331    cb_name_owner_changed = e_dbus_signal_handler_add
332          (e_connman_conn, E_DBUS_FDO_BUS, E_DBUS_FDO_PATH, E_DBUS_FDO_INTERFACE, "NameOwnerChanged",
333          _e_connman_system_name_owner_changed, NULL);
334
335    if (pending_get_name_owner)
336       dbus_pending_call_cancel(pending_get_name_owner);
337
338    pending_get_name_owner = e_dbus_get_name_owner
339          (e_connman_conn, bus_name, _e_connman_get_name_owner, NULL);
340
341    e_connman_elements_init();
342
343    return init_count;
344 }
345
346 static inline void
347 _stringshare_del(const char **str)
348 {
349    if (!*str)
350       return;
351
352    eina_stringshare_del(*str);
353    *str = NULL;
354 }
355
356 /**
357  * Shutdown connman system.
358  *
359  * When count drops to 0 resources will be released and no calls should be
360  * made anymore.
361  */
362 unsigned int
363 e_connman_system_shutdown(void)
364 {
365    if (init_count == 0)
366      {
367         ERR("connman system already shut down.");
368         return 0;
369      }
370
371    init_count--;
372    if (init_count > 0)
373       return init_count;
374
375    _stringshare_del(&e_connman_iface_manager);
376    _stringshare_del(&e_connman_iface_profile);
377    _stringshare_del(&e_connman_iface_service);
378    _stringshare_del(&e_connman_iface_connection);
379    _stringshare_del(&e_connman_iface_technology);
380
381    _stringshare_del(&e_connman_prop_available);
382    _stringshare_del(&e_connman_prop_connections);
383    _stringshare_del(&e_connman_prop_default);
384    _stringshare_del(&e_connman_prop_ipv4);
385    _stringshare_del(&e_connman_prop_ipv4_configuration);
386    _stringshare_del(&e_connman_prop_ethernet);
387    _stringshare_del(&e_connman_prop_interface);
388    _stringshare_del(&e_connman_prop_speed);
389    _stringshare_del(&e_connman_prop_duplex);
390    _stringshare_del(&e_connman_prop_method);
391    _stringshare_del(&e_connman_prop_address);
392    _stringshare_del(&e_connman_prop_gateway);
393    _stringshare_del(&e_connman_prop_netmask);
394    _stringshare_del(&e_connman_prop_mtu);
395    _stringshare_del(&e_connman_prop_name);
396    _stringshare_del(&e_connman_prop_offline_mode);
397    _stringshare_del(&e_connman_prop_policy);
398    _stringshare_del(&e_connman_prop_priority);
399    _stringshare_del(&e_connman_prop_profiles);
400    _stringshare_del(&e_connman_prop_profile_active);
401    _stringshare_del(&e_connman_prop_services);
402    _stringshare_del(&e_connman_prop_technologies);
403    _stringshare_del(&e_connman_prop_remember);
404    _stringshare_del(&e_connman_prop_state);
405    _stringshare_del(&e_connman_prop_strength);
406    _stringshare_del(&e_connman_prop_type);
407    _stringshare_del(&e_connman_prop_error);
408    _stringshare_del(&e_connman_prop_security);
409    _stringshare_del(&e_connman_prop_passphrase);
410    _stringshare_del(&e_connman_prop_passphrase_required);
411    _stringshare_del(&e_connman_prop_login_required);
412    _stringshare_del(&e_connman_prop_favorite);
413    _stringshare_del(&e_connman_prop_immutable);
414    _stringshare_del(&e_connman_prop_auto_connect);
415    _stringshare_del(&e_connman_prop_roaming);
416    _stringshare_del(&e_connman_prop_technology_default);
417    _stringshare_del(&e_connman_prop_technologies_available);
418    _stringshare_del(&e_connman_prop_technologies_enabled);
419    _stringshare_del(&e_connman_prop_technologies_connected);
420    _stringshare_del(&e_connman_prop_nameservers);
421    _stringshare_del(&e_connman_prop_domains);
422    _stringshare_del(&e_connman_prop_proxy);
423    _stringshare_del(&e_connman_prop_proxy_configuration);
424    _stringshare_del(&e_connman_prop_url);
425    _stringshare_del(&e_connman_prop_servers);
426    _stringshare_del(&e_connman_prop_excludes);
427
428    if (pending_get_name_owner)
429      {
430         dbus_pending_call_cancel(pending_get_name_owner);
431         pending_get_name_owner = NULL;
432      }
433
434    if (cb_name_owner_changed)
435      {
436         e_dbus_signal_handler_del(e_connman_conn, cb_name_owner_changed);
437         cb_name_owner_changed = NULL;
438      }
439
440    if (unique_name)
441       _e_connman_system_name_owner_exit();
442
443    e_connman_elements_shutdown();
444    eina_log_domain_unregister(_e_dbus_connman_log_dom);
445    e_connman_conn = NULL;
446
447    return init_count;
448 }
449