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 #ifndef _GTS_NANOAPPS_GENERAL_TEST_SEND_MESSAGE_TO_HOST_TEST_H_
18 #define _GTS_NANOAPPS_GENERAL_TEST_SEND_MESSAGE_TO_HOST_TEST_H_
19 
20 #include <general_test/test.h>
21 
22 #include <chre.h>
23 
24 namespace general_test {
25 
26 /**
27  * Check chreSendMessageToHost() works, along with an empty message from the
28  * host to the nanoapp.
29  *
30  * TODO(b/32114261): This test is way more complicated than it should be.
31  *     Specifically, the standard workaround for this bug involves
32  *     putting more data within the 'message' to/from host/nanoapp.  But
33  *     since we're specifically testing that data, we can't use that
34  *     workaround.  When that workaround is gone, we can make this test
35  *     much simpler.
36  *
37  * Protocol:
38  * Host:    kSendMessageToHostTest, no data
39  * Nanoapp: 3 bytes of 0xFE
40  * Nanoapp: 3 bytes of 0xFE
41  * Nanoapp: 3 bytes of 0xFE
42  * Nanoapp: 3 bytes of 0xFE
43  * Nanoapp: 0 bytes
44  * Nanoapp: kContinue, 4 bytes (little endian) with <MessageMaxSize>
45  * Nanoapp: <MessageMaxSize> bytes of 0xFE
46  * Host:    0 bytes
47  * [nanoapp waits for all 'freeCallback's to have been invoked]
48  * Nanoapp: kSuccess
49  */
50 class SendMessageToHostTest : public Test {
51  public:
52   SendMessageToHostTest();
53 
54  protected:
55   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
56                    const void* eventData) override;
57   void setUp(uint32_t messageSize, const void *message) override;
58 
59  private:
60   // Note, most of our data and methods are static, because much of our test
61   // logic happens in callbacks which must be static members.  This class
62   // instance is a singleton, so there's no issue with this approach.
63 
64   static constexpr uint8_t kDataByte = UINT8_C(0xFE);
65 
66   static constexpr uint32_t kSmallMessageSize = 3;
67   static constexpr size_t kSmallMessageTestCount = 4;
68   static uint8_t sSmallMessageData[kSmallMessageTestCount][kSmallMessageSize];
69 
70   static constexpr uint32_t kLargeSizes[2] = {
71     CHRE_MESSAGE_TO_HOST_MAX_SIZE + 1,
72     CHRE_MESSAGE_TO_HOST_MAX_SIZE
73   };
74   static void *sLargeMessageData[2];
75 
76   // Catch if CHRE implementation illegally reenters nanoapp code.
77   static bool sInMethod;
78 
79   // We have nine stages.  We set a bit in our finishedBitmask
80   // when each has succeeded.
81   static constexpr uint32_t kAllFinished = (1 << 9) - 1;
82   static uint32_t sFinishedBitmask;
83 
84   template<uint8_t kCallbackIndex>
85   static void smallMessageCallback(void *message, size_t messageSize);
86 
87   static void smallMessageCallback0(void *message, size_t messageSize);
88   static void smallMessageCallback1(void *message, size_t messageSize);
89 
90   static void largeMessageCallback(void *message, size_t messageSize);
91 
92   static uint32_t getSmallDataIndex(const uint8_t *data);
93 
94   static void markSuccess(uint32_t stage);
95 
96   static bool sendMessageToHost(void *message, uint32_t messageSize,
97                                 uint32_t reservedMessageType,
98                                 chreMessageFreeFunction *freeCallback);
99 
100   void prepTestMemory();
101   void sendMessageMaxSize();
102 };
103 
104 }  // namespace general_test
105 
106 
107 #endif  // _GTS_NANOAPPS_GENERAL_TEST_SEND_MESSAGE_TO_HOST_TEST_H_
108