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_api/chre/re.h"
18
19 #include "chre/core/event_loop.h"
20 #include "chre/core/event_loop_manager.h"
21 #include "chre/platform/fatal_error.h"
22 #include "chre/platform/shared/debug_dump.h"
23 #include "chre/platform/system_time.h"
24 #include "chre/util/macros.h"
25
26 using chre::EventLoopManager;
27 using chre::EventLoopManagerSingleton;
28 using chre::handleNanoappAbort;
29 using chre::Nanoapp;
30
chreGetCapabilities()31 DLL_EXPORT uint32_t chreGetCapabilities() {
32 uint32_t capabilities = CHRE_CAPABILITIES_NONE;
33
34 #ifdef CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED
35 capabilities |= CHRE_CAPABILITIES_RELIABLE_MESSAGES;
36 #endif // CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED
37
38 #ifdef CHRE_MESSAGE_ROUTER_SUPPORT_ENABLED
39 capabilities |= CHRE_CAPABILITIES_GENERIC_ENDPOINT_MESSAGES;
40 #endif // CHRE_MESSAGE_ROUTER_SUPPORT_ENABLED
41
42 return capabilities;
43 }
44
chreGetMessageToHostMaxSize()45 DLL_EXPORT uint32_t chreGetMessageToHostMaxSize() {
46 #ifdef CHRE_LARGE_PAYLOAD_MAX_SIZE
47 static_assert(CHRE_LARGE_PAYLOAD_MAX_SIZE >= CHRE_MESSAGE_TO_HOST_MAX_SIZE,
48 "CHRE_LARGE_PAYLOAD_MAX_SIZE must be greater than or equal to "
49 "CHRE_MESSAGE_TO_HOST_MAX_SIZE");
50
51 #ifdef CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED
52 static_assert(CHRE_LARGE_PAYLOAD_MAX_SIZE >= 32000,
53 "CHRE_LARGE_PAYLOAD_MAX_SIZE must be greater than or equal to "
54 "32000 when CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED is enabled");
55 #endif
56
57 return CHRE_LARGE_PAYLOAD_MAX_SIZE;
58 #else
59 #ifdef CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED
60 static_assert(false,
61 "CHRE_LARGE_PAYLOAD_MAX_SIZE must be defined if "
62 "CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED is enabled");
63 #endif
64
65 return CHRE_MESSAGE_TO_HOST_MAX_SIZE;
66 #endif // CHRE_LARGE_PAYLOAD_MAX_SIZE
67 }
68
chreGetTime()69 DLL_EXPORT uint64_t chreGetTime() {
70 return chre::SystemTime::getMonotonicTime().toRawNanoseconds();
71 }
72
chreGetEstimatedHostTimeOffset()73 DLL_EXPORT int64_t chreGetEstimatedHostTimeOffset() {
74 return chre::SystemTime::getEstimatedHostTimeOffset();
75 }
76
chreGetAppId(void)77 DLL_EXPORT uint64_t chreGetAppId(void) {
78 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
79 return nanoapp->getAppId();
80 }
81
chreGetInstanceId(void)82 DLL_EXPORT uint32_t chreGetInstanceId(void) {
83 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
84 return nanoapp->getInstanceId();
85 }
86
chreTimerSet(uint64_t duration,const void * cookie,bool oneShot)87 DLL_EXPORT uint32_t chreTimerSet(uint64_t duration, const void *cookie,
88 bool oneShot) {
89 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
90 return EventLoopManagerSingleton::get()
91 ->getEventLoop()
92 .getTimerPool()
93 .setNanoappTimer(nanoapp, chre::Nanoseconds(duration), cookie, oneShot);
94 }
95
chreTimerCancel(uint32_t timerId)96 DLL_EXPORT bool chreTimerCancel(uint32_t timerId) {
97 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
98 return EventLoopManagerSingleton::get()
99 ->getEventLoop()
100 .getTimerPool()
101 .cancelNanoappTimer(nanoapp, timerId);
102 }
103
chreAbort(uint32_t)104 DLL_EXPORT void chreAbort(uint32_t /* abortCode */) {
105 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
106 if (nanoapp == nullptr) {
107 FATAL_ERROR("chreAbort called in unknown context");
108 } else {
109 handleNanoappAbort(*nanoapp);
110 }
111 }
112
chreHeapAlloc(uint32_t bytes)113 DLL_EXPORT void *chreHeapAlloc(uint32_t bytes) {
114 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
115 return EventLoopManagerSingleton::get()->getMemoryManager().nanoappAlloc(
116 nanoapp, bytes);
117 }
118
chreHeapFree(void * ptr)119 DLL_EXPORT void chreHeapFree(void *ptr) {
120 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
121 EventLoopManagerSingleton::get()->getMemoryManager().nanoappFree(nanoapp,
122 ptr);
123 }
124
platform_chreDebugDumpVaLog(const char * formatStr,va_list args)125 DLL_EXPORT void platform_chreDebugDumpVaLog(const char *formatStr,
126 va_list args) {
127 Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
128 EventLoopManagerSingleton::get()->getDebugDumpManager().appendNanoappLog(
129 *nanoapp, formatStr, args);
130 }
131
chreDebugDumpLog(const char * formatStr,...)132 DLL_EXPORT void chreDebugDumpLog(const char *formatStr, ...) {
133 va_list args;
134 va_start(args, formatStr);
135 platform_chreDebugDumpVaLog(formatStr, args);
136 va_end(args);
137 }
138