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