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
25namespace chre {
26namespace {
27#endif  // CHRE_NANOAPP_INTERNAL
28
29namespace {
30
31constexpr uint32_t kMessageType = 1234;
32uint8_t gMessageData[CHRE_MESSAGE_TO_HOST_MAX_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8};
33
34void messageFreeCallback(void *message, size_t messageSize) {
35  LOGI("Got message free callback for message @"
36       " %p (match? %d) size %zu (match? %d)",
37       message, (message == gMessageData),
38       messageSize, (messageSize == sizeof(gMessageData)));
39  if (!chreSendEvent(CHRE_EVENT_FIRST_USER_VALUE, nullptr, nullptr,
40                     chreGetInstanceId())) {
41    LOGE("Failed to send event");
42  }
43}
44
45}  // anonymous namespace
46
47bool nanoappStart() {
48  LOGI("App started as instance %" PRIu32, chreGetInstanceId());
49
50  bool success = chreSendMessageToHostEndpoint(
51      gMessageData, sizeof(gMessageData), kMessageType,
52      CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
53  LOGI("Sent message to host from start callback, result %d", success);
54  return true;
55}
56
57void nanoappHandleEvent(uint32_t senderInstanceId,
58                        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
77void nanoappEnd() {
78  LOGI("Stopped");
79}
80
81#ifdef CHRE_NANOAPP_INTERNAL
82}  // anonymous namespace
83}  // namespace chre
84
85#include "chre/util/nanoapp/app_id.h"
86#include "chre/platform/static_nanoapp_init.h"
87
88CHRE_STATIC_NANOAPP_INIT(MessageWorld, chre::kMessageWorldAppId, 0);
89#endif  // CHRE_NANOAPP_INTERNAL
90