Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 "APM_EngineLoader" |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "EngineLibrary.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 26 | EngineInstance loadApmEngineLibraryAndCreateEngine(const std::string& librarySuffix, |
| 27 | const std::string& configXmlFilePath) |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 28 | { |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 29 | auto engLib = EngineLibrary::load(librarySuffix); |
| 30 | if (!engLib) { |
| 31 | ALOGE("%s: Failed to load the engine library, suffix \"%s\"", |
| 32 | __func__, librarySuffix.c_str()); |
| 33 | return nullptr; |
| 34 | } |
| 35 | auto engine = engLib->createEngineUsingXmlConfig(configXmlFilePath); |
| 36 | if (engine == nullptr) { |
| 37 | ALOGE("%s: Failed to instantiate the APM engine", __func__); |
| 38 | return nullptr; |
| 39 | } |
| 40 | return engine; |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | std::shared_ptr<EngineLibrary> EngineLibrary::load(const std::string& librarySuffix) |
| 45 | { |
| 46 | std::string libraryPath = "libaudiopolicyengine" + librarySuffix + ".so"; |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 47 | std::shared_ptr<EngineLibrary> engLib(new EngineLibrary()); |
| 48 | return engLib->init(std::move(libraryPath)) ? engLib : nullptr; |
| 49 | } |
| 50 | |
| 51 | EngineLibrary::~EngineLibrary() |
| 52 | { |
| 53 | close(); |
| 54 | } |
| 55 | |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 56 | EngineInstance EngineLibrary::createEngineUsingXmlConfig(const std::string& xmlFilePath) { |
| 57 | auto instance = createEngine(); |
| 58 | if (instance != nullptr) { |
| 59 | if (status_t status = instance->loadFromXmlConfigWithFallback(xmlFilePath); |
| 60 | status == OK) { |
| 61 | return instance; |
| 62 | } else { |
| 63 | ALOGE("%s: loading of the engine config with XML configuration file \"%s\" failed: %d", |
| 64 | __func__, xmlFilePath.empty() ? "default" : xmlFilePath.c_str(), status); |
| 65 | } |
| 66 | } |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 70 | bool EngineLibrary::init(std::string libraryPath) |
| 71 | { |
| 72 | mLibraryHandle = dlopen(libraryPath.c_str(), 0); |
| 73 | if (mLibraryHandle == nullptr) { |
| 74 | ALOGE("Could not dlopen %s: %s", libraryPath.c_str(), dlerror()); |
| 75 | return false; |
| 76 | } |
| 77 | mCreateEngineInstance = (EngineInterface* (*)())dlsym(mLibraryHandle, "createEngineInstance"); |
| 78 | mDestroyEngineInstance = (void (*)(EngineInterface*))dlsym( |
| 79 | mLibraryHandle, "destroyEngineInstance"); |
| 80 | if (mCreateEngineInstance == nullptr || mDestroyEngineInstance == nullptr) { |
| 81 | ALOGE("Could not find engine interface functions in %s", libraryPath.c_str()); |
| 82 | close(); |
| 83 | return false; |
| 84 | } |
| 85 | ALOGD("Loaded engine from %s", libraryPath.c_str()); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | EngineInstance EngineLibrary::createEngine() |
| 90 | { |
| 91 | if (mCreateEngineInstance == nullptr || mDestroyEngineInstance == nullptr) { |
| 92 | return EngineInstance(); |
| 93 | } |
| 94 | return EngineInstance(mCreateEngineInstance(), |
| 95 | [lib = shared_from_this(), destroy = mDestroyEngineInstance] (EngineInterface* e) { |
| 96 | destroy(e); |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | void EngineLibrary::close() |
| 101 | { |
| 102 | if (mLibraryHandle != nullptr) { |
| 103 | dlclose(mLibraryHandle); |
| 104 | } |
| 105 | mLibraryHandle = nullptr; |
| 106 | mCreateEngineInstance = nullptr; |
| 107 | mDestroyEngineInstance = nullptr; |
| 108 | } |
| 109 | |
| 110 | } // namespace android |