1 //
2 // Copyright (C) 2015 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 PROXY_DBUS_CLIENT_H
18 #define PROXY_DBUS_CLIENT_H
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sysexits.h>
23 
24 #include <iostream>
25 #include <vector>
26 #include <string>
27 
28 #include <base/cancelable_callback.h>
29 #include <base/logging.h>
30 #include <base/timer/timer.h>
31 #include <brillo/any.h>
32 #include <service_constants.h>
33 #include <shill/dbus-proxies.h>
34 
35 using ManagerProxy = org::chromium::flimflam::ManagerProxy;
36 using DeviceProxy = org::chromium::flimflam::DeviceProxy;
37 using ServiceProxy = org::chromium::flimflam::ServiceProxy;
38 using ProfileProxy = org::chromium::flimflam::ProfileProxy;
39 
40 typedef base::Callback<void(const std::string&,
41                             const brillo::Any&)> DbusPropertyChangeCallback;
42 
43 class ProxyDbusClient {
44  public:
45   enum Technology {
46     TECHNOLOGY_CELLULAR,
47     TECHNOLOGY_ETHERNET,
48     TECHNOLOGY_VPN,
49     TECHNOLOGY_WIFI,
50     TECHNOLOGY_WIMAX
51   };
52   static const char kCommonLogScopes[];
53   static const char kDbusErrorObjectUnknown[];
54   static const int kLogLevel;
55 
56   ProxyDbusClient(scoped_refptr<dbus::Bus> bus);
57   void SetLogging(Technology tech);
58   std::vector<std::unique_ptr<DeviceProxy>> GetDeviceProxies();
59   std::vector<std::unique_ptr<ServiceProxy>> GetServiceProxies();
60   std::vector<std::unique_ptr<ProfileProxy>> GetProfileProxies();
61   std::unique_ptr<DeviceProxy> GetMatchingDeviceProxy(
62       const brillo::VariantDictionary& expected_properties);
63   std::unique_ptr<ServiceProxy> GetMatchingServiceProxy(
64       const brillo::VariantDictionary& expected_properties);
65   std::unique_ptr<ProfileProxy> GetMatchingProfileProxy(
66       const brillo::VariantDictionary& expected_properties);
67   bool GetPropertyValueFromDeviceProxy(DeviceProxy* proxy,
68                                        const std::string& property_name,
69                                        brillo::Any* property_value);
70   bool GetPropertyValueFromServiceProxy(ServiceProxy* proxy,
71                                         const std::string& property_name,
72                                         brillo::Any* property_value);
73   bool GetPropertyValueFromProfileProxy(ProfileProxy* proxy,
74                                         const std::string& property_name,
75                                         brillo::Any* property_value);
76   // Optional outparams: |final_value| & |elapsed_time_seconds|. Pass nullptr
77   // if not required.
78   bool WaitForDeviceProxyPropertyValueIn(
79       const dbus::ObjectPath& object_path,
80       const std::string& property_name,
81       const std::vector<brillo::Any>& expected_values,
82       long timeout_milliseconds,
83       brillo::Any* final_value,
84       long* elapsed_time_milliseconds);
85   bool WaitForServiceProxyPropertyValueIn(
86       const dbus::ObjectPath& object_path,
87       const std::string& property_name,
88       const std::vector<brillo::Any>& expected_values,
89       long timeout_milliseconds,
90       brillo::Any* final_value,
91       long* elapsed_time_milliseconds);
92   bool WaitForProfileProxyPropertyValueIn(
93       const dbus::ObjectPath& object_path,
94       const std::string& property_name,
95       const std::vector<brillo::Any>& expected_values,
96       long timeout_milliseconds,
97       brillo::Any* final_value,
98       long* elapsed_time_milliseconds);
99   std::unique_ptr<ServiceProxy> GetServiceProxy(
100       const brillo::VariantDictionary& expected_properties);
101   std::unique_ptr<ProfileProxy> GetActiveProfileProxy();
102   // Optional outparam: |elapsed_time_seconds|. Pass nullptr
103   // if not required.
104   std::unique_ptr<ServiceProxy> WaitForMatchingServiceProxy(
105       const brillo::VariantDictionary& service_properties,
106       const std::string& service_type,
107       long timeout_milliseconds,
108       int rescan_interval_milliseconds,
109       long* elapsed_time_milliseconds);
110   bool ConfigureService(const brillo::VariantDictionary& config_params);
111   bool ConfigureServiceByGuid(const std::string& guid,
112                               const brillo::VariantDictionary& config_params);
113   bool ConnectService(const dbus::ObjectPath& object_path,
114                       long timeout_milliseconds);
115   bool DisconnectService(const dbus::ObjectPath& object_path,
116                          long timeout_milliseconds);
117   bool CreateProfile(const std::string& profile_name);
118   bool RemoveProfile(const std::string& profile_name);
119   bool PushProfile(const std::string& profile_name);
120   bool PopProfile(const std::string& profile_name);
121   bool PopAnyProfile();
122   bool RequestServiceScan(const std::string& service_type);
123   bool GetServiceOrder(std::string* order);
124   bool SetServiceOrder(const std::string& order);
125   bool SetSchedScan(bool enable);
126 
127  private:
128   bool GetPropertyValueFromManager(const std::string& property_name,
129                                    brillo::Any* property_value);
130   dbus::ObjectPath GetObjectPathForActiveProfile();
131   bool SetLoggingInternal(int level, const std::string& tags);
132   template<typename Proxy> std::unique_ptr<Proxy> GetProxyForObjectPath(
133       const dbus::ObjectPath& object_path);
134   template<typename Proxy> std::vector<std::unique_ptr<Proxy>> GetProxies(
135       const std::string& object_paths_property_name);
136   template<typename Proxy> std::unique_ptr<Proxy> GetMatchingProxy(
137       const std::string& object_paths_property_name,
138       const brillo::VariantDictionary& expected_properties);
139   template<typename Proxy> bool WaitForProxyPropertyValueIn(
140       const dbus::ObjectPath& object_path,
141       const std::string& property_name,
142       const std::vector<brillo::Any>& expected_values,
143       long timeout_milliseconds,
144       brillo::Any* final_value,
145       long* elapsed_time_milliseconds);
146   void IsMatchingServicePresent(
147       const brillo::VariantDictionary& service_properties,
148       base::Time wait_start_time,
149       bool* is_success,
150       std::unique_ptr<ServiceProxy>* service_proxy_out,
151       long* elapsed_time_milliseconds);
152   // This is invoked periodically to check if a service mathching the required
153   // params are found.
154   void FindServiceOrRestartScan(
155     const brillo::VariantDictionary& service_properties,
156     const std::string& service_type);
157   void InitiateScanForService(
158     base::TimeDelta rescan_interval,
159     const std::string& service_type,
160     const base::Closure& timer_callback);
161   void RestartScanForService(
162     const std::string& service_type);
163 
164   scoped_refptr<dbus::Bus> dbus_bus_;
165   ManagerProxy shill_manager_proxy_;
166   std::unique_ptr<base::Timer> wait_for_service_timer_;
167   base::WeakPtrFactory<ProxyDbusClient> weak_ptr_factory_;
168 };
169 #endif //PROXY_DBUS_CLIENT_H
170