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;
44 static int write_sysfs(char *path, char *value)
52 fp = fopen(path, "w");
54 _E("FAIL: fopen(%s)", path);
58 ret = fwrite(value, sizeof(char), strlen(value), fp);
59 if (ret < strlen(value)) {
60 _E("FAIL: fwrite(%s)", value);
71 static int set_configurations_to_sysfs(dd_list *list)
74 struct xmlConfiguration *conf;
80 DD_LIST_FOREACH(list, l, conf) {
84 ret = write_sysfs(conf->path, conf->value);
86 _E("FAIL: write_sysfs(%s, %s)", conf->path, conf->value);
93 static void run_operations_for_usb_mode(dd_list *list)
97 struct xmlOperation *oper;
102 DD_LIST_FOREACH(list, l, oper) {
103 ret = launch_app_cmd(oper->oper);
104 _I("operation: %s(%d)", oper->oper, ret);
108 void unset_client_mode(int mode, bool change)
115 update_current_usb_mode(SET_USB_NONE);
117 if (update_usb_state(VCONFKEY_SYSMAN_USB_DISCONNECTED) < 0)
118 _E("FAIL: update_usb_state(%d)", VCONFKEY_SYSMAN_USB_DISCONNECTED);
120 ret = make_configuration_list(SET_USB_NONE);
122 ret = get_configurations_list(&conf_list);
124 ret = set_configurations_to_sysfs(conf_list);
126 _E("FAIL: set_configurations_to_sysfs()");
130 ret = make_operation_list(mode, USB_CON_UNSET);
132 ret = get_operations_list(&oper_list);
134 run_operations_for_usb_mode(oper_list);
137 release_operations_list();
140 static void launch_ticker_notification(int cur_mode, int sel_mode)
142 struct ticker_data ticker;
143 const struct device_ops *ticker_ops;
146 case SET_USB_DEFAULT:
148 case SET_USB_SDB_DIAG:
149 if (cur_mode == SET_USB_DEFAULT
150 || cur_mode == SET_USB_SDB
151 || cur_mode == SET_USB_SDB_DIAG)
154 ticker.name = TICKER_TYPE_DEFAULT;
155 ticker.type = 0; /* WITHOUT_QUEUE */
158 case SET_USB_RNDIS_DIAG:
159 case SET_USB_RNDIS_SDB:
160 if (cur_mode == SET_USB_RNDIS
161 || cur_mode == SET_USB_RNDIS_TETHERING
162 || cur_mode == SET_USB_RNDIS_DIAG
163 || cur_mode == SET_USB_RNDIS_SDB)
166 ticker.name = TICKER_TYPE_SSH;
167 ticker.type = 0; /* WITHOUT_QUEUE */
170 case SET_USB_RNDIS_TETHERING:
175 ticker_ops = find_device("ticker");
177 if (get_cradle_status() > 0)
180 if (ticker_ops && ticker_ops->init)
181 ticker_ops->init(&ticker);
183 _E("cannot find \"ticker\" ops");
186 static int get_selected_mode_by_debug_mode(int mode)
190 debug = get_debug_mode();
193 case SET_USB_DEFAULT:
194 if (debug == 1) /* debug on */
198 case SET_USB_SDB_DIAG:
199 if (debug == 0) /* debug off */
200 mode = SET_USB_DEFAULT;
209 static bool get_usb_tethering_state(void)
214 if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &state) == 0
215 && (state & VCONFKEY_MOBILE_HOTSPOT_MODE_USB)) {
216 _I("USB tethering is on");
220 _I("USB tethering is off");
224 static bool check_usb_tethering(int sel_mode)
228 state = get_usb_tethering_state();
234 case SET_USB_RNDIS_TETHERING:
241 if (change_selected_usb_mode(SET_USB_RNDIS_TETHERING) != 0) {
242 _E("Failed to set usb selected mode (%d)", SET_USB_RNDIS_TETHERING);
249 static int turn_on_debug(void)
252 return vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, debug);
255 static int check_first_eng_mode(int sel_mode)
257 static bool first = true;
259 if (!eng_mode || !first)
264 if (sel_mode == SET_USB_DEFAULT) {
265 sel_mode = SET_USB_SDB;
266 if (turn_on_debug() != 0)
267 _E("Failed to turn on debug toggle");
273 static int decide_selected_mode(int sel_mode, int cur_mode)
277 if (check_usb_tethering(sel_mode))
280 mode = check_first_eng_mode(sel_mode);
282 mode = get_selected_mode_by_debug_mode(mode);
284 if (mode == cur_mode) {
285 _I("Selected usb mode (%d) is same with current usb mode (%d)", mode, cur_mode);
289 _I("Selected mode decided is (%d)", mode);
294 void change_client_setting(int options)
304 if (control_status() == DEVICE_OPS_STATUS_STOP) {
305 launch_syspopup(USB_RESTRICT);
309 sel_mode = get_selected_usb_mode();
310 cur_mode = get_current_usb_mode();
312 sel_mode = decide_selected_mode(sel_mode, cur_mode);
313 if (sel_mode == -ECANCELED)
315 else if (sel_mode <= 0) {
316 _E("Failed to get selected mode");
320 if (options & SET_CONFIGURATION) {
321 if (cur_mode != SET_USB_NONE) {
322 unset_client_mode(cur_mode, true);
325 ret = make_configuration_list(sel_mode);
327 _E("FAIL: make_configuration_list(%d)", sel_mode);
328 goto out_configuration;
331 ret = get_configurations_list(&conf_list);
333 _E("failed to get configuration list");
334 goto out_configuration;
337 ret = set_configurations_to_sysfs(conf_list);
339 _E("FAIL: set_configurations_to_sysfs()");
340 goto out_configuration;
344 if (options & SET_OPERATION) {
345 ret = make_operation_list(sel_mode, USB_CON_SET);
347 _E("FAIL: make_operation_list()");
351 ret = get_operations_list(&oper_list);
353 _E("failed to get operation list");
357 if (update_usb_state(VCONFKEY_SYSMAN_USB_AVAILABLE) < 0)
358 _E("FAIL: update_usb_state(%d)", VCONFKEY_SYSMAN_USB_AVAILABLE);
360 update_current_usb_mode(sel_mode);
362 run_operations_for_usb_mode(oper_list);
365 if (options & SET_NOTIFICATION) {
366 launch_ticker_notification(cur_mode, sel_mode);
372 release_operations_list();
375 if (make_configuration_list(SET_USB_NONE) < 0)
376 _E("Release configurations info error");
379 launch_syspopup(USB_ERROR);
384 void client_mode_changed(keynode_t* key, void *data)
388 if (get_wait_configured())
391 change_client_setting(SET_CONFIGURATION | SET_OPERATION | SET_NOTIFICATION);
395 void client_mode_changed(keynode_t* key, void *data)
404 if (control_status() == DEVICE_OPS_STATUS_STOP) {
405 launch_syspopup(USB_RESTRICT);
409 sel_mode = get_selected_usb_mode();
410 if (sel_mode <= SET_USB_NONE) {
411 _E("Getting selected usb mode error(%d)", sel_mode);
415 if (check_usb_tethering()) {
416 _I("Turning on the usb tethering");
420 ret = check_debug_mode(sel_mode);
424 sel_mode = check_first_eng_mode(sel_mode);
426 cur_mode = get_current_usb_mode();
427 if (cur_mode < SET_USB_NONE) {
428 _E("Getting current usb mode error(%d)", cur_mode);
432 if (cur_mode == sel_mode) {
433 _I("Current usb mode(%d) is same with selected usb mode(%d)", cur_mode, sel_mode);
437 if (cur_mode != SET_USB_NONE) {
438 unset_client_mode(cur_mode);
441 ret = make_configuration_list(sel_mode);
443 _E("FAIL: make_configuration_list(%d)", sel_mode);
447 ret = make_operation_list(sel_mode, USB_CON_SET);
449 _E("FAIL: make_operation_list()");
453 ret = get_configurations_list(&conf_list);
455 _E("failed to get configuration list");
459 ret = get_operations_list(&oper_list);
461 _E("failed to get operation list");
465 ret = set_configurations_to_sysfs(conf_list);
467 _E("FAIL: set_configurations_to_sysfs()");
471 update_current_usb_mode(sel_mode);
473 run_operations_for_usb_mode(oper_list);
475 launch_ticker_notification(cur_mode, sel_mode);
481 launch_syspopup(USB_ERROR);
483 release_operations_list();
484 ret = make_configuration_list(SET_USB_NONE);
486 _E("release configurations info error");
491 void debug_mode_changed(keynode_t* key, void *data)
497 if (control_status() == DEVICE_OPS_STATUS_STOP)
500 new_debug = get_debug_mode();
501 _I("old debug(%d), new debug(%d)", debug, new_debug);
502 if (debug == new_debug)
505 cur_mode = get_current_usb_mode();
506 _I("cur_mode(%d)", cur_mode);
509 case SET_USB_DEFAULT:
511 case SET_USB_SDB_DIAG:
513 sel_mode = SET_USB_DEFAULT;
515 sel_mode = SET_USB_SDB;
521 if (change_selected_usb_mode(sel_mode) != 0)
522 _E("FAIL: change_selected_usb_mode(%d)", sel_mode);
528 static int turn_on_usb_tethering(void)
534 cur_mode = get_current_usb_mode();
535 sel_mode = get_selected_usb_mode();
539 case SET_USB_RNDIS_TETHERING:
547 case SET_USB_RNDIS_TETHERING:
550 sel_mode = SET_USB_RNDIS_TETHERING;
554 ret = change_selected_usb_mode(sel_mode);
556 _E("FAIL: change_selected_usb_mode(%d)", sel_mode);
561 static int turn_off_usb_tethering(void)
567 cur_mode = get_current_usb_mode();
571 case SET_USB_RNDIS_TETHERING:
572 sel_mode = get_default_mode();
573 ret = change_selected_usb_mode(sel_mode);
575 _E("FAIL: change_selected_usb_mode(%d)", sel_mode);
583 void tethering_status_changed(keynode_t* key, void *data)
588 if (control_status() == DEVICE_OPS_STATUS_STOP)
591 usb_tethering = get_usb_tethering_state();
593 ret = turn_on_usb_tethering();
595 ret = turn_off_usb_tethering();
598 _E("Failed to change tethering mode");