1 /*
2 * Copyright (C) 2022 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/util/pigweed/chre_channel_output.h"
18
19 #include <cstdint>
20
21 #include "chre/util/nanoapp/callbacks.h"
22 #include "chre/util/pigweed/rpc_helper.h"
23
24 namespace chre {
25 namespace {
26
nappMessageFreeCb(uint16_t,void * eventData)27 void nappMessageFreeCb(uint16_t /* eventType */, void *eventData) {
28 chreHeapFree(eventData);
29 }
30
31 /**
32 * Sends the buffer to the nanoapp.
33 *
34 * The buffer is first wrapped into a ChrePigweedNanoappMessage struct.
35 *
36 * @param targetInstanceId The nanoapp to send the message to
37 * @param eventType The event to send to the nanoapp
38 * @param buffer The buffer to send
39 * @return The status of the operation
40 */
sendToNanoapp(uint32_t targetInstanceId,uint16_t eventType,pw::span<const std::byte> buffer)41 pw::Status sendToNanoapp(uint32_t targetInstanceId, uint16_t eventType,
42 pw::span<const std::byte> buffer) {
43 CHRE_ASSERT(targetInstanceId != 0);
44
45 if (buffer.size() > 0) {
46 auto *data = static_cast<ChrePigweedNanoappMessage *>(
47 chreHeapAlloc(buffer.size() + sizeof(ChrePigweedNanoappMessage)));
48 if (data == nullptr) {
49 return PW_STATUS_RESOURCE_EXHAUSTED;
50 }
51
52 data->msgSize = buffer.size();
53 data->msg = &data[1];
54 memcpy(data->msg, buffer.data(), buffer.size());
55
56 if (!chreSendEvent(eventType, data, nappMessageFreeCb, targetInstanceId)) {
57 return PW_STATUS_INVALID_ARGUMENT;
58 }
59 }
60
61 return PW_STATUS_OK;
62 }
63
64 } // namespace
65
ChreChannelOutputBase()66 ChreChannelOutputBase::ChreChannelOutputBase() : ChannelOutput("CHRE") {}
67
MaximumTransmissionUnit()68 size_t ChreChannelOutputBase::MaximumTransmissionUnit() {
69 return CHRE_MESSAGE_TO_HOST_MAX_SIZE - sizeof(ChrePigweedNanoappMessage);
70 }
71
setClient(uint32_t nanoappInstanceId)72 void ChreServerNanoappChannelOutput::setClient(uint32_t nanoappInstanceId) {
73 CHRE_ASSERT(nanoappInstanceId <= kRpcNanoappMaxId);
74 if (nanoappInstanceId <= kRpcNanoappMaxId) {
75 mClientInstanceId = static_cast<uint16_t>(nanoappInstanceId);
76 } else {
77 mClientInstanceId = 0;
78 }
79 }
80
Send(pw::span<const std::byte> buffer)81 pw::Status ChreServerNanoappChannelOutput::Send(
82 pw::span<const std::byte> buffer) {
83 // The permission is not enforced across nanoapps but we still need to
84 // reset the value as it is only applicable to the next message.
85 mPermission.getAndReset();
86
87 return sendToNanoapp(mClientInstanceId, PW_RPC_CHRE_NAPP_RESPONSE_EVENT_TYPE,
88 buffer);
89 }
90
setServer(uint32_t instanceId)91 void ChreClientNanoappChannelOutput::setServer(uint32_t instanceId) {
92 CHRE_ASSERT(instanceId <= kRpcNanoappMaxId);
93 if (instanceId <= kRpcNanoappMaxId) {
94 mServerInstanceId = static_cast<uint16_t>(instanceId);
95 } else {
96 mServerInstanceId = 0;
97 }
98 }
99
Send(pw::span<const std::byte> buffer)100 pw::Status ChreClientNanoappChannelOutput::Send(
101 pw::span<const std::byte> buffer) {
102 return sendToNanoapp(mServerInstanceId, PW_RPC_CHRE_NAPP_REQUEST_EVENT_TYPE,
103 buffer);
104 }
105
setHostEndpoint(uint16_t hostEndpoint)106 void ChreServerHostChannelOutput::setHostEndpoint(uint16_t hostEndpoint) {
107 mEndpointId = hostEndpoint;
108 }
109
Send(pw::span<const std::byte> buffer)110 pw::Status ChreServerHostChannelOutput::Send(pw::span<const std::byte> buffer) {
111 CHRE_ASSERT(mEndpointId != CHRE_HOST_ENDPOINT_UNSPECIFIED);
112 pw::Status returnCode = PW_STATUS_OK;
113
114 if (buffer.size() > 0) {
115 uint32_t permission = mPermission.getAndReset();
116 uint8_t *data = static_cast<uint8_t *>(chreHeapAlloc(buffer.size()));
117 if (data == nullptr) {
118 returnCode = PW_STATUS_RESOURCE_EXHAUSTED;
119 } else {
120 memcpy(data, buffer.data(), buffer.size());
121 if (!chreSendMessageWithPermissions(
122 data, buffer.size(), PW_RPC_CHRE_HOST_MESSAGE_TYPE, mEndpointId,
123 permission, heapFreeMessageCallback)) {
124 returnCode = PW_STATUS_INVALID_ARGUMENT;
125 }
126 }
127 }
128
129 return returnCode;
130 }
131
132 } // namespace chre
133