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