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