Home | History | Annotate | Download | only in wifilogd
      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 <string>
     25 #include <tuple>
     26 #include <utility>
     27 
     28 #include "android-base/macros.h"
     29 
     30 #include "wifilogd/local_utils.h"
     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:
     44   using Errno = int;
     45 
     46   struct Timestamp {
     47     uint32_t secs;  // Sufficient through 2100.
     48     uint32_t nsecs;
     49   };
     50 
     51   static constexpr int kInvalidFd = -1;
     52   static constexpr auto kMaxNanos = 999'999'999;
     53 
     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 
     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 
     69   // Returns the current time, as reported by the clock with |clock_id|.
     70   virtual Timestamp GetTimestamp(clockid_t clock_id) const;
     71 
     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 
     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 
     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|.
     96   virtual std::tuple<size_t, Errno> Write(int fd, NONNULL const void* buf,
     97                                           size_t buflen);
     98 
     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_
    109