Initialize Tizen 2.3
[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(void)
29 {
30         if (usb_control == DEVICE_OPS_STATUS_START)
31                 return 0;
32
33         usb_control = DEVICE_OPS_STATUS_START;
34         if (vconf_set_int(VCONFKEY_USB_CONTROL, usb_control) != 0)
35                 _E("Failed to set vconf");
36
37         if (check_current_usb_state() > 0)
38                 act_usb_connected();
39
40         return 0;
41 }
42
43 int control_stop(void)
44 {
45         int cur_mode;
46         if (usb_control == DEVICE_OPS_STATUS_STOP)
47                 return 0;
48
49         usb_control = DEVICE_OPS_STATUS_STOP;
50         if (vconf_set_int(VCONFKEY_USB_CONTROL, usb_control) != 0)
51                 _E("Failed to set vconf");
52
53         cur_mode = get_current_usb_mode();
54         if (cur_mode <= SET_USB_NONE) {
55                 _E("Current usb mode is already none");
56                 return 0;
57         }
58
59         unset_client_mode(cur_mode, false);
60
61         launch_syspopup(USB_RESTRICT);
62
63         return 0;
64 }
65
66 int control_status(void)
67 {
68         return usb_control;
69 }
70
71 static void check_prev_control_status(void)
72 {
73         if (vconf_get_int(VCONFKEY_USB_CONTROL, &usb_control) != 0)
74                 usb_control = DEVICE_OPS_STATUS_START;
75 }
76
77 static int usb_client_booting_done(void *data)
78 {
79         unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, usb_client_booting_done);
80         check_prev_control_status();
81
82         usbclient_init_booting_done();
83
84         if (check_current_usb_state() > 0)
85                 act_usb_connected();
86
87         return 0;
88 }
89
90 void wait_until_booting_done(void)
91 {
92         register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, usb_client_booting_done);
93         usb_control = DEVICE_OPS_STATUS_STOP;
94 }