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 #include "chre/core/event_loop.h"
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/platform/assert.h"
21 #include "chre/platform/memory.h"
22 #include "chre/platform/system_time.h"
23 #include "chre/util/macros.h"
24 
25 using chre::EventLoopManager;
26 using chre::EventLoopManagerSingleton;
27 
chreGetTime()28 DLL_EXPORT uint64_t chreGetTime() {
29   return chre::SystemTime::getMonotonicTime().toRawNanoseconds();
30 }
31 
chreGetEstimatedHostTimeOffset()32 DLL_EXPORT int64_t chreGetEstimatedHostTimeOffset() {
33   return chre::SystemTime::getEstimatedHostTimeOffset();
34 }
35 
chreGetAppId(void)36 DLL_EXPORT uint64_t chreGetAppId(void) {
37   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
38   return nanoapp->getAppId();
39 }
40 
chreGetInstanceId(void)41 DLL_EXPORT uint32_t chreGetInstanceId(void) {
42   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
43   return nanoapp->getInstanceId();
44 }
45 
chreTimerSet(uint64_t duration,const void * cookie,bool oneShot)46 DLL_EXPORT uint32_t chreTimerSet(uint64_t duration, const void *cookie,
47                                  bool oneShot) {
48   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
49   return EventLoopManagerSingleton::get()->getEventLoop().getTimerPool()
50       .setNanoappTimer(nanoapp, chre::Nanoseconds(duration), cookie, oneShot);
51 }
52 
chreTimerCancel(uint32_t timerId)53 DLL_EXPORT bool chreTimerCancel(uint32_t timerId) {
54   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
55   return EventLoopManagerSingleton::get()->getEventLoop().getTimerPool()
56       .cancelNanoappTimer(nanoapp, timerId);
57 }
58 
chreHeapAlloc(uint32_t bytes)59 DLL_EXPORT void *chreHeapAlloc(uint32_t bytes) {
60   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
61   return chre::EventLoopManagerSingleton::get()->getMemoryManager().
62       nanoappAlloc(nanoapp, bytes);
63 }
64 
chreHeapFree(void * ptr)65 DLL_EXPORT void chreHeapFree(void *ptr) {
66   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
67   chre::EventLoopManagerSingleton::get()->getMemoryManager().
68       nanoappFree(nanoapp, ptr);
69 }
70