[capi-tethering] Add APIs for dhcp feature
[platform/core/api/tethering.git] / src / tethering.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <net/if.h>
24 #include <arpa/inet.h>
25 #include <unistd.h>
26 #include <dbus/dbus.h>
27 #include <gio/gio.h>
28 #include <vconf.h>
29 #include <openssl/evp.h>
30 #include <openssl/sha.h>
31 #include <ckmc/ckmc-manager.h>
32 #include "tethering_private.h"
33
34 #define ALLOWED_LIST    "/etc/hostapd.accept"
35 #define BLOCKED_LIST    "/etc/hostapd.deny"
36 #define TEMP_LIST       "/etc/hostapd_tmp"
37 #define MAC_ADDR_LEN    18
38 #define MAX_BUF_SIZE    80
39
40 static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *sender_name,
41                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
42                         GVariant *parameters, gpointer user_data);
43
44 static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *sender_name,
45                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
46                 GVariant *parameters, gpointer user_data);
47
48 static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sender_name,
49                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
50                 GVariant *parameters, gpointer user_data);
51
52 static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *sender_name,
53                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
54                 GVariant *parameters, gpointer user_data);
55
56 static void __handle_bt_tether_on(GDBusConnection *connection, const gchar *sender_name,
57                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
58                 GVariant *parameters, gpointer user_data);
59
60 static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sender_name,
61                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
62                 GVariant *parameters, gpointer user_data);
63
64 static void __handle_wifi_ap_on(GDBusConnection *connection, const gchar *sender_name,
65                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
66                 GVariant *parameters, gpointer user_data);
67
68 static void __handle_wifi_ap_off(GDBusConnection *connection, const gchar *sender_name,
69                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
70                 GVariant *parameters, gpointer user_data);
71
72 static void __handle_net_closed(GDBusConnection *connection, const gchar *sender_name,
73                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
74                 GVariant *parameters, gpointer user_data);
75
76 static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *sender_name,
77                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
78                 GVariant *parameters, gpointer user_data);
79
80 static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *sender_name,
81                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
82                 GVariant *parameters, gpointer user_data);
83
84 static void __handle_flight_mode(GDBusConnection *connection, const gchar *sender_name,
85                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
86                 GVariant *parameters, gpointer user_data);
87
88 static void __handle_security_type_changed(GDBusConnection *connection, const gchar *sender_name,
89                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
90                 GVariant *parameters, gpointer user_data);
91
92 static void __handle_ssid_visibility_changed(GDBusConnection *connection, const gchar *sender_name,
93                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
94                 GVariant *parameters, gpointer user_data);
95
96 static void __handle_passphrase_changed(GDBusConnection *connection, const gchar *sender_name,
97                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
98                 GVariant *parameters, gpointer user_data);
99
100 static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name,
101                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
102                 GVariant *parameters, gpointer user_data);
103
104 static __tethering_sig_t sigs[] = {
105         {0, SIGNAL_NAME_NET_CLOSED, __handle_net_closed},
106         {0, SIGNAL_NAME_WIFI_TETHER_ON, __handle_wifi_tether_on},
107         {0, SIGNAL_NAME_WIFI_TETHER_OFF, __handle_wifi_tether_off},
108         {0, SIGNAL_NAME_USB_TETHER_ON, __handle_usb_tether_on},
109         {0, SIGNAL_NAME_USB_TETHER_OFF, __handle_usb_tether_off},
110         {0, SIGNAL_NAME_BT_TETHER_ON, __handle_bt_tether_on},
111         {0, SIGNAL_NAME_BT_TETHER_OFF, __handle_bt_tether_off},
112         {0, SIGNAL_NAME_WIFI_AP_ON, __handle_wifi_ap_on},
113         {0, SIGNAL_NAME_WIFI_AP_OFF, __handle_wifi_ap_off},
114         {0, SIGNAL_NAME_NO_DATA_TIMEOUT, __handle_no_data_timeout},
115         {0, SIGNAL_NAME_LOW_BATTERY_MODE, __handle_low_battery_mode},
116         {0, SIGNAL_NAME_FLIGHT_MODE, __handle_flight_mode},
117         {0, SIGNAL_NAME_SECURITY_TYPE_CHANGED, __handle_security_type_changed},
118         {0, SIGNAL_NAME_SSID_VISIBILITY_CHANGED, __handle_ssid_visibility_changed},
119         {0, SIGNAL_NAME_PASSPHRASE_CHANGED, __handle_passphrase_changed},
120         {0, SIGNAL_NAME_DHCP_STATUS, __handle_dhcp},
121         {0, "", NULL} };
122
123 static int retry = 0;
124
125 static void __send_dbus_signal(GDBusConnection *conn, const char *signal_name, const char *arg)
126 {
127         if (conn == NULL || signal_name == NULL)
128                 return;
129
130         GVariant *message = NULL;
131         GError *error = NULL;
132
133         if (arg)
134                 message = g_variant_new("(s)", arg);
135
136         g_dbus_connection_emit_signal(conn, NULL, TETHERING_SERVICE_OBJECT_PATH,
137                                         TETHERING_SERVICE_INTERFACE, signal_name, message, &error);
138         if (error) {
139                 ERR("g_dbus_connection_emit_signal is failed because  %s\n", error->message);
140                 g_error_free(error);
141         }
142         g_variant_unref(message);
143 }
144
145 static bool __any_tethering_is_enabled(tethering_h tethering)
146 {
147         if (tethering_is_enabled(tethering, TETHERING_TYPE_USB) ||
148                         tethering_is_enabled(tethering, TETHERING_TYPE_WIFI) ||
149                         tethering_is_enabled(tethering, TETHERING_TYPE_BT) ||
150                         tethering_is_enabled(tethering, TETHERING_TYPE_RESERVED))
151                 return true;
152
153         return false;
154 }
155
156 static tethering_error_e __set_security_type(const tethering_wifi_security_type_e security_type)
157 {
158         if (security_type != TETHERING_WIFI_SECURITY_TYPE_NONE &&
159                         security_type != TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK) {
160                 ERR("Invalid param\n");
161                 return TETHERING_ERROR_INVALID_PARAMETER;
162         }
163
164         if (vconf_set_int(VCONFKEY_MOBILE_HOTSPOT_SECURITY, security_type) < 0) {
165                 ERR("vconf_set_int is failed\n");
166                 return TETHERING_ERROR_OPERATION_FAILED;
167         }
168
169         return TETHERING_ERROR_NONE;
170 }
171
172 static tethering_error_e __get_security_type(tethering_wifi_security_type_e *security_type)
173 {
174         if (security_type == NULL) {
175                 ERR("Invalid param\n");
176                 return TETHERING_ERROR_INVALID_PARAMETER;
177         }
178
179         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_SECURITY,
180                                 (int *)security_type) < 0) {
181                 ERR("vconf_get_int is failed\n");
182                 return TETHERING_ERROR_OPERATION_FAILED;
183         }
184
185         return TETHERING_ERROR_NONE;
186 }
187
188 static bool __get_ssid_from_vconf(const char *path, char *ssid, unsigned int size)
189 {
190         if (path == NULL || ssid == NULL || size == 0)
191                 return false;
192
193         char *ptr = NULL;
194         char *ptr_tmp = NULL;
195
196         ptr = vconf_get_str(path);
197         if (ptr == NULL)
198                 return false;
199
200         if (!g_utf8_validate(ptr, -1, (const char **)&ptr_tmp))
201                 *ptr_tmp = '\0';
202
203         g_strlcpy(ssid, ptr, size);
204         free(ptr);
205
206         return true;
207 }
208
209 static tethering_error_e __set_visible(const bool visible)
210 {
211         if (vconf_set_int(VCONFKEY_MOBILE_HOTSPOT_HIDE, visible ? 0 : 1) < 0) {
212                 ERR("vconf_set_int is failed\n");
213                 return TETHERING_ERROR_OPERATION_FAILED;
214         }
215
216         return TETHERING_ERROR_NONE;
217 }
218
219 static tethering_error_e __get_visible(bool *visible)
220 {
221         if (visible == NULL) {
222                 ERR("Invalid param\n");
223                 return TETHERING_ERROR_INVALID_PARAMETER;
224         }
225
226         int hide = 0;
227
228         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_HIDE, &hide) < 0) {
229                 ERR("vconf_get_int is failed\n");
230                 return TETHERING_ERROR_OPERATION_FAILED;
231         }
232
233         if (hide)
234                 *visible = false;
235         else
236                 *visible = true;
237         return TETHERING_ERROR_NONE;
238 }
239
240 static unsigned int __generate_initial_passphrase(char *passphrase, unsigned int size)
241 {
242         if (passphrase == NULL ||
243                         size == 0 || size < TETHERING_WIFI_KEY_MIN_LEN + 1)
244                 return 0;
245
246         guint32 rand_int = 0;
247         int index = 0;
248
249         for (index = 0; index < TETHERING_WIFI_KEY_MIN_LEN; index++) {
250                 rand_int = g_random_int_range('a', 'z');
251                 passphrase[index] = rand_int;
252         }
253         passphrase[index] = '\0';
254
255         return index;
256 }
257
258 static tethering_error_e __get_error(int agent_error)
259 {
260         tethering_error_e err = TETHERING_ERROR_NONE;
261
262         switch (agent_error) {
263         case MOBILE_AP_ERROR_NONE:
264                 err = TETHERING_ERROR_NONE;
265                 break;
266
267         case MOBILE_AP_ERROR_RESOURCE:
268                 err = TETHERING_ERROR_OUT_OF_MEMORY;
269                 break;
270
271         case MOBILE_AP_ERROR_INTERNAL:
272                 err = TETHERING_ERROR_OPERATION_FAILED;
273                 break;
274
275         case MOBILE_AP_ERROR_INVALID_PARAM:
276                 err = TETHERING_ERROR_INVALID_PARAMETER;
277                 break;
278
279         case MOBILE_AP_ERROR_ALREADY_ENABLED:
280                 err = TETHERING_ERROR_OPERATION_FAILED;
281                 break;
282
283         case MOBILE_AP_ERROR_NOT_ENABLED:
284                 err = TETHERING_ERROR_NOT_ENABLED;
285                 break;
286
287         case MOBILE_AP_ERROR_NET_OPEN:
288                 err = TETHERING_ERROR_OPERATION_FAILED;
289                 break;
290
291         case MOBILE_AP_ERROR_NET_CLOSE:
292                 err = TETHERING_ERROR_OPERATION_FAILED;
293                 break;
294
295         case MOBILE_AP_ERROR_DHCP:
296                 err = TETHERING_ERROR_OPERATION_FAILED;
297                 break;
298
299         case MOBILE_AP_ERROR_IN_PROGRESS:
300                 err = TETHERING_ERROR_OPERATION_FAILED;
301                 break;
302
303         case MOBILE_AP_ERROR_NOT_PERMITTED:
304                 err = TETHERING_ERROR_NOT_PERMITTED;
305                 break;
306
307         case MOBILE_AP_ERROR_PERMISSION_DENIED:
308                 err = TETHERING_ERROR_PERMISSION_DENIED;
309                 break;
310
311         default:
312                 ERR("Not defined error : %d\n", agent_error);
313                 err = TETHERING_ERROR_OPERATION_FAILED;
314                 break;
315         }
316
317         return err;
318 }
319
320 static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name,
321                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
322                 GVariant *parameters, gpointer user_data)
323 {
324         DBG("+\n");
325
326         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
327
328         __tethering_h *th = (__tethering_h *)user_data;
329         bool opened = false;
330         tethering_type_e type = 0;
331         mobile_ap_type_e ap_type = 0;
332         tethering_connection_state_changed_cb ccb = NULL;
333         __tethering_client_h client;
334         void *data = NULL;
335         char *buf = NULL;
336         char *name = NULL;
337         char *mac = NULL;
338         char *ip = NULL;
339         guint timestamp;
340
341         memset(&client, 0, sizeof(__tethering_client_h));
342         g_variant_get(parameters, "(susssu)", &buf, &ap_type, &ip, &mac, &name, &timestamp);
343
344         if (!g_strcmp0(buf, "DhcpConnected")) {
345                 opened = true;
346         } else if (!g_strcmp0(buf, "DhcpLeaseDeleted")) {
347                 opened = false;
348         } else {
349                 ERR("Unknown event [%s]\n", buf);
350                 goto DONE;
351         }
352
353         if (ap_type == MOBILE_AP_TYPE_USB)
354                 type = TETHERING_TYPE_USB;
355         else if (ap_type == MOBILE_AP_TYPE_WIFI)
356                 type = TETHERING_TYPE_WIFI;
357         else if (ap_type == MOBILE_AP_TYPE_BT)
358                 type = TETHERING_TYPE_BT;
359         else if (ap_type == MOBILE_AP_TYPE_WIFI_AP) {
360                 type = TETHERING_TYPE_RESERVED;
361         } else {
362                 ERR("Not supported tethering type [%d]\n", ap_type);
363                 goto DONE;
364         }
365
366         ccb = th->changed_cb[type];
367         if (ccb == NULL)
368                 goto DONE;
369         data = th->changed_user_data[type];
370
371         client.interface = type;
372         g_strlcpy(client.ip, ip, sizeof(client.ip));
373         g_strlcpy(client.mac, mac, sizeof(client.mac));
374         if (name != NULL)
375                 client.hostname = g_strdup(name);
376         client.tm = (time_t)timestamp;
377
378         ccb((tethering_client_h)&client, opened, data);
379         g_free(client.hostname);
380 DONE:
381         g_free(buf);
382         g_free(ip);
383         g_free(mac);
384         g_free(name);
385         DBG("-\n");
386 }
387
388 static void __handle_net_closed(GDBusConnection *connection, const gchar *sender_name,
389                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
390                 GVariant *parameters, gpointer user_data)
391 {
392         DBG("+\n");
393
394         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
395
396         __tethering_h *th = (__tethering_h *)user_data;
397         tethering_type_e type = 0;
398         tethering_disabled_cb dcb = NULL;
399         void *data = NULL;
400         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_NETWORK_CLOSE;
401
402         for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
403                 dcb = th->disabled_cb[type];
404                 if (dcb == NULL)
405                         continue;
406                 data = th->disabled_user_data[type];
407
408                 dcb(TETHERING_ERROR_NONE, type, code, data);
409         }
410
411         DBG("-\n");
412 }
413
414 static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *sender_name,
415                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
416                         GVariant *parameters, gpointer user_data)
417 {
418         DBG("+\n");
419
420         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
421
422         __tethering_h *th = (__tethering_h *)user_data;
423         tethering_type_e type = TETHERING_TYPE_WIFI;
424         bool is_requested = false;
425         tethering_enabled_cb ecb = NULL;
426         void *data = NULL;
427
428         ecb = th->enabled_cb[type];
429         if (ecb == NULL)
430                 return;
431         data = th->enabled_user_data[type];
432
433         ecb(TETHERING_ERROR_NONE, type, is_requested, data);
434         DBG("-\n");
435 }
436
437 static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *sender_name,
438                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
439                         GVariant *parameters, gpointer user_data)
440 {
441         DBG("+\n");
442
443         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
444
445         __tethering_h *th = (__tethering_h *)user_data;
446         tethering_type_e type = TETHERING_TYPE_WIFI;
447         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
448         tethering_disabled_cb dcb = NULL;
449         void *data = NULL;
450         char *buf = NULL;
451
452         dcb = th->disabled_cb[type];
453         if (dcb == NULL)
454                 return;
455         data = th->disabled_user_data[type];
456         g_variant_get(parameters, "(s)", &buf);
457         if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
458                 code = TETHERING_DISABLED_BY_WIFI_ON;
459         else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT))
460                 code = TETHERING_DISABLED_BY_TIMEOUT;
461
462         g_free(buf);
463         dcb(TETHERING_ERROR_NONE, type, code, data);
464
465         DBG("-\n");
466 }
467
468 static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sender_name,
469                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
470                         GVariant *parameters, gpointer user_data)
471 {
472         DBG("+\n");
473
474         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
475
476         __tethering_h *th = (__tethering_h *)user_data;
477         tethering_type_e type = TETHERING_TYPE_USB;
478         bool is_requested = false;
479         tethering_enabled_cb ecb = NULL;
480         void *data = NULL;
481
482         ecb = th->enabled_cb[type];
483         if (ecb == NULL)
484                 return;
485         data = th->enabled_user_data[type];
486
487         ecb(TETHERING_ERROR_NONE, type, is_requested, data);
488         DBG("-\n");
489 }
490
491 static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *sender_name,
492                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
493                         GVariant *parameters, gpointer user_data)
494 {
495         DBG("+\n");
496
497         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
498
499         __tethering_h *th = (__tethering_h *)user_data;
500         tethering_type_e type = TETHERING_TYPE_USB;
501         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
502         tethering_disabled_cb dcb = NULL;
503         void *data = NULL;
504         char *buf = NULL;
505
506         dcb = th->disabled_cb[type];
507         if (dcb == NULL)
508                 return;
509         data = th->disabled_user_data[type];
510
511         g_variant_get(parameters, "(s)", &buf);
512         if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
513                 code = TETHERING_DISABLED_BY_USB_DISCONNECTION;
514
515         dcb(TETHERING_ERROR_NONE, type, code, data);
516         g_free(buf);
517         DBG("-\n");
518 }
519
520 static void __handle_bt_tether_on(GDBusConnection *connection, const gchar *sender_name,
521                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
522                         GVariant *parameters, gpointer user_data)
523 {
524         DBG("+\n");
525
526         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
527
528         __tethering_h *th = (__tethering_h *)user_data;
529         tethering_type_e type = TETHERING_TYPE_BT;
530         bool is_requested = false;
531         tethering_enabled_cb ecb = NULL;
532         void *data = NULL;
533
534         ecb = th->enabled_cb[type];
535         if (ecb == NULL)
536                 return;
537         data = th->enabled_user_data[type];
538
539         ecb(TETHERING_ERROR_NONE, type, is_requested, data);
540         DBG("-\n");
541 }
542
543 static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sender_name,
544                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
545                         GVariant *parameters, gpointer user_data)
546 {
547         DBG("+\n");
548
549         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
550
551         __tethering_h *th = (__tethering_h *)user_data;
552         tethering_type_e type = TETHERING_TYPE_BT;
553         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
554         tethering_disabled_cb dcb = NULL;
555         void *data = NULL;
556         char *buf = NULL;
557
558         dcb = th->disabled_cb[type];
559         if (dcb == NULL)
560                 return;
561         data = th->disabled_user_data[type];
562         g_variant_get(parameters, "(s)", &buf);
563         if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
564                 code = TETHERING_DISABLED_BY_BT_OFF;
565         else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT))
566                 code = TETHERING_DISABLED_BY_TIMEOUT;
567
568         dcb(TETHERING_ERROR_NONE, type, code, data);
569
570         g_free(buf);
571         DBG("-\n");
572 }
573
574 static void __handle_wifi_ap_on(GDBusConnection *connection, const gchar *sender_name,
575                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
576                         GVariant *parameters, gpointer user_data)
577 {
578         DBG("+\n");
579
580         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
581
582         __tethering_h *th = (__tethering_h *)user_data;
583         tethering_type_e type = TETHERING_TYPE_RESERVED;
584         bool is_requested = false;
585         tethering_enabled_cb ecb = NULL;
586         void *data = NULL;
587
588         ecb = th->enabled_cb[type];
589         if (ecb == NULL)
590                 return;
591         data = th->enabled_user_data[type];
592
593         ecb(TETHERING_ERROR_NONE, type, is_requested, data);
594         DBG("-\n");
595 }
596
597 static void __handle_wifi_ap_off(GDBusConnection *connection, const gchar *sender_name,
598                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
599                         GVariant *parameters, gpointer user_data)
600 {
601         DBG("+\n");
602
603         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
604
605         __tethering_h *th = (__tethering_h *)user_data;
606         tethering_type_e type = TETHERING_TYPE_RESERVED;
607         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
608         tethering_disabled_cb dcb = NULL;
609         void *data = NULL;
610         char *buf = NULL;
611
612         dcb = th->disabled_cb[type];
613         if (dcb == NULL)
614                 return;
615         data = th->disabled_user_data[type];
616         g_variant_get(parameters, "(s)", &buf);
617         if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
618                 code = TETHERING_DISABLED_BY_WIFI_ON;
619         else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT))
620                 code = TETHERING_DISABLED_BY_TIMEOUT;
621         g_free(buf);
622
623         dcb(TETHERING_ERROR_NONE, type, code, data);
624
625         DBG("-\n");
626 }
627
628 static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *sender_name,
629                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
630                         GVariant *parameters, gpointer user_data)
631 {
632         DBG("+\n");
633
634         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
635
636         __tethering_h *th = (__tethering_h *)user_data;
637         tethering_type_e type = 0;
638         tethering_disabled_cb dcb = NULL;
639         void *data = NULL;
640         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_TIMEOUT;
641
642         for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
643                 dcb = th->disabled_cb[type];
644                 if (dcb == NULL)
645                         continue;
646                 data = th->disabled_user_data[type];
647
648                 dcb(TETHERING_ERROR_NONE, type, code, data);
649         }
650         DBG("-\n");
651 }
652
653 static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *sender_name,
654                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
655                         GVariant *parameters, gpointer user_data)
656 {
657         DBG("+\n");
658
659         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
660
661         __tethering_h *th = (__tethering_h *)user_data;
662         tethering_type_e type = 0;
663         tethering_disabled_cb dcb = NULL;
664         void *data = NULL;
665         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_LOW_BATTERY;
666
667         for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_RESERVED; type++) {
668                 dcb = th->disabled_cb[type];
669                 if (dcb == NULL)
670                         continue;
671                 data = th->disabled_user_data[type];
672
673                 dcb(TETHERING_ERROR_NONE, type, code, data);
674         }
675         DBG("-\n");
676 }
677
678 static void __handle_flight_mode(GDBusConnection *connection, const gchar *sender_name,
679                         const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
680                         GVariant *parameters, gpointer user_data)
681 {
682         DBG("+\n");
683
684         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
685
686         __tethering_h *th = (__tethering_h *)user_data;
687         tethering_type_e type = 0;
688         tethering_disabled_cb dcb = NULL;
689         void *data = NULL;
690         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_FLIGHT_MODE;
691
692         for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_RESERVED; type++) {
693                 dcb = th->disabled_cb[type];
694                 if (dcb == NULL)
695                         continue;
696                 data = th->disabled_user_data[type];
697
698                 dcb(TETHERING_ERROR_NONE, type, code, data);
699         }
700         DBG("-\n");
701 }
702
703 static void __handle_security_type_changed(GDBusConnection *connection, const gchar *sender_name,
704                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
705                 GVariant *parameters, gpointer user_data)
706
707 {
708         DBG("+\n");
709
710         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
711         __tethering_h *th = (__tethering_h *)user_data;
712
713         tethering_wifi_security_type_changed_cb scb = NULL;
714         void *data = NULL;
715         tethering_wifi_security_type_e security_type;
716         char *buf = NULL;
717
718         scb = th->security_type_changed_cb;
719         if (scb == NULL)
720                 return;
721
722         g_variant_get(parameters, "(s)", &buf);
723         data = th->security_type_user_data;
724         if (g_strcmp0(buf, TETHERING_WIFI_SECURITY_TYPE_OPEN_STR) == 0)
725                 security_type = TETHERING_WIFI_SECURITY_TYPE_NONE;
726         else if (g_strcmp0(buf, TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK_STR) == 0)
727                 security_type = TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK;
728         else {
729                 SERR("Unknown type : %s\n", buf);
730                 g_free(buf);
731                 return;
732         }
733         g_free(buf);
734         scb(security_type, data);
735
736         return;
737 }
738
739 static void __handle_ssid_visibility_changed(GDBusConnection *connection, const gchar *sender_name,
740                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
741                 GVariant *parameters, gpointer user_data)
742 {
743         DBG("+\n");
744
745         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
746         __tethering_h *th = (__tethering_h *)user_data;
747
748         tethering_wifi_ssid_visibility_changed_cb scb = NULL;
749         void *data = NULL;
750         bool visible = false;
751         char *buf = NULL;
752
753         scb = th->ssid_visibility_changed_cb;
754         if (scb == NULL) {
755                 DBG("-\n");
756                 return;
757         }
758         g_variant_get(parameters, "(s)", &buf);
759         data = th->ssid_visibility_user_data;
760         if (g_strcmp0(buf, SIGNAL_MSG_SSID_VISIBLE) == 0)
761                 visible = true;
762
763         scb(visible, data);
764         g_free(buf);
765         DBG("-\n");
766 }
767
768 static void __handle_passphrase_changed(GDBusConnection *connection, const gchar *sender_name,
769                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
770                 GVariant *parameters, gpointer user_data)
771 {
772         DBG("+\n");
773
774         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
775         __tethering_h *th = (__tethering_h *)user_data;
776
777         tethering_wifi_passphrase_changed_cb pcb = NULL;
778         void *data = NULL;
779
780         pcb = th->passphrase_changed_cb;
781         if (pcb == NULL)
782                 return;
783
784         data = th->passphrase_user_data;
785
786         pcb(data);
787         DBG("-\n");
788 }
789
790 static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
791                 gpointer user_data)
792 {
793         DBG("+\n");
794
795         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
796         GError *g_error = NULL;
797         GVariant *g_var;
798         guint info;
799         tethering_error_e error;
800         __tethering_h *th = (__tethering_h *)user_data;
801         tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_WIFI];
802         void *data = th->enabled_user_data[TETHERING_TYPE_WIFI];
803
804         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
805         if (g_error) {
806                 ERR("DBus error [%s]\n", g_error->message);
807                 if (g_error->code == G_DBUS_ERROR_NO_REPLY &&
808                                 ++retry < TETHERING_ERROR_RECOVERY_MAX) {
809                         g_error_free(g_error);
810                         tethering_enable((tethering_h)th, TETHERING_TYPE_WIFI);
811                         return;
812                 } else if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
813                         error = TETHERING_ERROR_PERMISSION_DENIED;
814                 else
815                         error = TETHERING_ERROR_OPERATION_FAILED;
816                 g_error_free(g_error);
817         } else {
818                 g_variant_get(g_var, "(u)", &info);
819                 error = __get_error(info);
820         }
821         retry = 0;
822
823         sigs[E_SIGNAL_WIFI_TETHER_ON].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
824                         NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_WIFI_TETHER_ON].name,
825                         TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
826                         sigs[E_SIGNAL_WIFI_TETHER_ON].cb, (gpointer)th, NULL);
827
828         if (!ecb) {
829                 DBG("-\n");
830                 return;
831         }
832         ecb(error, TETHERING_TYPE_WIFI, true, data);
833         g_variant_unref(g_var);
834         DBG("-\n");
835 }
836
837 static void __bt_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
838                 gpointer user_data)
839 {
840         DBG("+\n");
841         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
842         GError *g_error = NULL;
843         GVariant *g_var;
844         guint info;
845         tethering_error_e error;
846
847         __tethering_h *th = (__tethering_h *)user_data;
848         tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_BT];
849         void *data = th->enabled_user_data[TETHERING_TYPE_BT];
850
851         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
852         if (g_error) {
853                 ERR("DBus error [%s]\n", g_error->message);
854                 if (g_error->code == G_DBUS_ERROR_NO_REPLY &&
855                                 ++retry < TETHERING_ERROR_RECOVERY_MAX) {
856                         g_error_free(g_error);
857                         tethering_enable((tethering_h)th, TETHERING_TYPE_BT);
858                         DBG("-\n");
859                         return;
860                 }
861                 if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
862                         error = TETHERING_ERROR_PERMISSION_DENIED;
863                 else
864                         error = TETHERING_ERROR_OPERATION_FAILED;
865                 g_error_free(g_error);
866         } else {
867                 g_variant_get(g_var, "(u)", &info);
868                 g_variant_unref(g_var);
869                 error = __get_error(info);
870         }
871         retry = 0;
872
873         sigs[E_SIGNAL_BT_TETHER_ON].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
874                         NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_BT_TETHER_ON].name,
875                         TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
876                         sigs[E_SIGNAL_BT_TETHER_ON].cb, (gpointer)th, NULL);
877
878         if (!ecb) {
879                 DBG("-\n");
880                 return;
881         }
882
883         ecb(error, TETHERING_TYPE_BT, true, data);
884         DBG("-\n");
885 }
886
887 static void __usb_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
888                                         gpointer user_data)
889 {
890         DBG("+\n");
891
892         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
893         __tethering_h *th = (__tethering_h *)user_data;
894         GError *g_error = NULL;
895         GVariant *g_var;
896         guint info;
897         tethering_error_e error;
898         tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_USB];
899         void *data = th->enabled_user_data[TETHERING_TYPE_USB];
900
901         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
902         if (g_error) {
903                 ERR("DBus error [%s]\n", g_error->message);
904                 if (g_error->code == G_DBUS_ERROR_NO_REPLY &&
905                                 ++retry < TETHERING_ERROR_RECOVERY_MAX) {
906                         g_error_free(g_error);
907                         tethering_enable((tethering_h)th, TETHERING_TYPE_USB);
908                         DBG("-\n");
909                         return;
910                 }
911                 if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
912                         error = TETHERING_ERROR_PERMISSION_DENIED;
913                 else
914                         error = TETHERING_ERROR_OPERATION_FAILED;
915                 g_error_free(g_error);
916         } else {
917                 g_variant_get(g_var, "(u)", &info);
918                 g_variant_unref(g_var);
919                 error = __get_error(info);
920         }
921         retry = 0;
922
923         sigs[E_SIGNAL_USB_TETHER_ON].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
924                         NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_USB_TETHER_ON].name,
925                         TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
926                         sigs[E_SIGNAL_USB_TETHER_ON].cb, (gpointer)th, NULL);
927
928         if (!ecb) {
929                 DBG("-\n");
930                 return;
931         }
932
933         ecb(error, TETHERING_TYPE_USB, true, data);
934         DBG("-\n");
935 }
936
937 static void __wifi_ap_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
938                 gpointer user_data)
939 {
940         DBG("+\n");
941
942         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
943         __tethering_h *th = (__tethering_h *)user_data;
944         GError *g_error = NULL;
945         GVariant *g_var;
946         guint info;
947         tethering_error_e error;
948         tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_RESERVED];
949         void *data = th->enabled_user_data[TETHERING_TYPE_RESERVED];
950
951         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
952         if (g_error) {
953                 ERR("DBus error [%s]\n", g_error->message);
954                 if (g_error->code == G_DBUS_ERROR_NO_REPLY &&
955                                 ++retry < TETHERING_ERROR_RECOVERY_MAX) {
956                         g_error_free(g_error);
957                         tethering_enable((tethering_h)th, TETHERING_TYPE_RESERVED);
958                         DBG("-\n");
959                         return;
960                 }
961                 if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
962                         error = TETHERING_ERROR_PERMISSION_DENIED;
963                 else
964                         error = TETHERING_ERROR_OPERATION_FAILED;
965                 g_error_free(g_error);
966         } else {
967                 g_variant_get(g_var, "(u)", &info);
968                 g_variant_unref(g_var);
969                 error = __get_error(info);
970         }
971         retry = 0;
972
973         sigs[E_SIGNAL_WIFI_AP_ON].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
974                         NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_WIFI_AP_ON].name,
975                         TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
976                         sigs[E_SIGNAL_WIFI_AP_ON].cb, (gpointer)th, NULL);
977
978         if (!ecb) {
979                 DBG("-\n");
980                 return;
981         }
982
983         ecb(error, TETHERING_TYPE_RESERVED, true, data);
984         DBG("-\n");
985 }
986
987 static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
988                 gpointer user_data)
989 {
990         DBG("+\n");
991
992         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
993         GError *g_error = NULL;
994         GVariant *g_var;
995         guint info, event_type;
996         tethering_error_e error;
997         tethering_type_e type;
998         tethering_h tethering = (tethering_h)user_data;
999         __tethering_h *th = (__tethering_h *)tethering;
1000         tethering_disabled_cause_e code = TETHERING_DISABLED_BY_REQUEST;
1001         tethering_disabled_cb dcb = NULL;
1002         void *data = NULL;
1003
1004         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
1005         if (g_error) {
1006                 ERR("DBus error [%s]\n", g_error->message);
1007                 g_error_free(g_error);
1008                 return;
1009         }
1010         g_variant_get(g_var, "(uu)", &event_type, &info);
1011         DBG("cfm event : %d info : %d\n", event_type, info);
1012         g_variant_unref(g_var);
1013         error = __get_error(info);
1014         DBG("cfm event : %d info : %d\n", event_type, error);
1015         switch (event_type) {
1016         case MOBILE_AP_DISABLE_WIFI_TETHERING_CFM:
1017                 sigs[E_SIGNAL_WIFI_TETHER_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1018                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_WIFI_TETHER_OFF].name,
1019                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1020                                 sigs[E_SIGNAL_WIFI_TETHER_OFF].cb, (gpointer)th, NULL);
1021
1022                 type = TETHERING_TYPE_WIFI;
1023                 dcb = th->disabled_cb[type];
1024                 data = th->disabled_user_data[type];
1025                 if (dcb)
1026                         dcb(error, type, code, data);
1027                 break;
1028
1029         case MOBILE_AP_DISABLE_BT_TETHERING_CFM:
1030                 sigs[E_SIGNAL_BT_TETHER_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1031                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_BT_TETHER_OFF].name,
1032                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1033                                 sigs[E_SIGNAL_BT_TETHER_OFF].cb, (gpointer)th, NULL);
1034
1035                 type = TETHERING_TYPE_BT;
1036                 dcb = th->disabled_cb[type];
1037                 data = th->disabled_user_data[type];
1038                 if (dcb)
1039                         dcb(error, type, code, data);
1040                 break;
1041
1042         case MOBILE_AP_DISABLE_USB_TETHERING_CFM:
1043                 sigs[E_SIGNAL_USB_TETHER_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1044                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_USB_TETHER_OFF].name,
1045                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1046                                 sigs[E_SIGNAL_USB_TETHER_OFF].cb, (gpointer)th, NULL);
1047
1048                 type = TETHERING_TYPE_USB;
1049                 dcb = th->disabled_cb[type];
1050                 data = th->disabled_user_data[type];
1051                 if (dcb)
1052                         dcb(error, type, code, data);
1053                 break;
1054
1055         case MOBILE_AP_DISABLE_WIFI_AP_CFM:
1056                 sigs[E_SIGNAL_WIFI_AP_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1057                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_WIFI_AP_OFF].name,
1058                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1059                                 sigs[E_SIGNAL_WIFI_AP_OFF].cb, (gpointer)th, NULL);
1060
1061                 type = TETHERING_TYPE_RESERVED;
1062                 dcb = th->disabled_cb[type];
1063                 data = th->disabled_user_data[type];
1064                 if (dcb)
1065                         dcb(error, type, code, data);
1066                 break;
1067
1068         case MOBILE_AP_DISABLE_CFM:
1069
1070                 sigs[E_SIGNAL_WIFI_TETHER_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1071                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_WIFI_TETHER_OFF].name,
1072                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1073                                 sigs[E_SIGNAL_WIFI_TETHER_OFF].cb, (gpointer)th, NULL);
1074                 sigs[E_SIGNAL_BT_TETHER_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1075                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_BT_TETHER_OFF].name,
1076                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1077                                 sigs[E_SIGNAL_BT_TETHER_OFF].cb, (gpointer)th, NULL);
1078                 sigs[E_SIGNAL_USB_TETHER_OFF].sig_id = g_dbus_connection_signal_subscribe(th->client_bus,
1079                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[E_SIGNAL_USB_TETHER_OFF].name,
1080                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1081                                 sigs[E_SIGNAL_USB_TETHER_OFF].cb, (gpointer)th, NULL);
1082
1083                 for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
1084                         dcb = th->disabled_cb[type];
1085                         if (dcb == NULL)
1086                                 continue;
1087                         data = th->disabled_user_data[type];
1088
1089                         dcb(error, type, code, data);
1090                 }
1091                 break;
1092
1093         default:
1094                 ERR("Invalid event\n");
1095                 break;
1096         }
1097         DBG("-\n");
1098 }
1099
1100 static void __get_data_usage_cb(GObject *source_object, GAsyncResult *res,
1101                                 gpointer user_data)
1102 {
1103         DBG("+\n");
1104
1105         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
1106
1107         GError *g_error = NULL;
1108         GVariant *g_var;
1109         guint event_type;
1110         guint64 tx_bytes, rx_bytes;
1111         __tethering_h *th = (__tethering_h *)user_data;
1112         tethering_error_e tethering_error = TETHERING_ERROR_NONE;
1113         bool flag = false;
1114
1115         g_var = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
1116         if (g_error) {
1117                 ERR("DBus fail [%s]\n", g_error->message);
1118                 if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
1119                         tethering_error = TETHERING_ERROR_PERMISSION_DENIED;
1120                 else
1121                         tethering_error = TETHERING_ERROR_OPERATION_FAILED;
1122
1123                 flag = true;
1124         }
1125         if (th->data_usage_cb == NULL) {
1126                 ERR("There is no data_usage_cb\n");
1127                 return;
1128         }
1129         if (flag) {
1130                 th->data_usage_cb(tethering_error, 0LL, 0LL, th->data_usage_user_data);
1131         } else {
1132                 g_variant_get(g_var, "(utt)", &event_type, &tx_bytes, &rx_bytes);
1133                 th->data_usage_cb(TETHERING_ERROR_NONE,
1134                         rx_bytes, tx_bytes, th->data_usage_user_data);
1135                 g_variant_unref(g_var);
1136         }
1137         th->data_usage_cb = NULL;
1138         th->data_usage_user_data = NULL;
1139
1140         DBG("-\n");
1141 }
1142
1143 static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
1144                 gpointer user_data)
1145 {
1146         DBG("+\n");
1147
1148         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
1149         GError *g_error = NULL;
1150         GVariant *g_var;
1151         guint info;
1152         __tethering_h *th = (__tethering_h *)user_data;
1153         tethering_error_e tethering_error;
1154
1155         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
1156         if (g_error) {
1157                 ERR("DBus fail [%s]\n", g_error->message);
1158                 if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
1159                         tethering_error = TETHERING_ERROR_PERMISSION_DENIED;
1160                 else
1161                         tethering_error = TETHERING_ERROR_OPERATION_FAILED;
1162                 g_error_free(g_error);
1163         }
1164         if (th->settings_reloaded_cb == NULL) {
1165                 DBG("There is no settings_reloaded_cb\n-\n");
1166                 return;
1167         }
1168         g_variant_get(g_var, "(u)", &info);
1169         tethering_error = __get_error(info);
1170         g_variant_unref(g_var);
1171
1172         th->settings_reloaded_cb(tethering_error,
1173                         th->settings_reloaded_user_data);
1174
1175         th->settings_reloaded_cb = NULL;
1176         th->settings_reloaded_user_data = NULL;
1177         DBG("-\n");
1178 }
1179
1180 static void __ap_settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
1181                 gpointer user_data)
1182 {
1183         DBG("+\n");
1184
1185         _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
1186         GError *g_error = NULL;
1187         GVariant *g_var;
1188         guint info;
1189         __tethering_h *th = (__tethering_h *)user_data;
1190         tethering_error_e tethering_error;
1191
1192         g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
1193         if (g_error) {
1194                 ERR("DBus fail [%s]\n", g_error->message);
1195                 if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
1196                         tethering_error = TETHERING_ERROR_PERMISSION_DENIED;
1197                 else
1198                         tethering_error = TETHERING_ERROR_OPERATION_FAILED;
1199                 g_error_free(g_error);
1200         }
1201         if (th->ap_settings_reloaded_cb == NULL) {
1202                 DBG("There is no settings_reloaded_cb\n-\n");
1203                 return;
1204         }
1205         g_variant_get(g_var, "(u)", &info);
1206         tethering_error = __get_error(info);
1207         g_variant_unref(g_var);
1208
1209         th->ap_settings_reloaded_cb(tethering_error,
1210                         th->ap_settings_reloaded_user_data);
1211
1212         th->ap_settings_reloaded_cb = NULL;
1213         th->ap_settings_reloaded_user_data = NULL;
1214         DBG("-\n");
1215 }
1216
1217 static void __connect_signals(tethering_h tethering)
1218 {
1219         DBG("+\n");
1220         _retm_if(tethering == NULL, "parameter(tethering) is NULL\n");
1221
1222         __tethering_h *th = (__tethering_h *)tethering;
1223         GDBusConnection *connection = th->client_bus;
1224         int i = 0;
1225
1226         for (i = E_SIGNAL_NET_CLOSED; i < E_SIGNAL_MAX; i++) {
1227                 sigs[i].sig_id = g_dbus_connection_signal_subscribe(connection,
1228                                 NULL, TETHERING_SERVICE_INTERFACE, sigs[i].name,
1229                                 TETHERING_SERVICE_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
1230                                 sigs[i].cb, tethering, NULL);
1231         }
1232         DBG("-\n");
1233 }
1234
1235 static void __disconnect_signals(tethering_h tethering)
1236 {
1237         DBG("+\n");
1238
1239         _retm_if(tethering == NULL, "parameter(tethering) is NULL\n");
1240
1241         __tethering_h *th = (__tethering_h *)tethering;
1242         GDBusConnection *connection = th->client_bus;
1243
1244         int i = 0;
1245
1246         for (i = E_SIGNAL_NET_CLOSED; i < E_SIGNAL_MAX; i++)
1247                 g_dbus_connection_signal_unsubscribe(connection, sigs[i].sig_id);
1248         DBG("-\n");
1249 }
1250
1251
1252
1253 static bool __get_intf_name(tethering_type_e type, char *buf, unsigned int len)
1254 {
1255         _retvm_if(buf == NULL, false, "parameter(buf) is NULL\n");
1256
1257         switch (type) {
1258         case TETHERING_TYPE_USB:
1259                 g_strlcpy(buf, TETHERING_USB_IF, len);
1260                 break;
1261
1262         case TETHERING_TYPE_WIFI:
1263                 g_strlcpy(buf, TETHERING_WIFI_IF, len);
1264                 break;
1265
1266         case TETHERING_TYPE_BT:
1267                 g_strlcpy(buf, TETHERING_BT_IF, len);
1268                 break;
1269
1270         case TETHERING_TYPE_RESERVED:
1271                 g_strlcpy(buf, TETHERING_WIFI_IF, len);
1272                 break;
1273
1274         default:
1275                 ERR("Not supported type : %d\n", type);
1276                 return false;
1277         }
1278         return true;
1279 }
1280
1281 static bool __get_gateway_addr(tethering_type_e type, char *buf, unsigned int len)
1282 {
1283         _retvm_if(buf == NULL, false, "parameter(buf) is NULL\n");
1284
1285         switch (type) {
1286         case TETHERING_TYPE_USB:
1287                 g_strlcpy(buf, TETHERING_USB_GATEWAY, len);
1288                 break;
1289
1290         case TETHERING_TYPE_WIFI:
1291                 g_strlcpy(buf, TETHERING_WIFI_GATEWAY, len);
1292                 break;
1293
1294         case TETHERING_TYPE_BT:
1295                 g_strlcpy(buf, TETHERING_BT_GATEWAY, len);
1296                 break;
1297
1298         case TETHERING_TYPE_RESERVED:
1299                 g_strlcpy(buf, TETHERING_WIFI_GATEWAY, len);
1300                 break;
1301
1302         default:
1303                 ERR("Not supported type : %d\n", type);
1304                 return false;
1305         }
1306         return true;
1307 }
1308
1309 static int __get_common_ssid(char *ssid, unsigned int size)
1310 {
1311         if (ssid == NULL) {
1312                 ERR("ssid is null\n");
1313                 return TETHERING_ERROR_INVALID_PARAMETER;
1314         }
1315
1316         char *ptr = NULL;
1317         char *ptr_tmp = NULL;
1318
1319         ptr = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
1320         if (ptr == NULL) {
1321                 ERR("vconf_get_str is failed and set default ssid");
1322                 g_strlcpy(ssid, TETHERING_DEFAULT_SSID, size);
1323         } else
1324                 g_strlcpy(ssid, ptr, size);
1325
1326         free(ptr);
1327
1328         if (!g_utf8_validate(ssid, -1, (const char **)&ptr_tmp))
1329                 *ptr_tmp = '\0';
1330
1331         return TETHERING_ERROR_NONE;
1332 }
1333
1334 static bool __get_wifi_mode_type(tethering_wifi_mode_type_e type, char **buf)
1335 {
1336         _retvm_if(buf == NULL, false, "parameter(buf) is NULL\n");
1337
1338         switch (type) {
1339                 case TETHERING_WIFI_MODE_TYPE_B:
1340                         *buf = g_strdup("b");
1341                         break;
1342                 case TETHERING_WIFI_MODE_TYPE_G:
1343                         *buf = g_strdup("g");
1344                         break;
1345                 case TETHERING_WIFI_MODE_TYPE_A:
1346                         *buf = g_strdup("a");
1347                         break;
1348                 case TETHERING_WIFI_MODE_TYPE_AD:
1349                         *buf = g_strdup("ad");
1350                         break;
1351                 default:
1352                         ERR("Not supported type : %d\n", type);
1353                         return false;
1354         }
1355         return true;
1356 }
1357
1358 static int __prepare_wifi_settings(tethering_h tethering, _softap_settings_t *set)
1359 {
1360         DBG("+\n");
1361
1362         __tethering_h *th = (__tethering_h *)tethering;
1363         tethering_error_e ret = TETHERING_ERROR_NONE;
1364         char *ptr = NULL;
1365
1366         if (th == NULL || set == NULL) {
1367                 ERR("null parameter\n-\n");
1368                 return TETHERING_ERROR_INVALID_PARAMETER;
1369         }
1370
1371         if (th->ssid == NULL)
1372                 __get_common_ssid(set->ssid, sizeof(set->ssid));
1373         else
1374                 g_strlcpy(set->ssid, th->ssid, sizeof(set->ssid));
1375
1376         ret = __get_security_type(&set->sec_type);
1377         if (ret != TETHERING_ERROR_NONE)
1378                 set->sec_type = th->sec_type;
1379
1380         ret = __get_visible(&set->visibility);
1381         if (ret != TETHERING_ERROR_NONE)
1382                 set->visibility = th->visibility;
1383
1384         set->mac_filter = th->mac_filter;
1385         set->channel = th->channel;
1386
1387         __get_wifi_mode_type (th->mode_type, &ptr);
1388         if (ptr == NULL) {
1389                 g_strlcpy(set->mode, "", sizeof(set->mode));
1390         } else {
1391                 g_strlcpy(set->mode, ptr, sizeof(set->mode));
1392                 free(ptr);
1393         }
1394
1395         if (set->sec_type == TETHERING_WIFI_SECURITY_TYPE_NONE) {
1396                 g_strlcpy(set->key, "", sizeof(set->key));
1397         } else {
1398                 GDBusProxy *proxy = th->client_bus_proxy;
1399                 GVariant *parameters;
1400                 GError *error = NULL;
1401                 char *passphrase = NULL;
1402                 unsigned int len = 0;
1403
1404                 parameters = g_dbus_proxy_call_sync(proxy, "get_wifi_tethering_passphrase",
1405                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1406
1407                 if (error) {
1408                         ERR("g_dbus_proxy_call_sync failed because  %s\n", error->message);
1409
1410                         if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
1411                                 ret = TETHERING_ERROR_PERMISSION_DENIED;
1412                         else
1413                                 ret = TETHERING_ERROR_OPERATION_FAILED;
1414
1415                         g_error_free(error);
1416                         return ret;
1417                 }
1418
1419                 if (parameters != NULL) {
1420                         g_variant_get(parameters, "(siu)", &passphrase, &len, &ret);
1421                         g_variant_unref(parameters);
1422                 }
1423
1424                 g_strlcpy(set->key, passphrase, sizeof(set->key));
1425         }
1426         DBG("-\n");
1427         return TETHERING_ERROR_NONE;
1428 }
1429
1430 static int __prepare_wifi_ap_settings(tethering_h tethering, _softap_settings_t *set)
1431 {
1432         DBG("+\n");
1433
1434         __tethering_h *th = (__tethering_h *)tethering;
1435
1436         if (th == NULL || set == NULL) {
1437                 ERR("null parameter\n");
1438                 return TETHERING_ERROR_INVALID_PARAMETER;
1439         }
1440
1441         g_strlcpy(set->ssid, th->ap_ssid, sizeof(set->ssid));
1442         set->sec_type = th->sec_type;
1443         set->visibility = th->visibility;
1444
1445         if (set->sec_type == TETHERING_WIFI_SECURITY_TYPE_NONE)
1446                 g_strlcpy(set->key, "", sizeof(set->key));
1447         else
1448                 g_strlcpy(set->key, th->passphrase, sizeof(set->key));
1449
1450         DBG("-\n");
1451         return TETHERING_ERROR_NONE;
1452 }
1453
1454 static bool __check_precondition(tethering_type_e type)
1455 {
1456         int dnet_state = 0;
1457         int cellular_state = 0;
1458         int wifi_state = 0;
1459
1460         /* data network through cellular */
1461         vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &cellular_state);
1462         if (cellular_state == VCONFKEY_NETWORK_CELLULAR_ON) {
1463                 ERR("Data Network can be connected later");
1464                 return TRUE;
1465         }
1466
1467         vconf_get_int(VCONFKEY_DNET_STATE, &dnet_state);
1468         if (dnet_state > VCONFKEY_DNET_OFF) {
1469                 ERR("Data Network is connected");
1470                 return TRUE;
1471         }
1472
1473         /* data network through wifi */
1474         if (type != TETHERING_TYPE_WIFI) {
1475                 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
1476                 if (wifi_state > VCONFKEY_WIFI_UNCONNECTED) {
1477                         ERR("Wi-Fi is connected!");
1478                         return TRUE;
1479                 }
1480         }
1481
1482         ERR("Network is not available!");
1483         return FALSE;
1484 }
1485
1486 /**
1487  * @internal
1488  * @brief  Creates the handle of tethering.
1489  * @since_tizen 2.3
1490  * @privlevel platform
1491  * @privilege http://tizen.org/privilege/tethering.admin
1492  * @remarks  The @a tethering must be released tethering_destroy() by you.
1493  * @param[out]  tethering  A handle of a new mobile ap handle on success
1494  * @return  0 on success, otherwise a negative error value.
1495  * @retval  #TETHERING_ERROR_NONE  Successful
1496  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
1497  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
1498  * @retval  #TETHERING_ERROR_NOT_SUPPORT_API  API is not supported
1499  * @see  tethering_destroy()
1500  */
1501 API int tethering_create(tethering_h *tethering)
1502 {
1503         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
1504         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
1505                         "parameter(tethering) is NULL\n");
1506         DBG("+\n");
1507
1508         __tethering_h *th = NULL;
1509         GError *error = NULL;
1510         char ssid[TETHERING_WIFI_SSID_MAX_LEN + 1] = {0, };
1511
1512         th = (__tethering_h *)malloc(sizeof(__tethering_h));
1513
1514         _retvm_if(th == NULL, TETHERING_ERROR_OUT_OF_MEMORY,
1515                         "malloc is failed\n");
1516         memset(th, 0x00, sizeof(__tethering_h));
1517         th->sec_type = TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK;
1518         th->visibility = true;
1519         th->mac_filter = false;
1520         th->channel = 6;
1521         th->mode_type = TETHERING_WIFI_MODE_TYPE_G;
1522
1523         if (__generate_initial_passphrase(th->passphrase,
1524                         sizeof(th->passphrase)) == 0) {
1525                 ERR("random passphrase generation failed\n");
1526                 free(th);
1527                 return TETHERING_ERROR_OPERATION_FAILED;
1528         }
1529
1530         if (__get_common_ssid(ssid, sizeof(ssid)) != TETHERING_ERROR_NONE) {
1531                 ERR("common ssid get failed\n");
1532                 free(th);
1533                 return TETHERING_ERROR_OPERATION_FAILED;
1534         }
1535
1536         th->ap_ssid = g_strdup(ssid);
1537         if (th->ap_ssid == NULL) {
1538                 ERR("g_strdup failed\n");
1539                 free(th);
1540                 return TETHERING_ERROR_OPERATION_FAILED;
1541         }
1542
1543 #if !GLIB_CHECK_VERSION(2, 36, 0)
1544         g_type_init();
1545 #endif
1546         GCancellable *cancellable = g_cancellable_new();
1547         th->client_bus = g_bus_get_sync(DBUS_BUS_SYSTEM, cancellable, &error);
1548         if (error) {
1549                 ERR("Couldn't connect to the System bus[%s]", error->message);
1550                 g_error_free(error);
1551                 g_cancellable_cancel(cancellable);
1552                 g_object_unref(cancellable);
1553                 g_free(th->ap_ssid);
1554                 free(th);
1555                 return TETHERING_ERROR_OPERATION_FAILED;
1556         }
1557         th->cancellable = cancellable;
1558
1559         th->client_bus_proxy = g_dbus_proxy_new_sync(th->client_bus, G_DBUS_PROXY_FLAGS_NONE,
1560                         NULL, TETHERING_SERVICE_NAME, TETHERING_SERVICE_OBJECT_PATH,
1561                         TETHERING_SERVICE_INTERFACE, th->cancellable, &error);
1562         if (!th->client_bus_proxy) {
1563                 ERR("Couldn't create the proxy object because of %s\n", error->message);
1564                 g_cancellable_cancel(th->cancellable);
1565                 g_object_unref(th->cancellable);
1566                 g_object_unref(th->client_bus);
1567                 g_free(th->ap_ssid);
1568                 free(th);
1569                 return TETHERING_ERROR_OPERATION_FAILED;
1570         }
1571
1572         __connect_signals((tethering_h)th);
1573
1574         *tethering = (tethering_h)th;
1575         DBG("Tethering Handle : 0x%X\n", th);
1576         DBG("-\n");
1577         return TETHERING_ERROR_NONE;
1578 }
1579
1580 /**
1581  * @internal
1582  * @brief  Destroys the handle of tethering.
1583  * @since_tizen 2.3
1584  * @privlevel platform
1585  * @privilege http://tizen.org/privilege/tethering.admin
1586  * @param[in]  tethering  The handle of tethering
1587  * @return  0 on success, otherwise a negative error value.
1588  * @retval  #TETHERING_ERROR_NONE  Successful
1589  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
1590  * @see  tethering_create()
1591  */
1592 API int tethering_destroy(tethering_h tethering)
1593 {
1594         DBG("+\n");
1595         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
1596         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
1597                         "parameter(tethering) is NULL\n");
1598
1599         __tethering_h *th = (__tethering_h *)tethering;
1600
1601         DBG("Tethering Handle : 0x%X\n", th);
1602         __disconnect_signals(tethering);
1603
1604         if (th->ssid)
1605                 free(th->ssid);
1606         if (th->ap_ssid)
1607                 free(th->ap_ssid);
1608
1609         g_object_unref(th->cancellable);
1610         g_object_unref(th->client_bus_proxy);
1611         g_object_unref(th->client_bus);
1612         memset(th, 0x00, sizeof(__tethering_h));
1613         free(th);
1614
1615         DBG("-\n");
1616         return TETHERING_ERROR_NONE;
1617 }
1618
1619 /**
1620  * @internal
1621  * @brief Enables the tethering, asynchronously.
1622  * @since_tizen 2.3
1623  * @privlevel platform
1624  * @privilege http://tizen.org/privilege/tethering.admin
1625  * @param[in]  tethering  The handle of tethering
1626  * @param[in]  type  The type of tethering
1627  * @return 0 on success, otherwise negative error value.
1628  * @retval  #TETHERING_ERROR_NONE  Successful
1629  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
1630  * @post tethering_enabled_cb() will be invoked.
1631  * @see  tethering_is_enabled()
1632  * @see  tethering_disable()
1633  */
1634 API int tethering_enable(tethering_h tethering, tethering_type_e type)
1635 {
1636         DBG("+ type :  %d\n", type);
1637         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
1638         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_USB_FEATURE);
1639         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_WIFI_FEATURE);
1640         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_BT_FEATURE);
1641
1642         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
1643                         "parameter(tethering) is NULL\n");
1644
1645         tethering_error_e ret = TETHERING_ERROR_NONE;
1646         __tethering_h *th = (__tethering_h *)tethering;
1647         GDBusProxy *proxy = th->client_bus_proxy;
1648         GDBusConnection *connection = th->client_bus;
1649
1650         g_dbus_proxy_set_default_timeout(proxy, DBUS_TIMEOUT_INFINITE);
1651
1652         if (type != TETHERING_TYPE_RESERVED
1653                 && __check_precondition(type) == FALSE) {
1654                 DBG("-\n");
1655                 return TETHERING_ERROR_OPERATION_FAILED;
1656         }
1657
1658         switch (type) {
1659         case TETHERING_TYPE_USB:
1660                 g_dbus_connection_signal_unsubscribe(connection,
1661                                 sigs[E_SIGNAL_USB_TETHER_ON].sig_id);
1662
1663                 g_dbus_proxy_call(proxy, "enable_usb_tethering", NULL,
1664                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1665                                 (GAsyncReadyCallback) __usb_enabled_cfm_cb, (gpointer)tethering);
1666                 break;
1667
1668         case TETHERING_TYPE_WIFI: {
1669                 _softap_settings_t set = {"", "", "", 0, false};
1670
1671                 ret = __prepare_wifi_settings(tethering, &set);
1672                 if (ret != TETHERING_ERROR_NONE) {
1673                         ERR("softap settings initialization failed\n");
1674                         DBG("-\n");
1675                         return TETHERING_ERROR_OPERATION_FAILED;
1676                 }
1677                 g_dbus_connection_signal_unsubscribe(connection,
1678                                 sigs[E_SIGNAL_WIFI_TETHER_ON].sig_id);
1679
1680                 g_dbus_proxy_call(proxy, "enable_wifi_tethering",
1681                                 g_variant_new("(sssiiii)", set.ssid, set.key, set.mode, set.channel, set.visibility, set.mac_filter, set.sec_type),
1682                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1683                                 (GAsyncReadyCallback) __wifi_enabled_cfm_cb, (gpointer)tethering);
1684                 break;
1685         }
1686
1687         case TETHERING_TYPE_BT:
1688                 g_dbus_connection_signal_unsubscribe(connection,
1689                                 sigs[E_SIGNAL_BT_TETHER_ON].sig_id);
1690
1691                 g_dbus_proxy_call(proxy, "enable_bt_tethering", NULL,
1692                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1693                                 (GAsyncReadyCallback) __bt_enabled_cfm_cb, (gpointer)tethering);
1694
1695                 break;
1696
1697         case TETHERING_TYPE_RESERVED: {
1698                 _softap_settings_t set = {"", "", "", 0, false};
1699
1700                 ret = __prepare_wifi_ap_settings(tethering, &set);
1701                 if (ret != TETHERING_ERROR_NONE) {
1702                         ERR("softap settings initialization failed\n");
1703                         return TETHERING_ERROR_OPERATION_FAILED;
1704                 }
1705
1706                 g_dbus_connection_signal_unsubscribe(connection,
1707                                 sigs[E_SIGNAL_WIFI_AP_ON].sig_id);
1708
1709                 g_dbus_proxy_call(proxy, "enable_wifi_ap",
1710                                 g_variant_new("(ssii)", set.ssid, set.key, set.visibility, set.sec_type),
1711                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable, (GAsyncReadyCallback) __wifi_ap_enabled_cfm_cb, (gpointer)tethering);
1712                 break;
1713         }
1714         case TETHERING_TYPE_ALL: {
1715                 _softap_settings_t set = {"", "", "", 0, false};
1716
1717                 ret = __prepare_wifi_settings(tethering, &set);
1718                 if (ret != TETHERING_ERROR_NONE) {
1719                         ERR("softap settings initialization failed\n");
1720                         return TETHERING_ERROR_OPERATION_FAILED;
1721                 }
1722
1723                 /* TETHERING_TYPE_USB */
1724                 g_dbus_connection_signal_unsubscribe(connection,
1725                                 sigs[E_SIGNAL_USB_TETHER_ON].sig_id);
1726
1727                 g_dbus_proxy_call(proxy, "enable_usb_tethering", NULL,
1728                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1729                                 (GAsyncReadyCallback) __usb_enabled_cfm_cb, (gpointer)tethering);
1730
1731                 /* TETHERING_TYPE_WIFI */
1732                 g_dbus_connection_signal_unsubscribe(connection,
1733                                 sigs[E_SIGNAL_WIFI_TETHER_ON].sig_id);
1734
1735                 g_dbus_proxy_call(proxy, "enable_wifi_tethering",
1736                                 g_variant_new("(ssii)", set.ssid, set.key, set.visibility, set.sec_type),
1737                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1738                                 (GAsyncReadyCallback) __wifi_enabled_cfm_cb, (gpointer)tethering);
1739
1740                 /* TETHERING_TYPE_BT */
1741                 g_dbus_connection_signal_unsubscribe(connection,
1742                                 sigs[E_SIGNAL_BT_TETHER_ON].sig_id);
1743
1744                 g_dbus_proxy_call(proxy, "enable_usb_tethering", NULL,
1745                                 G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1746                                 (GAsyncReadyCallback) __bt_enabled_cfm_cb, (gpointer)tethering);
1747                 break;
1748         }
1749         default:
1750                 ERR("Unknown type : %d\n", type);
1751
1752                 g_dbus_proxy_set_default_timeout(proxy, DBUS_TIMEOUT_USE_DEFAULT);
1753
1754                 DBG("-\n");
1755                 return TETHERING_ERROR_INVALID_PARAMETER;
1756         }
1757
1758         g_dbus_proxy_set_default_timeout(proxy, DBUS_TIMEOUT_USE_DEFAULT);
1759         DBG("-\n");
1760         return TETHERING_ERROR_NONE;
1761 }
1762
1763 /**
1764  * @internal
1765  * @brief Disables the tethering, asynchronously.
1766  * @since_tizen 2.3
1767  * @privlevel platform
1768  * @privilege http://tizen.org/privilege/tethering.admin
1769  * @param[in]  tethering  The handle of tethering
1770  * @param[in]  type  The type of tethering
1771  * @return 0 on success, otherwise negative error value.
1772  * @retval  #TETHERING_ERROR_NONE  Successful
1773  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
1774  * @post tethering_disabled_cb() will be invoked.
1775  * @see  tethering_is_enabled()
1776  * @see  tethering_enable()
1777  */
1778 API int tethering_disable(tethering_h tethering, tethering_type_e type)
1779 {
1780         DBG("+ type :  %d\n", type);
1781         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
1782         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
1783         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
1784
1785         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
1786                         "parameter(tethering) is NULL\n");
1787
1788         __tethering_h *th = (__tethering_h *)tethering;
1789         GDBusProxy *proxy = th->client_bus_proxy;
1790         GDBusConnection *connection = th->client_bus;
1791
1792         switch (type) {
1793         case TETHERING_TYPE_USB:
1794                 g_dbus_connection_signal_unsubscribe(connection,
1795                                 sigs[E_SIGNAL_USB_TETHER_OFF].sig_id);
1796
1797                 g_dbus_proxy_call(proxy, "disable_usb_tethering",
1798                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1799                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1800
1801                 break;
1802
1803         case TETHERING_TYPE_WIFI:
1804
1805                 g_dbus_connection_signal_unsubscribe(connection,
1806                                 sigs[E_SIGNAL_WIFI_TETHER_OFF].sig_id);
1807
1808                 g_dbus_proxy_call(proxy, "disable_wifi_tethering",
1809                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1810                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1811                 break;
1812
1813         case TETHERING_TYPE_BT:
1814
1815                 g_dbus_connection_signal_unsubscribe(connection,
1816                                 sigs[E_SIGNAL_BT_TETHER_OFF].sig_id);
1817
1818                 g_dbus_proxy_call(proxy, "disable_bt_tethering",
1819                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1820                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1821                 break;
1822
1823         case TETHERING_TYPE_RESERVED:
1824                 g_dbus_connection_signal_unsubscribe(connection,
1825                                 sigs[E_SIGNAL_WIFI_AP_OFF].sig_id);
1826
1827                 g_dbus_proxy_call(proxy, "disable_wifi_ap",
1828                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1829                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1830                 break;
1831
1832         case TETHERING_TYPE_ALL:
1833                 g_dbus_connection_signal_unsubscribe(connection,
1834                                 sigs[E_SIGNAL_USB_TETHER_OFF].sig_id);
1835
1836                 g_dbus_proxy_call(proxy, "disable_usb_tethering",
1837                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1838                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1839
1840                 g_dbus_connection_signal_unsubscribe(connection,
1841                                 sigs[E_SIGNAL_WIFI_TETHER_OFF].sig_id);
1842
1843                 g_dbus_proxy_call(proxy, "disable_wifi_tethering",
1844                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1845                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1846
1847                 g_dbus_connection_signal_unsubscribe(connection,
1848                                 sigs[E_SIGNAL_BT_TETHER_OFF].sig_id);
1849
1850                 g_dbus_proxy_call(proxy, "disable_bt_tethering",
1851                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
1852                                 (GAsyncReadyCallback) __disabled_cfm_cb, (gpointer)tethering);
1853                 break;
1854
1855         default:
1856                 ERR("Not supported tethering type [%d]\n", type);
1857                 DBG("-\n");
1858                 return TETHERING_ERROR_INVALID_PARAMETER;
1859         }
1860         DBG("-\n");
1861         return TETHERING_ERROR_NONE;
1862 }
1863
1864 /**
1865  * @internal
1866  * @brief  Checks whetehr the tethering is enabled or not.
1867  * @since_tizen 2.3
1868  * @privlevel platform
1869  * @privilege http://tizen.org/privilege/tethering.admin
1870  * @param[in]  tethering  The handle of tethering
1871  * @param[in]  type  The type of tethering
1872  * @return  @c true if tethering is enabled, \n @c false if tethering is disabled.
1873  */
1874 API bool tethering_is_enabled(tethering_h tethering, tethering_type_e type)
1875 {
1876         int is_on = 0;
1877         int vconf_type = VCONFKEY_MOBILE_HOTSPOT_MODE_NONE;
1878
1879         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
1880
1881         if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &is_on) != 0)
1882                 return FALSE;
1883
1884         switch (type) {
1885         case TETHERING_TYPE_USB:
1886                 vconf_type = VCONFKEY_MOBILE_HOTSPOT_MODE_USB;
1887                 break;
1888
1889         case TETHERING_TYPE_WIFI:
1890                 vconf_type = VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI;
1891                 break;
1892
1893         case TETHERING_TYPE_BT:
1894                 vconf_type = VCONFKEY_MOBILE_HOTSPOT_MODE_BT;
1895                 break;
1896
1897         case TETHERING_TYPE_RESERVED:
1898                 vconf_type = VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP;
1899                 break;
1900
1901         default:
1902                 ERR("Not supported type : %d\n", type);
1903                 break;
1904         }
1905         return is_on & vconf_type ? true : false;
1906 }
1907
1908 /**
1909  * @internal
1910  * @brief  Gets the MAC address of local device as "FC:A1:3E:D6:B1:B1".
1911  * @since_tizen 2.3
1912  * @privlevel platform
1913  * @privilege http://tizen.org/privilege/tethering.admin
1914  * @remarks @a mac_address must be released with free() by you.
1915  * @param[in]  tethering  The handle of tethering
1916  * @param[in]  type  The type of tethering
1917  * @param[out]  mac_address  The MAC address
1918  * @return  0 on success, otherwise a negative error value.
1919  * @retval  #TETHERING_ERROR_NONE  Successful
1920  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
1921  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
1922  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
1923  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
1924  * @pre  tethering must be enabled.
1925  * @see  tethering_is_enabled()
1926  * @see  tethering_enable()
1927  */
1928 API int tethering_get_mac_address(tethering_h tethering, tethering_type_e type, char **mac_address)
1929 {
1930         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
1931         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
1932         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
1933
1934         _retvm_if(tethering_is_enabled(tethering, type) == false,
1935                         TETHERING_ERROR_NOT_ENABLED,
1936                         "tethering type[%d] is not enabled\n", type);
1937         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
1938                         "parameter(tethering) is NULL\n");
1939         _retvm_if(mac_address == NULL, TETHERING_ERROR_INVALID_PARAMETER,
1940                         "parameter(mac_address) is NULL\n");
1941
1942         struct ifreq ifr;
1943         int s = 0;
1944         char *macbuf = NULL;
1945
1946         _retvm_if(!__get_intf_name(type, ifr.ifr_name, sizeof(ifr.ifr_name)),
1947                         TETHERING_ERROR_OPERATION_FAILED,
1948                         "getting interface name is failed\n");
1949
1950         s = socket(AF_INET, SOCK_DGRAM, 0);
1951         _retvm_if(s < 0, TETHERING_ERROR_OPERATION_FAILED,
1952                         "getting socket is failed\n");
1953         if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
1954                 ERR("getting mac is failed\n");
1955                 close(s);
1956                 return TETHERING_ERROR_OPERATION_FAILED;
1957         }
1958         close(s);
1959
1960         macbuf = (char *)malloc(TETHERING_STR_INFO_LEN);
1961         _retvm_if(macbuf == NULL, TETHERING_ERROR_OUT_OF_MEMORY,
1962                         "Not enough memory\n");
1963         snprintf(macbuf, TETHERING_STR_INFO_LEN, "%02X:%02X:%02X:%02X:%02X:%02X",
1964                         (unsigned char)ifr.ifr_hwaddr.sa_data[0],
1965                         (unsigned char)ifr.ifr_hwaddr.sa_data[1],
1966                         (unsigned char)ifr.ifr_hwaddr.sa_data[2],
1967                         (unsigned char)ifr.ifr_hwaddr.sa_data[3],
1968                         (unsigned char)ifr.ifr_hwaddr.sa_data[4],
1969                         (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
1970
1971         *mac_address = macbuf;
1972
1973         return TETHERING_ERROR_NONE;
1974 }
1975
1976 /**
1977  * @internal
1978  * @brief Gets the name of network interface. For example, usb0.
1979  * @since_tizen 2.3
1980  * @privlevel platform
1981  * @privilege http://tizen.org/privilege/tethering.admin
1982  * @remarks @a interface_name must be released with free() by you.
1983  * @param[in]  tethering  The handle of tethering
1984  * @param[in]  type  The type of tethering
1985  * @param[out]  interface_name  The name of network interface
1986  * @return 0 on success, otherwise negative error value.
1987  * @retval  #TETHERING_ERROR_NONE  Successful
1988  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
1989  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
1990  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
1991  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
1992  * @pre  tethering must be enabled.
1993  * @see  tethering_is_enabled()
1994  * @see  tethering_enable()
1995  */
1996 API int tethering_get_network_interface_name(tethering_h tethering, tethering_type_e type, char **interface_name)
1997 {
1998         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
1999         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2000         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2001
2002         _retvm_if(tethering_is_enabled(tethering, type) == false,
2003                         TETHERING_ERROR_NOT_ENABLED,
2004                         "tethering type[%d] is not enabled\n", type);
2005         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2006                         "parameter(tethering) is NULL\n");
2007         _retvm_if(interface_name == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2008                         "parameter(interface_name) is NULL\n");
2009
2010         char intf[TETHERING_STR_INFO_LEN] = {0, };
2011
2012         _retvm_if(!__get_intf_name(type, intf, sizeof(intf)),
2013                         TETHERING_ERROR_OPERATION_FAILED,
2014                         "getting interface name is failed\n");
2015         *interface_name = strdup(intf);
2016         _retvm_if(*interface_name == NULL, TETHERING_ERROR_OUT_OF_MEMORY,
2017                         "Not enough memory\n");
2018
2019         return TETHERING_ERROR_NONE;
2020 }
2021
2022 /**
2023  * @internal
2024  * @brief Gets the local IP address.
2025  * @since_tizen 2.3
2026  * @privlevel platform
2027  * @privilege http://tizen.org/privilege/tethering.admin
2028  * @remarks @a ip_address must be released with free() by you.
2029  * @param[in]  tethering  The handle of tethering
2030  * @param[in]  type  The type of tethering
2031  * @param[in]  address_family  The address family of IP address. Currently, #TETHERING_ADDRESS_FAMILY_IPV4 is only supported.
2032  * @param[out]  ip_address  The local IP address
2033  * @return 0 on success, otherwise negative error value.
2034  * @retval  #TETHERING_ERROR_NONE  Successful
2035  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2036  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
2037  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2038  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
2039  * @pre  tethering must be enabled.
2040  * @see  tethering_is_enabled()
2041  * @see  tethering_enable()
2042  */
2043 API int tethering_get_ip_address(tethering_h tethering, tethering_type_e type, tethering_address_family_e address_family, char **ip_address)
2044 {
2045
2046         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2047         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2048         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2049
2050         _retvm_if(tethering_is_enabled(tethering, type) == false,
2051                         TETHERING_ERROR_NOT_ENABLED,
2052                         "tethering type[%d] is not enabled\n", type);
2053         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2054                         "parameter(tethering) is NULL\n");
2055         _retvm_if(ip_address == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2056                         "parameter(ip_address) is NULL\n");
2057
2058         struct ifreq ifr;
2059         int s = 0;
2060         char *ipbuf = NULL;
2061
2062         _retvm_if(!__get_intf_name(type, ifr.ifr_name, sizeof(ifr.ifr_name)),
2063                         TETHERING_ERROR_OPERATION_FAILED,
2064                         "getting interface name is failed\n");
2065
2066         s = socket(AF_INET, SOCK_DGRAM, 0);
2067         _retvm_if(s < 0, TETHERING_ERROR_OPERATION_FAILED,
2068                         "getting socket is failed\n");
2069         if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
2070                 ERR("ioctl is failed\n");
2071                 close(s);
2072                 return TETHERING_ERROR_OPERATION_FAILED;
2073         }
2074         close(s);
2075
2076         ipbuf = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
2077         *ip_address = strdup(ipbuf);
2078         _retvm_if(*ip_address == NULL, TETHERING_ERROR_OUT_OF_MEMORY,
2079                         "Not enough memory\n");
2080
2081         return TETHERING_ERROR_NONE;
2082 }
2083
2084 /**
2085  * @internal
2086  * @brief Gets the Gateway address.
2087  * @since_tizen 2.3
2088  * @privlevel platform
2089  * @privilege http://tizen.org/privilege/tethering.admin
2090  * @remarks @a gateway_address must be released with free() by you.
2091  * @param[in]  tethering  The handle of tethering
2092  * @param[in]  type  The type of tethering
2093  * @param[in]  address_family  The address family of IP address. Currently, #TETHERING_ADDRESS_FAMILY_IPV4 is only supported.
2094  * @param[out]  gateway_address  The local IP address
2095  * @return 0 on success, otherwise negative error value.
2096  * @retval  #TETHERING_ERROR_NONE  Successful
2097  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2098  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
2099  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2100  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
2101  * @pre  tethering must be enabled.
2102  * @see  tethering_is_enabled()
2103  * @see  tethering_enable()
2104  */
2105 API int tethering_get_gateway_address(tethering_h tethering, tethering_type_e type, tethering_address_family_e address_family, char **gateway_address)
2106 {
2107
2108         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2109         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2110         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2111
2112         _retvm_if(tethering_is_enabled(tethering, type) == false,
2113                         TETHERING_ERROR_NOT_ENABLED,
2114                         "tethering type[%d] is not enabled\n", type);
2115         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2116                         "parameter(tethering) is NULL\n");
2117         _retvm_if(gateway_address == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2118                         "parameter(gateway_address) is NULL\n");
2119
2120         char gateway_buf[TETHERING_STR_INFO_LEN] = {0, };
2121
2122         _retvm_if(!__get_gateway_addr(type, gateway_buf, sizeof(gateway_buf)),
2123                         TETHERING_ERROR_OPERATION_FAILED,
2124                         "getting gateway address is failed\n");
2125
2126         *gateway_address = strdup(gateway_buf);
2127
2128         return TETHERING_ERROR_NONE;
2129 }
2130
2131 /**
2132  * @internal
2133  * @brief Gets the Subnet Mask.
2134  * @since_tizen 2.3
2135  * @privlevel platform
2136  * @privilege http://tizen.org/privilege/tethering.admin
2137  * @remarks @a subnet_mask must be released with free() by you.
2138  * @param[in]  tethering  The handle of tethering
2139  * @param[in]  type  The type of tethering
2140  * @param[in]  address_family  The address family of IP address. Currently, #TETHERING_ADDRESS_FAMILY_IPV4 is only supported.
2141  * @param[out]  subnet_mask  The local IP address
2142  * @return 0 on success, otherwise negative error value.
2143  * @retval  #TETHERING_ERROR_NONE  Successful
2144  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2145  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
2146  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2147  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
2148  * @pre  tethering must be enabled.
2149  * @see  tethering_is_enabled()
2150  * @see  tethering_enable()
2151  */
2152 API int tethering_get_subnet_mask(tethering_h tethering, tethering_type_e type, tethering_address_family_e address_family, char **subnet_mask)
2153 {
2154         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2155         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2156         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2157
2158         _retvm_if(tethering_is_enabled(tethering, type) == false,
2159                         TETHERING_ERROR_NOT_ENABLED,
2160                         "tethering is not enabled\n");
2161         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2162                         "parameter(tethering) is NULL\n");
2163         _retvm_if(subnet_mask == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2164                         "parameter(subnet_mask) is NULL\n");
2165
2166         *subnet_mask = strdup(TETHERING_SUBNET_MASK);
2167         _retvm_if(*subnet_mask == NULL, TETHERING_ERROR_OUT_OF_MEMORY,
2168                         "Not enough memory\n");
2169
2170         return TETHERING_ERROR_NONE;
2171 }
2172
2173 /**
2174  * @internal
2175  * @brief Gets the data usage.
2176  * @since_tizen 2.3
2177  * @privlevel platform
2178  * @privilege http://tizen.org/privilege/tethering.admin
2179  * @param[in]  tethering  The handle of tethering
2180  * @param[out]  usage  The data usage
2181  * @return 0 on success, otherwise negative error value.
2182  * @retval  #TETHERING_ERROR_NONE  Successful
2183  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2184  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2185  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
2186  * @pre  tethering must be enabled.
2187  * @see  tethering_is_enabled()
2188  * @see  tethering_enable()
2189  */
2190 API int tethering_get_data_usage(tethering_h tethering, tethering_data_usage_cb callback, void *user_data)
2191 {
2192         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE);
2193
2194         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2195                         "parameter(tethering) is NULL\n");
2196         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2197                         "parameter(callback) is NULL\n");
2198         _retvm_if(__any_tethering_is_enabled(tethering) == false,
2199                         TETHERING_ERROR_NOT_ENABLED,
2200                         "tethering is not enabled\n");
2201
2202         __tethering_h *th = (__tethering_h *)tethering;
2203         GDBusProxy *proxy = th->client_bus_proxy;
2204
2205         th->data_usage_cb = callback;
2206         th->data_usage_user_data = user_data;
2207
2208         g_dbus_proxy_call(proxy, "get_data_packet_usage",
2209                         NULL, G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
2210                         (GAsyncReadyCallback) __get_data_usage_cb, (gpointer)tethering);
2211
2212         return TETHERING_ERROR_NONE;
2213 }
2214
2215 /**
2216  * @internal
2217  * @brief Gets the client which is connected by tethering "type".
2218  * @since_tizen 2.3
2219  * @privlevel platform
2220  * @privilege http://tizen.org/privilege/tethering.admin
2221  * @param[in]  tethering  The handle of tethering
2222  * @param[in]  type  The type of tethering
2223  * @param[in]  callback  The callback function to invoke
2224  * @param[in]  user_data  The user data to be passed to the callback function
2225  * @retval  #TETHERING_ERROR_NONE  Successful
2226  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2227  * @retval  #TETHERING_ERROR_NOT_ENABLED  Not enabled
2228  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2229  * @pre  tethering must be enabled.
2230  * @see  tethering_is_enabled()
2231  * @see  tethering_enable()
2232  */
2233 API int tethering_foreach_connected_clients(tethering_h tethering, tethering_type_e type, tethering_connected_client_cb callback, void *user_data)
2234 {
2235         DBG("+\n");
2236         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2237         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2238         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2239
2240         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2241                         "parameter(tethering) is NULL\n");
2242         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2243                         "parameter(callback) is NULL\n");
2244         _retvm_if(__any_tethering_is_enabled(tethering) == false,
2245                         TETHERING_ERROR_NOT_ENABLED,
2246                         "tethering is not enabled\n");
2247
2248         mobile_ap_type_e interface;
2249         __tethering_h *th = (__tethering_h *)tethering;
2250         __tethering_client_h client = {0, };
2251         gchar *ip = NULL;
2252         gchar *mac = NULL;
2253         gchar *hostname = NULL;
2254         guint timestamp = 0;
2255         GError *error = NULL;
2256         GVariant *result = NULL;
2257         GVariantIter *outer_iter = NULL;
2258         GVariantIter *inner_iter = NULL;
2259         GVariant *station = NULL;
2260         GVariant *value = NULL;
2261         gchar *key = NULL;
2262
2263         result = g_dbus_proxy_call_sync(th->client_bus_proxy, "get_station_info",
2264                         NULL, G_DBUS_CALL_FLAGS_NONE,
2265                         -1, th->cancellable, &error);
2266         if (error)
2267                 ERR("g_dbus_proxy_call_sync is failed and error is %s\n", error->message);
2268         g_variant_get(result, "(a(a{sv}))", &outer_iter);
2269         while (g_variant_iter_loop(outer_iter, "(@a{sv})", &station)) {
2270                 g_variant_get(station, "a{sv}", &inner_iter);
2271                 while (g_variant_iter_loop(inner_iter, "{sv}", &key, &value)) {
2272                         if (g_strcmp0(key, "Type") == 0) {
2273                                 interface = g_variant_get_int32(value);
2274                                 if (interface == MOBILE_AP_TYPE_USB)
2275                                         client.interface = TETHERING_TYPE_USB;
2276                                 else if (interface == MOBILE_AP_TYPE_WIFI)
2277                                         client.interface = TETHERING_TYPE_WIFI;
2278                                 else if (interface == MOBILE_AP_TYPE_BT)
2279                                         client.interface = TETHERING_TYPE_BT;
2280                                 else if (interface == MOBILE_AP_TYPE_WIFI_AP)
2281                                         client.interface = TETHERING_TYPE_RESERVED;
2282                                 else {
2283                                         ERR("Invalid interface\n");
2284                                         g_free(key);
2285                                         g_variant_unref(value);
2286                                         break;
2287                                 }
2288                                 DBG("interface is %d\n", client.interface);
2289                                 if (client.interface != type && (TETHERING_TYPE_ALL != type &&
2290                                                         client.interface != TETHERING_TYPE_RESERVED)) {
2291                                         g_free(key);
2292                                         g_variant_unref(value);
2293                                         break;
2294                                 }
2295                         } else if (g_strcmp0(key, "IP") == 0) {
2296                                 g_variant_get(value, "s", &ip);
2297                                 SDBG("ip is %s\n", ip);
2298                                 g_strlcpy(client.ip, ip, sizeof(client.ip));
2299                         } else if (g_strcmp0(key, "MAC") == 0) {
2300                                 g_variant_get(value, "s", &mac);
2301                                 SDBG("mac is %s\n", mac);
2302                                 g_strlcpy(client.mac, mac, sizeof(client.mac));
2303                         } else if (g_strcmp0(key, "Name") == 0) {
2304                                 g_variant_get(value, "s", &hostname);
2305                                 SDBG("hsotname is %s\n", hostname);
2306                                 if (hostname)
2307                                         client.hostname = g_strdup(hostname);
2308                         } else if (g_strcmp0(key, "Time") == 0) {
2309                                 timestamp = g_variant_get_int32(value);
2310                                 DBG("timestamp is %d\n", timestamp);
2311                                 client.tm = (time_t)timestamp;
2312                         } else {
2313                                 ERR("Key %s not required\n", key);
2314                         }
2315                 }
2316                 g_free(hostname);
2317                 g_free(ip);
2318                 g_free(mac);
2319                 g_variant_iter_free(inner_iter);
2320                 if (callback((tethering_client_h)&client, user_data) == false) {
2321                         DBG("iteration is stopped\n");
2322                         g_free(client.hostname);
2323                         g_variant_iter_free(outer_iter);
2324                         g_variant_unref(station);
2325                         g_variant_unref(result);
2326                         DBG("-\n");
2327                         return TETHERING_ERROR_OPERATION_FAILED;
2328                 }
2329                 g_free(client.hostname);
2330         }
2331         g_variant_iter_free(outer_iter);
2332         g_variant_unref(station);
2333         g_variant_unref(result);
2334         DBG("-\n");
2335         return TETHERING_ERROR_NONE;
2336 }
2337
2338 /**
2339  * @internal
2340  * @brief Registers the callback function called when tethering is enabled.
2341  * @since_tizen 2.3
2342  * @privlevel platform
2343  * @privilege http://tizen.org/privilege/tethering.admin
2344  * @param[in]  tethering  The handle of tethering
2345  * @param[in]  type  The type of tethering
2346  * @param[in]  callback  The callback function to invoke
2347  * @param[in]  user_data  The user data to be passed to the callback function
2348  * @retval  #TETHERING_ERROR_NONE  Successful
2349  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2350  * @see  tethering_unset_enabled_cb()
2351  */
2352 API int tethering_set_enabled_cb(tethering_h tethering, tethering_type_e type, tethering_enabled_cb callback, void *user_data)
2353 {
2354         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2355         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2356         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2357
2358         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2359                         "parameter(tethering) is NULL\n");
2360         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2361                         "parameter(callback) is NULL\n");
2362
2363         __tethering_h *th = (__tethering_h *)tethering;
2364         tethering_type_e ti;
2365
2366         if (type != TETHERING_TYPE_ALL) {
2367                 th->enabled_cb[type] = callback;
2368                 th->enabled_user_data[type] = user_data;
2369
2370                 return TETHERING_ERROR_NONE;
2371         }
2372
2373         /* TETHERING_TYPE_ALL */
2374         for (ti = TETHERING_TYPE_USB; ti <= TETHERING_TYPE_BT; ti++) {
2375                 th->enabled_cb[ti] = callback;
2376                 th->enabled_user_data[ti] = user_data;
2377         }
2378
2379         return TETHERING_ERROR_NONE;
2380 }
2381
2382 /**
2383  * @internal
2384  * @brief Unregisters the callback function called when tethering is disabled.
2385  * @since_tizen 2.3
2386  * @privlevel platform
2387  * @privilege http://tizen.org/privilege/tethering.admin
2388  * @param[in]  tethering  The handle of tethering
2389  * @param[in]  type  The type of tethering
2390  * @retval  #TETHERING_ERROR_NONE  Successful
2391  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2392  * @see  tethering_set_enabled_cb()
2393  */
2394 API int tethering_unset_enabled_cb(tethering_h tethering, tethering_type_e type)
2395 {
2396         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2397         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2398         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2399
2400         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2401                         "parameter(tethering) is NULL\n");
2402
2403         __tethering_h *th = (__tethering_h *)tethering;
2404         tethering_type_e ti;
2405
2406         if (type != TETHERING_TYPE_ALL) {
2407                 th->enabled_cb[type] = NULL;
2408                 th->enabled_user_data[type] = NULL;
2409
2410                 return TETHERING_ERROR_NONE;
2411         }
2412
2413         /* TETHERING_TYPE_ALL */
2414         for (ti = TETHERING_TYPE_USB; ti <= TETHERING_TYPE_BT; ti++) {
2415                 th->enabled_cb[ti] = NULL;
2416                 th->enabled_user_data[ti] = NULL;
2417         }
2418
2419         return TETHERING_ERROR_NONE;
2420 }
2421
2422 /**
2423  * @internal
2424  * @brief Registers the callback function called when tethering is disabled.
2425  * @since_tizen 2.3
2426  * @privlevel platform
2427  * @privilege http://tizen.org/privilege/tethering.admin
2428  * @param[in]  tethering  The handle of tethering
2429  * @param[in]  type  The type of tethering
2430  * @param[in]  callback  The callback function to invoke
2431  * @param[in]  user_data  The user data to be passed to the callback function
2432  * @retval  #TETHERING_ERROR_NONE  Successful
2433  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2434  * @see  tethering_unset_disabled_cb()
2435  */
2436 API int tethering_set_disabled_cb(tethering_h tethering, tethering_type_e type, tethering_disabled_cb callback, void *user_data)
2437 {
2438         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2439         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2440         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2441
2442         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2443                         "parameter(tethering) is NULL\n");
2444         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2445                         "parameter(callback) is NULL\n");
2446
2447         __tethering_h *th = (__tethering_h *)tethering;
2448         tethering_type_e ti;
2449
2450         if (type != TETHERING_TYPE_ALL) {
2451                 th->disabled_cb[type] = callback;
2452                 th->disabled_user_data[type] = user_data;
2453
2454                 return TETHERING_ERROR_NONE;
2455         }
2456
2457         /* TETHERING_TYPE_ALL */
2458         for (ti = TETHERING_TYPE_USB; ti <= TETHERING_TYPE_BT; ti++) {
2459                 th->disabled_cb[ti] = callback;
2460                 th->disabled_user_data[ti] = user_data;
2461         }
2462
2463         return TETHERING_ERROR_NONE;
2464 }
2465
2466 /**
2467  * @internal
2468  * @brief Unregisters the callback function called when tethering is disabled.
2469  * @since_tizen 2.3
2470  * @privlevel platform
2471  * @privilege http://tizen.org/privilege/tethering.admin
2472  * @param[in]  tethering  The handle of tethering
2473  * @param[in]  type  The type of tethering
2474  * @retval  #TETHERING_ERROR_NONE  Successful
2475  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2476  * @see  tethering_set_disabled_cb()
2477  */
2478 API int tethering_unset_disabled_cb(tethering_h tethering, tethering_type_e type)
2479 {
2480         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2481         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2482         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2483
2484         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2485                         "parameter(tethering) is NULL\n");
2486
2487         __tethering_h *th = (__tethering_h *)tethering;
2488         tethering_type_e ti;
2489
2490         if (type != TETHERING_TYPE_ALL) {
2491                 th->disabled_cb[type] = NULL;
2492                 th->disabled_user_data[type] = NULL;
2493
2494                 return TETHERING_ERROR_NONE;
2495         }
2496
2497         /* TETHERING_TYPE_ALL */
2498         for (ti = TETHERING_TYPE_USB; ti <= TETHERING_TYPE_BT; ti++) {
2499                 th->disabled_cb[ti] = NULL;
2500                 th->disabled_user_data[ti] = NULL;
2501         }
2502
2503         return TETHERING_ERROR_NONE;
2504 }
2505
2506 /**
2507  * @internal
2508  * @brief Registers the callback function called when the state of connection is changed.
2509  * @since_tizen 2.3
2510  * @privlevel platform
2511  * @privilege http://tizen.org/privilege/tethering.admin
2512  * @param[in]  tethering  The handle of tethering
2513  * @param[in]  type  The type of tethering
2514  * @param[in]  callback  The callback function to invoke
2515  * @param[in]  user_data  The user data to be passed to the callback function
2516  * @retval  #TETHERING_ERROR_NONE  Successful
2517  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2518  * @see  tethering_unset_connection_state_changed_cb_cb()
2519  */
2520 API int tethering_set_connection_state_changed_cb(tethering_h tethering, tethering_type_e type, tethering_connection_state_changed_cb callback, void *user_data)
2521 {
2522         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2523         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2524         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2525
2526         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2527                         "parameter(tethering) is NULL\n");
2528         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2529                         "parameter(callback) is NULL\n");
2530
2531         __tethering_h *th = (__tethering_h *)tethering;
2532         tethering_type_e ti;
2533
2534         if (type != TETHERING_TYPE_ALL) {
2535                 th->changed_cb[type] = callback;
2536                 th->changed_user_data[type] = user_data;
2537
2538                 return TETHERING_ERROR_NONE;
2539         }
2540
2541         /* TETHERING_TYPE_ALL */
2542         for (ti = TETHERING_TYPE_USB; ti <= TETHERING_TYPE_BT; ti++) {
2543                 th->changed_cb[ti] = callback;
2544                 th->changed_user_data[ti] = user_data;
2545         }
2546
2547         return TETHERING_ERROR_NONE;
2548 }
2549
2550 /**
2551  * @internal
2552  * @brief Unregisters the callback function called when the state of connection is changed.
2553  * @since_tizen 2.3
2554  * @privlevel platform
2555  * @privilege http://tizen.org/privilege/tethering.admin
2556  * @param[in]  tethering  The handle of tethering
2557  * @param[in]  type  The type of tethering
2558  * @retval  #TETHERING_ERROR_NONE  Successful
2559  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2560  * @see  tethering_set_connection_state_changed_cb()
2561  */
2562 API int tethering_unset_connection_state_changed_cb(tethering_h tethering, tethering_type_e type)
2563 {
2564         if (type == TETHERING_TYPE_USB) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_USB_FEATURE);
2565         else if (type == TETHERING_TYPE_WIFI) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2566         else if (type == TETHERING_TYPE_BT) CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_BT_FEATURE);
2567
2568         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2569                         "parameter(tethering) is NULL\n");
2570
2571         __tethering_h *th = (__tethering_h *)tethering;
2572         tethering_type_e ti;
2573
2574         if (type != TETHERING_TYPE_ALL) {
2575                 th->changed_cb[type] = NULL;
2576                 th->changed_user_data[type] = NULL;
2577
2578                 return TETHERING_ERROR_NONE;
2579         }
2580
2581         /* TETHERING_TYPE_ALL */
2582         for (ti = TETHERING_TYPE_USB; ti <= TETHERING_TYPE_BT; ti++) {
2583                 th->changed_cb[ti] = NULL;
2584                 th->changed_user_data[ti] = NULL;
2585         }
2586
2587         return TETHERING_ERROR_NONE;
2588 }
2589
2590 /**
2591  * @internal
2592  * @brief Registers the callback function called when the security type of Wi-Fi tethering is changed.
2593  * @since_tizen 2.3
2594  * @privlevel platform
2595  * @privilege http://tizen.org/privilege/tethering.admin
2596  * @param[in]  tethering  The handle of tethering
2597  * @param[in]  callback  The callback function to invoke
2598  * @param[in]  user_data  The user data to be passed to the callback function
2599  * @retval  #TETHERING_ERROR_NONE  Successful
2600  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2601  * @see  tethering_wifi_unset_security_type_changed_cb()
2602  */
2603 API int tethering_wifi_set_security_type_changed_cb(tethering_h tethering, tethering_wifi_security_type_changed_cb callback, void *user_data)
2604 {
2605         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2606
2607         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2608                         "parameter(tethering) is NULL\n");
2609         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2610                         "parameter(callback) is NULL\n");
2611
2612         __tethering_h *th = (__tethering_h *)tethering;
2613
2614         th->security_type_changed_cb = callback;
2615         th->security_type_user_data = user_data;
2616
2617         return TETHERING_ERROR_NONE;
2618
2619 }
2620
2621 /**
2622  * @internal
2623  * @brief Unregisters the callback function called when the security type of Wi-Fi tethering is changed.
2624  * @since_tizen 2.3
2625  * @privlevel platform
2626  * @privilege http://tizen.org/privilege/tethering.admin
2627  * @param[in]  tethering  The handle of tethering
2628  * @param[in]  type  The type of tethering
2629  * @retval  #TETHERING_ERROR_NONE  Successful
2630  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2631  * @see  tethering_wifi_set_security_type_changed_cb()
2632  */
2633 API int tethering_wifi_unset_security_type_changed_cb(tethering_h tethering)
2634 {
2635         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2636
2637         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2638                         "parameter(tethering) is NULL\n");
2639
2640         __tethering_h *th = (__tethering_h *)tethering;
2641
2642         th->security_type_changed_cb = NULL;
2643         th->security_type_user_data = NULL;
2644
2645         return TETHERING_ERROR_NONE;
2646 }
2647
2648 /**
2649  * @internal
2650  * @brief Registers the callback function called when the visibility of SSID is changed.
2651  * @since_tizen 2.3
2652  * @privlevel platform
2653  * @privilege http://tizen.org/privilege/tethering.admin
2654  * @param[in]  tethering  The handle of tethering
2655  * @param[in]  callback  The callback function to invoke
2656  * @param[in]  user_data  The user data to be passed to the callback function
2657  * @retval  #TETHERING_ERROR_NONE  Successful
2658  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2659  * @see  tethering_wifi_unset_ssid_visibility_changed_cb_cb()
2660  */
2661 API int tethering_wifi_set_ssid_visibility_changed_cb(tethering_h tethering, tethering_wifi_ssid_visibility_changed_cb callback, void *user_data)
2662 {
2663         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2664
2665         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2666                         "parameter(tethering) is NULL\n");
2667         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2668                         "parameter(callback) is NULL\n");
2669
2670         __tethering_h *th = (__tethering_h *)tethering;
2671
2672         th->ssid_visibility_changed_cb = callback;
2673         th->ssid_visibility_user_data = user_data;
2674
2675         return TETHERING_ERROR_NONE;
2676 }
2677
2678 /**
2679  * @internal
2680  * @brief Unregisters the callback function called when the visibility of SSID is changed.
2681  * @since_tizen 2.3
2682  * @privlevel platform
2683  * @privilege http://tizen.org/privilege/tethering.admin
2684  * @param[in]  tethering  The handle of tethering
2685  * @retval  #TETHERING_ERROR_NONE  Successful
2686  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2687  * @see  tethering_wifi_set_ssid_visibility_changed_cb()
2688  */
2689 API int tethering_wifi_unset_ssid_visibility_changed_cb(tethering_h tethering)
2690 {
2691         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2692
2693         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2694                         "parameter(tethering) is NULL\n");
2695
2696         __tethering_h *th = (__tethering_h *)tethering;
2697
2698         th->ssid_visibility_changed_cb = NULL;
2699         th->ssid_visibility_user_data = NULL;
2700
2701         return TETHERING_ERROR_NONE;
2702 }
2703
2704 /**
2705  * @internal
2706  * @brief Registers the callback function called when the passphrase of Wi-Fi tethering is changed.
2707  * @since_tizen 2.3
2708  * @privlevel platform
2709  * @privilege http://tizen.org/privilege/tethering.admin
2710  * @param[in]  tethering  The handle of tethering
2711  * @param[in]  callback  The callback function to invoke
2712  * @param[in]  user_data  The user data to be passed to the callback function
2713  * @retval  #TETHERING_ERROR_NONE  Successful
2714  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2715  * @see  tethering_wifi_unset_passphrase_changed_cb()
2716  */
2717 API int tethering_wifi_set_passphrase_changed_cb(tethering_h tethering, tethering_wifi_passphrase_changed_cb callback, void *user_data)
2718 {
2719         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2720
2721         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2722                         "parameter(tethering) is NULL\n");
2723         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2724                         "parameter(callback) is NULL\n");
2725
2726         __tethering_h *th = (__tethering_h *)tethering;
2727
2728         th->passphrase_changed_cb = callback;
2729         th->passphrase_user_data = user_data;
2730
2731         return TETHERING_ERROR_NONE;
2732 }
2733
2734 /**
2735  * @internal
2736  * @brief Unregisters the callback function called when the passphrase of Wi-Fi tethering is changed.
2737  * @since_tizen 2.3
2738  * @privlevel platform
2739  * @privilege http://tizen.org/privilege/tethering.admin
2740  * @param[in]  tethering  The handle of tethering
2741  * @retval  #TETHERING_ERROR_NONE  Successful
2742  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2743  * @see  tethering_wifi_set_passphrase_changed_cb()
2744  */
2745 API int tethering_wifi_unset_passphrase_changed_cb(tethering_h tethering)
2746 {
2747         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2748
2749         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2750                         "parameter(tethering) is NULL\n");
2751
2752         __tethering_h *th = (__tethering_h *)tethering;
2753
2754         th->passphrase_changed_cb = NULL;
2755         th->passphrase_user_data = NULL;
2756
2757         return TETHERING_ERROR_NONE;
2758 }
2759
2760 /**
2761  * @internal
2762  * @brief Sets the security type of Wi-Fi tethering.
2763  * @since_tizen 2.3
2764  * @privlevel platform
2765  * @privilege http://tizen.org/privilege/tethering.admin
2766  * @remarks This change is applied next time Wi-Fi tethering is enabled
2767  * @param[in]  tethering  The handle of tethering
2768  * @param[in]  type  The security type
2769  * @return 0 on success, otherwise negative error value.
2770  * @retval  #TETHERING_ERROR_NONE  Successful
2771  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2772  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2773  * @see  tethering_wifi_get_security_type()
2774  */
2775 API int tethering_wifi_set_security_type(tethering_h tethering, tethering_wifi_security_type_e type)
2776 {
2777         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2778
2779         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2780                         "parameter(tethering) is NULL\n");
2781
2782         __tethering_h *th = (__tethering_h *)tethering;
2783         tethering_error_e ret = TETHERING_ERROR_NONE;
2784
2785         ret = __set_security_type(type);
2786         if (ret == TETHERING_ERROR_NONE) {
2787
2788                 __send_dbus_signal(th->client_bus,
2789                                 SIGNAL_NAME_SECURITY_TYPE_CHANGED,
2790                                 type == TETHERING_WIFI_SECURITY_TYPE_NONE ?
2791                                 TETHERING_WIFI_SECURITY_TYPE_OPEN_STR :
2792                                 TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK_STR);
2793         }
2794         return ret;
2795 }
2796
2797 /**
2798  * @internal
2799  * @brief Gets the security type of Wi-Fi tethering.
2800  * @since_tizen 2.3
2801  * @privlevel platform
2802  * @privilege http://tizen.org/privilege/tethering.admin
2803  * @param[in]  tethering  The handle of tethering
2804  * @param[out]  type  The security type
2805  * @return 0 on success, otherwise negative error value.
2806  * @retval  #TETHERING_ERROR_NONE  Successful
2807  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2808  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2809  * @see  tethering_wifi_set_security_type()
2810  */
2811 API int tethering_wifi_get_security_type(tethering_h tethering, tethering_wifi_security_type_e *type)
2812 {
2813         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2814
2815         _retvm_if(type == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2816                         "parameter(type) is NULL\n");
2817
2818         return __get_security_type(type);
2819 }
2820
2821 /**
2822  * @internal
2823  * @brief Sets the SSID (service set identifier).
2824  * @since_tizen 2.3
2825  * @privlevel platform
2826  * @privilege http://tizen.org/privilege/tethering.admin
2827  * @details If SSID is not set, Device name is used as SSID
2828  * @remarks This change is applied next time Wi-Fi tethering is enabled with same @a tethering handle
2829  * @param[in]  tethering  The handle of tethering
2830  * @param[out]  ssid  The SSID
2831  * @return 0 on success, otherwise negative error value.
2832  * @retval  #TETHERING_ERROR_NONE  Successful
2833  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2834  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
2835  */
2836 API int tethering_wifi_set_ssid(tethering_h tethering, const char *ssid)
2837 {
2838         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2839
2840         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2841                         "parameter(tethering) is NULL\n");
2842         _retvm_if(ssid == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2843                         "parameter(ssid) is NULL\n");
2844
2845         __tethering_h *th = (__tethering_h *)tethering;
2846         char *p_ssid = NULL;
2847         int ssid_len = 0;
2848
2849         ssid_len = strlen(ssid);
2850         if (ssid_len > TETHERING_WIFI_SSID_MAX_LEN) {
2851                 ERR("parameter(ssid) is too long");
2852                 return TETHERING_ERROR_INVALID_PARAMETER;
2853         }
2854
2855         p_ssid = strdup(ssid);
2856         _retvm_if(p_ssid == NULL, TETHERING_ERROR_OUT_OF_MEMORY,
2857                         "strdup is failed\n");
2858
2859         if (th->ssid)
2860                 free(th->ssid);
2861         th->ssid = p_ssid;
2862
2863         return TETHERING_ERROR_NONE;
2864 }
2865
2866 /**
2867  * @internal
2868  * @brief Gets the SSID (service set identifier).
2869  * @since_tizen 2.3
2870  * @privlevel platform
2871  * @privilege http://tizen.org/privilege/tethering.admin
2872  * @remarks @a ssid must be released with free() by you.
2873  * @param[in]  tethering  The handle of tethering
2874  * @param[out]  ssid  The SSID
2875  * @return 0 on success, otherwise negative error value.
2876  * @retval  #TETHERING_ERROR_NONE  Successful
2877  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2878  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
2879  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2880  */
2881 API int tethering_wifi_get_ssid(tethering_h tethering, char **ssid)
2882 {
2883         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2884
2885         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2886                         "parameter(tethering) is NULL\n");
2887         _retvm_if(ssid == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2888                         "parameter(ssid) is NULL\n");
2889         DBG("+\n");
2890
2891         __tethering_h *th = (__tethering_h *)tethering;
2892         char val[TETHERING_WIFI_SSID_MAX_LEN + 1] = {0, };
2893
2894         if (!tethering_is_enabled(NULL, TETHERING_TYPE_WIFI)) {
2895                 if (th->ssid != NULL) {
2896                         DBG("Private SSID is set\n");
2897                         *ssid = strdup(th->ssid);
2898                 } else {
2899                         if (__get_ssid_from_vconf(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
2900                                                 val, sizeof(val)) == false) {
2901                                 return TETHERING_ERROR_OPERATION_FAILED;
2902                         }
2903                         *ssid = strdup(val);
2904                 }
2905         } else {
2906                 if (__get_ssid_from_vconf(VCONFKEY_MOBILE_HOTSPOT_SSID,
2907                                         val, sizeof(val)) == false) {
2908                         return TETHERING_ERROR_OPERATION_FAILED;
2909                 }
2910                 *ssid = strdup(val);
2911         }
2912
2913         if (*ssid == NULL) {
2914                 ERR("strdup is failed\n");
2915                 return TETHERING_ERROR_OUT_OF_MEMORY;
2916         }
2917
2918         return TETHERING_ERROR_NONE;
2919 }
2920
2921 /**
2922  * @internal
2923  * @brief Sets the visibility of SSID(service set identifier).
2924  * @since_tizen 2.3
2925  * @privlevel platform
2926  * @privilege http://tizen.org/privilege/tethering.admin
2927  * @details If you set the visibility invisible, then the SSID of this device is hidden. So, Wi-Fi scan can't find your device.
2928  * @remarks This change is applied next time Wi-Fi tethering is enabled
2929  * @param[in]  tethering  The handle of tethering
2930  * @param[in]  visible  The visibility of SSID: (@c true = visible, @c false = invisible)
2931  * @return 0 on success, otherwise negative error value.
2932  * @retval  #TETHERING_ERROR_NONE  Successful
2933  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2934  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2935  * @see  tethering_wifi_get_ssid_visibility()
2936  */
2937 API int tethering_wifi_set_ssid_visibility(tethering_h tethering, bool visible)
2938 {
2939         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2940
2941         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2942                         "parameter(tethering) is NULL\n");
2943
2944         __tethering_h *th = (__tethering_h *)tethering;
2945         tethering_error_e ret = TETHERING_ERROR_NONE;
2946
2947         ret = __set_visible(visible);
2948         if (ret == TETHERING_ERROR_NONE) {
2949
2950                 __send_dbus_signal(th->client_bus,
2951                                 SIGNAL_NAME_SSID_VISIBILITY_CHANGED,
2952                                 visible ? SIGNAL_MSG_SSID_VISIBLE :
2953                                 SIGNAL_MSG_SSID_HIDE);
2954         }
2955         return ret;
2956 }
2957
2958 /**
2959  * @internal
2960  * @brief Gets the visibility of SSID(service set identifier).
2961  * @since_tizen 2.3
2962  * @privlevel platform
2963  * @privilege http://tizen.org/privilege/tethering.admin
2964  * @details If the visibility is set invisible, then the SSID of this device is hidden. So, Wi-Fi scan can't find your device.
2965  * @param[in]  tethering  The handle of tethering
2966  * @param[out]  visible  The visibility of SSID: (@c true = visible, @c false = invisible)
2967  * @return 0 on success, otherwise negative error value.
2968  * @retval  #TETHERING_ERROR_NONE  Successful
2969  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2970  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2971  * @see  tethering_wifi_set_ssid_visibility()
2972  */
2973 API int tethering_wifi_get_ssid_visibility(tethering_h tethering, bool *visible)
2974 {
2975         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
2976
2977         _retvm_if(visible == NULL, TETHERING_ERROR_INVALID_PARAMETER,
2978                         "parameter(visible) is NULL\n");
2979
2980         return __get_visible(visible);
2981 }
2982
2983 /**
2984  * @internal
2985  * @brief Sets the passphrase.
2986  * @since_tizen 2.3
2987  * @privlevel platform
2988  * @privilege http://tizen.org/privilege/tethering.admin
2989  * @remarks This change is applied next time Wi-Fi tethering is enabled
2990  * @param[in]  tethering  The handle of tethering
2991  * @param[in]  passphrase  The passphrase
2992  * @return 0 on success, otherwise negative error value.
2993  * @retval  #TETHERING_ERROR_NONE  Successful
2994  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
2995  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
2996  * @see  tethering_wifi_get_passphrase()
2997  */
2998 API int tethering_wifi_set_passphrase(tethering_h tethering, const char *passphrase)
2999 {
3000         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3001
3002         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3003                         "parameter(tethering) is NULL\n");
3004         _retvm_if(passphrase == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3005                         "parameter(passphrase) is NULL\n");
3006
3007         __tethering_h *th = (__tethering_h *)tethering;
3008         GDBusProxy *proxy = th->client_bus_proxy;
3009         GVariant *parameters;
3010         GError *error = NULL;
3011         int passphrase_len = 0;
3012         int ret = 0;
3013
3014         DBG("+");
3015         passphrase_len = strlen(passphrase);
3016         if (passphrase_len < TETHERING_WIFI_KEY_MIN_LEN ||
3017                         passphrase_len > TETHERING_WIFI_KEY_MAX_LEN) {
3018                 ERR("parameter(passphrase) is too short or long\n");
3019                 return TETHERING_ERROR_INVALID_PARAMETER;
3020         }
3021
3022         parameters = g_dbus_proxy_call_sync(proxy, "set_wifi_tethering_passphrase",
3023                         g_variant_new("(s)", passphrase), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3024
3025         if (error) {
3026                 ERR("g_dbus_proxy_call_sync failed because  %s\n", error->message);
3027
3028                 if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
3029                         ret = TETHERING_ERROR_PERMISSION_DENIED;
3030                 else
3031                         ret = TETHERING_ERROR_OPERATION_FAILED;
3032
3033                 g_error_free(error);
3034                 return ret;
3035         }
3036
3037         g_variant_get(parameters, "(u)", &ret);
3038         g_variant_unref(parameters);
3039
3040         if (ret == TETHERING_ERROR_NONE) {
3041                 __send_dbus_signal(th->client_bus,
3042                                 SIGNAL_NAME_PASSPHRASE_CHANGED, NULL);
3043         }
3044
3045         DBG("-");
3046         return ret;
3047 }
3048
3049 /**
3050  * @internal
3051  * @brief Gets the passphrase.
3052  * @since_tizen 2.3
3053  * @privlevel platform
3054  * @privilege http://tizen.org/privilege/tethering.admin
3055  * @remarks @a passphrase must be released with free() by you.
3056  * @param[in]  tethering  The handle of tethering
3057  * @param[out]  passphrase  The passphrase
3058  * @return 0 on success, otherwise negative error value.
3059  * @retval  #TETHERING_ERROR_NONE  Successful
3060  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3061  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
3062  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
3063  * @see  tethering_wifi_set_passphrase()
3064  */
3065 API int tethering_wifi_get_passphrase(tethering_h tethering, char **passphrase)
3066 {
3067         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3068
3069         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3070                         "parameter(tethering) is NULL\n");
3071         _retvm_if(passphrase == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3072                         "parameter(passphrase) is NULL\n");
3073
3074         __tethering_h *th = (__tethering_h *)tethering;
3075         GDBusProxy *proxy = th->client_bus_proxy;
3076         GVariant *parameters;
3077         GError *error = NULL;
3078         unsigned int len = 0;
3079         tethering_error_e ret = TETHERING_ERROR_NONE;
3080
3081         parameters = g_dbus_proxy_call_sync(proxy, "get_wifi_tethering_passphrase",
3082                         NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3083
3084         if (error) {
3085                 ERR("g_dbus_proxy_call_sync failed because  %s\n", error->message);
3086
3087                 if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
3088                         ret = TETHERING_ERROR_PERMISSION_DENIED;
3089                 else
3090                         ret = TETHERING_ERROR_OPERATION_FAILED;
3091
3092                 g_error_free(error);
3093                 return ret;
3094         }
3095
3096         if (parameters != NULL) {
3097                 g_variant_get(parameters, "(siu)", passphrase, &len, &ret);
3098                 g_variant_unref(parameters);
3099         }
3100
3101         return TETHERING_ERROR_NONE;
3102 }
3103
3104 API int tethering_wifi_set_channel(tethering_h tethering, int channel)
3105 {
3106         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3107         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3108                         "parameter(tethering) is NULL\n");
3109
3110         __tethering_h *th = (__tethering_h *)tethering;
3111         th->channel = channel;
3112
3113         return TETHERING_ERROR_NONE;
3114 }
3115
3116 API int tethering_wifi_get_channel(tethering_h tethering, int *channel)
3117 {
3118         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3119         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3120                         "parameter(tethering) is NULL\n");
3121
3122         _retvm_if(channel == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3123                         "parameter(channel) is NULL\n");
3124
3125         __tethering_h *th = (__tethering_h *)tethering;
3126         *channel = th->channel;
3127
3128         return TETHERING_ERROR_NONE;
3129 }
3130
3131 API int tethering_wifi_set_mode(tethering_h tethering, tethering_wifi_mode_type_e type)
3132 {
3133         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3134         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3135                         "parameter(tethering) is NULL\n");
3136
3137         __tethering_h *th = (__tethering_h *)tethering;
3138
3139         th->mode_type = type;
3140
3141         return TETHERING_ERROR_NONE;
3142 }
3143
3144 API int tethering_wifi_get_mode(tethering_h tethering, tethering_wifi_mode_type_e *type)
3145 {
3146         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3147         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3148                         "parameter(tethering) is NULL\n");
3149         _retvm_if(type == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3150                         "parameter(type) is NULL\n");
3151
3152         __tethering_h *th = (__tethering_h *)tethering;
3153         *type = th->mode_type;
3154
3155         return TETHERING_ERROR_NONE;
3156 }
3157
3158
3159 /**
3160  * @internal
3161  * @brief Reload the settings (SSID / Passphrase / Security type / SSID visibility).
3162  * @since_tizen 2.3
3163  * @privlevel platform
3164  * @privilege http://tizen.org/privilege/tethering.admin
3165  * @remarks Connected devices via Wi-Fi tethering or MobileAP will be disconnected when the settings are reloaded
3166  * @param[in]  tethering  The handle of tethering
3167  * @param[in]  callback  The callback function to invoke
3168  * @param[in]  user_data  The user data to be passed to the callback function
3169  * @return 0 on success, otherwise negative error value.
3170  * @retval  #TETHERING_ERROR_NONE  Successful
3171  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3172  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
3173  */
3174 API int tethering_wifi_reload_settings(tethering_h tethering, tethering_wifi_settings_reloaded_cb callback, void *user_data)
3175
3176 {
3177         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3178
3179         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3180                         "parameter(tethering) is NULL\n");
3181         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3182                         "parameter(callback) is NULL\n");
3183
3184         __tethering_h *th = (__tethering_h *)tethering;
3185         _softap_settings_t set = {"", "", "", 0, false};
3186         GDBusProxy *proxy = th->client_bus_proxy;
3187         int ret = 0;
3188
3189         DBG("+\n");
3190
3191         if (th->settings_reloaded_cb) {
3192                 ERR("Operation in progress\n");
3193                 return TETHERING_ERROR_OPERATION_FAILED;
3194         }
3195
3196         ret = __prepare_wifi_settings(tethering, &set);
3197         if (ret != TETHERING_ERROR_NONE) {
3198                 ERR("softap settings initialization failed\n");
3199                 return TETHERING_ERROR_OPERATION_FAILED;
3200         }
3201
3202         th->settings_reloaded_cb = callback;
3203         th->settings_reloaded_user_data = user_data;
3204
3205         g_dbus_proxy_call(proxy, "reload_wifi_settings",
3206                         g_variant_new("(sssiiii)", set.ssid, set.key, set.mode, set.channel, set.visibility, set.mac_filter, set.sec_type),
3207                         G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
3208                         (GAsyncReadyCallback) __settings_reloaded_cb, (gpointer)tethering);
3209
3210         return TETHERING_ERROR_NONE;
3211 }
3212
3213 API int tethering_wifi_set_mac_filter(tethering_h tethering, bool mac_filter)
3214 {
3215         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3216
3217         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3218                         "parameter(tethering) is NULL\n");
3219
3220         __tethering_h *th = (__tethering_h *)tethering;
3221         th->mac_filter = mac_filter;
3222
3223         return TETHERING_ERROR_NONE;
3224 }
3225
3226 API int tethering_wifi_get_mac_filter(tethering_h tethering, bool *mac_filter)
3227 {
3228         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3229
3230         _retvm_if(mac_filter == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3231                         "parameter(mac_filter) is NULL\n");
3232
3233         __tethering_h *th = (__tethering_h *)tethering;
3234         *mac_filter = th->mac_filter;
3235
3236         return TETHERING_ERROR_NONE;
3237 }
3238
3239 static int __add_mac_to_file(const char *filepath, const char *mac)
3240 {
3241         FILE *fp = NULL;
3242         char line[MAX_BUF_SIZE] = "\0";
3243         bool mac_exist = false;
3244
3245         fp = fopen(filepath, "a+");
3246         if (!fp) {
3247                 ERR("fopen is failed\n");
3248                 return TETHERING_ERROR_OPERATION_FAILED;
3249         }
3250
3251         while (fgets(line, MAX_BUF_SIZE, fp) != NULL) {
3252                 if (strncmp(mac, line, 17) == 0) {
3253                         DBG("MAC %s already exist in the list\n", mac);
3254                         mac_exist = true;
3255                         break;
3256                 }
3257         }
3258
3259         if (!mac_exist)
3260                 fprintf(fp, "%s\n", mac);
3261
3262         fclose(fp);
3263
3264         return TETHERING_ERROR_NONE;
3265 }
3266
3267 static int __remove_mac_from_file(const char *filepath, const char *mac)
3268 {
3269         FILE *fp = NULL;
3270         FILE *fp1 = NULL;
3271         char line[MAX_BUF_SIZE] = "\0";
3272
3273         fp = fopen(filepath, "r");
3274         if (!fp) {
3275                 ERR("fopen is failed\n");
3276                 return TETHERING_ERROR_OPERATION_FAILED;
3277         }
3278
3279         fp1 = fopen(TEMP_LIST, "w+");
3280         if (!fp1) {
3281                 fclose(fp);
3282                 ERR("fopen is failed\n");
3283                 return TETHERING_ERROR_OPERATION_FAILED;
3284         }
3285
3286         while (fgets(line, MAX_BUF_SIZE, fp) != NULL) {
3287                 if (strncmp(mac, line, 17) == 0)
3288                         DBG("MAC %s found in the list\n", mac);
3289                 else
3290                         fprintf(fp1, "%s", line);
3291         }
3292
3293         fclose(fp);
3294         fclose(fp1);
3295
3296         if ((strcmp(filepath, ALLOWED_LIST) == 0))
3297                 rename(TEMP_LIST, ALLOWED_LIST);
3298         else if ((strcmp(filepath, BLOCKED_LIST) == 0))
3299                 rename(TEMP_LIST, BLOCKED_LIST);
3300
3301         return TETHERING_ERROR_NONE;
3302 }
3303
3304 API int tethering_wifi_add_allowed_mac_list(tethering_h tethering, const char *mac)
3305 {
3306         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3307
3308         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3309                         "parameter(tethering) is NULL\n");
3310         _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3311                         "parameter(mac) is NULL\n");
3312
3313         return __add_mac_to_file(ALLOWED_LIST, mac);
3314 }
3315
3316 API int tethering_wifi_remove_allowed_mac_list(tethering_h tethering, const char *mac)
3317 {
3318         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3319                         "parameter(tethering) is NULL\n");
3320         _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3321                         "parameter(mac) is NULL\n");
3322
3323         return __remove_mac_from_file(ALLOWED_LIST, mac);
3324 }
3325
3326 API int tethering_wifi_add_blocked_mac_list(tethering_h tethering, const char *mac)
3327 {
3328         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3329                         "parameter(tethering) is NULL\n");
3330         _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3331                         "parameter(mac) is NULL\n");
3332
3333         return __add_mac_to_file(BLOCKED_LIST, mac);
3334 }
3335
3336 API int tethering_wifi_remove_blocked_mac_list(tethering_h tethering, const char *mac)
3337 {
3338         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3339                         "parameter(tethering) is NULL\n");
3340         _retvm_if(mac == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3341                         "parameter(mac) is NULL\n");
3342
3343         return __remove_mac_from_file(BLOCKED_LIST, mac);
3344 }
3345
3346 API int tethering_wifi_enable_dhcp(tethering_h tethering, bool enable)
3347 {
3348         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3349
3350         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3351                         "parameter(tethering) is NULL\n");
3352
3353         GVariant *parameters;
3354         GError *error = NULL;
3355         guint result;
3356
3357         __tethering_h *th = (__tethering_h *)tethering;
3358
3359         GDBusProxy *proxy = th->client_bus_proxy;
3360
3361         parameters = g_dbus_proxy_call_sync (proxy, "enable_dhcp",
3362                         g_variant_new("(b)", enable),
3363                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3364
3365         if (error) {
3366                 ERR("g_dbus_proxy_call_sync failed because  %s\n", error->message);
3367                 if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
3368                         result = TETHERING_ERROR_PERMISSION_DENIED;
3369                 else
3370                         result = TETHERING_ERROR_OPERATION_FAILED;
3371
3372                 g_error_free(error);
3373                 th->dhcp_enabled = false;
3374
3375                 return result;
3376         }
3377
3378         g_variant_get (parameters, "(u)", &result);
3379         g_variant_unref(parameters);
3380
3381         if (enable) {
3382                 th->dhcp_enabled = true;
3383         } else {
3384                 th->dhcp_enabled = false;
3385         }
3386
3387         return TETHERING_ERROR_NONE;
3388 }
3389
3390 API int tethering_wifi_set_dhcp_range(tethering_h tethering, char *rangestart, char *rangestop)
3391 {
3392         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3393
3394         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3395                         "parameter(tethering) is NULL\n");
3396         _retvm_if(rangestart == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3397                         "parameter(rangestart) is NULL\n");
3398         _retvm_if(rangestop == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3399                         "parameter(rangestop) is NULL\n");
3400
3401         GVariant *parameters;
3402         GError *error = NULL;
3403         guint result;
3404
3405         __tethering_h *th = (__tethering_h *)tethering;
3406
3407         GDBusProxy *proxy = th->client_bus_proxy;
3408
3409         parameters = g_dbus_proxy_call_sync (proxy, "dhcp_range",
3410                         g_variant_new("(ss)", rangestart, rangestop),
3411                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3412         if (error) {
3413                 ERR("g_dbus_proxy_call_sync failed because  %s\n", error->message);
3414
3415                 if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
3416                         result = TETHERING_ERROR_PERMISSION_DENIED;
3417                 else
3418                         result = TETHERING_ERROR_OPERATION_FAILED;
3419
3420                 g_error_free(error);
3421                 th->dhcp_enabled = false;
3422
3423                 return result;
3424         }
3425
3426         g_variant_get (parameters, "(u)", &result);
3427         g_variant_unref(parameters);
3428
3429         th->dhcp_enabled = true;
3430
3431         return TETHERING_ERROR_NONE;
3432 }
3433
3434 API int tethering_wifi_is_dhcp_enabled(tethering_h tethering, bool *dhcp_enabled)
3435 {
3436         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3437         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3438                         "parameter(tethering) is NULL\n");
3439         _retvm_if(dhcp_enabled == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3440                         "parameter(dhcp_enabled) is NULL\n");
3441
3442         __tethering_h *th = (__tethering_h *)tethering;
3443         *dhcp_enabled = th->dhcp_enabled;
3444
3445         return TETHERING_ERROR_NONE;
3446 }
3447
3448 /**
3449  * @internal
3450  * @brief Sets the security type of Wi-Fi AP.
3451  * @since_tizen 2.3
3452  * @privlevel platform
3453  * @privilege http://tizen.org/privilege/tethering.admin
3454  * @details If security type is not set, WPA2_PSK is used
3455  * @param[in]  tethering  The handle of tethering
3456  * @param[in]  type  The security type
3457  * @return 0 on success, otherwise negative error value.
3458  * @retval  #TETHERING_ERROR_NONE  Successful
3459  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3460  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
3461  * @see  tethering_wifi_ap_get_security_type()
3462  */
3463 API int tethering_wifi_ap_set_security_type(tethering_h tethering, tethering_wifi_security_type_e type)
3464 {
3465         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3466
3467         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3468                 "parameter(tethering) is NULL\n");
3469
3470         __tethering_h *th = (__tethering_h *)tethering;
3471         th->sec_type = type;
3472         return TETHERING_ERROR_NONE;
3473 }
3474
3475 /**
3476  * @internal
3477  * @brief Gets the security type of Wi-Fi AP.
3478  * @since_tizen 2.3
3479  * @privlevel platform
3480  * @privilege http://tizen.org/privilege/tethering.admin
3481  * @details If security type is not set, WPA2_PSK is used
3482  * @param[in]  tethering  The handle of tethering
3483  * @param[out]  type  The security type
3484  * @return 0 on success, otherwise negative error value.
3485  * @retval  #TETHERING_ERROR_NONE  Successful
3486  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3487  * @see  tethering_wifi_ap_set_security_type()
3488  */
3489 API int tethering_wifi_ap_get_security_type(tethering_h tethering, tethering_wifi_security_type_e *type)
3490 {
3491         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3492
3493         _retvm_if(type == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3494                 "parameter(type) is NULL\n");
3495
3496         __tethering_h *th = (__tethering_h *)tethering;
3497
3498         *type = th->sec_type;
3499         return TETHERING_ERROR_NONE;
3500 }
3501
3502 /**
3503  * @internal
3504  * @brief Sets the SSID (service set identifier) for Wi-Fi AP. The SSID cannot exceed 32 bytes.
3505  * @since_tizen 2.3
3506  * @privlevel platform
3507  * @privilege http://tizen.org/privilege/tethering.admin
3508  * @details If SSID is not set, Device name is used as SSID
3509  * @param[in]  tethering  The handle of tethering
3510  * @param[in]  ssid  The SSID
3511  * @return 0 on success, otherwise negative error value.
3512  * @retval  #TETHERING_ERROR_NONE  Successful
3513  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3514  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
3515  */
3516 API int tethering_wifi_ap_set_ssid(tethering_h tethering, const char *ssid)
3517 {
3518         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3519
3520         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3521                         "parameter(tethering) is NULL\n");
3522         _retvm_if(ssid == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3523                         "parameter(ssid) is NULL\n");
3524
3525         __tethering_h *th = (__tethering_h *)tethering;
3526         char *p_ssid = NULL;
3527         int ssid_len = 0;
3528
3529         ssid_len = strlen(ssid);
3530         if (ssid_len > TETHERING_WIFI_SSID_MAX_LEN) {
3531                 ERR("parameter(ssid) is too long");
3532                 return TETHERING_ERROR_INVALID_PARAMETER;
3533         }
3534
3535         p_ssid = strdup(ssid);
3536         if (p_ssid == NULL) {
3537                 ERR("strdup failed\n");
3538                 return TETHERING_ERROR_OUT_OF_MEMORY;
3539         }
3540
3541         if (th->ap_ssid)
3542                 g_free(th->ap_ssid);
3543         th->ap_ssid = p_ssid;
3544
3545         return TETHERING_ERROR_NONE;
3546 }
3547
3548 /**
3549  * @internal
3550  * @brief Gets the SSID (service set identifier) for Wi-Fi AP.
3551  * @since_tizen 2.3
3552  * @privlevel platform
3553  * @privilege http://tizen.org/privilege/tethering.admin
3554  * @details If SSID is not set, Device name is used as SSID
3555  * @remarks @a ssid must be released with free() by you.
3556  * @param[in]  tethering  The handle of tethering
3557  * @param[out]  ssid  The SSID
3558  * @return 0 on success, otherwise negative error value.
3559  * @retval  #TETHERING_ERROR_NONE  Successful
3560  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3561  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
3562  */
3563 API int tethering_wifi_ap_get_ssid(tethering_h tethering, char **ssid)
3564 {
3565         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3566
3567         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3568                         "parameter(tethering) is NULL\n");
3569         _retvm_if(ssid == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3570                         "parameter(ssid) is NULL\n");
3571
3572         __tethering_h *th = (__tethering_h *)tethering;
3573
3574         *ssid = g_strdup(th->ap_ssid);
3575         if (*ssid == NULL) {
3576                 ERR("strdup failed\n");
3577                 return TETHERING_ERROR_OUT_OF_MEMORY;
3578         }
3579
3580         return TETHERING_ERROR_NONE;
3581 }
3582
3583 /**
3584  * @internal
3585  * @brief Sets the visibility of SSID(service set identifier) for Wi-Fi AP.
3586  * @since_tizen 2.3
3587  * @privlevel platform
3588  * @privilege http://tizen.org/privilege/tethering.admin
3589  * @details If you set the visibility invisible, then the SSID of this device is hidden. So, Wi-Fi scan can't find your device.
3590  * @details by default visibility is set to true.
3591  * @remarks This change is applied next time Wi-Fi tethering is enabled
3592  * @param[in]  tethering  The handle of tethering
3593  * @param[in]  visible  The visibility of SSID: (@c true = visible, @c false = invisible)
3594  * @return 0 on success, otherwise negative error value.
3595  * @retval  #TETHERING_ERROR_NONE  Successful
3596  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3597  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
3598  * @see  tethering_wifi_ap_get_ssid_visibility()
3599  */
3600 API int tethering_wifi_ap_set_ssid_visibility(tethering_h tethering, bool visible)
3601 {
3602         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3603
3604         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3605                 "parameter(tethering) is NULL\n");
3606
3607         __tethering_h *th = (__tethering_h *)tethering;
3608         th->visibility = visible;
3609         return TETHERING_ERROR_NONE;
3610 }
3611
3612 /**
3613  * @internal
3614  * @brief Gets the visibility of SSID(service set identifier) for Wi-Fi AP.
3615  * @since_tizen 2.3
3616  * @privlevel platform
3617  * @privilege http://tizen.org/privilege/tethering.admin
3618  * @details If the visibility is set invisible, then the SSID of this device is hidden. So, Wi-Fi scan can't find your device.
3619  * @details by default visibility is set to true.
3620  * @param[in]  tethering  The handle of tethering
3621  * @param[out]  visible  The visibility of SSID: (@c true = visible, @c false = invisible)
3622  * @return 0 on success, otherwise negative error value.
3623  * @retval  #TETHERING_ERROR_NONE  Successful
3624  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3625  * @see  tethering_wifi_ap_set_ssid_visibility()
3626  */
3627 API int tethering_wifi_ap_get_ssid_visibility(tethering_h tethering, bool *visible)
3628 {
3629         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3630
3631         _retvm_if(visible == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3632                         "parameter(visible) is NULL\n");
3633
3634         __tethering_h *th = (__tethering_h *)tethering;
3635
3636         *visible = th->visibility;
3637         return TETHERING_ERROR_NONE;
3638 }
3639
3640 /**
3641  * @internal
3642  * @brief Sets the passphrase for Wi-Fi AP.
3643  * @since_tizen 2.3
3644  * @privlevel platform
3645  * @privilege http://tizen.org/privilege/tethering.admin
3646  * @details If the passphrase is not set, random string of 8 alphabets will be used.
3647  * @param[in]  tethering  The handle of tethering
3648  * @param[in]  passphrase  The passphrase
3649  * @return 0 on success, otherwise negative error value.
3650  * @retval  #TETHERING_ERROR_NONE  Successful
3651  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3652  * @see  tethering_wifi_ap_get_passphrase()
3653  */
3654 API int tethering_wifi_ap_set_passphrase(tethering_h tethering, const char *passphrase)
3655 {
3656         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3657
3658         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3659                 "parameter(tethering) is NULL\n");
3660         _retvm_if(passphrase == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3661                 "parameter(passphrase) is NULL\n");
3662
3663         __tethering_h *th = (__tethering_h *)tethering;
3664         int passphrase_len = 0;
3665
3666         passphrase_len = strlen(passphrase);
3667
3668         if (passphrase_len < TETHERING_WIFI_KEY_MIN_LEN ||
3669                         passphrase_len > TETHERING_WIFI_KEY_MAX_LEN) {
3670                 ERR("parameter(passphrase) is too short or long\n");
3671                 return TETHERING_ERROR_INVALID_PARAMETER;
3672         }
3673
3674         if (!g_strcmp0(passphrase, th->passphrase))
3675                 return TETHERING_ERROR_NONE;
3676
3677         g_strlcpy(th->passphrase, passphrase, sizeof(th->passphrase));
3678         return TETHERING_ERROR_NONE;
3679 }
3680
3681 /**
3682  * @internal
3683  * @brief Gets the passphrase for Wi-Fi AP.
3684  * @since_tizen 2.3
3685  * @privlevel platform
3686  * @privilege http://tizen.org/privilege/tethering.admin
3687  * @details If the passphrase is not set, random string of 8 alphabets will be used.
3688  * @remarks @a passphrase must be released with free() by you.
3689  * @param[in]  tethering  The handle of tethering
3690  * @param[out]  passphrase  The passphrase
3691  * @return 0 on success, otherwise negative error value.
3692  * @retval  #TETHERING_ERROR_NONE  Successful
3693  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3694  * @retval  #TETHERING_ERROR_OUT_OF_MEMORY  Out of memory
3695  * @see  tethering_wifi_ap_set_passphrase()
3696  */
3697 API int tethering_wifi_ap_get_passphrase(tethering_h tethering, char **passphrase)
3698 {
3699         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3700
3701         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3702                         "parameter(tethering) is NULL\n");
3703         _retvm_if(passphrase == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3704                         "parameter(passphrase) is NULL\n");
3705
3706         __tethering_h *th = (__tethering_h *)tethering;
3707
3708         *passphrase = g_strdup(th->passphrase);
3709         if (*passphrase == NULL) {
3710                 ERR("strdup is failed\n");
3711                 return TETHERING_ERROR_OUT_OF_MEMORY;
3712         }
3713
3714         return TETHERING_ERROR_NONE;
3715 }
3716
3717 /**
3718  * @internal
3719  * @brief Reload the settings (SSID / Passphrase / Security type / SSID visibility) for Wi-Fi AP.
3720  * @since_tizen 2.3
3721  * @privlevel platform
3722  * @privilege http://tizen.org/privilege/tethering.admin
3723  * @remarks Connected devices via MobileAP will be disconnected when the settings are reloaded
3724  * @param[in]  tethering  The handle of tethering
3725  * @param[in]  callback  The callback function to invoke
3726  * @param[in]  user_data  The user data to be passed to the callback function
3727  * @return 0 on success, otherwise negative error value.
3728  * @retval  #TETHERING_ERROR_NONE  Successful
3729  * @retval  #TETHERING_ERROR_INVALID_PARAMETER  Invalid parameter
3730  * @retval  #TETHERING_ERROR_OPERATION_FAILED  Operation failed
3731  */
3732 API int tethering_wifi_ap_reload_settings(tethering_h tethering, tethering_wifi_ap_settings_reloaded_cb callback, void *user_data)
3733
3734 {
3735         CHECK_FEATURE_SUPPORTED(TETHERING_FEATURE, TETHERING_WIFI_FEATURE);
3736
3737         _retvm_if(tethering == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3738                         "parameter(tethering) is NULL\n");
3739         _retvm_if(callback == NULL, TETHERING_ERROR_INVALID_PARAMETER,
3740                         "parameter(callback) is NULL\n");
3741
3742         __tethering_h *th = (__tethering_h *)tethering;
3743         _softap_settings_t set = {"", "", "", 0, false};
3744         GDBusProxy *proxy = th->client_bus_proxy;
3745         int ret = 0;
3746
3747         DBG("+\n");
3748
3749         if (th->ap_settings_reloaded_cb) {
3750                 ERR("Operation in progress\n");
3751                 return TETHERING_ERROR_OPERATION_FAILED;
3752         }
3753
3754         ret = __prepare_wifi_ap_settings(tethering, &set);
3755         if (ret != TETHERING_ERROR_NONE) {
3756                 ERR("softap settings initialization failed\n");
3757                 return TETHERING_ERROR_OPERATION_FAILED;
3758         }
3759
3760         th->ap_settings_reloaded_cb = callback;
3761         th->ap_settings_reloaded_user_data = user_data;
3762
3763         g_dbus_proxy_call(proxy, "reload_wifi_ap_settings",
3764                         g_variant_new("(ssii)", set.ssid, set.key, set.visibility, set.sec_type),
3765                         G_DBUS_CALL_FLAGS_NONE, -1, th->cancellable,
3766                         (GAsyncReadyCallback) __ap_settings_reloaded_cb, (gpointer)tethering);
3767
3768         return TETHERING_ERROR_NONE;
3769 }