• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const std::string kCmeErrorOperationNotAllowed;
74   static const std::string kCmeErrorOperationNotSupported;
75   static const std::string kCmeErrorSimNotInserted;
76   static const std::string kCmeErrorSimPinRequired;
77   static const std::string kCmeErrorSimPukRequired;
78   static const std::string kCmeErrorSimBusy;
79   static const std::string kCmeErrorIncorrectPassword;
80   static const std::string kCmeErrorMemoryFull;
81   static const std::string kCmeErrorInvalidIndex;
82   static const std::string kCmeErrorNotFound;
83   static const std::string kCmeErrorInvalidCharactersInTextString;
84   static const std::string kCmeErrorNoNetworkService;
85   static const std::string kCmeErrorNetworkNotAllowedEmergencyCallsOnly;
86   static const std::string kCmeErrorInCorrectParameters;
87   static const std::string kCmeErrorNetworkNotAttachedDueToMTFunctionalRestrictions;
88   static const std::string kCmeErrorFixedDialNumberOnlyAllowed;
89 
90   static const std::string kCmsErrorOperationNotAllowed;
91   static const std::string kCmsErrorOperationNotSupported;
92   static const std::string kCmsErrorInvalidPDUModeParam;
93   static const std::string kCmsErrorSCAddressUnknown;
94 
95   static const std::pair<int, int> kRemotePortRange;
96 
97  protected:
98   ModemService(int32_t service_id, std::vector<CommandHandler> command_handlers,
99                ChannelMonitor* channel_monitor, ThreadLooper* thread_looper);
100   void HandleCommandDefaultSupported(const Client& client);
101   void SendUnsolicitedCommand(std::string unsol_command);
102 
103   cuttlefish::SharedFD ConnectToRemoteCvd(std::string port);
104   void SendCommandToRemote(cuttlefish::SharedFD remote_client,
105                            std::string response);
106   void CloseRemoteConnection(cuttlefish::SharedFD remote_client);
107   static std::string GetHostId();
108 
109   int32_t service_id_;
110   const std::vector<CommandHandler> command_handlers_;
111   ThreadLooper* thread_looper_;
112   ChannelMonitor* channel_monitor_;
113 };
114 
115 }  // namespace cuttlefish
116