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