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     bool removeService(const wp<IBase>& who);
     58     bool removePackageListener(const wp<IBase>& who);
     59     bool removeServiceListener(const wp<IBase>& who);
     60     size_t countExistingService() const;
     61     void forEachExistingService(std::function<void(const HidlService *)> f) const;
     62     void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
     63 
     64     using InstanceMap = std::map<
     65             std::string, // instance name e.x. "manager"
     66             std::unique_ptr<HidlService>
     67         >;
     68 
     69     struct PackageInterfaceMap {
     70         InstanceMap &getInstanceMap();
     71         const InstanceMap &getInstanceMap() const;
     72 
     73         /**
     74          * Finds a HidlService with the desired name. If none,
     75          * returns nullptr. HidlService::getService() might also be nullptr
     76          * if there are registered IServiceNotification objects for it. Return
     77          * value should be treated as a temporary reference.
     78          */
     79         HidlService *lookup(
     80             const std::string &name);
     81         const HidlService *lookup(
     82             const std::string &name) const;
     83 
     84         void insertService(std::unique_ptr<HidlService> &&service);
     85 
     86         void addPackageListener(sp<IServiceNotification> listener);
     87         bool removePackageListener(const wp<IBase>& who);
     88         bool removeServiceListener(const wp<IBase>& who);
     89 
     90         void sendPackageRegistrationNotification(
     91             const hidl_string &fqName,
     92             const hidl_string &instanceName);
     93 
     94     private:
     95         InstanceMap mInstanceMap{};
     96 
     97         std::vector<sp<IServiceNotification>> mPackageListeners{};
     98     };
     99 
    100     AccessControl mAcl;
    101 
    102     /**
    103      * Access to this map doesn't need to be locked, since hwservicemanager
    104      * is single-threaded.
    105      *
    106      * e.x.
    107      * mServiceMap["android.hidl.manager (at) 1.0::IServiceManager"]["manager"]
    108      *     -> HidlService object
    109      */
    110     std::map<
    111         std::string, // package::interface e.x. "android.hidl.manager (at) 1.0::IServiceManager"
    112         PackageInterfaceMap
    113     > mServiceMap;
    114 };
    115 
    116 }  // namespace implementation
    117 }  // namespace manager
    118 }  // namespace hidl
    119 }  // namespace android
    120 
    121 #endif  // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
    122