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> |
| 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 | |
| 28 | namespace android { |
| 29 | namespace wifilogd { |
| 30 | |
| 31 | class 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 | |
| 41 | // Processes the given command. The effect of this call depends on the |
| 42 | // contents of |input_buf|. |
| 43 | bool ProcessInput(NONNULL const void* input_buf, size_t n_bytes_read); |
| 44 | |
| 45 | private: |
| 46 | struct TimestampHeader { |
| 47 | Os::Timestamp since_boot_awake_only; |
| 48 | Os::Timestamp since_boot_with_sleep; |
| 49 | Os::Timestamp since_epoch; // non-monotonic |
| 50 | }; |
| 51 | |
| 52 | // Copies |command_buffer| into the log buffer. Returns true if the |
| 53 | // command was copied. If |command_len| exceeds protocol::kMaxMessageSize, |
| 54 | // copies the first protocol::kMaxMessageSize of |command_buffer|, and returns |
| 55 | // true. |
| 56 | bool CopyCommandToLog(NONNULL const void* command_buffer, size_t command_len); |
| 57 | |
| 58 | // The MessageBuffer is inlined, since there's not much value to mocking |
| 59 | // simple data objects. See Testing on the Toilet Episode 173. |
| 60 | MessageBuffer current_log_buffer_; |
| 61 | const std::unique_ptr<Os> os_; |
| 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(CommandProcessor); |
| 64 | }; |
| 65 | |
| 66 | } // namespace wifilogd |
| 67 | } // namespace android |
| 68 | |
| 69 | #endif // COMMAND_PROCESSOR_H_ |