blob: 13717cf199977930bacdb00a6cafa350117f6a6a [file] [log] [blame]
Steven Moreland2173d3c2016-11-09 15:00:58 -08001#define LOG_TAG "hwservicemanager"
2#include "HidlService.h"
3
4#include <android-base/logging.h>
Steven Moreland2173d3c2016-11-09 15:00:58 -08005#include <sstream>
6
7namespace android {
8namespace hidl {
9namespace manager {
10namespace V1_0 {
11namespace implementation {
12
Steven Moreland2173d3c2016-11-09 15:00:58 -080013HidlService::HidlService(
Steven Morelandd544cf62017-01-04 15:24:32 -080014 const std::string &interfaceName,
15 const std::string &instanceName,
Steven Morelandcdf94722017-03-21 12:16:31 -070016 const sp<IBase> &service,
17 pid_t pid)
Steven Morelandd544cf62017-01-04 15:24:32 -080018: mInterfaceName(interfaceName),
19 mInstanceName(instanceName),
Steven Morelandcdf94722017-03-21 12:16:31 -070020 mService(service),
21 mPid(pid)
Steven Moreland2173d3c2016-11-09 15:00:58 -080022{}
23
Yifan Hongb3a90f02016-11-23 12:58:04 -080024sp<IBase> HidlService::getService() const {
Steven Moreland2173d3c2016-11-09 15:00:58 -080025 return mService;
26}
Steven Morelandcdf94722017-03-21 12:16:31 -070027void HidlService::setService(sp<IBase> service, pid_t pid) {
Steven Moreland2173d3c2016-11-09 15:00:58 -080028 mService = service;
Steven Morelandcdf94722017-03-21 12:16:31 -070029 mPid = pid;
Steven Moreland2173d3c2016-11-09 15:00:58 -080030
31 sendRegistrationNotifications();
32}
Steven Morelandcdf94722017-03-21 12:16:31 -070033
34pid_t HidlService::getPid() const {
35 return mPid;
36}
Steven Morelandd544cf62017-01-04 15:24:32 -080037const std::string &HidlService::getInterfaceName() const {
38 return mInterfaceName;
Steven Moreland2173d3c2016-11-09 15:00:58 -080039}
Steven Morelandd544cf62017-01-04 15:24:32 -080040const std::string &HidlService::getInstanceName() const {
Steven Moreland2173d3c2016-11-09 15:00:58 -080041 return mInstanceName;
42}
Steven Moreland2173d3c2016-11-09 15:00:58 -080043
44void HidlService::addListener(const sp<IServiceNotification> &listener) {
Steven Moreland2173d3c2016-11-09 15:00:58 -080045 if (mService != nullptr) {
Steven Moreland30acc222017-01-26 11:44:38 -080046 auto ret = listener->onRegistration(
47 mInterfaceName, mInstanceName, true /* preexisting */);
Martijn Coenen7fafc142017-03-06 16:17:51 +010048 if (!ret.isOk()) {
49 LOG(ERROR) << "Not adding listener for " << mInterfaceName << "/"
50 << mInstanceName << ": transport error when sending "
51 << "notification for already registered instance.";
52 return;
53 }
Steven Moreland2173d3c2016-11-09 15:00:58 -080054 }
Martijn Coenen7fafc142017-03-06 16:17:51 +010055 mListeners.push_back(listener);
56}
57
58bool HidlService::removeListener(const wp<IBase>& listener) {
59 bool found = false;
60
61 for (auto it = mListeners.begin(); it != mListeners.end();) {
62 if (*it == listener) {
63 it = mListeners.erase(it);
64 found = true;
65 } else {
66 ++it;
67 }
68 }
69
70 return found;
Steven Moreland2173d3c2016-11-09 15:00:58 -080071}
72
Yifan Hongee531a82017-02-03 15:10:18 -080073void HidlService::registerPassthroughClient(pid_t pid) {
74 mPassthroughClients.insert(pid);
75}
76
77const std::set<pid_t> &HidlService::getPassthroughClients() const {
78 return mPassthroughClients;
79}
80
Steven Moreland2173d3c2016-11-09 15:00:58 -080081std::string HidlService::string() const {
82 std::stringstream ss;
Steven Morelandd544cf62017-01-04 15:24:32 -080083 ss << mInterfaceName << "/" << mInstanceName;
Steven Moreland2173d3c2016-11-09 15:00:58 -080084 return ss.str();
85}
86
Martijn Coenen72103a02017-01-18 16:06:34 +010087void HidlService::sendRegistrationNotifications() {
Steven Moreland2173d3c2016-11-09 15:00:58 -080088 if (mListeners.size() == 0 || mService == nullptr) {
89 return;
90 }
91
Steven Morelandd544cf62017-01-04 15:24:32 -080092 hidl_string iface = mInterfaceName;
Steven Moreland2173d3c2016-11-09 15:00:58 -080093 hidl_string name = mInstanceName;
94
Martijn Coenen72103a02017-01-18 16:06:34 +010095 for (auto it = mListeners.begin(); it != mListeners.end();) {
96 auto ret = (*it)->onRegistration(iface, name, false /* preexisting */);
97 if (ret.isOk()) {
98 ++it;
99 } else {
Martijn Coenen7fafc142017-03-06 16:17:51 +0100100 LOG(ERROR) << "Dropping registration callback for " << iface << "/" << name
101 << ": transport error.";
Martijn Coenen72103a02017-01-18 16:06:34 +0100102 it = mListeners.erase(it);
103 }
Steven Moreland2173d3c2016-11-09 15:00:58 -0800104 }
105}
106
Steven Moreland2173d3c2016-11-09 15:00:58 -0800107} // namespace implementation
108} // namespace V1_0
109} // namespace manager
110} // namespace hidl
Yifan Hongb3a90f02016-11-23 12:58:04 -0800111} // namespace android