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