blob: 81d1e779fb72db291d4f1863fd2e81e10616b187 [file] [log] [blame]
Alex Vakulenkof0f55342015-08-18 15:51:40 -07001/*
2 * Copyright 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 BUFFET_NETWORK_CLIENT_H_
18#define BUFFET_NETWORK_CLIENT_H_
19
20#include <memory>
21#include <set>
22#include <string>
23
24#include <base/cancelable_callback.h>
Alex Vakulenko35d119a2015-09-10 16:16:27 -070025#include <chromeos/errors/error_codes.h>
Alex Vakulenko0fef8152015-09-25 08:45:22 -070026#include <weave/network_provider.h>
27#include <weave/wifi_provider.h>
Alex Vakulenkof0f55342015-08-18 15:51:40 -070028
29#include "buffet/socket_stream.h"
Alex Vakulenko35d119a2015-09-10 16:16:27 -070030#include "buffet/weave_error_conversion.h"
Alex Vakulenkof0f55342015-08-18 15:51:40 -070031
32namespace buffet {
33
Alex Vakulenko0fef8152015-09-25 08:45:22 -070034class NetworkClient : public weave::NetworkProvider,
35 public weave::WifiProvider {
Alex Vakulenkof0f55342015-08-18 15:51:40 -070036 public:
37 explicit NetworkClient(const std::set<std::string>& device_whitelist)
38 : device_whitelist_{device_whitelist} {
39 }
40
41 ~NetworkClient() override = default;
42
Alex Vakulenko0fef8152015-09-25 08:45:22 -070043 // NetworkProvider implementation.
44 void AddConnectionChangedCallback(
45 const ConnectionChangedCallback& listener) override {}
Alex Vakulenkof0f55342015-08-18 15:51:40 -070046
47 weave::NetworkState GetConnectionState() const override {
48 return weave::NetworkState::kOffline;
49 }
50
Alex Vakulenko35d119a2015-09-10 16:16:27 -070051 void OpenSslSocket(
Alex Vakulenkof0f55342015-08-18 15:51:40 -070052 const std::string& host,
Alex Vakulenko35d119a2015-09-10 16:16:27 -070053 uint16_t port,
Alex Vakulenkof0f55342015-08-18 15:51:40 -070054 const base::Callback<void(std::unique_ptr<weave::Stream>)>& on_success,
Alex Vakulenko0fef8152015-09-25 08:45:22 -070055 const weave::ErrorCallback& on_error) override {
Alex Vakulenko35d119a2015-09-10 16:16:27 -070056 auto socket = SocketStream::ConnectBlocking(host, port);
57 if (socket) {
58 SocketStream::TlsConnect(std::move(socket), host, on_success, on_error);
59 return;
60 }
61 chromeos::ErrorPtr error;
62 chromeos::errors::system::AddSystemError(&error, FROM_HERE, errno);
63 weave::ErrorPtr weave_error;
64 ConvertError(*error.get(), &weave_error);
65 on_error.Run(weave_error.get());
Alex Vakulenkof0f55342015-08-18 15:51:40 -070066 }
67
Alex Vakulenko0fef8152015-09-25 08:45:22 -070068 // WifiProvider implementation.
69 void Connect(const std::string& ssid,
70 const std::string& passphrase,
71 const weave::SuccessCallback& on_success,
72 const weave::ErrorCallback& on_error) override {}
73
74 void StartAccessPoint(const std::string& ssid) override {}
75
76 void StopAccessPoint() override {}
77
Alex Vakulenkof0f55342015-08-18 15:51:40 -070078 static std::unique_ptr<NetworkClient> CreateInstance(
79 const std::set<std::string>& device_whitelist);
80
81 protected:
82 std::set<std::string> device_whitelist_;
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(NetworkClient);
86};
87
88} // namespace buffet
89
90#endif // BUFFET_NETWORK_CLIENT_H_
91