Changyeon Jo | 8018901 | 2021-10-10 16:34:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | #include "FrameHandlerUltrasonics.h" |
| 18 | |
| 19 | #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h> |
| 20 | #include <aidl/android/hardware/automotive/evs/EvsEventType.h> |
| 21 | #include <aidl/android/hardware/automotive/evs/IEvsUltrasonicsArray.h> |
| 22 | #include <aidl/android/hardware/automotive/evs/UltrasonicsDataFrameDesc.h> |
| 23 | #include <android-base/logging.h> |
| 24 | |
| 25 | using ::aidl::android::hardware::automotive::evs::EvsEventDesc; |
| 26 | using ::aidl::android::hardware::automotive::evs::EvsEventType; |
| 27 | using ::aidl::android::hardware::automotive::evs::IEvsUltrasonicsArray; |
| 28 | using ::aidl::android::hardware::automotive::evs::UltrasonicsDataFrameDesc; |
| 29 | using ::ndk::ScopedAStatus; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // Struct used by SerializeWaveformData(). |
| 34 | struct WaveformData { |
| 35 | uint8_t receiverId; |
| 36 | std::vector<std::pair<float, float>> readings; |
| 37 | }; |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | FrameHandlerUltrasonics::FrameHandlerUltrasonics( |
| 42 | const std::shared_ptr<IEvsUltrasonicsArray>& pArray) |
| 43 | : mEvsUltrasonicsArray(pArray), mReceiveFramesCount(0) { |
| 44 | // Nothing but member initialization |
| 45 | } |
| 46 | |
| 47 | ScopedAStatus FrameHandlerUltrasonics::notify(const EvsEventDesc& evsEvent) { |
| 48 | switch (evsEvent.aType) { |
| 49 | case EvsEventType::STREAM_STARTED: |
| 50 | case EvsEventType::STREAM_STOPPED: |
| 51 | case EvsEventType::FRAME_DROPPED: |
| 52 | case EvsEventType::TIMEOUT: |
| 53 | mReceivedEvents.emplace_back(evsEvent); |
| 54 | break; |
| 55 | default: |
| 56 | LOG(ERROR) << "Received unexpected event"; |
| 57 | } |
| 58 | |
| 59 | return ScopedAStatus::ok(); |
| 60 | } |
| 61 | |
| 62 | // De-serializes shared memory to vector of WaveformData. |
| 63 | // TODO(b/149950362): Add a common library for serializing and deserializing waveform data. |
| 64 | std::vector<WaveformData> DeSerializeWaveformData(std::vector<uint32_t> recvReadingsCountList, |
| 65 | uint8_t* pData) { |
| 66 | std::vector<WaveformData> waveformDataList(recvReadingsCountList.size()); |
| 67 | |
| 68 | for (int i = 0; i < waveformDataList.size(); i++) { |
| 69 | // Set Id |
| 70 | memcpy(&waveformDataList[i].receiverId, pData, sizeof(uint8_t)); |
| 71 | pData += sizeof(uint8_t); |
| 72 | |
| 73 | waveformDataList[i].readings.resize(recvReadingsCountList[i]); |
| 74 | |
| 75 | for (auto& reading : waveformDataList[i].readings) { |
| 76 | // Set the time of flight. |
| 77 | memcpy(&reading.first, pData, sizeof(float)); |
| 78 | pData += sizeof(float); |
| 79 | |
| 80 | // Set the resonance. |
| 81 | memcpy(&reading.second, pData, sizeof(float)); |
| 82 | pData += sizeof(float); |
| 83 | } |
| 84 | } |
| 85 | return waveformDataList; |
| 86 | } |
| 87 | |
| 88 | bool DataFrameValidator(const UltrasonicsDataFrameDesc& /*dataFrameDesc*/) { |
| 89 | // TODO(b/214026378): implement a method to validate an ultrasonics data frame |
| 90 | (void)DeSerializeWaveformData; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | ScopedAStatus FrameHandlerUltrasonics::deliverDataFrame( |
| 95 | const UltrasonicsDataFrameDesc& dataFrameDesc) { |
| 96 | LOG(DEBUG) << "FrameHandlerUltrasonics::receiveFrames"; |
| 97 | |
| 98 | mReceiveFramesCount++; |
| 99 | |
| 100 | if (!DataFrameValidator(dataFrameDesc)) { |
| 101 | mAllFramesValid = false; |
| 102 | } |
| 103 | |
| 104 | // Send done with data frame. |
| 105 | mEvsUltrasonicsArray->doneWithDataFrame(dataFrameDesc); |
| 106 | return ScopedAStatus::ok(); |
| 107 | } |
| 108 | |
| 109 | bool FrameHandlerUltrasonics::checkEventReceived(const EvsEventDesc& evsEvent) { |
| 110 | LOG(DEBUG) << "FrameHandlerUltrasonics::checkEventReceived"; |
| 111 | int size = mReceivedEvents.size(); // work around |
| 112 | LOG(DEBUG) << "Received event number: " << size; |
| 113 | auto iter = find(mReceivedEvents.begin(), mReceivedEvents.end(), evsEvent); |
| 114 | return iter != mReceivedEvents.end(); |
| 115 | } |
| 116 | |
| 117 | int FrameHandlerUltrasonics::getReceiveFramesCount() { |
| 118 | return mReceiveFramesCount; |
| 119 | } |
| 120 | |
| 121 | bool FrameHandlerUltrasonics::areAllFramesValid() { |
| 122 | return mAllFramesValid; |
| 123 | } |