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 */, &gMessageTimerHandle,
40 false /* oneShot */);
41 chreConfigureHostSleepStateEvents(true /* enable */);
42
43 return (gMessageTimerHandle != CHRE_TIMER_INVALID);
44 }
45
handleTimerEvent(const void * eventData)46 void handleTimerEvent(const void *eventData) {
47 const uint32_t *timerHandle = static_cast<const uint32_t *>(eventData);
48 if (*timerHandle == gMessageTimerHandle) {
49 if (chreIsHostAwake()) {
50 chreSendMessageToHostEndpoint(
51 gMessageBuffer, sizeof(gMessageBuffer), 1234 /* messageType */,
52 CHRE_HOST_ENDPOINT_BROADCAST, nullptr /* freeCallback */);
53 }
54 } else {
55 LOGE("Received unexpected timer event");
56 }
57 }
58
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)59 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
60 const void *eventData) {
61 switch (eventType) {
62 case CHRE_EVENT_TIMER:
63 handleTimerEvent(eventData);
64 break;
65 case CHRE_EVENT_HOST_AWAKE:
66 LOGI("Received host awake event");
67 break;
68 case CHRE_EVENT_HOST_ASLEEP:
69 LOGI("Received host asleep event");
70 break;
71 default:
72 LOGW("Unknown event received");
73 break;
74 }
75 }
76
nanoappEnd()77 void nanoappEnd() {
78 LOGI("Host awake world end");
79 }
80
81 #ifdef CHRE_NANOAPP_INTERNAL
82 } // anonymous namespace
83 } // namespace chre
84
85 #include "chre/platform/static_nanoapp_init.h"
86 #include "chre/util/nanoapp/app_id.h"
87 #include "chre/util/system/napp_permissions.h"
88
89 CHRE_STATIC_NANOAPP_INIT(HostAwakeWorld, chre::kHostAwakeWorldAppId, 0,
90 chre::NanoappPermissions::CHRE_PERMS_NONE);
91 #endif // CHRE_NANOAPP_INTERNAL
92