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