xref: /aosp_15_r20/system/chre/host/common/pigweed/hal_channel_output.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2023 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_host/pigweed/hal_channel_output.h"
18 
19 #include <cstdint>
20 
21 #include "chre/event.h"
22 #include "chre/util/pigweed/rpc_common.h"
23 #include "chre_host/host_protocol_host.h"
24 #include "chre_host/log.h"
25 #include "chre_host/socket_client.h"
26 #include "pw_rpc/channel.h"
27 #include "pw_span/span.h"
28 
29 namespace android::chre {
30 
31 using ::flatbuffers::FlatBufferBuilder;
32 
MaximumTransmissionUnit()33 size_t HalChannelOutput::MaximumTransmissionUnit() {
34   return mMaxMessageLen > kFlatBufferPadding
35              ? mMaxMessageLen - kFlatBufferPadding
36              : 0;
37 }
38 
Send(pw::span<const std::byte> buffer)39 pw::Status HalChannelOutput::Send(pw::span<const std::byte> buffer) {
40   if (buffer.size() > 0) {
41     FlatBufferBuilder builder(buffer.size() + kFlatBufferPadding);
42 
43     HostProtocolHost::encodeNanoappMessage(
44         builder, mServerNanoappId, CHRE_MESSAGE_TYPE_RPC, mHostEndpointId,
45         buffer.data(), buffer.size());
46 
47     if (!mSocketClient.sendMessage(builder.GetBufferPointer(),
48                                    builder.GetSize())) {
49       LOGE("Failed to send message");
50       return PW_STATUS_UNKNOWN;
51     }
52   }
53 
54   return PW_STATUS_OK;
55 }
56 
57 }  // namespace android::chre