GeofenceAdapter.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /* Copyright (c) 2013-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_TAG "LocSvc_GeofenceAdapter"
  59. #include <GeofenceAdapter.h>
  60. #include "loc_log.h"
  61. #include <log_util.h>
  62. #include <string>
  63. using namespace loc_core;
  64. GeofenceAdapter::GeofenceAdapter() :
  65. LocAdapterBase(0,
  66. LocContext::getLocContext(LocContext::mLocationHalName),
  67. true /*isMaster*/, nullptr, true),
  68. mSystemPowerState(POWER_STATE_UNKNOWN)
  69. {
  70. LOC_LOGD("%s]: Constructor", __func__);
  71. // at last step, let us inform adapater base that we are done
  72. // with initialization, e.g.: ready to process handleEngineUpEvent
  73. doneInit();
  74. }
  75. void
  76. GeofenceAdapter::stopClientSessions(LocationAPI* client, bool eraseSession)
  77. {
  78. LOC_LOGD("%s]: client %p", __func__, client);
  79. for (auto it = mGeofenceIds.begin(); it != mGeofenceIds.end();) {
  80. uint32_t hwId = it->second;
  81. GeofenceKey key(it->first);
  82. if (client == key.client) {
  83. if (eraseSession)
  84. it = mGeofenceIds.erase(it);
  85. mLocApi->removeGeofence(hwId, key.id,
  86. new LocApiResponse(*getContext(),
  87. [this, hwId, eraseSession] (LocationError err) {
  88. if (LOCATION_ERROR_SUCCESS == err) {
  89. auto it2 = mGeofences.find(hwId);
  90. if (it2 != mGeofences.end()) {
  91. if (eraseSession)
  92. mGeofences.erase(it2);
  93. } else {
  94. LOC_LOGE("%s]:geofence item to erase not found. hwId %u", __func__, hwId);
  95. }
  96. }
  97. }));
  98. continue;
  99. }
  100. ++it; // increment only when not erasing an iterator
  101. }
  102. }
  103. void
  104. GeofenceAdapter::updateClientsEventMask()
  105. {
  106. LOC_API_ADAPTER_EVENT_MASK_T mask = 0;
  107. for (auto it=mClientData.begin(); it != mClientData.end(); ++it) {
  108. if (it->second.geofenceBreachCb != nullptr) {
  109. mask |= LOC_API_ADAPTER_BIT_BATCHED_GENFENCE_BREACH_REPORT;
  110. mask |= LOC_API_ADAPTER_BIT_REPORT_GENFENCE_DWELL;
  111. }
  112. if (it->second.geofenceStatusCb != nullptr) {
  113. mask |= LOC_API_ADAPTER_BIT_GEOFENCE_GEN_ALERT;
  114. }
  115. }
  116. updateEvtMask(mask, LOC_REGISTRATION_MASK_SET);
  117. }
  118. LocationError
  119. GeofenceAdapter::getHwIdFromClient(LocationAPI* client, uint32_t clientId, uint32_t& hwId)
  120. {
  121. GeofenceKey key(client, clientId);
  122. auto it = mGeofenceIds.find(key);
  123. if (it != mGeofenceIds.end()) {
  124. hwId = it->second;
  125. return LOCATION_ERROR_SUCCESS;
  126. }
  127. return LOCATION_ERROR_ID_UNKNOWN;
  128. }
  129. LocationError
  130. GeofenceAdapter::getGeofenceKeyFromHwId(uint32_t hwId, GeofenceKey& key)
  131. {
  132. auto it = mGeofences.find(hwId);
  133. if (it != mGeofences.end()) {
  134. key = it->second.key;
  135. return LOCATION_ERROR_SUCCESS;
  136. }
  137. return LOCATION_ERROR_ID_UNKNOWN;
  138. }
  139. void
  140. GeofenceAdapter::handleEngineUpEvent()
  141. {
  142. struct MsgSSREvent : public LocMsg {
  143. GeofenceAdapter& mAdapter;
  144. inline MsgSSREvent(GeofenceAdapter& adapter) :
  145. LocMsg(),
  146. mAdapter(adapter) {}
  147. virtual void proc() const {
  148. mAdapter.setEngineCapabilitiesKnown(true);
  149. mAdapter.broadcastCapabilities(mAdapter.getCapabilities());
  150. if ((POWER_STATE_SUSPEND != mAdapter.mSystemPowerState) &&
  151. POWER_STATE_SHUTDOWN != mAdapter.mSystemPowerState) {
  152. mAdapter.restartGeofences();
  153. }
  154. for (auto msg: mAdapter.mPendingMsgs) {
  155. mAdapter.sendMsg(msg);
  156. }
  157. mAdapter.mPendingMsgs.clear();
  158. }
  159. };
  160. sendMsg(new MsgSSREvent(*this));
  161. }
  162. void
  163. GeofenceAdapter::restartGeofences()
  164. {
  165. if (mGeofences.empty()) {
  166. return;
  167. }
  168. GeofencesMap oldGeofences(mGeofences);
  169. mGeofences.clear();
  170. mGeofenceIds.clear();
  171. for (auto it = oldGeofences.begin(); it != oldGeofences.end(); it++) {
  172. GeofenceObject object = it->second;
  173. GeofenceOption options = {sizeof(GeofenceOption),
  174. object.breachMask,
  175. object.responsiveness,
  176. object.dwellTime,
  177. object.confidence};
  178. GeofenceInfo info = {sizeof(GeofenceInfo),
  179. object.latitude,
  180. object.longitude,
  181. object.radius};
  182. mLocApi->addGeofence(object.key.id,
  183. options,
  184. info,
  185. new LocApiResponseData<LocApiGeofenceData>(*getContext(),
  186. [this, object, options, info] (LocationError err, LocApiGeofenceData data) {
  187. if (LOCATION_ERROR_SUCCESS == err) {
  188. if (true == object.paused) {
  189. mLocApi->pauseGeofence(data.hwId, object.key.id,
  190. new LocApiResponse(*getContext(), [] (LocationError err __unused) {}));
  191. }
  192. saveGeofenceItem(object.key.client, object.key.id, data.hwId, options, info);
  193. }
  194. }));
  195. }
  196. }
  197. void
  198. GeofenceAdapter::reportResponse(LocationAPI* client, size_t count, LocationError* errs,
  199. uint32_t* ids)
  200. {
  201. IF_LOC_LOGD {
  202. std::string idsString = "[";
  203. std::string errsString = "[";
  204. if (NULL != ids && NULL != errs) {
  205. for (size_t i=0; i < count; ++i) {
  206. idsString += std::to_string(ids[i]) + " ";
  207. errsString += std::to_string(errs[i]) + " ";
  208. }
  209. }
  210. idsString += "]";
  211. errsString += "]";
  212. LOC_LOGD("%s]: client %p ids %s errs %s",
  213. __func__, client, idsString.c_str(), errsString.c_str());
  214. }
  215. auto it = mClientData.find(client);
  216. if (it != mClientData.end() && it->second.collectiveResponseCb != nullptr) {
  217. it->second.collectiveResponseCb(count, errs, ids);
  218. } else {
  219. LOC_LOGE("%s]: client %p response not found in info", __func__, client);
  220. }
  221. }
  222. uint32_t*
  223. GeofenceAdapter::addGeofencesCommand(LocationAPI* client, size_t count, GeofenceOption* options,
  224. GeofenceInfo* infos)
  225. {
  226. LOC_LOGD("%s]: client %p count %zu", __func__, client, count);
  227. struct MsgAddGeofences : public LocMsg {
  228. GeofenceAdapter& mAdapter;
  229. LocApiBase& mApi;
  230. LocationAPI* mClient;
  231. size_t mCount;
  232. uint32_t* mIds;
  233. GeofenceOption* mOptions;
  234. GeofenceInfo* mInfos;
  235. inline MsgAddGeofences(GeofenceAdapter& adapter,
  236. LocApiBase& api,
  237. LocationAPI* client,
  238. size_t count,
  239. uint32_t* ids,
  240. GeofenceOption* options,
  241. GeofenceInfo* infos) :
  242. LocMsg(),
  243. mAdapter(adapter),
  244. mApi(api),
  245. mClient(client),
  246. mCount(count),
  247. mIds(ids),
  248. mOptions(options),
  249. mInfos(infos) {}
  250. inline virtual void proc() const {
  251. LocationError* errs = new LocationError[mCount];
  252. if (nullptr == errs) {
  253. LOC_LOGE("%s]: new failed to allocate errs", __func__);
  254. return;
  255. }
  256. for (size_t i=0; i < mCount; ++i) {
  257. if (NULL == mIds || NULL == mOptions || NULL == mInfos) {
  258. errs[i] = LOCATION_ERROR_INVALID_PARAMETER;
  259. } else {
  260. mApi.addToCallQueue(new LocApiResponse(*mAdapter.getContext(),
  261. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient,
  262. mOptions = mOptions, mInfos = mInfos, mIds = mIds, &mApi = mApi,
  263. errs, i] (LocationError err __unused) {
  264. mApi.addGeofence(mIds[i], mOptions[i], mInfos[i],
  265. new LocApiResponseData<LocApiGeofenceData>(*mAdapter.getContext(),
  266. [&mAdapter = mAdapter, mOptions = mOptions, mClient = mClient,
  267. mCount = mCount, mIds = mIds, mInfos = mInfos, errs, i]
  268. (LocationError err, LocApiGeofenceData data) {
  269. if (LOCATION_ERROR_SUCCESS == err) {
  270. mAdapter.saveGeofenceItem(mClient,
  271. mIds[i],
  272. data.hwId,
  273. mOptions[i],
  274. mInfos[i]);
  275. }
  276. errs[i] = err;
  277. // Send aggregated response on last item and cleanup
  278. if (i == mCount-1) {
  279. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  280. delete[] errs;
  281. delete[] mIds;
  282. delete[] mOptions;
  283. delete[] mInfos;
  284. }
  285. }));
  286. }));
  287. }
  288. }
  289. }
  290. };
  291. if (0 == count) {
  292. return NULL;
  293. }
  294. uint32_t* ids = new uint32_t[count];
  295. if (nullptr == ids) {
  296. LOC_LOGE("%s]: new failed to allocate ids", __func__);
  297. return NULL;
  298. }
  299. if (NULL != ids) {
  300. for (size_t i=0; i < count; ++i) {
  301. ids[i] = generateSessionId();
  302. }
  303. }
  304. GeofenceOption* optionsCopy;
  305. if (options == NULL) {
  306. optionsCopy = NULL;
  307. } else {
  308. optionsCopy = new GeofenceOption[count];
  309. if (nullptr == optionsCopy) {
  310. LOC_LOGE("%s]: new failed to allocate optionsCopy", __func__);
  311. return NULL;
  312. }
  313. COPY_IF_NOT_NULL(optionsCopy, options, count);
  314. }
  315. GeofenceInfo* infosCopy;
  316. if (infos == NULL) {
  317. infosCopy = NULL;
  318. } else {
  319. infosCopy = new GeofenceInfo[count];
  320. if (nullptr == infosCopy) {
  321. LOC_LOGE("%s]: new failed to allocate infosCopy", __func__);
  322. return NULL;
  323. }
  324. COPY_IF_NOT_NULL(infosCopy, infos, count);
  325. }
  326. sendMsg(new MsgAddGeofences(*this, *mLocApi, client, count, ids, optionsCopy, infosCopy));
  327. return ids;
  328. }
  329. void
  330. GeofenceAdapter::removeGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids)
  331. {
  332. LOC_LOGD("%s]: client %p count %zu", __func__, client, count);
  333. struct MsgRemoveGeofences : public LocMsg {
  334. GeofenceAdapter& mAdapter;
  335. LocApiBase& mApi;
  336. LocationAPI* mClient;
  337. size_t mCount;
  338. uint32_t* mIds;
  339. inline MsgRemoveGeofences(GeofenceAdapter& adapter,
  340. LocApiBase& api,
  341. LocationAPI* client,
  342. size_t count,
  343. uint32_t* ids) :
  344. LocMsg(),
  345. mAdapter(adapter),
  346. mApi(api),
  347. mClient(client),
  348. mCount(count),
  349. mIds(ids) {}
  350. inline virtual void proc() const {
  351. LocationError* errs = new LocationError[mCount];
  352. if (nullptr == errs) {
  353. LOC_LOGE("%s]: new failed to allocate errs", __func__);
  354. return;
  355. }
  356. for (size_t i=0; i < mCount; ++i) {
  357. mApi.addToCallQueue(new LocApiResponse(*mAdapter.getContext(),
  358. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, mIds = mIds,
  359. &mApi = mApi, errs, i] (LocationError err __unused) {
  360. uint32_t hwId = 0;
  361. errs[i] = mAdapter.getHwIdFromClient(mClient, mIds[i], hwId);
  362. if (LOCATION_ERROR_SUCCESS == errs[i]) {
  363. mApi.removeGeofence(hwId, mIds[i],
  364. new LocApiResponse(*mAdapter.getContext(),
  365. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, mIds = mIds,
  366. hwId, errs, i] (LocationError err ) {
  367. if (LOCATION_ERROR_SUCCESS == err) {
  368. mAdapter.removeGeofenceItem(hwId);
  369. }
  370. errs[i] = err;
  371. // Send aggregated response on last item and cleanup
  372. if (i == mCount-1) {
  373. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  374. delete[] errs;
  375. delete[] mIds;
  376. }
  377. }));
  378. } else {
  379. // Send aggregated response on last item and cleanup
  380. if (i == mCount-1) {
  381. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  382. delete[] errs;
  383. delete[] mIds;
  384. }
  385. }
  386. }));
  387. }
  388. }
  389. };
  390. if (0 == count) {
  391. return;
  392. }
  393. uint32_t* idsCopy = new uint32_t[count];
  394. if (nullptr == idsCopy) {
  395. LOC_LOGE("%s]: new failed to allocate idsCopy", __func__);
  396. return;
  397. }
  398. COPY_IF_NOT_NULL(idsCopy, ids, count);
  399. sendMsg(new MsgRemoveGeofences(*this, *mLocApi, client, count, idsCopy));
  400. }
  401. void
  402. GeofenceAdapter::pauseGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids)
  403. {
  404. LOC_LOGD("%s]: client %p count %zu", __func__, client, count);
  405. struct MsgPauseGeofences : public LocMsg {
  406. GeofenceAdapter& mAdapter;
  407. LocApiBase& mApi;
  408. LocationAPI* mClient;
  409. size_t mCount;
  410. uint32_t* mIds;
  411. inline MsgPauseGeofences(GeofenceAdapter& adapter,
  412. LocApiBase& api,
  413. LocationAPI* client,
  414. size_t count,
  415. uint32_t* ids) :
  416. LocMsg(),
  417. mAdapter(adapter),
  418. mApi(api),
  419. mClient(client),
  420. mCount(count),
  421. mIds(ids) {}
  422. inline virtual void proc() const {
  423. LocationError* errs = new LocationError[mCount];
  424. if (nullptr == errs) {
  425. LOC_LOGE("%s]: new failed to allocate errs", __func__);
  426. return;
  427. }
  428. for (size_t i=0; i < mCount; ++i) {
  429. mApi.addToCallQueue(new LocApiResponse(*mAdapter.getContext(),
  430. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, mIds = mIds,
  431. &mApi = mApi, errs, i] (LocationError err __unused) {
  432. uint32_t hwId = 0;
  433. errs[i] = mAdapter.getHwIdFromClient(mClient, mIds[i], hwId);
  434. if (LOCATION_ERROR_SUCCESS == errs[i]) {
  435. mApi.pauseGeofence(hwId, mIds[i], new LocApiResponse(*mAdapter.getContext(),
  436. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, mIds = mIds,
  437. hwId, errs, i] (LocationError err ) {
  438. if (LOCATION_ERROR_SUCCESS == err) {
  439. mAdapter.pauseGeofenceItem(hwId);
  440. }
  441. errs[i] = err;
  442. // Send aggregated response on last item and cleanup
  443. if (i == mCount-1) {
  444. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  445. delete[] errs;
  446. delete[] mIds;
  447. }
  448. }));
  449. } else {
  450. // Send aggregated response on last item and cleanup
  451. if (i == mCount-1) {
  452. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  453. delete[] errs;
  454. delete[] mIds;
  455. }
  456. }
  457. }));
  458. }
  459. }
  460. };
  461. if (0 == count) {
  462. return;
  463. }
  464. uint32_t* idsCopy = new uint32_t[count];
  465. if (nullptr == idsCopy) {
  466. LOC_LOGE("%s]: new failed to allocate idsCopy", __func__);
  467. return;
  468. }
  469. COPY_IF_NOT_NULL(idsCopy, ids, count);
  470. sendMsg(new MsgPauseGeofences(*this, *mLocApi, client, count, idsCopy));
  471. }
  472. void
  473. GeofenceAdapter::resumeGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids)
  474. {
  475. LOC_LOGD("%s]: client %p count %zu", __func__, client, count);
  476. struct MsgResumeGeofences : public LocMsg {
  477. GeofenceAdapter& mAdapter;
  478. LocApiBase& mApi;
  479. LocationAPI* mClient;
  480. size_t mCount;
  481. uint32_t* mIds;
  482. inline MsgResumeGeofences(GeofenceAdapter& adapter,
  483. LocApiBase& api,
  484. LocationAPI* client,
  485. size_t count,
  486. uint32_t* ids) :
  487. LocMsg(),
  488. mAdapter(adapter),
  489. mApi(api),
  490. mClient(client),
  491. mCount(count),
  492. mIds(ids) {}
  493. inline virtual void proc() const {
  494. LocationError* errs = new LocationError[mCount];
  495. if (nullptr == errs) {
  496. LOC_LOGE("%s]: new failed to allocate errs", __func__);
  497. return;
  498. }
  499. for (size_t i=0; i < mCount; ++i) {
  500. mApi.addToCallQueue(new LocApiResponse(*mAdapter.getContext(),
  501. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, mIds = mIds,
  502. &mApi = mApi, errs, i] (LocationError err __unused) {
  503. uint32_t hwId = 0;
  504. errs[i] = mAdapter.getHwIdFromClient(mClient, mIds[i], hwId);
  505. if (LOCATION_ERROR_SUCCESS == errs[i]) {
  506. mApi.resumeGeofence(hwId, mIds[i],
  507. new LocApiResponse(*mAdapter.getContext(),
  508. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, hwId,
  509. errs, mIds = mIds, i] (LocationError err ) {
  510. if (LOCATION_ERROR_SUCCESS == err) {
  511. errs[i] = err;
  512. mAdapter.resumeGeofenceItem(hwId);
  513. // Send aggregated response on last item and cleanup
  514. if (i == mCount-1) {
  515. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  516. delete[] errs;
  517. delete[] mIds;
  518. }
  519. }
  520. }));
  521. } else {
  522. // Send aggregated response on last item and cleanup
  523. if (i == mCount-1) {
  524. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  525. delete[] errs;
  526. delete[] mIds;
  527. }
  528. }
  529. }));
  530. }
  531. }
  532. };
  533. if (0 == count) {
  534. return;
  535. }
  536. uint32_t* idsCopy = new uint32_t[count];
  537. if (nullptr == idsCopy) {
  538. LOC_LOGE("%s]: new failed to allocate idsCopy", __func__);
  539. return;
  540. }
  541. COPY_IF_NOT_NULL(idsCopy, ids, count);
  542. sendMsg(new MsgResumeGeofences(*this, *mLocApi, client, count, idsCopy));
  543. }
  544. void
  545. GeofenceAdapter::modifyGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids,
  546. GeofenceOption* options)
  547. {
  548. LOC_LOGD("%s]: client %p count %zu", __func__, client, count);
  549. struct MsgModifyGeofences : public LocMsg {
  550. GeofenceAdapter& mAdapter;
  551. LocApiBase& mApi;
  552. LocationAPI* mClient;
  553. size_t mCount;
  554. uint32_t* mIds;
  555. GeofenceOption* mOptions;
  556. inline MsgModifyGeofences(GeofenceAdapter& adapter,
  557. LocApiBase& api,
  558. LocationAPI* client,
  559. size_t count,
  560. uint32_t* ids,
  561. GeofenceOption* options) :
  562. LocMsg(),
  563. mAdapter(adapter),
  564. mApi(api),
  565. mClient(client),
  566. mCount(count),
  567. mIds(ids),
  568. mOptions(options) {}
  569. inline virtual void proc() const {
  570. LocationError* errs = new LocationError[mCount];
  571. if (nullptr == errs) {
  572. LOC_LOGE("%s]: new failed to allocate errs", __func__);
  573. return;
  574. }
  575. for (size_t i=0; i < mCount; ++i) {
  576. if (NULL == mIds || NULL == mOptions) {
  577. errs[i] = LOCATION_ERROR_INVALID_PARAMETER;
  578. } else {
  579. mApi.addToCallQueue(new LocApiResponse(*mAdapter.getContext(),
  580. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient, mIds = mIds,
  581. &mApi = mApi, mOptions = mOptions, errs, i] (LocationError err __unused) {
  582. uint32_t hwId = 0;
  583. errs[i] = mAdapter.getHwIdFromClient(mClient, mIds[i], hwId);
  584. if (LOCATION_ERROR_SUCCESS == errs[i]) {
  585. mApi.modifyGeofence(hwId, mIds[i], mOptions[i],
  586. new LocApiResponse(*mAdapter.getContext(),
  587. [&mAdapter = mAdapter, mCount = mCount, mClient = mClient,
  588. mIds = mIds, mOptions = mOptions, hwId, errs, i]
  589. (LocationError err ) {
  590. if (LOCATION_ERROR_SUCCESS == err) {
  591. errs[i] = err;
  592. mAdapter.modifyGeofenceItem(hwId, mOptions[i]);
  593. }
  594. // Send aggregated response on last item and cleanup
  595. if (i == mCount-1) {
  596. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  597. delete[] errs;
  598. delete[] mIds;
  599. delete[] mOptions;
  600. }
  601. }));
  602. } else {
  603. // Send aggregated response on last item and cleanup
  604. if (i == mCount-1) {
  605. mAdapter.reportResponse(mClient, mCount, errs, mIds);
  606. delete[] errs;
  607. delete[] mIds;
  608. delete[] mOptions;
  609. }
  610. }
  611. }));
  612. }
  613. }
  614. }
  615. };
  616. if (0 == count) {
  617. return;
  618. }
  619. uint32_t* idsCopy = new uint32_t[count];
  620. if (nullptr == idsCopy) {
  621. LOC_LOGE("%s]: new failed to allocate idsCopy", __func__);
  622. return;
  623. }
  624. COPY_IF_NOT_NULL(idsCopy, ids, count);
  625. GeofenceOption* optionsCopy;
  626. if (options == NULL) {
  627. optionsCopy = NULL;
  628. } else {
  629. optionsCopy = new GeofenceOption[count];
  630. if (nullptr == optionsCopy) {
  631. LOC_LOGE("%s]: new failed to allocate optionsCopy", __func__);
  632. return;
  633. }
  634. COPY_IF_NOT_NULL(optionsCopy, options, count);
  635. }
  636. sendMsg(new MsgModifyGeofences(*this, *mLocApi, client, count, idsCopy, optionsCopy));
  637. }
  638. void
  639. GeofenceAdapter::saveGeofenceItem(LocationAPI* client, uint32_t clientId, uint32_t hwId,
  640. const GeofenceOption& options, const GeofenceInfo& info)
  641. {
  642. LOC_LOGD("%s]: hwId %u client %p clientId %u", __func__, hwId, client, clientId);
  643. GeofenceKey key(client, clientId);
  644. GeofenceObject object = {key,
  645. options.breachTypeMask,
  646. options.responsiveness,
  647. options.dwellTime,
  648. options.confidence,
  649. info.latitude,
  650. info.longitude,
  651. info.radius,
  652. false};
  653. mGeofences[hwId] = object;
  654. mGeofenceIds[key] = hwId;
  655. dump();
  656. }
  657. void
  658. GeofenceAdapter::removeGeofenceItem(uint32_t hwId)
  659. {
  660. GeofenceKey key;
  661. LocationError err = getGeofenceKeyFromHwId(hwId, key);
  662. if (LOCATION_ERROR_SUCCESS != err) {
  663. LOC_LOGE("%s]: can not find the key for hwId %u", __func__, hwId);
  664. } else {
  665. auto it1 = mGeofenceIds.find(key);
  666. if (it1 != mGeofenceIds.end()) {
  667. mGeofenceIds.erase(it1);
  668. auto it2 = mGeofences.find(hwId);
  669. if (it2 != mGeofences.end()) {
  670. mGeofences.erase(it2);
  671. dump();
  672. } else {
  673. LOC_LOGE("%s]:geofence item to erase not found. hwId %u", __func__, hwId);
  674. }
  675. } else {
  676. LOC_LOGE("%s]: geofence item to erase not found. hwId %u", __func__, hwId);
  677. }
  678. }
  679. }
  680. void
  681. GeofenceAdapter::pauseGeofenceItem(uint32_t hwId)
  682. {
  683. auto it = mGeofences.find(hwId);
  684. if (it != mGeofences.end()) {
  685. it->second.paused = true;
  686. dump();
  687. } else {
  688. LOC_LOGE("%s]: geofence item to pause not found. hwId %u", __func__, hwId);
  689. }
  690. }
  691. void
  692. GeofenceAdapter::resumeGeofenceItem(uint32_t hwId)
  693. {
  694. auto it = mGeofences.find(hwId);
  695. if (it != mGeofences.end()) {
  696. it->second.paused = false;
  697. dump();
  698. } else {
  699. LOC_LOGE("%s]: geofence item to resume not found. hwId %u", __func__, hwId);
  700. }
  701. }
  702. void
  703. GeofenceAdapter::modifyGeofenceItem(uint32_t hwId, const GeofenceOption& options)
  704. {
  705. auto it = mGeofences.find(hwId);
  706. if (it != mGeofences.end()) {
  707. it->second.breachMask = options.breachTypeMask;
  708. it->second.responsiveness = options.responsiveness;
  709. it->second.dwellTime = options.dwellTime;
  710. it->second.confidence = options.confidence;
  711. dump();
  712. } else {
  713. LOC_LOGE("%s]: geofence item to modify not found. hwId %u", __func__, hwId);
  714. }
  715. }
  716. void
  717. GeofenceAdapter::geofenceBreachEvent(size_t count, uint32_t* hwIds, Location& location,
  718. GeofenceBreachType breachType, uint64_t timestamp)
  719. {
  720. IF_LOC_LOGD {
  721. std::string idsString = "[";
  722. if (NULL != hwIds) {
  723. for (size_t i=0; i < count; ++i) {
  724. idsString += std::to_string(hwIds[i]) + " ";
  725. }
  726. }
  727. idsString += "]";
  728. LOC_LOGD("%s]: breachType %u count %zu ids %s",
  729. __func__, breachType, count, idsString.c_str());
  730. }
  731. if (0 == count || NULL == hwIds)
  732. return;
  733. struct MsgGeofenceBreach : public LocMsg {
  734. GeofenceAdapter& mAdapter;
  735. size_t mCount;
  736. uint32_t* mHwIds;
  737. Location mLocation;
  738. GeofenceBreachType mBreachType;
  739. uint64_t mTimestamp;
  740. inline MsgGeofenceBreach(GeofenceAdapter& adapter,
  741. size_t count,
  742. uint32_t* hwIds,
  743. Location& location,
  744. GeofenceBreachType breachType,
  745. uint64_t timestamp) :
  746. LocMsg(),
  747. mAdapter(adapter),
  748. mCount(count),
  749. mHwIds(new uint32_t[count]),
  750. mLocation(location),
  751. mBreachType(breachType),
  752. mTimestamp(timestamp)
  753. {
  754. if (nullptr == mHwIds) {
  755. LOC_LOGE("%s]: new failed to allocate mHwIds", __func__);
  756. return;
  757. }
  758. COPY_IF_NOT_NULL(mHwIds, hwIds, mCount);
  759. }
  760. inline virtual ~MsgGeofenceBreach() {
  761. delete[] mHwIds;
  762. }
  763. inline virtual void proc() const {
  764. mAdapter.geofenceBreach(mCount, mHwIds, mLocation, mBreachType, mTimestamp);
  765. }
  766. };
  767. sendMsg(new MsgGeofenceBreach(*this, count, hwIds, location, breachType, timestamp));
  768. }
  769. void
  770. GeofenceAdapter::geofenceBreach(size_t count, uint32_t* hwIds, const Location& location,
  771. GeofenceBreachType breachType, uint64_t timestamp)
  772. {
  773. for (auto it = mClientData.begin(); it != mClientData.end(); ++it) {
  774. uint32_t* clientIds = new uint32_t[count];
  775. if (nullptr == clientIds) {
  776. return;
  777. }
  778. uint32_t index = 0;
  779. for (size_t i=0; i < count; ++i) {
  780. GeofenceKey key;
  781. LocationError err = getGeofenceKeyFromHwId(hwIds[i], key);
  782. if (LOCATION_ERROR_SUCCESS == err) {
  783. if (key.client == it->first) {
  784. clientIds[index++] = key.id;
  785. }
  786. }
  787. }
  788. if (index > 0 && it->second.geofenceBreachCb != nullptr) {
  789. GeofenceBreachNotification notify = {sizeof(GeofenceBreachNotification),
  790. index,
  791. clientIds,
  792. location,
  793. breachType,
  794. timestamp};
  795. it->second.geofenceBreachCb(notify);
  796. }
  797. delete[] clientIds;
  798. }
  799. }
  800. void
  801. GeofenceAdapter::geofenceStatusEvent(GeofenceStatusAvailable available)
  802. {
  803. LOC_LOGD("%s]: available %u ", __func__, available);
  804. struct MsgGeofenceStatus : public LocMsg {
  805. GeofenceAdapter& mAdapter;
  806. GeofenceStatusAvailable mAvailable;
  807. inline MsgGeofenceStatus(GeofenceAdapter& adapter,
  808. GeofenceStatusAvailable available) :
  809. LocMsg(),
  810. mAdapter(adapter),
  811. mAvailable(available) {}
  812. inline virtual void proc() const {
  813. mAdapter.geofenceStatus(mAvailable);
  814. }
  815. };
  816. sendMsg(new MsgGeofenceStatus(*this, available));
  817. }
  818. void
  819. GeofenceAdapter::geofenceStatus(GeofenceStatusAvailable available)
  820. {
  821. for (auto it = mClientData.begin(); it != mClientData.end(); ++it) {
  822. if (it->second.geofenceStatusCb != nullptr) {
  823. GeofenceStatusNotification notify = {sizeof(GeofenceStatusNotification),
  824. available,
  825. LOCATION_TECHNOLOGY_TYPE_GNSS};
  826. it->second.geofenceStatusCb(notify);
  827. }
  828. }
  829. }
  830. void
  831. GeofenceAdapter::updateSystemPowerStateCommand(PowerStateType powerState)
  832. {
  833. LOC_LOGD("%s]: powerState: %d", __func__, powerState);
  834. struct MsgUpdateSystemPowerState : public LocMsg {
  835. GeofenceAdapter& mAdapter;
  836. PowerStateType mPowerState;
  837. inline MsgUpdateSystemPowerState(GeofenceAdapter& adapter,
  838. PowerStateType powerState) :
  839. mAdapter(adapter),
  840. mPowerState(powerState) {}
  841. inline virtual void proc() const {
  842. mAdapter.updateSystemPowerState(mPowerState);
  843. }
  844. };
  845. sendMsg(new MsgUpdateSystemPowerState(*this, powerState));
  846. }
  847. void
  848. GeofenceAdapter::pauseOrResumeGeofences(bool pauseOrResume /*false - pause, true - resume*/)
  849. {
  850. for (auto it = mGeofenceIds.begin(); it != mGeofenceIds.end(); ++it) {
  851. uint32_t hwId = it->second;
  852. if (false == pauseOrResume) {
  853. mLocApi->pauseGeofence(it->second, it->first.id, new LocApiResponse(*getContext(),
  854. [&mAdapter = *this, hwId = hwId] (LocationError err) {
  855. if (LOCATION_ERROR_SUCCESS == err) {
  856. mAdapter.pauseGeofenceItem(hwId);
  857. }
  858. }));
  859. } else {
  860. mLocApi->resumeGeofence(it->second, it->first.id, new LocApiResponse(*getContext(),
  861. [&mAdapter = *this, hwId = hwId] (LocationError err) {
  862. if (LOCATION_ERROR_SUCCESS == err) {
  863. mAdapter.resumeGeofenceItem(hwId);
  864. }
  865. }));
  866. }
  867. }
  868. }
  869. void
  870. GeofenceAdapter::updateSystemPowerState(PowerStateType systemPowerState)
  871. {
  872. if (POWER_STATE_UNKNOWN != systemPowerState) {
  873. mSystemPowerState = systemPowerState;
  874. /*Manage active GNSS sessions based on power event*/
  875. switch (systemPowerState){
  876. case POWER_STATE_SUSPEND:
  877. case POWER_STATE_SHUTDOWN:
  878. pauseOrResumeGeofences(false /*pause*/);
  879. LOC_LOGd("Pause all geoFences -- powerState: %d", systemPowerState);
  880. break;
  881. case POWER_STATE_RESUME:
  882. pauseOrResumeGeofences(true /*resume*/);
  883. LOC_LOGd("Resume all geoFences -- powerState: %d", systemPowerState);
  884. break;
  885. default:
  886. break;
  887. } // switch
  888. }
  889. }
  890. void
  891. GeofenceAdapter::dump()
  892. {
  893. IF_LOC_LOGV {
  894. LOC_LOGV(
  895. "HAL | hwId | mask | respon | latitude | longitude | radius | paused | Id | client");
  896. for (auto it = mGeofences.begin(); it != mGeofences.end(); ++it) {
  897. uint32_t hwId = it->first;
  898. GeofenceObject object = it->second;
  899. LOC_LOGV(" | %5u | %4u | %6u | %8.2f | %9.2f | %6.2f | %6u | %04x | %p ",
  900. hwId, object.breachMask, object.responsiveness,
  901. object.latitude, object.longitude, object.radius,
  902. object.paused, object.key.id, object.key.client);
  903. }
  904. }
  905. }