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