Tizen 2.1 base
[platform/core/connectivity/data-router.git] / src / dr-noti-handler.c
1 /*
2  * Data-Router
3  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 #include <glib.h>
20 #include <sys/wait.h>
21 #include <ITapiPS.h>
22 #include <tapi_common.h>
23 #include <ITapiModem.h>
24
25 #include "dr-main.h"
26 #include "dr-usb.h"
27 #include "dr-modem.h"
28 #include "dr-common.h"
29
30
31
32 #define ACM_WAIT_TIME 300000
33
34 extern dr_info_t dr_info;
35 volatile gboolean dsr_status = FALSE;
36 TapiHandle *tapi_handle = NULL;
37
38
39 static void __usb_status_noti_handler(void *data)
40 {
41         int usb_state = -1;
42         int usb_mode = -1;
43         int ret;
44
45         if (_get_usb_state(&usb_state) < 0) {
46                 ERR(" Unable to get usb status !\n");
47                 return;
48         }
49
50         ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &usb_mode);
51         if (ret != 0) {
52                 DBG("Vconf get failed\n");
53                 return;
54         }
55
56         DBG("USB noti handler, USB state : %d, Mode : %d\n", usb_state, usb_mode);
57         DBG("usb_fd = 0x%x\n", dr_info.usb.usb_fd);
58
59
60         /*
61          * If USB driver builted in the Kernel, VCONFKEY_SYSMAN_USB_CONNECTED is not used.
62          * If USB driver loaded in DR/MTP then need to check VCONFKEY_SYSMAN_USB_CONNECTED.
63          * After usb driver loaded, MTP will be set vconf value as VCONFKEY_SYSMAN_USB_CONNECTED
64          */
65         if (usb_state != VCONFKEY_SYSMAN_USB_DISCONNECTED &&
66                 usb_mode == SETTING_USB_DEFAULT_MODE) {
67                 _init_usb();
68         } else if (usb_state == VCONFKEY_SYSMAN_USB_DISCONNECTED) {
69                 _deinit_usb();
70                 _deinit_dr();
71         }
72         return;
73 }
74
75 static void __usb_mode_noti_handler(void *data)
76 {
77         int usb_state = -1;
78         int usb_mode = -1;
79         int ret;
80
81         ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &usb_mode);
82         if (ret != 0) {
83                 DBG("Vconf get failed\n");
84                 return;
85         }
86
87         DBG("USB Mode noti handler, USB  Mode : %d\n", usb_mode);
88         DBG("usb_fd = 0x%x\n", dr_info.usb.usb_fd);
89
90         if (usb_mode != SETTING_USB_DEFAULT_MODE) {
91                 _deinit_usb();
92                 _deinit_dr();
93         }
94
95         return;
96 }
97
98
99 gboolean _register_vconf_notification(void)
100 {
101         int ret;
102
103         ret =
104             vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS, (vconf_callback_fn) __usb_status_noti_handler, NULL);
105         if (ret < 0) {
106                 ERR("Error !!! VCONFKEY reg noti  : %s\n", VCONFKEY_SYSMAN_USB_STATUS);
107         }
108
109         ret =
110             vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
111                                      (vconf_callback_fn) __usb_mode_noti_handler, NULL);
112         if (ret < 0) {
113                 ERR("Error !!! VCONFKEY reg noti  : %s\n", VCONFKEY_SETAPPL_USB_MODE_INT);
114         }
115
116         return TRUE;
117 }
118
119 void _unregister_vconf_notification(void)
120 {
121         vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS, (vconf_callback_fn) __usb_status_noti_handler);
122         vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT, (vconf_callback_fn) __usb_mode_noti_handler);
123         return;
124 }
125
126
127 static void __dcd_pin_handler(tapi_ps_btdun_pincontrol_status status)
128 {
129         DBG(" \n");
130
131         if (status == GPRS_SIGNAL_STATUS_ON) {
132                 DBG("Receive DCD + from Modem\n");
133                 dr_info.line.output_line_state.bits.dcd = TRUE;
134                 DBG("Current modem output line status = 0x%X\n",
135                              dr_info.line.output_line_state.state);
136         }else if (status == GPRS_SIGNAL_STATUS_OFF) {
137                 DBG("Receive DCD - from Modem\n");
138                 dr_info.line.output_line_state.bits.dcd = FALSE;
139                 DBG("Current modem output line status = 0x%X\n",
140                              dr_info.line.output_line_state.state);
141         }
142         _send_usb_line_state(dr_info.line.output_line_state.state);
143 }
144
145 static void __dsr_pin_handler(tapi_ps_btdun_pincontrol_status status)
146 {
147         if (status == GPRS_SIGNAL_STATUS_ON) {
148                 dr_info.line.output_line_state.bits.dsr = TRUE;
149                 dsr_status = TRUE;
150                 DBG("Receive DSR+ from modem\n");
151         }else if (status == GPRS_SIGNAL_STATUS_OFF) {
152                 dr_info.line.output_line_state.bits.dsr = FALSE;
153                 dsr_status = FALSE;
154
155                 DBG("Receive DSR- from modem\n");
156         }
157         _send_usb_line_state(dr_info.line.output_line_state.state);
158 }
159
160
161
162 static void __tel_dun_pincontrol_handler(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
163 {
164         DBG("+\n");
165
166         if (data == NULL)
167                 return;
168
169         tapi_ps_btdun_pincontrol *pinctrl;
170         pinctrl = (tapi_ps_btdun_pincontrol *)data;
171
172         DBG("pincontrol - Signal: %d, status: %d\n", pinctrl->signal, pinctrl->status);
173         switch (pinctrl->signal) {
174         case GPRS_DATA_SIGNAL_DSR:
175                 __dsr_pin_handler(pinctrl->status);
176                 break;
177         case GPRS_SIGNAL_DCD:
178                 __dcd_pin_handler(pinctrl->status);
179                 break;
180         default:
181                 break;
182         }
183
184         DBG("-\n");
185         return;
186 }
187
188 gboolean _register_telephony_event(void)
189 {
190         int ret;
191
192         tapi_handle = tel_init(NULL);
193         if (tapi_handle == NULL) {
194                 ERR("tel_init failed !!!\n");
195                 return FALSE;
196         }
197
198         ret = tel_register_noti_event(tapi_handle, TAPI_NOTI_MODEM_DUN_PIN_CTRL,
199                                  __tel_dun_pincontrol_handler,
200                                  NULL);
201
202         return TRUE;
203 }
204
205 void _unregister_telephony_event(void)
206 {
207         tel_deregister_noti_event(tapi_handle, TAPI_NOTI_MODEM_DUN_PIN_CTRL);
208
209         if (tel_deinit(tapi_handle) != TAPI_API_SUCCESS) {
210                 ERR("tel_deinit failed !!!\n");
211         }
212
213         tapi_handle = NULL;
214
215         return;
216 }
217