1 /*
2  * Copyright (C) 2023 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 #include <string>
18 
19 #include <android-base/logging.h>
20 #include <gflags/gflags.h>
21 
22 #include "common/libs/fs/shared_buf.h"
23 #include "common/libs/fs/shared_fd.h"
24 #include "host/commands/cvd_update_security_algorithm/update_security_algorithm_command_builder.h"
25 #include "host/libs/config/cuttlefish_config.h"
26 
27 DEFINE_int32(instance_num, cuttlefish::GetInstance(),
28              "Which instance to read the configs from");
29 DEFINE_int32(modem_num, 0, "Which modem to send command to");
30 DEFINE_int32(connection_event, 0,
31              "The type if connection event. See "
32              "android.hardware.radio.network.ConnectionEvent");
33 DEFINE_int32(encryption, 0,
34              "The encryption algorithm being used. See "
35              "android.hardware.radio.network.SecurityAlgorithm");
36 DEFINE_int32(integrity, 0,
37              "The integrity algorithm being used. See "
38              "android.hardware.radio.network.SecurityAlgorithm");
39 DEFINE_bool(is_unprotected_emergency, false,
40             "Whether the connection event is associated with an unprotected"
41             "emergency session");
42 
43 namespace cuttlefish {
44 namespace {
45 
UpdateSecurityAlgorithm(SharedFD fd)46 void UpdateSecurityAlgorithm(SharedFD fd) {
47   std::string command = fmt::format(
48       "REM{}{}", FLAGS_modem_num,
49       GetATCommand(FLAGS_connection_event, FLAGS_encryption, FLAGS_integrity,
50                    FLAGS_is_unprotected_emergency));
51 
52   LOG(DEBUG) << "Attempting to send command: " << command;
53 
54   long written = WriteAll(fd, command);
55   if (written != command.size()) {
56     LOG(FATAL) << "Failed to write data to shared fd. Tried to write "
57                << command.size() << " bytes, but only wrote " << written
58                << " bytes.";
59   }
60 }
61 
UpdateSecurityAlgorithmMain(int argc,char ** argv)62 int UpdateSecurityAlgorithmMain(int argc, char **argv) {
63   ::android::base::InitLogging(argv, android::base::StderrLogger);
64   google::ParseCommandLineFlags(&argc, &argv, true);
65 
66   auto config = CuttlefishConfig::Get();
67   if (!config) {
68     LOG(FATAL) << "Failed to obtain config object";
69   }
70 
71   auto cf_config = config->ForInstance(FLAGS_instance_num);
72   std::string socket_name =
73       fmt::format("modem_simulator{}", cf_config.modem_simulator_host_id());
74 
75   LOG(INFO) << "Connecting over local socket: " << socket_name;
76   SharedFD modem_simulator_fd =
77       cuttlefish::SharedFD::SocketLocalClient(socket_name, true, SOCK_STREAM);
78 
79   UpdateSecurityAlgorithm(modem_simulator_fd);
80 
81   return 0;
82 }
83 
84 }  // namespace
85 }  // namespace cuttlefish
86 
main(int argc,char ** argv)87 int main(int argc, char **argv) {
88   return cuttlefish::UpdateSecurityAlgorithmMain(argc, argv);
89 }
90