Revise signal handler for Wi-Fi powered event from ConnMan
[platform/core/connectivity/net-config.git] / src / dbus / netdbus.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "log.h"
25 #include "netdbus.h"
26 #include "netconfig.h"
27
28 #define NETCONFIG_DBUS_REPLY_TIMEOUT (10 * 1000)
29
30 #define DBUS_PARAM_TYPE_STRING          "string"
31 #define DBUS_PARAM_TYPE_INT16           "int16"
32 #define DBUS_PARAM_TYPE_UINT16          "uint16"
33 #define DBUS_PARAM_TYPE_INT32           "int32"
34 #define DBUS_PARAM_TYPE_UINT32          "uint32"
35 #define DBUS_PARAM_TYPE_INT64           "int64"
36 #define DBUS_PARAM_TYPE_UINT64          "uint64"
37 #define DBUS_PARAM_TYPE_DOUBLE          "double"
38 #define DBUS_PARAM_TYPE_BYTE            "byte"
39 #define DBUS_PARAM_TYPE_BOOLEAN         "boolean"
40 #define DBUS_PARAM_TYPE_OBJECT_PATH     "objpath"
41 #define DBUS_PARAM_TYPE_VARIANT         "variant"
42 #define DBUS_PARAM_TYPE_ARRAY           "array"
43
44
45 static gboolean __netconfig_dbus_append_param_variant(
46                 DBusMessageIter *iter, char *type, char *param)
47 {
48         DBusMessageIter value, array;
49         char *args = NULL, *ch = NULL;
50         dbus_bool_t b_value = FALSE;
51
52         if (strcmp(type, DBUS_PARAM_TYPE_STRING) == 0) {
53                 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
54                                 DBUS_TYPE_STRING_AS_STRING, &value);
55
56                 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &param);
57
58                 dbus_message_iter_close_container(iter, &value);
59         } else if (strcmp(type, DBUS_PARAM_TYPE_BOOLEAN) == 0) {
60                 if (strcmp(param, "true") == 0) {
61                         b_value = TRUE;
62                         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
63                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
64                         dbus_message_iter_append_basic(&value,
65                                         DBUS_TYPE_BOOLEAN, &b_value);
66                         dbus_message_iter_close_container(iter, &value);
67                 } else if (strcmp(param, "false") == 0) {
68                         b_value = FALSE;
69                         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
70                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
71                         dbus_message_iter_append_basic(&value,
72                                         DBUS_TYPE_BOOLEAN, &b_value);
73                         dbus_message_iter_close_container(iter, &value);
74                 } else {
75                         ERR("Error!!! Expected \"true\" or"
76                                 "\"false\" instead of \"%s\"", ch);
77                         return FALSE;
78                 }
79         } else if (strcmp(type, DBUS_PARAM_TYPE_OBJECT_PATH) == 0) {
80                 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
81                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &value);
82
83                 dbus_message_iter_append_basic(&value, DBUS_TYPE_OBJECT_PATH, &param);
84
85                 dbus_message_iter_close_container(iter, &value);
86         } else if (strcmp(type, DBUS_PARAM_TYPE_ARRAY) == 0) {
87                 args = param;
88                 ch = strchr(args, ':');
89                 if (ch == NULL) {
90                         ERR("Error!!! Invalid data format[\"%s\"]", args);
91                         return FALSE;
92                 }
93                 *ch = 0; ch++;
94
95                 if (strcmp(args, DBUS_PARAM_TYPE_STRING) == 0) {
96                         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
97                                         DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
98                                         &value);
99
100                         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
101                                         DBUS_TYPE_STRING_AS_STRING, &array);
102
103                         dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &ch);
104
105                         dbus_message_iter_close_container(&value, &array);
106
107                         dbus_message_iter_close_container(iter, &value);
108                 } else if (strcmp(args, DBUS_PARAM_TYPE_OBJECT_PATH) == 0) {
109                         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
110                                         DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
111                                         &value);
112
113                         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
114                                         DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
115
116                         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH, &ch);
117
118                         dbus_message_iter_close_container(&value, &array);
119
120                         dbus_message_iter_close_container(iter, &value);
121                 } else {
122                         ERR("Error!!! Not supported data format[\"%s\"]", args);
123                         return FALSE;
124                 }
125         } else {
126                 ERR("Error!!! Not supported data format[\"%s\"]", args);
127                 return FALSE;
128         }
129
130         return TRUE;
131 }
132
133 static gboolean __netconfig_dbus_append_param(
134                 DBusMessage *message, char *param_array[])
135 {
136         int count = 0;
137         dbus_uint32_t uint32 = 0;
138         DBusMessageIter iter;
139         char *args = NULL, *ch = NULL;
140
141         if (param_array == NULL)
142                 return TRUE;
143
144         dbus_message_iter_init_append(message, &iter);
145
146         while (param_array[count] != NULL) {
147                 args = param_array[count];
148                 DBG("parameter %d - [%s]", count, param_array[count]);
149
150                 ch = strchr(args, ':');
151                 if (ch == NULL) {
152                         ERR("Error!!! Invalid parameter[\"%s\"]\n", args);
153                         return FALSE;
154                 }
155                 *ch = 0; ch++;
156
157                 if (strcmp(args, DBUS_PARAM_TYPE_STRING) == 0) {
158                         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &ch);
159                 } else if (strcmp(args, DBUS_PARAM_TYPE_UINT32) == 0) {
160                         uint32 = strtoul(ch, NULL, 0);
161                         dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &uint32);
162                 } else if (strcmp(args, DBUS_PARAM_TYPE_VARIANT) == 0) {
163                         args = ch;
164                         ch = strchr(args, ':');
165                         if (ch == NULL) {
166                                 ERR("Error!!! Invalid data format[\"%s\"]\n", args);
167                                 return FALSE;
168                         }
169                         *ch = 0; ch++;
170
171                         if (__netconfig_dbus_append_param_variant(&iter, args, ch) != TRUE)
172                                 return FALSE;
173
174                 } else if (strcmp(args, DBUS_PARAM_TYPE_OBJECT_PATH) == 0) {
175                         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &ch);
176                 } else {
177                         ERR("Error!!! Not supported data format[\"%s\"]", args);
178                         return FALSE;
179                 }
180
181                 count++;
182         }
183
184         return TRUE;
185 }
186
187 gboolean netconfig_dbus_get_basic_params_string(DBusMessage *message,
188                 char **key, int type, void *value)
189 {
190         DBusMessageIter iter, iter_variant;
191
192         dbus_message_iter_init(message, &iter);
193         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
194                 DBG("Argument type %d", dbus_message_iter_get_arg_type(&iter));
195                 return FALSE;
196         }
197
198         dbus_message_iter_get_basic(&iter, key);
199
200         if (value == NULL)
201                 return TRUE;
202
203         dbus_message_iter_next(&iter);
204         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
205                 DBG("Argument type %d", dbus_message_iter_get_arg_type(&iter));
206                 return TRUE;
207         }
208
209         dbus_message_iter_recurse(&iter, &iter_variant);
210         if (dbus_message_iter_get_arg_type(&iter_variant) != type)
211                 return FALSE;
212
213         dbus_message_iter_get_basic(&iter_variant, value);
214
215         return TRUE;
216 }
217
218 gboolean netconfig_dbus_get_basic_params_array(DBusMessage *message,
219                 char **key, void **value)
220 {
221         DBusMessageIter args, dict, entry, variant;
222         int type = 0;
223
224         if (key == NULL)
225                 return FALSE;
226
227         /* read parameters */
228         if (dbus_message_iter_init(message, &args) == FALSE) {
229                 DBG("Message does not have parameters");
230                 return FALSE;
231         }
232
233         if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY) {
234                 DBG("Argument type %d", dbus_message_iter_get_arg_type(&args));
235                 return FALSE;
236         }
237
238         dbus_message_iter_recurse(&args, &dict);
239
240         if (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_DICT_ENTRY) {
241                 DBG("Argument type %d", dbus_message_iter_get_arg_type(&dict));
242                 return FALSE;
243         }
244
245         dbus_message_iter_recurse(&dict, &entry);
246
247         if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING) {
248                 DBG("Argument type %d", dbus_message_iter_get_arg_type(&entry));
249                 return FALSE;
250         }
251
252         dbus_message_iter_get_basic(&entry, key);
253
254         if (value == NULL)
255                 return TRUE;
256
257         dbus_message_iter_next(&entry);
258
259         if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT) {
260                 DBG("Argument type %d", dbus_message_iter_get_arg_type(&entry));
261                 return TRUE;
262         }
263
264         dbus_message_iter_recurse(&entry, &variant);
265
266         type = dbus_message_iter_get_arg_type(&variant);
267         if (type == DBUS_TYPE_STRING)
268                 dbus_message_iter_get_basic(&variant, value);
269         else if (type == DBUS_TYPE_BYTE || type == DBUS_TYPE_BOOLEAN ||
270                         type == DBUS_TYPE_INT16 || type == DBUS_TYPE_UINT16 ||
271                         type == DBUS_TYPE_INT32 || type == DBUS_TYPE_UINT32 ||
272                         type == DBUS_TYPE_DOUBLE)
273                 dbus_message_iter_get_basic(&variant, *value);
274         else
275                 DBG("Argument type %d", type);
276
277         return TRUE;
278 }
279
280 gboolean netconfig_is_cellular_profile(const char *profile)
281 {
282         if (profile == NULL)
283                 return FALSE;
284
285         return g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
286 }
287
288 gboolean netconfig_is_wifi_profile(const char *profile)
289 {
290         if (profile == NULL)
291                 return FALSE;
292
293         return g_str_has_prefix(profile, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
294 }
295
296 gboolean netconfig_is_ethernet_profile(const char *profile)
297 {
298         if (profile == NULL)
299                 return FALSE;
300
301         return g_str_has_prefix(profile, CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
302 }
303
304 gboolean netconfig_is_bluetooth_profile(const char *profile)
305 {
306         if (profile == NULL)
307                 return FALSE;
308
309         return g_str_has_prefix(profile, CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
310 }
311
312 DBusMessage *netconfig_invoke_dbus_method(const char *dest, const char *path,
313                 const char *interface_name, const char *method, char *param_array[])
314 {
315         DBusError error;
316         DBusConnection *conn = NULL;
317         DBusMessage *reply = NULL;
318         DBusMessage *message = NULL;
319
320         DBG("[DBUS Sync] %s %s %s", interface_name, method, path);
321
322         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
323         if (conn == NULL) {
324                 ERR("Failed to get system bus");
325                 return NULL;
326         }
327
328         message = dbus_message_new_method_call(dest, path, interface_name, method);
329         if (message == NULL) {
330                 ERR("Error!!! Failed to GetProperties");
331                 dbus_connection_unref(conn);
332                 return NULL;
333         }
334
335         if (__netconfig_dbus_append_param(message, param_array) == FALSE) {
336                 ERR("Error!!! __netconfig_dbus_append_param() failed");
337                 dbus_message_unref(message);
338                 dbus_connection_unref(conn);
339                 return NULL;
340         }
341
342         dbus_error_init(&error);
343
344         reply = dbus_connection_send_with_reply_and_block(conn, message,
345                         NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
346
347         if (reply == NULL) {
348                 if (dbus_error_is_set(&error) == TRUE) {
349                         ERR("Error!!! dbus_connection_send_with_reply_and_block() failed. "
350                                         "DBus error [%s: %s]", error.name, error.message);
351
352                         dbus_error_free(&error);
353                 } else
354                         ERR("Error!!! Failed to get properties");
355
356                 dbus_message_unref(message);
357                 dbus_connection_unref(conn);
358
359                 return NULL;
360         }
361
362         dbus_message_unref(message);
363         dbus_connection_unref(conn);
364
365         return reply;
366 }
367
368 char *netconfig_wifi_get_connected_service_name(DBusMessage *message)
369 {
370         int is_connected = 0;
371         char *essid_name = NULL;
372         DBusMessageIter iter, array;
373
374         dbus_message_iter_init(message, &iter);
375         dbus_message_iter_recurse(&iter, &array);
376
377         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
378                 DBusMessageIter entry, string;
379                 const char *key = NULL;
380
381                 dbus_message_iter_recurse(&array, &entry);
382                 dbus_message_iter_get_basic(&entry, &key);
383
384                 if (g_str_equal(key, "State") == TRUE && is_connected == 0) {
385                         dbus_message_iter_next(&entry);
386                         dbus_message_iter_recurse(&entry, &string);
387
388                         if (dbus_message_iter_get_arg_type(&string) == DBUS_TYPE_STRING) {
389                                 dbus_message_iter_get_basic(&string, &key);
390
391                                 if (g_str_equal(key, "ready") == TRUE ||
392                                                 g_str_equal(key, "online") == TRUE)
393                                         is_connected = 1;
394                         }
395                 } else if (g_str_equal(key, "Name") == TRUE) {
396                         dbus_message_iter_next(&entry);
397                         dbus_message_iter_recurse(&entry, &string);
398
399                         if (dbus_message_iter_get_arg_type(&string) == DBUS_TYPE_STRING) {
400                                 dbus_message_iter_get_basic(&string, &key);
401
402                                 essid_name = (char *)g_strdup(key);
403                         }
404                 }
405
406                 dbus_message_iter_next(&array);
407         }
408
409         if (is_connected == 1 && essid_name != NULL)
410                 return essid_name;
411
412         if (essid_name != NULL)
413                 g_free(essid_name);
414
415         return NULL;
416 }
417
418 DBusGConnection *netconfig_setup_dbus(void)
419 {
420         DBusGConnection* connection = NULL;
421         GError *error = NULL;
422         DBusGProxy *proxy;
423         guint rv = 0;
424
425         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
426         if (connection == NULL) {
427                 ERR("Fail to get DBus(%s)", error->message);
428                 g_error_free(error);
429
430                 return connection;
431         }
432
433         INFO("Successfully get system DBus connection(%p)", connection);
434
435         proxy = dbus_g_proxy_new_for_name(connection, "org.freedesktop.DBus",
436                         "/org/freedesktop/DBus",
437                         "org.freedesktop.DBus");
438
439         if (!dbus_g_proxy_call(proxy, "RequestName", &error,
440                         G_TYPE_STRING, NETCONFIG_SERVICE, G_TYPE_UINT, 0,
441                         G_TYPE_INVALID, G_TYPE_UINT, &rv,
442                         G_TYPE_INVALID)) {
443                 ERR("Failed to acquire service(%s) error(%s)",
444                                 NETCONFIG_SERVICE, error->message);
445                 g_error_free(error);
446
447                 dbus_g_connection_unref(connection);
448
449                 return NULL;
450         }
451
452         if (rv != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
453                 ERR("Service name is already in use");
454
455                 dbus_g_connection_unref(connection);
456
457                 return NULL;
458         }
459
460         return connection;
461 }