blob: fa797444d569be0929980b00c1e0e5c7fcd424a4 [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
Ed Coyne7464ac92017-06-08 12:26:48 -070021#include <dlfcn.h>
Braden Kell6dd64f32017-09-25 17:30:28 -070022
Ed Coyne7464ac92017-06-08 12:26:48 -070023#include <pio/peripheral_manager_client.h>
24#include <utils/Log.h>
25
Ed Coyne7464ac92017-06-08 12:26:48 -070026namespace android {
27
28BootAction::~BootAction() {
29 if (mLibHandle != nullptr) {
30 dlclose(mLibHandle);
31 }
32}
33
David Pursell54a8fe42017-09-29 16:05:26 -070034bool BootAction::init(const std::string& libraryPath,
35 const std::vector<ABootActionParameter>& parameters) {
Ed Coyne7464ac92017-06-08 12:26:48 -070036 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
Braden Kell6dd64f32017-09-25 17:30:28 -070049
Ed Coyne428ed512017-08-14 15:10:06 -070050 ALOGI("Loading boot action %s", libraryPath.c_str());
51 mLibHandle = dlopen(libraryPath.c_str(), RTLD_NOW);
Ed Coyne7464ac92017-06-08 12:26:48 -070052 if (mLibHandle == nullptr) {
53 ALOGE("Unable to load library at %s :: %s",
Ed Coyne428ed512017-08-14 15:10:06 -070054 libraryPath.c_str(), dlerror());
Ed Coyne7464ac92017-06-08 12:26:48 -070055 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");
Braden Kell6dd64f32017-09-25 17:30:28 -070081 bool result = mLibInit(parameters.data(), parameters.size());
Ed Coyne7464ac92017-06-08 12:26:48 -070082 ALOGD("Returned from boot_action_init");
83 return result;
84}
85
86void 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
94void BootAction::shutdown() {
95 ALOGD("Entering boot_action_shutdown");
96 mLibShutdown();
97 ALOGD("Returned from boot_action_shutdown");
98}
99
100bool 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
Ed Coyne7464ac92017-06-08 12:26:48 -0700109} // namespace android