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