os.h revision b8f8f6ab38fe147fa7aed5cdd9f9ce0e5a3a2e2f
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 OS_H_
18#define OS_H_
19
20#include <time.h>
21
22#include <cstdint>
23#include <memory>
24#include <tuple>
25#include <utility>
26
27#include "android-base/macros.h"
28
29#include "wifilogd/raw_os.h"
30
31namespace android {
32namespace wifilogd {
33
34// Abstracts operating system calls.
35//
36// There are three reasons we want to abstract OS calls:
37// 1. Allow tests to run hermetically.
38// 2. Verify that the application logic invokes the OS calls as expected.
39// 3. Provide interfaces that as easier to use, than the underlying OS calls.
40class Os {
41 public:
42  using Errno = int;
43
44  struct Timestamp {
45    uint32_t secs;  // Sufficient through 2100.
46    uint32_t nsecs;
47  };
48
49  // Constructs an Os instance.
50  Os();
51
52  // Constructs an Os instance, with the caller-provided RawOs. This method
53  // allows tests to provide a MockRawOs.
54  explicit Os(std::unique_ptr<RawOs> raw_os);
55
56  virtual ~Os();
57
58  // Returns the current time, as reported by the clock with |clock_id|.
59  virtual Timestamp GetTimestamp(clockid_t clock_id) const;
60
61  // Writes |buflen| bytes from |buf| to |fd|. Returns the number of bytes
62  // written, and the result of the operation (0 for success, |errno|
63  // otherwise).
64  //
65  // Notes:
66  // - |buflen| may not exceed the maximal value for ssize_t.
67  // - The returned size_t will not exceed |buflen|.
68  virtual std::tuple<size_t, Errno> Write(int fd, const void* buf,
69                                          size_t buflen);
70
71 private:
72  const std::unique_ptr<RawOs> raw_os_;
73
74  DISALLOW_COPY_AND_ASSIGN(Os);
75};
76
77}  // namespace wifilogd
78}  // namespace android
79
80#endif  // OS_H_
81