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 #include "BootAction.h"
18
19 #define LOG_TAG "BootAction"
20
21 #include <dlfcn.h>
22
23 #include <pio/peripheral_manager_client.h>
24 #include <utils/Log.h>
25
26 namespace android {
27
~BootAction()28 BootAction::~BootAction() {
29 if (mLibHandle != nullptr) {
30 dlclose(mLibHandle);
31 }
32 }
33
init(const std::string & libraryPath,const std::vector<ABootActionParameter> & parameters)34 bool BootAction::init(const std::string& libraryPath,
35 const std::vector<ABootActionParameter>& parameters) {
36 APeripheralManagerClient* client = nullptr;
37 ALOGD("Connecting to peripheralmanager");
38 // Wait for peripheral manager to come up.
39 while (client == nullptr) {
40 client = APeripheralManagerClient_new();
41 if (client == nullptr) {
42 ALOGV("peripheralmanager is not up, sleeping before we check again.");
43 usleep(250000);
44 }
45 }
46 ALOGD("Peripheralmanager is up.");
47 APeripheralManagerClient_delete(client);
48
49
50 ALOGI("Loading boot action %s", libraryPath.c_str());
51 mLibHandle = dlopen(libraryPath.c_str(), RTLD_NOW);
52 if (mLibHandle == nullptr) {
53 ALOGE("Unable to load library at %s :: %s",
54 libraryPath.c_str(), dlerror());
55 return false;
56 }
57
58 void* loaded = nullptr;
59 if (!loadSymbol("boot_action_init", &loaded) || loaded == nullptr) {
60 return false;
61 }
62 mLibInit = reinterpret_cast<libInit>(loaded);
63
64 loaded = nullptr;
65 if (!loadSymbol("boot_action_shutdown", &loaded) || loaded == nullptr) {
66 return false;
67 }
68 mLibShutdown = reinterpret_cast<libShutdown>(loaded);
69
70 // StartPart is considered optional, if it isn't exported by the library
71 // we will still call init and shutdown.
72 loaded = nullptr;
73 if (!loadSymbol("boot_action_start_part", &loaded) || loaded == nullptr) {
74 ALOGI("No boot_action_start_part found, action will not be told when "
75 "Animation parts change.");
76 } else {
77 mLibStartPart = reinterpret_cast<libStartPart>(loaded);
78 }
79
80 ALOGD("Entering boot_action_init");
81 bool result = mLibInit(parameters.data(), parameters.size());
82 ALOGD("Returned from boot_action_init");
83 return result;
84 }
85
startPart(int partNumber,int playNumber)86 void BootAction::startPart(int partNumber, int playNumber) {
87 if (mLibStartPart == nullptr) return;
88
89 ALOGD("Entering boot_action_start_part");
90 mLibStartPart(partNumber, playNumber);
91 ALOGD("Returned from boot_action_start_part");
92 }
93
shutdown()94 void BootAction::shutdown() {
95 ALOGD("Entering boot_action_shutdown");
96 mLibShutdown();
97 ALOGD("Returned from boot_action_shutdown");
98 }
99
loadSymbol(const char * symbol,void ** loaded)100 bool BootAction::loadSymbol(const char* symbol, void** loaded) {
101 *loaded = dlsym(mLibHandle, symbol);
102 if (loaded == nullptr) {
103 ALOGE("Unable to load symbol : %s :: %s", symbol, dlerror());
104 return false;
105 }
106 return true;
107 }
108
109 } // namespace android
110