mukesh agrawal | ec533e0 | 2016-09-30 19:04:33 -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 OS_H_ |
| 18 | #define OS_H_ |
| 19 | |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <cstdint> |
| 23 | #include <memory> |
mukesh agrawal | a5bb08c | 2016-10-26 14:38:52 -0700 | [diff] [blame] | 24 | #include <string> |
mukesh agrawal | b8f8f6a | 2016-10-06 15:11:59 -0700 | [diff] [blame] | 25 | #include <tuple> |
mukesh agrawal | ec533e0 | 2016-09-30 19:04:33 -0700 | [diff] [blame] | 26 | #include <utility> |
| 27 | |
| 28 | #include "android-base/macros.h" |
| 29 | |
mukesh agrawal | d75fd9e | 2016-10-26 16:34:04 -0700 | [diff] [blame] | 30 | #include "wifilogd/local_utils.h" |
mukesh agrawal | ec533e0 | 2016-09-30 19:04:33 -0700 | [diff] [blame] | 31 | #include "wifilogd/raw_os.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace 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. |
| 42 | class Os { |
| 43 | public: |
mukesh agrawal | b8f8f6a | 2016-10-06 15:11:59 -0700 | [diff] [blame] | 44 | using Errno = int; |
| 45 | |
mukesh agrawal | ec533e0 | 2016-09-30 19:04:33 -0700 | [diff] [blame] | 46 | struct Timestamp { |
| 47 | uint32_t secs; // Sufficient through 2100. |
| 48 | uint32_t nsecs; |
| 49 | }; |
| 50 | |
mukesh agrawal | cc8458a | 2016-10-06 15:45:35 -0700 | [diff] [blame] | 51 | static constexpr int kInvalidFd = -1; |
mukesh agrawal | 960c8a8 | 2016-10-27 17:39:18 -0700 | [diff] [blame] | 52 | static constexpr auto kMaxNanos = 999'999'999; |
mukesh agrawal | cc8458a | 2016-10-06 15:45:35 -0700 | [diff] [blame] | 53 | |
mukesh agrawal | ec533e0 | 2016-09-30 19:04:33 -0700 | [diff] [blame] | 54 | // 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 agrawal | a5bb08c | 2016-10-26 14:38:52 -0700 | [diff] [blame] | 63 | // 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 agrawal | ec533e0 | 2016-09-30 19:04:33 -0700 | [diff] [blame] | 69 | // Returns the current time, as reported by the clock with |clock_id|. |
| 70 | virtual Timestamp GetTimestamp(clockid_t clock_id) const; |
| 71 | |
mukesh agrawal | 960c8a8 | 2016-10-27 17:39:18 -0700 | [diff] [blame] | 72 | // 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 agrawal | d75fd9e | 2016-10-26 16:34:04 -0700 | [diff] [blame] | 76 | // 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 agrawal | b8f8f6a | 2016-10-06 15:11:59 -0700 | [diff] [blame] | 89 | // 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 agrawal | d75fd9e | 2016-10-26 16:34:04 -0700 | [diff] [blame] | 96 | virtual std::tuple<size_t, Errno> Write(int fd, NONNULL const void* buf, |
mukesh agrawal | b8f8f6a | 2016-10-06 15:11:59 -0700 | [diff] [blame] | 97 | size_t buflen); |
| 98 | |
mukesh agrawal | ec533e0 | 2016-09-30 19:04:33 -0700 | [diff] [blame] | 99 | 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_ |