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_api/chre.h" 20#include "chre/platform/assert.h" 21#include "chre/platform/log.h" 22#include "chre/platform/static_nanoapp_init.h" 23#include "chre/util/nanoapp/app_id.h" 24 25/** 26 * @file 27 * A nanoapp exclusively for testing, which sends a constant stream of messages 28 * and events. Must only be compiled as a static/internal nanoapp. 29 */ 30 31namespace chre { 32namespace { 33 34constexpr uint32_t kMessageType = 1234; 35uint8_t gMessageData[] = {1, 2, 3, 4, 5, 6, 7, 8}; 36bool gRunning = false; 37uint64_t gTotalEventCount = 0; 38 39constexpr uint16_t kMessageSentEvent = CHRE_EVENT_FIRST_USER_VALUE; 40constexpr uint16_t kRepeatedEvent = kMessageSentEvent + 1; 41constexpr uint16_t kEmptyEvent = kRepeatedEvent + 2; 42 43void *kMessageSentEventData = reinterpret_cast<void *>(0x100); 44void *kRepeatedEventData = reinterpret_cast<void *>(0x200); 45 46void messageSentEventFreeCallback(uint16_t eventType, void *data) { 47 CHRE_ASSERT(gRunning); 48 CHRE_ASSERT(eventType == kMessageSentEvent); 49 CHRE_ASSERT(data == kMessageSentEventData); 50} 51 52void messageFreeCallback(void *message, size_t messageSize) { 53 CHRE_ASSERT(gRunning); 54 if (!chreSendEvent(kMessageSentEvent, kMessageSentEventData, 55 messageSentEventFreeCallback, chreGetInstanceId())) { 56 LOGE("Couldn't send event from message free callback"); 57 } 58} 59 60void repeatedEventFreeCallback(uint16_t eventType, void *data) { 61 CHRE_ASSERT(gRunning); 62 CHRE_ASSERT(eventType == kRepeatedEvent); 63 CHRE_ASSERT(data == kRepeatedEventData); 64} 65 66bool nanoappStart() { 67 gRunning = true; 68 LOGI("Spammer started as instance %" PRIu32, chreGetInstanceId()); 69 70 if (!chreSendMessageToHostEndpoint( 71 gMessageData, sizeof(gMessageData), kMessageType, 72 CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback)) { 73 LOGE("Couldn't send message from start callback"); 74 } 75 76 if (!chreSendEvent(kRepeatedEvent, kRepeatedEventData, 77 repeatedEventFreeCallback, chreGetInstanceId())) { 78 LOGE("Couldn't send first repeated event"); 79 } 80 if (!chreSendEvent(kEmptyEvent, nullptr, nullptr, chreGetInstanceId())) { 81 LOGE("Couldn't send first empty event"); 82 } 83 84 return true; 85} 86 87void nanoappHandleEvent(uint32_t senderInstanceId, 88 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 115void nanoappEnd() { 116 LOGI("Stopped - handled %" PRIu64 " total events", gTotalEventCount); 117 gRunning = false; 118} 119 120} // anonymous namespace 121} // namespace chre 122 123CHRE_STATIC_NANOAPP_INIT(Spammer, chre::kSpammerAppId, 0); 124