1/* 2 * Hotspot 2.0 client - Web browser using Android browser 3 * Copyright (c) 2013, Qualcomm Atheros, Inc. 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "includes.h" 10 11#include "common.h" 12#include "utils/eloop.h" 13#include "wps/http_server.h" 14#include "browser.h" 15 16 17struct browser_data { 18 int success; 19}; 20 21 22static void browser_timeout(void *eloop_data, void *user_ctx) 23{ 24 wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to " 25 "complete"); 26 eloop_terminate(); 27} 28 29 30static void http_req(void *ctx, struct http_request *req) 31{ 32 struct browser_data *data = ctx; 33 struct wpabuf *resp; 34 const char *url; 35 int done = 0; 36 37 url = http_request_get_uri(req); 38 wpa_printf(MSG_INFO, "Browser response received: %s", url); 39 40 if (os_strcmp(url, "/") == 0) { 41 data->success = 1; 42 done = 1; 43 } else if (os_strncmp(url, "/osu/", 5) == 0) { 44 data->success = atoi(url + 5); 45 done = 1; 46 } 47 48 resp = wpabuf_alloc(1); 49 if (resp == NULL) { 50 http_request_deinit(req); 51 if (done) 52 eloop_terminate(); 53 return; 54 } 55 56 if (done) { 57 eloop_cancel_timeout(browser_timeout, NULL, NULL); 58 eloop_register_timeout(0, 500000, browser_timeout, &data, NULL); 59 } 60 61 http_request_send_and_deinit(req, resp); 62} 63 64 65int hs20_web_browser(const char *url) 66{ 67 struct http_server *http; 68 struct in_addr addr; 69 struct browser_data data; 70 pid_t pid; 71 72 wpa_printf(MSG_INFO, "Launching Android browser to %s", url); 73 74 os_memset(&data, 0, sizeof(data)); 75 76 if (eloop_init() < 0) { 77 wpa_printf(MSG_ERROR, "eloop_init failed"); 78 return -1; 79 } 80 addr.s_addr = htonl((127 << 24) | 1); 81 http = http_server_init(&addr, 12345, http_req, &data); 82 if (http == NULL) { 83 wpa_printf(MSG_ERROR, "http_server_init failed"); 84 eloop_destroy(); 85 return -1; 86 } 87 88 pid = fork(); 89 if (pid < 0) { 90 wpa_printf(MSG_ERROR, "fork: %s", strerror(errno)); 91 http_server_deinit(http); 92 eloop_destroy(); 93 return -1; 94 } 95 96 if (pid == 0) { 97 /* run the external command in the child process */ 98 char *argv[7]; 99 100 argv[0] = "browser-android"; 101 argv[1] = "start"; 102 argv[2] = "-a"; 103 argv[3] = "android.intent.action.VIEW"; 104 argv[4] = "-d"; 105 argv[5] = (void *) url; 106 argv[6] = NULL; 107 108 execv("/system/bin/am", argv); 109 wpa_printf(MSG_ERROR, "execv: %s", strerror(errno)); 110 exit(0); 111 return -1; 112 } 113 114 eloop_register_timeout(30, 0, browser_timeout, &data, NULL); 115 eloop_run(); 116 eloop_cancel_timeout(browser_timeout, &data, NULL); 117 http_server_deinit(http); 118 eloop_destroy(); 119 120 wpa_printf(MSG_INFO, "Closing Android browser"); 121 if (system("/system/bin/input keyevent KEYCODE_HOME") != 0) { 122 wpa_printf(MSG_INFO, "Failed to inject keyevent"); 123 } 124 125 return data.success; 126} 127