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