1 // 2 // Copyright (C) 2012 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 #ifndef SHILL_CELLULAR_MODEM_H_ 18 #define SHILL_CELLULAR_MODEM_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include <base/macros.h> 25 #include <base/files/file_util.h> 26 #include <gtest/gtest_prod.h> // for FRIEND_TEST 27 28 #include "shill/cellular/cellular.h" 29 #include "shill/cellular/dbus_objectmanager_proxy_interface.h" 30 #include "shill/cellular/modem_info.h" 31 #include "shill/dbus_properties_proxy_interface.h" 32 #include "shill/refptr_types.h" 33 34 namespace shill { 35 36 class ControlInterface; 37 38 // Handles an instance of ModemManager.Modem and an instance of a Cellular 39 // device. 40 class Modem { 41 public: 42 // ||path| is the ModemManager.Modem DBus object path (e.g., 43 // "/org/chromium/ModemManager/Gobi/0"). 44 Modem(const std::string& service, 45 const std::string& path, 46 ModemInfo* modem_info, 47 ControlInterface* control_interface); 48 virtual ~Modem(); 49 50 // Asynchronously initializes support for the modem. 51 // If the |properties| are valid and the MAC address is present, 52 // constructs and registers a Cellular device in |device_| based on 53 // |properties|. 54 virtual void CreateDeviceFromModemProperties( 55 const InterfaceToProperties& properties); 56 57 void OnDeviceInfoAvailable(const std::string& link_name); 58 service()59 const std::string& service() const { return service_; } path()60 const std::string& path() const { return path_; } 61 set_type(Cellular::Type type)62 void set_type(Cellular::Type type) { type_ = type; } 63 64 protected: 65 static const char kPropertyLinkName[]; 66 static const char kPropertyIPMethod[]; 67 static const char kPropertyType[]; 68 69 virtual void Init(); 70 device()71 CellularRefPtr device() const { return device_; } 72 73 virtual Cellular* ConstructCellular(const std::string& link_name, 74 const std::string& device_name, 75 int interface_index); 76 virtual bool GetLinkName(const KeyValueStore& properties, 77 std::string* name) const = 0; 78 // Returns the name of the DBUS Modem interface. 79 virtual std::string GetModemInterface(void) const = 0; 80 81 private: 82 friend class ModemTest; 83 friend class Modem1Test; 84 FRIEND_TEST(Modem1Test, Init); 85 FRIEND_TEST(Modem1Test, CreateDeviceMM1); 86 FRIEND_TEST(ModemManager1Test, Connect); 87 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); 88 FRIEND_TEST(ModemManagerClassicTest, Connect); 89 FRIEND_TEST(ModemManagerClassicTest, StartStop); 90 FRIEND_TEST(ModemManagerCoreTest, ShouldAddModem); 91 FRIEND_TEST(ModemTest, CreateDeviceEarlyFailures); 92 FRIEND_TEST(ModemTest, CreateDevicePPP); 93 FRIEND_TEST(ModemTest, EarlyDeviceProperties); 94 FRIEND_TEST(ModemTest, GetDeviceParams); 95 FRIEND_TEST(ModemTest, Init); 96 FRIEND_TEST(ModemTest, PendingDevicePropertiesAndCreate); 97 98 // Constants associated with fake network devices for PPP dongles. 99 // See |fake_dev_serial_|, below, for more info. 100 static constexpr char kFakeDevNameFormat[] = "no_netdev_%zu"; 101 static const char kFakeDevAddress[]; 102 static const int kFakeDevInterfaceIndex; 103 104 // Find the |mac_address| and |interface_index| for the kernel 105 // network device with name |link_name|. Returns true iff both 106 // |mac_address| and |interface_index| were found. Modifies 107 // |interface_index| even on failure. 108 virtual bool GetDeviceParams(std::string* mac_address, int* interface_index); 109 110 virtual void OnPropertiesChanged( 111 const std::string& interface, 112 const KeyValueStore& changed_properties, 113 const std::vector<std::string>& invalidated_properties); 114 virtual void OnModemManagerPropertiesChanged( 115 const std::string& interface, 116 const KeyValueStore& properties); 117 118 // A proxy to the org.freedesktop.DBusProperties interface used to obtain 119 // ModemManager.Modem properties and watch for property changes 120 std::unique_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_; 121 122 InterfaceToProperties initial_properties_; 123 124 const std::string service_; 125 const std::string path_; 126 127 CellularRefPtr device_; 128 129 ModemInfo* modem_info_; 130 std::string link_name_; 131 Cellular::Type type_; 132 bool pending_device_info_; 133 RTNLHandler* rtnl_handler_; 134 135 ControlInterface* control_interface_; 136 137 // Serial number used to uniquify fake device names for Cellular 138 // devices that don't have network devices. (Names must be unique 139 // for D-Bus, and PPP dongles don't have network devices.) 140 static size_t fake_dev_serial_; 141 142 DISALLOW_COPY_AND_ASSIGN(Modem); 143 }; 144 145 class ModemClassic : public Modem { 146 public: 147 ModemClassic(const std::string& service, 148 const std::string& path, 149 ModemInfo* modem_info, 150 ControlInterface* control_interface); 151 ~ModemClassic() override; 152 153 // Gathers information and passes it to CreateDeviceFromModemProperties. 154 void CreateDeviceClassic(const KeyValueStore& modem_properties); 155 156 protected: 157 bool GetLinkName(const KeyValueStore& modem_properties, 158 std::string* name) const override; 159 std::string GetModemInterface(void) const override; 160 161 private: 162 DISALLOW_COPY_AND_ASSIGN(ModemClassic); 163 }; 164 165 class Modem1 : public Modem { 166 public: 167 Modem1(const std::string& service, 168 const std::string& path, 169 ModemInfo* modem_info, 170 ControlInterface* control_interface); 171 ~Modem1() override; 172 173 // Gathers information and passes it to CreateDeviceFromModemProperties. 174 void CreateDeviceMM1(const InterfaceToProperties& properties); 175 176 protected: 177 bool GetLinkName(const KeyValueStore& modem_properties, 178 std::string* name) const override; 179 std::string GetModemInterface(void) const override; 180 181 private: 182 friend class Modem1Test; 183 184 DISALLOW_COPY_AND_ASSIGN(Modem1); 185 }; 186 187 } // namespace shill 188 189 #endif // SHILL_CELLULAR_MODEM_H_ 190