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