blob: 0c2ae66997150055ce4f1e9facb5b50be4980199 [file] [log] [blame]
mukesh agrawal63068972016-09-29 17:42:46 -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#ifndef COMMAND_PROCESSOR_H_
18#define COMMAND_PROCESSOR_H_
19
20#include <memory>
21
22#include "android-base/macros.h"
23
24#include "wifilogd/local_utils.h"
25#include "wifilogd/message_buffer.h"
26#include "wifilogd/os.h"
27
28namespace android {
29namespace wifilogd {
30
31class CommandProcessor {
32 public:
33 // Constructs a CommandProcessor with a buffer of |buffer_size_bytes|.
34 explicit CommandProcessor(size_t buffer_size_bytes);
35
36 // Constructs a CommandProcessor with a buffer of |buffer_size_bytes|.
37 // The CommandProcessor will use |os| to call into operating system services.
38 // This method allows tests to provide a MockOs.
39 CommandProcessor(size_t buffer_size_bytes, std::unique_ptr<Os> os);
40
mukesh agrawalcc8458a2016-10-06 15:45:35 -070041 // Processes the given command, with the given file descriptor. The effect of
42 // this call depends on the contents of |input_buf|. In particular, depending
43 // on the command, |fd| may be used for reading or writing, or |fd| may be
44 // ignored. However, |fd| is guaranteed to be closed before ProcessCommand()
45 // returns, in all cases.
46 //
47 // (Ideally, we might want to take |fd| as a unique_fd. Unfortunately,
48 // GoogleMock doesn't deal well with move-only parameters. And we'll
49 // want to mock this method, eventually.
50 // https://github.com/google/googletest/issues/395)
51 bool ProcessCommand(NONNULL const void* input_buf, size_t n_bytes_read,
52 int fd);
mukesh agrawal63068972016-09-29 17:42:46 -070053
54 private:
55 struct TimestampHeader {
56 Os::Timestamp since_boot_awake_only;
57 Os::Timestamp since_boot_with_sleep;
58 Os::Timestamp since_epoch; // non-monotonic
59 };
60
61 // Copies |command_buffer| into the log buffer. Returns true if the
62 // command was copied. If |command_len| exceeds protocol::kMaxMessageSize,
63 // copies the first protocol::kMaxMessageSize of |command_buffer|, and returns
64 // true.
65 bool CopyCommandToLog(NONNULL const void* command_buffer, size_t command_len);
66
67 // The MessageBuffer is inlined, since there's not much value to mocking
68 // simple data objects. See Testing on the Toilet Episode 173.
69 MessageBuffer current_log_buffer_;
70 const std::unique_ptr<Os> os_;
71
72 DISALLOW_COPY_AND_ASSIGN(CommandProcessor);
73};
74
75} // namespace wifilogd
76} // namespace android
77
78#endif // COMMAND_PROCESSOR_H_