BatchingAdapter.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (c) 2017-2019, 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. #ifndef BATCHING_ADAPTER_H
  30. #define BATCHING_ADAPTER_H
  31. #include <LocAdapterBase.h>
  32. #include <LocContext.h>
  33. #include <LocationAPI.h>
  34. #include <map>
  35. using namespace loc_core;
  36. class BatchingAdapter : public LocAdapterBase {
  37. /* ==== BATCHING ======================================================================= */
  38. typedef struct {
  39. uint32_t accumulatedDistanceOngoingBatch;
  40. uint32_t accumulatedDistanceThisTrip;
  41. uint32_t accumulatedDistanceOnTripRestart;
  42. uint32_t tripDistance;
  43. uint32_t tripTBFInterval;
  44. } TripSessionStatus;
  45. typedef std::map<uint32_t, TripSessionStatus> TripSessionStatusMap;
  46. typedef std::map<LocationSessionKey, BatchingOptions> BatchingSessionMap;
  47. BatchingSessionMap mBatchingSessions;
  48. TripSessionStatusMap mTripSessions;
  49. uint32_t mOngoingTripDistance;
  50. uint32_t mOngoingTripTBFInterval;
  51. bool mTripWithOngoingTBFDropped;
  52. bool mTripWithOngoingTripDistanceDropped;
  53. PowerStateType mSystemPowerState;
  54. void startTripBatchingMultiplex(LocationAPI* client, uint32_t sessionId,
  55. const BatchingOptions& batchingOptions);
  56. void stopTripBatchingMultiplex(LocationAPI* client, uint32_t sessionId,
  57. bool restartNeeded,
  58. const BatchingOptions& batchOptions,
  59. bool eraseSession = true);
  60. inline void stopTripBatchingMultiplex(LocationAPI* client, uint32_t id,
  61. bool eraseSession = true) {
  62. BatchingOptions batchOptions;
  63. stopTripBatchingMultiplex(client, id, false, batchOptions, eraseSession);
  64. };
  65. void stopTripBatchingMultiplexCommon(LocationError err,
  66. LocationAPI* client,
  67. uint32_t sessionId,
  68. bool restartNeeded,
  69. const BatchingOptions& batchOptions,
  70. bool eraseSession = true);
  71. void restartTripBatching(bool queryAccumulatedDistance, uint32_t accDist = 0,
  72. uint32_t numbatchedPos = 0);
  73. void printTripReport();
  74. /* ==== CONFIGURATION ================================================================== */
  75. uint32_t mBatchingTimeout;
  76. uint32_t mBatchingAccuracy;
  77. size_t mBatchSize;
  78. size_t mTripBatchSize;
  79. protected:
  80. /* ==== CLIENT ========================================================================= */
  81. virtual void updateClientsEventMask();
  82. virtual void stopClientSessions(LocationAPI* client, bool eraseSession = true);
  83. public:
  84. BatchingAdapter();
  85. virtual ~BatchingAdapter() {}
  86. /* ==== SSR ============================================================================ */
  87. /* ======== EVENTS ====(Called from QMI Thread)========================================= */
  88. virtual void handleEngineUpEvent();
  89. /* ======== UTILITIES ================================================================== */
  90. void restartSessions();
  91. /* ==== BATCHING ======================================================================= */
  92. /* ======== COMMANDS ====(Called from Client Thread)==================================== */
  93. uint32_t startBatchingCommand(LocationAPI* client, BatchingOptions &batchOptions);
  94. void updateBatchingOptionsCommand(
  95. LocationAPI* client, uint32_t id, BatchingOptions& batchOptions);
  96. void stopBatchingCommand(LocationAPI* client, uint32_t id);
  97. void getBatchedLocationsCommand(LocationAPI* client, uint32_t id, size_t count);
  98. void updateSystemPowerStateCommand(PowerStateType systemPowerState);
  99. /* ======== RESPONSES ================================================================== */
  100. void reportResponse(LocationAPI* client, LocationError err, uint32_t sessionId);
  101. /* ======== UTILITIES ================================================================== */
  102. bool hasBatchingCallback(LocationAPI* client);
  103. bool isBatchingSession(LocationAPI* client, uint32_t sessionId);
  104. bool isTripSession(uint32_t sessionId);
  105. void saveBatchingSession(LocationAPI* client, uint32_t sessionId,
  106. const BatchingOptions& batchingOptions);
  107. void eraseBatchingSession(LocationAPI* client, uint32_t sessionId);
  108. uint32_t autoReportBatchingSessionsCount();
  109. void startBatching(LocationAPI* client, uint32_t sessionId,
  110. const BatchingOptions& batchingOptions);
  111. void stopBatching(LocationAPI* client, uint32_t sessionId, bool restartNeeded,
  112. const BatchingOptions& batchOptions, bool eraseSession = true);
  113. void stopBatching(LocationAPI* client, uint32_t sessionId, bool eraseSession = true) {
  114. BatchingOptions batchOptions;
  115. stopBatching(client, sessionId, false, batchOptions, eraseSession);
  116. };
  117. void suspendBatchingSessions();
  118. void updateSystemPowerState(PowerStateType systemPowerState);
  119. /* ==== REPORTS ======================================================================== */
  120. /* ======== EVENTS ====(Called from QMI Thread)========================================= */
  121. void reportLocationsEvent(const Location* locations, size_t count,
  122. BatchingMode batchingMode);
  123. void reportCompletedTripsEvent(uint32_t accumulatedDistance);
  124. void reportBatchStatusChangeEvent(BatchingStatus batchStatus);
  125. /* ======== UTILITIES ================================================================== */
  126. void reportLocations(Location* locations, size_t count, BatchingMode batchingMode);
  127. void reportBatchStatusChange(BatchingStatus batchStatus,
  128. std::list<uint32_t> & completedTripsList);
  129. /* ==== CONFIGURATION ================================================================== */
  130. /* ======== COMMANDS ====(Called from Client Thread)==================================== */
  131. void readConfigCommand();
  132. void setConfigCommand();
  133. /* ======== UTILITIES ================================================================== */
  134. void setBatchSize(size_t batchSize) { mBatchSize = batchSize; }
  135. size_t getBatchSize() { return mBatchSize; }
  136. void setTripBatchSize(size_t batchSize) { mTripBatchSize = batchSize; }
  137. size_t getTripBatchSize() { return mTripBatchSize; }
  138. void setBatchingTimeout(uint32_t batchingTimeout) { mBatchingTimeout = batchingTimeout; }
  139. uint32_t getBatchingTimeout() { return mBatchingTimeout; }
  140. void setBatchingAccuracy(uint32_t accuracy) { mBatchingAccuracy = accuracy; }
  141. uint32_t getBatchingAccuracy() { return mBatchingAccuracy; }
  142. };
  143. #endif /* BATCHING_ADAPTER_H */