g_free 'field' and 'value' if you break out of a while loop.
[platform/core/connectivity/net-config.git] / src / network-dpm.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 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 <vconf.h>
21 #include <vconf-keys.h>
22
23 #include "log.h"
24 #include "util.h"
25 #include "netdbus.h"
26 #include "wifi-power.h"
27 #include "network-dpm.h"
28 #include "network-state.h"
29
30 #define NETCONFIG_SIGNAL_DPM_WIFI                       "DPMWifi"
31 #define NETCONFIG_SIGNAL_DPM_WIFI_PROFILE       "DPMWifiProfile"
32
33 static int dpm_policy_wifi = 1;
34 static int dpm_policy_wifi_profile = 1;
35
36 static void __netconfig_dpm_notify_result(const char *sig_name, const char *key)
37 {
38         gboolean reply;
39         GVariant *params;
40         GVariantBuilder *builder = NULL;
41         GDBusConnection *connection = NULL;
42         GError *error = NULL;
43         const char *prop_key = "key";
44
45         INFO("[Signal] %s %s", sig_name, key);
46
47         connection = netdbus_get_connection();
48         if (connection == NULL) {
49                 ERR("Failed to get GDBus Connection");
50                 return;
51         }
52
53         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
54         g_variant_builder_add(builder, "{sv}", prop_key, g_variant_new_string(key));
55         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
56
57         g_variant_builder_unref(builder);
58
59         reply = g_dbus_connection_emit_signal(connection,
60                         NULL,
61                         NETCONFIG_NETWORK_PATH,
62                         NETCONFIG_NETWORK_INTERFACE,
63                         sig_name,
64                         params,
65                         &error);
66
67         if (reply != TRUE) {
68                 if (error != NULL) {
69                         ERR("Failed to send signal [%s]", error->message);
70                         g_error_free(error);
71                 }
72                 return;
73         }
74
75         INFO("Sent signal (%s), key (%s)", sig_name, key);
76         return;
77 }
78
79 void netconfig_dpm_init(void)
80 {
81         INFO("DPM initialized");
82         return;
83 }
84
85 void netconfig_dpm_deinit(void)
86 {
87         INFO("DPM deinitialized");
88         return;
89 }
90
91 int netconfig_dpm_update_from_wifi(void)
92 {
93         INFO("DPM update from wifi [%d]", dpm_policy_wifi);
94
95         if (!dpm_policy_wifi) {
96                 int wifi_state = 0;
97                 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
98                 if (wifi_state != VCONFKEY_WIFI_OFF) {
99                         int err = wifi_power_off();
100                         if (err < 0) {
101                                 if (err == -EINPROGRESS)
102                                         ERR("wifi power off : InProgress");
103                                 else if (err == -EALREADY)
104                                         ERR("wifi power off : AlreadyExists");
105                                 else if (err == -EPERM)
106                                         ERR("wifi power off : PermissionDenied");
107                                 else
108                                         ERR("wifi power off : WifiDriverFailed");
109                         } else
110                                 DBG("wifi power off : ErrorNone");
111
112                         netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_OFF_BY_AIRPLANE, 0);
113                         netconfig_send_restriction_to_net_popup("Wi-Fi unavailable",
114                                         "toast_popup", "wifi");
115                 }
116         }
117
118         return dpm_policy_wifi;
119 }
120
121 int netconfig_dpm_update_from_wifi_profile(void)
122 {
123         INFO("DPM update from wifi profile [%d]", dpm_policy_wifi_profile);
124         return dpm_policy_wifi_profile;
125 }
126
127 gboolean handle_device_policy_set_wifi(
128                 Network *object,
129                 GDBusMethodInvocation *context,
130                 gint state)
131 {
132         INFO("DPM device policy wifi changed : [%d -> %d]",
133                 dpm_policy_wifi, state);
134
135         dpm_policy_wifi = state;
136         netconfig_dpm_update_from_wifi();
137         __netconfig_dpm_notify_result(NETCONFIG_SIGNAL_DPM_WIFI,
138                 state ? "allowed" : "disallowed");
139
140         network_complete_device_policy_set_wifi(object, context);
141         return TRUE;
142 }
143
144 gboolean handle_device_policy_get_wifi(
145                 Network *object,
146                 GDBusMethodInvocation *context)
147 {
148         INFO("Successfully get wifi device policy [%d]",
149                 dpm_policy_wifi);
150
151         network_complete_device_policy_get_wifi(object,
152                 context, dpm_policy_wifi);
153         return TRUE;
154 }
155
156 gboolean handle_device_policy_set_wifi_profile(
157                 Network *object,
158                 GDBusMethodInvocation *context,
159                 gint state)
160 {
161         INFO("DPM device policy wifi profile changed : [%d -> %d]",
162                 dpm_policy_wifi_profile, state);
163
164         dpm_policy_wifi_profile = state;
165         netconfig_dpm_update_from_wifi_profile();
166         __netconfig_dpm_notify_result(NETCONFIG_SIGNAL_DPM_WIFI_PROFILE,
167                 state ? "allowed" : "disallowed");
168
169         network_complete_device_policy_set_wifi_profile(object, context);
170         return TRUE;
171 }
172
173 gboolean handle_device_policy_get_wifi_profile(
174                 Network *object,
175                 GDBusMethodInvocation *context)
176 {
177         INFO("Successfully get wifi profile device policy [%d]",
178                 dpm_policy_wifi_profile);
179
180         network_complete_device_policy_get_wifi(object,
181                 context, dpm_policy_wifi_profile);
182         return TRUE;
183 }