Rectify gvariant type for frequency
[platform/core/connectivity/net-config.git] / src / clatd-handler.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <unistd.h>
21 #include <stdlib.h>
22
23 #include "log.h"
24 #include "util.h"
25 #include "network-state.h"
26 #include "netdbus.h"
27 #include "clatd-handler.h"
28
29 static gboolean g_is_running = FALSE;
30
31 static void __netconfig_clatd_async_callback(GObject *source_object,
32                 GAsyncResult *res, gpointer user_data)
33 {
34         GDBusConnection *conn = NULL;
35         GVariant *reply = NULL;
36         GError *error = NULL;
37         DBG("__netconfig_clatd_async_callback");
38
39         conn = G_DBUS_CONNECTION(source_object);
40         reply = g_dbus_connection_call_finish(conn, res, &error);
41         if (error) {
42                 ERR("Dbus error : %s", error->message);
43                 g_error_free(error);
44         } else {
45                 int is_clat_enabled = -1;
46                 g_variant_get(reply, "(i)", &is_clat_enabled);
47
48                 if (is_clat_enabled == 0) {
49                         DBG("OK");
50                 } else if (is_clat_enabled == 1) {
51                         INFO("already on going");
52                         g_is_running = TRUE;
53                 } else {
54                         ERR("Failed to clat enable, %d", is_clat_enabled);
55                 }
56         }
57
58         if (reply)
59                 g_variant_unref(reply);
60 }
61
62 int netconfig_clatd_enable(void)
63 {
64         DBG("");
65         GVariant *params = NULL;
66         GVariantBuilder *builder = NULL;
67         gboolean rv = FALSE;
68         struct clatd_ctrl_hint hint = {0 ,};
69         char ns_buff[256] = {0, };
70
71         if (g_is_running) {
72                 INFO("clatd is already ENABLED");
73                 return 0;
74         }
75
76         if (!netconfig_get_connected_cellular_internet_ipv6only_profile(&hint)) {
77                 ERR("Failed to get cellular profile");
78                 return -1;
79         }
80
81         const char *if_name = netconfig_get_default_ifname();
82         if (if_name == NULL) {
83                 int idx;
84                 for (idx = 0; idx < MAX_DNS; idx++)
85                         g_free(hint.nameserver[idx]);
86
87                 ERR("There is no interface name");
88                 return -1;
89         } else {
90                 DBG("if_name : %s", if_name);
91         }
92
93         builder = g_variant_builder_new(G_VARIANT_TYPE ("a{is}"));
94
95         g_variant_builder_add(builder, "{is}", 'i', if_name);
96
97         // nameserver addresses
98         if (hint.nameserver[0] != NULL || hint.nameserver[1] != NULL) {
99                 g_snprintf(ns_buff, 256, "%s;%s",
100                                         ((hint.nameserver[0] != NULL) ? hint.nameserver[0] : "NULL"),
101                                         ((hint.nameserver[1] != NULL) ? hint.nameserver[1] : "NULL"));
102         }
103         g_variant_builder_add(builder, "{is}", 'd', ns_buff);
104
105         params = g_variant_builder_end(builder);
106         g_variant_builder_unref(builder);
107
108         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
109                         CLATD_INTERFACE, "Start", g_variant_new("(@a{is})", params),
110                         __netconfig_clatd_async_callback, NULL);
111
112         if (!rv) {
113                 DBG("Failed to dbus call");
114                 g_variant_unref(params);
115
116                 int idx;
117                 for (idx = 0; idx < MAX_DNS; idx++)
118                         g_free(hint.nameserver[idx]);
119                 return -1;
120         }
121
122         g_is_running = TRUE;
123         DBG("clatd enabled[%d]", g_is_running);
124         int idx;
125         for (idx = 0; idx < MAX_DNS; idx++)
126                 g_free(hint.nameserver[idx]);
127         return 0;
128 }
129
130 int netconfig_clatd_disable(void)
131 {
132         DBG("");
133         gboolean rv = FALSE;
134
135         if (!g_is_running) {
136                 ERR("clatd is already DISABLED");
137                 return 0;
138         }
139
140         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
141                         CLATD_INTERFACE, "Stop", NULL, NULL, NULL);
142         g_is_running = FALSE;
143         DBG("clatd disabled[%d]", g_is_running);
144         if (!rv) {
145                 DBG("Failed to dbus call");
146                 return -1;
147         }
148
149         return 0;
150 }
151
152 void netconfig_clatd_reset()
153 {
154         DBG("");
155         g_is_running = FALSE;
156         netconfig_clatd_enable();
157 }