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