Add battery monitor framework
[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), __netconfig_clatd_async_callback);
110
111         if (!rv) {
112                 DBG("Failed to dbus call");
113                 g_variant_unref(params);
114
115                 int idx;
116                 for (idx = 0; idx < MAX_DNS; idx++)
117                         g_free(hint.nameserver[idx]);
118                 return -1;
119         }
120
121         g_is_running = TRUE;
122         DBG("clatd enabled[%d]", g_is_running);
123         int idx;
124         for (idx = 0; idx < MAX_DNS; idx++)
125                 g_free(hint.nameserver[idx]);
126         return 0;
127 }
128
129 int netconfig_clatd_disable(void)
130 {
131         DBG("");
132         gboolean rv = FALSE;
133
134         if (!g_is_running) {
135                 ERR("clatd is already DISABLED");
136                 return 0;
137         }
138
139         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
140                         CLATD_INTERFACE, "Stop", NULL, NULL);
141         g_is_running = FALSE;
142         DBG("clatd disabled[%d]", g_is_running);
143         if (!rv) {
144                 DBG("Failed to dbus call");
145                 return -1;
146         }
147
148         return 0;
149 }
150
151 void netconfig_clatd_reset()
152 {
153         DBG("");
154         g_is_running = FALSE;
155         netconfig_clatd_enable();
156 }