tizen 2.3 release
[framework/system/deviced.git] / src / usb / usb-client-control.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <vconf.h>
20 #include <stdbool.h>
21 #include "usb-client.h"
22 #include "core/device-notifier.h"
23
24 #define VCONFKEY_USB_CONTROL "db/private/usb/usb_control"
25
26 static int usb_control = DEVICE_OPS_STATUS_START;
27
28 int control_start(enum device_flags flags)
29 {
30         int state;
31
32         if (usb_control == DEVICE_OPS_STATUS_START)
33                 return 0;
34
35         usb_control = DEVICE_OPS_STATUS_START;
36         if (vconf_set_int(VCONFKEY_USB_CONTROL, usb_control) != 0)
37                 _E("Failed to set vconf");
38
39         state = get_current_usb_physical_state();
40         usb_state_changed(state);
41
42         if (state > 0)
43                 act_usb_connected();
44
45         return 0;
46 }
47
48 int control_stop(enum device_flags flags)
49 {
50         int cur_mode;
51         if (usb_control == DEVICE_OPS_STATUS_STOP)
52                 return 0;
53
54         usb_control = DEVICE_OPS_STATUS_STOP;
55         if (vconf_set_int(VCONFKEY_USB_CONTROL, usb_control) != 0)
56                 _E("Failed to set vconf");
57
58         cur_mode = get_current_usb_mode();
59         if (cur_mode <= SET_USB_NONE) {
60                 _E("Current usb mode is already none");
61                 return 0;
62         }
63
64         unset_client_mode(cur_mode, false);
65
66         launch_syspopup(USB_RESTRICT);
67
68         return 0;
69 }
70
71 int control_status(void)
72 {
73         return usb_control;
74 }
75
76 static void check_prev_control_status(void)
77 {
78         if (vconf_get_int(VCONFKEY_USB_CONTROL, &usb_control) != 0)
79                 usb_control = DEVICE_OPS_STATUS_START;
80 }
81
82 static int usb_client_booting_done(void *data)
83 {
84         int state;
85
86         unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, usb_client_booting_done);
87         check_prev_control_status();
88
89         usbclient_init_booting_done();
90
91         state = get_current_usb_physical_state();
92         usb_state_changed(state);
93
94         if (state > 0)
95                 act_usb_connected();
96
97         return 0;
98 }
99
100 void wait_until_booting_done(void)
101 {
102         register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, usb_client_booting_done);
103         usb_control = DEVICE_OPS_STATUS_STOP;
104 }