Fix parsing logic for dbus return value
[platform/core/connectivity/wifi-mesh-manager.git] / src / mesh-gdbus.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mesh-log.h"
18 #include "mesh-util.h"
19 #include "mesh-gdbus.h"
20 #include "mesh-request.h"
21
22 #include "nl80211.h"
23
24 static GDBusProxy *_gproxy_connman = NULL;
25 static GDBusProxy *_gproxy_connman_mesh = NULL;
26 static GDBusProxy *_gproxy_connman_technology = NULL;
27
28 static int _meshd_close_gdbus_call(mesh_service *service);
29
30 static int __channel_to_frequency(int channel, enum nl80211_band band)
31 {
32         if (channel <= 0)
33                 return 0;
34
35         switch (band) {
36         case NL80211_BAND_2GHZ:
37                 if (channel == 14)
38                         return 2484;
39                 else if (channel < 14)
40                         return 2407 + channel * 5;
41                 break;
42         case NL80211_BAND_5GHZ:
43                 if (channel >= 182 && channel <= 196)
44                         return 4000 + channel * 5;
45                 else
46                         return 5000 + channel * 5;
47                 break;
48         default:
49                 break;
50         }
51
52         /* not supported */
53         return 0;
54 }
55
56 static int __frequency_to_channel(int freq)
57 {
58         if (freq == 2484)
59                 return 14;
60         else if (freq < 2484)
61                 return (freq - 2407) / 5;
62         else if (freq >= 4910 && freq <= 4980)
63                 return (freq - 4000) / 5;
64         else if (freq <= 45000)
65                 return (freq - 5000) / 5;
66         else if (freq >= 58320 && freq <= 64800)
67                 return (freq - 56160) / 2160;
68         else
69                 return 0;
70 }
71
72 static GDBusProxy *_proxy_get_connman(mesh_service *service)
73 {
74         GDBusProxy *proxy = NULL;
75         meshd_check_null_ret_error("service", service, NULL);
76
77         if (NULL == _gproxy_connman) {
78                 proxy = g_dbus_proxy_new_sync(service->connection,
79                         G_DBUS_PROXY_FLAGS_NONE, NULL,
80                         CONNMAN_SERVER_NAME,
81                         CONNMAN_OBJECT_PATH,
82                         CONNMAN_INTERFACE_MANAGER,
83                         NULL, NULL);
84         } else
85                 proxy = _gproxy_connman;
86
87         return proxy;
88 }
89
90 static GDBusProxy *_proxy_get_connman_mesh(mesh_service *service)
91 {
92         GDBusProxy *proxy = NULL;
93         meshd_check_null_ret_error("service", service, NULL);
94
95         if (NULL == _gproxy_connman_mesh) {
96                 proxy = g_dbus_proxy_new_sync(service->connection,
97                         G_DBUS_PROXY_FLAGS_NONE, NULL,
98                         CONNMAN_SERVER_NAME,
99                         CONNMAN_OBJECT_PATH_MESH,
100                         CONNMAN_INTERFACE_MESH,
101                         NULL, NULL);
102         } else
103                 proxy = _gproxy_connman_mesh;
104
105         return proxy;
106 }
107
108 static GDBusProxy *_proxy_get_connman_technology(mesh_service *service)
109 {
110         GDBusProxy *proxy = NULL;
111         meshd_check_null_ret_error("service", service, NULL);
112
113         if (NULL == _gproxy_connman_technology) {
114                 proxy = g_dbus_proxy_new_sync(service->connection,
115                         G_DBUS_PROXY_FLAGS_NONE, NULL,
116                         CONNMAN_SERVER_NAME,
117                         CONNMAN_OBJECT_PATH_TECH_MESH,
118                         CONNMAN_INTERFACE_TECH,
119                         NULL, NULL);
120         } else
121                 proxy = _gproxy_connman_technology;
122
123         return proxy;
124 }
125
126 static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec,
127                 gpointer *user_data)
128 {
129         GDBusProxy *proxy = G_DBUS_PROXY(object);
130         gchar *name_owner = g_dbus_proxy_get_name_owner(proxy);
131         mesh_service *service = (mesh_service*)user_data;
132
133         NOTUSED(pspec);
134
135         if (NULL == name_owner) {
136                 MESH_LOGE("name_owner is not exists !");
137                 _meshd_close_gdbus_call(service);
138         }
139
140         g_free(name_owner);
141 }
142
143 static int _meshd_create_gdbus_call(mesh_service *service)
144 {
145         int id;
146         GError *error = NULL;
147
148         if (NULL == service)
149                 return MESHD_ERROR_INVALID_PARAMETER;
150
151         if (NULL != service->connection)
152                 return MESHD_ERROR_ALREADY_REGISTERED;
153
154         service->connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
155         if (service->connection == NULL) {
156                 if (error != NULL) {
157                         MESH_LOGE("Failed to connect to the D-BUS daemon [%s]", error->message);
158                         g_error_free(error);
159                 }
160                 return MESHD_ERROR_IO_ERROR;
161         }
162
163         id = g_signal_connect(service->connection, "notify::g-name-owner",
164                         G_CALLBACK(_dbus_name_owner_notify), service);
165         if (0 == id) {
166                 MESH_LOGE("g_signal_connect() Fail");
167                 g_object_unref(service->connection);
168                 service->connection = NULL;
169                 return MESHD_ERROR_IO_ERROR;
170         }
171
172         return MESHD_ERROR_NONE;
173 }
174
175 static int _meshd_close_gdbus_call(mesh_service *service)
176 {
177         /* CHECK: is connection ref count required? */
178         g_object_unref(service->connection);
179         service->connection = NULL;
180
181         return MESHD_ERROR_NONE;
182 }
183
184 static void _meshd_signal_handler(GDBusConnection *connection,
185                 const gchar *sender_name, const gchar *object_path, const gchar *interface_name,
186                 const gchar *signal_name, GVariant *parameters, gpointer user_data)
187 {
188         mesh_service *service = (mesh_service*)user_data;
189
190         meshd_check_null_ret("user_data", user_data);
191         //meshd_check_null_ret("event_handler", service->event_handler);
192         NOTUSED(connection);
193         NOTUSED(sender_name);
194         NOTUSED(object_path);
195         NOTUSED(interface_name);
196         NOTUSED(signal_name);
197         NOTUSED(parameters);
198
199         NOTUSED(service);
200
201         MESH_LOGD("signal received = %s", signal_name);
202         if (0 == g_strcmp0(signal_name, "ScanDone")) {
203                 /* TODO: Handle event */
204                 mesh_notify_scan_done();
205         }
206 }
207
208 static void _meshd_subscribe_event(mesh_service *service)
209 {
210         unsigned int id;
211
212         id = g_dbus_connection_signal_subscribe(
213                         (GDBusConnection *)service->connection,
214                         CONNMAN_SERVER_NAME, CONNMAN_INTERFACE_MANAGER,
215                         "ScanDone", "/", NULL,
216                         G_DBUS_CALL_FLAGS_NONE, _meshd_signal_handler, service, NULL);
217         if (0 == id) {
218                 MESH_LOGE("g_dbus_connection_signal_subscribe(ScanDone) Fail(%d)", errno);
219                 return;
220         }
221         service->dbus_sub_ids = g_list_append(service->dbus_sub_ids, GUINT_TO_POINTER(id));
222         MESH_LOGD("[Signal subscribe] : ScanDone (%d)", id);
223
224         /* End of subscription */
225 }
226
227 int meshd_dbus_start(mesh_service *service)
228 {
229         int rv;
230
231         rv = _meshd_create_gdbus_call(service);
232         if (MESHD_ERROR_NONE != rv)
233                 return rv;
234
235         service->ca = g_cancellable_new();
236
237         /* Create all required proxies here */
238         _gproxy_connman = _proxy_get_connman(service);
239         meshd_check_null_ret_error("_gproxy_connman", _gproxy_connman,
240                                 MESHD_ERROR_IO_ERROR);
241         g_dbus_proxy_set_default_timeout(
242                         G_DBUS_PROXY(_gproxy_connman), MESH_DBUS_PROXY_TIMEOUT);
243
244         _gproxy_connman_mesh = _proxy_get_connman_mesh(service);
245         meshd_check_null_ret_error("_gproxy_connman_mesh", _gproxy_connman_mesh,
246                                 MESHD_ERROR_IO_ERROR);
247         g_dbus_proxy_set_default_timeout(
248                         G_DBUS_PROXY(_gproxy_connman_mesh), MESH_DBUS_PROXY_TIMEOUT);
249
250         _gproxy_connman_technology = _proxy_get_connman_technology(service);
251         meshd_check_null_ret_error("_gproxy_connman_technology", _gproxy_connman_technology,
252                                 MESHD_ERROR_IO_ERROR);
253         g_dbus_proxy_set_default_timeout(
254                         G_DBUS_PROXY(_gproxy_connman_technology), MESH_DBUS_PROXY_TIMEOUT);
255
256         /* Subscribe events */
257         _meshd_subscribe_event(service);
258
259         return MESHD_ERROR_NONE;
260 }
261
262 int meshd_dbus_stop(mesh_service *service)
263 {
264         int rv;
265
266         if (NULL == service)
267                 return MESHD_ERROR_INVALID_PARAMETER;
268
269         /* Unref all proxies here */
270         if (_gproxy_connman) {
271                 g_object_unref(_gproxy_connman);
272                 _gproxy_connman = NULL;
273         }
274         if (_gproxy_connman_mesh) {
275                 g_object_unref(_gproxy_connman_mesh);
276                 _gproxy_connman_mesh = NULL;
277         }
278         if (_gproxy_connman_technology) {
279                 g_object_unref(_gproxy_connman_technology);
280                 _gproxy_connman_technology = NULL;
281         }
282
283         g_cancellable_cancel(service->ca);
284         g_object_unref(service->ca);
285         service->ca = NULL;
286
287         rv = _meshd_close_gdbus_call(service);
288         return rv;
289 }
290
291 int mesh_ipc_create_mesh_interface(mesh_service *service)
292 {
293         GVariant *variant = NULL;
294         GError *error = NULL;
295         GVariant *var_dict = NULL;
296         GVariantDict dict;
297         mesh_interface_s *info = NULL;
298
299         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
300         meshd_check_null_ret_error("connection", service->connection,
301                         MESHD_ERROR_INVALID_PARAMETER);
302         meshd_check_null_ret_error("_gproxy_connman_technology",
303                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
304
305         info = service->interface_info;
306
307         g_variant_dict_init(&dict, NULL);
308         g_variant_dict_insert(&dict, "Ifname", "s", info->mesh_interface);
309         g_variant_dict_insert(&dict, "ParentIfname", "s", info->base_interface);
310         g_variant_dict_insert(&dict, "BridgeIfname", "s", info->bridge_interface);
311         var_dict = g_variant_dict_end(&dict);
312
313         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
314                                 g_variant_new("(sv)", "MeshInterfaceAdd", var_dict),
315                                 G_DBUS_CALL_FLAGS_NONE,
316                                 -1, NULL, &error);
317         if (variant) {
318                 MESH_LOGD("Successfully requested. [MeshInterfaceAdd]");
319         } else if (error) {
320                 MESH_LOGE("Failed DBus call [%s]", error->message);
321                 g_error_free(error);
322                 return MESHD_ERROR_IO_ERROR;
323         }
324
325         return MESHD_ERROR_NONE;
326 }
327
328 int mesh_ipc_remove_mesh_interface(mesh_service *service)
329 {
330         GVariant *variant = NULL;
331         GError *error = NULL;
332         GVariant *var_dict = NULL;
333         GVariantDict dict;
334         mesh_interface_s *info = NULL;
335
336         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
337         meshd_check_null_ret_error("connection", service->connection,
338                         MESHD_ERROR_INVALID_PARAMETER);
339         meshd_check_null_ret_error("_gproxy_connman_technology",
340                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
341
342         info = service->interface_info;
343
344         g_variant_dict_init(&dict, NULL);
345         g_variant_dict_insert(&dict, "Ifname", "s", info->mesh_interface);
346         var_dict = g_variant_dict_end(&dict);
347
348         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
349                                 g_variant_new("(sv)", "MeshInterfaceRemove", var_dict),
350                                 G_DBUS_CALL_FLAGS_NONE,
351                                 -1, NULL, &error);
352         if (variant) {
353                 MESH_LOGD("Successfully requested. [MeshInterfaceRemove]");
354         } else if (error) {
355                 MESH_LOGE("Failed DBus call [%s]", error->message);
356                 g_error_free(error);
357                 return MESHD_ERROR_IO_ERROR;
358         }
359
360         return MESHD_ERROR_NONE;
361 }
362
363 int mesh_ipc_mesh_scan(mesh_service *service)
364 {
365         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
366         meshd_check_null_ret_error("connection", service->connection,
367                         MESHD_ERROR_INVALID_PARAMETER);
368         meshd_check_null_ret_error("_gproxy_connman_technology",
369                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
370
371         g_dbus_proxy_call(_gproxy_connman_technology, "Scan",
372                                 NULL,
373                                 G_DBUS_CALL_FLAGS_NONE,
374                                 -1, NULL, NULL, NULL);
375
376         MESH_LOGD("Successfully requested. [Scan]");
377
378         return MESHD_ERROR_NONE;
379 }
380
381 int mesh_ipc_mesh_specific_scan(mesh_service *service, gchar *mesh_id,
382         gint channel)
383 {
384         GVariant *variant = NULL;
385         GError *error = NULL;
386         GVariant *var_dict = NULL;
387         GVariantDict dict;
388
389         enum nl80211_band band = (channel <= 14) ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
390         gushort freq = __channel_to_frequency(channel, band);
391
392         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
393         meshd_check_null_ret_error("connection", service->connection,
394                         MESHD_ERROR_INVALID_PARAMETER);
395         meshd_check_null_ret_error("_gproxy_connman_technology",
396                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
397
398         g_variant_dict_init(&dict, NULL);
399         g_variant_dict_insert(&dict, "Name", "s", mesh_id);
400         g_variant_dict_insert(&dict, "Frequency", "q", freq);
401         var_dict = g_variant_dict_end(&dict);
402
403         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
404                                 g_variant_new("(sv)", "MeshSpecificScan", var_dict),
405                                 G_DBUS_CALL_FLAGS_NONE,
406                                 -1, NULL, &error);
407         if (variant) {
408                 MESH_LOGD("Successfully requested. [MeshSpecificScan]");
409         } else if (error) {
410                 MESH_LOGE("Failed DBus call [%s]", error->message);
411                 g_error_free(error);
412                 return MESHD_ERROR_IO_ERROR;
413         }
414
415         return MESHD_ERROR_NONE;
416 }
417
418 int mesh_ipc_mesh_cancel_scan(mesh_service *service)
419 {
420         GVariant *variant = NULL;
421         GError *error = NULL;
422         GVariant *var_dict = NULL;
423         GVariantDict dict;
424
425         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
426         meshd_check_null_ret_error("connection", service->connection,
427                         MESHD_ERROR_INVALID_PARAMETER);
428         meshd_check_null_ret_error("_gproxy_connman_technology",
429                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
430
431         g_variant_dict_init(&dict, NULL);
432         var_dict = g_variant_dict_end(&dict);
433
434         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
435                                 g_variant_new("(sv)", "AbortScan", var_dict),
436                                 G_DBUS_CALL_FLAGS_NONE,
437                                 -1, NULL, &error);
438         if (variant) {
439                 MESH_LOGD("Successfully requested. [AbortScan]");
440         } else if (error) {
441                 MESH_LOGE("Failed DBus call [%s]", error->message);
442                 g_error_free(error);
443                 return MESHD_ERROR_IO_ERROR;
444         }
445
446         return MESHD_ERROR_NONE;
447 }
448
449 static void _on_scan_result_destroy(gpointer data)
450 {
451         mesh_scan_result_s *scan_item = (mesh_scan_result_s *)data;
452
453         if (scan_item) {
454                 g_free(scan_item->mesh_id);
455                 g_free(scan_item->bssid);
456                 g_free(scan_item->object_path);
457         }
458 }
459
460 static void _get_joined_network(mesh_service *service, GVariant *variant)
461 {
462         GVariantIter *peer = NULL;
463         GVariantIter *property = NULL;
464         gchar *key = NULL;
465         GVariant *val = NULL;
466         gsize len = 0;
467         GVariant *child;
468         gchar *var_string = NULL;
469         const gchar* obj_path = NULL;
470         const gchar* buf = NULL;
471
472         g_variant_get(variant, "(a(oa{sv}))", &peer);
473         while ((child = g_variant_iter_next_value(peer))) {
474                 mesh_network_info_s *joined_info = NULL;
475                 gboolean valid_state = TRUE;
476
477                 MESH_LOGD("    Child : [%s]", g_variant_get_type_string(child));
478                 var_string = g_variant_print(child, FALSE);
479                 MESH_LOGD("    %s", var_string);
480                 g_free(var_string);
481
482                 g_variant_get(child, "(oa{sv})", &obj_path, &property);
483                 MESH_LOGD("  Object: [%s]", obj_path);
484                 if (NULL == obj_path) {
485                         MESH_LOGE("Null object");
486                         continue;
487                 }
488
489                 /* Create an information structure for joined network */
490                 joined_info = g_try_new0(mesh_network_info_s, 1);
491                 if (NULL == joined_info) {
492                         MESH_LOGE("Failed to allocate !");
493                         return;
494                 }
495
496                 while (g_variant_iter_loop(property, "{sv}", &key, &val)) {
497                         if (strcasecmp(key, "Name") == 0)  {
498                                 buf = g_variant_get_string(val, &len);
499                                 joined_info->mesh_id = g_strdup(buf);
500                         }
501                         else if (strcasecmp(key, "Address") == 0)  {
502                                 buf = g_variant_get_string(val, &len);
503                                 joined_info->bssid = g_strdup(buf);
504                         }
505                         else if (strcasecmp(key, "State") == 0)  {
506                                 buf = g_variant_get_string(val, &len);
507                                 MESH_LOGD("    State : %s", buf);
508
509                                 /* Skip ignorable state */
510                                 if (g_strcmp0(buf, "idle") == 0
511                                         || g_strcmp0(buf, "disconnect") == 0
512                                         || g_strcmp0(buf, "failure") == 0) {
513                                         valid_state = FALSE;
514                                         break;
515                                 }
516                         }
517                         else if (strcasecmp(key, "Frequency") == 0)  {
518                                 joined_info->channel = __frequency_to_channel(g_variant_get_uint16(val));
519                         }
520                 }
521
522                 /* Skip ignorable state */
523                 if (FALSE == valid_state) {
524                         g_free(joined_info->mesh_id);
525                         g_free(joined_info->bssid);
526                         continue;
527                 }
528
529                 MESH_LOGD("    Mesh ID : %s", joined_info->mesh_id);
530                 MESH_LOGD("    BSSID   : %s", joined_info->bssid);
531                 MESH_LOGD("    Channel : %d", joined_info->channel);
532                 service->joined_network = joined_info;
533
534                 g_variant_iter_free(property);
535
536                 /* If found, stop loop iteration */
537                 break;
538         }
539         g_variant_iter_free(peer);
540 }
541
542 static void _get_mesh_peers(mesh_service *service, GVariant *variant)
543 {
544         GVariantIter *peer = NULL;
545         GVariantIter *property = NULL;
546         gchar *key = NULL;
547         GVariant *val = NULL;
548         gsize len = 0;
549         GVariant *child;
550         gchar *var_string = NULL;
551         const gchar* obj_path = NULL;
552
553         g_variant_get(variant, "(a(oa{sv}))", &peer);
554         //while (g_variant_iter_next(peer, "oa{sv}", &tmp, &property)) {
555         while ((child = g_variant_iter_next_value(peer))) {
556                 mesh_scan_result_s *scan_info = NULL;
557
558                 scan_info = g_try_new0(mesh_scan_result_s, 1);
559                 if (NULL == scan_info) {
560                         MESH_LOGE("Failed to allocate !");
561                         return;
562                 }
563
564                 MESH_LOGD("    Child : [%s]", g_variant_get_type_string(child));
565                 var_string = g_variant_print(child, FALSE);
566                 MESH_LOGD("    %s", var_string);
567                 g_free(var_string);
568
569                 g_variant_get(child, "(oa{sv})", &obj_path, &property);
570                 if (NULL == obj_path) {
571                         MESH_LOGE("Null object");
572                         g_free(scan_info);
573                         continue;
574                 }
575                 MESH_LOGD("    Obj path : [%s]", obj_path);
576                 scan_info->object_path = g_strdup(obj_path);
577
578                 while (g_variant_iter_loop(property, "{sv}", &key, &val)) {
579                         if (strcasecmp(key, "Name") == 0)  {
580                                 const char *buf = g_variant_get_string(val, &len);
581                                 scan_info->mesh_id = g_strdup(buf);
582                                 MESH_LOGD("    Mesh ID : %s", scan_info->mesh_id);
583                         }
584                         else if (strcasecmp(key, "Address") == 0)  {
585                                 const char *buf = g_variant_get_string(val, &len);
586                                 scan_info->bssid = g_strdup(buf);
587                                 MESH_LOGD("    BSSID : %s", scan_info->bssid);
588                         }
589                         else if (strcasecmp(key, "Frequency") == 0)  {
590                                 scan_info->channel = __frequency_to_channel(g_variant_get_uint16(val));
591                                 MESH_LOGD("    Channel : %d", scan_info->channel);
592                         }
593                         else if (strcasecmp(key, "Strength") == 0)  {
594                                 scan_info->rssi = (gint)g_variant_get_byte(val);
595                                 MESH_LOGD("    RSSI : %d", scan_info->rssi);
596                         }
597                 }
598                 /* Last element */
599                 service->scanned_mesh_network =
600                         g_list_prepend(service->scanned_mesh_network, scan_info);
601
602                 g_variant_iter_free(property);
603         }
604         g_variant_iter_free(peer);
605 }
606
607 int mesh_ipc_get_mesh_peers(mesh_service *service)
608 {
609         GVariant *variant = NULL;
610         GError *error = NULL;
611
612         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
613         meshd_check_null_ret_error("connection", service->connection,
614                         MESHD_ERROR_INVALID_PARAMETER);
615         meshd_check_null_ret_error("_gproxy_connman",
616                         _gproxy_connman, MESHD_ERROR_IO_ERROR);
617
618         variant = g_dbus_proxy_call_sync(_gproxy_connman, "GetMeshPeers",
619                                 NULL,
620                                 G_DBUS_CALL_FLAGS_NONE,
621                                 -1, NULL, &error);
622         if (variant) {
623                 MESH_LOGD("Successfully requested. [GetMeshPeers]");
624
625                 if (service->scanned_mesh_network) {
626                         g_list_free_full(service->scanned_mesh_network, _on_scan_result_destroy);
627                         service->scanned_mesh_network = NULL;
628                 }
629
630                 _get_mesh_peers(service, variant);
631
632                 /* List item is saved with reversed order for efficiency. */
633                 service->scanned_mesh_network =
634                                 g_list_reverse(service->scanned_mesh_network);
635         } else if (error) {
636                 MESH_LOGE("Failed DBus call [%s]", error->message);
637                 g_error_free(error);
638                 return MESHD_ERROR_IO_ERROR;
639         }
640
641         return MESHD_ERROR_NONE;
642 }
643
644 int mesh_ipc_get_joined_mesh_network(mesh_service *service)
645 {
646         GVariant *variant = NULL;
647         GError *error = NULL;
648
649         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
650         meshd_check_null_ret_error("connection", service->connection,
651                         MESHD_ERROR_INVALID_PARAMETER);
652         meshd_check_null_ret_error("_gproxy_connman",
653                         _gproxy_connman, MESHD_ERROR_IO_ERROR);
654
655         variant = g_dbus_proxy_call_sync(_gproxy_connman, "GetMeshPeers",
656                                 NULL,
657                                 G_DBUS_CALL_FLAGS_NONE,
658                                 -1, NULL, &error);
659         if (variant) {
660                 MESH_LOGD("Successfully requested. [GetMeshPeers]");
661
662                 if (service->joined_network) {
663                         g_free(service->joined_network->mesh_id);
664                         g_free(service->joined_network->bssid);
665                         g_free(service->joined_network);
666                         service->joined_network = NULL;
667                 }
668
669                 _get_joined_network(service, variant);
670         } else if (error) {
671                 MESH_LOGE("Failed DBus call [%s]", error->message);
672                 g_error_free(error);
673                 return MESHD_ERROR_IO_ERROR;
674         }
675
676         return MESHD_ERROR_NONE;
677 }
678
679 int mesh_ipc_create_network(mesh_service *service, gchar *mesh_id, gint channel,
680         gint security)
681 {
682         GVariant *variant = NULL;
683         GError *error = NULL;
684         GVariant *var_dict = NULL;
685         GVariantBuilder builder;
686         const gchar* secu = (security == 0) ? "none" : "SAE";
687
688         enum nl80211_band band = (channel <= 14) ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
689         gushort freq = __channel_to_frequency(channel, band);
690
691         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
692         meshd_check_null_ret_error("connection", service->connection,
693                         MESHD_ERROR_INVALID_PARAMETER);
694         meshd_check_null_ret_error("_gproxy_connman_technology",
695                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
696
697         g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
698         g_variant_builder_open(&builder, G_VARIANT_TYPE("a{sv}"));
699
700         g_variant_builder_open(&builder, G_VARIANT_TYPE("{sv}"));
701         g_variant_builder_add(&builder, "s", "Name");
702         g_variant_builder_add(&builder, "v", g_variant_new_string(mesh_id));
703         g_variant_builder_close(&builder); /* {sv} */
704
705         g_variant_builder_open(&builder, G_VARIANT_TYPE("{sv}"));
706         g_variant_builder_add(&builder, "s", "Frequency");
707         g_variant_builder_add(&builder, "v", g_variant_new_uint16(freq));
708         g_variant_builder_close(&builder); /* {sv} */
709
710         g_variant_builder_open(&builder, G_VARIANT_TYPE("{sv}"));
711         g_variant_builder_add(&builder, "s", "Security");
712         g_variant_builder_add(&builder, "v", g_variant_new_string(secu));
713         g_variant_builder_close(&builder); /* {sv} */
714
715         g_variant_builder_close(&builder); /* a{sv} */
716
717         var_dict = g_variant_builder_end(&builder);
718
719         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
720                                 g_variant_new("(sv)", "MeshCreateNetwork", var_dict),
721                                 G_DBUS_CALL_FLAGS_NONE,
722                                 -1, NULL, &error);
723         if (variant) {
724                 MESH_LOGD("Successfully requested. [MeshCreateNetwork]");
725         } else if (error) {
726                 MESH_LOGE("Failed DBus call [%s]", error->message);
727                 g_error_free(error);
728                 return MESHD_ERROR_IO_ERROR;
729         }
730
731         return MESHD_ERROR_NONE;
732 }
733
734 int mesh_ipc_connect_network(mesh_service *service, mesh_scan_result_s *info)
735 {
736         GVariant *variant = NULL;
737         GError *error = NULL;
738
739         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
740         meshd_check_null_ret_error("info", info, MESHD_ERROR_INVALID_PARAMETER);
741
742         variant = g_dbus_connection_call_sync(service->connection,
743                         CONNMAN_SERVER_NAME,
744                         info->object_path,
745                         CONNMAN_INTERFACE_MESH,
746                         "Connect",
747                         NULL, NULL,
748                         G_DBUS_CALL_FLAGS_NONE,
749                         -1, NULL, &error);
750         if (variant) {
751                 MESH_LOGD("Successfully requested. [Connect]");
752         } else if (error) {
753                 LOGE("Failed DBus call [%s]", error->message);
754                 g_error_free(error);
755                 return MESHD_ERROR_IO_ERROR;
756         }
757
758         return MESHD_ERROR_NONE;
759 }
760
761 int mesh_ipc_disconnect_network(mesh_service *service, mesh_scan_result_s *info)
762 {
763         GVariant *variant = NULL;
764         GError *error = NULL;
765
766         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
767         meshd_check_null_ret_error("info", info, MESHD_ERROR_INVALID_PARAMETER);
768
769         variant = g_dbus_connection_call_sync(service->connection,
770                         CONNMAN_SERVER_NAME,
771                         info->object_path,
772                         CONNMAN_INTERFACE_MESH,
773                         "Disconnect",
774                         NULL, NULL,
775                         G_DBUS_CALL_FLAGS_NONE,
776                         -1, NULL, &error);
777         if (variant) {
778                 MESH_LOGD("Successfully requested. [Disconnect]");
779         } else if (error) {
780                 LOGE("Failed DBus call [%s]", error->message);
781                 g_error_free(error);
782                 return MESHD_ERROR_IO_ERROR;
783         }
784
785         return MESHD_ERROR_NONE;
786 }
787
788 int mesh_ipc_remove_network(mesh_service *service, mesh_scan_result_s *info)
789 {
790         GVariant *variant = NULL;
791         GError *error = NULL;
792
793         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
794         meshd_check_null_ret_error("info", info, MESHD_ERROR_INVALID_PARAMETER);
795
796         variant = g_dbus_connection_call_sync(service->connection,
797                         CONNMAN_SERVER_NAME,
798                         info->object_path,
799                         CONNMAN_INTERFACE_MESH,
800                         "Remove",
801                         NULL, NULL,
802                         G_DBUS_CALL_FLAGS_NONE,
803                         -1, NULL, &error);
804         if (variant) {
805                 MESH_LOGD("Successfully requested. [Remove]");
806         } else if (error) {
807                 LOGE("Failed DBus call [%s]", error->message);
808                 g_error_free(error);
809                 return MESHD_ERROR_IO_ERROR;
810         }
811
812         return MESHD_ERROR_NONE;
813 }