1 #define LOG_TAG "hwservicemanager" 2 #include "HidlService.h" 3 4 #include <android-base/logging.h> 5 #include <sstream> 6 7 namespace android { 8 namespace hidl { 9 namespace manager { 10 namespace V1_0 { 11 namespace implementation { 12 13 HidlService::HidlService( 14 const std::string &interfaceName, 15 const std::string &instanceName, 16 const sp<IBase> &service, 17 pid_t pid) 18 : mInterfaceName(interfaceName), 19 mInstanceName(instanceName), 20 mService(service), 21 mPid(pid) 22 {} 23 24 sp<IBase> HidlService::getService() const { 25 return mService; 26 } 27 void HidlService::setService(sp<IBase> service, pid_t pid) { 28 mService = service; 29 mPid = pid; 30 31 sendRegistrationNotifications(); 32 } 33 34 pid_t HidlService::getPid() const { 35 return mPid; 36 } 37 const std::string &HidlService::getInterfaceName() const { 38 return mInterfaceName; 39 } 40 const std::string &HidlService::getInstanceName() const { 41 return mInstanceName; 42 } 43 44 void HidlService::addListener(const sp<IServiceNotification> &listener) { 45 if (mService != nullptr) { 46 auto ret = listener->onRegistration( 47 mInterfaceName, mInstanceName, true /* preexisting */); 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 } 54 } 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; 71 } 72 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 81 std::string HidlService::string() const { 82 std::stringstream ss; 83 ss << mInterfaceName << "/" << mInstanceName; 84 return ss.str(); 85 } 86 87 void HidlService::sendRegistrationNotifications() { 88 if (mListeners.size() == 0 || mService == nullptr) { 89 return; 90 } 91 92 hidl_string iface = mInterfaceName; 93 hidl_string name = mInstanceName; 94 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 { 100 LOG(ERROR) << "Dropping registration callback for " << iface << "/" << name 101 << ": transport error."; 102 it = mListeners.erase(it); 103 } 104 } 105 } 106 107 } // namespace implementation 108 } // namespace V1_0 109 } // namespace manager 110 } // namespace hidl 111 } // namespace android 112