15b9c547cSRui Paulo /* 25b9c547cSRui Paulo * Hotspot 2.0 client - Web browser using Android browser 35b9c547cSRui Paulo * Copyright (c) 2013, Qualcomm Atheros, Inc. 45b9c547cSRui Paulo * 55b9c547cSRui Paulo * This software may be distributed under the terms of the BSD license. 65b9c547cSRui Paulo * See README for more details. 75b9c547cSRui Paulo */ 85b9c547cSRui Paulo 95b9c547cSRui Paulo #include "includes.h" 105b9c547cSRui Paulo 115b9c547cSRui Paulo #include "common.h" 125b9c547cSRui Paulo #include "utils/eloop.h" 135b9c547cSRui Paulo #include "wps/http_server.h" 145b9c547cSRui Paulo #include "browser.h" 155b9c547cSRui Paulo 165b9c547cSRui Paulo 175b9c547cSRui Paulo struct browser_data { 185b9c547cSRui Paulo int success; 195b9c547cSRui Paulo }; 205b9c547cSRui Paulo 215b9c547cSRui Paulo 225b9c547cSRui Paulo static void browser_timeout(void *eloop_data, void *user_ctx) 235b9c547cSRui Paulo { 245b9c547cSRui Paulo wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to " 255b9c547cSRui Paulo "complete"); 265b9c547cSRui Paulo eloop_terminate(); 275b9c547cSRui Paulo } 285b9c547cSRui Paulo 295b9c547cSRui Paulo 305b9c547cSRui Paulo static void http_req(void *ctx, struct http_request *req) 315b9c547cSRui Paulo { 325b9c547cSRui Paulo struct browser_data *data = ctx; 335b9c547cSRui Paulo struct wpabuf *resp; 345b9c547cSRui Paulo const char *url; 355b9c547cSRui Paulo int done = 0; 365b9c547cSRui Paulo 375b9c547cSRui Paulo url = http_request_get_uri(req); 385b9c547cSRui Paulo wpa_printf(MSG_INFO, "Browser response received: %s", url); 395b9c547cSRui Paulo 405b9c547cSRui Paulo if (os_strcmp(url, "/") == 0) { 415b9c547cSRui Paulo data->success = 1; 425b9c547cSRui Paulo done = 1; 435b9c547cSRui Paulo } else if (os_strncmp(url, "/osu/", 5) == 0) { 445b9c547cSRui Paulo data->success = atoi(url + 5); 455b9c547cSRui Paulo done = 1; 465b9c547cSRui Paulo } 475b9c547cSRui Paulo 485b9c547cSRui Paulo resp = wpabuf_alloc(1); 495b9c547cSRui Paulo if (resp == NULL) { 505b9c547cSRui Paulo http_request_deinit(req); 515b9c547cSRui Paulo if (done) 525b9c547cSRui Paulo eloop_terminate(); 535b9c547cSRui Paulo return; 545b9c547cSRui Paulo } 555b9c547cSRui Paulo 565b9c547cSRui Paulo if (done) { 575b9c547cSRui Paulo eloop_cancel_timeout(browser_timeout, NULL, NULL); 585b9c547cSRui Paulo eloop_register_timeout(0, 500000, browser_timeout, &data, NULL); 595b9c547cSRui Paulo } 605b9c547cSRui Paulo 615b9c547cSRui Paulo http_request_send_and_deinit(req, resp); 625b9c547cSRui Paulo } 635b9c547cSRui Paulo 645b9c547cSRui Paulo 655b9c547cSRui Paulo int hs20_web_browser(const char *url) 665b9c547cSRui Paulo { 675b9c547cSRui Paulo struct http_server *http; 685b9c547cSRui Paulo struct in_addr addr; 695b9c547cSRui Paulo struct browser_data data; 705b9c547cSRui Paulo pid_t pid; 715b9c547cSRui Paulo 725b9c547cSRui Paulo wpa_printf(MSG_INFO, "Launching Android browser to %s", url); 735b9c547cSRui Paulo 745b9c547cSRui Paulo os_memset(&data, 0, sizeof(data)); 755b9c547cSRui Paulo 765b9c547cSRui Paulo if (eloop_init() < 0) { 775b9c547cSRui Paulo wpa_printf(MSG_ERROR, "eloop_init failed"); 785b9c547cSRui Paulo return -1; 795b9c547cSRui Paulo } 805b9c547cSRui Paulo addr.s_addr = htonl((127 << 24) | 1); 815b9c547cSRui Paulo http = http_server_init(&addr, 12345, http_req, &data); 825b9c547cSRui Paulo if (http == NULL) { 835b9c547cSRui Paulo wpa_printf(MSG_ERROR, "http_server_init failed"); 845b9c547cSRui Paulo eloop_destroy(); 855b9c547cSRui Paulo return -1; 865b9c547cSRui Paulo } 875b9c547cSRui Paulo 885b9c547cSRui Paulo pid = fork(); 895b9c547cSRui Paulo if (pid < 0) { 905b9c547cSRui Paulo wpa_printf(MSG_ERROR, "fork: %s", strerror(errno)); 915b9c547cSRui Paulo http_server_deinit(http); 925b9c547cSRui Paulo eloop_destroy(); 935b9c547cSRui Paulo return -1; 945b9c547cSRui Paulo } 955b9c547cSRui Paulo 965b9c547cSRui Paulo if (pid == 0) { 975b9c547cSRui Paulo /* run the external command in the child process */ 98*780fb4a2SCy Schubert char *argv[7]; 995b9c547cSRui Paulo 1005b9c547cSRui Paulo argv[0] = "browser-android"; 1015b9c547cSRui Paulo argv[1] = "start"; 1025b9c547cSRui Paulo argv[2] = "-a"; 1035b9c547cSRui Paulo argv[3] = "android.intent.action.VIEW"; 1045b9c547cSRui Paulo argv[4] = "-d"; 1055b9c547cSRui Paulo argv[5] = (void *) url; 106*780fb4a2SCy Schubert argv[6] = NULL; 1075b9c547cSRui Paulo 1085b9c547cSRui Paulo execv("/system/bin/am", argv); 1095b9c547cSRui Paulo wpa_printf(MSG_ERROR, "execv: %s", strerror(errno)); 1105b9c547cSRui Paulo exit(0); 1115b9c547cSRui Paulo return -1; 1125b9c547cSRui Paulo } 1135b9c547cSRui Paulo 1145b9c547cSRui Paulo eloop_register_timeout(30, 0, browser_timeout, &data, NULL); 1155b9c547cSRui Paulo eloop_run(); 1165b9c547cSRui Paulo eloop_cancel_timeout(browser_timeout, &data, NULL); 1175b9c547cSRui Paulo http_server_deinit(http); 1185b9c547cSRui Paulo eloop_destroy(); 1195b9c547cSRui Paulo 1205b9c547cSRui Paulo wpa_printf(MSG_INFO, "Closing Android browser"); 1215b9c547cSRui Paulo if (system("/system/bin/input keyevent KEYCODE_HOME") != 0) { 1225b9c547cSRui Paulo wpa_printf(MSG_INFO, "Failed to inject keyevent"); 1235b9c547cSRui Paulo } 1245b9c547cSRui Paulo 1255b9c547cSRui Paulo return data.success; 1265b9c547cSRui Paulo } 127