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,
43                                    const void* eventData) {
44   if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
45     auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
46 
47     if (senderInstanceId != CHRE_INSTANCE_ID) {
48       sendFatalFailureToHost("Invalid sender instance ID:", &senderInstanceId);
49     }
50 
51     messageBuffer[0] = (msg->hostEndpoint & 0xff00) >> 8;
52     messageBuffer[1] = (msg->hostEndpoint & 0xff);
53     if (!chreSendMessageToHostEndpoint(
54             static_cast<void*>(messageBuffer), sizeof(messageBuffer),
55             msg->messageType, msg->hostEndpoint,
56             nullptr /* messageFreeCallback */)) {
57       sendFatalFailureToHost("Failed to send message to host");
58     }
59   }
60 }
61 
nanoappStart(void)62 extern "C" bool nanoappStart(void) {
63   return true;
64 }
65 
nanoappEnd(void)66 extern "C" void nanoappEnd(void) {
67 }
68 
69 }  // namespace chre
70