1 /*
2  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials provided
12  *       with the distribution.
13  *     * Neither the name of The Linux Foundation nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #define LOG_TAG "LocSvc_GnssAntennaInfoInterface"
30 
31 #include <log_util.h>
32 #include "Gnss.h"
33 #include "GnssAntennaInfo.h"
34 #include <android/hardware/gnss/1.0/types.h>
35 
36 namespace android {
37 namespace hardware {
38 namespace gnss {
39 namespace V2_1 {
40 namespace implementation {
41 
42 static GnssAntennaInfo* spGnssAntennaInfo = nullptr;
43 
44 static void convertGnssAntennaInfo(std::vector<GnssAntennaInformation>& in,
45         hidl_vec<IGnssAntennaInfoCallback::GnssAntennaInfo>& antennaInfos);
46 
serviceDied(uint64_t cookie,const wp<IBase> & who)47 void GnssAntennaInfo::GnssAntennaInfoDeathRecipient::serviceDied(uint64_t cookie,
48                                                                  const wp<IBase>& who) {
49     LOC_LOGE("%s] service died. cookie: %llu, who: %p",
50             __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
51     // we do nothing here
52     // Gnss::GnssDeathRecipient will stop the session
53     // However, we need to inform the adapter that the service has died
54     if (nullptr == spGnssAntennaInfo) {
55         LOC_LOGE("%s]: spGnssAntennaInfo is nullptr", __FUNCTION__);
56         return;
57     }
58     if (nullptr == spGnssAntennaInfo->mGnss) {
59         LOC_LOGE("%s]: spGnssAntennaInfo->mGnss is nullptr", __FUNCTION__);
60         return;
61     }
62 
63     spGnssAntennaInfo->mGnss->getGnssInterface()->antennaInfoClose();
64 }
65 
convertGnssAntennaInfo(std::vector<GnssAntennaInformation> & in,hidl_vec<IGnssAntennaInfoCallback::GnssAntennaInfo> & out)66 static void convertGnssAntennaInfo(std::vector<GnssAntennaInformation>& in,
67         hidl_vec<IGnssAntennaInfoCallback::GnssAntennaInfo>& out) {
68 
69     uint32_t vecSize, numberOfRows, numberOfColumns;
70     vecSize = in.size();
71     out.resize(vecSize);
72     for (uint32_t i = 0; i < vecSize; i++) {
73         out[i].carrierFrequencyMHz = in[i].carrierFrequencyMHz;
74         out[i].phaseCenterOffsetCoordinateMillimeters.x =
75                 in[i].phaseCenterOffsetCoordinateMillimeters.x;
76         out[i].phaseCenterOffsetCoordinateMillimeters.xUncertainty =
77                 in[i].phaseCenterOffsetCoordinateMillimeters.xUncertainty;
78         out[i].phaseCenterOffsetCoordinateMillimeters.y =
79                 in[i].phaseCenterOffsetCoordinateMillimeters.y;
80         out[i].phaseCenterOffsetCoordinateMillimeters.yUncertainty =
81                 in[i].phaseCenterOffsetCoordinateMillimeters.yUncertainty;
82         out[i].phaseCenterOffsetCoordinateMillimeters.z =
83                 in[i].phaseCenterOffsetCoordinateMillimeters.z;
84         out[i].phaseCenterOffsetCoordinateMillimeters.zUncertainty =
85                 in[i].phaseCenterOffsetCoordinateMillimeters.zUncertainty;
86 
87         numberOfRows = in[i].phaseCenterVariationCorrectionMillimeters.size();
88         out[i].phaseCenterVariationCorrectionMillimeters.resize(numberOfRows);
89         for (uint32_t j = 0; j < numberOfRows; j++) {
90             numberOfColumns = in[i].phaseCenterVariationCorrectionMillimeters[j].size();
91             out[i].phaseCenterVariationCorrectionMillimeters[j].row.resize(numberOfColumns);
92             for (uint32_t k = 0; k < numberOfColumns; k++) {
93                 out[i].phaseCenterVariationCorrectionMillimeters[j].row[k] =
94                         in[i].phaseCenterVariationCorrectionMillimeters[j][k];
95             }
96         }
97 
98         numberOfRows = in[i].phaseCenterVariationCorrectionUncertaintyMillimeters.size();
99         out[i].phaseCenterVariationCorrectionUncertaintyMillimeters.resize(numberOfRows);
100         for (uint32_t j = 0; j < numberOfRows; j++) {
101             numberOfColumns = in[i].phaseCenterVariationCorrectionUncertaintyMillimeters[j].size();
102             out[i].phaseCenterVariationCorrectionUncertaintyMillimeters[j].
103                     row.resize(numberOfColumns);
104             for (uint32_t k = 0; k < numberOfColumns; k++) {
105                 out[i].phaseCenterVariationCorrectionUncertaintyMillimeters[j].row[k] =
106                         in[i].phaseCenterVariationCorrectionUncertaintyMillimeters[j][k];
107             }
108         }
109 
110         numberOfRows = in[i].signalGainCorrectionDbi.size();
111         out[i].signalGainCorrectionDbi.resize(numberOfRows);
112         for (uint32_t j = 0; j < numberOfRows; j++) {
113             numberOfColumns = in[i].signalGainCorrectionDbi[j].size();
114             out[i].signalGainCorrectionDbi[j].row.resize(numberOfColumns);
115             for (uint32_t k = 0; k < numberOfColumns; k++) {
116                 out[i].signalGainCorrectionDbi[j].row[k] = in[i].signalGainCorrectionDbi[j][k];
117             }
118         }
119 
120         numberOfRows = in[i].signalGainCorrectionUncertaintyDbi.size();
121         out[i].signalGainCorrectionUncertaintyDbi.resize(numberOfRows);
122         for (uint32_t j = 0; j < numberOfRows; j++) {
123             numberOfColumns = in[i].signalGainCorrectionUncertaintyDbi[j].size();
124             out[i].signalGainCorrectionUncertaintyDbi[j].row.resize(numberOfColumns);
125             for (uint32_t k = 0; k < numberOfColumns; k++) {
126                 out[i].signalGainCorrectionUncertaintyDbi[j].row[k] =
127                         in[i].signalGainCorrectionUncertaintyDbi[j][k];
128             }
129         }
130     }
131 }
132 
GnssAntennaInfo(Gnss * gnss)133 GnssAntennaInfo::GnssAntennaInfo(Gnss* gnss) : mGnss(gnss) {
134     mGnssAntennaInfoDeathRecipient = new GnssAntennaInfoDeathRecipient(this);
135     spGnssAntennaInfo = this;
136 }
137 
~GnssAntennaInfo()138 GnssAntennaInfo::~GnssAntennaInfo() {
139     spGnssAntennaInfo = nullptr;
140 }
141 
142 // Methods from ::android::hardware::gnss::V2_1::IGnssAntennaInfo follow.
143 Return<GnssAntennaInfo::GnssAntennaInfoStatus>
setCallback(const sp<IGnssAntennaInfoCallback> & callback)144         GnssAntennaInfo::setCallback(const sp<IGnssAntennaInfoCallback>& callback)  {
145     uint32_t retValue;
146     if (mGnss == nullptr) {
147         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
148         return GnssAntennaInfoStatus::ERROR_GENERIC;
149     }
150 
151     mGnssAntennaInfoCbIface = callback;
152     retValue = mGnss->getGnssInterface()->antennaInfoInit(aiGnssAntennaInfoCb);
153 
154     switch (retValue) {
155     case ANTENNA_INFO_SUCCESS: return GnssAntennaInfoStatus::SUCCESS;
156     case ANTENNA_INFO_ERROR_ALREADY_INIT: return GnssAntennaInfoStatus::ERROR_ALREADY_INIT;
157     case ANTENNA_INFO_ERROR_GENERIC:
158     default: return GnssAntennaInfoStatus::ERROR_GENERIC;
159     }
160 }
161 
close(void)162 Return<void> GnssAntennaInfo::close(void)  {
163     if (mGnss == nullptr) {
164         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
165         return Void();
166     }
167 
168     mGnss->getGnssInterface()->antennaInfoClose();
169 
170     return Void();
171 }
172 
aiGnssAntennaInfoCb(std::vector<GnssAntennaInformation> gnssAntennaInformations)173 void GnssAntennaInfo::aiGnssAntennaInfoCb
174         (std::vector<GnssAntennaInformation> gnssAntennaInformations) {
175     if (nullptr != spGnssAntennaInfo) {
176         spGnssAntennaInfo->gnssAntennaInfoCb(gnssAntennaInformations);
177     }
178 }
179 
gnssAntennaInfoCb(std::vector<GnssAntennaInformation> gnssAntennaInformations)180 void GnssAntennaInfo::gnssAntennaInfoCb
181         (std::vector<GnssAntennaInformation> gnssAntennaInformations) {
182 
183     if (mGnssAntennaInfoCbIface != nullptr) {
184         hidl_vec<IGnssAntennaInfoCallback::GnssAntennaInfo> antennaInfos;
185 
186         // Convert from one structure to another
187         convertGnssAntennaInfo(gnssAntennaInformations, antennaInfos);
188 
189         auto r = mGnssAntennaInfoCbIface->gnssAntennaInfoCb(antennaInfos);
190         if (!r.isOk()) {
191             LOC_LOGw("Error antenna info cb %s", r.description().c_str());
192         }
193     } else {
194         LOC_LOGw("setCallback has not been called yet");
195     }
196 }
197 
198 }  // namespace implementation
199 }  // namespace V2_1
200 }  // namespace gnss
201 }  // namespace hardware
202 }  // namespace android
203