xref: /freebsd/contrib/wpa/wpa_supplicant/binder/binder_manager.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/IServiceManager.h>
11*780fb4a2SCy Schubert 
12*780fb4a2SCy Schubert #include "binder_constants.h"
13*780fb4a2SCy Schubert #include "binder_manager.h"
14*780fb4a2SCy Schubert 
15*780fb4a2SCy Schubert extern "C" {
16*780fb4a2SCy Schubert #include "utils/common.h"
17*780fb4a2SCy Schubert #include "utils/includes.h"
18*780fb4a2SCy Schubert }
19*780fb4a2SCy Schubert 
20*780fb4a2SCy Schubert namespace wpa_supplicant_binder {
21*780fb4a2SCy Schubert 
22*780fb4a2SCy Schubert BinderManager *BinderManager::instance_ = NULL;
23*780fb4a2SCy Schubert 
getInstance()24*780fb4a2SCy Schubert BinderManager *BinderManager::getInstance()
25*780fb4a2SCy Schubert {
26*780fb4a2SCy Schubert 	if (!instance_)
27*780fb4a2SCy Schubert 		instance_ = new BinderManager();
28*780fb4a2SCy Schubert 	return instance_;
29*780fb4a2SCy Schubert }
30*780fb4a2SCy Schubert 
destroyInstance()31*780fb4a2SCy Schubert void BinderManager::destroyInstance()
32*780fb4a2SCy Schubert {
33*780fb4a2SCy Schubert 	if (instance_)
34*780fb4a2SCy Schubert 		delete instance_;
35*780fb4a2SCy Schubert 	instance_ = NULL;
36*780fb4a2SCy Schubert }
37*780fb4a2SCy Schubert 
registerBinderService(struct wpa_global * global)38*780fb4a2SCy Schubert int BinderManager::registerBinderService(struct wpa_global *global)
39*780fb4a2SCy Schubert {
40*780fb4a2SCy Schubert 	/* Create the main binder service object and register with
41*780fb4a2SCy Schubert 	 * system service manager. */
42*780fb4a2SCy Schubert 	supplicant_object_ = new Supplicant(global);
43*780fb4a2SCy Schubert 	android::String16 service_name(binder_constants::kServiceName);
44*780fb4a2SCy Schubert 	android::defaultServiceManager()->addService(
45*780fb4a2SCy Schubert 	    service_name, android::IInterface::asBinder(supplicant_object_));
46*780fb4a2SCy Schubert 	return 0;
47*780fb4a2SCy Schubert }
48*780fb4a2SCy Schubert 
registerInterface(struct wpa_supplicant * wpa_s)49*780fb4a2SCy Schubert int BinderManager::registerInterface(struct wpa_supplicant *wpa_s)
50*780fb4a2SCy Schubert {
51*780fb4a2SCy Schubert 	if (!wpa_s)
52*780fb4a2SCy Schubert 		return 1;
53*780fb4a2SCy Schubert 
54*780fb4a2SCy Schubert 	/* Using the corresponding wpa_supplicant pointer as key to our
55*780fb4a2SCy Schubert 	 * object map. */
56*780fb4a2SCy Schubert 	const void *iface_key = wpa_s;
57*780fb4a2SCy Schubert 
58*780fb4a2SCy Schubert 	/* Return failure if we already have an object for that iface_key. */
59*780fb4a2SCy Schubert 	if (iface_object_map_.find(iface_key) != iface_object_map_.end())
60*780fb4a2SCy Schubert 		return 1;
61*780fb4a2SCy Schubert 
62*780fb4a2SCy Schubert 	iface_object_map_[iface_key] = new Iface(wpa_s);
63*780fb4a2SCy Schubert 	if (!iface_object_map_[iface_key].get())
64*780fb4a2SCy Schubert 		return 1;
65*780fb4a2SCy Schubert 
66*780fb4a2SCy Schubert 	wpa_s->binder_object_key = iface_key;
67*780fb4a2SCy Schubert 
68*780fb4a2SCy Schubert 	return 0;
69*780fb4a2SCy Schubert }
70*780fb4a2SCy Schubert 
unregisterInterface(struct wpa_supplicant * wpa_s)71*780fb4a2SCy Schubert int BinderManager::unregisterInterface(struct wpa_supplicant *wpa_s)
72*780fb4a2SCy Schubert {
73*780fb4a2SCy Schubert 	if (!wpa_s || !wpa_s->binder_object_key)
74*780fb4a2SCy Schubert 		return 1;
75*780fb4a2SCy Schubert 
76*780fb4a2SCy Schubert 	const void *iface_key = wpa_s;
77*780fb4a2SCy Schubert 	if (iface_object_map_.find(iface_key) == iface_object_map_.end())
78*780fb4a2SCy Schubert 		return 1;
79*780fb4a2SCy Schubert 
80*780fb4a2SCy Schubert 	/* Delete the corresponding iface object from our map. */
81*780fb4a2SCy Schubert 	iface_object_map_.erase(iface_key);
82*780fb4a2SCy Schubert 	wpa_s->binder_object_key = NULL;
83*780fb4a2SCy Schubert 	return 0;
84*780fb4a2SCy Schubert }
85*780fb4a2SCy Schubert 
getIfaceBinderObjectByKey(const void * iface_object_key,android::sp<fi::w1::wpa_supplicant::IIface> * iface_object)86*780fb4a2SCy Schubert int BinderManager::getIfaceBinderObjectByKey(
87*780fb4a2SCy Schubert     const void *iface_object_key,
88*780fb4a2SCy Schubert     android::sp<fi::w1::wpa_supplicant::IIface> *iface_object)
89*780fb4a2SCy Schubert {
90*780fb4a2SCy Schubert 	if (!iface_object_key || !iface_object)
91*780fb4a2SCy Schubert 		return 1;
92*780fb4a2SCy Schubert 
93*780fb4a2SCy Schubert 	if (iface_object_map_.find(iface_object_key) == iface_object_map_.end())
94*780fb4a2SCy Schubert 		return 1;
95*780fb4a2SCy Schubert 
96*780fb4a2SCy Schubert 	*iface_object = iface_object_map_[iface_object_key];
97*780fb4a2SCy Schubert 	return 0;
98*780fb4a2SCy Schubert }
99*780fb4a2SCy Schubert 
100*780fb4a2SCy Schubert } /* namespace wpa_supplicant_binder */
101