blob: 90ec9b9a978090bfd9ce2c57cf913944413d962b [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>
mukesh agrawalfd080642016-10-07 15:45:37 -070021#include <string>
mukesh agrawal63068972016-09-29 17:42:46 -070022
23#include "android-base/macros.h"
mukesh agrawalfd080642016-10-07 15:45:37 -070024#include "android-base/unique_fd.h"
mukesh agrawal63068972016-09-29 17:42:46 -070025
26#include "wifilogd/local_utils.h"
27#include "wifilogd/message_buffer.h"
28#include "wifilogd/os.h"
29
30namespace android {
31namespace wifilogd {
32
33class CommandProcessor {
34 public:
35 // Constructs a CommandProcessor with a buffer of |buffer_size_bytes|.
36 explicit CommandProcessor(size_t buffer_size_bytes);
37
38 // Constructs a CommandProcessor with a buffer of |buffer_size_bytes|.
39 // The CommandProcessor will use |os| to call into operating system services.
40 // This method allows tests to provide a MockOs.
41 CommandProcessor(size_t buffer_size_bytes, std::unique_ptr<Os> os);
42
mukesh agrawalcc8458a2016-10-06 15:45:35 -070043 // Processes the given command, with the given file descriptor. The effect of
44 // this call depends on the contents of |input_buf|. In particular, depending
45 // on the command, |fd| may be used for reading or writing, or |fd| may be
46 // ignored. However, |fd| is guaranteed to be closed before ProcessCommand()
47 // returns, in all cases.
48 //
49 // (Ideally, we might want to take |fd| as a unique_fd. Unfortunately,
50 // GoogleMock doesn't deal well with move-only parameters. And we'll
51 // want to mock this method, eventually.
52 // https://github.com/google/googletest/issues/395)
53 bool ProcessCommand(NONNULL const void* input_buf, size_t n_bytes_read,
54 int fd);
mukesh agrawal63068972016-09-29 17:42:46 -070055
56 private:
mukesh agrawalfd080642016-10-07 15:45:37 -070057 class TimestampHeader {
58 public:
59 // Returns a string with a formatted representation of the timestamps
60 // contained within this header.
61 std::string ToString() const;
62
mukesh agrawal63068972016-09-29 17:42:46 -070063 Os::Timestamp since_boot_awake_only;
64 Os::Timestamp since_boot_with_sleep;
65 Os::Timestamp since_epoch; // non-monotonic
66 };
67
68 // Copies |command_buffer| into the log buffer. Returns true if the
69 // command was copied. If |command_len| exceeds protocol::kMaxMessageSize,
70 // copies the first protocol::kMaxMessageSize of |command_buffer|, and returns
71 // true.
72 bool CopyCommandToLog(NONNULL const void* command_buffer, size_t command_len);
73
mukesh agrawalfd080642016-10-07 15:45:37 -070074 // Dumps all of the logged messages to |dump_fd|. Returns true unless
75 // an unrecoverable error was encountered.
76 bool Dump(::android::base::unique_fd dump_fd);
77
mukesh agrawal63068972016-09-29 17:42:46 -070078 // The MessageBuffer is inlined, since there's not much value to mocking
79 // simple data objects. See Testing on the Toilet Episode 173.
80 MessageBuffer current_log_buffer_;
81 const std::unique_ptr<Os> os_;
82
83 DISALLOW_COPY_AND_ASSIGN(CommandProcessor);
84};
85
86} // namespace wifilogd
87} // namespace android
88
89#endif // COMMAND_PROCESSOR_H_