blob: 62bed3115890d119085ca49286f2c17525e254ea [file] [log] [blame]
mukesh agrawal3b1d20a2016-10-27 14:19:12 -07001/*
2 * Copyright (C) 2016 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 <array>
18#include <cstdint>
19#include <cstring>
20#include <memory>
21#include <utility>
22
23#include "android-base/logging.h"
24
25#include "wifilogd/main_loop.h"
26#include "wifilogd/protocol.h"
27
28namespace android {
29namespace wifilogd {
30
31namespace {
32constexpr auto kMainBufferSizeBytes = 128 * 1024;
33}
34
35MainLoop::MainLoop(const std::string& socket_name)
36 : MainLoop(socket_name, std::make_unique<Os>(),
37 std::make_unique<CommandProcessor>(kMainBufferSizeBytes)) {}
38
39MainLoop::MainLoop(const std::string& socket_name, std::unique_ptr<Os> os,
40 std::unique_ptr<CommandProcessor> command_processor)
41 : os_(std::move(os)), command_processor_(std::move(command_processor)) {
42 Os::Errno err;
43 std::tie(sock_fd_, err) = os_->GetControlSocket(socket_name);
44 if (err) {
45 LOG(FATAL) << "Failed to get control socket: " << std::strerror(errno);
46 }
47}
48
49void MainLoop::RunOnce() {
50 std::array<uint8_t, protocol::kMaxMessageSize> input_buf;
51 size_t datagram_len;
52 Os::Errno err;
53 std::tie(datagram_len, err) =
54 os_->ReceiveDatagram(sock_fd_, input_buf.data(), input_buf.size());
55 if (err) {
56 // TODO(b/32098735): Increment stats counter.
57 // TODO(b/32481888): Improve error handling.
58 return;
59 }
60
61 if (datagram_len > protocol::kMaxMessageSize) {
62 // TODO(b/32098735): Increment stats counter.
63 datagram_len = protocol::kMaxMessageSize;
64 }
65
66 command_processor_->ProcessCommand(input_buf.data(), datagram_len,
67 Os::kInvalidFd);
68}
69
70} // namespace wifilogd
71} // namespace android