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_MANAGER_H_ 18 #define SHILL_CELLULAR_MODEM_MANAGER_H_ 19 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include <base/macros.h> 26 #include <base/memory/weak_ptr.h> 27 #include <gtest/gtest_prod.h> // for FRIEND_TEST 28 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 33 namespace shill { 34 35 class ControlInterface; 36 class DBusObjectManagerProxyInterface; 37 class DBusPropertiesProxyInterface; 38 class Modem1; 39 class Modem; 40 class ModemClassic; 41 class ModemManagerProxyInterface; 42 43 // Handles a modem manager service and creates and destroys modem instances. 44 class ModemManager { 45 public: 46 ModemManager(ControlInterface* control_interface, 47 const std::string& service, 48 const std::string& path, 49 ModemInfo* modem_info); 50 virtual ~ModemManager(); 51 52 // Starts watching for and handling the DBus modem manager service. 53 virtual void Start() = 0; 54 55 // Stops watching for the DBus modem manager service and destroys any 56 // associated modems. 57 virtual void Stop() = 0; 58 59 void OnDeviceInfoAvailable(const std::string& link_name); 60 61 protected: 62 typedef std::map<std::string, std::shared_ptr<Modem>> Modems; 63 service()64 const std::string& service() const { return service_; } path()65 const std::string& path() const { return path_; } control_interface()66 ControlInterface* control_interface() const { return control_interface_; } modem_info()67 ModemInfo* modem_info() const { return modem_info_; } 68 69 // Service availability callbacks. 70 void OnAppeared(); 71 void OnVanished(); 72 73 // Connect/Disconnect to a modem manager service. 74 // Inheriting classes must call this superclass method. 75 virtual void Connect(); 76 // Inheriting classes must call this superclass method. 77 virtual void Disconnect(); 78 79 bool ModemExists(const std::string& path) const; 80 // Put the modem into our modem map 81 void RecordAddedModem(std::shared_ptr<Modem> modem); 82 83 // Removes a modem on |path|. 84 void RemoveModem(const std::string& path); 85 86 private: 87 friend class ModemManagerCoreTest; 88 friend class ModemManagerClassicTest; 89 friend class ModemManager1Test; 90 91 FRIEND_TEST(ModemInfoTest, RegisterModemManager); 92 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); 93 FRIEND_TEST(ModemManager1Test, Connect); 94 FRIEND_TEST(ModemManagerClassicTest, Connect); 95 FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem); 96 FRIEND_TEST(ModemManagerCoreTest, ConnectDisconnect); 97 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish); 98 99 ControlInterface* control_interface_; 100 101 const std::string service_; 102 const std::string path_; 103 bool service_connected_; 104 105 Modems modems_; // Maps a modem |path| to a modem instance. 106 107 ModemInfo* modem_info_; 108 109 DISALLOW_COPY_AND_ASSIGN(ModemManager); 110 }; 111 112 class ModemManagerClassic : public ModemManager { 113 public: 114 ModemManagerClassic(ControlInterface* control_interface, 115 const std::string& service, 116 const std::string& path, 117 ModemInfo* modem_info); 118 119 ~ModemManagerClassic() override; 120 121 void Start() override; 122 void Stop() override; 123 124 // Called by our dbus proxy 125 void OnDeviceAdded(const std::string& path); 126 void OnDeviceRemoved(const std::string& path); 127 128 protected: 129 void Connect() override; 130 void Disconnect() override; 131 132 virtual void AddModemClassic(const std::string& path); 133 virtual void InitModemClassic(std::shared_ptr<ModemClassic> modem); 134 135 private: 136 std::unique_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy 137 std::unique_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_; 138 139 FRIEND_TEST(ModemManagerClassicTest, Connect); 140 FRIEND_TEST(ModemManagerClassicTest, StartStop); 141 142 DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic); 143 }; 144 145 class ModemManager1 : public ModemManager { 146 public: 147 ModemManager1(ControlInterface* control_interface, 148 const std::string& service, 149 const std::string& path, 150 ModemInfo* modem_info); 151 152 ~ModemManager1() override; 153 154 void Start() override; 155 void Stop() override; 156 157 protected: 158 void AddModem1(const std::string& path, 159 const InterfaceToProperties& properties); 160 virtual void InitModem1(std::shared_ptr<Modem1> modem, 161 const InterfaceToProperties& properties); 162 163 // ModemManager methods 164 void Connect() override; 165 void Disconnect() override; 166 167 // DBusObjectManagerProxyDelegate signal methods 168 virtual void OnInterfacesAddedSignal( 169 const std::string& object_path, 170 const InterfaceToProperties& properties); 171 virtual void OnInterfacesRemovedSignal( 172 const std::string& object_path, 173 const std::vector<std::string>& interfaces); 174 175 // DBusObjectManagerProxyDelegate method callbacks 176 virtual void OnGetManagedObjectsReply( 177 const ObjectsWithProperties& objects_with_properties, 178 const Error& error); 179 180 private: 181 friend class ModemManager1Test; 182 FRIEND_TEST(ModemManager1Test, Connect); 183 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); 184 FRIEND_TEST(ModemManager1Test, StartStop); 185 186 std::unique_ptr<DBusObjectManagerProxyInterface> proxy_; 187 base::WeakPtrFactory<ModemManager1> weak_ptr_factory_; 188 189 DISALLOW_COPY_AND_ASSIGN(ModemManager1); 190 }; 191 192 } // namespace shill 193 194 #endif // SHILL_CELLULAR_MODEM_MANAGER_H_ 195