mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 1 | /* |
| 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 agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 21 | #include <string> |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 22 | |
| 23 | #include "android-base/macros.h" |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 24 | #include "android-base/unique_fd.h" |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 25 | |
| 26 | #include "wifilogd/local_utils.h" |
mukesh agrawal | 93f78dc | 2016-10-13 16:07:40 -0700 | [diff] [blame] | 27 | #include "wifilogd/memory_reader.h" |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 28 | #include "wifilogd/message_buffer.h" |
| 29 | #include "wifilogd/os.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace wifilogd { |
| 33 | |
| 34 | class 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 agrawal | 82816d2 | 2016-10-27 14:18:29 -0700 | [diff] [blame] | 44 | virtual ~CommandProcessor(); |
| 45 | |
mukesh agrawal | cc8458a | 2016-10-06 15:45:35 -0700 | [diff] [blame] | 46 | // Processes the given command, with the given file descriptor. The effect of |
| 47 | // this call depends on the contents of |input_buf|. In particular, depending |
| 48 | // on the command, |fd| may be used for reading or writing, or |fd| may be |
| 49 | // ignored. However, |fd| is guaranteed to be closed before ProcessCommand() |
| 50 | // returns, in all cases. |
| 51 | // |
| 52 | // (Ideally, we might want to take |fd| as a unique_fd. Unfortunately, |
mukesh agrawal | 82816d2 | 2016-10-27 14:18:29 -0700 | [diff] [blame] | 53 | // GoogleMock doesn't deal well with move-only parameters. |
mukesh agrawal | cc8458a | 2016-10-06 15:45:35 -0700 | [diff] [blame] | 54 | // https://github.com/google/googletest/issues/395) |
mukesh agrawal | 82816d2 | 2016-10-27 14:18:29 -0700 | [diff] [blame] | 55 | virtual bool ProcessCommand(NONNULL const void* input_buf, |
| 56 | size_t n_bytes_read, int fd); |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 59 | // Copies |command_buffer| into the log buffer. Returns true if the |
| 60 | // command was copied. If |command_len| exceeds protocol::kMaxMessageSize, |
| 61 | // copies the first protocol::kMaxMessageSize of |command_buffer|, and returns |
| 62 | // true. |
| 63 | bool CopyCommandToLog(NONNULL const void* command_buffer, size_t command_len); |
| 64 | |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 65 | // Dumps all of the logged messages to |dump_fd|. Returns true unless |
| 66 | // an unrecoverable error was encountered. |
| 67 | bool Dump(::android::base::unique_fd dump_fd); |
| 68 | |
mukesh agrawal | 93f78dc | 2016-10-13 16:07:40 -0700 | [diff] [blame] | 69 | // Returns a human-friendly string representation of the AsciiMessage |
| 70 | // contained at the head of the memory referenced by |memory_reader|. |
| 71 | // Validates that |memory_reader| has enough bytes to contain an AsciiMessage |
| 72 | // header, and the payload described by that header. Reports any errors in |
| 73 | // the returned string. |
| 74 | std::string FormatAsciiMessage(MemoryReader memory_reader); |
| 75 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 76 | // The MessageBuffer is inlined, since there's not much value to mocking |
| 77 | // simple data objects. See Testing on the Toilet Episode 173. |
mukesh agrawal | 93f78dc | 2016-10-13 16:07:40 -0700 | [diff] [blame] | 78 | // |
| 79 | // Note that the messages in |current_log_buffer_| have not been validated, |
| 80 | // expect to ensure that: |
| 81 | // a) each message starts with a TimestampHeader, and |
| 82 | // b) each message is large enough for a protocol::Command to follow the |
| 83 | // TimestampHeader,and |
| 84 | // c) the protocol::Command::opcode for each message is a supported opcode. |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 85 | MessageBuffer current_log_buffer_; |
| 86 | const std::unique_ptr<Os> os_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(CommandProcessor); |
| 89 | }; |
| 90 | |
| 91 | } // namespace wifilogd |
| 92 | } // namespace android |
| 93 | |
| 94 | #endif // COMMAND_PROCESSOR_H_ |