1 /*
2  * Copyright (C) 2017 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 <cinttypes>
18 
19 #include "chre/platform/assert.h"
20 #include "chre/platform/static_nanoapp_init.h"
21 #include "chre/util/nanoapp/app_id.h"
22 #include "chre/util/nanoapp/log.h"
23 #include "chre/util/system/napp_permissions.h"
24 #include "chre_api/chre.h"
25 
26 /**
27  * @file
28  * A nanoapp exclusively for testing, which sends a constant stream of messages
29  * and events. Must only be compiled as a static/internal nanoapp.
30  */
31 
32 namespace chre {
33 namespace {
34 
35 constexpr uint32_t kMessageType = 1234;
36 uint8_t gMessageData[] = {1, 2, 3, 4, 5, 6, 7, 8};
37 bool gRunning = false;
38 uint64_t gTotalEventCount = 0;
39 
40 constexpr uint16_t kMessageSentEvent = CHRE_EVENT_FIRST_USER_VALUE;
41 constexpr uint16_t kRepeatedEvent = kMessageSentEvent + 1;
42 constexpr uint16_t kEmptyEvent = kRepeatedEvent + 2;
43 
44 void *kMessageSentEventData = reinterpret_cast<void *>(0x100);
45 void *kRepeatedEventData = reinterpret_cast<void *>(0x200);
46 
messageSentEventFreeCallback(uint16_t eventType,void * data)47 void messageSentEventFreeCallback(uint16_t eventType, void *data) {
48   CHRE_ASSERT(gRunning);
49   CHRE_ASSERT(eventType == kMessageSentEvent);
50   CHRE_ASSERT(data == kMessageSentEventData);
51 }
52 
messageFreeCallback(void * message,size_t messageSize)53 void messageFreeCallback(void *message, size_t messageSize) {
54   CHRE_ASSERT(gRunning);
55   if (!chreSendEvent(kMessageSentEvent, kMessageSentEventData,
56                      messageSentEventFreeCallback, chreGetInstanceId())) {
57     LOGE("Couldn't send event from message free callback");
58   }
59 }
60 
repeatedEventFreeCallback(uint16_t eventType,void * data)61 void repeatedEventFreeCallback(uint16_t eventType, void *data) {
62   CHRE_ASSERT(gRunning);
63   CHRE_ASSERT(eventType == kRepeatedEvent);
64   CHRE_ASSERT(data == kRepeatedEventData);
65 }
66 
nanoappStart()67 bool nanoappStart() {
68   gRunning = true;
69   LOGI("Spammer started as instance %" PRIu32, chreGetInstanceId());
70 
71   if (!chreSendMessageToHostEndpoint(gMessageData, sizeof(gMessageData),
72                                      kMessageType, CHRE_HOST_ENDPOINT_BROADCAST,
73                                      messageFreeCallback)) {
74     LOGE("Couldn't send message from start callback");
75   }
76 
77   if (!chreSendEvent(kRepeatedEvent, kRepeatedEventData,
78                      repeatedEventFreeCallback, chreGetInstanceId())) {
79     LOGE("Couldn't send first repeated event");
80   }
81   if (!chreSendEvent(kEmptyEvent, nullptr, nullptr, chreGetInstanceId())) {
82     LOGE("Couldn't send first empty event");
83   }
84 
85   return true;
86 }
87 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)88 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
89                         const void *eventData) {
90   CHRE_ASSERT(gRunning);
91   gTotalEventCount++;
92   if (eventType == kMessageSentEvent) {
93     CHRE_ASSERT(eventData == kMessageSentEventData);
94     if (!chreSendMessageToHostEndpoint(
95             gMessageData, sizeof(gMessageData), kMessageType,
96             CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback)) {
97       // Note: commented out to prevent logspam because Linux simulator
98       // currently always returns false
99       // LOGE("Couldn't send message");
100     }
101   } else if (eventType == kRepeatedEvent) {
102     CHRE_ASSERT(eventData == kRepeatedEventData);
103     if (!chreSendEvent(kRepeatedEvent, kRepeatedEventData,
104                        repeatedEventFreeCallback, chreGetInstanceId())) {
105       LOGE("Couldn't send repeated event");
106     }
107   } else if (eventType == kEmptyEvent) {
108     CHRE_ASSERT(eventData == nullptr);
109     if (!chreSendEvent(kEmptyEvent, nullptr, nullptr, chreGetInstanceId())) {
110       LOGE("Couldn't send empty event");
111     }
112   }
113 }
114 
nanoappEnd()115 void nanoappEnd() {
116   LOGI("Stopped - handled %" PRIu64 " total events", gTotalEventCount);
117   gRunning = false;
118 }
119 
120 }  // anonymous namespace
121 }  // namespace chre
122 
123 CHRE_STATIC_NANOAPP_INIT(Spammer, chre::kSpammerAppId, 0,
124                          chre::NanoappPermissions::CHRE_PERMS_NONE);
125