blob: 9901fc8b3f66412f6581d1a17e1d544c00190e8e [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 Vakulenkof0f55342015-08-18 15:51:40 -070026#include <weave/network.h>
Alex Vakulenko059c30d2015-09-22 14:09:16 -070027#include <weave/wifi.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 Vakulenko059c30d2015-09-22 14:09:16 -070034class NetworkClient : public weave::Network, public weave::Wifi {
Alex Vakulenkof0f55342015-08-18 15:51:40 -070035 public:
36 explicit NetworkClient(const std::set<std::string>& device_whitelist)
37 : device_whitelist_{device_whitelist} {
38 }
39
40 ~NetworkClient() override = default;
41
42 // Implements the Network interface.
43 void AddOnConnectionChangedCallback(
44 const OnConnectionChangedCallback& listener) override {
45 }
46
Alex Vakulenko059c30d2015-09-22 14:09:16 -070047 void ConnectToService(
48 const std::string& ssid,
49 const std::string& passphrase,
50 const base::Closure& on_success,
51 const base::Callback<void(const weave::Error*)>& on_error) override {
Alex Vakulenkof0f55342015-08-18 15:51:40 -070052 }
53
54 weave::NetworkState GetConnectionState() const override {
55 return weave::NetworkState::kOffline;
56 }
57
58 void EnableAccessPoint(const std::string& ssid) override {
59 }
60
61 void DisableAccessPoint() override {
62 }
63
Alex Vakulenko35d119a2015-09-10 16:16:27 -070064 void OpenSslSocket(
Alex Vakulenkof0f55342015-08-18 15:51:40 -070065 const std::string& host,
Alex Vakulenko35d119a2015-09-10 16:16:27 -070066 uint16_t port,
Alex Vakulenkof0f55342015-08-18 15:51:40 -070067 const base::Callback<void(std::unique_ptr<weave::Stream>)>& on_success,
68 const base::Callback<void(const weave::Error*)>& on_error) override {
Alex Vakulenko35d119a2015-09-10 16:16:27 -070069 auto socket = SocketStream::ConnectBlocking(host, port);
70 if (socket) {
71 SocketStream::TlsConnect(std::move(socket), host, on_success, on_error);
72 return;
73 }
74 chromeos::ErrorPtr error;
75 chromeos::errors::system::AddSystemError(&error, FROM_HERE, errno);
76 weave::ErrorPtr weave_error;
77 ConvertError(*error.get(), &weave_error);
78 on_error.Run(weave_error.get());
Alex Vakulenkof0f55342015-08-18 15:51:40 -070079 }
80
81 static std::unique_ptr<NetworkClient> CreateInstance(
82 const std::set<std::string>& device_whitelist);
83
84 protected:
85 std::set<std::string> device_whitelist_;
86
87 private:
88 DISALLOW_COPY_AND_ASSIGN(NetworkClient);
89};
90
91} // namespace buffet
92
93#endif // BUFFET_NETWORK_CLIENT_H_
94