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 #include "FrameHandlerUltrasonics.h"
18 
19 #include <android-base/logging.h>
20 #include <hidlmemory/mapping.h>
21 #include <android/hidl/memory/1.0/IMemory.h>
22 
23 using ::android::hidl::memory::V1_0::IMemory;
24 using ::android::hardware::Return;
25 using ::android::sp;
26 
27 using ::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArrayStream;
28 using ::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArray;
29 using ::android::hardware::automotive::evs::V1_1::UltrasonicsDataFrameDesc;
30 using ::android::hardware::automotive::evs::V1_1::EvsEventDesc;
31 using ::android::hardware::automotive::evs::V1_1::EvsEventType;
32 
FrameHandlerUltrasonics(sp<IEvsUltrasonicsArray> pEvsUltrasonicsArray)33 FrameHandlerUltrasonics::FrameHandlerUltrasonics(sp<IEvsUltrasonicsArray> pEvsUltrasonicsArray) :
34     mEvsUltrasonicsArray(pEvsUltrasonicsArray), mReceiveFramesCount(0) {
35     // Nothing but member initialization
36 }
37 
notify(const EvsEventDesc & evsEvent)38 Return<void> FrameHandlerUltrasonics::notify(const EvsEventDesc& evsEvent) {
39     switch (evsEvent.aType) {
40         case EvsEventType::STREAM_STARTED:
41         case EvsEventType::STREAM_STOPPED:
42         case EvsEventType::FRAME_DROPPED:
43         case EvsEventType::TIMEOUT:
44             mReceivedEvents.emplace_back(evsEvent);
45             break;
46         default:
47             LOG(ERROR) << "Received unexpected event";
48     }
49 
50     return android::hardware::Void();
51 }
52 
53 // Struct used by SerializeWaveformData().
54 struct WaveformData {
55     uint8_t receiverId;
56     std::vector<std::pair<float, float>> readings;
57 };
58 
59 // De-serializes shared memory to vector of WaveformData.
60 // TODO(b/149950362): Add a common library for serialiazing and deserializing waveform data.
DeSerializeWaveformData(std::vector<uint32_t> recvReadingsCountList,uint8_t * pData)61 std::vector<WaveformData> DeSerializeWaveformData(std::vector<uint32_t> recvReadingsCountList,
62         uint8_t* pData) {
63     std::vector<WaveformData> waveformDataList(recvReadingsCountList.size());
64 
65     for (int i = 0; i < waveformDataList.size(); i++) {
66         // Set Id
67         memcpy(&waveformDataList[i].receiverId, pData, sizeof(uint8_t));
68         pData += sizeof(uint8_t);
69 
70         waveformDataList[i].readings.resize(recvReadingsCountList[i]);
71 
72         for (auto& reading : waveformDataList[i].readings) {
73             // Set the time of flight.
74             memcpy(&reading.first, pData, sizeof(float));
75             pData += sizeof(float);
76 
77             // Set the resonance.
78             memcpy(&reading.second, pData, sizeof(float));
79             pData += sizeof(float);
80         }
81     }
82     return waveformDataList;
83 }
84 
DataFrameValidator(const UltrasonicsDataFrameDesc & dataFrameDesc)85 bool DataFrameValidator(const UltrasonicsDataFrameDesc& dataFrameDesc) {
86 
87     if (dataFrameDesc.receiversIdList.size() != dataFrameDesc.receiversReadingsCountList.size()) {
88         LOG(ERROR) << "Size mismatch of receiversIdList and receiversReadingsCountList";
89         return false;
90     }
91 
92     if(!dataFrameDesc.waveformsData.valid()) {
93         LOG(ERROR) << "Data frame does not valid hidl memory";
94         return false;
95     }
96 
97     // Check total bytes from dataFrameDesc are within the shared memory size.
98     int totalWaveformDataBytesSize = 0;
99     for (int i = 0; i < dataFrameDesc.receiversIdList.size(); i++) {
100         totalWaveformDataBytesSize = 1 + (4 * 2 * dataFrameDesc.receiversReadingsCountList[i]);
101     }
102     if (totalWaveformDataBytesSize > dataFrameDesc.waveformsData.size()) {
103         LOG(ERROR) << "Total waveform data bytes in desc exceed shared memory size";
104         return false;
105     }
106 
107     sp<IMemory> pIMemory = mapMemory(dataFrameDesc.waveformsData);
108     if(pIMemory.get() == nullptr) {
109         LOG(ERROR) << "Failed to map hidl memory";
110         return false;
111     }
112 
113     uint8_t* pData = (uint8_t*)((void*)pIMemory->getPointer());
114     if(pData == nullptr) {
115         LOG(ERROR) << "Failed getPointer from mapped shared memory";
116         return false;
117     }
118 
119     const std::vector<WaveformData> waveformDataList = DeSerializeWaveformData(
120             dataFrameDesc.receiversReadingsCountList, pData);
121 
122     // Verify the waveforms data.
123     for(int i = 0; i < waveformDataList.size(); i++) {
124         if (waveformDataList[i].receiverId != dataFrameDesc.receiversIdList[i]) {
125             LOG(ERROR) << "Receiver Id mismatch";
126             return false;
127         }
128         for(auto& reading : waveformDataList[i].readings) {
129             if (reading.second < 0.0f || reading.second > 1.0f) {
130                 LOG(ERROR) << "Resonance reading is not in range [0, 1]";
131                 return false;
132             }
133         }
134     }
135     return true;
136 }
137 
deliverDataFrame(const UltrasonicsDataFrameDesc & dataFrameDesc)138 Return<void> FrameHandlerUltrasonics::deliverDataFrame(
139         const UltrasonicsDataFrameDesc& dataFrameDesc) {
140     LOG(DEBUG) << "FrameHandlerUltrasonics::receiveFrames";
141 
142     mReceiveFramesCount++;
143     mLastReceivedFrames = dataFrameDesc;
144 
145     if(!DataFrameValidator(dataFrameDesc)) {
146         mAllFramesValid = false;
147     }
148 
149     // Send done with data frame.
150     mEvsUltrasonicsArray->doneWithDataFrame(dataFrameDesc);
151 
152     return android::hardware::Void();
153 }
154 
checkEventReceived(EvsEventDesc evsEvent)155 bool FrameHandlerUltrasonics::checkEventReceived(EvsEventDesc evsEvent) {
156     LOG(DEBUG) << "FrameHandlerUltrasonics::checkEventReceived";
157     int size = mReceivedEvents.size(); // work around
158     LOG(DEBUG) << "Received event number: " << size;
159     auto iter = find(mReceivedEvents.begin(), mReceivedEvents.end(), evsEvent);
160     return iter != mReceivedEvents.end();
161 }
162 
getReceiveFramesCount()163 int FrameHandlerUltrasonics::getReceiveFramesCount() {
164     return mReceiveFramesCount;
165 }
166 
areAllFramesValid()167 bool FrameHandlerUltrasonics::areAllFramesValid() {
168     return mAllFramesValid;
169 }
170