1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "drm-vts-vendor-modules"
18 
19 #include <dirent.h>
20 #include <dlfcn.h>
21 #include <log/log.h>
22 #include <memory>
23 #include <utils/String8.h>
24 #include <SharedLibrary.h>
25 
26 #include "drm_hal_vendor_module_api.h"
27 #include "vendor_modules.h"
28 
29 using std::string;
30 using std::vector;
31 using std::unique_ptr;
32 using ::android::String8;
33 using ::android::hardware::drm::V1_0::helper::SharedLibrary;
34 
35 namespace drm_vts {
scanModules(const std::string & directory)36 void VendorModules::scanModules(const std::string &directory) {
37     DIR* dir = opendir(directory.c_str());
38     if (dir == NULL) {
39         ALOGE("Unable to open drm VTS vendor directory %s", directory.c_str());
40     } else {
41         struct dirent* entry;
42         while ((entry = readdir(dir))) {
43             ALOGD("checking file %s", entry->d_name);
44             string fullpath = directory + "/" + entry->d_name;
45             if (endsWith(fullpath, ".so")) {
46                 mPathList.push_back(fullpath);
47             }
48         }
49         closedir(dir);
50     }
51 }
52 
getModule(const string & path)53 DrmHalVTSVendorModule* VendorModules::getModule(const string& path) {
54     if (mOpenLibraries.find(path) == mOpenLibraries.end()) {
55         auto library = std::make_unique<SharedLibrary>(String8(path.c_str()));
56         if (!library) {
57             ALOGE("failed to map shared library %s", path.c_str());
58             return NULL;
59         }
60         mOpenLibraries[path] = std::move(library);
61     }
62     const unique_ptr<SharedLibrary>& library = mOpenLibraries[path];
63     void* symbol = library->lookup("vendorModuleFactory");
64     if (symbol == NULL) {
65         ALOGE("getVendorModule failed to lookup 'vendorModuleFactory' in %s: "
66               "%s", path.c_str(), library->lastError());
67         return NULL;
68     }
69     typedef DrmHalVTSVendorModule* (*ModuleFactory)();
70     ModuleFactory moduleFactory = reinterpret_cast<ModuleFactory>(symbol);
71     return (*moduleFactory)();
72 }
73 
getModuleByName(const string & name)74 DrmHalVTSVendorModule* VendorModules::getModuleByName(const string& name) {
75     for (const auto &path : mPathList) {
76         auto module = getModule(path);
77         if (module->getServiceName() == name) {
78             return module;
79         }
80 
81     }
82     return NULL;
83 }
84 };
85