LocationAPI.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /* Copyright (c) 2017-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. Changes from Qualcomm Innovation Center are provided under the following license:
  30. Copyright (c) 2022, 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  31. Redistribution and use in source and binary forms, with or without
  32. modification, are permitted (subject to the limitations in the
  33. disclaimer below) provided that the following conditions are met:
  34. * Redistributions of source code must retain the above copyright
  35. notice, this list of conditions and the following disclaimer.
  36. * Redistributions in binary form must reproduce the above
  37. copyright notice, this list of conditions and the following
  38. disclaimer in the documentation and/or other materials provided
  39. with the distribution.
  40. * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  41. contributors may be used to endorse or promote products derived
  42. from this software without specific prior written permission.
  43. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  44. GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  45. HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  46. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  47. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  48. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  49. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  51. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  52. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  53. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  54. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  55. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. #define LOG_NDEBUG 0
  58. #define LOG_TAG "LocSvc_LocationAPI"
  59. #include <location_interface.h>
  60. #include <dlfcn.h>
  61. #include <loc_pla.h>
  62. #include <log_util.h>
  63. #include <pthread.h>
  64. #include <map>
  65. #include <loc_misc_utils.h>
  66. typedef const GnssInterface* (getGnssInterface)();
  67. typedef const GeofenceInterface* (getGeofenceInterface)();
  68. typedef const BatchingInterface* (getBatchingInterface)();
  69. // GTP services
  70. typedef void (enableProviderGetter)();
  71. typedef void (disableProviderGetter)();
  72. typedef void (getSingleNetworkLocationGetter)(trackingCallback* callback);
  73. typedef void (stopNetworkLocationGetter)(trackingCallback* callback);
  74. typedef struct {
  75. // bit mask of the adpaters that we need to wait for the removeClientCompleteCallback
  76. // before we invoke the registered locationApiDestroyCompleteCallback
  77. LocationAdapterTypeMask waitAdapterMask;
  78. locationApiDestroyCompleteCallback destroyCompleteCb;
  79. } LocationAPIDestroyCbData;
  80. // This is the map for the client that has requested destroy with
  81. // destroy callback provided.
  82. typedef std::map<LocationAPI*, LocationAPIDestroyCbData>
  83. LocationClientDestroyCbMap;
  84. typedef std::map<LocationAPI*, LocationCallbacks> LocationClientMap;
  85. typedef struct {
  86. LocationClientMap clientData;
  87. LocationClientDestroyCbMap destroyClientData;
  88. LocationControlAPI* controlAPI;
  89. LocationControlCallbacks controlCallbacks;
  90. GnssInterface* gnssInterface;
  91. GeofenceInterface* geofenceInterface;
  92. BatchingInterface* batchingInterface;
  93. } LocationAPIData;
  94. static LocationAPIData gData = {};
  95. static pthread_mutex_t gDataMutex = PTHREAD_MUTEX_INITIALIZER;
  96. static bool gGnssLoadFailed = false;
  97. static bool gBatchingLoadFailed = false;
  98. static bool gGeofenceLoadFailed = false;
  99. template <typename T1, typename T2>
  100. static const T1* loadLocationInterface(const char* library, const char* name) {
  101. void* libhandle = nullptr;
  102. T2* getter = (T2*)dlGetSymFromLib(libhandle, library, name);
  103. if (nullptr == getter) {
  104. return (const T1*) getter;
  105. }else {
  106. return (*getter)();
  107. }
  108. }
  109. static void loadLibGnss() {
  110. if (NULL == gData.gnssInterface && !gGnssLoadFailed) {
  111. gData.gnssInterface =
  112. (GnssInterface*)loadLocationInterface<GnssInterface,
  113. getGnssInterface>("libgnss.so", "getGnssInterface");
  114. if (NULL == gData.gnssInterface) {
  115. gGnssLoadFailed = true;
  116. LOC_LOGW("%s:%d]: No gnss interface available", __func__, __LINE__);
  117. } else {
  118. gData.gnssInterface->initialize();
  119. }
  120. }
  121. }
  122. static void loadLibBatching() {
  123. if (NULL == gData.batchingInterface && !gBatchingLoadFailed) {
  124. gData.batchingInterface =
  125. (BatchingInterface*)loadLocationInterface<BatchingInterface,
  126. getBatchingInterface>("libbatching.so", "getBatchingInterface");
  127. if (NULL == gData.batchingInterface) {
  128. gBatchingLoadFailed = true;
  129. LOC_LOGW("%s:%d]: No batching interface available", __func__, __LINE__);
  130. } else {
  131. gData.batchingInterface->initialize();
  132. }
  133. }
  134. }
  135. static void loadLibGeofencing() {
  136. if (NULL == gData.geofenceInterface && !gGeofenceLoadFailed) {
  137. gData.geofenceInterface =
  138. (GeofenceInterface*)loadLocationInterface<GeofenceInterface,
  139. getGeofenceInterface>("libgeofencing.so", "getGeofenceInterface");
  140. if (NULL == gData.geofenceInterface) {
  141. gGeofenceLoadFailed = true;
  142. LOC_LOGW("%s:%d]: No geofence interface available", __func__, __LINE__);
  143. } else {
  144. gData.geofenceInterface->initialize();
  145. }
  146. }
  147. }
  148. static bool isGnssClient(LocationCallbacks& locationCallbacks)
  149. {
  150. return (locationCallbacks.gnssNiCb != nullptr ||
  151. locationCallbacks.trackingCb != nullptr ||
  152. locationCallbacks.gnssLocationInfoCb != nullptr ||
  153. locationCallbacks.engineLocationsInfoCb != nullptr ||
  154. locationCallbacks.gnssSvCb != nullptr ||
  155. locationCallbacks.gnssNmeaCb != nullptr ||
  156. locationCallbacks.gnssDataCb != nullptr ||
  157. locationCallbacks.gnssMeasurementsCb != nullptr ||
  158. locationCallbacks.gnssNHzMeasurementsCb != nullptr ||
  159. locationCallbacks.locationSystemInfoCb != nullptr);
  160. }
  161. static bool isBatchingClient(LocationCallbacks& locationCallbacks)
  162. {
  163. return (locationCallbacks.batchingCb != nullptr);
  164. }
  165. static bool isGeofenceClient(LocationCallbacks& locationCallbacks)
  166. {
  167. return (locationCallbacks.geofenceBreachCb != nullptr ||
  168. locationCallbacks.geofenceStatusCb != nullptr);
  169. }
  170. void LocationAPI::onRemoveClientCompleteCb (LocationAdapterTypeMask adapterType)
  171. {
  172. bool invokeCallback = false;
  173. locationApiDestroyCompleteCallback destroyCompleteCb;
  174. LOC_LOGd("adatper type %x", adapterType);
  175. pthread_mutex_lock(&gDataMutex);
  176. auto it = gData.destroyClientData.find(this);
  177. if (it != gData.destroyClientData.end()) {
  178. it->second.waitAdapterMask &= ~adapterType;
  179. if (it->second.waitAdapterMask == 0) {
  180. invokeCallback = true;
  181. destroyCompleteCb = it->second.destroyCompleteCb;
  182. gData.destroyClientData.erase(it);
  183. }
  184. }
  185. pthread_mutex_unlock(&gDataMutex);
  186. if (invokeCallback) {
  187. LOC_LOGd("invoke client destroy cb");
  188. if (!destroyCompleteCb) {
  189. (destroyCompleteCb) ();
  190. }
  191. delete this;
  192. }
  193. }
  194. void onGnssRemoveClientCompleteCb (LocationAPI* client)
  195. {
  196. client->onRemoveClientCompleteCb (LOCATION_ADAPTER_GNSS_TYPE_BIT);
  197. }
  198. void onBatchingRemoveClientCompleteCb (LocationAPI* client)
  199. {
  200. client->onRemoveClientCompleteCb (LOCATION_ADAPTER_BATCHING_TYPE_BIT);
  201. }
  202. void onGeofenceRemoveClientCompleteCb (LocationAPI* client)
  203. {
  204. client->onRemoveClientCompleteCb (LOCATION_ADAPTER_GEOFENCE_TYPE_BIT);
  205. }
  206. LocationAPI*
  207. LocationAPI::createInstance (LocationCallbacks& locationCallbacks)
  208. {
  209. if (nullptr == locationCallbacks.capabilitiesCb ||
  210. nullptr == locationCallbacks.responseCb ||
  211. nullptr == locationCallbacks.collectiveResponseCb) {
  212. LOC_LOGe("missing mandatory callback, return null");
  213. return NULL;
  214. }
  215. LocationAPI* newLocationAPI = new LocationAPI();
  216. bool requestedCapabilities = false;
  217. pthread_mutex_lock(&gDataMutex);
  218. if (isGnssClient(locationCallbacks)) {
  219. loadLibGnss();
  220. if (NULL != gData.gnssInterface) {
  221. gData.gnssInterface->addClient(newLocationAPI, locationCallbacks);
  222. if (!requestedCapabilities) {
  223. gData.gnssInterface->requestCapabilities(newLocationAPI);
  224. requestedCapabilities = true;
  225. }
  226. }
  227. }
  228. if (isBatchingClient(locationCallbacks)) {
  229. loadLibBatching();
  230. if (NULL != gData.batchingInterface) {
  231. gData.batchingInterface->addClient(newLocationAPI, locationCallbacks);
  232. if (!requestedCapabilities) {
  233. gData.batchingInterface->requestCapabilities(newLocationAPI);
  234. requestedCapabilities = true;
  235. }
  236. }
  237. }
  238. if (isGeofenceClient(locationCallbacks)) {
  239. loadLibGeofencing();
  240. if (NULL != gData.geofenceInterface) {
  241. gData.geofenceInterface->addClient(newLocationAPI, locationCallbacks);
  242. if (!requestedCapabilities) {
  243. gData.geofenceInterface->requestCapabilities(newLocationAPI);
  244. requestedCapabilities = true;
  245. }
  246. }
  247. }
  248. if (!requestedCapabilities && locationCallbacks.capabilitiesCb != nullptr) {
  249. loadLibGnss();
  250. if (NULL != gData.gnssInterface) {
  251. gData.gnssInterface->addClient(newLocationAPI, locationCallbacks);
  252. gData.gnssInterface->requestCapabilities(newLocationAPI);
  253. requestedCapabilities = true;
  254. }
  255. }
  256. gData.clientData[newLocationAPI] = locationCallbacks;
  257. pthread_mutex_unlock(&gDataMutex);
  258. return newLocationAPI;
  259. }
  260. void
  261. LocationAPI::destroy(locationApiDestroyCompleteCallback destroyCompleteCb)
  262. {
  263. bool invokeDestroyCb = false;
  264. pthread_mutex_lock(&gDataMutex);
  265. auto it = gData.clientData.find(this);
  266. if (it != gData.clientData.end()) {
  267. bool removeFromGnssInf = (NULL != gData.gnssInterface);
  268. bool removeFromBatchingInf = (NULL != gData.batchingInterface);
  269. bool removeFromGeofenceInf = (NULL != gData.geofenceInterface);
  270. bool needToWait = (removeFromGnssInf || removeFromBatchingInf || removeFromGeofenceInf);
  271. LOC_LOGe("removeFromGnssInf: %d, removeFromBatchingInf: %d, removeFromGeofenceInf: %d,"
  272. "needToWait: %d", removeFromGnssInf, removeFromBatchingInf, removeFromGeofenceInf,
  273. needToWait);
  274. if ((NULL != destroyCompleteCb) && (true == needToWait)) {
  275. LocationAPIDestroyCbData destroyCbData = {};
  276. destroyCbData.destroyCompleteCb = destroyCompleteCb;
  277. // record down from which adapter we need to wait for the destroy complete callback
  278. // only when we have received all the needed callbacks from all the associated stacks,
  279. // we shall notify the client.
  280. destroyCbData.waitAdapterMask =
  281. (removeFromGnssInf ? LOCATION_ADAPTER_GNSS_TYPE_BIT : 0);
  282. destroyCbData.waitAdapterMask |=
  283. (removeFromBatchingInf ? LOCATION_ADAPTER_BATCHING_TYPE_BIT : 0);
  284. destroyCbData.waitAdapterMask |=
  285. (removeFromGeofenceInf ? LOCATION_ADAPTER_GEOFENCE_TYPE_BIT : 0);
  286. gData.destroyClientData[this] = destroyCbData;
  287. LOC_LOGi("destroy data stored in the map: 0x%x", destroyCbData.waitAdapterMask);
  288. }
  289. if (removeFromGnssInf) {
  290. gData.gnssInterface->removeClient(it->first,
  291. onGnssRemoveClientCompleteCb);
  292. }
  293. if (removeFromBatchingInf) {
  294. gData.batchingInterface->removeClient(it->first,
  295. onBatchingRemoveClientCompleteCb);
  296. }
  297. if (removeFromGeofenceInf) {
  298. gData.geofenceInterface->removeClient(it->first,
  299. onGeofenceRemoveClientCompleteCb);
  300. }
  301. gData.clientData.erase(it);
  302. if (!needToWait) {
  303. invokeDestroyCb = true;
  304. }
  305. } else {
  306. LOC_LOGE("%s:%d]: Location API client %p not found in client data",
  307. __func__, __LINE__, this);
  308. }
  309. pthread_mutex_unlock(&gDataMutex);
  310. if (invokeDestroyCb) {
  311. if (!destroyCompleteCb) {
  312. (destroyCompleteCb) ();
  313. }
  314. delete this;
  315. }
  316. }
  317. LocationAPI::LocationAPI()
  318. {
  319. LOC_LOGD("LOCATION API CONSTRUCTOR");
  320. }
  321. // private destructor
  322. LocationAPI::~LocationAPI()
  323. {
  324. LOC_LOGD("LOCATION API DESTRUCTOR");
  325. }
  326. void
  327. LocationAPI::updateCallbacks(LocationCallbacks& locationCallbacks)
  328. {
  329. if (nullptr == locationCallbacks.capabilitiesCb ||
  330. nullptr == locationCallbacks.responseCb ||
  331. nullptr == locationCallbacks.collectiveResponseCb) {
  332. return;
  333. }
  334. pthread_mutex_lock(&gDataMutex);
  335. LocationCallbacks currentCallbacks = {};
  336. auto it = gData.clientData.find(this);
  337. if (it != gData.clientData.end()) {
  338. currentCallbacks = gData.clientData[this];
  339. }
  340. if (isGnssClient(locationCallbacks)) {
  341. loadLibGnss();
  342. if (NULL != gData.gnssInterface) {
  343. // either adds new Client or updates existing Client
  344. gData.gnssInterface->addClient(this, locationCallbacks);
  345. }
  346. } else if (NULL != gData.gnssInterface && isGnssClient(currentCallbacks)) {
  347. gData.gnssInterface->removeClient(this, nullptr);
  348. }
  349. if (isBatchingClient(locationCallbacks)) {
  350. loadLibBatching();
  351. if (NULL != gData.batchingInterface) {
  352. // either adds new Client or updates existing Client
  353. gData.batchingInterface->addClient(this, locationCallbacks);
  354. }
  355. } else if (NULL != gData.batchingInterface && isBatchingClient(currentCallbacks)) {
  356. gData.batchingInterface->removeClient(this, nullptr);
  357. }
  358. if (isGeofenceClient(locationCallbacks)) {
  359. loadLibGeofencing();
  360. if (NULL != gData.geofenceInterface) {
  361. // either adds new Client or updates existing Client
  362. gData.geofenceInterface->addClient(this, locationCallbacks);
  363. }
  364. } else if (NULL != gData.geofenceInterface && isGeofenceClient(currentCallbacks)) {
  365. gData.geofenceInterface->removeClient(this, nullptr);
  366. }
  367. gData.clientData[this] = locationCallbacks;
  368. pthread_mutex_unlock(&gDataMutex);
  369. }
  370. uint32_t
  371. LocationAPI::startTracking(TrackingOptions& trackingOptions)
  372. {
  373. uint32_t id = 0;
  374. pthread_mutex_lock(&gDataMutex);
  375. auto it = gData.clientData.find(this);
  376. if (it != gData.clientData.end()) {
  377. if (NULL != gData.gnssInterface) {
  378. id = gData.gnssInterface->startTracking(this, trackingOptions);
  379. } else {
  380. LOC_LOGE("%s:%d]: No gnss interface available for Location API client %p ",
  381. __func__, __LINE__, this);
  382. }
  383. } else {
  384. LOC_LOGE("%s:%d]: Location API client %p not found in client data",
  385. __func__, __LINE__, this);
  386. }
  387. pthread_mutex_unlock(&gDataMutex);
  388. return id;
  389. }
  390. void
  391. LocationAPI::stopTracking(uint32_t id)
  392. {
  393. pthread_mutex_lock(&gDataMutex);
  394. auto it = gData.clientData.find(this);
  395. if (it != gData.clientData.end()) {
  396. if (gData.gnssInterface != NULL) {
  397. gData.gnssInterface->stopTracking(this, id);
  398. } else {
  399. LOC_LOGE("%s:%d]: No gnss interface available for Location API client %p ",
  400. __func__, __LINE__, this);
  401. }
  402. } else {
  403. LOC_LOGE("%s:%d]: Location API client %p not found in client data",
  404. __func__, __LINE__, this);
  405. }
  406. pthread_mutex_unlock(&gDataMutex);
  407. }
  408. void
  409. LocationAPI::updateTrackingOptions(
  410. uint32_t id, TrackingOptions& trackingOptions)
  411. {
  412. pthread_mutex_lock(&gDataMutex);
  413. auto it = gData.clientData.find(this);
  414. if (it != gData.clientData.end()) {
  415. if (gData.gnssInterface != NULL) {
  416. gData.gnssInterface->updateTrackingOptions(this, id, trackingOptions);
  417. } else {
  418. LOC_LOGE("%s:%d]: No gnss interface available for Location API client %p ",
  419. __func__, __LINE__, this);
  420. }
  421. } else {
  422. LOC_LOGE("%s:%d]: Location API client %p not found in client data",
  423. __func__, __LINE__, this);
  424. }
  425. pthread_mutex_unlock(&gDataMutex);
  426. }
  427. uint32_t
  428. LocationAPI::startBatching(BatchingOptions &batchingOptions)
  429. {
  430. uint32_t id = 0;
  431. pthread_mutex_lock(&gDataMutex);
  432. if (NULL != gData.batchingInterface) {
  433. id = gData.batchingInterface->startBatching(this, batchingOptions);
  434. } else {
  435. LOC_LOGE("%s:%d]: No batching interface available for Location API client %p ",
  436. __func__, __LINE__, this);
  437. }
  438. pthread_mutex_unlock(&gDataMutex);
  439. return id;
  440. }
  441. void
  442. LocationAPI::stopBatching(uint32_t id)
  443. {
  444. pthread_mutex_lock(&gDataMutex);
  445. if (NULL != gData.batchingInterface) {
  446. gData.batchingInterface->stopBatching(this, id);
  447. } else {
  448. LOC_LOGE("%s:%d]: No batching interface available for Location API client %p ",
  449. __func__, __LINE__, this);
  450. }
  451. pthread_mutex_unlock(&gDataMutex);
  452. }
  453. void
  454. LocationAPI::updateBatchingOptions(uint32_t id, BatchingOptions& batchOptions)
  455. {
  456. pthread_mutex_lock(&gDataMutex);
  457. if (NULL != gData.batchingInterface) {
  458. gData.batchingInterface->updateBatchingOptions(this, id, batchOptions);
  459. } else {
  460. LOC_LOGE("%s:%d]: No batching interface available for Location API client %p ",
  461. __func__, __LINE__, this);
  462. }
  463. pthread_mutex_unlock(&gDataMutex);
  464. }
  465. void
  466. LocationAPI::getBatchedLocations(uint32_t id, size_t count)
  467. {
  468. pthread_mutex_lock(&gDataMutex);
  469. if (gData.batchingInterface != NULL) {
  470. gData.batchingInterface->getBatchedLocations(this, id, count);
  471. } else {
  472. LOC_LOGE("%s:%d]: No batching interface available for Location API client %p ",
  473. __func__, __LINE__, this);
  474. }
  475. pthread_mutex_unlock(&gDataMutex);
  476. }
  477. uint32_t*
  478. LocationAPI::addGeofences(size_t count, GeofenceOption* options, GeofenceInfo* info)
  479. {
  480. uint32_t* ids = NULL;
  481. pthread_mutex_lock(&gDataMutex);
  482. if (gData.geofenceInterface != NULL) {
  483. ids = gData.geofenceInterface->addGeofences(this, count, options, info);
  484. } else {
  485. LOC_LOGE("%s:%d]: No geofence interface available for Location API client %p ",
  486. __func__, __LINE__, this);
  487. }
  488. pthread_mutex_unlock(&gDataMutex);
  489. return ids;
  490. }
  491. void
  492. LocationAPI::removeGeofences(size_t count, uint32_t* ids)
  493. {
  494. pthread_mutex_lock(&gDataMutex);
  495. if (gData.geofenceInterface != NULL) {
  496. gData.geofenceInterface->removeGeofences(this, count, ids);
  497. } else {
  498. LOC_LOGE("%s:%d]: No geofence interface available for Location API client %p ",
  499. __func__, __LINE__, this);
  500. }
  501. pthread_mutex_unlock(&gDataMutex);
  502. }
  503. void
  504. LocationAPI::modifyGeofences(size_t count, uint32_t* ids, GeofenceOption* options)
  505. {
  506. pthread_mutex_lock(&gDataMutex);
  507. if (gData.geofenceInterface != NULL) {
  508. gData.geofenceInterface->modifyGeofences(this, count, ids, options);
  509. } else {
  510. LOC_LOGE("%s:%d]: No geofence interface available for Location API client %p ",
  511. __func__, __LINE__, this);
  512. }
  513. pthread_mutex_unlock(&gDataMutex);
  514. }
  515. void
  516. LocationAPI::pauseGeofences(size_t count, uint32_t* ids)
  517. {
  518. pthread_mutex_lock(&gDataMutex);
  519. if (gData.geofenceInterface != NULL) {
  520. gData.geofenceInterface->pauseGeofences(this, count, ids);
  521. } else {
  522. LOC_LOGE("%s:%d]: No geofence interface available for Location API client %p ",
  523. __func__, __LINE__, this);
  524. }
  525. pthread_mutex_unlock(&gDataMutex);
  526. }
  527. void
  528. LocationAPI::resumeGeofences(size_t count, uint32_t* ids)
  529. {
  530. pthread_mutex_lock(&gDataMutex);
  531. if (gData.geofenceInterface != NULL) {
  532. gData.geofenceInterface->resumeGeofences(this, count, ids);
  533. } else {
  534. LOC_LOGE("%s:%d]: No geofence interface available for Location API client %p ",
  535. __func__, __LINE__, this);
  536. }
  537. pthread_mutex_unlock(&gDataMutex);
  538. }
  539. void
  540. LocationAPI::gnssNiResponse(uint32_t id, GnssNiResponse response)
  541. {
  542. pthread_mutex_lock(&gDataMutex);
  543. if (gData.gnssInterface != NULL) {
  544. gData.gnssInterface->gnssNiResponse(this, id, response);
  545. } else {
  546. LOC_LOGE("%s:%d]: No gnss interface available for Location API client %p ",
  547. __func__, __LINE__, this);
  548. }
  549. pthread_mutex_unlock(&gDataMutex);
  550. }
  551. void LocationAPI::enableNetworkProvider() {
  552. void* libHandle = nullptr;
  553. enableProviderGetter* setter = (enableProviderGetter*)dlGetSymFromLib(libHandle,
  554. "liblocationservice_glue.so", "enableNetworkProvider");
  555. if (setter != nullptr) {
  556. (*setter)();
  557. } else {
  558. LOC_LOGe("dlGetSymFromLib failed for liblocationservice_glue.so");
  559. }
  560. }
  561. void LocationAPI::disableNetworkProvider() {
  562. void* libHandle = nullptr;
  563. disableProviderGetter* setter = (disableProviderGetter*)dlGetSymFromLib(libHandle,
  564. "liblocationservice_glue.so", "disableNetworkProvider");
  565. if (setter != nullptr) {
  566. (*setter)();
  567. } else {
  568. LOC_LOGe("dlGetSymFromLib failed for liblocationservice_glue.so");
  569. }
  570. }
  571. void LocationAPI::startNetworkLocation(trackingCallback* callback) {
  572. void* libHandle = nullptr;
  573. getSingleNetworkLocationGetter* setter =
  574. (getSingleNetworkLocationGetter*)dlGetSymFromLib(libHandle,
  575. "liblocationservice_glue.so", "startNetworkLocation");
  576. if (setter != nullptr) {
  577. (*setter)(callback);
  578. } else {
  579. LOC_LOGe("dlGetSymFromLib failed for liblocationservice_glue.so");
  580. }
  581. }
  582. void LocationAPI::stopNetworkLocation(trackingCallback* callback) {
  583. void* libHandle = nullptr;
  584. stopNetworkLocationGetter* setter = (stopNetworkLocationGetter*)dlGetSymFromLib(libHandle,
  585. "liblocationservice_glue.so", "stopNetworkLocation");
  586. if (setter != nullptr) {
  587. LOC_LOGe("called");
  588. (*setter)(callback);
  589. } else {
  590. LOC_LOGe("dlGetSymFromLib failed for liblocationservice_glue.so");
  591. }
  592. }
  593. LocationControlAPI*
  594. LocationControlAPI::createInstance(LocationControlCallbacks& locationControlCallbacks)
  595. {
  596. LocationControlAPI* controlAPI = NULL;
  597. pthread_mutex_lock(&gDataMutex);
  598. if (NULL != gData.controlAPI) {
  599. controlAPI = gData.controlAPI;
  600. } else {
  601. if (nullptr != locationControlCallbacks.responseCb && NULL == gData.controlAPI) {
  602. loadLibGnss();
  603. if (NULL != gData.gnssInterface) {
  604. gData.controlAPI = new LocationControlAPI();
  605. gData.controlCallbacks = locationControlCallbacks;
  606. gData.gnssInterface->setControlCallbacks(locationControlCallbacks);
  607. controlAPI = gData.controlAPI;
  608. }
  609. }
  610. }
  611. pthread_mutex_unlock(&gDataMutex);
  612. return controlAPI;
  613. }
  614. LocationControlAPI*
  615. LocationControlAPI::getInstance()
  616. {
  617. LocationControlAPI* controlAPI = NULL;
  618. pthread_mutex_lock(&gDataMutex);
  619. controlAPI = gData.controlAPI;
  620. pthread_mutex_unlock(&gDataMutex);
  621. return controlAPI;
  622. }
  623. void
  624. LocationControlAPI::destroy()
  625. {
  626. delete this;
  627. }
  628. LocationControlAPI::LocationControlAPI()
  629. {
  630. LOC_LOGD("LOCATION CONTROL API CONSTRUCTOR");
  631. }
  632. LocationControlAPI::~LocationControlAPI()
  633. {
  634. LOC_LOGD("LOCATION CONTROL API DESTRUCTOR");
  635. pthread_mutex_lock(&gDataMutex);
  636. gData.controlAPI = NULL;
  637. pthread_mutex_unlock(&gDataMutex);
  638. }
  639. uint32_t
  640. LocationControlAPI::enable(LocationTechnologyType techType)
  641. {
  642. uint32_t id = 0;
  643. pthread_mutex_lock(&gDataMutex);
  644. if (gData.gnssInterface != NULL) {
  645. id = gData.gnssInterface->enable(techType);
  646. } else {
  647. LOC_LOGE("%s:%d]: No gnss interface available for Location Control API client %p ",
  648. __func__, __LINE__, this);
  649. }
  650. pthread_mutex_unlock(&gDataMutex);
  651. return id;
  652. }
  653. void
  654. LocationControlAPI::disable(uint32_t id)
  655. {
  656. pthread_mutex_lock(&gDataMutex);
  657. if (gData.gnssInterface != NULL) {
  658. gData.gnssInterface->disable(id);
  659. } else {
  660. LOC_LOGE("%s:%d]: No gnss interface available for Location Control API client %p ",
  661. __func__, __LINE__, this);
  662. }
  663. pthread_mutex_unlock(&gDataMutex);
  664. }
  665. uint32_t*
  666. LocationControlAPI::gnssUpdateConfig(const GnssConfig& config)
  667. {
  668. uint32_t* ids = NULL;
  669. pthread_mutex_lock(&gDataMutex);
  670. if (gData.gnssInterface != NULL) {
  671. ids = gData.gnssInterface->gnssUpdateConfig(config);
  672. } else {
  673. LOC_LOGE("%s:%d]: No gnss interface available for Location Control API client %p ",
  674. __func__, __LINE__, this);
  675. }
  676. pthread_mutex_unlock(&gDataMutex);
  677. return ids;
  678. }
  679. uint32_t* LocationControlAPI::gnssGetConfig(GnssConfigFlagsMask mask) {
  680. uint32_t* ids = NULL;
  681. pthread_mutex_lock(&gDataMutex);
  682. if (NULL != gData.gnssInterface) {
  683. ids = gData.gnssInterface->gnssGetConfig(mask);
  684. } else {
  685. LOC_LOGe("No gnss interface available for Control API client %p", this);
  686. }
  687. pthread_mutex_unlock(&gDataMutex);
  688. return ids;
  689. }
  690. uint32_t
  691. LocationControlAPI::gnssDeleteAidingData(GnssAidingData& data)
  692. {
  693. uint32_t id = 0;
  694. pthread_mutex_lock(&gDataMutex);
  695. if (gData.gnssInterface != NULL) {
  696. id = gData.gnssInterface->gnssDeleteAidingData(data);
  697. } else {
  698. LOC_LOGE("%s:%d]: No gnss interface available for Location Control API client %p ",
  699. __func__, __LINE__, this);
  700. }
  701. pthread_mutex_unlock(&gDataMutex);
  702. return id;
  703. }
  704. uint32_t LocationControlAPI::configConstellations(
  705. const GnssSvTypeConfig& constellationEnablementConfig,
  706. const GnssSvIdConfig& blacklistSvConfig) {
  707. uint32_t id = 0;
  708. pthread_mutex_lock(&gDataMutex);
  709. if (gData.gnssInterface != NULL) {
  710. id = gData.gnssInterface->gnssUpdateSvConfig(
  711. constellationEnablementConfig, blacklistSvConfig);
  712. } else {
  713. LOC_LOGe("No gnss interface available for Location Control API");
  714. }
  715. pthread_mutex_unlock(&gDataMutex);
  716. return id;
  717. }
  718. uint32_t LocationControlAPI::configConstellationSecondaryBand(
  719. const GnssSvTypeConfig& secondaryBandConfig) {
  720. uint32_t id = 0;
  721. pthread_mutex_lock(&gDataMutex);
  722. if (gData.gnssInterface != NULL) {
  723. id = gData.gnssInterface->gnssUpdateSecondaryBandConfig(secondaryBandConfig);
  724. } else {
  725. LOC_LOGe("No gnss interface available for Location Control API");
  726. }
  727. pthread_mutex_unlock(&gDataMutex);
  728. return id;
  729. }
  730. uint32_t LocationControlAPI::configConstrainedTimeUncertainty(
  731. bool enable, float tuncThreshold, uint32_t energyBudget) {
  732. uint32_t id = 0;
  733. pthread_mutex_lock(&gDataMutex);
  734. if (gData.gnssInterface != NULL) {
  735. id = gData.gnssInterface->setConstrainedTunc(enable,
  736. tuncThreshold,
  737. energyBudget);
  738. } else {
  739. LOC_LOGe("No gnss interface available for Location Control API");
  740. }
  741. pthread_mutex_unlock(&gDataMutex);
  742. return id;
  743. }
  744. uint32_t LocationControlAPI::configPositionAssistedClockEstimator(bool enable) {
  745. uint32_t id = 0;
  746. pthread_mutex_lock(&gDataMutex);
  747. if (gData.gnssInterface != NULL) {
  748. id = gData.gnssInterface->setPositionAssistedClockEstimator(enable);
  749. } else {
  750. LOC_LOGe("No gnss interface available for Location Control API");
  751. }
  752. pthread_mutex_unlock(&gDataMutex);
  753. return id;
  754. }
  755. uint32_t LocationControlAPI::configLeverArm(const LeverArmConfigInfo& configInfo) {
  756. uint32_t id = 0;
  757. pthread_mutex_lock(&gDataMutex);
  758. if (gData.gnssInterface != NULL) {
  759. id = gData.gnssInterface->configLeverArm(configInfo);
  760. } else {
  761. LOC_LOGe("No gnss interface available for Location Control API");
  762. }
  763. pthread_mutex_unlock(&gDataMutex);
  764. return id;
  765. }
  766. uint32_t LocationControlAPI::configRobustLocation(bool enable, bool enableForE911) {
  767. uint32_t id = 0;
  768. pthread_mutex_lock(&gDataMutex);
  769. if (gData.gnssInterface != NULL) {
  770. id = gData.gnssInterface->configRobustLocation(enable, enableForE911);
  771. } else {
  772. LOC_LOGe("No gnss interface available for Location Control API");
  773. }
  774. pthread_mutex_unlock(&gDataMutex);
  775. return id;
  776. }
  777. uint32_t LocationControlAPI::configMinGpsWeek(uint16_t minGpsWeek) {
  778. uint32_t id = 0;
  779. pthread_mutex_lock(&gDataMutex);
  780. if (gData.gnssInterface != NULL) {
  781. id = gData.gnssInterface->configMinGpsWeek(minGpsWeek);
  782. } else {
  783. LOC_LOGe("No gnss interface available for Location Control API");
  784. }
  785. pthread_mutex_unlock(&gDataMutex);
  786. return id;
  787. }
  788. uint32_t LocationControlAPI::configDeadReckoningEngineParams(
  789. const DeadReckoningEngineConfig& dreConfig) {
  790. uint32_t id = 0;
  791. pthread_mutex_lock(&gDataMutex);
  792. if (gData.gnssInterface != NULL) {
  793. id = gData.gnssInterface->configDeadReckoningEngineParams(dreConfig);
  794. } else {
  795. LOC_LOGe("No gnss interface available for Location Control API");
  796. }
  797. pthread_mutex_unlock(&gDataMutex);
  798. return id;
  799. }
  800. uint32_t LocationControlAPI::configEngineRunState(
  801. PositioningEngineMask engType, LocEngineRunState engState) {
  802. uint32_t id = 0;
  803. pthread_mutex_lock(&gDataMutex);
  804. if (gData.gnssInterface != NULL) {
  805. id = gData.gnssInterface->configEngineRunState(engType, engState);
  806. } else {
  807. LOC_LOGe("No gnss interface available for Location Control API");
  808. }
  809. pthread_mutex_unlock(&gDataMutex);
  810. return id;
  811. }
  812. uint32_t LocationControlAPI::setOptInStatus(bool userConsent) {
  813. uint32_t id = 0;
  814. pthread_mutex_lock(&gDataMutex);
  815. if (gData.gnssInterface != NULL) {
  816. id = gData.gnssInterface->setOptInStatus(userConsent);
  817. } else {
  818. LOC_LOGe("No gnss interface available for Location Control API");
  819. }
  820. pthread_mutex_unlock(&gDataMutex);
  821. return id;
  822. }
  823. uint32_t LocationControlAPI::configOutputNmeaTypes(
  824. GnssNmeaTypesMask enabledNmeaTypes) {
  825. uint32_t id = 0;
  826. pthread_mutex_lock(&gDataMutex);
  827. if (gData.gnssInterface != NULL) {
  828. id = gData.gnssInterface->configOutputNmeaTypes(enabledNmeaTypes);
  829. } else {
  830. LOC_LOGe("No gnss interface available for Location Control API");
  831. }
  832. pthread_mutex_unlock(&gDataMutex);
  833. return id;
  834. }
  835. void LocationControlAPI::powerStateEvent(PowerStateType powerState) {
  836. pthread_mutex_lock(&gDataMutex);
  837. LOC_LOGv("--> entry, powerState: %d", powerState);
  838. if (NULL != gData.gnssInterface) {
  839. gData.gnssInterface->updateSystemPowerState(powerState);
  840. } else {
  841. LOC_LOGv("No gnss interface available.");
  842. }
  843. if (NULL != gData.geofenceInterface) {
  844. gData.geofenceInterface->updateSystemPowerState(powerState);
  845. } else {
  846. LOC_LOGv("No geofence interface available.");
  847. }
  848. if (NULL != gData.batchingInterface) {
  849. gData.batchingInterface->updateSystemPowerState(powerState);
  850. } else {
  851. LOC_LOGv("No batching interface available.");
  852. }
  853. pthread_mutex_unlock(&gDataMutex);
  854. }