a99e05dc58e554bc1588a737c49a4a5344c2c337
[platform/core/connectivity/data-router.git] / src / dr-noti-handler.c
1 /*
2  * Data-Router
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Injun Yang <injun.yang@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *              http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24 #include <glib.h>
25 #include <sys/wait.h>
26
27 #include "dr-main.h"
28 #include "dr-usb.h"
29 #include "dr-modem.h"
30 #include "dr-common.h"
31
32
33
34 #define ACM_WAIT_TIME 300000
35
36 extern dr_info_t dr_info;
37 volatile gboolean dsr_status = FALSE;
38
39 static gboolean acm_enabled(int mode)
40 {
41         switch (mode) {
42         case SET_USB_DEFAULT:
43         case SET_USB_SDB:
44         case SET_USB_SDB_DIAG:
45                 return TRUE;
46         default:
47                 return FALSE;
48         }
49 }
50
51 static void __usb_status_noti_handler(void *data)
52 {
53         int usb_state = -1;
54         int usb_mode = -1;
55         int ret;
56
57         if (_get_usb_state(&usb_state) < 0) {
58                 ERR(" Unable to get usb status !\n");
59                 return;
60         }
61
62         ret = vconf_get_int(VCONFKEY_USB_CUR_MODE, &usb_mode);
63         if (ret != 0) {
64                 DBG("Vconf get failed\n");
65                 return;
66         }
67
68         DBG("USB noti handler, USB state : %d, Mode : %d\n", usb_state, usb_mode);
69         DBG("usb_fd = 0x%x\n", dr_info.usb.usb_fd);
70
71
72         /*
73          * If USB driver builted in the Kernel, VCONFKEY_SYSMAN_USB_CONNECTED is not used.
74          * If USB driver loaded in DR/MTP then need to check VCONFKEY_SYSMAN_USB_CONNECTED.
75          * After usb driver loaded, MTP will be set vconf value as VCONFKEY_SYSMAN_USB_CONNECTED
76          */
77         if (usb_state != VCONFKEY_SYSMAN_USB_DISCONNECTED &&
78                         acm_enabled(usb_mode)) {
79                 _init_usb();
80         } else if (usb_state == VCONFKEY_SYSMAN_USB_DISCONNECTED) {
81                 _deinit_usb();
82                 _deinit_dr();
83         }
84         return;
85 }
86
87 static void __usb_mode_noti_handler(void *data)
88 {
89         int usb_mode = -1;
90         int ret;
91
92         ret = vconf_get_int(VCONFKEY_USB_CUR_MODE, &usb_mode);
93         if (ret != 0) {
94                 DBG("Vconf get failed\n");
95                 return;
96         }
97
98         DBG("USB Mode noti handler, USB  Mode : %d\n", usb_mode);
99         DBG("usb_fd = 0x%x\n", dr_info.usb.usb_fd);
100
101         if (!acm_enabled(usb_mode)) {
102                 _deinit_usb();
103                 _deinit_dr();
104         }
105
106         return;
107 }
108
109
110 gboolean _register_vconf_notification(void)
111 {
112         int ret;
113
114         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
115                         (vconf_callback_fn)__usb_status_noti_handler, NULL);
116         if (ret < 0)
117                 ERR("Error !!! VCONFKEY reg noti  : %s\n", VCONFKEY_SYSMAN_USB_STATUS);
118
119         ret = vconf_notify_key_changed(VCONFKEY_USB_CUR_MODE,
120                         (vconf_callback_fn) __usb_mode_noti_handler, NULL);
121         if (ret < 0)
122                 ERR("Error !!! VCONFKEY reg noti  : %s\n", VCONFKEY_USB_CUR_MODE);
123
124         return TRUE;
125 }
126
127 void _unregister_vconf_notification(void)
128 {
129         vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
130                         (vconf_callback_fn) __usb_status_noti_handler);
131         vconf_ignore_key_changed(VCONFKEY_USB_CUR_MODE,
132                         (vconf_callback_fn) __usb_mode_noti_handler);
133         return;
134 }
135
136 gboolean _register_telephony_event(void)
137 {
138         return TRUE;
139 }
140
141 void _unregister_telephony_event(void)
142 {
143         return;
144 }
145