blob: 599938d4ef304b8796967b34f5915d53211f1aef [file] [log] [blame]
Roshan Pius57ffbcf2016-09-27 09:12:46 -07001/*
2 * hidl interface for wpa_supplicant daemon
3 * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
Roshan Pius7c0ebf22016-09-20 15:11:56 -070010#include <hwbinder/IPCThreadState.h>
11#include <hwbinder/ProcessState.h>
Roshan Pius57ffbcf2016-09-27 09:12:46 -070012
13#include "hidl_manager.h"
14
15extern "C" {
16#include "hidl.h"
17#include "hidl_i.h"
18#include "utils/common.h"
19#include "utils/eloop.h"
20#include "utils/includes.h"
21}
22
Roshan Pius7c0ebf22016-09-20 15:11:56 -070023using android::hardware::ProcessState;
24using android::hardware::IPCThreadState;
25using android::hardware::wifi::supplicant::V1_0::implementation::HidlManager;
26
Roshan Pius57ffbcf2016-09-27 09:12:46 -070027void wpas_hidl_sock_handler(
28 int /* sock */, void * /* eloop_ctx */, void *sock_ctx)
29{
30 struct wpas_hidl_priv *priv = (wpas_hidl_priv *)sock_ctx;
Roshan Pius7c0ebf22016-09-20 15:11:56 -070031 wpa_printf(MSG_DEBUG, "Processing hidl events on FD %d", priv->hidl_fd);
32 IPCThreadState::self()->handlePolledCommands();
Roshan Pius57ffbcf2016-09-27 09:12:46 -070033}
34
35struct wpas_hidl_priv *wpas_hidl_init(struct wpa_global *global)
36{
37 struct wpas_hidl_priv *priv;
Roshan Pius7c0ebf22016-09-20 15:11:56 -070038 HidlManager *hidl_manager;
Roshan Pius57ffbcf2016-09-27 09:12:46 -070039
40 priv = (wpas_hidl_priv *)os_zalloc(sizeof(*priv));
41 if (!priv)
42 return NULL;
43 priv->global = global;
44
45 wpa_printf(MSG_DEBUG, "Initing hidl control");
46
Roshan Pius7c0ebf22016-09-20 15:11:56 -070047 ProcessState::self()->setThreadPoolMaxThreadCount(0);
48 IPCThreadState::self()->disableBackgroundScheduling(true);
49 IPCThreadState::self()->setupPolling(&priv->hidl_fd);
Roshan Pius57ffbcf2016-09-27 09:12:46 -070050 if (priv->hidl_fd < 0)
51 goto err;
52
Roshan Pius7c0ebf22016-09-20 15:11:56 -070053 wpa_printf(MSG_INFO, "Processing hidl events on FD %d", priv->hidl_fd);
54 // Look for read events from the hidl socket in the eloop.
Roshan Pius57ffbcf2016-09-27 09:12:46 -070055 if (eloop_register_read_sock(
56 priv->hidl_fd, wpas_hidl_sock_handler, global, priv) < 0)
57 goto err;
58
Roshan Pius7c0ebf22016-09-20 15:11:56 -070059 hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -070060 if (!hidl_manager)
61 goto err;
62 hidl_manager->registerHidlService(global);
Roshan Pius7c0ebf22016-09-20 15:11:56 -070063 // We may not need to store this hidl manager reference in the
64 // global data strucure because we've made it a singleton class.
Roshan Pius57ffbcf2016-09-27 09:12:46 -070065 priv->hidl_manager = (void *)hidl_manager;
66
67 return priv;
68err:
69 wpas_hidl_deinit(priv);
70 return NULL;
71}
72
73void wpas_hidl_deinit(struct wpas_hidl_priv *priv)
74{
75 if (!priv)
76 return;
77
78 wpa_printf(MSG_DEBUG, "Deiniting hidl control");
79
Roshan Pius7c0ebf22016-09-20 15:11:56 -070080 HidlManager::destroyInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -070081 eloop_unregister_read_sock(priv->hidl_fd);
Roshan Pius7c0ebf22016-09-20 15:11:56 -070082 IPCThreadState::shutdown();
Roshan Pius57ffbcf2016-09-27 09:12:46 -070083 os_free(priv);
84}
85
86int wpas_hidl_register_interface(struct wpa_supplicant *wpa_s)
87{
88 if (!wpa_s || !wpa_s->global->hidl)
89 return 1;
90
91 wpa_printf(
92 MSG_DEBUG, "Registering interface to hidl control: %s",
93 wpa_s->ifname);
94
Roshan Pius7c0ebf22016-09-20 15:11:56 -070095 HidlManager *hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -070096 if (!hidl_manager)
97 return 1;
98
99 return hidl_manager->registerInterface(wpa_s);
100}
101
102int wpas_hidl_unregister_interface(struct wpa_supplicant *wpa_s)
103{
104 if (!wpa_s || !wpa_s->global->hidl)
105 return 1;
106
107 wpa_printf(
108 MSG_DEBUG, "Deregistering interface from hidl control: %s",
109 wpa_s->ifname);
110
Roshan Pius7c0ebf22016-09-20 15:11:56 -0700111 HidlManager *hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -0700112 if (!hidl_manager)
113 return 1;
114
115 return hidl_manager->unregisterInterface(wpa_s);
116}
117
118int wpas_hidl_register_network(
119 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
120{
121 if (!wpa_s || !wpa_s->global->hidl || !ssid)
122 return 1;
123
124 wpa_printf(
125 MSG_DEBUG, "Registering network to hidl control: %d", ssid->id);
126
Roshan Pius7c0ebf22016-09-20 15:11:56 -0700127 HidlManager *hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -0700128 if (!hidl_manager)
129 return 1;
130
131 return hidl_manager->registerNetwork(wpa_s, ssid);
132}
133
134int wpas_hidl_unregister_network(
135 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
136{
137 if (!wpa_s || !wpa_s->global->hidl || !ssid)
138 return 1;
139
140 wpa_printf(
Roshan Pius7c0ebf22016-09-20 15:11:56 -0700141 MSG_DEBUG, "Deregistering network from hidl control: %d", ssid->id);
Roshan Pius57ffbcf2016-09-27 09:12:46 -0700142
Roshan Pius7c0ebf22016-09-20 15:11:56 -0700143 HidlManager *hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -0700144 if (!hidl_manager)
145 return 1;
146
147 return hidl_manager->unregisterNetwork(wpa_s, ssid);
148}
149
150int wpas_hidl_notify_state_changed(struct wpa_supplicant *wpa_s)
151{
152 if (!wpa_s || !wpa_s->global->hidl)
153 return 1;
154
155 wpa_printf(
156 MSG_DEBUG, "Notifying state change event to hidl control: %d",
157 wpa_s->wpa_state);
158
Roshan Pius7c0ebf22016-09-20 15:11:56 -0700159 HidlManager *hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -0700160 if (!hidl_manager)
161 return 1;
162
163 return hidl_manager->notifyStateChange(wpa_s);
164}
165
166int wpas_hidl_notify_network_request(
167 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
168 enum wpa_ctrl_req_type rtype, const char *default_txt)
169{
170 if (!wpa_s || !wpa_s->global->hidl || !ssid)
171 return 1;
172
173 wpa_printf(
174 MSG_DEBUG, "Notifying network request to hidl control: %d",
175 ssid->id);
176
Roshan Pius7c0ebf22016-09-20 15:11:56 -0700177 HidlManager *hidl_manager = HidlManager::getInstance();
Roshan Pius57ffbcf2016-09-27 09:12:46 -0700178 if (!hidl_manager)
179 return 1;
180
181 return hidl_manager->notifyNetworkRequest(
182 wpa_s, ssid, rtype, default_txt);
183}