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