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 <cinttypes>
18
19 #include "chre/util/macros.h"
20 #include "chre/util/nanoapp/log.h"
21 #include "chre_api/chre.h"
22
23 #define LOG_TAG "[MsgWorld]"
24
25 #ifdef CHRE_NANOAPP_INTERNAL
26 namespace chre {
27 namespace {
28 #endif // CHRE_NANOAPP_INTERNAL
29
30 namespace {
31
32 enum MessageType { kDefault = 1, kCustomReplyMessageSize = 2 };
33
34 #ifdef CHRE_RELIABLE_MESSAGE_SUPPORT_ENABLED
35 constexpr uint32_t gMaxReplyMessageSize = CHRE_LARGE_PAYLOAD_MAX_SIZE;
36 #else
37 constexpr uint32_t gMaxReplyMessageSize = CHRE_MESSAGE_TO_HOST_MAX_SIZE;
38 #endif
39
40 uint8_t gMessageData[gMaxReplyMessageSize] = {};
41
messageFreeCallback(void * message,size_t messageSize)42 void messageFreeCallback(void *message, size_t messageSize) {
43 LOGI("Got message free callback for message @ %p (%s) size %zu", message,
44 (message == gMessageData) ? "matched" : "unmatched", messageSize);
45 if (!chreSendEvent(CHRE_EVENT_FIRST_USER_VALUE, /* eventData= */ nullptr,
46 /* freeCallback= */ nullptr, chreGetInstanceId())) {
47 LOGE("Failed to send event");
48 }
49 }
50
51 } // anonymous namespace
52
nanoappStart()53 bool nanoappStart() {
54 LOGI("App started as instance %" PRIu32, chreGetInstanceId());
55 // initialize gMessageData
56 for (uint32_t i = 0; i < gMaxReplyMessageSize; ++i) {
57 gMessageData[i] = i % 10;
58 }
59 bool success = chreSendMessageToHostEndpoint(
60 gMessageData, sizeof(gMessageData), MessageType::kDefault,
61 CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
62 LOGI("Sent message of size %zu to host from start callback: %s",
63 sizeof(gMessageData), success ? "success" : "failure");
64 return true;
65 }
66
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)67 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
68 const void *eventData) {
69 if (eventType != CHRE_EVENT_MESSAGE_FROM_HOST) {
70 return;
71 }
72 auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
73 LOGI("Got message from host with type %" PRIu32 " size %" PRIu32
74 " data @ %p hostEndpoint 0x%" PRIx16,
75 msg->messageType, msg->messageSize, msg->message, msg->hostEndpoint);
76 if (senderInstanceId != CHRE_INSTANCE_ID) {
77 LOGE("Message from host came from unexpected instance ID %" PRIu32,
78 senderInstanceId);
79 }
80
81 uint32_t messageSize = gMaxReplyMessageSize;
82 if (msg->messageType == MessageType::kCustomReplyMessageSize) {
83 messageSize =
84 MIN(messageSize, *(static_cast<const uint32_t *>(msg->message)));
85 }
86
87 bool success = chreSendMessageToHostEndpoint(
88 gMessageData, messageSize, MessageType::kDefault, msg->hostEndpoint,
89 messageFreeCallback);
90 LOGI("Result of sending reply (size=%" PRIu32 "): %s", messageSize,
91 success ? "success" : "failure");
92 }
93
nanoappEnd()94 void nanoappEnd() {
95 LOGI("Stopped");
96 }
97
98 #ifdef CHRE_NANOAPP_INTERNAL
99 } // anonymous namespace
100 } // namespace chre
101
102 #include "chre/platform/static_nanoapp_init.h"
103 #include "chre/util/nanoapp/app_id.h"
104 #include "chre/util/system/napp_permissions.h"
105
106 CHRE_STATIC_NANOAPP_INIT(MessageWorld, chre::kMessageWorldAppId, 0,
107 chre::NanoappPermissions::CHRE_PERMS_NONE);
108 #endif // CHRE_NANOAPP_INTERNAL
109