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