• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
19 #include "chre/util/nanoapp/log.h"
20 
21 #define LOG_TAG "[HostAwakeWorld]"
22 
23 /**
24  * A nanoapp that sets a periodic timer to send broadcast messages to the host
25  * only if it is awake.
26  */
27 
28 #ifdef CHRE_NANOAPP_INTERNAL
29 namespace chre {
30 namespace {
31 #endif  // CHRE_NANOAPP_INTERNAL
32 
33 uint8_t gMessageBuffer[] = {0, 1, 2, 3, 4};
34 
35 uint32_t gMessageTimerHandle;
36 
nanoappStart()37 bool nanoappStart() {
38   LOGI("Host awake world start");
39   gMessageTimerHandle = chreTimerSet(5000000 /* 5 ms */,
40                                      &gMessageTimerHandle,
41                                      false /* oneShot */);
42   chreConfigureHostSleepStateEvents(true /* enable */);
43 
44   return (gMessageTimerHandle != CHRE_TIMER_INVALID);
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 == gMessageTimerHandle) {
50     if (chreIsHostAwake()) {
51       chreSendMessageToHostEndpoint(
52           gMessageBuffer, sizeof(gMessageBuffer), 1234 /* messageType */,
53           CHRE_HOST_ENDPOINT_BROADCAST, nullptr /* freeCallback */);
54     }
55   } else {
56     LOGE("Received unexpected timer event");
57   }
58 }
59 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)60 void nanoappHandleEvent(uint32_t senderInstanceId,
61                         uint16_t eventType,
62                         const void *eventData) {
63   switch (eventType) {
64     case CHRE_EVENT_TIMER:
65       handleTimerEvent(eventData);
66       break;
67     case CHRE_EVENT_HOST_AWAKE:
68       LOGI("Received host awake event");
69       break;
70     case CHRE_EVENT_HOST_ASLEEP:
71       LOGI("Received host asleep event");
72       break;
73     default:
74       LOGW("Unknown event received");
75       break;
76   }
77 }
78 
nanoappEnd()79 void nanoappEnd() {
80   LOGI("Host awake world end");
81 }
82 
83 #ifdef CHRE_NANOAPP_INTERNAL
84 }  // anonymous namespace
85 }  // namespace chre
86 
87 #include "chre/util/nanoapp/app_id.h"
88 #include "chre/platform/static_nanoapp_init.h"
89 
90 CHRE_STATIC_NANOAPP_INIT(HostAwakeWorld, chre::kHostAwakeWorldAppId, 0);
91 #endif  // CHRE_NANOAPP_INTERNAL
92