Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 1 | #define LOG_TAG "hwservicemanager" |
| 2 | #include "HidlService.h" |
| 3 | |
| 4 | #include <android-base/logging.h> |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 5 | #include <sstream> |
| 6 | |
| 7 | namespace android { |
| 8 | namespace hidl { |
| 9 | namespace manager { |
| 10 | namespace V1_0 { |
| 11 | namespace implementation { |
| 12 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 13 | HidlService::HidlService( |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 14 | const std::string &interfaceName, |
| 15 | const std::string &instanceName, |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 16 | const sp<IBase> &service, |
| 17 | pid_t pid) |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 18 | : mInterfaceName(interfaceName), |
| 19 | mInstanceName(instanceName), |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 20 | mService(service), |
| 21 | mPid(pid) |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 22 | {} |
| 23 | |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 24 | sp<IBase> HidlService::getService() const { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 25 | return mService; |
| 26 | } |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 27 | void HidlService::setService(sp<IBase> service, pid_t pid) { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 28 | mService = service; |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 29 | mPid = pid; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 30 | |
| 31 | sendRegistrationNotifications(); |
| 32 | } |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 33 | |
| 34 | pid_t HidlService::getPid() const { |
| 35 | return mPid; |
| 36 | } |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 37 | const std::string &HidlService::getInterfaceName() const { |
| 38 | return mInterfaceName; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 39 | } |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 40 | const std::string &HidlService::getInstanceName() const { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 41 | return mInstanceName; |
| 42 | } |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 43 | |
| 44 | void HidlService::addListener(const sp<IServiceNotification> &listener) { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 45 | if (mService != nullptr) { |
Steven Moreland | 30acc22 | 2017-01-26 11:44:38 -0800 | [diff] [blame] | 46 | auto ret = listener->onRegistration( |
| 47 | mInterfaceName, mInstanceName, true /* preexisting */); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 48 | 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 Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 54 | } |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 55 | mListeners.push_back(listener); |
| 56 | } |
| 57 | |
| 58 | bool 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 Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 73 | void HidlService::registerPassthroughClient(pid_t pid) { |
| 74 | mPassthroughClients.insert(pid); |
| 75 | } |
| 76 | |
| 77 | const std::set<pid_t> &HidlService::getPassthroughClients() const { |
| 78 | return mPassthroughClients; |
| 79 | } |
| 80 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 81 | std::string HidlService::string() const { |
| 82 | std::stringstream ss; |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 83 | ss << mInterfaceName << "/" << mInstanceName; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 84 | return ss.str(); |
| 85 | } |
| 86 | |
Martijn Coenen | 72103a0 | 2017-01-18 16:06:34 +0100 | [diff] [blame] | 87 | void HidlService::sendRegistrationNotifications() { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 88 | if (mListeners.size() == 0 || mService == nullptr) { |
| 89 | return; |
| 90 | } |
| 91 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 92 | hidl_string iface = mInterfaceName; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 93 | hidl_string name = mInstanceName; |
| 94 | |
Martijn Coenen | 72103a0 | 2017-01-18 16:06:34 +0100 | [diff] [blame] | 95 | 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 Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 100 | LOG(ERROR) << "Dropping registration callback for " << iface << "/" << name |
| 101 | << ": transport error."; |
Martijn Coenen | 72103a0 | 2017-01-18 16:06:34 +0100 | [diff] [blame] | 102 | it = mListeners.erase(it); |
| 103 | } |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 107 | } // namespace implementation |
| 108 | } // namespace V1_0 |
| 109 | } // namespace manager |
| 110 | } // namespace hidl |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 111 | } // namespace android |