0f9463e1fea6cb621a8748833356d70918ee9123
[framework/system/system-server.git] / ss_device_change_handler.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <vconf.h>
23 #include <sysman.h>
24 #include <pmapi.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mount.h>
28 #include <syspopup_caller.h>
29
30 #include "ss_queue.h"
31 #include "ss_log.h"
32 #include "ss_device_handler.h"
33 #include "ss_device_plugin.h"
34 #include "ss_noti.h"
35 #include "include/ss_data.h"
36
37 #define BUFF_MAX                                        255
38 #define SYS_CLASS_INPUT                                 "/sys/class/input"
39
40 struct input_event {
41         long dummy[2];
42         unsigned short type;
43         unsigned short code;
44         int value;
45 };
46
47 enum snd_jack_types {
48         SND_JACK_HEADPHONE = 0x0001,
49         SND_JACK_MICROPHONE = 0x0002,
50         SND_JACK_HEADSET = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE,
51         SND_JACK_LINEOUT = 0x0004,
52         SND_JACK_MECHANICAL = 0x0008,   /* If detected separately */
53         SND_JACK_VIDEOOUT = 0x0010,
54         SND_JACK_AVOUT = SND_JACK_LINEOUT | SND_JACK_VIDEOOUT,
55 };
56
57 static void usb_chgdet_cb(struct ss_main_data *ad)
58 {
59         PRT_TRACE("jack - usb changed\n");
60         pm_change_state(LCD_NORMAL);
61         /* check charging now */
62         ss_lowbat_is_charge_in_now();
63         /* check current battery level */
64         ss_lowbat_monitor(NULL);
65         ss_action_entry_call_internal(PREDEF_USBCON, 0);
66 }
67
68 static void ta_chgdet_cb(struct ss_main_data *ad)
69 {
70         PRT_TRACE("jack - ta changed\n");
71         pm_change_state(LCD_NORMAL);
72         /* check charging now */
73         ss_lowbat_is_charge_in_now();
74         /* check current battery level */
75         ss_lowbat_monitor(NULL);
76
77         int val = -1;
78         int ret = -1;
79         int bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
80
81         if (plugin_intf->OEM_sys_get_jack_charger_online(&val) == 0) {
82                 if (val == 0) {
83                         pm_unlock_state(LCD_OFF, STAY_CUR_STATE);
84
85                         vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state);
86                         if(bat_state < VCONFKEY_SYSMAN_BAT_NORMAL) {
87                                 bundle *b = NULL;
88                                 b = bundle_create();
89                                 bundle_add(b, "_SYSPOPUP_CONTENT_", "warning");
90
91                                 ret = syspopup_launch("lowbat-syspopup", b);
92                                 if (ret < 0) {
93                                         PRT_TRACE_EM("popup lauch failed\n");
94                                 }
95                                 bundle_free(b);
96                         }
97                 } else {
98                         pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
99         }
100         }
101         else
102                 PRT_TRACE_ERR("failed to get ta status\n");
103 }
104
105 static void earjack_chgdet_cb(struct ss_main_data *ad)
106 {
107         PRT_TRACE("jack - earjack changed\n");
108         ss_action_entry_call_internal(PREDEF_EARJACKCON, 0);
109 }
110
111 static void earkey_chgdet_cb(struct ss_main_data *ad)
112 {
113         int val;
114         PRT_TRACE("jack - earkey changed\n");
115
116         if (plugin_intf->OEM_sys_get_jack_earkey_online(&val) == 0)
117                 vconf_set_int(VCONFKEY_SYSMAN_EARJACKKEY, val);
118 }
119
120 static void tvout_chgdet_cb(struct ss_main_data *ad)
121 {
122         PRT_TRACE("jack - tvout changed\n");
123         pm_change_state(LCD_NORMAL);
124 }
125
126 static void hdmi_chgdet_cb(struct ss_main_data *ad)
127 {
128         PRT_TRACE("jack - hdmi changed\n");
129         pm_change_state(LCD_NORMAL);
130 }
131
132 static void cradle_chgdet_cb(struct ss_main_data *ad)
133 {
134         PRT_TRACE("jack - cradle changed\n");
135 }
136
137 static void keyboard_chgdet_cb(struct ss_main_data *ad)
138 {
139         int val = -1;
140         int ret = -1;
141         PRT_TRACE("jack - keyboard changed\n");
142
143         ret = plugin_intf->OEM_sys_get_jack_keyboard_online(&val);
144         if( ret == 0) {
145                 if(val != 1)
146                         val = 0;
147                 vconf_set_int(VCONFKEY_SYSMAN_SLIDING_KEYBOARD, val);
148         } else {
149                 vconf_set_int(VCONFKEY_SYSMAN_SLIDING_KEYBOARD, VCONFKEY_SYSMAN_SLIDING_KEYBOARD_NOT_SUPPORTED);
150         }
151 }
152
153 static void mmc_chgdet_cb(void *data)
154 {
155         if (data == NULL) {
156                 PRT_TRACE("mmc removed");
157                 ss_mmc_removed();
158         } else {
159                 PRT_TRACE("mmc added");
160                 ss_mmc_inserted();
161         }
162 }
163
164 static void ums_unmount_cb(void *data)
165 {
166         umount(MOVINAND_MOUNT_POINT);
167 }
168
169 static void charge_cb(struct ss_main_data *ad)
170 {
171         int val = -1;
172
173         if (plugin_intf->OEM_sys_get_battery_health(&val) == 0) {
174                 if (val != BATTERY_GOOD) {
175                         PRT_TRACE_ERR("Battery health status is not good (%d)", val);
176                         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, CHARGE_ERROR_ACT);
177                         return;
178                 }
179         } else {
180                 PRT_TRACE_ERR("failed to get battery health status");
181 }
182
183         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, CHARGE_CHECK_ACT);
184 }
185
186 static void __usb_storage_cb(keynode_t *key, void *data)
187 {
188         char *vconf_value;
189
190         if (data == NULL) {
191                 PRT_TRACE("USB Storage removed");
192                 vconf_value = vconf_get_str(VCONFKEY_INTERNAL_REMOVED_USB_STORAGE);
193                 ss_action_entry_call_internal(PREDEF_USB_STORAGE_REMOVE, 1, vconf_value);
194         } else {
195                 PRT_TRACE("USB Storage added");
196                 vconf_value = vconf_get_str(VCONFKEY_INTERNAL_ADDED_USB_STORAGE);
197                 ss_action_entry_call_internal(PREDEF_USB_STORAGE_ADD, 1, vconf_value);
198         }
199 }
200
201 int ss_device_change_init(struct ss_main_data *ad)
202 {
203         /* for simple noti change cb */
204         ss_noti_add("device_usb_chgdet", (void *)usb_chgdet_cb, (void *)ad);
205         ss_noti_add("device_ta_chgdet", (void *)ta_chgdet_cb, (void *)ad);
206         ss_noti_add("device_earjack_chgdet", (void *)earjack_chgdet_cb, (void *)ad);
207         ss_noti_add("device_earkey_chgdet", (void *)earkey_chgdet_cb, (void *)ad);
208         ss_noti_add("device_tvout_chgdet", (void *)tvout_chgdet_cb, (void *)ad);
209         ss_noti_add("device_hdmi_chgdet", (void *)hdmi_chgdet_cb, (void *)ad);
210         ss_noti_add("device_cradle_chgdet", (void *)cradle_chgdet_cb, (void *)ad);
211         ss_noti_add("device_keyboard_chgdet", (void *)keyboard_chgdet_cb, (void *)ad);
212         ss_noti_add("mmcblk_add", (void *)mmc_chgdet_cb, (void *)1);
213         ss_noti_add("mmcblk_remove", (void *)mmc_chgdet_cb, NULL);
214
215         ss_noti_add("unmount_ums", (void *)ums_unmount_cb, NULL);
216         ss_noti_add("device_charge_chgdet", (void *)charge_cb, (void *)ad);
217
218         if (vconf_notify_key_changed(VCONFKEY_INTERNAL_ADDED_USB_STORAGE, (void *)__usb_storage_cb, (void *)1) < 0) {
219                 PRT_TRACE_ERR("Vconf notify key chaneged failed: KEY(%s)", VCONFKEY_SYSMAN_ADDED_USB_STORAGE);
220         }
221
222         if (vconf_notify_key_changed(VCONFKEY_INTERNAL_REMOVED_USB_STORAGE, (void *)__usb_storage_cb, NULL) < 0) {
223                 PRT_TRACE_ERR("Vconf notify key chaneged failed: KEY(%s)", VCONFKEY_SYSMAN_REMOVED_USB_STORAGE);
224         }
225
226         cradle_chgdet_cb(NULL);
227
228         return 0;
229 }