command_processor.h 3.6 KB

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