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