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_WIMAX_WIMAX_PROVIDER_H_
18 #define SHILL_WIMAX_WIMAX_PROVIDER_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <base/macros.h>
25 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
26 
27 #include "shill/accessor_interface.h"
28 #include "shill/provider_interface.h"
29 #include "shill/refptr_types.h"
30 #include "shill/wimax/wimax_network_proxy_interface.h"
31 
32 namespace shill {
33 
34 class ControlInterface;
35 class EventDispatcher;
36 class KeyValueStore;
37 class Manager;
38 class Metrics;
39 class StoreInterface;
40 class WiMaxManagerProxyInterface;
41 
42 class WiMaxProvider : public ProviderInterface {
43  public:
44   WiMaxProvider(ControlInterface* control,
45                 EventDispatcher* dispatcher,
46                 Metrics* metrics,
47                 Manager* manager);
48   ~WiMaxProvider() override;
49 
50   // Called by Manager as a part of the Provider interface.  The attributes
51   // used for matching services for the WiMax provider are the NetworkId,
52   // mode and Name parameters.
53   void CreateServicesFromProfile(const ProfileRefPtr& profile) override;
54   ServiceRefPtr FindSimilarService(
55       const KeyValueStore& args, Error* error) const override;
56   ServiceRefPtr GetService(const KeyValueStore& args, Error* error) override;
57   ServiceRefPtr CreateTemporaryService(
58       const KeyValueStore& args, Error* error) override;
59   ServiceRefPtr CreateTemporaryServiceFromProfile(
60       const ProfileRefPtr& profile,
61       const std::string& entry_name,
62       Error* error) override;
63   void Start() override;
64   void Stop() override;
65 
66   // Signaled by DeviceInfo when a new WiMAX device becomes available.
67   virtual void OnDeviceInfoAvailable(const std::string& link_name);
68 
69   // Signaled by a WiMAX device when its set of live networks changes.
70   virtual void OnNetworksChanged();
71 
72   // Signaled by |service| when it's been unloaded by Manager. Returns true if
73   // this provider has released ownership of the service, and false otherwise.
74   virtual bool OnServiceUnloaded(const WiMaxServiceRefPtr& service);
75 
76   // Selects and returns a WiMAX device to connect |service| through.
77   virtual WiMaxRefPtr SelectCarrier(const WiMaxServiceConstRefPtr& service);
78 
79  private:
80   friend class WiMaxProviderTest;
81   FRIEND_TEST(WiMaxProviderTest, ConnectDisconnectWiMaxManager);
82   FRIEND_TEST(WiMaxProviderTest, CreateDevice);
83   FRIEND_TEST(WiMaxProviderTest, CreateServicesFromProfile);
84   FRIEND_TEST(WiMaxProviderTest, DestroyAllServices);
85   FRIEND_TEST(WiMaxProviderTest, DestroyDeadDevices);
86   FRIEND_TEST(WiMaxProviderTest, FindService);
87   FRIEND_TEST(WiMaxProviderTest, GetLinkName);
88   FRIEND_TEST(WiMaxProviderTest, GetUniqueService);
89   FRIEND_TEST(WiMaxProviderTest, OnDeviceInfoAvailable);
90   FRIEND_TEST(WiMaxProviderTest, OnDevicesChanged);
91   FRIEND_TEST(WiMaxProviderTest, OnNetworksChanged);
92   FRIEND_TEST(WiMaxProviderTest, OnServiceUnloaded);
93   FRIEND_TEST(WiMaxProviderTest, RetrieveNetworkInfo);
94   FRIEND_TEST(WiMaxProviderTest, SelectCarrier);
95   FRIEND_TEST(WiMaxProviderTest, StartLiveServices);
96   FRIEND_TEST(WiMaxProviderTest, StartStop);
97   FRIEND_TEST(WiMaxProviderTest, StopDeadServices);
98 
99   struct NetworkInfo {
100     WiMaxNetworkId id;
101     std::string name;
102   };
103 
104   void ConnectToWiMaxManager();
105   void DisconnectFromWiMaxManager();
106   void OnWiMaxManagerAppeared();
107   void OnWiMaxManagerVanished();
108 
109   void OnDevicesChanged(const RpcIdentifiers& devices);
110 
111   void CreateDevice(const std::string& link_name, const RpcIdentifier& path);
112   void DestroyDeadDevices(const RpcIdentifiers& live_devices);
113 
114   std::string GetLinkName(const RpcIdentifier& path);
115 
116   // Retrieves network info for a network at RPC |path| into |networks_| if it's
117   // not already available.
118   void RetrieveNetworkInfo(const RpcIdentifier& path);
119 
120   // Finds and returns the service identified by |storage_id|. Returns nullptr
121   // if the service is not found.
122   WiMaxServiceRefPtr FindService(const std::string& storage_id) const;
123 
124   // Finds or creates a service with the given parameters. The parameters
125   // uniquely identify a service so no duplicate services will be created.
126   // The service will be registered and a memeber of the provider's
127   // |services_| map.
128   WiMaxServiceRefPtr GetUniqueService(const WiMaxNetworkId& id,
129                                       const std::string& name);
130 
131   // Allocates a service with the given parameters.
132   WiMaxServiceRefPtr CreateService(const WiMaxNetworkId& id,
133                                    const std::string& name);
134 
135   // Populates the |id_ptr| and |name_ptr| from the parameters in |args|.
136   // Returns true on success, otheriwse populates |error| and returns false.
137   static bool GetServiceParametersFromArgs(const KeyValueStore& args,
138                                            WiMaxNetworkId* id_ptr,
139                                            std::string* name_ptr,
140                                            Error* error);
141 
142   static bool GetServiceParametersFromStorage(const StoreInterface* storage,
143                                               const std::string& entry_name,
144                                               WiMaxNetworkId* id_ptr,
145                                               std::string* name_ptr,
146                                               Error* error);
147 
148   // Starts all services with network ids in the current set of live
149   // networks. This method also creates, registers and starts the default
150   // service for each live network.
151   void StartLiveServices();
152 
153   // Stops all services with network ids that are not in the current set of live
154   // networks.
155   void StopDeadServices();
156 
157   // Stops, deregisters and destroys all services.
158   void DestroyAllServices();
159 
160   ControlInterface* control_;
161   EventDispatcher* dispatcher_;
162   Metrics* metrics_;
163   Manager* manager_;
164 
165   std::unique_ptr<WiMaxManagerProxyInterface> wimax_manager_proxy_;
166 
167   // Key is the interface link name.
168   std::map<std::string, RpcIdentifier> pending_devices_;
169   std::map<std::string, WiMaxRefPtr> devices_;
170   // Key is service's storage identifier.
171   std::map<std::string, WiMaxServiceRefPtr> services_;
172   std::map<RpcIdentifier, NetworkInfo> networks_;
173 
174   DISALLOW_COPY_AND_ASSIGN(WiMaxProvider);
175 };
176 
177 }  // namespace shill
178 
179 #endif  // SHILL_WIMAX_WIMAX_PROVIDER_H_
180