2 * Network Configuration Module
4 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "wmesh-log.h"
26 #include "wmesh-util.h"
27 #include "wmesh-service-interface.h"
28 #include "wmesh-generated-code.h"
30 #include "wmesh-bridge.h"
31 #include "wmesh-netlink.h"
32 #include "wmesh-interface.h"
33 #include "wmesh-request.h"
34 #include "wmesh-softap.h"
35 #include "wmesh-gdbus.h"
37 int wmesh_request_set_mesh_gate(const char* bridge_interface,
38 const char* mesh_interface, const char* external_interface)
40 int ret = WMESHD_ERROR_NONE;
43 ret = wmesh_interface_check_external_exists(external_interface, &state);
44 if (WMESHD_ERROR_NONE != ret) {
45 WMESH_LOGE("Failed to get external interface state !");
46 return WMESHD_ERROR_OPERATION_FAILED;
48 WMESH_LOGD(" Ethernet cable state [%s]",
49 (state) ? "Connected" : "Disconnected");
52 ret = wmesh_bridge_set_stp(bridge_interface, TRUE);
53 if (WMESHD_ERROR_NONE != ret) {
54 WMESH_LOGE("Failed to turn STP on !");
58 /* Set mesh parameters */
59 ret = wmesh_netlink_set_mesh_parameter(mesh_interface,
60 "mesh_hwmp_rootmode", 4);
61 if (WMESHD_ERROR_NONE != ret)
62 WMESH_LOGE("Failed to set [mesh_hwmp_rootmode] : 4");
64 ret = wmesh_netlink_set_mesh_parameter(mesh_interface,
65 "mesh_gate_announcements", 1);
66 if (WMESHD_ERROR_NONE != ret)
67 WMESH_LOGE("Failed to set [mesh_gate_announcements] : 1");
69 return WMESHD_ERROR_NONE;
72 int wmesh_request_unset_mesh_gate(const char* bridge_interface,
73 const char* mesh_interface, const char* external_interface)
75 int ret = WMESHD_ERROR_NONE;
77 NOTUSED(external_interface);
79 /* Set mesh parameters */
80 ret = wmesh_netlink_set_mesh_parameter(mesh_interface,
81 "mesh_hwmp_rootmode", 0);
82 if (WMESHD_ERROR_NONE != ret)
83 WMESH_LOGE("Failed to set [mesh_hwmp_rootmode] : 0");
85 ret = wmesh_netlink_set_mesh_parameter(mesh_interface,
86 "mesh_gate_announcements", 0);
87 if (WMESHD_ERROR_NONE != ret)
88 WMESH_LOGE("Failed to set [mesh_gate_announcements] : 0");
91 ret = wmesh_bridge_set_stp(bridge_interface, FALSE);
92 if (WMESHD_ERROR_NONE != ret) {
93 WMESH_LOGE("Failed to turn STP off!");
97 return WMESHD_ERROR_NONE;
100 int wmesh_request_add_bridge_interface(const char* bridge_interface,
101 const char* interface)
103 int ret = WMESHD_ERROR_NONE;
105 if (NULL == bridge_interface || NULL == interface) {
106 WMESH_LOGE("Invalid parameters");
107 return WMESHD_ERROR_INVALID_PARAMETER;
110 WMESH_LOGD("Adding interface[%s] into bridge[%s]...", interface, bridge_interface);
112 /* Add external interface into bridge */
113 ret = wmesh_bridge_add_interface(bridge_interface, interface);
114 if (WMESHD_ERROR_NONE != ret) {
115 WMESH_LOGE("Failed to add interface !");
119 /* Make external interface down */
120 ret = wmesh_interface_set(interface, NULL, WMESH_INTERFACE_DOWN);
121 if (WMESHD_ERROR_NONE != ret) {
122 WMESH_LOGE("Failed to change external interface state");
126 /* Make external interface up with cleared IP */
127 ret = wmesh_interface_set(interface, "0.0.0.0", WMESH_INTERFACE_UP);
128 if (WMESHD_ERROR_NONE != ret) {
129 WMESH_LOGE("Failed to change external interface state");
133 return WMESHD_ERROR_NONE;
136 int wmesh_request_remove_bridge_interface(const char* bridge_interface,
137 const char* interface)
139 int ret = WMESHD_ERROR_NONE;
141 if (NULL == bridge_interface || NULL == interface) {
142 WMESH_LOGE("Invalid parameters");
143 return WMESHD_ERROR_INVALID_PARAMETER;
146 WMESH_LOGD("Removing interface[%s] from bridge[%s]...", interface, bridge_interface);
148 ret = wmesh_interface_check_bridge_interface_exists(bridge_interface, interface);
149 if (WMESHD_ERROR_NONE != ret) {
150 WMESH_LOGD("Interface is not exist in bridge");
151 return WMESHD_ERROR_NONE;
154 /* Remove external interface into bridge */
155 ret = wmesh_bridge_del_interface(bridge_interface, interface);
156 if (WMESHD_ERROR_NONE != ret) {
157 WMESH_LOGE("Failed to remove interface !");
161 return WMESHD_ERROR_NONE;
164 int wmesh_request_set_softap_config(const char* softap_interface,
165 const char *ssid, const char* mode, int channel, int visibility,
166 int max_sta, int security, const char* passphrase)
168 int ret = WMESHD_ERROR_NONE;
170 WMESH_LOGD("Setting configuration for SoftAP");
172 ret = wmesh_softap_set_configuration(softap_interface, ssid, mode, channel,
173 visibility, max_sta, security, passphrase);
174 if (WMESHD_ERROR_NONE != ret) {
175 WMESH_LOGE("Failed to set Configuration for SoftAP");
182 int wmesh_request_get_softap_config(char **softap_interface, char **ssid,
183 char **mode, int *channel, int *visibility,
184 int *max_sta, int *security, char **passphrase)
186 int ret = WMESHD_ERROR_NONE;
188 WMESH_LOGD("Get configuration for SoftAP");
190 ret = wmesh_softap_get_configuration(softap_interface, ssid, mode, channel,
191 visibility, max_sta, security, passphrase);
192 if (WMESHD_ERROR_NONE != ret) {
193 WMESH_LOGE("Failed to get Configuration for SoftAP");
200 int wmesh_request_enable_softap(
201 const char* bridge_interface, const char* softap_interface)
203 int ret = WMESHD_ERROR_NONE;
205 WMESH_LOGD("Request to turn SoftAP on");
206 ret = wmesh_softap_enable_softap(softap_interface);
207 if (WMESHD_ERROR_NONE != ret) {
208 WMESH_LOGE("Failed to wmesh_softap_enable_softap");
212 /* Add softAP interface into bridge */
213 ret = wmesh_request_add_bridge_interface(bridge_interface, softap_interface);
214 if (WMESHD_ERROR_NONE != ret) {
215 wmesh_softap_disable_softap();
222 int wmesh_request_disable_softap(
223 const char* bridge_interface, const char* softap_interface)
225 int ret = WMESHD_ERROR_NONE;
227 WMESH_LOGD("Request to turn SoftAP off");
228 ret = wmesh_softap_disable_softap();
230 /* Remove softAP interface from bridge */
231 ret = wmesh_request_remove_bridge_interface(bridge_interface, softap_interface);
232 if (WMESHD_ERROR_NONE != ret)
238 bool wmesh_request_check_softap_status()
240 WMESH_LOGD("Check softAp status");
242 return wmesh_softap_check_softap_status();
245 int wmesh_request_get_station_info(const char* mesh_interface, GList **station_list)
247 int ret = WMESHD_ERROR_NONE;
249 WMESH_LOGD("Request to get station info");
251 /* Get station info */
252 ret = wmesh_netlink_get_station_info(mesh_interface, station_list);
253 if (WMESHD_ERROR_NONE != ret)
256 return WMESHD_ERROR_NONE;
259 int wmesh_request_get_mpath_info(const char* mesh_interface, GList **mpath_list)
261 int ret = WMESHD_ERROR_NONE;
263 WMESH_LOGD("Request to get mpath info");
266 ret = wmesh_netlink_get_mpath_info(mesh_interface, mpath_list);
267 if (WMESHD_ERROR_NONE != ret)
270 return WMESHD_ERROR_NONE;
273 int wmesh_request_get_meshconf_info(wmesh_service *service)
275 int ret = WMESHD_ERROR_NONE;
277 WMESH_LOGD("Request to get meshconf info");
280 ret = wmesh_netlink_get_meshconf_info(service);
281 if (WMESHD_ERROR_NONE != ret)
284 return WMESHD_ERROR_NONE;
287 int wmesh_request_register_event_handler(wmesh_service *service)
289 int ret = WMESHD_ERROR_NONE;
291 WMESH_LOGD("Request to register mesh event handler");
294 ret = wmesh_netlink_register_event_handler(service);
295 if (WMESHD_ERROR_NONE != ret)
298 return WMESHD_ERROR_NONE;
301 int wmesh_request_unregister_event_handler()
303 int ret = WMESHD_ERROR_NONE;
305 WMESH_LOGD("Request to unregister mesh event handler");
308 ret = wmesh_netlink_unregister_event_handler();
309 if (WMESHD_ERROR_NONE != ret)
312 return WMESHD_ERROR_NONE;
315 int wmesh_request_enable_network(wmesh_service *service)
318 wmesh_interface_s *info = NULL;
320 if (NULL == service) {
321 WMESH_LOGE("Invalid parameter");
322 return WMESHD_ERROR_INVALID_PARAMETER;
325 WMESH_LOGD("[IPC] Enable mesh network");
327 /* Check if mesh interface exists */
328 info = service->interface_info;
329 ret = wmesh_interface_check(info->mesh_interface);
330 if (WMESHD_ERROR_NONE == ret) {
331 /* Interface already exists */
332 return WMESHD_ERROR_NONE;
335 ret = wmesh_gdbus_create_mesh_interface(service);
336 if (WMESHD_ERROR_NONE != ret) {
337 WMESH_LOGE("Failed to create mesh network");
341 return WMESHD_ERROR_NONE;
344 int wmesh_request_disable_network(wmesh_service *service)
347 if (NULL == service) {
348 WMESH_LOGE("Invalid parameter");
349 return WMESHD_ERROR_INVALID_PARAMETER;
352 WMESH_LOGD("[IPC] Disable mesh network");
354 ret = wmesh_gdbus_remove_mesh_interface(service);
355 if (WMESHD_ERROR_NONE != ret) {
356 WMESH_LOGE("Failed to disable mesh network");
360 return WMESHD_ERROR_NONE;
363 int wmesh_request_scan(wmesh_service *service)
366 if (NULL == service) {
367 WMESH_LOGE("Invalid parameter");
368 return WMESHD_ERROR_INVALID_PARAMETER;
371 WMESH_LOGD("[IPC] Request scan for mesh network");
373 ret = wmesh_gdbus_mesh_scan(service);
374 if (WMESHD_ERROR_NONE != ret) {
375 WMESH_LOGE("Failed to request scan for mesh network");
379 return WMESHD_ERROR_NONE;
382 int wmesh_request_specific_scan(wmesh_service *service, gchar *mesh_id, gint channel)
385 if (NULL == service) {
386 WMESH_LOGE("Invalid parameter");
387 return WMESHD_ERROR_INVALID_PARAMETER;
390 WMESH_LOGD("[IPC] Request specific scan for mesh network");
392 ret = wmesh_gdbus_mesh_specific_scan(service, mesh_id, channel);
393 if (WMESHD_ERROR_NONE != ret) {
394 WMESH_LOGE("Failed to request specific scan for mesh network");
398 return WMESHD_ERROR_NONE;
401 int wmesh_request_cancel_scan(wmesh_service *service)
404 if (NULL == service) {
405 WMESH_LOGE("Invalid parameter");
406 return WMESHD_ERROR_INVALID_PARAMETER;
409 WMESH_LOGD("[IPC] Cancel scan for mesh network");
411 ret = wmesh_gdbus_mesh_cancel_scan(service);
412 if (WMESHD_ERROR_NONE != ret) {
413 WMESH_LOGE("Failed to cancel scan for mesh network");
417 return WMESHD_ERROR_NONE;
420 int wmesh_request_get_networks(wmesh_service *service)
423 if (NULL == service) {
424 WMESH_LOGE("Invalid parameter");
425 return WMESHD_ERROR_INVALID_PARAMETER;
428 WMESH_LOGD("[IPC] Get mesh networks");
430 ret = wmesh_gdbus_get_mesh_networks(service);
431 if (WMESHD_ERROR_NONE != ret) {
432 WMESH_LOGE("Failed to get mesh networks !");
436 return WMESHD_ERROR_NONE;
439 static int _select_matched_network(GList *scanned_network,
440 const char *mesh_id, int mesh_channel, wmeshd_security_type_e sec,
441 wmesh_scan_result_s **info)
443 int ret = WMESHD_ERROR_NONE;
445 wmesh_scan_result_s *item = NULL;
446 gboolean found = FALSE;
448 wmeshd_check_null_ret_error("scanned_network", scanned_network,
449 WMESHD_ERROR_INVALID_PARAMETER);
451 iter = scanned_network;
452 while (iter != NULL) {
453 item = (wmesh_scan_result_s*)iter->data;
455 if (g_strcmp0(mesh_id, item->mesh_id) == 0) {
456 if (item->channel == mesh_channel && item->security == sec) {
462 iter = g_list_next(iter);
466 return WMESHD_ERROR_NO_DATA;
471 int wmesh_request_get_joined_network(wmesh_service *service)
475 if (NULL == service) {
476 WMESH_LOGE("Invalid parameter");
477 return WMESHD_ERROR_INVALID_PARAMETER;
480 WMESH_LOGD("[IPC] Get joined mesh network");
482 ret = wmesh_gdbus_get_joined_mesh_network(service);
483 if (WMESHD_ERROR_NONE != ret) {
484 WMESH_LOGE("Failed to get joined mesh network");
488 return WMESHD_ERROR_NONE;
491 int wmesh_request_get_connected_peers(wmesh_service *service)
495 if (NULL == service) {
496 WMESH_LOGE("Invalid parameter");
497 return WMESHD_ERROR_INVALID_PARAMETER;
500 WMESH_LOGD("[IPC] Get connected mesh peers");
502 ret = wmesh_gdbus_get_connected_peers(service);
503 if (WMESHD_ERROR_NONE != ret) {
504 WMESH_LOGE("Failed to get connected mesh peers");
508 return WMESHD_ERROR_NONE;
511 int wmesh_request_create_mesh_network(wmesh_service *service, gchar *mesh_id,
512 gint channel, wmeshd_security_type_e sec)
516 if (NULL == service) {
517 WMESH_LOGE("Invalid parameter");
518 return WMESHD_ERROR_INVALID_PARAMETER;
521 WMESH_LOGD("[IPC] Create a new mesh network");
523 ret = wmesh_gdbus_create_network(service, mesh_id, channel, sec);
524 if (WMESHD_ERROR_NONE != ret) {
525 WMESH_LOGE("Failed to create mesh network");
529 return WMESHD_ERROR_NONE;
532 int wmesh_request_connect_mesh_network(wmesh_service *service, gchar *mesh_id,
533 gint channel, wmeshd_security_type_e sec, gchar *passphrase)
536 wmesh_scan_result_s *info = NULL;
538 if (NULL == service) {
539 WMESH_LOGE("Invalid parameter");
540 return WMESHD_ERROR_INVALID_PARAMETER;
543 WMESH_LOGD("[IPC] Connect mesh network");
545 /* Get mesh_id and channel from saved network */
546 ret = _select_matched_network(service->scanned_mesh_network,
547 mesh_id, channel, sec, &info);
548 if (WMESHD_ERROR_NONE != ret) {
549 WMESH_LOGE("Failed to mesh_network_get_first_mesh_network");
554 if (WMESHD_SECURITY_NONE != sec) {
555 if (NULL != passphrase) {
556 ret = wmesh_gdbus_set_passphrase(service, info, passphrase);
557 if (WMESHD_ERROR_NONE != ret) {
558 WMESH_LOGE("Failed to set passphrase for mesh network");
562 WMESH_LOGE("Passphrase is required !");
563 return WMESHD_ERROR_INVALID_PARAMETER;
567 ret = wmesh_gdbus_connect_network(service, info);
568 if (WMESHD_ERROR_NONE != ret) {
569 WMESH_LOGE("Failed to connect mesh network");
573 return WMESHD_ERROR_NONE;
576 int wmesh_request_disconnect_mesh_network(wmesh_service *service,
577 gchar *mesh_id, gint channel, wmeshd_security_type_e sec)
580 wmesh_scan_result_s *info = NULL;
582 if (NULL == service) {
583 WMESH_LOGE("Invalid parameter");
584 return WMESHD_ERROR_INVALID_PARAMETER;
587 WMESH_LOGD("[IPC] Disconnect mesh network");
589 /* Get mesh_id and channel from saved network */
590 ret = _select_matched_network(service->scanned_mesh_network,
591 mesh_id, channel, sec, &info);
592 if (WMESHD_ERROR_NONE != ret) {
593 WMESH_LOGE("Failed to _select_matched_network");
597 ret = wmesh_gdbus_disconnect_network(service, info);
598 if (WMESHD_ERROR_NONE != ret) {
599 WMESH_LOGE("Failed to disconnect mesh network");
603 return WMESHD_ERROR_NONE;
606 int wmesh_request_remove_mesh_network(wmesh_service *service,
607 gchar *mesh_id, gint channel, wmeshd_security_type_e sec)
610 wmesh_scan_result_s *info = NULL;
612 if (NULL == service) {
613 WMESH_LOGE("Invalid parameter");
614 return WMESHD_ERROR_INVALID_PARAMETER;
617 WMESH_LOGD("[IPC] Remove mesh network");
619 /* Get mesh_id and channel from saved network */
620 ret = _select_matched_network(service->scanned_mesh_network,
621 mesh_id, channel, sec, &info);
622 if (WMESHD_ERROR_NONE != ret) {
623 WMESH_LOGE("Failed to _select_matched_network");
627 ret = wmesh_gdbus_remove_network(service, info);
628 if (WMESHD_ERROR_NONE != ret) {
629 WMESH_LOGE("Failed to remove mesh network");
633 return WMESHD_ERROR_NONE;
637 void wmesh_notify_scan_done()
639 NetWmesh *object = wmeshd_dbus_get_object();
641 net_wmesh_emit_scan_done(object);
644 void wmesh_notify_connection_state(const char* mesh_id, const char* bssid,
645 int channel, wmeshd_security_type_e sec, wmeshd_connection_state_e state)
647 NetWmesh *object = wmeshd_dbus_get_object();
649 net_wmesh_emit_connection_state(object, mesh_id, bssid, channel, (int)sec, (int)state);
652 void wmesh_notify_station_joined(const char* bssid,
653 wmeshd_station_type_e station_type)
655 NetWmesh *object = wmeshd_dbus_get_object();
657 net_wmesh_emit_sta_joined(object, bssid, station_type);
660 void wmesh_notify_station_left(const char* bssid,
661 wmeshd_station_type_e station_type)
663 NetWmesh *object = wmeshd_dbus_get_object();
665 net_wmesh_emit_sta_left(object, bssid, station_type);