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 
19 #include <functional>
20 #include <map>
21 #include <optional>
22 
23 #include "host/commands/modem_simulator/channel_monitor.h"
24 #include "host/commands/modem_simulator/command_parser.h"
25 #include "host/commands/modem_simulator/thread_looper.h"
26 
27 namespace cuttlefish {
28 
29 enum ModemServiceType : int {
30   kSimService     = 0,
31   kNetworkService = 1,
32   kDataService    = 2,
33   kCallService    = 3,
34   kSmsService     = 4,
35   kSupService     = 5,
36   kStkService     = 6,
37   kMiscService    = 7,
38 };
39 
40 using f_func = std::function<void(const Client&)>;                // Full match
41 using p_func = std::function<void(const Client&, std::string&)>;  // Partial match
42 
43 class CommandHandler {
44  public:
45   CommandHandler(const std::string& command, f_func handler);
46   CommandHandler(const std::string& command, p_func handler);
47 
48   ~CommandHandler() = default;
49 
50   int Compare(const std::string& command) const;
51   void HandleCommand(const Client& client, std::string& command) const;
52 
53  private:
54   enum MatchMode {FULL_MATCH = 0, PARTIAL_MATCH = 1};
55 
56   std::string command_prefix;
57   MatchMode match_mode;
58 
59   std::optional<f_func> f_command_handler;
60   std::optional<p_func> p_command_handler;
61 };
62 
63 class ModemService {
64  public:
65 
66   virtual ~ModemService() = default;
67 
68   ModemService(const ModemService &) = delete;
69   ModemService &operator=(const ModemService &) = delete;
70 
71   bool HandleModemCommand(const Client& client, std::string command);
72 
73   static constexpr char kCmeErrorOperationNotAllowed[] = "+CME ERROR: 3";
74   static constexpr char kCmeErrorOperationNotSupported[] = "+CME ERROR: 4";
75   static constexpr char kCmeErrorSimNotInserted[] = "+CME ERROR: 10";
76   static constexpr char kCmeErrorSimPinRequired[] = "+CME ERROR: 11";
77   static constexpr char kCmeErrorSimPukRequired[] = "+CME ERROR: 12";
78   static constexpr char kCmeErrorSimBusy[] = "+CME ERROR: 14";
79   static constexpr char kCmeErrorIncorrectPassword[] = "+CME ERROR: 16";
80   static constexpr char kCmeErrorMemoryFull[] = "+CME ERROR: 20";
81   static constexpr char kCmeErrorInvalidIndex[] = "+CME ERROR: 21";
82   static constexpr char kCmeErrorNotFound[] = "+CME ERROR: 22";
83   static constexpr char kCmeErrorInvalidCharactersInTextString[] =
84       "+CME ERROR: 27";
85   static constexpr char kCmeErrorNoNetworkService[] = "+CME ERROR: 30";
86   static constexpr char kCmeErrorNetworkNotAllowedEmergencyCallsOnly[] =
87       "+CME ERROR: 32";
88   static constexpr char kCmeErrorInCorrectParameters[] = "+CME ERROR: 50";
89   static constexpr char
90       kCmeErrorNetworkNotAttachedDueToMTFunctionalRestrictions[] =
91           "+CME ERROR: 53";
92   static constexpr char kCmeErrorFixedDialNumberOnlyAllowed[] =
93       "+CME ERROR: 56";
94 
95   static constexpr char kCmsErrorOperationNotAllowed[] = "+CMS ERROR: 302";
96   static constexpr char kCmsErrorOperationNotSupported[] = "+CMS ERROR: 303";
97   static constexpr char kCmsErrorInvalidPDUModeParam[] = "+CMS ERROR: 304";
98   static constexpr char kCmsErrorSCAddressUnknown[] = "+CMS ERROR: 304";
99 
100   static constexpr std::pair<int, int> kRemotePortRange{6520, 6527};
101 
102   void CloseRemoteConnection(ClientId remote_client);
103 
104  protected:
105   ModemService(int32_t service_id, std::vector<CommandHandler> command_handlers,
106                ChannelMonitor* channel_monitor, ThreadLooper* thread_looper);
107   void HandleCommandDefaultSupported(const Client& client);
108   void SendUnsolicitedCommand(std::string unsol_command);
109 
110   cuttlefish::SharedFD ConnectToRemoteCvd(std::string port);
111   void SendCommandToRemote(ClientId remote_client, std::string response);
112   static std::string GetHostId();
113 
114   int32_t service_id_;
115   const std::vector<CommandHandler> command_handlers_;
116   ThreadLooper* thread_looper_;
117   ChannelMonitor* channel_monitor_;
118 };
119 
120 }  // namespace cuttlefish
121