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