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 
chreGetTime()29 DLL_EXPORT uint64_t chreGetTime() {
30   return chre::SystemTime::getMonotonicTime().toRawNanoseconds();
31 }
32 
chreGetEstimatedHostTimeOffset()33 DLL_EXPORT int64_t chreGetEstimatedHostTimeOffset() {
34   return chre::SystemTime::getEstimatedHostTimeOffset();
35 }
36 
chreGetAppId(void)37 DLL_EXPORT uint64_t chreGetAppId(void) {
38   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
39   return nanoapp->getAppId();
40 }
41 
chreGetInstanceId(void)42 DLL_EXPORT uint32_t chreGetInstanceId(void) {
43   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
44   return nanoapp->getInstanceId();
45 }
46 
chreTimerSet(uint64_t duration,const void * cookie,bool oneShot)47 DLL_EXPORT uint32_t chreTimerSet(uint64_t duration, const void *cookie,
48                                  bool oneShot) {
49   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
50   return EventLoopManagerSingleton::get()
51       ->getEventLoop()
52       .getTimerPool()
53       .setNanoappTimer(nanoapp, chre::Nanoseconds(duration), cookie, oneShot);
54 }
55 
chreTimerCancel(uint32_t timerId)56 DLL_EXPORT bool chreTimerCancel(uint32_t timerId) {
57   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
58   return EventLoopManagerSingleton::get()
59       ->getEventLoop()
60       .getTimerPool()
61       .cancelNanoappTimer(nanoapp, timerId);
62 }
63 
chreHeapAlloc(uint32_t bytes)64 DLL_EXPORT void *chreHeapAlloc(uint32_t bytes) {
65   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
66   return chre::EventLoopManagerSingleton::get()
67       ->getMemoryManager()
68       .nanoappAlloc(nanoapp, bytes);
69 }
70 
chreHeapFree(void * ptr)71 DLL_EXPORT void chreHeapFree(void *ptr) {
72   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
73   chre::EventLoopManagerSingleton::get()->getMemoryManager().nanoappFree(
74       nanoapp, ptr);
75 }
76 
platform_chreDebugDumpVaLog(const char * formatStr,va_list args)77 DLL_EXPORT void platform_chreDebugDumpVaLog(const char *formatStr,
78                                             va_list args) {
79   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
80   chre::EventLoopManagerSingleton::get()
81       ->getDebugDumpManager()
82       .appendNanoappLog(*nanoapp, formatStr, args);
83 }
84 
chreDebugDumpLog(const char * formatStr,...)85 DLL_EXPORT void chreDebugDumpLog(const char *formatStr, ...) {
86   va_list args;
87   va_start(args, formatStr);
88   platform_chreDebugDumpVaLog(formatStr, args);
89   va_end(args);
90 }
91