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