os.h revision cc8458a847dafc8be2ce5de5a0e39a292780e6d5
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 static constexpr int kInvalidFd = -1; 50 51 // Constructs an Os instance. 52 Os(); 53 54 // Constructs an Os instance, with the caller-provided RawOs. This method 55 // allows tests to provide a MockRawOs. 56 explicit Os(std::unique_ptr<RawOs> raw_os); 57 58 virtual ~Os(); 59 60 // Returns the current time, as reported by the clock with |clock_id|. 61 virtual Timestamp GetTimestamp(clockid_t clock_id) const; 62 63 // Writes |buflen| bytes from |buf| to |fd|. Returns the number of bytes 64 // written, and the result of the operation (0 for success, |errno| 65 // otherwise). 66 // 67 // Notes: 68 // - |buflen| may not exceed the maximal value for ssize_t. 69 // - The returned size_t will not exceed |buflen|. 70 virtual std::tuple<size_t, Errno> Write(int fd, const void* buf, 71 size_t buflen); 72 73 private: 74 const std::unique_ptr<RawOs> raw_os_; 75 76 DISALLOW_COPY_AND_ASSIGN(Os); 77}; 78 79} // namespace wifilogd 80} // namespace android 81 82#endif // OS_H_ 83