message_world.cc revision 3d0e53bc6419ee6110ca75ab30f369856fe5b28d
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.h"
20
21namespace chre {
22namespace app {
23
24namespace {
25
26constexpr uint32_t kMessageType = 1234;
27uint8_t gMessageData[] = {1, 2, 3, 4, 5, 6, 7, 8};
28
29void messageFreeCallback(void *message, size_t messageSize) {
30  chreLog(CHRE_LOG_INFO, "Message world got message free callback for message @"
31          " %p (expected %d) size %zu (expected %d)",
32          message, (message == gMessageData),
33          messageSize, (messageSize == sizeof(gMessageData)));
34}
35
36}  // anonymous namespace
37
38bool messageWorldStart() {
39  chreLog(CHRE_LOG_INFO, "Message world app started as instance %" PRIu32,
40          chreGetInstanceId());
41
42  bool success = chreSendMessageToHostEndpoint(
43      gMessageData, sizeof(gMessageData), kMessageType,
44      CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
45  chreLog(CHRE_LOG_INFO, "Sent message to host from start callback, result %d",
46          success);
47
48  return true;
49}
50
51void messageWorldHandleEvent(uint32_t senderInstanceId,
52                             uint16_t eventType,
53                             const void *eventData) {
54  chreLog(CHRE_LOG_INFO, "Message world got event 0x%" PRIx16 " from instance "
55          "%" PRIu32, eventType, senderInstanceId);
56
57  if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
58    auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
59    chreLog(CHRE_LOG_INFO, "Message world got message from host with type %"
60            PRIu32 " size %" PRIu32 " data @ %p hostEndpoint 0x%" PRIx16,
61            msg->messageType, msg->messageSize, msg->message,
62            msg->hostEndpoint);
63    if (senderInstanceId != CHRE_INSTANCE_ID) {
64      chreLog(CHRE_LOG_ERROR, "Message from host came from unexpected instance "
65              "ID %" PRIu32, senderInstanceId);
66    }
67
68    bool success = chreSendMessageToHostEndpoint(
69      gMessageData, sizeof(gMessageData), kMessageType,
70      CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
71    chreLog(CHRE_LOG_INFO, "Result of sending reply: %d", success);
72  }
73}
74
75void messageWorldStop() {
76  chreLog(CHRE_LOG_INFO, "Message world app stopped");
77}
78
79}  // namespace app
80}  // namespace chre
81