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