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