File: hotspot_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (289 lines) | stat: -rw-r--r-- 9,662 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/hotspot_util.h"

#include <optional>

#include "base/strings/string_number_conversions.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

namespace {

hotspot_config::mojom::WiFiBand ShillBandToMojom(
    const std::string& shill_band) {
  using hotspot_config::mojom::WiFiBand;

  if (shill_band == shill::kBand2GHz) {
    return WiFiBand::k2_4GHz;
  }
  if (shill_band == shill::kBandAll) {
    return WiFiBand::kAutoChoose;
  }
  NET_LOG(ERROR) << "Unexpected shill tethering band: " << shill_band;
  return WiFiBand::kAutoChoose;
}

std::string MojomBandToString(hotspot_config::mojom::WiFiBand mojom_band) {
  using hotspot_config::mojom::WiFiBand;

  switch (mojom_band) {
    case WiFiBand::k2_4GHz:
      return shill::kBand2GHz;
    case WiFiBand::kAutoChoose:
      return shill::kBandAll;
  }
}

std::string MojomSecurityToString(
    hotspot_config::mojom::WiFiSecurityMode mojom_security) {
  using hotspot_config::mojom::WiFiSecurityMode;

  switch (mojom_security) {
    case WiFiSecurityMode::kWpa2:
      return shill::kSecurityWpa2;
    case WiFiSecurityMode::kWpa3:
      return shill::kSecurityWpa3;
    case WiFiSecurityMode::kWpa2Wpa3:
      return shill::kSecurityWpa2Wpa3;
  }
}

std::string HexDecode(const std::string& hex_ssid) {
  std::string ssid;
  if (!base::HexStringToString(hex_ssid, &ssid)) {
    NET_LOG(ERROR) << "Error decoding HexSSID: " << hex_ssid;
  }

  return ssid;
}

}  // namespace

hotspot_config::mojom::HotspotState ShillTetheringStateToMojomState(
    const std::string& shill_state) {
  using hotspot_config::mojom::HotspotState;
  if (shill_state == shill::kTetheringStateActive) {
    return HotspotState::kEnabled;
  }

  if (shill_state == shill::kTetheringStateIdle) {
    return HotspotState::kDisabled;
  }

  if (shill_state == shill::kTetheringStateStarting ||
      shill_state == shill::kTetheringStateRestarting) {
    return HotspotState::kEnabling;
  }

  if (shill_state == shill::kTetheringStateStopping) {
    return HotspotState::kDisabling;
  }

  NET_LOG(ERROR) << "Unexpected shill tethering state: " << shill_state;
  return HotspotState::kDisabled;
}

hotspot_config::mojom::DisableReason ShillTetheringIdleReasonToMojomState(
    const std::string& idle_reason) {
  using hotspot_config::mojom::DisableReason;

  if (idle_reason == shill::kTetheringIdleReasonInactive) {
    return DisableReason::kAutoDisabled;
  }

  if (idle_reason == shill::kTetheringIdleReasonUpstreamDisconnect) {
    return DisableReason::kUpstreamNetworkNotAvailable;
  }

  if (idle_reason == shill::kTetheringIdleReasonError) {
    return DisableReason::kInternalError;
  }

  if (idle_reason == shill::kTetheringIdleReasonSuspend) {
    return DisableReason::kSuspended;
  }

  if (idle_reason == shill::kTetheringIdleReasonUserExit ||
      idle_reason == shill::kTetheringIdleReasonClientStop) {
    return DisableReason::kUserInitiated;
  }

  if (idle_reason == shill::kTetheringIdleReasonConfigChange) {
    return DisableReason::kRestart;
  }

  if (idle_reason == shill::kTetheringIdleReasonUpstreamNoInternet) {
    return DisableReason::kUpstreamNoInternet;
  }

  if (idle_reason == shill::kTetheringIdleReasonDownstreamLinkDisconnect) {
    return DisableReason::kDownstreamLinkDisconnect;
  }

  if (idle_reason == shill::kTetheringIdleReasonDownstreamNetworkDisconnect) {
    return DisableReason::kDownstreamNetworkDisconnect;
  }

  if (idle_reason == shill::kTetheringIdleReasonStartTimeout) {
    return DisableReason::kStartTimeout;
  }

  if (idle_reason == shill::kTetheringIdleReasonUpstreamNotAvailable) {
    return DisableReason::kUpstreamNotAvailable;
  }
  if (idle_reason == shill::kTetheringIdleReasonResourceBusy) {
    return DisableReason::kResourceBusy;
  }

  NET_LOG(ERROR) << "Unexpected idle reason: " << idle_reason;
  return DisableReason::kUnknownError;
}

hotspot_config::mojom::WiFiSecurityMode ShillSecurityToMojom(
    const std::string& shill_security) {
  using hotspot_config::mojom::WiFiSecurityMode;

  if (shill_security == shill::kSecurityWpa2) {
    return WiFiSecurityMode::kWpa2;
  }
  if (shill_security == shill::kSecurityWpa3) {
    return WiFiSecurityMode::kWpa3;
  }
  if (shill_security == shill::kSecurityWpa2Wpa3) {
    return WiFiSecurityMode::kWpa2Wpa3;
  }

  NET_LOG(ERROR) << "Unexpeted shill tethering security mode: "
                 << shill_security;
  return WiFiSecurityMode::kWpa2;
}

