1 /*
2 * Copyright (C) 2017 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/platform/shared/host_protocol_common.h"
18
19 #include <string.h>
20
21 #include "chre/platform/shared/host_messages_generated.h"
22
23 using flatbuffers::FlatBufferBuilder;
24 using flatbuffers::Offset;
25 using flatbuffers::Vector;
26
27 namespace chre {
28
encodeNanoappMessage(FlatBufferBuilder & builder,uint64_t appId,uint32_t messageType,uint16_t hostEndpoint,const void * messageData,size_t messageDataLen)29 void HostProtocolCommon::encodeNanoappMessage(
30 FlatBufferBuilder& builder, uint64_t appId, uint32_t messageType,
31 uint16_t hostEndpoint, const void *messageData, size_t messageDataLen) {
32 auto messageDataOffset = builder.CreateVector(
33 static_cast<const uint8_t *>(messageData), messageDataLen);
34
35 auto nanoappMessage = fbs::CreateNanoappMessage(
36 builder, appId, messageType, hostEndpoint, messageDataOffset);
37 finalize(builder, fbs::ChreMessage::NanoappMessage, nanoappMessage.Union());
38 }
39
addStringAsByteVector(FlatBufferBuilder & builder,const char * str)40 Offset<Vector<int8_t>> HostProtocolCommon::addStringAsByteVector(
41 FlatBufferBuilder& builder, const char *str) {
42 return builder.CreateVector(reinterpret_cast<const int8_t *>(str),
43 strlen(str) + 1);
44 }
45
finalize(FlatBufferBuilder & builder,fbs::ChreMessage messageType,flatbuffers::Offset<void> message,uint16_t hostClientId)46 void HostProtocolCommon::finalize(
47 FlatBufferBuilder& builder, fbs::ChreMessage messageType,
48 flatbuffers::Offset<void> message, uint16_t hostClientId) {
49 fbs::HostAddress hostAddr(hostClientId);
50 auto container = fbs::CreateMessageContainer(
51 builder, messageType, message, &hostAddr);
52 builder.Finish(container);
53 }
54
verifyMessage(const void * message,size_t messageLen)55 bool HostProtocolCommon::verifyMessage(const void *message, size_t messageLen) {
56 bool valid = false;
57
58 if (message != nullptr) {
59 flatbuffers::Verifier verifier(static_cast<const uint8_t *>(message),
60 messageLen);
61
62 valid = fbs::VerifyMessageContainerBuffer(verifier);
63 }
64
65 return valid;
66 }
67
68
69 } // namespace chre
70