-
Notifications
You must be signed in to change notification settings - Fork 0
/
HidlService.cpp
111 lines (94 loc) · 2.78 KB
/
HidlService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#define LOG_TAG "hwservicemanager"
#include "HidlService.h"
#include <android-base/logging.h>
#include <sstream>
namespace android {
namespace hidl {
namespace manager {
namespace V1_0 {
namespace implementation {
HidlService::HidlService(
const std::string &interfaceName,
const std::string &instanceName,
const sp<IBase> &service,
pid_t pid)
: mInterfaceName(interfaceName),
mInstanceName(instanceName),
mService(service),
mPid(pid)
{}
sp<IBase> HidlService::getService() const {
return mService;
}
void HidlService::setService(sp<IBase> service, pid_t pid) {
mService = service;
mPid = pid;
sendRegistrationNotifications();
}
pid_t HidlService::getPid() const {
return mPid;
}
const std::string &HidlService::getInterfaceName() const {
return mInterfaceName;
}
const std::string &HidlService::getInstanceName() const {
return mInstanceName;
}
void HidlService::addListener(const sp<IServiceNotification> &listener) {
if (mService != nullptr) {
auto ret = listener->onRegistration(
mInterfaceName, mInstanceName, true /* preexisting */);
if (!ret.isOk()) {
LOG(ERROR) << "Not adding listener for " << mInterfaceName << "/"
<< mInstanceName << ": transport error when sending "
<< "notification for already registered instance.";
return;
}
}
mListeners.push_back(listener);
}
bool HidlService::removeListener(const wp<IBase>& listener) {
bool found = false;
for (auto it = mListeners.begin(); it != mListeners.end();) {
if (*it == listener) {
it = mListeners.erase(it);
found = true;
} else {
++it;
}
}
return found;
}
void HidlService::registerPassthroughClient(pid_t pid) {
mPassthroughClients.insert(pid);
}
const std::set<pid_t> &HidlService::getPassthroughClients() const {
return mPassthroughClients;
}
std::string HidlService::string() const {
std::stringstream ss;
ss << mInterfaceName << "/" << mInstanceName;
return ss.str();
}
void HidlService::sendRegistrationNotifications() {
if (mListeners.size() == 0 || mService == nullptr) {
return;
}
hidl_string iface = mInterfaceName;
hidl_string name = mInstanceName;
for (auto it = mListeners.begin(); it != mListeners.end();) {
auto ret = (*it)->onRegistration(iface, name, false /* preexisting */);
if (ret.isOk()) {
++it;
} else {
LOG(ERROR) << "Dropping registration callback for " << iface << "/" << name
<< ": transport error.";
it = mListeners.erase(it);
}
}
}
} // namespace implementation
} // namespace V1_0
} // namespace manager
} // namespace hidl
} // namespace android