aboutsummaryrefslogtreecommitdiff
path: root/apps/message_world/message_world.cc
blob: f844f1ae3d5fa9c34c402afb412cd985549207b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <chre.h>
#include <cinttypes>

#include "chre/util/nanoapp/log.h"

#define LOG_TAG "[MsgWorld]"

#ifdef CHRE_NANOAPP_INTERNAL
namespace chre {
namespace {
#endif  // CHRE_NANOAPP_INTERNAL

namespace {

constexpr uint32_t kMessageType = 1234;
uint8_t gMessageData[CHRE_MESSAGE_TO_HOST_MAX_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8};

void messageFreeCallback(void *message, size_t messageSize) {
  LOGI(
      "Got message free callback for message @ %p (match? %d) size %zu (match?"
      " %d)",
      message, (message == gMessageData), messageSize,
      (messageSize == sizeof(gMessageData)));
  if (!chreSendEvent(CHRE_EVENT_FIRST_USER_VALUE, nullptr, nullptr,
                     chreGetInstanceId())) {
    LOGE("Failed to send event");
  }
}

}  // anonymous namespace

bool nanoappStart() {
  LOGI("App started as instance %" PRIu32, chreGetInstanceId());

  bool success = chreSendMessageToHostEndpoint(
      gMessageData, sizeof(gMessageData), kMessageType,
      CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
  LOGI("Sent message to host from start callback, result %d", success);
  return true;
}

void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
                        const void *eventData) {
  if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
    auto *msg = static_cast<const chreMessageFromHostData *>(eventData);
    LOGI("Got message from host with type %" PRIu32 " size %" PRIu32
         " data @ %p hostEndpoint 0x%" PRIx16,
         msg->messageType, msg->messageSize, msg->message, msg->hostEndpoint);
    if (senderInstanceId != CHRE_INSTANCE_ID) {
      LOGE("Message from host came from unexpected instance ID %" PRIu32,
           senderInstanceId);
    }

    bool success = chreSendMessageToHostEndpoint(
        gMessageData, sizeof(gMessageData), kMessageType,
        CHRE_HOST_ENDPOINT_BROADCAST, messageFreeCallback);
    LOGI("Result of sending reply: %d", success);
  }
}

void nanoappEnd() {
  LOGI("Stopped");
}

#ifdef CHRE_NANOAPP_INTERNAL
}  // anonymous namespace
}  // namespace chre

#include "chre/platform/static_nanoapp_init.h"
#include "chre/util/nanoapp/app_id.h"
#include "chre/util/system/napp_permissions.h"

CHRE_STATIC_NANOAPP_INIT(MessageWorld, chre::kMessageWorldAppId, 0,
                         chre::NanoappPermissions::CHRE_PERMS_NONE);
#endif  // CHRE_NANOAPP_INTERNAL