Tanmay Patil | b97cceb | 2020-02-07 16:48:39 -0800 | [diff] [blame] | 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 ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSULTRASONICSARRAY_H |
| 18 | #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSULTRASONICSARRAY_H |
| 19 | |
| 20 | #include <thread> |
| 21 | #include <utility> |
| 22 | |
| 23 | #include <android-base/macros.h> |
| 24 | #include <android/hidl/allocator/1.0/IAllocator.h> |
| 25 | #include <android/hidl/memory/1.0/IMemory.h> |
| 26 | #include <utils/threads.h> |
| 27 | |
| 28 | #include <android/hardware/automotive/evs/1.1/IEvsUltrasonicsArray.h> |
| 29 | #include <android/hardware/automotive/evs/1.1/IEvsUltrasonicsArrayStream.h> |
| 30 | #include <android/hardware/automotive/evs/1.1/types.h> |
| 31 | |
| 32 | using ::android::hardware::hidl_memory; |
| 33 | using ::android::hardware::automotive::evs::V1_0::EvsResult; |
| 34 | using ::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArray; |
| 35 | using ::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArrayStream; |
| 36 | using ::android::hardware::automotive::evs::V1_1::UltrasonicsArrayDesc; |
| 37 | using ::android::hardware::automotive::evs::V1_1::UltrasonicsDataFrameDesc; |
| 38 | using ::android::hidl::allocator::V1_0::IAllocator; |
| 39 | using ::android::hidl::memory::V1_0::IMemory; |
| 40 | |
| 41 | namespace android { |
| 42 | namespace hardware { |
| 43 | namespace automotive { |
| 44 | namespace evs { |
| 45 | namespace V1_1 { |
| 46 | namespace implementation { |
| 47 | |
| 48 | class EvsUltrasonicsArray : public IEvsUltrasonicsArray { |
| 49 | public: |
| 50 | // Methods from ::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArray follow. |
| 51 | Return<void> getUltrasonicArrayInfo(getUltrasonicArrayInfo_cb _get_info_cb) override; |
| 52 | Return<EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override; |
| 53 | Return<void> doneWithDataFrame(const UltrasonicsDataFrameDesc& dataFrameDesc) override; |
| 54 | Return<EvsResult> startStream(const ::android::sp<IEvsUltrasonicsArrayStream>& stream) override; |
| 55 | Return<void> stopStream() override; |
| 56 | |
| 57 | // Factory function to create a array. |
| 58 | static sp<EvsUltrasonicsArray> Create(const char* deviceName); |
| 59 | |
| 60 | // Returns a ultrasonics array descriptor filled with sample data. |
Changyeon Jo | cb877f9 | 2020-08-06 13:14:43 -0700 | [diff] [blame^] | 61 | static UltrasonicsArrayDesc GetMockArrayDesc(const char* id); |
Tanmay Patil | b97cceb | 2020-02-07 16:48:39 -0800 | [diff] [blame] | 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(EvsUltrasonicsArray); |
| 64 | virtual ~EvsUltrasonicsArray() override; |
| 65 | void forceShutdown(); // This gets called if another caller "steals" ownership |
| 66 | |
| 67 | private: |
| 68 | // Structure holding the hidl memory struct and the interface to a shared memory. |
| 69 | struct SharedMemory { |
| 70 | hidl_memory hidlMemory; |
| 71 | sp<IMemory> pIMemory; |
| 72 | |
| 73 | SharedMemory() : hidlMemory(hidl_memory()), pIMemory(nullptr){}; |
| 74 | |
| 75 | SharedMemory(hidl_memory hidlMem, sp<IMemory> pIMem) |
| 76 | : hidlMemory(hidlMem), pIMemory(pIMem) {} |
| 77 | |
| 78 | bool IsValid() { return (pIMemory.get() != nullptr && hidlMemory.valid()); } |
| 79 | |
| 80 | void clear() { |
| 81 | hidlMemory = hidl_memory(); |
| 82 | pIMemory.clear(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | // Struct for a data frame record. |
| 87 | struct DataFrameRecord { |
| 88 | SharedMemory sharedMemory; |
| 89 | bool inUse; |
| 90 | explicit DataFrameRecord(SharedMemory shMem) : sharedMemory(shMem), inUse(false){}; |
| 91 | }; |
| 92 | |
| 93 | enum StreamStateValues { |
| 94 | STOPPED, |
| 95 | RUNNING, |
| 96 | STOPPING, |
| 97 | DEAD, |
| 98 | }; |
| 99 | |
| 100 | EvsUltrasonicsArray(const char* deviceName); |
| 101 | |
| 102 | // These three functions are expected to be called while mAccessLock is held |
| 103 | bool setAvailableFrames_Locked(unsigned bufferCount); |
| 104 | unsigned increaseAvailableFrames_Locked(unsigned numToAdd); |
| 105 | unsigned decreaseAvailableFrames_Locked(unsigned numToRemove); |
| 106 | |
| 107 | void generateDataFrames(); |
| 108 | |
| 109 | SharedMemory allocateAndMapSharedMemory(); |
| 110 | |
| 111 | UltrasonicsArrayDesc mArrayDesc = {}; // The properties of this ultrasonic array. |
| 112 | |
| 113 | std::thread mCaptureThread; // The thread we'll use to synthesize frames |
| 114 | |
| 115 | sp<IEvsUltrasonicsArrayStream> mStream = nullptr; // The callback used to deliver each frame |
| 116 | |
| 117 | sp<IAllocator> mShmemAllocator = nullptr; // Shared memory allocator. |
| 118 | |
| 119 | std::mutex mAccessLock; |
| 120 | std::vector<DataFrameRecord> mDataFrames GUARDED_BY(mAccessLock); // Shared memory buffers. |
| 121 | unsigned mFramesAllowed GUARDED_BY(mAccessLock); // How many buffers are we currently using. |
| 122 | unsigned mFramesInUse GUARDED_BY(mAccessLock); // How many buffers are currently outstanding. |
| 123 | |
| 124 | StreamStateValues mStreamState GUARDED_BY(mAccessLock); |
| 125 | }; |
| 126 | |
| 127 | } // namespace implementation |
| 128 | } // namespace V1_1 |
| 129 | } // namespace evs |
| 130 | } // namespace automotive |
| 131 | } // namespace hardware |
| 132 | } // namespace android |
| 133 | |
| 134 | #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSULTRASONICSARRAY_H |