]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/utils/browser-wpadebug.c
HS 2.0R2: Add wrapper for wpadebug browser on Android
[thirdparty/hostap.git] / src / utils / browser-wpadebug.c
CommitLineData
db3ca20e
JM
1/*
2 * Hotspot 2.0 client - Web browser using wpadebug on Android
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(100);
49 if (resp == NULL) {
50 http_request_deinit(req);
51 if (done)
52 eloop_terminate();
53 return;
54 }
55 wpabuf_put_str(resp, "User input completed");
56
57 if (done) {
58 eloop_cancel_timeout(browser_timeout, NULL, NULL);
59 eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
60 }
61
62 http_request_send_and_deinit(req, resp);
63}
64
65
66int hs20_web_browser(const char *url)
67{
68 char cmd[2000];
69 int ret;
70 struct http_server *http;
71 struct in_addr addr;
72 struct browser_data data;
73
74 wpa_printf(MSG_INFO, "Launching wpadebug browser to %s", url);
75
76 os_memset(&data, 0, sizeof(data));
77
78 ret = os_snprintf(cmd, sizeof(cmd),
79 "am start -a android.action.MAIN "
80 "-c android.intent.category.LAUNCHER "
81 "-n w1.fi.wpadebug/.WpaWebViewActivity "
82 "-e w1.fi.wpadebug.URL '%s'", url);
83 if (ret < 0 || (size_t) ret >= sizeof(cmd)) {
84 wpa_printf(MSG_ERROR, "Too long URL");
85 return -1;
86 }
87
88 if (eloop_init() < 0) {
89 wpa_printf(MSG_ERROR, "eloop_init failed");
90 return -1;
91 }
92 addr.s_addr = htonl((127 << 24) | 1);
93 http = http_server_init(&addr, 12345, http_req, &data);
94 if (http == NULL) {
95 wpa_printf(MSG_ERROR, "http_server_init failed");
96 eloop_destroy();
97 return -1;
98 }
99
100 if (system(cmd) != 0) {
101 wpa_printf(MSG_INFO, "Failed to launch wpadebug browser");
102 eloop_cancel_timeout(browser_timeout, NULL, NULL);
103 http_server_deinit(http);
104 eloop_destroy();
105 return -1;
106 }
107
108 eloop_register_timeout(300, 0, browser_timeout, &data, NULL);
109 eloop_run();
110 eloop_cancel_timeout(browser_timeout, &data, NULL);
111 http_server_deinit(http);
112 eloop_destroy();
113
114 wpa_printf(MSG_INFO, "Closing Android browser");
115 if (system("am start -a android.action.MAIN "
116 "-c android.intent.category.LAUNCHER "
117 "-n w1.fi.wpadebug/.WpaWebViewActivity "
118 "-e w1.fi.wpadebug.URL FINISH") != 0) {
119 wpa_printf(MSG_INFO, "Failed to close wpadebug browser");
120 }
121
122 return data.success;
123}