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[] = {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("Got message free callback for message @"
36 " %p (expected %d) size %zu (expected %d)",
37 message, (message == gMessageData),
38 messageSize, (messageSize == sizeof(gMessageData)));
39 }
40
41 } // anonymous namespace
42
nanoappStart()43 bool nanoappStart() {
44 LOGI("App started as instance %" PRIu32, chreGetInstanceId());
45
46 bool success = chreSendMessageToHostEndpoint(
47 gMessageData, sizeof(gMessageData), kMessageType,
48 CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
49 LOGI("Sent message to host from start callback, result %d", success);
50 return true;
51 }
52
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)53 void nanoappHandleEvent(uint32_t senderInstanceId,
54 uint16_t eventType,
55 const void *eventData) {
56 LOGI("Got event 0x%" PRIx16 " from instance %" PRIu32,
57 eventType, senderInstanceId);
58
59 if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
60 auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
61 LOGI("Got message from host with type %" PRIu32 " size %" PRIu32
62 " data @ %p hostEndpoint 0x%" PRIx16,
63 msg->messageType, msg->messageSize, msg->message, msg->hostEndpoint);
64 if (senderInstanceId != CHRE_INSTANCE_ID) {
65 LOGE("Message from host came from unexpected instance ID %" PRIu32,
66 senderInstanceId);
67 }
68
69 bool success = chreSendMessageToHostEndpoint(
70 gMessageData, sizeof(gMessageData), kMessageType,
71 CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
72 LOGI("Result of sending reply: %d", success);
73 }
74 }
75
nanoappEnd()76 void nanoappEnd() {
77 LOGI("Stopped");
78 }
79
80 #ifdef CHRE_NANOAPP_INTERNAL
81 } // anonymous namespace
82 } // namespace chre
83
84 #include "chre/util/nanoapp/app_id.h"
85 #include "chre/platform/static_nanoapp_init.h"
86
87 CHRE_STATIC_NANOAPP_INIT(MessageWorld, chre::kMessageWorldAppId, 0);
88 #endif // CHRE_NANOAPP_INTERNAL
89