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.
26 #include "mesh-util.h"
27 #include "mesh-service-interface.h"
28 #include "mesh-generated-code.h"
30 #include "mesh-bridge.h"
31 #include "mesh-netlink.h"
32 #include "mesh-interface.h"
33 #include "mesh-request.h"
34 #include "mesh-softap.h"
35 #include "mesh-gdbus.h"
37 int mesh_request_set_mesh_gate(const char* bridge_interface,
38 const char* mesh_interface, const char* external_interface)
40 int ret = MESHD_ERROR_NONE;
43 ret = mesh_interface_check_external_exists(external_interface, &state);
44 if (MESHD_ERROR_NONE != ret) {
45 MESH_LOGE("Failed to get external interface state !");
46 return MESHD_ERROR_OPERATION_FAILED;
51 ret = mesh_bridge_set_stp(bridge_interface, TRUE);
52 if (MESHD_ERROR_NONE != ret) {
53 MESH_LOGE("Failed to turn STP on !");
57 /* Set mesh parameters */
58 ret = mesh_netlink_set_mesh_parameter(mesh_interface,
59 "mesh_hwmp_rootmode", 4);
60 if (MESHD_ERROR_NONE != ret)
61 MESH_LOGE("Failed to set [mesh_hwmp_rootmode] : 4");
63 ret = mesh_netlink_set_mesh_parameter(mesh_interface,
64 "mesh_gate_announcements", 1);
65 if (MESHD_ERROR_NONE != ret)
66 MESH_LOGE("Failed to set [mesh_gate_announcements] : 1");
68 MESH_LOGE("External interface is not available !");
69 return MESHD_ERROR_OPERATION_FAILED;
72 return MESHD_ERROR_NONE;
75 int mesh_request_unset_mesh_gate(const char* bridge_interface,
76 const char* mesh_interface, const char* external_interface)
78 int ret = MESHD_ERROR_NONE;
80 NOTUSED(external_interface);
82 /* Set mesh parameters */
83 ret = mesh_netlink_set_mesh_parameter(mesh_interface,
84 "mesh_hwmp_rootmode", 0);
85 if (MESHD_ERROR_NONE != ret)
86 MESH_LOGE("Failed to set [mesh_hwmp_rootmode] : 0");
88 ret = mesh_netlink_set_mesh_parameter(mesh_interface,
89 "mesh_gate_announcements", 0);
90 if (MESHD_ERROR_NONE != ret)
91 MESH_LOGE("Failed to set [mesh_gate_announcements] : 0");
94 ret = mesh_bridge_set_stp(bridge_interface, FALSE);
95 if (MESHD_ERROR_NONE != ret) {
96 MESH_LOGE("Failed to turn STP off!");
100 return MESHD_ERROR_NONE;
103 int mesh_request_add_bridge_interface(const char* bridge_interface,
104 const char* interface)
106 int ret = MESHD_ERROR_NONE;
108 if (NULL == bridge_interface || NULL == interface) {
109 MESH_LOGE("Invalid parameters");
110 return MESHD_ERROR_INVALID_PARAMETER;
113 MESH_LOGD("Adding interface[%s] into bridge[%s]...", interface, bridge_interface);
115 /* Add external interface into bridge */
116 ret = mesh_bridge_add_interface(bridge_interface, interface);
117 if (MESHD_ERROR_NONE != ret) {
118 MESH_LOGE("Failed to add interface !");
122 /* Make external interface down */
123 ret = mesh_interface_set(interface, NULL, MESH_INTERFACE_DOWN);
124 if (MESHD_ERROR_NONE != ret) {
125 MESH_LOGE("Failed to change external interface state");
129 /* Make external interface up with cleared IP */
130 ret = mesh_interface_set(interface, "0.0.0.0", MESH_INTERFACE_UP);
131 if (MESHD_ERROR_NONE != ret) {
132 MESH_LOGE("Failed to change external interface state");
136 return MESHD_ERROR_NONE;
139 int mesh_request_remove_bridge_interface(const char* bridge_interface,
140 const char* interface)
142 int ret = MESHD_ERROR_NONE;
144 if (NULL == bridge_interface || NULL == interface) {
145 MESH_LOGE("Invalid parameters");
146 return MESHD_ERROR_INVALID_PARAMETER;
149 MESH_LOGD("Removing interface[%s] from bridge[%s]...", interface, bridge_interface);
151 /* Remove external interface into bridge */
152 ret = mesh_bridge_del_interface(bridge_interface, interface);
153 if (MESHD_ERROR_NONE != ret) {
154 MESH_LOGE("Failed to remove interface !");
158 return MESHD_ERROR_NONE;
161 int mesh_request_set_softap_config(const char* softap_interface,
162 const char *ssid, const char* mode, int channel, int visibility,
163 int max_sta, int security, const char* passphrase)
165 int ret = MESHD_ERROR_NONE;
167 MESH_LOGD("Setting configuration for SoftAP");
169 ret = mesh_softap_set_configuration(softap_interface, ssid, mode, channel,
170 visibility, max_sta, security, passphrase);
171 if (MESHD_ERROR_NONE != ret) {
172 MESH_LOGE("Failed to set Configuration for SoftAP");
179 int mesh_request_enable_softap(
180 const char* bridge_interface, const char* softap_interface)
182 int ret = MESHD_ERROR_NONE;
184 MESH_LOGD("Request to turn SoftAP on");
185 ret = mesh_softap_enable_softap(softap_interface);
186 if (MESHD_ERROR_NONE != ret) {
187 MESH_LOGE("Failed to mesh_softap_enable_softap");
191 /* Add softAP interface into bridge */
192 ret = mesh_request_add_bridge_interface(bridge_interface, softap_interface);
193 if (MESHD_ERROR_NONE != ret)
199 int mesh_request_disable_softap(
200 const char* bridge_interface, const char* softap_interface)
202 int ret = MESHD_ERROR_NONE;
204 MESH_LOGD("Request to turn SoftAP off");
205 ret = mesh_softap_disable_softap();
207 /* Remove softAP interface from bridge */
208 ret = mesh_request_remove_bridge_interface(bridge_interface, softap_interface);
209 if (MESHD_ERROR_NONE != ret)
215 int mesh_request_get_station_info(const char* mesh_interface, GList **station_list)
217 int ret = MESHD_ERROR_NONE;
219 MESH_LOGD("Request to get station info");
221 /* Get station info */
222 ret = mesh_netlink_get_station_info(mesh_interface, station_list);
223 if (MESHD_ERROR_NONE != ret)
226 return MESHD_ERROR_NONE;
229 int mesh_request_get_mpath_info(const char* mesh_interface, GList **mpath_list)
231 int ret = MESHD_ERROR_NONE;
233 MESH_LOGD("Request to get mpath info");
236 ret = mesh_netlink_get_mpath_info(mesh_interface, mpath_list);
237 if (MESHD_ERROR_NONE != ret)
240 return MESHD_ERROR_NONE;
243 int mesh_request_register_event_handler()
245 int ret = MESHD_ERROR_NONE;
247 MESH_LOGD("Request to register mesh event handler");
250 ret = mesh_netlink_register_event_handler();
251 if (MESHD_ERROR_NONE != ret)
254 return MESHD_ERROR_NONE;
257 int mesh_request_unregister_event_handler()
259 int ret = MESHD_ERROR_NONE;
261 MESH_LOGD("Request to unregister mesh event handler");
264 ret = mesh_netlink_unregister_event_handler();
265 if (MESHD_ERROR_NONE != ret)
268 return MESHD_ERROR_NONE;
271 int mesh_request_enable_network(mesh_service *service)
274 if (NULL == service) {
275 MESH_LOGE("Invalid parameter");
276 return MESHD_ERROR_INVALID_PARAMETER;
279 MESH_LOGD("[IPC] Enable mesh network");
281 ret = mesh_gdbus_create_mesh_interface(service);
282 if (MESHD_ERROR_NONE != ret) {
283 MESH_LOGE("Failed to create mesh network");
287 return MESHD_ERROR_NONE;
290 int mesh_request_disable_network(mesh_service *service)
293 if (NULL == service) {
294 MESH_LOGE("Invalid parameter");
295 return MESHD_ERROR_INVALID_PARAMETER;
298 MESH_LOGD("[IPC] Disable mesh network");
300 ret = mesh_gdbus_remove_mesh_interface(service);
301 if (MESHD_ERROR_NONE != ret) {
302 MESH_LOGE("Failed to create mesh network");
306 return MESHD_ERROR_NONE;
309 int mesh_request_scan(mesh_service *service)
312 if (NULL == service) {
313 MESH_LOGE("Invalid parameter");
314 return MESHD_ERROR_INVALID_PARAMETER;
317 MESH_LOGD("[IPC] Request scan for mesh network");
319 ret = mesh_gdbus_mesh_scan(service);
320 if (MESHD_ERROR_NONE != ret) {
321 MESH_LOGE("Failed to request scan for mesh network");
325 return MESHD_ERROR_NONE;
328 int mesh_request_specific_scan(mesh_service *service, gchar *mesh_id, gint channel)
331 if (NULL == service) {
332 MESH_LOGE("Invalid parameter");
333 return MESHD_ERROR_INVALID_PARAMETER;
336 MESH_LOGD("[IPC] Request specific scan for mesh network");
338 ret = mesh_gdbus_mesh_specific_scan(service, mesh_id, channel);
339 if (MESHD_ERROR_NONE != ret) {
340 MESH_LOGE("Failed to request specific scan for mesh network");
344 return MESHD_ERROR_NONE;
347 int mesh_request_cancel_scan(mesh_service *service)
350 if (NULL == service) {
351 MESH_LOGE("Invalid parameter");
352 return MESHD_ERROR_INVALID_PARAMETER;
355 MESH_LOGD("[IPC] Cancel scan for mesh network");
357 ret = mesh_gdbus_mesh_cancel_scan(service);
358 if (MESHD_ERROR_NONE != ret) {
359 MESH_LOGE("Failed to cancel scan for mesh network");
363 return MESHD_ERROR_NONE;
366 int mesh_request_get_networks(mesh_service *service)
369 if (NULL == service) {
370 MESH_LOGE("Invalid parameter");
371 return MESHD_ERROR_INVALID_PARAMETER;
374 MESH_LOGD("[IPC] Get mesh networks");
376 ret = mesh_gdbus_get_mesh_networks(service);
377 if (MESHD_ERROR_NONE != ret) {
378 MESH_LOGE("Failed to get mesh networks !");
382 return MESHD_ERROR_NONE;
385 static int _select_matched_network(GList *scanned_network,
386 const char *mesh_id, int mesh_channel, meshd_security_type_e sec,
387 mesh_scan_result_s **info)
389 int ret = MESHD_ERROR_NONE;
391 mesh_scan_result_s *item = NULL;
392 gboolean found = FALSE;
394 meshd_check_null_ret_error("scanned_network", scanned_network,
395 MESHD_ERROR_INVALID_PARAMETER);
397 iter = scanned_network;
398 while (iter != NULL) {
399 item = (mesh_scan_result_s*)iter->data;
401 if (g_strcmp0(mesh_id, item->mesh_id) == 0) {
402 if (item->channel == mesh_channel && item->security == sec) {
408 iter = g_list_next(iter);
412 return MESHD_ERROR_NO_DATA;
417 int mesh_request_get_joined_network(mesh_service *service)
421 if (NULL == service) {
422 MESH_LOGE("Invalid parameter");
423 return MESHD_ERROR_INVALID_PARAMETER;
426 MESH_LOGD("[IPC] Get joined mesh network");
428 ret = mesh_gdbus_get_joined_mesh_network(service);
429 if (MESHD_ERROR_NONE != ret) {
430 MESH_LOGE("Failed to get joined mesh network");
434 return MESHD_ERROR_NONE;
437 int mesh_request_get_connected_peers(mesh_service *service)
441 if (NULL == service) {
442 MESH_LOGE("Invalid parameter");
443 return MESHD_ERROR_INVALID_PARAMETER;
446 MESH_LOGD("[IPC] Get connected mesh peers");
448 ret = mesh_gdbus_get_connected_peers(service);
449 if (MESHD_ERROR_NONE != ret) {
450 MESH_LOGE("Failed to get connected mesh peers");
454 return MESHD_ERROR_NONE;
457 int mesh_request_create_mesh_network(mesh_service *service, gchar *mesh_id,
458 gint channel, meshd_security_type_e sec)
462 if (NULL == service) {
463 MESH_LOGE("Invalid parameter");
464 return MESHD_ERROR_INVALID_PARAMETER;
467 MESH_LOGD("[IPC] Create a new mesh network");
469 ret = mesh_gdbus_create_network(service, mesh_id, channel, sec);
470 if (MESHD_ERROR_NONE != ret) {
471 MESH_LOGE("Failed to create mesh network");
475 return MESHD_ERROR_NONE;
478 int mesh_request_connect_mesh_network(mesh_service *service, gchar *mesh_id,
479 gint channel, meshd_security_type_e sec, gchar *passphrase)
482 mesh_scan_result_s *info = NULL;
484 if (NULL == service) {
485 MESH_LOGE("Invalid parameter");
486 return MESHD_ERROR_INVALID_PARAMETER;
489 MESH_LOGD("[IPC] Connect mesh network");
491 /* Get mesh_id and channel from saved network */
492 ret = _select_matched_network(service->scanned_mesh_network,
493 mesh_id, channel, sec, &info);
494 if (MESHD_ERROR_NONE != ret) {
495 MESH_LOGE("Failed to mesh_network_get_first_mesh_network");
500 if (MESHD_SECURITY_NONE != sec) {
501 if (NULL != passphrase) {
502 ret = mesh_gdbus_set_passphrase(service, info, passphrase);
503 if (MESHD_ERROR_NONE != ret) {
504 MESH_LOGE("Failed to set passphrase for mesh network");
508 MESH_LOGE("Passphrase is required !");
509 return MESHD_ERROR_INVALID_PARAMETER;
513 ret = mesh_gdbus_connect_network(service, info);
514 if (MESHD_ERROR_NONE != ret) {
515 MESH_LOGE("Failed to connect mesh network");
519 return MESHD_ERROR_NONE;
522 int mesh_request_disconnect_mesh_network(mesh_service *service,
523 gchar *mesh_id, gint channel, meshd_security_type_e sec)
526 mesh_scan_result_s *info = NULL;
528 if (NULL == service) {
529 MESH_LOGE("Invalid parameter");
530 return MESHD_ERROR_INVALID_PARAMETER;
533 MESH_LOGD("[IPC] Disconnect mesh network");
535 /* Get mesh_id and channel from saved network */
536 ret = _select_matched_network(service->scanned_mesh_network,
537 mesh_id, channel, sec, &info);
538 if (MESHD_ERROR_NONE != ret) {
539 MESH_LOGE("Failed to _select_matched_network");
543 ret = mesh_gdbus_disconnect_network(service, info);
544 if (MESHD_ERROR_NONE != ret) {
545 MESH_LOGE("Failed to disconnect mesh network");
549 return MESHD_ERROR_NONE;
552 int mesh_request_remove_mesh_network(mesh_service *service,
553 gchar *mesh_id, gint channel, meshd_security_type_e sec)
556 mesh_scan_result_s *info = NULL;
558 if (NULL == service) {
559 MESH_LOGE("Invalid parameter");
560 return MESHD_ERROR_INVALID_PARAMETER;
563 MESH_LOGD("[IPC] Remove mesh network");
565 /* Get mesh_id and channel from saved network */
566 ret = _select_matched_network(service->scanned_mesh_network,
567 mesh_id, channel, sec, &info);
568 if (MESHD_ERROR_NONE != ret) {
569 MESH_LOGE("Failed to _select_matched_network");
573 ret = mesh_gdbus_remove_network(service, info);
574 if (MESHD_ERROR_NONE != ret) {
575 MESH_LOGE("Failed to remove mesh network");
579 return MESHD_ERROR_NONE;
583 void mesh_notify_scan_done()
585 NetMesh *object = meshd_dbus_get_object();
587 net_mesh_emit_scan_done(object);
590 void mesh_notify_connection_state(const char* mesh_id, const char* bssid,
591 int channel, meshd_security_type_e sec, meshd_connection_state_e state)
593 NetMesh *object = meshd_dbus_get_object();
595 net_mesh_emit_connection_state(object, mesh_id, bssid, channel, (int)sec, (int)state);
598 void mesh_notify_station_joined(const char* bssid)
600 NetMesh *object = meshd_dbus_get_object();
602 net_mesh_emit_sta_joined(object, bssid);
605 void mesh_notify_station_left(const char* bssid)
607 NetMesh *object = meshd_dbus_get_object();
609 net_mesh_emit_sta_left(object, bssid);