Home | History | Annotate | Download | only in hidl
      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 ANDROID_HIDL_INTERNAL_H
     18 #define ANDROID_HIDL_INTERNAL_H
     19 
     20 #include <cstdint>
     21 #include <dirent.h>
     22 #include <functional>
     23 #include <string>
     24 #include <vector>
     25 #include <utility>
     26 
     27 namespace android {
     28 namespace hardware {
     29 namespace details {
     30 
     31 // tag for pure interfaces (e.x. IFoo)
     32 struct i_tag {};
     33 
     34 // tag for server interfaces (e.x. BnHwFoo)
     35 struct bnhw_tag {};
     36 
     37 // tag for proxy interfaces (e.x. BpHwFoo)
     38 struct bphw_tag {};
     39 
     40 // tag for passthrough interfaces (e.x. BsFoo)
     41 struct bs_tag {};
     42 
     43 //Templated classes can use the below method
     44 //to avoid creating dependencies on liblog.
     45 void logAlwaysFatal(const char *message);
     46 
     47 // Returns vndk version from "ro.vndk.version" with '-' as a prefix.
     48 // If "ro.vndk.version" is not set or set to "current", it returns empty string.
     49 std::string getVndkVersionStr();
     50 
     51 // HIDL client/server code should *NOT* use this class.
     52 //
     53 // hidl_pointer wraps a pointer without taking ownership,
     54 // and stores it in a union with a uint64_t. This ensures
     55 // that we always have enough space to store a pointer,
     56 // regardless of whether we're running in a 32-bit or 64-bit
     57 // process.
     58 template<typename T>
     59 struct hidl_pointer {
     60     hidl_pointer()
     61         : _pad(0) {
     62     }
     63     hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
     64     hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
     65     hidl_pointer(hidl_pointer<T>&& other) : hidl_pointer() { *this = std::move(other); }
     66 
     67     hidl_pointer &operator=(const hidl_pointer<T>& other) {
     68         mPointer = other.mPointer;
     69         return *this;
     70     }
     71     hidl_pointer &operator=(hidl_pointer<T>&& other) {
     72         mPointer = other.mPointer;
     73         other.mPointer = nullptr;
     74         return *this;
     75     }
     76     hidl_pointer &operator=(T* ptr) {
     77         mPointer = ptr;
     78         return *this;
     79     }
     80 
     81     operator T*() const {
     82         return mPointer;
     83     }
     84     explicit operator void*() const { // requires explicit cast to avoid ambiguity
     85         return mPointer;
     86     }
     87     T& operator*() const {
     88         return *mPointer;
     89     }
     90     T* operator->() const {
     91         return mPointer;
     92     }
     93     T &operator[](size_t index) {
     94         return mPointer[index];
     95     }
     96     const T &operator[](size_t index) const {
     97         return mPointer[index];
     98     }
     99 
    100 private:
    101     union {
    102         T* mPointer;
    103         uint64_t _pad;
    104     };
    105 };
    106 
    107 #define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
    108 #define HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION "/system/lib64/vndk-sp%s/hw/"
    109 #define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
    110 #define HAL_LIBRARY_PATH_ODM_64BIT    "/odm/lib64/hw/"
    111 #define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
    112 #define HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION "/system/lib/vndk-sp%s/hw/"
    113 #define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
    114 #define HAL_LIBRARY_PATH_ODM_32BIT    "/odm/lib/hw/"
    115 
    116 #if defined(__LP64__)
    117 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
    118 #define HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION
    119 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
    120 #define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_64BIT
    121 #else
    122 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
    123 #define HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION
    124 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
    125 #define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_32BIT
    126 #endif
    127 
    128 // ----------------------------------------------------------------------
    129 // Class that provides Hidl instrumentation utilities.
    130 struct HidlInstrumentor {
    131     // Event that triggers the instrumentation. e.g. enter of an API call on
    132     // the server/client side, exit of an API call on the server/client side
    133     // etc.
    134     enum InstrumentationEvent {
    135         SERVER_API_ENTRY = 0,
    136         SERVER_API_EXIT,
    137         CLIENT_API_ENTRY,
    138         CLIENT_API_EXIT,
    139         SYNC_CALLBACK_ENTRY,
    140         SYNC_CALLBACK_EXIT,
    141         ASYNC_CALLBACK_ENTRY,
    142         ASYNC_CALLBACK_EXIT,
    143         PASSTHROUGH_ENTRY,
    144         PASSTHROUGH_EXIT,
    145     };
    146 
    147     // Signature of the instrumentation callback function.
    148     using InstrumentationCallback = std::function<void(
    149             const InstrumentationEvent event,
    150             const char *package,
    151             const char *version,
    152             const char *interface,
    153             const char *method,
    154             std::vector<void *> *args)>;
    155 
    156     explicit HidlInstrumentor(
    157             const std::string &package,
    158             const std::string &insterface);
    159     virtual ~HidlInstrumentor();
    160 
    161    public:
    162     const std::vector<InstrumentationCallback>& getInstrumentationCallbacks() {
    163         return mInstrumentationCallbacks;
    164     }
    165     bool isInstrumentationEnabled() { return mEnableInstrumentation; }
    166 
    167    protected:
    168     // Set mEnableInstrumentation based on system property
    169     // hal.instrumentation.enable, register/de-register instrumentation
    170     // callbacks if mEnableInstrumentation is true/false.
    171     void configureInstrumentation(bool log=true);
    172     // Function that lookup and dynamically loads the hidl instrumentation
    173     // libraries and registers the instrumentation callback functions.
    174     //
    175     // The instrumentation libraries should be stored under any of the following
    176     // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VNDK_SP,
    177     // HAL_LIBRARY_PATH_VENDOR and HAL_LIBRARY_PATH_ODM.
    178     // The name of instrumentation libraries should follow pattern:
    179     // ^profilerPrefix(.*).profiler.so$
    180     //
    181     // Each instrumentation library is expected to implement the instrumentation
    182     // function called HIDL_INSTRUMENTATION_FUNCTION.
    183     //
    184     // A no-op for user build.
    185     void registerInstrumentationCallbacks(
    186             std::vector<InstrumentationCallback> *instrumentationCallbacks);
    187 
    188     // Utility function to determine whether a give file is a instrumentation
    189     // library (i.e. the file name follow the expected pattern).
    190     bool isInstrumentationLib(const dirent *file);
    191 
    192     // A list of registered instrumentation callbacks.
    193     std::vector<InstrumentationCallback> mInstrumentationCallbacks;
    194     // Flag whether to enable instrumentation.
    195     bool mEnableInstrumentation;
    196     // Prefix to lookup the instrumentation libraries.
    197     std::string mInstrumentationLibPackage;
    198     // Used for dlsym to load the profiling method for given interface.
    199     std::string mInterfaceName;
    200 
    201 };
    202 
    203 }  // namespace details
    204 }  // namespace hardware
    205 }  // namespace android
    206 
    207 #endif  // ANDROID_HIDL_INTERNAL_H
    208