LocAdapterBase.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #define LOG_NDEBUG 0
  30. #define LOG_TAG "LocSvc_LocAdapterBase"
  31. #include <dlfcn.h>
  32. #include <LocAdapterBase.h>
  33. #include <loc_target.h>
  34. #include <log_util.h>
  35. #include <LocAdapterProxyBase.h>
  36. namespace loc_core {
  37. // This is the top level class, so the constructor will
  38. // always gets called. Here we prepare for the default.
  39. // But if getLocApi(targetEnumType target) is overriden,
  40. // the right locApi should get created.
  41. LocAdapterBase::LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,
  42. ContextBase* context, bool isMaster,
  43. LocAdapterProxyBase *adapterProxyBase,
  44. bool waitForDoneInit) :
  45. mIsMaster(isMaster), mEvtMask(mask), mContext(context),
  46. mLocApi(context->getLocApi()), mLocAdapterProxyBase(adapterProxyBase),
  47. mMsgTask(context->getMsgTask()),
  48. mIsEngineCapabilitiesKnown(ContextBase::sIsEngineCapabilitiesKnown)
  49. {
  50. LOC_LOGd("waitForDoneInit: %d", waitForDoneInit);
  51. if (!waitForDoneInit) {
  52. mLocApi->addAdapter(this);
  53. mAdapterAdded = true;
  54. } else {
  55. mAdapterAdded = false;
  56. }
  57. }
  58. uint32_t LocAdapterBase::mSessionIdCounter(1);
  59. uint32_t LocAdapterBase::generateSessionId()
  60. {
  61. if (++mSessionIdCounter == 0xFFFFFFFF)
  62. mSessionIdCounter = 1;
  63. return mSessionIdCounter;
  64. }
  65. void LocAdapterBase::handleEngineUpEvent()
  66. {
  67. if (mLocAdapterProxyBase) {
  68. mLocAdapterProxyBase->handleEngineUpEvent();
  69. }
  70. }
  71. void LocAdapterBase::handleEngineDownEvent()
  72. {
  73. if (mLocAdapterProxyBase) {
  74. mLocAdapterProxyBase->handleEngineDownEvent();
  75. }
  76. }
  77. void LocAdapterBase::
  78. reportPositionEvent(const UlpLocation& location,
  79. const GpsLocationExtended& locationExtended,
  80. enum loc_sess_status status,
  81. LocPosTechMask loc_technology_mask,
  82. GnssDataNotification* pDataNotify,
  83. int msInWeek)
  84. {
  85. if (mLocAdapterProxyBase != NULL) {
  86. mLocAdapterProxyBase->reportPositionEvent((UlpLocation&)location,
  87. (GpsLocationExtended&)locationExtended,
  88. status,
  89. loc_technology_mask);
  90. } else {
  91. DEFAULT_IMPL()
  92. }
  93. }
  94. void LocAdapterBase::
  95. reportSvEvent(const GnssSvNotification& /*svNotify*/)
  96. DEFAULT_IMPL()
  97. void LocAdapterBase::
  98. reportSvPolynomialEvent(GnssSvPolynomial &/*svPolynomial*/)
  99. DEFAULT_IMPL()
  100. void LocAdapterBase::
  101. reportSvEphemerisEvent(GnssSvEphemerisReport &/*svEphemeris*/)
  102. DEFAULT_IMPL()
  103. void LocAdapterBase::
  104. reportStatus(LocGpsStatusValue /*status*/)
  105. DEFAULT_IMPL()
  106. void LocAdapterBase::
  107. reportNmeaEvent(const char* /*nmea*/, size_t /*length*/)
  108. DEFAULT_IMPL()
  109. void LocAdapterBase::
  110. reportDataEvent(const GnssDataNotification& /*dataNotify*/,
  111. int /*msInWeek*/)
  112. DEFAULT_IMPL()
  113. bool LocAdapterBase::
  114. reportXtraServer(const char* /*url1*/, const char* /*url2*/,
  115. const char* /*url3*/, const int /*maxlength*/)
  116. DEFAULT_IMPL(false)
  117. void LocAdapterBase::
  118. reportLocationSystemInfoEvent(const LocationSystemInfo& /*locationSystemInfo*/)
  119. DEFAULT_IMPL()
  120. bool LocAdapterBase::
  121. requestXtraData()
  122. DEFAULT_IMPL(false)
  123. bool LocAdapterBase::
  124. requestTime()
  125. DEFAULT_IMPL(false)
  126. bool LocAdapterBase::
  127. requestLocation()
  128. DEFAULT_IMPL(false)
  129. bool LocAdapterBase::
  130. requestATL(int /*connHandle*/, LocAGpsType /*agps_type*/,
  131. LocApnTypeMask /*apn_type_mask*/, LocSubId /*sub_id*/)
  132. DEFAULT_IMPL(false)
  133. bool LocAdapterBase::
  134. releaseATL(int /*connHandle*/)
  135. DEFAULT_IMPL(false)
  136. bool LocAdapterBase::
  137. requestNiNotifyEvent(const GnssNiNotification &/*notify*/,
  138. const void* /*data*/,
  139. const LocInEmergency emergencyState)
  140. DEFAULT_IMPL(false)
  141. void LocAdapterBase::
  142. reportGnssMeasurementsEvent(const GnssMeasurements& /*gnssMeasurements*/,
  143. int /*msInWeek*/)
  144. DEFAULT_IMPL()
  145. bool LocAdapterBase::
  146. reportWwanZppFix(LocGpsLocation &/*zppLoc*/)
  147. DEFAULT_IMPL(false)
  148. bool LocAdapterBase::
  149. reportZppBestAvailableFix(LocGpsLocation& /*zppLoc*/,
  150. GpsLocationExtended& /*location_extended*/, LocPosTechMask /*tech_mask*/)
  151. DEFAULT_IMPL(false)
  152. void LocAdapterBase::reportGnssSvIdConfigEvent(const GnssSvIdConfig& /*config*/)
  153. DEFAULT_IMPL()
  154. void LocAdapterBase::reportGnssSvTypeConfigEvent(const GnssSvTypeConfig& /*config*/)
  155. DEFAULT_IMPL()
  156. void LocAdapterBase::reportGnssConfigEvent(uint32_t, /* session id*/
  157. const GnssConfig& /*gnssConfig*/)
  158. DEFAULT_IMPL()
  159. bool LocAdapterBase::
  160. requestOdcpiEvent(OdcpiRequestInfo& /*request*/)
  161. DEFAULT_IMPL(false)
  162. bool LocAdapterBase::
  163. reportGnssEngEnergyConsumedEvent(uint64_t /*energyConsumedSinceFirstBoot*/)
  164. DEFAULT_IMPL(false)
  165. bool LocAdapterBase::
  166. reportDeleteAidingDataEvent(GnssAidingData & /*aidingData*/)
  167. DEFAULT_IMPL(false)
  168. bool LocAdapterBase::
  169. reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& /*ionoModel*/)
  170. DEFAULT_IMPL(false)
  171. bool LocAdapterBase::
  172. reportGnssAdditionalSystemInfoEvent(GnssAdditionalSystemInfo& /*additionalSystemInfo*/)
  173. DEFAULT_IMPL(false)
  174. void LocAdapterBase::
  175. reportNfwNotificationEvent(GnssNfwNotification& /*notification*/)
  176. DEFAULT_IMPL()
  177. void
  178. LocAdapterBase::geofenceBreachEvent(size_t /*count*/, uint32_t* /*hwIds*/, Location& /*location*/,
  179. GeofenceBreachType /*breachType*/, uint64_t /*timestamp*/)
  180. DEFAULT_IMPL()
  181. void
  182. LocAdapterBase::geofenceStatusEvent(GeofenceStatusAvailable /*available*/)
  183. DEFAULT_IMPL()
  184. void
  185. LocAdapterBase::reportLocationsEvent(const Location* /*locations*/, size_t /*count*/,
  186. BatchingMode /*batchingMode*/)
  187. DEFAULT_IMPL()
  188. void
  189. LocAdapterBase::reportCompletedTripsEvent(uint32_t /*accumulated_distance*/)
  190. DEFAULT_IMPL()
  191. void
  192. LocAdapterBase::reportBatchStatusChangeEvent(BatchingStatus /*batchStatus*/)
  193. DEFAULT_IMPL()
  194. void
  195. LocAdapterBase::reportPositionEvent(UlpLocation& /*location*/,
  196. GpsLocationExtended& /*locationExtended*/,
  197. enum loc_sess_status /*status*/,
  198. LocPosTechMask /*loc_technology_mask*/)
  199. DEFAULT_IMPL()
  200. void
  201. LocAdapterBase::saveClient(LocationAPI* client, const LocationCallbacks& callbacks)
  202. {
  203. mClientData[client] = callbacks;
  204. updateClientsEventMask();
  205. }
  206. void
  207. LocAdapterBase::eraseClient(LocationAPI* client)
  208. {
  209. auto it = mClientData.find(client);
  210. if (it != mClientData.end()) {
  211. mClientData.erase(it);
  212. }
  213. updateClientsEventMask();
  214. }
  215. LocationCallbacks
  216. LocAdapterBase::getClientCallbacks(LocationAPI* client)
  217. {
  218. LocationCallbacks callbacks = {};
  219. auto it = mClientData.find(client);
  220. if (it != mClientData.end()) {
  221. callbacks = it->second;
  222. }
  223. return callbacks;
  224. }
  225. LocationCapabilitiesMask
  226. LocAdapterBase::getCapabilities()
  227. {
  228. LocationCapabilitiesMask mask = 0;
  229. if (isEngineCapabilitiesKnown()) {
  230. // time based tracking always supported
  231. mask |= LOCATION_CAPABILITIES_TIME_BASED_TRACKING_BIT;
  232. if (ContextBase::isMessageSupported(
  233. LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_LOCATION_BATCHING)){
  234. mask |= LOCATION_CAPABILITIES_TIME_BASED_BATCHING_BIT |
  235. LOCATION_CAPABILITIES_DISTANCE_BASED_BATCHING_BIT;
  236. }
  237. if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_TRACKING)) {
  238. mask |= LOCATION_CAPABILITIES_DISTANCE_BASED_TRACKING_BIT;
  239. }
  240. if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_OUTDOOR_TRIP_BATCHING)) {
  241. mask |= LOCATION_CAPABILITIES_OUTDOOR_TRIP_BATCHING_BIT;
  242. }
  243. // geofence always supported
  244. mask |= LOCATION_CAPABILITIES_GEOFENCE_BIT;
  245. if (ContextBase::gnssConstellationConfig()) {
  246. mask |= LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT;
  247. }
  248. uint32_t carrierCapabilities = ContextBase::getCarrierCapabilities();
  249. if (carrierCapabilities & LOC_GPS_CAPABILITY_MSB) {
  250. mask |= LOCATION_CAPABILITIES_GNSS_MSB_BIT;
  251. }
  252. if (LOC_GPS_CAPABILITY_MSA & carrierCapabilities) {
  253. mask |= LOCATION_CAPABILITIES_GNSS_MSA_BIT;
  254. }
  255. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_DEBUG_NMEA_V02)) {
  256. mask |= LOCATION_CAPABILITIES_DEBUG_NMEA_BIT;
  257. }
  258. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_CONSTELLATION_ENABLEMENT_V02)) {
  259. mask |= LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT;
  260. }
  261. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_AGPM_V02)) {
  262. mask |= LOCATION_CAPABILITIES_AGPM_BIT;
  263. }
  264. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_LOCATION_PRIVACY)) {
  265. mask |= LOCATION_CAPABILITIES_PRIVACY_BIT;
  266. }
  267. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_MEASUREMENTS_CORRECTION)) {
  268. mask |= LOCATION_CAPABILITIES_MEASUREMENTS_CORRECTION_BIT;
  269. }
  270. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_ROBUST_LOCATION)) {
  271. mask |= LOCATION_CAPABILITIES_CONFORMITY_INDEX_BIT;
  272. }
  273. if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_EDGNSS) ||
  274. (ContextBase::getQwesFeatureStatus() & LOCATION_CAPABILITIES_QWES_DGNSS)) {
  275. mask |= LOCATION_CAPABILITIES_EDGNSS_BIT;
  276. }
  277. if ((ContextBase::getQwesFeatureStatus() & LOCATION_CAPABILITIES_QWES_PPE)) {
  278. mask |= LOCATION_CAPABILITIES_QWES_PPE;
  279. }
  280. } else {
  281. LOC_LOGE("%s]: attempt to get capabilities before they are known.", __func__);
  282. }
  283. return mask;
  284. }
  285. void
  286. LocAdapterBase::broadcastCapabilities(LocationCapabilitiesMask mask)
  287. {
  288. for (auto clientData : mClientData) {
  289. if (nullptr != clientData.second.capabilitiesCb) {
  290. clientData.second.capabilitiesCb(mask);
  291. }
  292. }
  293. }
  294. void
  295. LocAdapterBase::updateClientsEventMask()
  296. DEFAULT_IMPL()
  297. void
  298. LocAdapterBase::stopClientSessions(LocationAPI* client, bool eraseSession)
  299. DEFAULT_IMPL()
  300. void
  301. LocAdapterBase::addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks)
  302. {
  303. LOC_LOGD("%s]: client %p", __func__, client);
  304. struct MsgAddClient : public LocMsg {
  305. LocAdapterBase& mAdapter;
  306. LocationAPI* mClient;
  307. const LocationCallbacks mCallbacks;
  308. inline MsgAddClient(LocAdapterBase& adapter,
  309. LocationAPI* client,
  310. const LocationCallbacks& callbacks) :
  311. LocMsg(),
  312. mAdapter(adapter),
  313. mClient(client),
  314. mCallbacks(callbacks) {}
  315. inline virtual void proc() const {
  316. mAdapter.saveClient(mClient, mCallbacks);
  317. }
  318. };
  319. sendMsg(new MsgAddClient(*this, client, callbacks));
  320. }
  321. void
  322. LocAdapterBase::removeClientCommand(LocationAPI* client,
  323. removeClientCompleteCallback rmClientCb)
  324. {
  325. LOC_LOGD("%s]: client %p", __func__, client);
  326. struct MsgRemoveClient : public LocMsg {
  327. LocAdapterBase& mAdapter;
  328. LocationAPI* mClient;
  329. removeClientCompleteCallback mRmClientCb;
  330. inline MsgRemoveClient(LocAdapterBase& adapter,
  331. LocationAPI* client,
  332. removeClientCompleteCallback rmCb) :
  333. LocMsg(),
  334. mAdapter(adapter),
  335. mClient(client),
  336. mRmClientCb(rmCb){}
  337. inline virtual void proc() const {
  338. mAdapter.stopClientSessions(mClient);
  339. mAdapter.eraseClient(mClient);
  340. if (nullptr != mRmClientCb) {
  341. (mRmClientCb)(mClient);
  342. }
  343. }
  344. };
  345. sendMsg(new MsgRemoveClient(*this, client, rmClientCb));
  346. }
  347. void
  348. LocAdapterBase::requestCapabilitiesCommand(LocationAPI* client)
  349. {
  350. LOC_LOGD("%s]: ", __func__);
  351. struct MsgRequestCapabilities : public LocMsg {
  352. LocAdapterBase& mAdapter;
  353. LocationAPI* mClient;
  354. inline MsgRequestCapabilities(LocAdapterBase& adapter,
  355. LocationAPI* client) :
  356. LocMsg(),
  357. mAdapter(adapter),
  358. mClient(client) {}
  359. inline virtual void proc() const {
  360. if (!mAdapter.isEngineCapabilitiesKnown()) {
  361. mAdapter.mPendingMsgs.push_back(new MsgRequestCapabilities(*this));
  362. return;
  363. }
  364. LocationCallbacks callbacks = mAdapter.getClientCallbacks(mClient);
  365. if (callbacks.capabilitiesCb != nullptr) {
  366. callbacks.capabilitiesCb(mAdapter.getCapabilities());
  367. }
  368. }
  369. };
  370. sendMsg(new MsgRequestCapabilities(*this, client));
  371. }
  372. void
  373. LocAdapterBase::reportLatencyInfoEvent(const GnssLatencyInfo& /*gnssLatencyInfo*/)
  374. DEFAULT_IMPL()
  375. bool LocAdapterBase::
  376. reportQwesCapabilities(const std::unordered_map<LocationQwesFeatureType, bool> &featureMap)
  377. DEFAULT_IMPL(false)
  378. } // namespace loc_core