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_RPC_WORLD_MANAGER_H_
18 #define CHRE_RPC_WORLD_MANAGER_H_
19 
20 #include <cinttypes>
21 #include <cstdint>
22 
23 #include "chre/re.h"
24 #include "chre/util/nanoapp/app_id.h"
25 #include "chre/util/pigweed/rpc_client.h"
26 #include "chre/util/pigweed/rpc_server.h"
27 #include "chre/util/singleton.h"
28 #include "chre_api/chre.h"
29 #include "rpc_world.rpc.pb.h"
30 
31 class RpcWorldService final
32     : public chre::rpc::pw_rpc::nanopb::RpcWorldService::Service<
33           RpcWorldService> {
34  public:
35   // Increment RPC unary service definition.
36   // See generated IncrementService::Service for more details.
37   pw::Status Increment(const chre_rpc_NumberMessage &request,
38                        chre_rpc_NumberMessage &response);
39 
40   // Timer RPC server streaming service definition.
41   // See generated TimerService::Service for more details.
42   void Timer(const chre_rpc_TimerRequest &request,
43              ServerWriter<chre_rpc_TimerResponse> &writer);
44 
45   // Add RPC client streaming service definition.
46   // See generated AddService::Service for more details.
47   void Add(
48       ServerReader<chre_rpc_NumberMessage, chre_rpc_NumberMessage> &reader);
49 };
50 
51 /**
52  * Acts both as a RPC server and a RPC client.
53  * The client calls the RPCWorld service provided by the server.
54  */
55 class RpcWorldManager {
56  public:
57   /**
58    * Allows the manager to do any init necessary as part of nanoappStart.
59    */
60   bool start();
61 
62   /**
63    * Handle a CHRE event.
64    *
65    * @param senderInstanceId The instand ID that sent the event.
66    * @param eventType The type of the event.
67    * @param eventData The data for the event.
68    */
69   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
70                    const void *eventData);
71 
72   /**
73    * Allows the manager to do any cleanup necessary as part of nanoappEnd.
74    */
75   void end();
76 
77   /**
78    * Starts the tick timer.
79    *
80    * @param numTicks Number of ticks to stream.
81    * @param writer Used to stream the responses.
82    */
83   void timerStart(
84       uint32_t numTicks,
85       RpcWorldService::ServerWriter<chre_rpc_TimerResponse> &writer);
86 
87   /**
88    * Starts a client streamed add.
89    *
90    * @param reader Used to read the streamed requests.
91    */
92   void addStart(RpcWorldService::ServerReader<chre_rpc_NumberMessage,
93                                               chre_rpc_NumberMessage> &reader);
94 
95   /**
96    * Sets the permission for the next server message.
97    *
98    * @params permission Bitmasked CHRE_MESSAGE_PERMISSION_.
99    */
100   void setPermissionForNextMessage(uint32_t permission);
101 
102   uint32_t mSum = 0;
103 
104  private:
105   chre::RpcServer mServer;
106   chre::RpcClient mClient{chre::kRpcWorldAppId};
107   // pw_rpc service used to process the RPCs.
108   RpcWorldService mRpcWorldService;
109   RpcWorldService::ServerWriter<chre_rpc_TimerResponse> mTimerWriter;
110   RpcWorldService::ServerReader<chre_rpc_NumberMessage, chre_rpc_NumberMessage>
111       mAddReader;
112   uint32_t mTimerId = CHRE_TIMER_INVALID;
113   uint32_t mTimerCurrentTick;
114   uint32_t mTimerTotalTicks;
115   pw::rpc::NanopbUnaryReceiver<chre_rpc_NumberMessage> mIncrementCall;
116   pw::rpc::NanopbClientReader<chre_rpc_TimerResponse> mTimerCall;
117   pw::rpc::NanopbClientWriter<chre_rpc_NumberMessage, chre_rpc_NumberMessage>
118       mAddCall;
119 };
120 
121 typedef chre::Singleton<RpcWorldManager> RpcWorldManagerSingleton;
122 
123 #endif  // CHRE_RPC_WORLD_MANAGER_H_
124