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 <cstdarg>
18 #include <cstdio>
19 #include <cstring>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/core/host_comms_manager.h"
23 #include "chre/platform/fatal_error.h"
24 #include "chre/platform/log.h"
25 #include "chre/util/macros.h"
26 #include "chre/util/system/napp_permissions.h"
27 #include "chre_api/chre/event.h"
28 #include "chre_api/chre/re.h"
29
30 using chre::EventLoop;
31 using chre::EventLoopManager;
32 using chre::EventLoopManagerSingleton;
33 using chre::Nanoapp;
34
chreAbort(uint32_t abortCode)35 DLL_EXPORT void chreAbort(uint32_t abortCode) {
36 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
37
38 // TODO: we should cleanly unload the nanoapp, release all of its resources,
39 // and send an abort notification to the host so as to localize the impact to
40 // the calling nanoapp
41 if (nanoapp == nullptr) {
42 FATAL_ERROR("chreAbort called in unknown context");
43 } else {
44 FATAL_ERROR("chreAbort called by app 0x%016" PRIx64, nanoapp->getAppId());
45 }
46 }
47
chreSendEvent(uint16_t eventType,void * eventData,chreEventCompleteFunction * freeCallback,uint32_t targetInstanceId)48 DLL_EXPORT bool chreSendEvent(uint16_t eventType, void *eventData,
49 chreEventCompleteFunction *freeCallback,
50 uint32_t targetInstanceId) {
51 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
52
53 // Prevent an app that is in the process of being unloaded from generating new
54 // events
55 bool success = false;
56 EventLoop &eventLoop = EventLoopManagerSingleton::get()->getEventLoop();
57 if (eventLoop.currentNanoappIsStopping()) {
58 LOGW("Rejecting event from app instance %" PRIu32 " because it's stopping",
59 nanoapp->getInstanceId());
60 } else {
61 success = eventLoop.postLowPriorityEventOrFree(
62 eventType, eventData, freeCallback, nanoapp->getInstanceId(),
63 targetInstanceId);
64 }
65 return success;
66 }
67
chreSendMessageToHost(void * message,uint32_t messageSize,uint32_t messageType,chreMessageFreeFunction * freeCallback)68 DLL_EXPORT bool chreSendMessageToHost(void *message, uint32_t messageSize,
69 uint32_t messageType,
70 chreMessageFreeFunction *freeCallback) {
71 return chreSendMessageToHostEndpoint(
72 message, static_cast<size_t>(messageSize), messageType,
73 CHRE_HOST_ENDPOINT_BROADCAST, freeCallback);
74 }
75
chreSendMessageWithPermissions(void * message,size_t messageSize,uint32_t messageType,uint16_t hostEndpoint,uint32_t messagePermissions,chreMessageFreeFunction * freeCallback)76 DLL_EXPORT bool chreSendMessageWithPermissions(
77 void *message, size_t messageSize, uint32_t messageType,
78 uint16_t hostEndpoint, uint32_t messagePermissions,
79 chreMessageFreeFunction *freeCallback) {
80 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
81
82 bool success = false;
83 const EventLoop &eventLoop = EventLoopManagerSingleton::get()->getEventLoop();
84 if (eventLoop.currentNanoappIsStopping()) {
85 LOGW("Rejecting message to host from app instance %" PRIu32
86 " because it's stopping",
87 nanoapp->getInstanceId());
88 } else {
89 auto &hostCommsManager =
90 EventLoopManagerSingleton::get()->getHostCommsManager();
91 success = hostCommsManager.sendMessageToHostFromNanoapp(
92 nanoapp, message, messageSize, messageType, hostEndpoint,
93 messagePermissions, freeCallback);
94 }
95
96 if (!success && freeCallback != nullptr) {
97 freeCallback(message, messageSize);
98 }
99
100 return success;
101 }
102
chreSendMessageToHostEndpoint(void * message,size_t messageSize,uint32_t messageType,uint16_t hostEndpoint,chreMessageFreeFunction * freeCallback)103 DLL_EXPORT bool chreSendMessageToHostEndpoint(
104 void *message, size_t messageSize, uint32_t messageType,
105 uint16_t hostEndpoint, chreMessageFreeFunction *freeCallback) {
106 return chreSendMessageWithPermissions(
107 message, messageSize, messageType, hostEndpoint,
108 static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_NONE),
109 freeCallback);
110 }
111
chreGetNanoappInfoByAppId(uint64_t appId,struct chreNanoappInfo * info)112 DLL_EXPORT bool chreGetNanoappInfoByAppId(uint64_t appId,
113 struct chreNanoappInfo *info) {
114 return EventLoopManagerSingleton::get()
115 ->getEventLoop()
116 .populateNanoappInfoForAppId(appId, info);
117 }
118
chreGetNanoappInfoByInstanceId(uint32_t instanceId,struct chreNanoappInfo * info)119 DLL_EXPORT bool chreGetNanoappInfoByInstanceId(uint32_t instanceId,
120 struct chreNanoappInfo *info) {
121 return EventLoopManagerSingleton::get()
122 ->getEventLoop()
123 .populateNanoappInfoForInstanceId(instanceId, info);
124 }
125
chreConfigureNanoappInfoEvents(bool enable)126 DLL_EXPORT void chreConfigureNanoappInfoEvents(bool enable) {
127 chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
128 nanoapp->configureNanoappInfoEvents(enable);
129 }
130
chreConfigureHostSleepStateEvents(bool enable)131 DLL_EXPORT void chreConfigureHostSleepStateEvents(bool enable) {
132 chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
133 nanoapp->configureHostSleepEvents(enable);
134 }
135
chreIsHostAwake()136 DLL_EXPORT bool chreIsHostAwake() {
137 return EventLoopManagerSingleton::get()
138 ->getEventLoop()
139 .getPowerControlManager()
140 .hostIsAwake();
141 }
142
chreConfigureDebugDumpEvent(bool enable)143 DLL_EXPORT void chreConfigureDebugDumpEvent(bool enable) {
144 chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
145 nanoapp->configureDebugDumpEvent(enable);
146 }
147