• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "shill/dbus/chromeos_third_party_vpn_dbus_adaptor.h"
18 
19 #include <base/logging.h>
20 #if defined(__ANDROID__)
21 #include <dbus/service_constants.h>
22 #else
23 #include <chromeos/dbus/service_constants.h>
24 #endif  // __ANDROID__
25 
26 #include "shill/logging.h"
27 #include "shill/service.h"
28 #include "shill/vpn/third_party_vpn_driver.h"
29 
30 using brillo::dbus_utils::AsyncEventSequencer;
31 using brillo::dbus_utils::ExportedObjectManager;
32 
33 namespace shill {
34 
35 namespace Logging {
36 
37 static auto kModuleLogScope = ScopeLogger::kVPN;
ObjectID(const ChromeosThirdPartyVpnDBusAdaptor * v)38 static std::string ObjectID(const ChromeosThirdPartyVpnDBusAdaptor* v) {
39   return "(third_party_vpn_dbus_adaptor)";
40 }
41 
42 }  // namespace Logging
43 
44 namespace {
45 
46 // The API converts external connection state to internal one
ConvertConnectState(ChromeosThirdPartyVpnDBusAdaptor::ExternalConnectState external_state,Service::ConnectState * internal_state)47 bool ConvertConnectState(
48     ChromeosThirdPartyVpnDBusAdaptor::ExternalConnectState external_state,
49     Service::ConnectState* internal_state) {
50   switch (external_state) {
51     case ChromeosThirdPartyVpnDBusAdaptor::kStateConnected:
52       *internal_state = Service::kStateOnline;
53       break;
54     case ChromeosThirdPartyVpnDBusAdaptor::kStateFailure:
55       *internal_state = Service::kStateFailure;
56       break;
57     default:
58       return false;
59   }
60   return true;
61 }
62 
63 }  // namespace
64 
ChromeosThirdPartyVpnDBusAdaptor(const scoped_refptr<dbus::Bus> & bus,ThirdPartyVpnDriver * client)65 ChromeosThirdPartyVpnDBusAdaptor::ChromeosThirdPartyVpnDBusAdaptor(
66     const scoped_refptr<dbus::Bus>& bus,
67     ThirdPartyVpnDriver* client)
68     : org::chromium::flimflam::ThirdPartyVpnAdaptor(this),
69       ChromeosDBusAdaptor(bus,
70                           kObjectPathBase + client->object_path_suffix()),
71       client_(client) {
72   // Register DBus object.
73   RegisterWithDBusObject(dbus_object());
74   dbus_object()->RegisterAndBlock();
75 }
76 
~ChromeosThirdPartyVpnDBusAdaptor()77 ChromeosThirdPartyVpnDBusAdaptor::~ChromeosThirdPartyVpnDBusAdaptor() {
78   dbus_object()->UnregisterAsync();
79 }
80 
EmitPacketReceived(const std::vector<uint8_t> & packet)81 void ChromeosThirdPartyVpnDBusAdaptor::EmitPacketReceived(
82     const std::vector<uint8_t>& packet) {
83   SLOG(this, 2) << __func__;
84   SendOnPacketReceivedSignal(packet);
85 }
86 
EmitPlatformMessage(uint32_t message)87 void ChromeosThirdPartyVpnDBusAdaptor::EmitPlatformMessage(uint32_t message) {
88   SLOG(this, 2) << __func__ << "(" << message << ")";
89   SendOnPlatformMessageSignal(message);
90 }
91 
SetParameters(brillo::ErrorPtr * error,const std::map<std::string,std::string> & parameters,std::string * warning_message)92 bool ChromeosThirdPartyVpnDBusAdaptor::SetParameters(
93     brillo::ErrorPtr* error,
94     const std::map<std::string, std::string>& parameters,
95     std::string* warning_message) {
96   SLOG(this, 2) << __func__;
97   std::string error_message;
98   Error e;
99   client_->SetParameters(parameters, &error_message, warning_message);
100   if (!error_message.empty()) {
101     e.Populate(Error::kInvalidArguments, error_message);
102   }
103   return !e.ToChromeosError(error);
104 }
105 
UpdateConnectionState(brillo::ErrorPtr * error,uint32_t connection_state)106 bool ChromeosThirdPartyVpnDBusAdaptor::UpdateConnectionState(
107     brillo::ErrorPtr* error, uint32_t connection_state) {
108   SLOG(this, 2) << __func__ << "(" << connection_state << ")";
109   // Externally supported states are from Service::kStateConnected to
110   // Service::kStateOnline.
111   Service::ConnectState internal_state;
112   std::string error_message;
113   Error e;
114   if (ConvertConnectState(static_cast<ExternalConnectState>(connection_state),
115                           &internal_state)) {
116     client_->UpdateConnectionState(internal_state, &error_message);
117     if (!error_message.empty()) {
118       e.Populate(Error::kInvalidArguments, error_message);
119     }
120   } else {
121     e.Populate(Error::kNotSupported, "Connection state is not supported");
122   }
123   return !e.ToChromeosError(error);
124 }
125 
SendPacket(brillo::ErrorPtr * error,const std::vector<uint8_t> & ip_packet)126 bool ChromeosThirdPartyVpnDBusAdaptor::SendPacket(
127     brillo::ErrorPtr* error,
128     const std::vector<uint8_t>& ip_packet) {
129   SLOG(this, 2) << __func__;
130   std::string error_message;
131   client_->SendPacket(ip_packet, &error_message);
132   Error e;
133   if (!error_message.empty()) {
134     e.Populate(Error::kWrongState, error_message);
135   }
136   return !e.ToChromeosError(error);
137 }
138 
139 }  // namespace shill
140