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