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