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