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