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 /**
18 * A nanoapp that will provide the host endpoint ID of a pinging client.
19 *
20 * When a message is received, this nanoapp will send back a message indicating
21 * the client's host endpoint ID.
22 */
23
24 #include <cinttypes>
25 #include <cstdint>
26 #include <cstring>
27
28 #include <chre.h>
29 #include <shared/send_message.h>
30
31 using nanoapp_testing::sendFatalFailureToHost;
32
33 namespace chre {
34 namespace {
35
36 //! A buffer to store the host endpoint ID of a messaging client.
37 uint8_t messageBuffer[2];
38
39 } // anonymous namespace
40
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)41 extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
42 uint16_t eventType, const void *eventData) {
43 if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
44 auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
45
46 if (senderInstanceId != CHRE_INSTANCE_ID) {
47 sendFatalFailureToHost("Invalid sender instance ID:", &senderInstanceId);
48 }
49
50 messageBuffer[0] = (msg->hostEndpoint & 0xff00) >> 8;
51 messageBuffer[1] = (msg->hostEndpoint & 0xff);
52 if (!chreSendMessageToHostEndpoint(static_cast<void *>(messageBuffer),
53 sizeof(messageBuffer), msg->messageType,
54 msg->hostEndpoint,
55 nullptr /* messageFreeCallback */)) {
56 sendFatalFailureToHost("Failed to send message to host");
57 }
58 }
59 }
60
nanoappStart(void)61 extern "C" bool nanoappStart(void) {
62 return true;
63 }
64
nanoappEnd(void)65 extern "C" void nanoappEnd(void) {}
66
67 } // namespace chre
68