Home | History | Annotate | Download | only in hwservicemanager
      1 #ifndef ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
      2 #define ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
      3 
      4 #include <android/hidl/manager/1.1/IServiceManager.h>
      5 #include <hidl/Status.h>
      6 #include <hidl/MQDescriptor.h>
      7 #include <map>
      8 
      9 #include "AccessControl.h"
     10 #include "HidlService.h"
     11 
     12 namespace android {
     13 namespace hidl {
     14 namespace manager {
     15 namespace implementation {
     16 
     17 using ::android::hardware::hidl_death_recipient;
     18 using ::android::hardware::hidl_vec;
     19 using ::android::hardware::hidl_string;
     20 using ::android::hardware::Return;
     21 using ::android::hardware::Void;
     22 using ::android::hidl::base::V1_0::IBase;
     23 using ::android::hidl::manager::V1_1::IServiceManager;
     24 using ::android::hidl::manager::V1_0::IServiceNotification;
     25 using ::android::sp;
     26 using ::android::wp;
     27 
     28 struct ServiceManager : public IServiceManager, hidl_death_recipient {
     29     // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
     30     Return<sp<IBase>> get(const hidl_string& fqName,
     31                           const hidl_string& name) override;
     32     Return<bool> add(const hidl_string& name,
     33                      const sp<IBase>& service) override;
     34 
     35     Return<Transport> getTransport(const hidl_string& fqName,
     36                                    const hidl_string& name);
     37 
     38     Return<void> list(list_cb _hidl_cb) override;
     39     Return<void> listByInterface(const hidl_string& fqInstanceName,
     40                                  listByInterface_cb _hidl_cb) override;
     41 
     42     Return<bool> registerForNotifications(const hidl_string& fqName,
     43                                           const hidl_string& name,
     44                                           const sp<IServiceNotification>& callback) override;
     45 
     46     Return<void> debugDump(debugDump_cb _cb) override;
     47     Return<void> registerPassthroughClient(const hidl_string &fqName,
     48             const hidl_string &name) override;
     49 
     50     // Methods from ::android::hidl::manager::V1_1::IServiceManager follow.
     51     Return<bool> unregisterForNotifications(const hidl_string& fqName,
     52                                             const hidl_string& name,
     53                                             const sp<IServiceNotification>& callback) override;
     54 
     55     virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
     56 private:
     57     // if restrictToInstanceName is nullptr, remove all, otherwise only those services
     58     // which match this instance name. Returns whether all instances were removed.
     59     bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName);
     60     bool removePackageListener(const wp<IBase>& who);
     61     bool removeServiceListener(const wp<IBase>& who);
     62     size_t countExistingService() const;
     63     void forEachExistingService(std::function<void(const HidlService *)> f) const;
     64     void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
     65 
     66     using InstanceMap = std::map<
     67             std::string, // instance name e.x. "manager"
     68             std::unique_ptr<HidlService>
     69         >;
     70 
     71     struct PackageInterfaceMap {
     72         InstanceMap &getInstanceMap();
     73         const InstanceMap &getInstanceMap() const;
     74 
     75         /**
     76          * Finds a HidlService with the desired name. If none,
     77          * returns nullptr. HidlService::getService() might also be nullptr
     78          * if there are registered IServiceNotification objects for it. Return
     79          * value should be treated as a temporary reference.
     80          */
     81         HidlService *lookup(
     82             const std::string &name);
     83         const HidlService *lookup(
     84             const std::string &name) const;
     85 
     86         void insertService(std::unique_ptr<HidlService> &&service);
     87 
     88         void addPackageListener(sp<IServiceNotification> listener);
     89         bool removePackageListener(const wp<IBase>& who);
     90         bool removeServiceListener(const wp<IBase>& who);
     91 
     92         void sendPackageRegistrationNotification(
     93             const hidl_string &fqName,
     94             const hidl_string &instanceName);
     95 
     96     private:
     97         InstanceMap mInstanceMap{};
     98 
     99         std::vector<sp<IServiceNotification>> mPackageListeners{};
    100     };
    101 
    102     AccessControl mAcl;
    103 
    104     /**
    105      * Access to this map doesn't need to be locked, since hwservicemanager
    106      * is single-threaded.
    107      *
    108      * e.x.
    109      * mServiceMap["android.hidl.manager (at) 1.0::IServiceManager"]["manager"]
    110      *     -> HidlService object
    111      */
    112     std::map<
    113         std::string, // package::interface e.x. "android.hidl.manager (at) 1.0::IServiceManager"
    114         PackageInterfaceMap
    115     > mServiceMap;
    116 };
    117 
    118 }  // namespace implementation
    119 }  // namespace manager
    120 }  // namespace hidl
    121 }  // namespace android
    122 
    123 #endif  // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
    124