1 /* 2 * Copyright 2020 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 SURROUND_VIEW_SERVICE_IMPL_VHALHANDLER_H_ 18 #define SURROUND_VIEW_SERVICE_IMPL_VHALHANDLER_H_ 19 20 #include <mutex> 21 #include <thread> 22 #include <vector> 23 24 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h> 25 26 using android::sp; 27 28 namespace android { 29 namespace hardware { 30 namespace automotive { 31 namespace sv { 32 namespace V1_0 { 33 namespace implementation { 34 35 // Vhal handler cache vhal properties needed and updates them at a fixed rate. 36 class VhalHandler { 37 public: 38 // Enumeration for the method to use for updating the VHAL properties, 39 enum UpdateMethod { 40 // Makes a periodic get call in a polling thread. 41 // Use when VHAL implementation does not support multiple clients in subscribe calls. 42 GET = 0, 43 44 // Subscribes to the VHAL properties, to receive values periodically in a callback. 45 // Use when VHAL implementation support multiple clients in subscribe calls. 46 // NOTE: Currently not implemented. 47 SUBSCRIBE 48 }; 49 50 // Empty vhal handler constructor. VhalHandler()51 VhalHandler() : mIsInitialized(false), mUpdateMethod(GET), mRate(0), mIsUpdateActive(false) {} 52 53 // Initializes the VHAL handler. 54 // Valid range of rate is [1, 100] Hz. 55 // For subscribe the rate must be within each properties min and maximum sampling rate. 56 // For get, higher rate may result in excessive binder calls and increased latency. 57 bool initialize(UpdateMethod updateMethod, int rate); 58 59 // List of VHAL properties to read, can include vendor specific VHAL properties. 60 // The updated method determines if properties are updated using get or subscribe calls. 61 bool setPropertiesToRead(const std::vector<vehicle::V2_0::VehiclePropValue>& propertiesToRead); 62 63 // Convenience function to set vhal properties in a format returned from IO Module. 64 // uint64_t = (32 bits vhal property id) | (32 bits area id). 65 bool setPropertiesToRead(const std::vector<uint64_t>& propertiesToRead); 66 67 // Starts updating the VHAL properties with the specified rate. 68 bool startPropertiesUpdate(); 69 70 // Gets the last updated VHAL property values. 71 // property_values is empty if startPropertiesUpdate() has not been called. 72 bool getPropertyValues(std::vector<vehicle::V2_0::VehiclePropValue>* property_values); 73 74 // Stops updating the VHAL properties. 75 // For Get method, waits for the polling thread to exit. 76 bool stopPropertiesUpdate(); 77 78 private: 79 // Thread function to poll properties. 80 void pollProperties(); 81 82 // Pointer to VHAL service. 83 sp<vehicle::V2_0::IVehicle> mVhalServicePtr; 84 85 // Mutex for locking VHAL properties data. 86 std::mutex mAccessLock; 87 88 // Initialized parameters. 89 bool mIsInitialized; 90 UpdateMethod mUpdateMethod; 91 int mRate; 92 bool mIsUpdateActive; 93 94 // GET method related data members. 95 std::thread mPollingThread; 96 std::mutex mPollThreadSleepMutex; 97 std::condition_variable mPollThreadCondition; 98 bool mPollStopSleeping; 99 100 // List of properties to read. 101 std::vector<vehicle::V2_0::VehiclePropValue> mPropertiesToRead; 102 103 // Updated list of property values. 104 std::vector<vehicle::V2_0::VehiclePropValue> mPropertyValues; 105 }; 106 107 } // namespace implementation 108 } // namespace V1_0 109 } // namespace sv 110 } // namespace automotive 111 } // namespace hardware 112 } // namespace android 113 114 #endif // SURROUND_VIEW_SERVICE_IMPL_VHALHANDLER_H_ 115