hotspot_config::mojom::HotspotConfigPtr ShillTetheringConfigToMojomConfig(
    const base::Value::Dict& shill_tethering_config) {
  using hotspot_config::mojom::HotspotConfig;

  auto result = HotspotConfig::New();
  std::optional<bool> auto_disable =
      shill_tethering_config.FindBool(shill::kTetheringConfAutoDisableProperty);
  if (!auto_disable) {
    NET_LOG(ERROR) << "Auto_disable not found in tethering config.";
  }
  result->auto_disable = auto_disable.value_or(true);
  const std::string* wifi_band =
      shill_tethering_config.FindString(shill::kTetheringConfBandProperty);
  if (!wifi_band) {
    NET_LOG(ERROR) << "WiFi band not found in tethering config.";
    result->band = hotspot_config::mojom::WiFiBand::kAutoChoose;
  } else {
    result->band = ShillBandToMojom(*wifi_band);
  }

  const std::string* security =
      shill_tethering_config.FindString(shill::kTetheringConfSecurityProperty);
  if (!security) {
    NET_LOG(ERROR) << "WiFi security mode not found in tethering config.";
    result->security = hotspot_config::mojom::WiFiSecurityMode::kWpa2;
  } else {
    result->security = ShillSecurityToMojom(*security);
  }

  const std::string* ssid =
      shill_tethering_config.FindString(shill::kTetheringConfSSIDProperty);
  if (!ssid) {
    NET_LOG(ERROR) << "SSID not found in tethering config.";
  }
  result->ssid = ssid ? HexDecode(*ssid) : std::string();
  const std::string* passphrase = shill_tethering_config.FindString(
      shill::kTetheringConfPassphraseProperty);
  if (!passphrase) {
    NET_LOG(ERROR) << "Passphrase not found in tethering config.";
  }
  result->passphrase = passphrase ? *passphrase : std::string();
  std::optional<bool> bssid_randomization =
      shill_tethering_config.FindBool(shill::kTetheringConfMARProperty);
  if (!bssid_randomization) {
    NET_LOG(ERROR) << shill::kTetheringConfMARProperty
                   << " not found in tethering config.";
  }
  // Default to true for privacy concern, specifically, to lower the possibility
  // of a user tracking.
  result->bssid_randomization = bssid_randomization.value_or(true);
  return result;
}

base::Value::Dict MojomConfigToShillConfig(
    const hotspot_config::mojom::HotspotConfigPtr mojom_config) {
  using hotspot_config::mojom::HotspotConfig;

  base::Value::Dict result;
  result.Set(shill::kTetheringConfAutoDisableProperty,
             mojom_config->auto_disable);
  result.Set(shill::kTetheringConfBandProperty,
             MojomBandToString(mojom_config->band));
  result.Set(shill::kTetheringConfSecurityProperty,
             MojomSecurityToString(mojom_config->security));
  result.Set(shill::kTetheringConfSSIDProperty,
             base::HexEncode(mojom_config->ssid));
  result.Set(shill::kTetheringConfPassphraseProperty, mojom_config->passphrase);
  result.Set(shill::kTetheringConfMARProperty,
             mojom_config->bssid_randomization);
  return result;
}

hotspot_config::mojom::HotspotControlResult SetTetheringEnabledResultToMojom(
    const std::string& shill_enabled_result) {
  using hotspot_config::mojom::HotspotControlResult;

  if (shill_enabled_result == shill::kTetheringEnableResultSuccess) {
    return HotspotControlResult::kSuccess;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultInvalidProperties) {
    return HotspotControlResult::kInvalidConfiguration;
  }
  if (shill_enabled_result ==
      shill::kTetheringEnableResultUpstreamNotAvailable) {
    return HotspotControlResult::kUpstreamNotAvailable;
  }
  if (shill_enabled_result ==
      shill::kTetheringEnableResultNetworkSetupFailure) {
    return HotspotControlResult::kNetworkSetupFailure;
  }
  if (shill_enabled_result ==
      shill::kTetheringEnableResultDownstreamWiFiFailure) {
    return HotspotControlResult::kDownstreamWifiFailure;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultUpstreamFailure) {
    return HotspotControlResult::kUpstreamFailure;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultWrongState) {
    return HotspotControlResult::kAlreadyFulfilled;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultAbort) {
    return HotspotControlResult::kAborted;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultNotAllowed) {
    return HotspotControlResult::kNotAllowed;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultBusy) {
    return HotspotControlResult::kBusy;
  }
  if (shill_enabled_result ==
      shill::kTetheringEnableResultConcurrencyNotSupported) {
    return HotspotControlResult::kConcurrencyNotSupported;
  }
  if (shill_enabled_result == shill::kTetheringEnableResultFailure) {
    return HotspotControlResult::kOperationFailure;
  }

  NET_LOG(ERROR) << "Unknown enable/disable tethering error: "
                 << shill_enabled_result;
  return HotspotControlResult::kUnknownFailure;
}

}  // namespace ash