1 /*
2 * Copyright (C) 2016 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 "chre/platform/platform_nanoapp.h"
18
19 #include <cinttypes>
20 #include <dlfcn.h>
21
22 #include "chre_api/chre/version.h"
23 #include "chre/platform/assert.h"
24 #include "chre/platform/log.h"
25 #include "chre/platform/shared/nanoapp_dso_util.h"
26
27 namespace chre {
28
~PlatformNanoapp()29 PlatformNanoapp::~PlatformNanoapp() {
30 closeNanoapp();
31 }
32
start()33 bool PlatformNanoapp::start() {
34 return openNanoapp() && mAppInfo->entryPoints.start();
35 }
36
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)37 void PlatformNanoapp::handleEvent(uint32_t senderInstanceId,
38 uint16_t eventType,
39 const void *eventData) {
40 mAppInfo->entryPoints.handleEvent(senderInstanceId, eventType, eventData);
41 }
42
end()43 void PlatformNanoapp::end() {
44 mAppInfo->entryPoints.end();
45 closeNanoapp();
46 }
47
getAppId() const48 uint64_t PlatformNanoapp::getAppId() const {
49 return (mAppInfo == nullptr) ? 0 : mAppInfo->appId;
50 }
51
getAppVersion() const52 uint32_t PlatformNanoapp::getAppVersion() const {
53 return mAppInfo->appVersion;
54 }
55
getTargetApiVersion() const56 uint32_t PlatformNanoapp::getTargetApiVersion() const {
57 return CHRE_API_VERSION;
58 }
59
isSystemNanoapp() const60 bool PlatformNanoapp::isSystemNanoapp() const {
61 return (mAppInfo != nullptr && mAppInfo->isSystemNanoapp);
62 }
63
logStateToBuffer(char * buffer,size_t * bufferPos,size_t bufferSize) const64 bool PlatformNanoapp::logStateToBuffer(char *buffer, size_t *bufferPos,
65 size_t bufferSize) const {
66 return true;
67 }
68
loadFromFile(const std::string & filename)69 void PlatformNanoappBase::loadFromFile(const std::string& filename) {
70 CHRE_ASSERT(!isLoaded());
71 mFilename = filename;
72 }
73
loadStatic(const struct chreNslNanoappInfo * appInfo)74 void PlatformNanoappBase::loadStatic(const struct chreNslNanoappInfo *appInfo) {
75 CHRE_ASSERT(!isLoaded());
76 mIsStatic = true;
77 mAppInfo = appInfo;
78 }
79
isLoaded() const80 bool PlatformNanoappBase::isLoaded() const {
81 return (mIsStatic || mDsoHandle != nullptr);
82 }
83
openNanoapp()84 bool PlatformNanoappBase::openNanoapp() {
85 bool success = false;
86
87 if (mIsStatic) {
88 success = true;
89 } else if (!mFilename.empty()) {
90 success = openNanoappFromFile();
91 } else {
92 CHRE_ASSERT(false);
93 }
94
95 return success;
96 }
97
openNanoappFromFile()98 bool PlatformNanoappBase::openNanoappFromFile() {
99 CHRE_ASSERT(!mFilename.empty());
100 CHRE_ASSERT_LOG(mDsoHandle == nullptr, "Re-opening nanoapp");
101 bool success = false;
102
103 mDsoHandle = dlopen(mFilename.c_str(), RTLD_NOW | RTLD_GLOBAL);
104 if (mDsoHandle == nullptr) {
105 LOGE("Failed to load nanoapp from file %s: %s",
106 mFilename.c_str(), dlerror());
107 } else {
108 mAppInfo = static_cast<const struct chreNslNanoappInfo *>(
109 dlsym(mDsoHandle, CHRE_NSL_DSO_NANOAPP_INFO_SYMBOL_NAME));
110 if (mAppInfo == nullptr) {
111 LOGE("Failed to find app info symbol in %s: %s",
112 mFilename.c_str(), dlerror());
113 } else {
114 success = validateAppInfo(0 /* skip ID validation */, 0, mAppInfo,
115 true /* ignoreAppVersion */);
116 if (!success) {
117 mAppInfo = nullptr;
118 } else {
119 LOGI("Successfully loaded nanoapp %s (0x%016" PRIx64 ") version 0x%"
120 PRIx32 " uimg %d system %d from file %s", mAppInfo->name,
121 mAppInfo->appId, mAppInfo->appVersion, mAppInfo->isTcmNanoapp,
122 mAppInfo->isSystemNanoapp, mFilename.c_str());
123 }
124 }
125 }
126
127 return success;
128 }
129
closeNanoapp()130 void PlatformNanoappBase::closeNanoapp() {
131 if (mDsoHandle != nullptr) {
132 if (dlclose(mDsoHandle) != 0) {
133 LOGE("dlclose failed: %s", dlerror());
134 }
135 mDsoHandle = nullptr;
136 }
137 }
138
139 } // namespace chre
140