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 "proxy_rpc_in_data_types.h"
18 #include "proxy_util.h"
19 
20 namespace {
21 // Autotest Server test encodes the object type in this key.
22 static const char kXmlRpcStructTypeKey[] = "xmlrpc_struct_type_key";
23 
AssertStructTypeStringFromXmlRpcValue(XmlRpc::XmlRpcValue * xml_rpc_value_in,std::string expected_type)24 void AssertStructTypeStringFromXmlRpcValue(
25     XmlRpc::XmlRpcValue* xml_rpc_value_in,
26     std::string expected_type) {
27   if ((*xml_rpc_value_in)[kXmlRpcStructTypeKey] != expected_type) {
28     LOG(FATAL) << "Unexpected object received. Expected: " << expected_type
29                << "Recieved: " << (*xml_rpc_value_in)[kXmlRpcStructTypeKey];
30   }
31 }
32 
ParseStationTypeFromXmlRpcValue(XmlRpc::XmlRpcValue * xml_rpc_value_in)33 ProxyShillWifiClient::StationType ParseStationTypeFromXmlRpcValue(
34     XmlRpc::XmlRpcValue* xml_rpc_value_in) {
35   ProxyShillWifiClient::StationType station_type;
36   std::string station_type_as_string;
37   GetStringValueFromXmlRpcValueStructMember(
38       xml_rpc_value_in, "station_type", "managed", &station_type_as_string);
39   if (station_type_as_string == "managed") {
40     station_type = ProxyShillWifiClient::kStationTypeManaged;
41   } else if (station_type_as_string == "ibss") {
42     station_type = ProxyShillWifiClient::kStationTypeIBSS;
43   } else {
44     station_type = ProxyShillWifiClient::kStationTypeUnknown;
45   }
46   return station_type;
47 }
48 
ParseAutoConnectTypeFromXmlRpcValue(XmlRpc::XmlRpcValue * xml_rpc_value_in)49 ProxyShillWifiClient::AutoConnectType ParseAutoConnectTypeFromXmlRpcValue(
50     XmlRpc::XmlRpcValue* xml_rpc_value_in) {
51   ProxyShillWifiClient::AutoConnectType autoconnect_type;
52   bool autoconnect_type_as_bool;
53   if (GetBoolValueFromXmlRpcValueStructMember(
54           xml_rpc_value_in, "autoconnect", false, &autoconnect_type_as_bool)) {
55     if (autoconnect_type_as_bool) {
56       autoconnect_type = ProxyShillWifiClient::kAutoConnectTypeEnabled;
57     } else {
58       autoconnect_type = ProxyShillWifiClient::kAutoConnectTypeDisabled;
59     }
60   } else {
61     autoconnect_type = ProxyShillWifiClient::kAutoConnectTypeUnspecified;
62   }
63   return autoconnect_type;
64 }
65 
66 } // namespace
67 
68 const  int BgscanConfiguration::kDefaultShortIntervalSeconds = 30;
69 const int BgscanConfiguration::kDefaultLongIntervalSeconds = 180;
70 const int BgscanConfiguration::kDefaultSignalThreshold = -50;
71 const char BgscanConfiguration::kDefaultScanMethod[] = "default";
72 const int AssociationParameters::kDefaultDiscoveryTimeoutSeconds = 15;
73 const int AssociationParameters::kDefaultAssociationTimeoutSeconds = 15;
74 const int AssociationParameters::kDefaultConfigurationTimeoutSeconds = 15;
75 
BgscanConfiguration(XmlRpc::XmlRpcValue * xml_rpc_value_in)76 BgscanConfiguration::BgscanConfiguration(
77     XmlRpc::XmlRpcValue* xml_rpc_value_in) {
78   AssertStructTypeStringFromXmlRpcValue(
79       xml_rpc_value_in, "BgscanConfiguration");
80   GetStringValueFromXmlRpcValueStructMember(
81       xml_rpc_value_in, "interface", std::string(), &interface_);
82   GetIntValueFromXmlRpcValueStructMember(
83       xml_rpc_value_in, "signal", kDefaultSignalThreshold, &signal_threshold_);
84   GetIntValueFromXmlRpcValueStructMember(
85       xml_rpc_value_in, "short_interval", kDefaultShortIntervalSeconds,
86       &short_interval_);
87   GetIntValueFromXmlRpcValueStructMember(
88       xml_rpc_value_in, "short_interval", kDefaultLongIntervalSeconds,
89       &long_interval_);
90   GetStringValueFromXmlRpcValueStructMember(
91       xml_rpc_value_in, "method", kDefaultScanMethod, &method_);
92 }
93 
AssociationParameters(XmlRpc::XmlRpcValue * xml_rpc_value_in)94 AssociationParameters::AssociationParameters(
95     XmlRpc::XmlRpcValue* xml_rpc_value_in) {
96   AssertStructTypeStringFromXmlRpcValue(
97       xml_rpc_value_in, "AssociationParameters");
98   GetStringValueFromXmlRpcValueStructMember(
99       xml_rpc_value_in, "ssid", std::string(), &ssid_);
100   GetIntValueFromXmlRpcValueStructMember(
101       xml_rpc_value_in, "discovery_timeout",
102       kDefaultDiscoveryTimeoutSeconds, &discovery_timeout_seconds_);
103   GetIntValueFromXmlRpcValueStructMember(
104       xml_rpc_value_in, "association_timeout",
105       kDefaultAssociationTimeoutSeconds, &association_timeout_seconds_);
106   GetIntValueFromXmlRpcValueStructMember(
107       xml_rpc_value_in, "configuration_timeout",
108       kDefaultConfigurationTimeoutSeconds, &configuration_timeout_seconds_);
109   GetBoolValueFromXmlRpcValueStructMember(
110       xml_rpc_value_in, "is_hidden", false, &is_hidden_);
111   GetBoolValueFromXmlRpcValueStructMember(
112       xml_rpc_value_in, "save_credentials", false, &save_credentials_);
113   station_type_ = ParseStationTypeFromXmlRpcValue(xml_rpc_value_in);
114   GetStringValueFromXmlRpcValueStructMember(
115       xml_rpc_value_in, "guid", std::string(), &guid_);
116   GetBoolValueFromXmlRpcValueStructMember(
117       xml_rpc_value_in, "expect_failure", false, &expect_failure_);
118   autoconnect_type_ = ParseAutoConnectTypeFromXmlRpcValue(xml_rpc_value_in);
119   bgscan_config_ = std::unique_ptr<BgscanConfiguration>(
120       new BgscanConfiguration(&(*xml_rpc_value_in)["bgscan_config"]));
121   security_config_ = SecurityConfig::CreateSecurityConfigObject(
122       &(*xml_rpc_value_in)["security_config"]);
123 }
124 
ConfigureServiceParameters(XmlRpc::XmlRpcValue * xml_rpc_value_in)125 ConfigureServiceParameters::ConfigureServiceParameters(
126     XmlRpc::XmlRpcValue* xml_rpc_value_in) {
127   AssertStructTypeStringFromXmlRpcValue(
128       xml_rpc_value_in, "ConfigureServiceParameters");
129   GetStringValueFromXmlRpcValueStructMember(
130       xml_rpc_value_in, "guid", std::string(), &guid_);
131   GetStringValueFromXmlRpcValueStructMember(
132       xml_rpc_value_in, "passphrase", std::string(), &passphrase_);
133   autoconnect_type_ = ParseAutoConnectTypeFromXmlRpcValue(xml_rpc_value_in);
134 }
135