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.h>
18 #include <cinttypes>
19 
20 #include "chre/util/nanoapp/audio.h"
21 #include "chre/util/nanoapp/log.h"
22 
23 #define LOG_TAG "[TimerWorld]"
24 
25 #ifdef CHRE_NANOAPP_INTERNAL
26 namespace chre {
27 namespace {
28 #endif  // CHRE_NANOAPP_INTERNAL
29 
30 uint32_t gOneShotTimerHandle;
31 uint32_t gCyclicTimerHandle;
32 int gCyclicTimerCount;
33 
nanoappStart()34 bool nanoappStart() {
35   LOGI("App started on platform ID %" PRIx64, chreGetPlatformId());
36 
37   gOneShotTimerHandle =
38       chreTimerSet(100000000 /* duration: 100ms */,
39                    &gOneShotTimerHandle /* data */, true /* oneShot */);
40   gCyclicTimerHandle =
41       chreTimerSet(150000000 /* duration: 150ms */,
42                    &gCyclicTimerHandle /* data */, false /* oneShot */);
43   gCyclicTimerCount = 0;
44   return true;
45 }
46 
handleTimerEvent(const void * eventData)47 void handleTimerEvent(const void *eventData) {
48   const uint32_t *timerHandle = static_cast<const uint32_t *>(eventData);
49   if (*timerHandle == gOneShotTimerHandle) {
50     LOGI("One shot timer event received");
51   } else if (*timerHandle == gCyclicTimerHandle) {
52     LOGI("Cyclic timer event received");
53     gCyclicTimerCount++;
54     if (gCyclicTimerCount > 1) {
55       chreTimerCancel(gCyclicTimerHandle);
56     }
57   }
58 }
59 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)60 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
61                         const void *eventData) {
62   switch (eventType) {
63     case CHRE_EVENT_TIMER:
64       handleTimerEvent(eventData);
65       break;
66     default:
67       LOGW("Unknown event received");
68       break;
69   }
70 }
71 
nanoappEnd()72 void nanoappEnd() {
73   LOGI("Stopped");
74 }
75 
76 #ifdef CHRE_NANOAPP_INTERNAL
77 }  // anonymous namespace
78 }  // namespace chre
79 
80 #include "chre/platform/static_nanoapp_init.h"
81 #include "chre/util/nanoapp/app_id.h"
82 #include "chre/util/system/napp_permissions.h"
83 
84 CHRE_STATIC_NANOAPP_INIT(TimerWorld, chre::kTimerWorldAppId, 0,
85                          chre::NanoappPermissions::CHRE_PERMS_NONE);
86 #endif  // CHRE_NANOAPP_INTERNAL
87