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_H_
18 #define SHILL_WIMAX_WIMAX_H_
19 
20 #include <memory>
21 #include <set>
22 #include <string>
23 
24 #include <base/cancelable_callback.h>
25 #include <base/memory/weak_ptr.h>
26 #if defined(__ANDROID__)
27 #include <dbus/service_constants.h>
28 #else
29 #include <chromeos/dbus/service_constants.h>
30 #endif  // __ANDROID__
31 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
32 
33 #include "shill/device.h"
34 
35 namespace shill {
36 
37 class WiMaxDeviceProxyInterface;
38 
39 class WiMax : public Device {
40  public:
41   WiMax(ControlInterface* control,
42         EventDispatcher* dispatcher,
43         Metrics* metrics,
44         Manager* manager,
45         const std::string& link_name,
46         const std::string& address,
47         int interface_index,
48         const RpcIdentifier& path);
49 
50   ~WiMax() override;
51 
52   // Inherited from Device.
53   void Start(Error* error,
54              const EnabledStateChangedCallback& callback) override;
55   void Stop(Error* error, const EnabledStateChangedCallback& callback) override;
56   void Scan(ScanType /*scan_type*/, Error* error,
57             const std::string& /*reason*/) override;
58 
59   virtual void ConnectTo(const WiMaxServiceRefPtr& service, Error* error);
60   virtual void DisconnectFrom(const ServiceRefPtr& service, Error* error);
61 
62   // Signaled by |service| when stopped.
63   virtual void OnServiceStopped(const WiMaxServiceRefPtr& service);
64 
65   // Signaled by WiMaxProvider when the RPC device disappears. The provider will
66   // deregister and destroy the device after invoking this method.
67   virtual void OnDeviceVanished();
68 
69   // Returns true if this device is not connecting or connected to a service.
70   virtual bool IsIdle() const;
71 
path()72   const RpcIdentifier& path() const { return path_; }
scanning()73   bool scanning() const { return scanning_; }
networks()74   const std::set<RpcIdentifier>& networks() const { return networks_; }
75 
76  private:
77   friend class WiMaxTest;
78   FRIEND_TEST(WiMaxProviderTest, OnNetworksChanged);
79   FRIEND_TEST(WiMaxTest, ConnectTimeout);
80   FRIEND_TEST(WiMaxTest, ConnectTo);
81   FRIEND_TEST(WiMaxTest, DropService);
82   FRIEND_TEST(WiMaxTest, IsIdle);
83   FRIEND_TEST(WiMaxTest, OnConnectComplete);
84   FRIEND_TEST(WiMaxTest, OnDeviceVanished);
85   FRIEND_TEST(WiMaxTest, OnEnableComplete);
86   FRIEND_TEST(WiMaxTest, OnNetworksChanged);
87   FRIEND_TEST(WiMaxTest, OnServiceStopped);
88   FRIEND_TEST(WiMaxTest, OnStatusChanged);
89   FRIEND_TEST(WiMaxTest, StartStop);
90 
91   static const int kDefaultConnectTimeoutSeconds;
92   static const int kDefaultRPCTimeoutSeconds;
93 
94   void OnScanNetworksComplete(const Error& error);
95   void OnConnectComplete(const Error& error);
96   void OnDisconnectComplete(const Error& error);
97   void OnEnableComplete(const EnabledStateChangedCallback& callback,
98                         const Error& error);
99   void OnDisableComplete(const EnabledStateChangedCallback& callback,
100                          const Error& error);
101 
102   void OnNetworksChanged(const RpcIdentifiers& networks);
103   void OnStatusChanged(wimax_manager::DeviceStatus status);
104 
105   void DropService(Service::ConnectState state);
106 
107   // Initializes a callback that will invoke OnConnectTimeout. The timeout will
108   // not be restarted if it's already scheduled.
109   void StartConnectTimeout();
110   // Cancels the connect timeout callback, if any, previously scheduled through
111   // StartConnectTimeout.
112   void StopConnectTimeout();
113   // Returns true if a connect timeout is scheduled, false otherwise.
114   bool IsConnectTimeoutStarted() const;
115   // Called if a connect timeout scheduled through StartConnectTimeout
116   // fires. Marks the callback as stopped and invokes DropService.
117   void OnConnectTimeout();
118 
119   const RpcIdentifier path_;
120 
121   base::WeakPtrFactory<WiMax> weak_ptr_factory_;
122   std::unique_ptr<WiMaxDeviceProxyInterface> proxy_;
123   bool scanning_;
124   WiMaxServiceRefPtr pending_service_;
125   std::set<RpcIdentifier> networks_;
126   wimax_manager::DeviceStatus status_;
127 
128   base::CancelableClosure connect_timeout_callback_;
129   int connect_timeout_seconds_;
130 
131   DISALLOW_COPY_AND_ASSIGN(WiMax);
132 };
133 
134 }  // namespace shill
135 
136 #endif  // SHILL_WIMAX_WIMAX_H_
137