1 //
2 // Copyright (C) 2020 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 #pragma once
17 
18 #include <mutex>
19 #include <thread>
20 #include <vector>
21 
22 #include "common/libs/fs/shared_select.h"
23 
24 namespace cuttlefish {
25 
26 class ModemSimulator;
27 
28 enum ModemSimulatorExitCodes : int {
29   kSuccess = 0,
30   kSelectError = 1,
31   kServerError = 2,
32 };
33 
34 /**
35  * Client object managed by ChannelMonitor, contains two types, the RIL client
36  * and the remote client of other cuttlefish instance.
37  * Due to std::mutex does not implement its copy and operate= constructors, it
38  * cann't be stored in standard contains (vector, map), so use the point instead.
39  */
40 class Client {
41  public:
42   enum ClientType { RIL, REMOTE };
43 
44   ClientType type = RIL;
45   cuttlefish::SharedFD client_fd;
46   std::string incomplete_command;
47   std::mutex write_mutex;
48   bool first_read_command_;  // Only used when ClientType::REMOTE
49   bool is_valid = true;
50 
51   Client() = default;
52   ~Client() = default;
53   Client(cuttlefish::SharedFD fd);
54   Client(cuttlefish::SharedFD fd, ClientType client_type);
55   Client(const Client& client) = delete;
56   Client(Client&& client) = delete;
57 
58   Client& operator=(Client&& other) = delete;
59 
60   bool operator==(const Client& other) const;
61 
62   void SendCommandResponse(std::string response) const;
63   void SendCommandResponse(const std::vector<std::string>& responses) const;
64 };
65 
66 class ChannelMonitor {
67  public:
68   ChannelMonitor(ModemSimulator* modem, cuttlefish::SharedFD server);
69   ~ChannelMonitor();
70 
71   ChannelMonitor(const ChannelMonitor&) = delete;
72   ChannelMonitor& operator=(const ChannelMonitor&) = delete;
73 
74   void SetRemoteClient(cuttlefish::SharedFD client,  bool is_accepted);
75   void SendRemoteCommand(cuttlefish::SharedFD client, std::string& response);
76   void CloseRemoteConnection(cuttlefish::SharedFD client);
77 
78   // For modem services to send unsolicited commands
79   void SendUnsolicitedCommand(std::string& response);
80 
81  private:
82   ModemSimulator* modem_;
83   std::thread monitor_thread_;
84   cuttlefish::SharedFD server_;
85   cuttlefish::SharedFD read_pipe_;
86   cuttlefish::SharedFD write_pipe_;
87   std::vector<std::unique_ptr<Client>> clients_;
88   std::vector<std::unique_ptr<Client>> remote_clients_;
89 
90   void AcceptIncomingConnection();
91   void OnClientSocketClosed(int sock);
92   void ReadCommand(Client& client);
93 
94   void MonitorLoop();
95 };
96 
97 }  // namespace cuttlefish
98