xref: /aosp_15_r20/system/chre/util/include/chre/util/pigweed/chre_channel_output.h (revision 84e339476a462649f82315436d70fd732297a399)
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   uint8_t msg[];
36 };
37 
38 /**
39  * Channel output that must be used on the server side of the channel between
40  * two nanoapps.
41  */
42 class ChreServerNanoappChannelOutput : public pw::rpc::ChannelOutput {
43  public:
ChreServerNanoappChannelOutput(RpcPermission & permission)44   explicit ChreServerNanoappChannelOutput(RpcPermission &permission)
45       : ChannelOutput("CHRE"), mPermission(permission) {}
46 
47   /**
48    * Sets the nanoapp instance ID that is being communicated with over this
49    * channel output.
50    */
51   void setClient(uint32_t nanoappInstanceId);
52 
53   size_t MaximumTransmissionUnit() override;
54 
55   pw::Status Send(pw::span<const std::byte> buffer) override;
56 
57  private:
58   uint16_t mClientInstanceId = 0;
59   RpcPermission &mPermission;
60 };
61 
62 /**
63  * Channel output that must be used on the client side of the channel between
64  * two nanoapps.
65  */
66 class ChreClientNanoappChannelOutput : public pw::rpc::ChannelOutput {
67  public:
ChreClientNanoappChannelOutput()68   ChreClientNanoappChannelOutput() : ChannelOutput("CHRE") {}
69 
70   /**
71    * Sets the server instance ID.
72    *
73    * This method must only be called for clients.
74    *
75    * @param instanceId The instance ID of the server.
76    */
77   void setServer(uint32_t instanceId);
78 
79   size_t MaximumTransmissionUnit() override;
80 
81   pw::Status Send(pw::span<const std::byte> buffer) override;
82 
83  private:
84   uint16_t mServerInstanceId = 0;
85 };
86 
87 /**
88  * Channel output that must be used if the channel is between a nanoapp and
89  * host client.
90  */
91 class ChreServerHostChannelOutput : public pw::rpc::ChannelOutput {
92  public:
ChreServerHostChannelOutput(RpcPermission & permission)93   explicit ChreServerHostChannelOutput(RpcPermission &permission)
94       : ChannelOutput("CHRE"), mPermission(permission) {}
95 
96   /**
97    * Sets the host endpoint being communicated with.
98    */
99   void setHostEndpoint(uint16_t hostEndpoint);
100 
101   size_t MaximumTransmissionUnit() override;
102 
103   pw::Status Send(pw::span<const std::byte> buffer) override;
104 
105  private:
106   uint16_t mEndpointId = CHRE_HOST_ENDPOINT_UNSPECIFIED;
107   RpcPermission &mPermission;
108 };
109 
110 }  // namespace chre
111 
112 #endif  // CHRE_CHANNEL_OUTPUT_H_
113