Handle error cases
[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         int ret = MESHD_ERROR_NONE;
294         GVariant *variant = NULL;
295         GError *error = NULL;
296         GVariant *var_dict = NULL;
297         GVariantDict dict;
298         mesh_interface_s *info = NULL;
299
300         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
301         meshd_check_null_ret_error("connection", service->connection,
302                         MESHD_ERROR_INVALID_PARAMETER);
303         meshd_check_null_ret_error("_gproxy_connman_technology",
304                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
305
306         info = service->interface_info;
307
308         g_variant_dict_init(&dict, NULL);
309         g_variant_dict_insert(&dict, "Ifname", "s", info->mesh_interface);
310         g_variant_dict_insert(&dict, "ParentIfname", "s", info->base_interface);
311         g_variant_dict_insert(&dict, "BridgeIfname", "s", info->bridge_interface);
312         var_dict = g_variant_dict_end(&dict);
313
314         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
315                                 g_variant_new("(sv)", "MeshInterfaceAdd", var_dict),
316                                 G_DBUS_CALL_FLAGS_NONE,
317                                 -1, NULL, &error);
318         if (variant) {
319                 MESH_LOGD("Successfully requested. [MeshInterfaceAdd]");
320         } else if (error) {
321                 ret = MESHD_ERROR_IO_ERROR;
322                 MESH_LOGE("Failed DBus call [%s]", error->message);
323
324                 /* Interface not exists */
325                 if (g_strrstr(error->message, "No such device"))
326                         ret = MESHD_ERROR_INVALID_PARAMETER;
327                 g_error_free(error);
328         }
329
330         return ret;
331 }
332
333 int mesh_ipc_remove_mesh_interface(mesh_service *service)
334 {
335         GVariant *variant = NULL;
336         GError *error = NULL;
337         GVariant *var_dict = NULL;
338         GVariantDict dict;
339         mesh_interface_s *info = NULL;
340
341         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
342         meshd_check_null_ret_error("connection", service->connection,
343                         MESHD_ERROR_INVALID_PARAMETER);
344         meshd_check_null_ret_error("_gproxy_connman_technology",
345                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
346
347         info = service->interface_info;
348
349         g_variant_dict_init(&dict, NULL);
350         g_variant_dict_insert(&dict, "Ifname", "s", info->mesh_interface);
351         var_dict = g_variant_dict_end(&dict);
352
353         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
354                                 g_variant_new("(sv)", "MeshInterfaceRemove", var_dict),
355                                 G_DBUS_CALL_FLAGS_NONE,
356                                 -1, NULL, &error);
357         if (variant) {
358                 MESH_LOGD("Successfully requested. [MeshInterfaceRemove]");
359         } else if (error) {
360                 MESH_LOGE("Failed DBus call [%s]", error->message);
361                 g_error_free(error);
362                 return MESHD_ERROR_IO_ERROR;
363         }
364
365         return MESHD_ERROR_NONE;
366 }
367
368 int mesh_ipc_mesh_scan(mesh_service *service)
369 {
370         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
371         meshd_check_null_ret_error("connection", service->connection,
372                         MESHD_ERROR_INVALID_PARAMETER);
373         meshd_check_null_ret_error("_gproxy_connman_technology",
374                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
375
376         g_dbus_proxy_call(_gproxy_connman_technology, "Scan",
377                                 NULL,
378                                 G_DBUS_CALL_FLAGS_NONE,
379                                 -1, NULL, NULL, NULL);
380
381         MESH_LOGD("Successfully requested. [Scan]");
382
383         return MESHD_ERROR_NONE;
384 }
385
386 int mesh_ipc_mesh_specific_scan(mesh_service *service, gchar *mesh_id,
387         gint channel)
388 {
389         GVariant *variant = NULL;
390         GError *error = NULL;
391         GVariant *var_dict = NULL;
392         GVariantDict dict;
393
394         enum nl80211_band band = (channel <= 14) ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
395         gushort freq = __channel_to_frequency(channel, band);
396
397         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
398         meshd_check_null_ret_error("connection", service->connection,
399                         MESHD_ERROR_INVALID_PARAMETER);
400         meshd_check_null_ret_error("_gproxy_connman_technology",
401                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
402
403         g_variant_dict_init(&dict, NULL);
404         g_variant_dict_insert(&dict, "Name", "s", mesh_id);
405         g_variant_dict_insert(&dict, "Frequency", "q", freq);
406         var_dict = g_variant_dict_end(&dict);
407
408         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
409                                 g_variant_new("(sv)", "MeshSpecificScan", var_dict),
410                                 G_DBUS_CALL_FLAGS_NONE,
411                                 -1, NULL, &error);
412         if (variant) {
413                 MESH_LOGD("Successfully requested. [MeshSpecificScan]");
414         } else if (error) {
415                 MESH_LOGE("Failed DBus call [%s]", error->message);
416                 g_error_free(error);
417                 return MESHD_ERROR_IO_ERROR;
418         }
419
420         return MESHD_ERROR_NONE;
421 }
422
423 int mesh_ipc_mesh_cancel_scan(mesh_service *service)
424 {
425         GVariant *variant = NULL;
426         GError *error = NULL;
427         GVariant *var_dict = NULL;
428         GVariantDict dict;
429
430         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
431         meshd_check_null_ret_error("connection", service->connection,
432                         MESHD_ERROR_INVALID_PARAMETER);
433         meshd_check_null_ret_error("_gproxy_connman_technology",
434                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
435
436         g_variant_dict_init(&dict, NULL);
437         var_dict = g_variant_dict_end(&dict);
438
439         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
440                                 g_variant_new("(sv)", "AbortScan", var_dict),
441                                 G_DBUS_CALL_FLAGS_NONE,
442                                 -1, NULL, &error);
443         if (variant) {
444                 MESH_LOGD("Successfully requested. [AbortScan]");
445         } else if (error) {
446                 MESH_LOGE("Failed DBus call [%s]", error->message);
447                 g_error_free(error);
448                 return MESHD_ERROR_IO_ERROR;
449         }
450
451         return MESHD_ERROR_NONE;
452 }
453
454 static void _on_scan_result_destroy(gpointer data)
455 {
456         mesh_scan_result_s *scan_item = (mesh_scan_result_s *)data;
457
458         if (scan_item) {
459                 g_free(scan_item->mesh_id);
460                 g_free(scan_item->bssid);
461                 g_free(scan_item->object_path);
462         }
463 }
464
465 static void _get_joined_network(mesh_service *service, GVariant *variant)
466 {
467         GVariantIter *peer = NULL;
468         GVariantIter *property = NULL;
469         gchar *key = NULL;
470         GVariant *val = NULL;
471         gsize len = 0;
472         GVariant *child;
473         const gchar* obj_path = NULL;
474         const gchar* buf = NULL;
475
476         g_variant_get(variant, "(a(oa{sv}))", &peer);
477         while ((child = g_variant_iter_next_value(peer))) {
478                 mesh_network_info_s *joined_info = NULL;
479                 gboolean valid_state = TRUE;
480
481                 g_variant_get(child, "(oa{sv})", &obj_path, &property);
482                 MESH_LOGD("  Object: [%s]", obj_path);
483                 if (NULL == obj_path) {
484                         MESH_LOGE("Null object");
485                         continue;
486                 }
487
488                 /* Create an information structure for joined network */
489                 joined_info = g_try_new0(mesh_network_info_s, 1);
490                 if (NULL == joined_info) {
491                         MESH_LOGE("Failed to allocate !");
492                         return;
493                 }
494
495                 while (g_variant_iter_loop(property, "{sv}", &key, &val)) {
496                         if (strcasecmp(key, "Name") == 0)  {
497                                 buf = g_variant_get_string(val, &len);
498                                 joined_info->mesh_id = g_strdup(buf);
499                         }
500                         else if (strcasecmp(key, "Address") == 0)  {
501                                 buf = g_variant_get_string(val, &len);
502                                 joined_info->bssid = g_strdup(buf);
503                         }
504                         else if (strcasecmp(key, "State") == 0)  {
505                                 buf = g_variant_get_string(val, &len);
506                                 MESH_LOGD("    State : %s", buf);
507
508                                 /* Skip ignorable state */
509                                 if (g_strcmp0(buf, "idle") == 0
510                                         || g_strcmp0(buf, "disconnect") == 0
511                                         || g_strcmp0(buf, "failure") == 0) {
512                                         valid_state = FALSE;
513                                         break;
514                                 }
515                         }
516                         else if (strcasecmp(key, "Frequency") == 0)  {
517                                 joined_info->channel = __frequency_to_channel(g_variant_get_uint16(val));
518                         }
519                 }
520
521                 /* Skip ignorable state */
522                 if (FALSE == valid_state) {
523                         g_free(joined_info->mesh_id);
524                         g_free(joined_info->bssid);
525                         continue;
526                 }
527
528                 MESH_LOGD("    Mesh ID : %s", joined_info->mesh_id);
529                 MESH_LOGD("    BSSID   : %s", joined_info->bssid);
530                 MESH_LOGD("    Channel : %d", joined_info->channel);
531                 service->joined_network = joined_info;
532
533                 g_variant_iter_free(property);
534
535                 /* If found, stop loop iteration */
536                 break;
537         }
538         g_variant_iter_free(peer);
539 }
540
541 static void _get_mesh_peers(mesh_service *service, GVariant *variant)
542 {
543         GVariantIter *peer = NULL;
544         GVariantIter *property = NULL;
545         gchar *key = NULL;
546         GVariant *val = NULL;
547         gsize len = 0;
548         GVariant *child;
549         const gchar* obj_path = NULL;
550
551         g_variant_get(variant, "(a(oa{sv}))", &peer);
552         while ((child = g_variant_iter_next_value(peer))) {
553                 mesh_scan_result_s *scan_info = NULL;
554
555                 scan_info = g_try_new0(mesh_scan_result_s, 1);
556                 if (NULL == scan_info) {
557                         MESH_LOGE("Failed to allocate !");
558                         return;
559                 }
560
561                 g_variant_get(child, "(oa{sv})", &obj_path, &property);
562                 if (NULL == obj_path) {
563                         MESH_LOGE("Null object");
564                         g_free(scan_info);
565                         continue;
566                 }
567                 MESH_LOGD("    Obj path : [%s]", obj_path);
568                 scan_info->object_path = g_strdup(obj_path);
569
570                 while (g_variant_iter_loop(property, "{sv}", &key, &val)) {
571                         if (strcasecmp(key, "Name") == 0)  {
572                                 const char *buf = g_variant_get_string(val, &len);
573                                 scan_info->mesh_id = g_strdup(buf);
574                                 MESH_LOGD("    Mesh ID : %s", scan_info->mesh_id);
575                         }
576                         else if (strcasecmp(key, "Address") == 0)  {
577                                 const char *buf = g_variant_get_string(val, &len);
578                                 scan_info->bssid = g_strdup(buf);
579                                 MESH_LOGD("    BSSID : %s", scan_info->bssid);
580                         }
581                         else if (strcasecmp(key, "State") == 0)  {
582                                 const char *buf = g_variant_get_string(val, &len);
583                                 MESH_LOGD("    State : %s", buf);
584                         }
585                         else if (strcasecmp(key, "Security") == 0)  {
586                                 const char *buf = g_variant_get_string(val, &len);
587                                 MESH_LOGD("    Security : %s", buf);
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, "Favorite") == 0)  {
594                                 const char *buf = g_variant_get_string(val, &len);
595                                 MESH_LOGD("    Favorite : %s", buf);
596                         }
597                         else if (strcasecmp(key, "Strength") == 0)  {
598                                 scan_info->rssi = (gint)g_variant_get_byte(val);
599                                 MESH_LOGD("    RSSI : %d", scan_info->rssi);
600                         }
601                 }
602                 /* Last element */
603                 service->scanned_mesh_network =
604                         g_list_prepend(service->scanned_mesh_network, scan_info);
605
606                 g_variant_iter_free(property);
607         }
608         g_variant_iter_free(peer);
609 }
610
611 int mesh_ipc_get_mesh_peers(mesh_service *service)
612 {
613         GVariant *variant = NULL;
614         GError *error = NULL;
615
616         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
617         meshd_check_null_ret_error("connection", service->connection,
618                         MESHD_ERROR_INVALID_PARAMETER);
619         meshd_check_null_ret_error("_gproxy_connman",
620                         _gproxy_connman, MESHD_ERROR_IO_ERROR);
621
622         variant = g_dbus_proxy_call_sync(_gproxy_connman, "GetMeshPeers",
623                                 NULL,
624                                 G_DBUS_CALL_FLAGS_NONE,
625                                 -1, NULL, &error);
626         if (variant) {
627                 MESH_LOGD("Successfully requested. [GetMeshPeers]");
628
629                 if (service->scanned_mesh_network) {
630                         g_list_free_full(service->scanned_mesh_network, _on_scan_result_destroy);
631                         service->scanned_mesh_network = NULL;
632                 }
633
634                 _get_mesh_peers(service, variant);
635
636                 /* List item is saved with reversed order for efficiency. */
637                 service->scanned_mesh_network =
638                                 g_list_reverse(service->scanned_mesh_network);
639         } else if (error) {
640                 MESH_LOGE("Failed DBus call [%s]", error->message);
641                 g_error_free(error);
642                 return MESHD_ERROR_IO_ERROR;
643         }
644
645         return MESHD_ERROR_NONE;
646 }
647
648 int mesh_ipc_get_joined_mesh_network(mesh_service *service)
649 {
650         GVariant *variant = NULL;
651         GError *error = NULL;
652
653         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
654         meshd_check_null_ret_error("connection", service->connection,
655                         MESHD_ERROR_INVALID_PARAMETER);
656         meshd_check_null_ret_error("_gproxy_connman",
657                         _gproxy_connman, MESHD_ERROR_IO_ERROR);
658
659         variant = g_dbus_proxy_call_sync(_gproxy_connman, "GetMeshPeers",
660                                 NULL,
661                                 G_DBUS_CALL_FLAGS_NONE,
662                                 -1, NULL, &error);
663         if (variant) {
664                 MESH_LOGD("Successfully requested. [GetMeshPeers]");
665
666                 if (service->joined_network) {
667                         g_free(service->joined_network->mesh_id);
668                         g_free(service->joined_network->bssid);
669                         g_free(service->joined_network);
670                         service->joined_network = NULL;
671                 }
672
673                 _get_joined_network(service, variant);
674         } else if (error) {
675                 MESH_LOGE("Failed DBus call [%s]", error->message);
676                 g_error_free(error);
677                 return MESHD_ERROR_IO_ERROR;
678         }
679
680         return MESHD_ERROR_NONE;
681 }
682
683 int mesh_ipc_create_network(mesh_service *service, gchar *mesh_id, gint channel,
684         gint security)
685 {
686         GVariant *variant = NULL;
687         GError *error = NULL;
688         GVariant *var_dict = NULL;
689         GVariantBuilder builder;
690         const gchar* secu = (security == 0) ? "none" : "SAE";
691
692         enum nl80211_band band = (channel <= 14) ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
693         gushort freq = __channel_to_frequency(channel, band);
694
695         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
696         meshd_check_null_ret_error("connection", service->connection,
697                         MESHD_ERROR_INVALID_PARAMETER);
698         meshd_check_null_ret_error("_gproxy_connman_technology",
699                         _gproxy_connman_technology, MESHD_ERROR_IO_ERROR);
700
701         g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
702         g_variant_builder_open(&builder, G_VARIANT_TYPE("a{sv}"));
703
704         g_variant_builder_open(&builder, G_VARIANT_TYPE("{sv}"));
705         g_variant_builder_add(&builder, "s", "Name");
706         g_variant_builder_add(&builder, "v", g_variant_new_string(mesh_id));
707         g_variant_builder_close(&builder); /* {sv} */
708
709         g_variant_builder_open(&builder, G_VARIANT_TYPE("{sv}"));
710         g_variant_builder_add(&builder, "s", "Frequency");
711         g_variant_builder_add(&builder, "v", g_variant_new_uint16(freq));
712         g_variant_builder_close(&builder); /* {sv} */
713
714         g_variant_builder_open(&builder, G_VARIANT_TYPE("{sv}"));
715         g_variant_builder_add(&builder, "s", "Security");
716         g_variant_builder_add(&builder, "v", g_variant_new_string(secu));
717         g_variant_builder_close(&builder); /* {sv} */
718
719         g_variant_builder_close(&builder); /* a{sv} */
720
721         var_dict = g_variant_builder_end(&builder);
722
723         variant = g_dbus_proxy_call_sync(_gproxy_connman_technology, "MeshCommands",
724                                 g_variant_new("(sv)", "MeshCreateNetwork", var_dict),
725                                 G_DBUS_CALL_FLAGS_NONE,
726                                 -1, NULL, &error);
727         if (variant) {
728                 MESH_LOGD("Successfully requested. [MeshCreateNetwork]");
729         } else if (error) {
730                 MESH_LOGE("Failed DBus call [%s]", error->message);
731                 g_error_free(error);
732                 return MESHD_ERROR_IO_ERROR;
733         }
734
735         return MESHD_ERROR_NONE;
736 }
737
738 int mesh_ipc_connect_network(mesh_service *service, mesh_scan_result_s *info)
739 {
740         int ret = MESHD_ERROR_NONE;
741         GVariant *variant = NULL;
742         GError *error = NULL;
743
744         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
745         meshd_check_null_ret_error("info", info, MESHD_ERROR_INVALID_PARAMETER);
746
747         variant = g_dbus_connection_call_sync(service->connection,
748                         CONNMAN_SERVER_NAME,
749                         info->object_path,
750                         CONNMAN_INTERFACE_MESH,
751                         "Connect",
752                         NULL, NULL,
753                         G_DBUS_CALL_FLAGS_NONE,
754                         -1, NULL, &error);
755         if (variant) {
756                 MESH_LOGD("Successfully requested. [Connect]");
757         } else if (error) {
758                 ret = MESHD_ERROR_IO_ERROR;
759                 LOGE("Failed DBus call [%s]", error->message);
760
761                 if (g_strrstr(error->message, "Already exists"))
762                         ret = MESHD_ERROR_ALREADY_REGISTERED;
763                 else
764                         ret = MESHD_ERROR_IO_ERROR;
765
766                 g_error_free(error);
767         }
768
769         return ret;
770 }
771
772 int mesh_ipc_disconnect_network(mesh_service *service, mesh_scan_result_s *info)
773 {
774         GVariant *variant = NULL;
775         GError *error = NULL;
776
777         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
778         meshd_check_null_ret_error("info", info, MESHD_ERROR_INVALID_PARAMETER);
779
780         variant = g_dbus_connection_call_sync(service->connection,
781                         CONNMAN_SERVER_NAME,
782                         info->object_path,
783                         CONNMAN_INTERFACE_MESH,
784                         "Disconnect",
785                         NULL, NULL,
786                         G_DBUS_CALL_FLAGS_NONE,
787                         -1, NULL, &error);
788         if (variant) {
789                 MESH_LOGD("Successfully requested. [Disconnect]");
790         } else if (error) {
791                 LOGE("Failed DBus call [%s]", error->message);
792                 g_error_free(error);
793                 return MESHD_ERROR_IO_ERROR;
794         }
795
796         return MESHD_ERROR_NONE;
797 }
798
799 int mesh_ipc_remove_network(mesh_service *service, mesh_scan_result_s *info)
800 {
801         GVariant *variant = NULL;
802         GError *error = NULL;
803
804         meshd_check_null_ret_error("service", service, MESHD_ERROR_INVALID_PARAMETER);
805         meshd_check_null_ret_error("info", info, MESHD_ERROR_INVALID_PARAMETER);
806
807         variant = g_dbus_connection_call_sync(service->connection,
808                         CONNMAN_SERVER_NAME,
809                         info->object_path,
810                         CONNMAN_INTERFACE_MESH,
811                         "Remove",
812                         NULL, NULL,
813                         G_DBUS_CALL_FLAGS_NONE,
814                         -1, NULL, &error);
815         if (variant) {
816                 MESH_LOGD("Successfully requested. [Remove]");
817         } else if (error) {
818                 LOGE("Failed DBus call [%s]", error->message);
819                 g_error_free(error);
820                 return MESHD_ERROR_IO_ERROR;
821         }
822
823         return MESHD_ERROR_NONE;
824 }