LocAdapterBase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* Copyright (c) 2011-2014, 2016-2018, 2020-2021 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. /*
  30. Changes from Qualcomm Innovation Center are provided under the following license:
  31. Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  32. Redistribution and use in source and binary forms, with or without
  33. modification, are permitted (subject to the limitations in the
  34. disclaimer below) provided that the following conditions are met:
  35. * Redistributions of source code must retain the above copyright
  36. notice, this list of conditions and the following disclaimer.
  37. * Redistributions in binary form must reproduce the above
  38. copyright notice, this list of conditions and the following
  39. disclaimer in the documentation and/or other materials provided
  40. with the distribution.
  41. * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  42. contributors may be used to endorse or promote products derived
  43. from this software without specific prior written permission.
  44. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  45. GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  46. HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  47. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  48. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  49. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  50. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  52. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  53. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  54. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  55. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  56. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. */
  58. #define LOG_NDEBUG 0
  59. #define LOG_TAG "LocSvc_LocAdapterBase"
  60. #include <dlfcn.h>
  61. #include <LocAdapterBase.h>
  62. #include <loc_target.h>
  63. #include <log_util.h>
  64. #include <LocAdapterProxyBase.h>
  65. namespace loc_core {
  66. // This is the top level class, so the constructor will
  67. // always gets called. Here we prepare for the default.
  68. // But if getLocApi(targetEnumType target) is overriden,
  69. // the right locApi should get created.
  70. LocAdapterBase::LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,
  71. ContextBase* context, bool isMaster,
  72. LocAdapterProxyBase *adapterProxyBase,
  73. bool waitForDoneInit) :
  74. mIsMaster(isMaster),
  75. mIsEngineCapabilitiesKnown(ContextBase::sIsEngineCapabilitiesKnown),
  76. mEvtMask(mask), mContext(context), mLocApi(context->getLocApi()),
  77. mLocAdapterProxyBase(adapterProxyBase), mMsgTask(context->getMsgTask())
  78. {
  79. LOC_LOGd("waitForDoneInit: %d", waitForDoneInit);
  80. if (!waitForDoneInit) {
  81. mLocApi->addAdapter(this);
  82. mAdapterAdded = true;
  83. } else {
  84. mAdapterAdded = false;
  85. }
  86. }
  87. uint32_t LocAdapterBase::mSessionIdCounter(1);
  88. uint32_t LocAdapterBase::generateSessionId()
  89. {
  90. if (++mSessionIdCounter == 0xFFFFFFFF)
  91. mSessionIdCounter = 1;
  92. return mSessionIdCounter;
  93. }
  94. void LocAdapterBase::handleEngineUpEvent()
  95. {
  96. if (mLocAdapterProxyBase) {
  97. mLocAdapterProxyBase->handleEngineUpEvent();
  98. }
  99. }
  100. void LocAdapterBase::handleEngineDownEvent()
  101. {
  102. if (mLocAdapterProxyBase) {
  103. mLocAdapterProxyBase->handleEngineDownEvent();
  104. }
  105. }
  106. void LocAdapterBase::
  107. reportPositionEvent(const UlpLocation& location,
  108. const GpsLocationExtended& locationExtended,
  109. enum loc_sess_status status,
  110. LocPosTechMask loc_technology_mask,
  111. GnssDataNotification* pDataNotify __unused,
  112. int msInWeek __unused)
  113. {
  114. if (mLocAdapterProxyBase != NULL) {
  115. mLocAdapterProxyBase->reportPositionEvent((UlpLocation&)location,
  116. (GpsLocationExtended&)locationExtended,
  117. status,
  118. loc_technology_mask);
  119. } else {
  120. DEFAULT_IMPL()
  121. }
  122. }
  123. void LocAdapterBase::
  124. reportSvEvent(const GnssSvNotification& /*svNotify*/)
  125. DEFAULT_IMPL()
  126. void LocAdapterBase::
  127. reportSvPolynomialEvent(GnssSvPolynomial &/*svPolynomial*/)
  128. DEFAULT_IMPL()
  129. void LocAdapterBase::
  130. reportSvEphemerisEvent(GnssSvEphemerisReport &/*svEphemeris*/)
  131. DEFAULT_IMPL()
  132. void LocAdapterBase::
  133. reportStatus(LocGpsStatusValue /*status*/)
  134. DEFAULT_IMPL()
  135. void LocAdapterBase::
  136. reportNmeaEvent(const char* /*nmea*/, size_t /*length*/)
  137. DEFAULT_IMPL()
  138. void LocAdapterBase::
  139. reportDataEvent(const GnssDataNotification& /*dataNotify*/,
  140. int /*msInWeek*/)
  141. DEFAULT_IMPL()
  142. bool LocAdapterBase::
  143. reportXtraServer(const char* /*url1*/, const char* /*url2*/,
  144. const char* /*url3*/, const int /*maxlength*/)
  145. DEFAULT_IMPL(false)
  146. void LocAdapterBase::
  147. reportLocationSystemInfoEvent(const LocationSystemInfo& /*locationSystemInfo*/)
  148. DEFAULT_IMPL()
  149. bool LocAdapterBase::
  150. requestXtraData()
  151. DEFAULT_IMPL(false)
  152. bool LocAdapterBase::
  153. requestTime()
  154. DEFAULT_IMPL(false)
  155. bool LocAdapterBase::
  156. requestLocation()
  157. DEFAULT_IMPL(false)
  158. bool LocAdapterBase::
  159. requestATL(int /*connHandle*/, LocAGpsType /*agps_type*/,
  160. LocApnTypeMask /*apn_type_mask*/, LocSubId /*sub_id*/)
  161. DEFAULT_IMPL(false)
  162. bool LocAdapterBase::
  163. releaseATL(int /*connHandle*/)
  164. DEFAULT_IMPL(false)
  165. bool LocAdapterBase::
  166. requestNiNotifyEvent(const GnssNiNotification &/*notify*/,
  167. const void* /*data*/,
  168. const LocInEmergency /*emergencyState*/)
  169. DEFAULT_IMPL(false)
  170. void LocAdapterBase::
  171. reportGnssMeasurementsEvent(const GnssMeasurements& /*gnssMeasurements*/,
  172. int /*msInWeek*/)
  173. DEFAULT_IMPL()
  174. bool LocAdapterBase::
  175. reportWwanZppFix(LocGpsLocation &/*zppLoc*/)
  176. DEFAULT_IMPL(false)
  177. bool LocAdapterBase::
  178. reportZppBestAvailableFix(LocGpsLocation& /*zppLoc*/,
  179. GpsLocationExtended& /*location_extended*/, LocPosTechMask /*tech_mask*/)
  180. DEFAULT_IMPL(false)
  181. void LocAdapterBase::reportGnssSvIdConfigEvent(const GnssSvIdConfig& /*config*/)
  182. DEFAULT_IMPL()
  183. void LocAdapterBase::reportGnssSvTypeConfigEvent(const GnssSvTypeConfig& /*config*/)
  184. DEFAULT_IMPL()
  185. void LocAdapterBase::reportGnssConfigEvent(uint32_t, /* session id*/
  186. const GnssConfig& /*gnssConfig*/)
  187. DEFAULT_IMPL()
  188. bool LocAdapterBase::
  189. requestOdcpiEvent(OdcpiRequestInfo& /*request*/)
  190. DEFAULT_IMPL(false)
  191. bool LocAdapterBase::
  192. reportGnssEngEnergyConsumedEvent(uint64_t /*energyConsumedSinceFirstBoot*/)
  193. DEFAULT_IMPL(false)
  194. bool LocAdapterBase::
  195. reportDeleteAidingDataEvent(GnssAidingData & /*aidingData*/)
  196. DEFAULT_IMPL(false)
  197. bool LocAdapterBase::
  198. reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& /*ionoModel*/)
  199. DEFAULT_IMPL(false)
  200. bool LocAdapterBase::
  201. reportGnssAdditionalSystemInfoEvent(GnssAdditionalSystemInfo& /*additionalSystemInfo*/)
  202. DEFAULT_IMPL(false)
  203. void LocAdapterBase::
  204. reportNfwNotificationEvent(GnssNfwNotification& /*notification*/)
  205. DEFAULT_IMPL()
  206. void
  207. LocAdapterBase::geofenceBreachEvent(size_t /*count*/, uint32_t* /*hwIds*/, Location& /*location*/,
  208. GeofenceBreachType /*breachType*/, uint64_t /*timestamp*/)
  209. DEFAULT_IMPL()
  210. void
  211. LocAdapterBase::geofenceStatusEvent(GeofenceStatusAvailable /*available*/)
  212. DEFAULT_IMPL()
  213. void
  214. LocAdapterBase::reportLocationsEvent(const Location* /*locations*/, size_t /*count*/,
  215. BatchingMode /*batchingMode*/)
  216. DEFAULT_IMPL()
  217. void
  218. LocAdapterBase::reportCompletedTripsEvent(uint32_t /*accumulated_distance*/)
  219. DEFAULT_IMPL()
  220. void
  221. LocAdapterBase::reportBatchStatusChangeEvent(BatchingStatus /*batchStatus*/)
  222. DEFAULT_IMPL()
  223. void
  224. LocAdapterBase::reportPositionEvent(UlpLocation& /*location*/,
  225. GpsLocationExtended& /*locationExtended*/,
  226. enum loc_sess_status /*status*/,
  227. LocPosTechMask /*loc_technology_mask*/)
  228. DEFAULT_IMPL()
  229. void
  230. LocAdapterBase::saveClient(LocationAPI* client, const LocationCallbacks& callbacks)
  231. {
  232. mClientData[client] = callbacks;
  233. updateClientsEventMask();
  234. }
  235. void
  236. LocAdapterBase::eraseClient(LocationAPI* client)
  237. {
  238. auto it = mClientData.find(client);
  239. if (it != mClientData.end()) {
  240. mClientData.erase(it);
  241. }
  242. updateClientsEventMask();
  243. }
  244. LocationCallbacks
  245. LocAdapterBase::getClientCallbacks(LocationAPI* client)
  246. {
  247. LocationCallbacks callbacks = {};
  248. auto it = mClientData.find(client);
  249. if (it != mClientData.end()) {
  250. callbacks = it->second;
  251. }
  252. return callbacks;
  253. }
  254. LocationCapabilitiesMask
  255. LocAdapterBase::getCapabilities()
  256. {
  257. LocationCapabilitiesMask mask = 0;
  258. if (isEngineCapabilitiesKnown()) {
  259. // time based tracking always supported
  260. mask |= LOCATION_CAPABILITIES_TIME_BASED_TRACKING_BIT;
  261. if (ContextBase::isMessageSupported(
  262. LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_LOCATION_BATCHING)){
  263. mask |= LOCATION_CAPABILITIES_TIME_BASED_BATCHING_BIT |
  264. LOCATION_CAPABILITIES_DISTANCE_BASED_BATCHING_BIT;
  265. }
  266. if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_TRACKING)) {
  267. mask |= LOCATION_CAPABILITIES_DISTANCE_BASED_TRACKING_BIT;
  268. }
  269. if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_OUTDOOR_TRIP_BATCHING)) {
  270. mask |= LOCATION_CAPABILITIES_OUTDOOR_TRIP_BATCHING_BIT;
  271. }
  272. // geofence always supported
  273. mask |= LOCATION_CAPABILITIES_GEOFENCE_BIT;
  274. if (ContextBase::gnssConstellationConfig()) {
  275. mask |= LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT;
  276. }
  277. uint32_t carrierCapabilities = ContextBase::getCarrierCapabilities();
  278. if (carrierCapabilities & LOC_GPS_CAPABILITY_MSB) {
  279. mask |= LOCATION_CAPABILITIES_GNSS_MSB_BIT;
  280. }
  281. if (LOC_GPS_CAPABILITY_MSA & carrierCapabilities) {
  282. mask |= LOCATION_CAPABILITIES_GNSS_MSA_BIT;
  283. }
  284. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_DEBUG_NMEA_V02)) {
  285. mask |= LOCATION_CAPABILITIES_DEBUG_NMEA_BIT;
  286. }
  287. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_CONSTELLATION_ENABLEMENT_V02)) {
  288. mask |= LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT;
  289. }
  290. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_AGPM_V02)) {
  291. mask |= LOCATION_CAPABILITIES_AGPM_BIT;
  292. }
  293. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_LOCATION_PRIVACY)) {
  294. mask |= LOCATION_CAPABILITIES_PRIVACY_BIT;
  295. }
  296. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_MEASUREMENTS_CORRECTION)) {
  297. mask |= LOCATION_CAPABILITIES_MEASUREMENTS_CORRECTION_BIT;
  298. }
  299. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_ROBUST_LOCATION)) {
  300. mask |= LOCATION_CAPABILITIES_CONFORMITY_INDEX_BIT;
  301. }
  302. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_EDGNSS) ||
  303. (ContextBase::getQwesFeatureStatus() & LOCATION_CAPABILITIES_QWES_DGNSS)) {
  304. mask |= LOCATION_CAPABILITIES_EDGNSS_BIT;
  305. }
  306. if ((ContextBase::getQwesFeatureStatus() & LOCATION_CAPABILITIES_QWES_PPE)) {
  307. mask |= LOCATION_CAPABILITIES_QWES_PPE;
  308. }
  309. } else {
  310. LOC_LOGE("%s]: attempt to get capabilities before they are known.", __func__);
  311. }
  312. return mask;
  313. }
  314. void
  315. LocAdapterBase::broadcastCapabilities(LocationCapabilitiesMask mask)
  316. {
  317. for (auto clientData : mClientData) {
  318. if (nullptr != clientData.second.capabilitiesCb) {
  319. clientData.second.capabilitiesCb(mask);
  320. }
  321. }
  322. }
  323. void
  324. LocAdapterBase::updateClientsEventMask()
  325. DEFAULT_IMPL()
  326. void
  327. LocAdapterBase::stopClientSessions(LocationAPI* /*client*/, bool /*eraseSession*/)
  328. DEFAULT_IMPL()
  329. void
  330. LocAdapterBase::addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks)
  331. {
  332. LOC_LOGD("%s]: client %p", __func__, client);
  333. struct MsgAddClient : public LocMsg {
  334. LocAdapterBase& mAdapter;
  335. LocationAPI* mClient;
  336. const LocationCallbacks mCallbacks;
  337. inline MsgAddClient(LocAdapterBase& adapter,
  338. LocationAPI* client,
  339. const LocationCallbacks& callbacks) :
  340. LocMsg(),
  341. mAdapter(adapter),
  342. mClient(client),
  343. mCallbacks(callbacks) {}
  344. inline virtual void proc() const {
  345. mAdapter.saveClient(mClient, mCallbacks);
  346. }
  347. };
  348. sendMsg(new MsgAddClient(*this, client, callbacks));
  349. }
  350. void
  351. LocAdapterBase::removeClientCommand(LocationAPI* client,
  352. removeClientCompleteCallback rmClientCb)
  353. {
  354. LOC_LOGD("%s]: client %p", __func__, client);
  355. struct MsgRemoveClient : public LocMsg {
  356. LocAdapterBase& mAdapter;
  357. LocationAPI* mClient;
  358. removeClientCompleteCallback mRmClientCb;
  359. inline MsgRemoveClient(LocAdapterBase& adapter,
  360. LocationAPI* client,
  361. removeClientCompleteCallback rmCb) :
  362. LocMsg(),
  363. mAdapter(adapter),
  364. mClient(client),
  365. mRmClientCb(rmCb){}
  366. inline virtual void proc() const {
  367. mAdapter.stopClientSessions(mClient);
  368. mAdapter.eraseClient(mClient);
  369. if (nullptr != mRmClientCb) {
  370. (mRmClientCb)(mClient);
  371. }
  372. }
  373. };
  374. sendMsg(new MsgRemoveClient(*this, client, rmClientCb));
  375. }
  376. void
  377. LocAdapterBase::requestCapabilitiesCommand(LocationAPI* client)
  378. {
  379. LOC_LOGD("%s]: ", __func__);
  380. struct MsgRequestCapabilities : public LocMsg {
  381. LocAdapterBase& mAdapter;
  382. LocationAPI* mClient;
  383. inline MsgRequestCapabilities(LocAdapterBase& adapter,
  384. LocationAPI* client) :
  385. LocMsg(),
  386. mAdapter(adapter),
  387. mClient(client) {}
  388. inline virtual void proc() const {
  389. if (!mAdapter.isEngineCapabilitiesKnown()) {
  390. mAdapter.mPendingMsgs.push_back(new MsgRequestCapabilities(*this));
  391. return;
  392. }
  393. LocationCallbacks callbacks = mAdapter.getClientCallbacks(mClient);
  394. if (callbacks.capabilitiesCb != nullptr) {
  395. callbacks.capabilitiesCb(mAdapter.getCapabilities());
  396. }
  397. }
  398. };
  399. sendMsg(new MsgRequestCapabilities(*this, client));
  400. }
  401. void
  402. LocAdapterBase::reportLatencyInfoEvent(const GnssLatencyInfo& /*gnssLatencyInfo*/)
  403. DEFAULT_IMPL()
  404. bool LocAdapterBase::
  405. reportQwesCapabilities(const std::unordered_map<LocationQwesFeatureType, bool> &/*featureMap*/)
  406. DEFAULT_IMPL(false)
  407. void LocAdapterBase::reportEngDebugDataInfoEvent(GnssEngineDebugDataInfo& gnssEngineDebugDataInfo)
  408. DEFAULT_IMPL()
  409. } // namespace loc_core