xref: /freebsd/contrib/wpa/wpa_supplicant/binder/binder.cpp (revision 780fb4a2fa9a9aee5ac48a60b790f567c0dc13e9)
1*780fb4a2SCy Schubert /*
2*780fb4a2SCy Schubert  * binder interface for wpa_supplicant daemon
3*780fb4a2SCy Schubert  * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4*780fb4a2SCy Schubert  * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
5*780fb4a2SCy Schubert  *
6*780fb4a2SCy Schubert  * This software may be distributed under the terms of the BSD license.
7*780fb4a2SCy Schubert  * See README for more details.
8*780fb4a2SCy Schubert  */
9*780fb4a2SCy Schubert 
10*780fb4a2SCy Schubert #include <binder/IPCThreadState.h>
11*780fb4a2SCy Schubert #include <binder/IServiceManager.h>
12*780fb4a2SCy Schubert #include <binder/ProcessState.h>
13*780fb4a2SCy Schubert 
14*780fb4a2SCy Schubert #include "binder_manager.h"
15*780fb4a2SCy Schubert 
16*780fb4a2SCy Schubert extern "C" {
17*780fb4a2SCy Schubert #include "binder.h"
18*780fb4a2SCy Schubert #include "binder_i.h"
19*780fb4a2SCy Schubert #include "utils/common.h"
20*780fb4a2SCy Schubert #include "utils/eloop.h"
21*780fb4a2SCy Schubert #include "utils/includes.h"
22*780fb4a2SCy Schubert }
23*780fb4a2SCy Schubert 
wpas_binder_sock_handler(int sock,void * eloop_ctx,void * sock_ctx)24*780fb4a2SCy Schubert void wpas_binder_sock_handler(int sock, void *eloop_ctx, void *sock_ctx)
25*780fb4a2SCy Schubert {
26*780fb4a2SCy Schubert 	struct wpa_global *global = (wpa_global *)eloop_ctx;
27*780fb4a2SCy Schubert 	struct wpas_binder_priv *priv = (wpas_binder_priv *)sock_ctx;
28*780fb4a2SCy Schubert 
29*780fb4a2SCy Schubert 	wpa_printf(
30*780fb4a2SCy Schubert 	    MSG_DEBUG, "Processing binder events on FD %d", priv->binder_fd);
31*780fb4a2SCy Schubert 	android::IPCThreadState::self()->handlePolledCommands();
32*780fb4a2SCy Schubert }
33*780fb4a2SCy Schubert 
wpas_binder_init(struct wpa_global * global)34*780fb4a2SCy Schubert struct wpas_binder_priv *wpas_binder_init(struct wpa_global *global)
35*780fb4a2SCy Schubert {
36*780fb4a2SCy Schubert 	struct wpas_binder_priv *priv;
37*780fb4a2SCy Schubert 	wpa_supplicant_binder::BinderManager *binder_manager;
38*780fb4a2SCy Schubert 
39*780fb4a2SCy Schubert 	priv = (wpas_binder_priv *)os_zalloc(sizeof(*priv));
40*780fb4a2SCy Schubert 	if (!priv)
41*780fb4a2SCy Schubert 		return NULL;
42*780fb4a2SCy Schubert 	priv->global = global;
43*780fb4a2SCy Schubert 
44*780fb4a2SCy Schubert 	android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
45*780fb4a2SCy Schubert 	android::IPCThreadState::self()->disableBackgroundScheduling(true);
46*780fb4a2SCy Schubert 	android::IPCThreadState::self()->setupPolling(&priv->binder_fd);
47*780fb4a2SCy Schubert 	wpa_printf(MSG_INFO, "Process binder events on FD %d", priv->binder_fd);
48*780fb4a2SCy Schubert 	if (priv->binder_fd < 0)
49*780fb4a2SCy Schubert 		goto err;
50*780fb4a2SCy Schubert 	/* Look for read events from the binder socket in the eloop. */
51*780fb4a2SCy Schubert 	if (eloop_register_read_sock(
52*780fb4a2SCy Schubert 		priv->binder_fd, wpas_binder_sock_handler, global, priv) < 0)
53*780fb4a2SCy Schubert 		goto err;
54*780fb4a2SCy Schubert 
55*780fb4a2SCy Schubert 	binder_manager = wpa_supplicant_binder::BinderManager::getInstance();
56*780fb4a2SCy Schubert 	if (!binder_manager)
57*780fb4a2SCy Schubert 		goto err;
58*780fb4a2SCy Schubert 	binder_manager->registerBinderService(global);
59*780fb4a2SCy Schubert 	/* We may not need to store this binder manager reference in the
60*780fb4a2SCy Schubert 	 * global data strucure because we've made it a singleton class. */
61*780fb4a2SCy Schubert 	priv->binder_manager = (void *)binder_manager;
62*780fb4a2SCy Schubert 
63*780fb4a2SCy Schubert 	return priv;
64*780fb4a2SCy Schubert 
65*780fb4a2SCy Schubert err:
66*780fb4a2SCy Schubert 	wpas_binder_deinit(priv);
67*780fb4a2SCy Schubert 	return NULL;
68*780fb4a2SCy Schubert }
69*780fb4a2SCy Schubert 
wpas_binder_deinit(struct wpas_binder_priv * priv)70*780fb4a2SCy Schubert void wpas_binder_deinit(struct wpas_binder_priv *priv)
71*780fb4a2SCy Schubert {
72*780fb4a2SCy Schubert 	if (!priv)
73*780fb4a2SCy Schubert 		return;
74*780fb4a2SCy Schubert 
75*780fb4a2SCy Schubert 	wpa_supplicant_binder::BinderManager::destroyInstance();
76*780fb4a2SCy Schubert 	eloop_unregister_read_sock(priv->binder_fd);
77*780fb4a2SCy Schubert 	android::IPCThreadState::shutdown();
78*780fb4a2SCy Schubert }
79*780fb4a2SCy Schubert 
wpas_binder_register_interface(struct wpa_supplicant * wpa_s)80*780fb4a2SCy Schubert int wpas_binder_register_interface(struct wpa_supplicant *wpa_s)
81*780fb4a2SCy Schubert {
82*780fb4a2SCy Schubert 	if (!wpa_s->global->binder)
83*780fb4a2SCy Schubert 		return 1;
84*780fb4a2SCy Schubert 
85*780fb4a2SCy Schubert 	wpa_supplicant_binder::BinderManager *binder_manager =
86*780fb4a2SCy Schubert 	    wpa_supplicant_binder::BinderManager::getInstance();
87*780fb4a2SCy Schubert 	if (!binder_manager)
88*780fb4a2SCy Schubert 		return 1;
89*780fb4a2SCy Schubert 
90*780fb4a2SCy Schubert 	return binder_manager->registerInterface(wpa_s);
91*780fb4a2SCy Schubert }
92*780fb4a2SCy Schubert 
wpas_binder_unregister_interface(struct wpa_supplicant * wpa_s)93*780fb4a2SCy Schubert int wpas_binder_unregister_interface(struct wpa_supplicant *wpa_s)
94*780fb4a2SCy Schubert {
95*780fb4a2SCy Schubert 	if (!wpa_s->global->binder)
96*780fb4a2SCy Schubert 		return 1;
97*780fb4a2SCy Schubert 
98*780fb4a2SCy Schubert 	wpa_supplicant_binder::BinderManager *binder_manager =
99*780fb4a2SCy Schubert 	    wpa_supplicant_binder::BinderManager::getInstance();
100*780fb4a2SCy Schubert 	if (!binder_manager)
101*780fb4a2SCy Schubert 		return 1;
102*780fb4a2SCy Schubert 
103*780fb4a2SCy Schubert 	return binder_manager->unregisterInterface(wpa_s);
104*780fb4a2SCy Schubert }
105