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 #include "host/commands/cvd_send_sms/sms_sender.h"
17 
18 #include <algorithm>
19 #include <codecvt>
20 #include <cstddef>
21 #include <iomanip>
22 #include <iostream>
23 #include <map>
24 #include <vector>
25 
26 #include "android-base/logging.h"
27 #include "common/libs/fs/shared_buf.h"
28 #include "host/commands/cvd_send_sms/pdu_format_builder.h"
29 
30 namespace cuttlefish {
31 
SmsSender(SharedFD modem_simulator_client_fd)32 SmsSender::SmsSender(SharedFD modem_simulator_client_fd)
33     : modem_simulator_client_fd_(modem_simulator_client_fd) {}
34 
Send(const std::string & content,const std::string & sender_number,uint32_t modem_id)35 bool SmsSender::Send(const std::string& content,
36                      const std::string& sender_number, uint32_t modem_id) {
37   if (!modem_simulator_client_fd_->IsOpen()) {
38     LOG(ERROR) << "Failed to connect to remote modem simulator, error: "
39                << modem_simulator_client_fd_->StrError();
40     return false;
41   }
42   PDUFormatBuilder builder;
43   builder.SetUserData(content);
44   builder.SetSenderNumber(sender_number);
45   std::string pdu_format_str = builder.Build();
46   if (pdu_format_str.empty()) {
47     return false;
48   }
49   // https://cs.android.com/android/platform/superproject/+/master:device/google/cuttlefish/host/commands/modem_simulator/main.cpp;l=151;drc=cbfe7dba44bfea95049152b828c1a5d35c9e0522
50   std::string at_command = "REM" + std::to_string(modem_id) +
51                            "AT+REMOTESMS=" + pdu_format_str + "\r";
52   if (WriteAll(modem_simulator_client_fd_, at_command) != at_command.size()) {
53     LOG(ERROR) << "Error writing to socket: "
54                << modem_simulator_client_fd_->StrError();
55     return false;
56   }
57   return true;
58 }
59 }  // namespace cuttlefish
60