aboutsummaryrefslogtreecommitdiff
path: root/host/common/host_protocol_host.cc
blob: 21904f58552b3fde6fb3279089105584d8fb5553 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (C) 2017 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_host/host_protocol_host.h"

#include <inttypes.h>
#include <string.h>

#include "chre_host/log.h"

using flatbuffers::FlatBufferBuilder;
using flatbuffers::Offset;

// Aliased for consistency with the way these symbols are referenced in
// CHRE-side code
namespace fbs = ::chre::fbs;

namespace android {
namespace chre {

// This is similar to getStringFromByteVector in host_protocol_chre.h. Ensure
// that method's implementation is kept in sync with this.
const char *getStringFromByteVector(const std::vector<int8_t> &vec) {
  constexpr int8_t kNullChar = static_cast<int8_t>('\0');
  const char *str = nullptr;

  // Check that the vector is present, non-empty, and null-terminated
  if (vec.size() > 0 && vec[vec.size() - 1] == kNullChar) {
    str = reinterpret_cast<const char *>(vec.data());
  }

  return str;
}

bool HostProtocolHost::decodeMessageFromChre(const void *message,
                                             size_t messageLen,
                                             IChreMessageHandlers &handlers) {
  bool success = verifyMessage(message, messageLen);
  if (success) {
    std::unique_ptr<fbs::MessageContainerT> container =
        fbs::UnPackMessageContainer(message);
    fbs::ChreMessageUnion &msg = container->message;

    switch (container->message.type) {
      case fbs::ChreMessage::NanoappMessage:
        handlers.handleNanoappMessage(*msg.AsNanoappMessage());
        break;

      case fbs::ChreMessage::HubInfoResponse:
        handlers.handleHubInfoResponse(*msg.AsHubInfoResponse());
        break;

      case fbs::ChreMessage::NanoappListResponse:
        handlers.handleNanoappListResponse(*msg.AsNanoappListResponse());
        break;

      case fbs::ChreMessage::LoadNanoappResponse:
        handlers.handleLoadNanoappResponse(*msg.AsLoadNanoappResponse());
        break;

      case fbs::ChreMessage::UnloadNanoappResponse:
        handlers.handleUnloadNanoappResponse(*msg.AsUnloadNanoappResponse());
        break;

      case fbs::ChreMessage::DebugDumpData:
        handlers.handleDebugDumpData(*msg.AsDebugDumpData());
        break;

      case fbs::ChreMessage::DebugDumpResponse:
        handlers.handleDebugDumpResponse(*msg.AsDebugDumpResponse());
        break;

      default:
        LOGW("Got invalid/unexpected message type %" PRIu8,
             static_cast<uint8_t>(msg.type));
        success = false;
    }
  }

  return success;
}

void HostProtocolHost::encodeHubInfoRequest(FlatBufferBuilder &builder) {
  auto request = fbs::CreateHubInfoRequest(builder);
  finalize(builder, fbs::ChreMessage::HubInfoRequest, request.Union());
}

void HostProtocolHost::encodeFragmentedLoadNanoappRequest(
    flatbuffers::FlatBufferBuilder &builder,
    const FragmentedLoadRequest &request) {
  encodeLoadNanoappRequestForBinary(
      builder, request.transactionId, request.appId, request.appVersion,
      request.targetApiVersion, request.binary, request.fragmentId,
      request.appTotalSizeBytes);
}

void HostProtocolHost::encodeNanoappListRequest(FlatBufferBuilder &builder) {
  auto request = fbs::CreateNanoappListRequest(builder);
  finalize(builder, fbs::ChreMessage::NanoappListRequest, request.Union());
}

void HostProtocolHost::encodeUnloadNanoappRequest(
    FlatBufferBuilder &builder, uint32_t transactionId, uint64_t appId,
    bool allowSystemNanoappUnload) {
  auto request = fbs::CreateUnloadNanoappRequest(builder, transactionId, appId,
                                                 allowSystemNanoappUnload);
  finalize(builder, fbs::ChreMessage::UnloadNanoappRequest, request.Union());
}

void HostProtocolHost::encodeTimeSyncMessage(FlatBufferBuilder &builder,
                                             int64_t offset) {
  auto request = fbs::CreateTimeSyncMessage(builder, offset);
  finalize(builder, fbs::ChreMessage::TimeSyncMessage, request.Union());
}

void HostProtocolHost::encodeDebugDumpRequest(FlatBufferBuilder &builder) {
  auto request = fbs::CreateDebugDumpRequest(builder);
  finalize(builder, fbs::ChreMessage::DebugDumpRequest, request.Union());
}

bool HostProtocolHost::extractHostClientIdAndType(
    const void *message, size_t messageLen, uint16_t *hostClientId,
    ::chre::fbs::ChreMessage *messageType) {
  bool success = false;
  if (hostClientId != nullptr && messageType != nullptr) {
    success = verifyMessage(message, messageLen);

    if (success) {
      const fbs::MessageContainer *container =
          fbs::GetMessageContainer(message);
      // host_addr guaranteed to be non-null via verifyMessage (it's a required
      // field)
      *hostClientId = container->host_addr()->client_id();
      *messageType = container->message_type();
    }
  }

  return success;
}

bool HostProtocolHost::mutateHostClientId(void *message, size_t messageLen,
                                          uint16_t hostClientId) {
  bool success = verifyMessage(message, messageLen);

  if (!success) {
    LOGE("Message verification failed - can't mutate host ID");
  } else {
    fbs::MessageContainer *container = fbs::GetMutableMessageContainer(message);
    // host_addr guaranteed to be non-null via verifyMessage (it's a required
    // field)
    container->mutable_host_addr()->mutate_client_id(hostClientId);
    success = true;
  }

  return success;
}

void HostProtocolHost::encodeLoadNanoappRequestForBinary(
    FlatBufferBuilder &builder, uint32_t transactionId, uint64_t appId,
    uint32_t appVersion, uint32_t targetApiVersion,
    const std::vector<uint8_t> &nanoappBinary, uint32_t fragmentId,
    size_t appTotalSizeBytes) {
  auto appBinary = builder.CreateVector(nanoappBinary);
  auto request = fbs::CreateLoadNanoappRequest(
      builder, transactionId, appId, appVersion, targetApiVersion, appBinary,
      fragmentId, appTotalSizeBytes);
  finalize(builder, fbs::ChreMessage::LoadNanoappRequest, request.Union());
}

void HostProtocolHost::encodeLoadNanoappRequestForFile(
    flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId,
    uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion,
    const char *nanoappBinaryName) {
  const std::vector<uint8_t> emptyAppBinary;
  auto appBinary = builder.CreateVector(emptyAppBinary);
  auto appBinaryName = addStringAsByteVector(builder, nanoappBinaryName);
  auto request = fbs::CreateLoadNanoappRequest(
      builder, transactionId, appId, appVersion, targetApiVersion, appBinary,
      0 /* fragmentId */, 0 /* appTotalSizeBytes */, appBinaryName);
  finalize(builder, fbs::ChreMessage::LoadNanoappRequest, request.Union());
}

void HostProtocolHost::encodeSettingChangeNotification(
    flatbuffers::FlatBufferBuilder &builder, ::chre::fbs::Setting setting,
    ::chre::fbs::SettingState newState) {
  auto notification =
      fbs::CreateSettingChangeMessage(builder, setting, newState);
  finalize(builder, fbs::ChreMessage::SettingChangeMessage,
           notification.Union());
}

}  // namespace chre
}  // namespace android