blob: 24ac21fdf00c08a1bdb65edbb87a7a45acca0bcf [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"
mukesh agrawal93f78dc2016-10-13 16:07:40 -070027#include "wifilogd/memory_reader.h"
mukesh agrawal63068972016-09-29 17:42:46 -070028#include "wifilogd/message_buffer.h"
29#include "wifilogd/os.h"
30
31namespace android {
32namespace wifilogd {
33
34class CommandProcessor {
35 public:
36 // Constructs a CommandProcessor with a buffer of |buffer_size_bytes|.
37 explicit CommandProcessor(size_t buffer_size_bytes);
38
39 // Constructs a CommandProcessor with a buffer of |buffer_size_bytes|.
40 // The CommandProcessor will use |os| to call into operating system services.
41 // This method allows tests to provide a MockOs.
42 CommandProcessor(size_t buffer_size_bytes, std::unique_ptr<Os> os);
43
mukesh agrawalcc8458a2016-10-06 15:45:35 -070044 // Processes the given command, with the given file descriptor. The effect of
45 // this call depends on the contents of |input_buf|. In particular, depending
46 // on the command, |fd| may be used for reading or writing, or |fd| may be
47 // ignored. However, |fd| is guaranteed to be closed before ProcessCommand()
48 // returns, in all cases.
49 //
50 // (Ideally, we might want to take |fd| as a unique_fd. Unfortunately,
51 // GoogleMock doesn't deal well with move-only parameters. And we'll
52 // want to mock this method, eventually.
53 // https://github.com/google/googletest/issues/395)
54 bool ProcessCommand(NONNULL const void* input_buf, size_t n_bytes_read,
55 int fd);
mukesh agrawal63068972016-09-29 17:42:46 -070056
57 private:
mukesh agrawalfd080642016-10-07 15:45:37 -070058 class TimestampHeader {
59 public:
60 // Returns a string with a formatted representation of the timestamps
61 // contained within this header.
62 std::string ToString() const;
63
mukesh agrawal63068972016-09-29 17:42:46 -070064 Os::Timestamp since_boot_awake_only;
65 Os::Timestamp since_boot_with_sleep;
66 Os::Timestamp since_epoch; // non-monotonic
67 };
68
69 // Copies |command_buffer| into the log buffer. Returns true if the
70 // command was copied. If |command_len| exceeds protocol::kMaxMessageSize,
71 // copies the first protocol::kMaxMessageSize of |command_buffer|, and returns
72 // true.
73 bool CopyCommandToLog(NONNULL const void* command_buffer, size_t command_len);
74
mukesh agrawalfd080642016-10-07 15:45:37 -070075 // Dumps all of the logged messages to |dump_fd|. Returns true unless
76 // an unrecoverable error was encountered.
77 bool Dump(::android::base::unique_fd dump_fd);
78
mukesh agrawal93f78dc2016-10-13 16:07:40 -070079 // Returns a human-friendly string representation of the AsciiMessage
80 // contained at the head of the memory referenced by |memory_reader|.
81 // Validates that |memory_reader| has enough bytes to contain an AsciiMessage
82 // header, and the payload described by that header. Reports any errors in
83 // the returned string.
84 std::string FormatAsciiMessage(MemoryReader memory_reader);
85
mukesh agrawal63068972016-09-29 17:42:46 -070086 // The MessageBuffer is inlined, since there's not much value to mocking
87 // simple data objects. See Testing on the Toilet Episode 173.
mukesh agrawal93f78dc2016-10-13 16:07:40 -070088 //
89 // Note that the messages in |current_log_buffer_| have not been validated,
90 // expect to ensure that:
91 // a) each message starts with a TimestampHeader, and
92 // b) each message is large enough for a protocol::Command to follow the
93 // TimestampHeader,and
94 // c) the protocol::Command::opcode for each message is a supported opcode.
mukesh agrawal63068972016-09-29 17:42:46 -070095 MessageBuffer current_log_buffer_;
96 const std::unique_ptr<Os> os_;
97
98 DISALLOW_COPY_AND_ASSIGN(CommandProcessor);
99};
100
101} // namespace wifilogd
102} // namespace android
103
104#endif // COMMAND_PROCESSOR_H_