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/macros.h"
20 #include "chre/util/nanoapp/log.h"
21
22 #define LOG_TAG "[HostAwakeWorld]"
23
24 /**
25 * A nanoapp that sets a periodic timer to send broadcast messages to the host
26 * only if it is awake.
27 */
28
29 #ifdef CHRE_NANOAPP_INTERNAL
30 namespace chre {
31 namespace {
32 #endif // CHRE_NANOAPP_INTERNAL
33
34 uint8_t gMessageBuffer[] = {0, 1, 2, 3, 4};
35
36 uint32_t gMessageTimerHandle;
37
nanoappStart()38 bool nanoappStart() {
39 LOGI("Host awake world start");
40 gMessageTimerHandle = chreTimerSet(5000000 /* 5 ms */, &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, uint16_t eventType,
61 const void *eventData) {
62 UNUSED_VAR(senderInstanceId);
63
64 switch (eventType) {
65 case CHRE_EVENT_TIMER:
66 handleTimerEvent(eventData);
67 break;
68 case CHRE_EVENT_HOST_AWAKE:
69 LOGI("Received host awake event");
70 break;
71 case CHRE_EVENT_HOST_ASLEEP:
72 LOGI("Received host asleep event");
73 break;
74 default:
75 LOGW("Unknown event received");
76 break;
77 }
78 }
79
nanoappEnd()80 void nanoappEnd() {
81 LOGI("Host awake world end");
82 }
83
84 #ifdef CHRE_NANOAPP_INTERNAL
85 } // anonymous namespace
86 } // namespace chre
87
88 #include "chre/platform/static_nanoapp_init.h"
89 #include "chre/util/nanoapp/app_id.h"
90 #include "chre/util/system/napp_permissions.h"
91
92 CHRE_STATIC_NANOAPP_INIT(HostAwakeWorld, chre::kHostAwakeWorldAppId, 0,
93 chre::NanoappPermissions::CHRE_PERMS_NONE);
94 #endif // CHRE_NANOAPP_INTERNAL
95