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/context.h"
22 #include "chre/platform/memory.h"
23 #include "chre/platform/system_time.h"
24 
25 using chre::EventLoopManager;
26 
chreGetTime()27 uint64_t chreGetTime() {
28   return chre::SystemTime::getMonotonicTime().toRawNanoseconds();
29 }
30 
chreGetAppId(void)31 uint64_t chreGetAppId(void) {
32   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
33   return nanoapp->getAppId();
34 }
35 
chreGetInstanceId(void)36 uint32_t chreGetInstanceId(void) {
37   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
38   return nanoapp->getInstanceId();
39 }
40 
chreTimerSet(uint64_t duration,const void * cookie,bool oneShot)41 uint32_t chreTimerSet(uint64_t duration, const void *cookie, bool oneShot) {
42   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
43   return chre::getCurrentEventLoop()->getTimerPool().setTimer(nanoapp,
44       chre::Nanoseconds(duration), cookie, oneShot);
45 }
46 
chreTimerCancel(uint32_t timerId)47 bool chreTimerCancel(uint32_t timerId) {
48   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
49   return chre::getCurrentEventLoop()->getTimerPool().cancelTimer(nanoapp,
50                                                                  timerId);
51 }
52 
chreHeapAlloc(uint32_t bytes)53 void *chreHeapAlloc(uint32_t bytes) {
54   // TODO: Build a MemoryManager to track allocations to an app. If an app is
55   // unloaded or aborts we need to release any unfreed memory.
56   return chre::memoryAlloc(bytes);
57 }
58 
chreHeapFree(void * ptr)59 void chreHeapFree(void *ptr) {
60   chre::memoryFree(ptr);
61 }
62