blob: b14d3d8bcdf349db2e2f5c244ab9158a714aec25 [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;
mukesh agrawalfd592212016-11-11 16:20:15 -080033// TODO(b/32840641): Tune the sleep time.
34constexpr auto kTransientErrorSleepTimeNsec = 100 * 1000; // 100 usec
mukesh agrawal3b1d20a2016-10-27 14:19:12 -070035}
36
37MainLoop::MainLoop(const std::string& socket_name)
38 : MainLoop(socket_name, std::make_unique<Os>(),
39 std::make_unique<CommandProcessor>(kMainBufferSizeBytes)) {}
40
41MainLoop::MainLoop(const std::string& socket_name, std::unique_ptr<Os> os,
42 std::unique_ptr<CommandProcessor> command_processor)
43 : os_(std::move(os)), command_processor_(std::move(command_processor)) {
44 Os::Errno err;
45 std::tie(sock_fd_, err) = os_->GetControlSocket(socket_name);
46 if (err) {
Elliott Hughes53ec1e82019-03-08 13:24:49 -080047 PLOG(FATAL) << "Failed to get control socket";
mukesh agrawal3b1d20a2016-10-27 14:19:12 -070048 }
49}
50
51void MainLoop::RunOnce() {
52 std::array<uint8_t, protocol::kMaxMessageSize> input_buf;
53 size_t datagram_len;
54 Os::Errno err;
55 std::tie(datagram_len, err) =
56 os_->ReceiveDatagram(sock_fd_, input_buf.data(), input_buf.size());
57 if (err) {
mukesh agrawalfd592212016-11-11 16:20:15 -080058 ProcessError(err);
mukesh agrawal3b1d20a2016-10-27 14:19:12 -070059 return;
60 }
61
62 if (datagram_len > protocol::kMaxMessageSize) {
63 // TODO(b/32098735): Increment stats counter.
64 datagram_len = protocol::kMaxMessageSize;
65 }
66
67 command_processor_->ProcessCommand(input_buf.data(), datagram_len,
68 Os::kInvalidFd);
69}
70
mukesh agrawalfd592212016-11-11 16:20:15 -080071// Private methods below.
72
73void MainLoop::ProcessError(Os::Errno err) {
74 if (err == EINTR || err == ENOMEM) {
75 // TODO(b/32098735): Increment stats counter.
76 os_->Nanosleep(kTransientErrorSleepTimeNsec);
77 return;
78 }
79
80 // Any other error is unexpected, and assumed to be non-recoverable.
81 // (If, e.g., our socket is in a bad state, then we won't be able to receive
82 // any new log messages.)
Elliott Hughes53ec1e82019-03-08 13:24:49 -080083 PLOG(FATAL) << "Unexpected error";
mukesh agrawalfd592212016-11-11 16:20:15 -080084}
85
mukesh agrawal3b1d20a2016-10-27 14:19:12 -070086} // namespace wifilogd
87} // namespace android