blob: 290f9e967bb4c60b8250142e18f39dd642af1616 [file] [log] [blame]
mukesh agrawalec533e02016-09-30 19:04:33 -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 OS_H_
18#define OS_H_
19
20#include <time.h>
21
22#include <cstdint>
23#include <memory>
mukesh agrawala5bb08c2016-10-26 14:38:52 -070024#include <string>
mukesh agrawalb8f8f6a2016-10-06 15:11:59 -070025#include <tuple>
mukesh agrawalec533e02016-09-30 19:04:33 -070026#include <utility>
27
28#include "android-base/macros.h"
29
mukesh agrawald75fd9e2016-10-26 16:34:04 -070030#include "wifilogd/local_utils.h"
mukesh agrawalec533e02016-09-30 19:04:33 -070031#include "wifilogd/raw_os.h"
32
33namespace android {
34namespace wifilogd {
35
36// Abstracts operating system calls.
37//
38// There are three reasons we want to abstract OS calls:
39// 1. Allow tests to run hermetically.
40// 2. Verify that the application logic invokes the OS calls as expected.
41// 3. Provide interfaces that as easier to use, than the underlying OS calls.
42class Os {
43 public:
mukesh agrawalb8f8f6a2016-10-06 15:11:59 -070044 using Errno = int;
45
mukesh agrawalec533e02016-09-30 19:04:33 -070046 struct Timestamp {
47 uint32_t secs; // Sufficient through 2100.
48 uint32_t nsecs;
49 };
50
mukesh agrawalcc8458a2016-10-06 15:45:35 -070051 static constexpr int kInvalidFd = -1;
mukesh agrawal960c8a82016-10-27 17:39:18 -070052 static constexpr auto kMaxNanos = 999'999'999;
mukesh agrawalcc8458a2016-10-06 15:45:35 -070053
mukesh agrawalec533e02016-09-30 19:04:33 -070054 // Constructs an Os instance.
55 Os();
56
57 // Constructs an Os instance, with the caller-provided RawOs. This method
58 // allows tests to provide a MockRawOs.
59 explicit Os(std::unique_ptr<RawOs> raw_os);
60
61 virtual ~Os();
62
mukesh agrawala5bb08c2016-10-26 14:38:52 -070063 // Returns the Android control socket with name |socket_name|. If no such
64 // socket exists, or the init daemon has not provided this process with
65 // access to said socket, returns {kInvalidFd, errno}.
66 virtual std::tuple<int, Errno> GetControlSocket(
67 const std::string& socket_name);
68
mukesh agrawalec533e02016-09-30 19:04:33 -070069 // Returns the current time, as reported by the clock with |clock_id|.
70 virtual Timestamp GetTimestamp(clockid_t clock_id) const;
71
mukesh agrawal960c8a82016-10-27 17:39:18 -070072 // Suspends execution of this process, for |sleep_time_nsec|. The passed
73 // value must not exceed kMaxNanos.
74 virtual void Nanosleep(uint32_t sleep_time_nsec);
75
mukesh agrawald75fd9e2016-10-26 16:34:04 -070076 // Receives a datagram of up to |buflen| from |fd|, writing the data to |buf|.
77 // Returns the size of the datagram, and the result of the operation (0 for
78 // success, |errno| otherwise).
79 //
80 // Notes:
81 // - |buflen| may not exceed the maximal value for ssize_t.
82 // - The call blocks until a datagram is available.
83 // - If the datagram is larger than |buflen|, only |buflen| bytes will
84 // be received. The returned size_t will, however, reflect the full
85 // length of the datagram.
86 virtual std::tuple<size_t, Errno> ReceiveDatagram(int fd, NONNULL void* buf,
87 size_t buflen);
88
mukesh agrawalb8f8f6a2016-10-06 15:11:59 -070089 // Writes |buflen| bytes from |buf| to |fd|. Returns the number of bytes
90 // written, and the result of the operation (0 for success, |errno|
91 // otherwise).
92 //
93 // Notes:
94 // - |buflen| may not exceed the maximal value for ssize_t.
95 // - The returned size_t will not exceed |buflen|.
mukesh agrawald75fd9e2016-10-26 16:34:04 -070096 virtual std::tuple<size_t, Errno> Write(int fd, NONNULL const void* buf,
mukesh agrawalb8f8f6a2016-10-06 15:11:59 -070097 size_t buflen);
98
mukesh agrawalec533e02016-09-30 19:04:33 -070099 private:
100 const std::unique_ptr<RawOs> raw_os_;
101
102 DISALLOW_COPY_AND_ASSIGN(Os);
103};
104
105} // namespace wifilogd
106} // namespace android
107
108#endif // OS_H_