136d97821Schristos /*
236d97821Schristos * Hotspot 2.0 client - Web browser using Android browser
336d97821Schristos * Copyright (c) 2013, Qualcomm Atheros, Inc.
436d97821Schristos *
536d97821Schristos * This software may be distributed under the terms of the BSD license.
636d97821Schristos * See README for more details.
736d97821Schristos */
836d97821Schristos
936d97821Schristos #include "includes.h"
1036d97821Schristos
1136d97821Schristos #include "common.h"
1236d97821Schristos #include "utils/eloop.h"
1336d97821Schristos #include "wps/http_server.h"
1436d97821Schristos #include "browser.h"
1536d97821Schristos
1636d97821Schristos
1736d97821Schristos struct browser_data {
1836d97821Schristos int success;
1936d97821Schristos };
2036d97821Schristos
2136d97821Schristos
browser_timeout(void * eloop_data,void * user_ctx)2236d97821Schristos static void browser_timeout(void *eloop_data, void *user_ctx)
2336d97821Schristos {
2436d97821Schristos wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to "
2536d97821Schristos "complete");
2636d97821Schristos eloop_terminate();
2736d97821Schristos }
2836d97821Schristos
2936d97821Schristos
http_req(void * ctx,struct http_request * req)3036d97821Schristos static void http_req(void *ctx, struct http_request *req)
3136d97821Schristos {
3236d97821Schristos struct browser_data *data = ctx;
3336d97821Schristos struct wpabuf *resp;
3436d97821Schristos const char *url;
3536d97821Schristos int done = 0;
3636d97821Schristos
3736d97821Schristos url = http_request_get_uri(req);
3836d97821Schristos wpa_printf(MSG_INFO, "Browser response received: %s", url);
3936d97821Schristos
4036d97821Schristos if (os_strcmp(url, "/") == 0) {
4136d97821Schristos data->success = 1;
4236d97821Schristos done = 1;
4336d97821Schristos } else if (os_strncmp(url, "/osu/", 5) == 0) {
4436d97821Schristos data->success = atoi(url + 5);
4536d97821Schristos done = 1;
4636d97821Schristos }
4736d97821Schristos
4836d97821Schristos resp = wpabuf_alloc(1);
4936d97821Schristos if (resp == NULL) {
5036d97821Schristos http_request_deinit(req);
5136d97821Schristos if (done)
5236d97821Schristos eloop_terminate();
5336d97821Schristos return;
5436d97821Schristos }
5536d97821Schristos
5636d97821Schristos if (done) {
5736d97821Schristos eloop_cancel_timeout(browser_timeout, NULL, NULL);
5836d97821Schristos eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
5936d97821Schristos }
6036d97821Schristos
6136d97821Schristos http_request_send_and_deinit(req, resp);
6236d97821Schristos }
6336d97821Schristos
6436d97821Schristos
hs20_web_browser(const char * url)6536d97821Schristos int hs20_web_browser(const char *url)
6636d97821Schristos {
6736d97821Schristos struct http_server *http;
6836d97821Schristos struct in_addr addr;
6936d97821Schristos struct browser_data data;
709a53cbbeSchristos pid_t pid;
7136d97821Schristos
7236d97821Schristos wpa_printf(MSG_INFO, "Launching Android browser to %s", url);
7336d97821Schristos
7436d97821Schristos os_memset(&data, 0, sizeof(data));
7536d97821Schristos
7636d97821Schristos if (eloop_init() < 0) {
7736d97821Schristos wpa_printf(MSG_ERROR, "eloop_init failed");
7836d97821Schristos return -1;
7936d97821Schristos }
8036d97821Schristos addr.s_addr = htonl((127 << 24) | 1);
8136d97821Schristos http = http_server_init(&addr, 12345, http_req, &data);
8236d97821Schristos if (http == NULL) {
8336d97821Schristos wpa_printf(MSG_ERROR, "http_server_init failed");
8436d97821Schristos eloop_destroy();
8536d97821Schristos return -1;
8636d97821Schristos }
8736d97821Schristos
889a53cbbeSchristos pid = fork();
899a53cbbeSchristos if (pid < 0) {
909a53cbbeSchristos wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
9136d97821Schristos http_server_deinit(http);
9236d97821Schristos eloop_destroy();
9336d97821Schristos return -1;
9436d97821Schristos }
9536d97821Schristos
969a53cbbeSchristos if (pid == 0) {
979a53cbbeSchristos /* run the external command in the child process */
98*928750b6Schristos char *argv[7];
999a53cbbeSchristos
1009a53cbbeSchristos argv[0] = "browser-android";
1019a53cbbeSchristos argv[1] = "start";
1029a53cbbeSchristos argv[2] = "-a";
1039a53cbbeSchristos argv[3] = "android.intent.action.VIEW";
1049a53cbbeSchristos argv[4] = "-d";
1059a53cbbeSchristos argv[5] = (void *) url;
106*928750b6Schristos argv[6] = NULL;
1079a53cbbeSchristos
1089a53cbbeSchristos execv("/system/bin/am", argv);
1099a53cbbeSchristos wpa_printf(MSG_ERROR, "execv: %s", strerror(errno));
1109a53cbbeSchristos exit(0);
1119a53cbbeSchristos return -1;
1129a53cbbeSchristos }
1139a53cbbeSchristos
11436d97821Schristos eloop_register_timeout(30, 0, browser_timeout, &data, NULL);
11536d97821Schristos eloop_run();
11636d97821Schristos eloop_cancel_timeout(browser_timeout, &data, NULL);
11736d97821Schristos http_server_deinit(http);
11836d97821Schristos eloop_destroy();
11936d97821Schristos
12036d97821Schristos wpa_printf(MSG_INFO, "Closing Android browser");
1219a53cbbeSchristos if (system("/system/bin/input keyevent KEYCODE_HOME") != 0) {
12236d97821Schristos wpa_printf(MSG_INFO, "Failed to inject keyevent");
12336d97821Schristos }
12436d97821Schristos
12536d97821Schristos return data.success;
12636d97821Schristos }
127