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 #ifndef CHRE_CHANNEL_OUTPUT_H_ 18 #define CHRE_CHANNEL_OUTPUT_H_ 19 20 #include <cstdint> 21 22 #include "chre/util/pigweed/permission.h" 23 #include "chre_api/chre.h" 24 #include "pw_rpc/channel.h" 25 #include "pw_span/span.h" 26 27 namespace chre { 28 29 /** 30 * Message format used for communicating between nanoapps since CHRE doesn't 31 * have a standard format for this as part of the API definition. 32 */ 33 struct ChrePigweedNanoappMessage { 34 size_t msgSize; 35 void *msg; 36 }; 37 38 /** 39 * ChannelOutput that can be used for nanoapps wishing to utilize 40 * pw::rpc::Server and pw::rpc::Client for RPC communication between other 41 * nanoapps and Android app host clients. 42 */ 43 class ChreChannelOutputBase : public pw::rpc::ChannelOutput { 44 public: 45 // Random value chosen that matches Java client util, but is random enough 46 // to not conflict with other CHRE messages the nanoapp and client may send. 47 static constexpr uint32_t PW_RPC_CHRE_HOST_MESSAGE_TYPE = INT32_MAX - 10; 48 49 // Random values chosen to be towards the end of the nanoapp event type region 50 // so it doesn't conflict with existing nanoapp messages that can be sent. 51 static constexpr uint16_t PW_RPC_CHRE_NAPP_REQUEST_EVENT_TYPE = 52 UINT16_MAX - 10; 53 static constexpr uint16_t PW_RPC_CHRE_NAPP_RESPONSE_EVENT_TYPE = 54 UINT16_MAX - 9; 55 56 size_t MaximumTransmissionUnit() override; 57 58 protected: 59 ChreChannelOutputBase(); 60 }; 61 62 /** 63 * Channel output that must be used on the server side of the channel between 64 * two nanoapps. 65 */ 66 class ChreServerNanoappChannelOutput : public ChreChannelOutputBase { 67 public: ChreServerNanoappChannelOutput(RpcPermission & permission)68 explicit ChreServerNanoappChannelOutput(RpcPermission &permission) 69 : mPermission(permission) {} 70 /** 71 * Sets the nanoapp instance ID that is being communicated with over this 72 * channel output. 73 */ 74 void setClient(uint32_t nanoappInstanceId); 75 76 pw::Status Send(pw::span<const std::byte> buffer) override; 77 78 private: 79 uint16_t mClientInstanceId = 0; 80 RpcPermission &mPermission; 81 }; 82 83 /** 84 * Channel output that must be used on the client side of the channel between 85 * two nanoapps. 86 */ 87 class ChreClientNanoappChannelOutput : public ChreChannelOutputBase { 88 public: 89 /** 90 * Sets the server instance ID. 91 * 92 * This method must only be called for clients. 93 * 94 * @param instanceId The instance ID of the server. 95 */ 96 void setServer(uint32_t instanceId); 97 98 pw::Status Send(pw::span<const std::byte> buffer) override; 99 100 private: 101 uint16_t mServerInstanceId = 0; 102 }; 103 104 /** 105 * Channel output that must be used if the channel is between a nanoapp and 106 * host client. 107 */ 108 class ChreServerHostChannelOutput : public ChreChannelOutputBase { 109 public: ChreServerHostChannelOutput(RpcPermission & permission)110 explicit ChreServerHostChannelOutput(RpcPermission &permission) 111 : mPermission(permission) {} 112 /** 113 * Sets the host endpoint being communicated with. 114 */ 115 void setHostEndpoint(uint16_t hostEndpoint); 116 117 pw::Status Send(pw::span<const std::byte> buffer) override; 118 119 private: 120 uint16_t mEndpointId = CHRE_HOST_ENDPOINT_UNSPECIFIED; 121 RpcPermission &mPermission; 122 }; 123 124 } // namespace chre 125 126 #endif // CHRE_CHANNEL_OUTPUT_H_ 127