• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <span>
21 
22 #include <chre.h>
23 
24 #include "pw_rpc/channel.h"
25 
26 namespace chre {
27 
28 /**
29  * Message format used for communicating between nanoapps since CHRE doesn't
30  * have a standard format for this as part of the API definition.
31  */
32 struct ChrePigweedNanoappMessage {
33   size_t msgSize;
34   uint8_t msg[];
35 };
36 
37 /**
38  * ChannelOutput that can be used for nanoapps wishing to utilize
39  * pw::rpc::Server and pw::rpc::Client for RPC communication between other
40  * nanoapps and Android app host clients.
41  */
42 class ChreChannelOutputBase : public pw::rpc::ChannelOutput {
43  public:
44   // Random value chosen that matches Java client util, but is random enough
45   // to not conflict with other CHRE messages the nanoapp and client may send.
46   static constexpr uint32_t PW_RPC_CHRE_HOST_MESSAGE_TYPE = INT32_MAX - 10;
47 
48   // Random value chosen to be towards the end of the nanoapp event type region
49   // so it doesn't conflict with existing nanoapp messages that can be sent.
50   static constexpr uint16_t PW_RPC_CHRE_NAPP_EVENT_TYPE = UINT16_MAX - 10;
51 
52   size_t MaximumTransmissionUnit() override;
53 
54  protected:
55   ChreChannelOutputBase();
56 
57   /**
58    * Sets the endpoint ID that the message should be sent to.
59    *
60    * @param endpointId Either a host endpoint ID or nanoapp instance ID
61    *     corresponding to the endpoint that should receive messages sent through
62    *     this channel output.
63    */
64   void setEndpointId(uint16_t endpointId);
65 
66   uint16_t mEndpointId = CHRE_HOST_ENDPOINT_UNSPECIFIED;
67 };
68 
69 /**
70  * Channel output that must be used if the channel is between two nanoapps.
71  */
72 class ChreNanoappChannelOutput : public ChreChannelOutputBase {
73  public:
74   /**
75    * Sets the nanoapp instance ID that is being communicated with over this
76    * channel output.
77    */
78   void setNanoappEndpoint(uint32_t nanoappInstanceId);
79 
80   pw::Status Send(std::span<const std::byte> buffer) override;
81 };
82 
83 /**
84  * Channel output that must be used if the channel is between a nanoapp and
85  * host client.
86  */
87 class ChreHostChannelOutput : public ChreChannelOutputBase {
88  public:
89   /**
90    * Sets the host endpoint being communicated with.
91    */
92   void setHostEndpoint(uint16_t hostEndpoint);
93 
94   pw::Status Send(std::span<const std::byte> buffer) override;
95 };
96 
97 }  // namespace chre
98 
99 #endif  // CHRE_CHANNEL_OUTPUT_H_
100