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