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 <general_test/event_between_apps_test.h>
18
19 #include <general_test/nanoapp_info.h>
20
21 #include <cstddef>
22
23 #include <shared/abort.h>
24 #include <shared/macros.h>
25 #include <shared/nano_endian.h>
26 #include <shared/nano_string.h>
27 #include <shared/send_message.h>
28
29 #include "chre_api/chre.h"
30
31 using nanoapp_testing::MessageType;
32
33 using nanoapp_testing::sendSuccessToHost;
34
35 namespace general_test {
36
37 // Arbitrary, just to confirm our data is properly sent.
38 const uint32_t EventBetweenApps0::kMagic = UINT32_C(0x51501984);
39
EventBetweenApps0()40 EventBetweenApps0::EventBetweenApps0()
41 : Test(CHRE_API_VERSION_1_0), mContinueCount(0) {}
42
setUp(uint32_t messageSize,const void *)43 void EventBetweenApps0::setUp(uint32_t messageSize,
44 const void * /* message */) {
45 if (messageSize != 0) {
46 EXPECT_FAIL_RETURN("Initial message expects 0 additional bytes, got ",
47 &messageSize);
48 }
49
50 NanoappInfo info;
51 info.sendToHost();
52 }
53
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)54 void EventBetweenApps0::handleEvent(uint32_t senderInstanceId,
55 uint16_t eventType, const void *eventData) {
56 uint32_t app1InstanceId;
57 const void *message = getMessageDataFromHostEvent(
58 senderInstanceId, eventType, eventData, MessageType::kContinue,
59 sizeof(app1InstanceId));
60 if (mContinueCount > 0) {
61 EXPECT_FAIL_RETURN("Multiple kContinue messages sent");
62 }
63
64 mContinueCount++;
65 nanoapp_testing::memcpy(&app1InstanceId, message, sizeof(app1InstanceId));
66 app1InstanceId = nanoapp_testing::littleEndianToHost(app1InstanceId);
67 // It's safe to strip the 'const' because we're using nullptr for our
68 // free callback.
69 uint32_t *sendData = const_cast<uint32_t *>(&kMagic);
70 // Send an event to app1. Note since app1 is on the same system, there are
71 // no endian concerns for our sendData.
72 chreSendEvent(kEventType, sendData, nullptr, app1InstanceId);
73 }
74
EventBetweenApps1()75 EventBetweenApps1::EventBetweenApps1()
76 : Test(CHRE_API_VERSION_1_0),
77 mApp0InstanceId(CHRE_INSTANCE_ID),
78 mReceivedInstanceId(CHRE_INSTANCE_ID) {}
79
setUp(uint32_t messageSize,const void *)80 void EventBetweenApps1::setUp(uint32_t messageSize,
81 const void * /* message */) {
82 if (messageSize != 0) {
83 EXPECT_FAIL_RETURN("Initial message expects 0 additional bytes, got ",
84 &messageSize);
85 }
86
87 NanoappInfo appInfo;
88 appInfo.sendToHost();
89 }
90
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)91 void EventBetweenApps1::handleEvent(uint32_t senderInstanceId,
92 uint16_t eventType, const void *eventData) {
93 if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
94 const void *message = getMessageDataFromHostEvent(
95 senderInstanceId, eventType, eventData, MessageType::kContinue,
96 sizeof(mApp0InstanceId));
97 // We expect kContinue once, with the app0's instance ID as data.
98 if (mApp0InstanceId != CHRE_INSTANCE_ID) {
99 // We know app0's instance ID can't be CHRE_INSTANCE_ID, otherwise
100 // we would have aborted this test in commonInit().
101 EXPECT_FAIL_RETURN("Multiple kContinue messages from host.");
102 }
103 nanoapp_testing::memcpy(&mApp0InstanceId, message, sizeof(mApp0InstanceId));
104 mApp0InstanceId = nanoapp_testing::littleEndianToHost(mApp0InstanceId);
105
106 } else if (eventType == EventBetweenApps0::kEventType) {
107 if (mReceivedInstanceId != CHRE_INSTANCE_ID) {
108 EXPECT_FAIL_RETURN("Multiple messages from other nanoapp.");
109 }
110 if (senderInstanceId == CHRE_INSTANCE_ID) {
111 EXPECT_FAIL_RETURN(
112 "Received event from other nanoapp with CHRE_INSTANCE_ID for sender");
113 }
114 mReceivedInstanceId = senderInstanceId;
115 uint32_t magic;
116 nanoapp_testing::memcpy(&magic, eventData, sizeof(magic));
117 if (magic != EventBetweenApps0::kMagic) {
118 EXPECT_FAIL_RETURN("Got incorrect magic data: ", &magic);
119 }
120
121 } else {
122 unexpectedEvent(eventType);
123 }
124
125 if ((mApp0InstanceId != CHRE_INSTANCE_ID) &&
126 (mReceivedInstanceId != CHRE_INSTANCE_ID)) {
127 if (mApp0InstanceId == mReceivedInstanceId) {
128 sendSuccessToHost();
129 } else {
130 EXPECT_FAIL_RETURN("Got bad sender instance ID for nanoapp event: ",
131 &mReceivedInstanceId);
132 }
133 }
134 }
135
136 } // namespace general_test
137