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(static_cast<uint32_t>(
48 buffer.size() + sizeof(ChrePigweedNanoappMessage))));
49 if (data == nullptr) {
50 return PW_STATUS_RESOURCE_EXHAUSTED;
51 }
52
53 data->msgSize = buffer.size();
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
setClient(uint32_t nanoappInstanceId)66 void ChreServerNanoappChannelOutput::setClient(uint32_t nanoappInstanceId) {
67 CHRE_ASSERT(nanoappInstanceId <= kRpcNanoappMaxId);
68 if (nanoappInstanceId <= kRpcNanoappMaxId) {
69 mClientInstanceId = static_cast<uint16_t>(nanoappInstanceId);
70 } else {
71 mClientInstanceId = 0;
72 }
73 }
74
MaximumTransmissionUnit()75 size_t ChreServerNanoappChannelOutput::MaximumTransmissionUnit() {
76 return CHRE_MESSAGE_TO_HOST_MAX_SIZE - sizeof(ChrePigweedNanoappMessage);
77 }
78
Send(pw::span<const std::byte> buffer)79 pw::Status ChreServerNanoappChannelOutput::Send(
80 pw::span<const std::byte> buffer) {
81 // The permission is not enforced across nanoapps but we still need to
82 // reset the value as it is only applicable to the next message.
83 mPermission.getAndReset();
84
85 return sendToNanoapp(mClientInstanceId, CHRE_EVENT_RPC_RESPONSE, buffer);
86 }
87
setServer(uint32_t instanceId)88 void ChreClientNanoappChannelOutput::setServer(uint32_t instanceId) {
89 CHRE_ASSERT(instanceId <= kRpcNanoappMaxId);
90 if (instanceId <= kRpcNanoappMaxId) {
91 mServerInstanceId = static_cast<uint16_t>(instanceId);
92 } else {
93 mServerInstanceId = 0;
94 }
95 }
96
MaximumTransmissionUnit()97 size_t ChreClientNanoappChannelOutput::MaximumTransmissionUnit() {
98 return CHRE_MESSAGE_TO_HOST_MAX_SIZE - sizeof(ChrePigweedNanoappMessage);
99 }
100
Send(pw::span<const std::byte> buffer)101 pw::Status ChreClientNanoappChannelOutput::Send(
102 pw::span<const std::byte> buffer) {
103 return sendToNanoapp(mServerInstanceId, CHRE_EVENT_RPC_REQUEST, buffer);
104 }
105
setHostEndpoint(uint16_t hostEndpoint)106 void ChreServerHostChannelOutput::setHostEndpoint(uint16_t hostEndpoint) {
107 mEndpointId = hostEndpoint;
108 }
109
MaximumTransmissionUnit()110 size_t ChreServerHostChannelOutput::MaximumTransmissionUnit() {
111 return CHRE_MESSAGE_TO_HOST_MAX_SIZE - sizeof(ChrePigweedNanoappMessage);
112 }
113
Send(pw::span<const std::byte> buffer)114 pw::Status ChreServerHostChannelOutput::Send(pw::span<const std::byte> buffer) {
115 CHRE_ASSERT(mEndpointId != CHRE_HOST_ENDPOINT_UNSPECIFIED);
116 pw::Status returnCode = PW_STATUS_OK;
117
118 if (buffer.size() > 0) {
119 uint32_t permission = mPermission.getAndReset();
120 uint8_t *data = static_cast<uint8_t *>(
121 chreHeapAlloc(static_cast<uint32_t>(buffer.size())));
122 if (data == nullptr) {
123 returnCode = PW_STATUS_RESOURCE_EXHAUSTED;
124 } else {
125 memcpy(data, buffer.data(), buffer.size());
126 if (!chreSendMessageWithPermissions(
127 data, buffer.size(), CHRE_MESSAGE_TYPE_RPC, mEndpointId,
128 permission, heapFreeMessageCallback)) {
129 returnCode = PW_STATUS_INVALID_ARGUMENT;
130 }
131 }
132 }
133
134 return returnCode;
135 }
136
137 } // namespace chre
138