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 "vendor_modules.h"
27
28using std::string;
29using std::vector;
30using std::unique_ptr;
31using ::android::String8;
32using ::android::hardware::drm::V1_0::helper::SharedLibrary;
33
34namespace drm_vts {
35void VendorModules::scanModules(const std::string &directory) {
36    DIR* dir = opendir(directory.c_str());
37    if (dir == NULL) {
38        ALOGE("Unable to open drm VTS vendor directory %s", directory.c_str());
39    } else {
40        struct dirent* entry;
41        while ((entry = readdir(dir))) {
42            ALOGD("checking file %s", entry->d_name);
43            string fullpath = directory + "/" + entry->d_name;
44            if (endsWith(fullpath, ".so")) {
45                mPathList.push_back(fullpath);
46            }
47        }
48        closedir(dir);
49    }
50}
51
52DrmHalVTSVendorModule* VendorModules::getModule(const string& path) {
53    if (mOpenLibraries.find(path) == mOpenLibraries.end()) {
54        auto library = std::make_unique<SharedLibrary>(String8(path.c_str()));
55        if (!library) {
56            ALOGE("failed to map shared library %s", path.c_str());
57            return NULL;
58        }
59        mOpenLibraries[path] = std::move(library);
60    }
61    const unique_ptr<SharedLibrary>& library = mOpenLibraries[path];
62    void* symbol = library->lookup("vendorModuleFactory");
63    if (symbol == NULL) {
64        ALOGE("getVendorModule failed to lookup 'vendorModuleFactory' in %s: "
65              "%s", path.c_str(), library->lastError());
66        return NULL;
67    }
68    typedef DrmHalVTSVendorModule* (*ModuleFactory)();
69    ModuleFactory moduleFactory = reinterpret_cast<ModuleFactory>(symbol);
70    return (*moduleFactory)();
71}
72};
73