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 <stdint.h>
18 #include <inttypes.h>
19 #include <chre.h>
20
21 #define APP_LABEL "CHRE App 2: "
22
23 /* chre.h does not define printf format attribute for chreLog() */
24 void chreLog(enum chreLogLevel level, const char *str, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
25
26 #define EVT_LOCAL_SETUP CHRE_EVENT_FIRST_USER_VALUE
27
28 struct MyTimer {
29 uint64_t startTime;
30 uint32_t timerId;
31 };
32
33 struct ExtMsg
34 {
35 uint8_t msg;
36 uint32_t val;
37 } __attribute__((packed));
38
39 static const uint64_t kOneSecond = UINT64_C(1000000000); // in nanoseconds
40
41 static uint32_t mMyTid;
42 static uint64_t mMyAppId;
43 static int mCnt;
44 static struct MyTimer mTimer;
45
nanoappStart(void)46 bool nanoappStart(void)
47 {
48 mMyAppId = chreGetAppId();
49 mMyTid = chreGetInstanceId();
50 mCnt = 3;
51 chreSendEvent(EVT_LOCAL_SETUP, NULL, NULL, mMyTid);
52 chreLog(CHRE_LOG_INFO, APP_LABEL "init: offset: %" PRId64, chreGetEstimatedHostTimeOffset());
53 chreConfigureNanoappInfoEvents(true);
54 return true;
55 }
56
nanoappEnd(void)57 void nanoappEnd(void)
58 {
59 chreLog(CHRE_LOG_INFO, APP_LABEL "terminating");
60 }
61
nanoappHandleEvent(uint32_t srcTid,uint16_t evtType,const void * evtData)62 void nanoappHandleEvent(uint32_t srcTid, uint16_t evtType, const void* evtData)
63 {
64 switch (evtType) {
65 case EVT_LOCAL_SETUP:
66 {
67 uint32_t instanceId = chreGetInstanceId();
68 uint64_t appId = chreGetAppId();
69 struct chreNanoappInfo info;
70
71 mTimer.startTime = chreGetTime();
72 mTimer.timerId = chreTimerSet(kOneSecond, &mTimer, false);
73 chreLog(CHRE_LOG_INFO, APP_LABEL "started with tid %04" PRIX32
74 " timerid %" PRIu32
75 "\n", mMyTid, mTimer.timerId);
76 chreLog(CHRE_LOG_INFO, APP_LABEL "appId=%016llx; instanceId=%ld",
77 appId, instanceId);
78 if (chreGetNanoappInfoByInstanceId(instanceId, &info)) {
79 chreLog(CHRE_LOG_INFO, APP_LABEL "info by instanceId; appId=%08llx; version=%ld; instanceId=%ld",
80 info.appId, info.version, info.instanceId);
81 } else {
82 chreLog(CHRE_LOG_INFO, APP_LABEL "error getting info by instance id");
83 }
84 if (chreGetNanoappInfoByAppId(appId, &info)) {
85 chreLog(CHRE_LOG_INFO, APP_LABEL "info by appId; appId=%08llx; version=%ld; instanceId=%ld",
86 info.appId, info.version, info.instanceId);
87 } else {
88 chreLog(CHRE_LOG_INFO, APP_LABEL "error getting info by app id");
89 }
90 break;
91 }
92 case CHRE_EVENT_TIMER:
93 {
94 const struct MyTimer *t = (const struct MyTimer *)evtData;
95
96 chreLog(CHRE_LOG_INFO, APP_LABEL "received timer %" PRIu32
97 " (START: %" PRIu64 " TIME: %" PRIu64 " OFFSET: %" PRId64
98 ") cnt: %d\n", t->timerId, t->startTime, chreGetTime(),
99 chreGetEstimatedHostTimeOffset(), mCnt);
100 if (mCnt-- <= 0) {
101 chreTimerCancel(t->timerId);
102 chreAbort(0x0001);
103 chreAbort(0x0002);
104 }
105 break;
106 }
107 case CHRE_EVENT_MESSAGE_FROM_HOST:
108 {
109 const struct chreMessageFromHostData *msg = (const struct chreMessageFromHostData *)evtData;
110 const uint8_t *data = (const uint8_t *)msg->message;
111 const size_t size = msg->messageSize;
112 const uint32_t type = msg->messageType;
113 const uint16_t endpoint = msg->hostEndpoint;
114 chreLog(CHRE_LOG_INFO, APP_LABEL "message=%p; code=%d; size=%zu; type=%ld; endpoint=%d",
115 data, (data && size) ? data[0] : 0, size, type, endpoint);
116 break;
117 }
118 case CHRE_EVENT_NANOAPP_STARTED:
119 {
120 const struct chreNanoappInfo *msg = (const struct chreNanoappInfo *)evtData;
121 const uint64_t appId = msg->appId;
122 const uint32_t version = msg->version;
123 const uint32_t instanceId = msg->instanceId;
124 chreLog(CHRE_LOG_INFO, APP_LABEL "app started; appId=%08llx; version=%ld; instanceId=%ld",
125 appId, version, instanceId);
126 break;
127 }
128 case CHRE_EVENT_NANOAPP_STOPPED:
129 {
130 const struct chreNanoappInfo *msg = (const struct chreNanoappInfo *)evtData;
131 const uint64_t appId = msg->appId;
132 const uint32_t version = msg->version;
133 const uint32_t instanceId = msg->instanceId;
134 chreLog(CHRE_LOG_INFO, APP_LABEL "app stopped; appId=%08llx; version=%ld; instanceId=%ld",
135 appId, version, instanceId);
136 break;
137 }
138 }
139 }
140