message_world.cc revision 3807f5eba9d96cd143dfcedfef4974c5c77aa414
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
26uint8_t gMessageData[] = {1, 2, 3, 4, 5, 6, 7, 8};
27
28void messageFreeCallback(void *message, size_t messageSize) {
29  chreLog(CHRE_LOG_INFO, "Message world got message free callback for message @"
30          " %p (expected %d) size %zu (expected %d)",
31          message, (message == gMessageData),
32          messageSize, (messageSize == sizeof(gMessageData)));
33}
34
35}  // anonymous namespace
36
37bool messageWorldStart() {
38  chreLog(CHRE_LOG_INFO, "Message world app started as instance %" PRIu32,
39          chreGetInstanceId());
40  constexpr uint32_t kMessageType = 1234;
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}
69
70void messageWorldStop() {
71  chreLog(CHRE_LOG_INFO, "Message world app started");
72}
73
74}  // namespace app
75}  // namespace chre
76