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 <shared/send_message.h>
29 
30 #include "chre_api/chre.h"
31 
32 using nanoapp_testing::sendFatalFailureToHost;
33 
34 namespace chre {
35 namespace {
36 
37 //! A buffer to store the host endpoint ID of a messaging client.
38 uint8_t messageBuffer[2];
39 
40 }  // anonymous namespace
41 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)42 extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
43                                    uint16_t eventType, 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(static_cast<void *>(messageBuffer),
54                                        sizeof(messageBuffer), msg->messageType,
55                                        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 }  // namespace chre
69