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