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