1 /*
2  * Copyright (C) 2021 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 #include <cinttypes>
19 
20 #include <pb_decode.h>
21 
22 #include "chre/util/nanoapp/log.h"
23 #include "ping_test.nanopb.h"
24 #include "send_message.h"
25 
26 #define LOG_TAG "[PingTest]"
27 
28 namespace chre {
29 
30 namespace {
31 
32 //! The message payload to use when responding to a ping request.
33 const char *kPingResponseMsg = "Pong!";
34 
handleMessageFromHost(uint32_t senderInstanceId,const chreMessageFromHostData * hostData)35 void handleMessageFromHost(uint32_t senderInstanceId,
36                            const chreMessageFromHostData *hostData) {
37   bool success = false;
38   uint32_t messageType = hostData->messageType;
39   if (senderInstanceId != CHRE_INSTANCE_ID) {
40     LOGE("Incorrect sender instance id: %" PRIu32, senderInstanceId);
41   } else if (messageType != ping_test_MessageType_PING_COMMAND) {
42     LOGE("Invalid message type %" PRIu32, messageType);
43   } else {
44     pb_istream_t istream = pb_istream_from_buffer(
45         static_cast<const pb_byte_t *>(hostData->message),
46         hostData->messageSize);
47     ping_test_PingCommand command = ping_test_PingCommand_init_default;
48 
49     if (!pb_decode(&istream, ping_test_PingCommand_fields, &command)) {
50       LOGE("Failed to decode ping command error %s", PB_GET_ERROR(&istream));
51     } else {
52       uint32_t permissions = command.permissions;
53       LOGI("Got ping command message with permission 0x%" PRIx32, permissions);
54       success = chreSendMessageWithPermissions(
55           const_cast<char *>(kPingResponseMsg), strlen(kPingResponseMsg) + 1,
56           ping_test_MessageType_PING_RESPONSE, hostData->hostEndpoint,
57           permissions, nullptr /* freeCallback */);
58     }
59   }
60 
61   if (!success) {
62     test_shared::sendTestResultToHost(
63         hostData->hostEndpoint,
64         ping_test_MessageType_TEST_RESULT /* messageType */,
65         false /* success */);
66   }
67 }
68 
69 }  // anonymous namespace
70 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)71 extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
72                                    uint16_t eventType, const void *eventData) {
73   if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
74     handleMessageFromHost(
75         senderInstanceId,
76         static_cast<const chreMessageFromHostData *>(eventData));
77   } else {
78     LOGW("Got unknown event type from senderInstanceId %" PRIu32
79          " and with eventType %" PRIu16,
80          senderInstanceId, eventType);
81   }
82 }
83 
nanoappStart(void)84 extern "C" bool nanoappStart(void) {
85   return true;
86 }
87 
nanoappEnd(void)88 extern "C" void nanoappEnd(void) {}
89 
90 }  // namespace chre
91