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 #include <android-base/strings.h>
17 #include <gflags/gflags.h>
18 #include <signal.h>
19 #include <unistd.h>
20
21 #include <limits>
22
23 #include "common/libs/device_config/device_config.h"
24 #include "common/libs/fs/shared_buf.h"
25 #include "common/libs/fs/shared_fd.h"
26 #include "common/libs/utils/tee_logging.h"
27 #include "host/commands/modem_simulator/modem_simulator.h"
28 #include "host/libs/config/cuttlefish_config.h"
29
30 // we can start multiple modems simultaneously; each modem
31 // will listent to one server fd for incoming sms/phone call
32 // there should be at least 1 valid fd
33 DEFINE_string(server_fds, "", "A comma separated list of file descriptors");
34 DEFINE_int32(sim_type, 1, "Sim type: 1 for normal, 2 for CtsCarrierApiTestCases");
35
ServerFdsFromCmdline()36 std::vector<cuttlefish::SharedFD> ServerFdsFromCmdline() {
37 // Validate the parameter
38 std::string fd_list = FLAGS_server_fds;
39 for (auto c: fd_list) {
40 if (c != ',' && (c < '0' || c > '9')) {
41 LOG(ERROR) << "Invalid file descriptor list: " << fd_list;
42 std::exit(1);
43 }
44 }
45
46 auto fds = android::base::Split(fd_list, ",");
47 std::vector<cuttlefish::SharedFD> shared_fds;
48 for (auto& fd_str: fds) {
49 auto fd = std::stoi(fd_str);
50 auto shared_fd = cuttlefish::SharedFD::Dup(fd);
51 close(fd);
52 shared_fds.push_back(shared_fd);
53 }
54
55 return shared_fds;
56 }
57
main(int argc,char ** argv)58 int main(int argc, char** argv) {
59 ::android::base::InitLogging(argv, android::base::StderrLogger);
60 google::ParseCommandLineFlags(&argc, &argv, false);
61
62 // Modem simulator log saved in cuttlefish_runtime
63 auto config = cuttlefish::CuttlefishConfig::Get();
64 auto instance = config->ForDefaultInstance();
65
66 auto modem_log_path = instance.PerInstancePath("modem_simulator.log");
67
68 {
69 auto log_path = instance.launcher_log_path();
70 std::vector<std::string> log_files{log_path, modem_log_path};
71 android::base::SetLogger(cuttlefish::LogToStderrAndFiles(log_files));
72 }
73
74 LOG(INFO) << "Start modem simulator, server_fds: " << FLAGS_server_fds
75 << ", Sim type: " << ((FLAGS_sim_type == 2) ?
76 "special for CtsCarrierApiTestCases" : "normal" );
77
78 auto server_fds = ServerFdsFromCmdline();
79 if (server_fds.empty()) {
80 LOG(ERROR) << "Need to provide server fd";
81 return -1;
82 }
83
84 cuttlefish::NvramConfig::InitNvramConfigService(server_fds.size(), FLAGS_sim_type);
85
86 // Don't get a SIGPIPE from the clients
87 if (sigaction(SIGPIPE, nullptr, nullptr) != 0) {
88 LOG(ERROR) << "Failed to set SIGPIPE to be ignored: " << strerror(errno);
89 }
90
91 auto nvram_config = cuttlefish::NvramConfig::Get();
92 auto nvram_config_file = nvram_config->ConfigFileLocation();
93
94 // Start channel monitor, wait for RIL to connect
95 int32_t modem_id = 0;
96 std::vector<std::shared_ptr<cuttlefish::ModemSimulator>> modem_simulators;
97
98 for (auto& fd : server_fds) {
99 CHECK(fd->IsOpen()) << "Error creating or inheriting modem simulator server: "
100 << fd->StrError();
101
102 auto modem_simulator = std::make_shared<cuttlefish::ModemSimulator>(modem_id);
103 auto channel_monitor =
104 std::make_unique<cuttlefish::ChannelMonitor>(modem_simulator.get(), fd);
105
106 modem_simulator->Initialize(std::move(channel_monitor));
107
108 modem_simulators.push_back(modem_simulator);
109
110 modem_id++;
111 }
112
113 // Monitor exit request and
114 // remote call, remote sms from other cuttlefish instance
115 std::string monitor_socket_name = "modem_simulator";
116 std::stringstream ss;
117 ss << instance.host_port();
118 monitor_socket_name.append(ss.str());
119
120 auto monitor_socket = cuttlefish::SharedFD::SocketLocalServer(
121 monitor_socket_name.c_str(), true, SOCK_STREAM, 0666);
122 if (!monitor_socket->IsOpen()) {
123 LOG(ERROR) << "Unable to create monitor socket for modem simulator";
124 std::exit(cuttlefish::kServerError);
125 }
126
127 // Server loop
128 while (true) {
129 cuttlefish::SharedFDSet read_set;
130 read_set.Set(monitor_socket);
131 int num_fds = cuttlefish::Select(&read_set, nullptr, nullptr, nullptr);
132 if (num_fds <= 0) { // Ignore select error
133 LOG(ERROR) << "Select call returned error : " << strerror(errno);
134 } else if (read_set.IsSet(monitor_socket)) {
135 auto conn = cuttlefish::SharedFD::Accept(*monitor_socket);
136 std::string buf(4, ' ');
137 auto read = cuttlefish::ReadExact(conn, &buf);
138 if (read <= 0) {
139 conn->Close();
140 LOG(WARNING) << "Detected close from the other side";
141 continue;
142 }
143 if (buf == "STOP") { // Exit request from parent process
144 LOG(INFO) << "Exit request from parent process";
145 nvram_config->SaveToFile(nvram_config_file);
146 for (auto modem : modem_simulators) {
147 modem->SaveModemState();
148 }
149 cuttlefish::WriteAll(conn, "OK"); // Ignore the return value. Exit anyway.
150 std::exit(cuttlefish::kSuccess);
151 } else if (buf.compare(0, 3, "REM") == 0) { // REMO for modem id 0 ...
152 // Remote request from other cuttlefish instance
153 int id = std::stoi(buf.substr(3, 1));
154 if (id >= modem_simulators.size()) {
155 LOG(ERROR) << "Not supported modem simulator count: " << id;
156 } else {
157 modem_simulators[id]->SetRemoteClient(conn, true);
158 }
159 }
160 }
161 }
162 // Until kill or exit
163 }
164