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 #include <android-base/logging.h>
18 #include <gflags/gflags.h>
19 
20 #include "common/libs/fs/shared_fd.h"
21 #include "host/commands/cvd_send_sms/sms_sender.h"
22 
23 constexpr char kUsage[] = R"(
24 NAME
25     cvd_send_sms - send SMSs to cvds.
26 
27 SYNOPSIS
28     cvd_send_sms <message>
29 
30 EXAMPLES
31 
32    $ cvd_send_sms "hello world"
33 
34    $ cvd_send_sms --sender_number="+16501239999" "hello world"
35 
36    $ cvd_send_sms --sender_number="16501239999" "hello world"
37 
38    $ cvd_send_sms --instance-number=2 "hello world"
39 
40    $ cvd_send_sms --instance-number=2 --modem_id=1 "hello world"
41 )";
42 
43 DEFINE_string(sender_number, "+16501234567",
44               "sender phone number in E.164 format");
45 DEFINE_uint32(instance_number, 1,
46               "number of the cvd instance to send the sms to, default is 1");
47 DEFINE_uint32(modem_id, 0,
48               "modem id needed for multisim devices, default is 0");
49 
50 namespace cuttlefish {
51 namespace {
52 
SendSmsMain(int argc,char ** argv)53 int SendSmsMain(int argc, char** argv) {
54   ::gflags::SetUsageMessage(kUsage);
55   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
56   if (argc == 1) {
57     LOG(ERROR) << "Missing message content. First positional argument is used "
58                   "as the message content, `cvd_send_sms --instance-number=2 "
59                   "\"hello world\"`";
60     return -1;
61   }
62   // Builds the name of the corresponding modem simulator monitor socket.
63   // https://cs.android.com/android/platform/superproject/+/master:device/google/cuttlefish/host/commands/modem_simulator/main.cpp;l=115;drc=cbfe7dba44bfea95049152b828c1a5d35c9e0522
64   std::string socket_name = std::string("modem_simulator") +
65                             std::to_string(1000 + FLAGS_instance_number);
66   auto client_socket = cuttlefish::SharedFD::SocketLocalClient(
67       socket_name.c_str(), /* abstract */ true, SOCK_STREAM);
68   SmsSender sms_sender(client_socket);
69   if (!sms_sender.Send(argv[1], FLAGS_sender_number, FLAGS_modem_id)) {
70     return -1;
71   }
72   return 0;
73 }
74 
75 }  // namespace
76 }  // namespace cuttlefish
77 
main(int argc,char ** argv)78 int main(int argc, char** argv) { return cuttlefish::SendSmsMain(argc, argv); }
79