4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "core/devices.h"
23 #include "core/launch.h"
24 #include "usb-client.h"
28 #define TICKER_TYPE_DEFAULT "usb-client-default"
29 #define TICKER_TYPE_SSH "usb-client-ssh"
31 #ifdef TIZEN_ENGINEER_MODE
32 const static bool eng_mode = true;
34 const static bool eng_mode = false;
39 static int write_sysfs(char *path, char *value)
45 if (strlen(path) == 0)
48 if (strlen(value) > 0)
53 fp = fopen(path, "w");
55 _E("FAIL: fopen(%s)", path);
59 ret = fwrite(conf, sizeof(char), strlen(conf), fp);
60 if (ret < strlen(conf)) {
61 _E("FAIL: fwrite(%s)", conf);
70 static int set_configurations_to_sysfs(dd_list *list)
73 struct usb_configuration *conf;
75 char *root_path, path[BUF_MAX];
80 ret = get_root_path(&root_path);
82 _E("Failed to get root path for usb configuration (%d)", ret);
86 DD_LIST_FOREACH(list, l, conf) {
87 snprintf(path, sizeof(path), "%s/%s", root_path, conf->name);
88 _I("Usb conf: (%s, %s)", path, conf->value);
89 ret = write_sysfs(path, conf->value);
91 _E("FAIL: write_sysfs(%s, %s)", path, conf->value);
98 static void run_operations_for_usb_mode(dd_list *list)
102 struct usb_operation *oper;
107 DD_LIST_FOREACH(list, l, oper) {
108 ret = launch_app_cmd(oper->oper);
109 _I("operation: %s(%d)", oper->oper, ret);
113 void unset_client_mode(int mode, bool change)
120 update_current_usb_mode(SET_USB_NONE);
122 if (update_usb_state(VCONFKEY_SYSMAN_USB_DISCONNECTED) < 0)
123 _E("FAIL: update_usb_state(%d)", VCONFKEY_SYSMAN_USB_DISCONNECTED);
125 ret = make_configuration_list(SET_USB_NONE);
127 ret = get_configurations_list(&conf_list);
129 ret = set_configurations_to_sysfs(conf_list);
131 _E("FAIL: set_configurations_to_sysfs()");
134 release_configuration_list();
136 ret = make_operation_list(mode, USB_CON_STOP);
138 ret = get_operations_list(&oper_list);
140 run_operations_for_usb_mode(oper_list);
142 release_operations_list();
145 static int get_selected_mode_by_debug_mode(int mode)
149 debug = get_debug_mode();
152 case SET_USB_DEFAULT:
153 if (debug == 1) /* debug on */
157 case SET_USB_SDB_DIAG:
158 if (debug == 0) /* debug off */
159 mode = SET_USB_DEFAULT;
168 static bool get_usb_tethering_state(void)
173 if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &state) == 0
174 && (state & VCONFKEY_MOBILE_HOTSPOT_MODE_USB)) {
175 _I("USB tethering is on");
179 _I("USB tethering is off");
183 static bool check_usb_tethering(int sel_mode)
187 state = get_usb_tethering_state();
193 case SET_USB_RNDIS_TETHERING:
200 if (change_selected_usb_mode(SET_USB_RNDIS_TETHERING) != 0) {
201 _E("Failed to set usb selected mode (%d)", SET_USB_RNDIS_TETHERING);
208 static int turn_on_debug(void)
211 return vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, debug);
214 static int check_first_eng_mode(int sel_mode)
216 static bool first = true;
218 if (!eng_mode || !first)
223 if (sel_mode == SET_USB_DEFAULT) {
224 sel_mode = SET_USB_SDB;
225 if (turn_on_debug() != 0)
226 _E("Failed to turn on debug toggle");
232 static int decide_selected_mode(int sel_mode, int cur_mode)
236 if (check_usb_tethering(sel_mode))
239 mode = check_first_eng_mode(sel_mode);
241 mode = get_selected_mode_by_debug_mode(mode);
243 if (mode == cur_mode) {
244 _I("Selected usb mode (%d) is same with current usb mode (%d)", mode, cur_mode);
248 _I("Selected mode decided is (%d)", mode);
253 void change_client_setting(int options)
263 if (control_status() == DEVICE_OPS_STATUS_STOP) {
264 launch_syspopup(USB_RESTRICT);
268 sel_mode = get_selected_usb_mode();
269 cur_mode = get_current_usb_mode();
271 sel_mode = decide_selected_mode(sel_mode, cur_mode);
272 if (sel_mode == -ECANCELED)
274 else if (sel_mode <= 0) {
275 _E("Failed to get selected mode");
279 if (options & SET_CONFIGURATION) {
280 if (cur_mode != SET_USB_NONE) {
281 unset_client_mode(cur_mode, true);
284 ret = make_configuration_list(sel_mode);
286 _E("FAIL: make_configuration_list(%d)", sel_mode);
290 ret = get_configurations_list(&conf_list);
292 _E("failed to get configuration list");
296 ret = set_configurations_to_sysfs(conf_list);
298 _E("FAIL: set_configurations_to_sysfs()");
303 if (options & SET_OPERATION) {
304 ret = make_operation_list(sel_mode, USB_CON_START);
306 _E("FAIL: make_operation_list()");
310 ret = get_operations_list(&oper_list);
312 _E("failed to get operation list");
316 if (update_usb_state(VCONFKEY_SYSMAN_USB_AVAILABLE) < 0)
317 _E("FAIL: update_usb_state(%d)", VCONFKEY_SYSMAN_USB_AVAILABLE);
319 update_current_usb_mode(sel_mode);
321 run_operations_for_usb_mode(oper_list);
324 if (options & SET_NOTIFICATION) {
331 release_operations_list();
332 release_configuration_list();
335 launch_syspopup(USB_ERROR);
340 void client_mode_changed(keynode_t* key, void *data)
344 if (get_wait_configured())
347 change_client_setting(SET_CONFIGURATION | SET_OPERATION | SET_NOTIFICATION);
350 void debug_mode_changed(keynode_t* key, void *data)
356 if (control_status() == DEVICE_OPS_STATUS_STOP)
359 new_debug = get_debug_mode();
360 _I("old debug(%d), new debug(%d)", debug, new_debug);
361 if (debug == new_debug)
364 cur_mode = get_current_usb_mode();
365 _I("cur_mode(%d)", cur_mode);
368 case SET_USB_DEFAULT:
370 case SET_USB_SDB_DIAG:
372 sel_mode = SET_USB_DEFAULT;
374 sel_mode = SET_USB_SDB;
380 if (change_selected_usb_mode(sel_mode) != 0)
381 _E("FAIL: change_selected_usb_mode(%d)", sel_mode);
387 static int turn_on_usb_tethering(void)
393 cur_mode = get_current_usb_mode();
394 sel_mode = get_selected_usb_mode();
398 case SET_USB_RNDIS_TETHERING:
406 case SET_USB_RNDIS_TETHERING:
409 sel_mode = SET_USB_RNDIS_TETHERING;
413 ret = change_selected_usb_mode(sel_mode);
415 _E("FAIL: change_selected_usb_mode(%d)", sel_mode);
420 static int turn_off_usb_tethering(void)
426 cur_mode = get_current_usb_mode();
430 case SET_USB_RNDIS_TETHERING:
431 sel_mode = get_default_mode();
432 ret = change_selected_usb_mode(sel_mode);
434 _E("FAIL: change_selected_usb_mode(%d)", sel_mode);
442 void tethering_status_changed(keynode_t* key, void *data)
447 if (control_status() == DEVICE_OPS_STATUS_STOP)
450 usb_tethering = get_usb_tethering_state();
452 ret = turn_on_usb_tethering();
454 ret = turn_off_usb_tethering();
457 _E("Failed to change tethering mode");