blob: ac1ebe61685ff3e98331d45f4912d3159abaded8 [file] [log] [blame]
Saurabh Srivastavaa2f2fdc2020-07-10 16:46:16 +05301/* Copyright (c) 2017-2020 The Linux Foundation. All rights reserved.
Dante Russoc85c8ff2017-02-28 16:45:47 -08002 *
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 LOCATION_API_CLINET_BASE_H
30#define LOCATION_API_CLINET_BASE_H
31
32#include <stdint.h>
33#include <stdlib.h>
34#include <pthread.h>
35#include <queue>
36#include <map>
37
38#include "LocationAPI.h"
Kevin Tang61de97e2016-09-12 17:20:55 -070039#include <loc_pla.h>
40#include <log_util.h>
Dante Russoc85c8ff2017-02-28 16:45:47 -080041
42enum SESSION_MODE {
43 SESSION_MODE_NONE = 0,
44 SESSION_MODE_ON_FULL,
45 SESSION_MODE_ON_FIX,
Bhavna Sharma686a5c52017-05-02 14:29:46 -070046 SESSION_MODE_ON_TRIP_COMPLETED
Dante Russoc85c8ff2017-02-28 16:45:47 -080047};
48
49enum REQUEST_TYPE {
50 REQUEST_TRACKING = 0,
Baili Feng2df685d2017-07-20 17:02:26 +080051 REQUEST_SESSION,
Dante Russoc85c8ff2017-02-28 16:45:47 -080052 REQUEST_GEOFENCE,
53 REQUEST_NIRESPONSE,
Dante Russoc85c8ff2017-02-28 16:45:47 -080054 REQUEST_MAX,
55};
56
Baili Feng1a128bd2017-06-21 17:46:23 +080057enum CTRL_REQUEST_TYPE {
58 CTRL_REQUEST_DELETEAIDINGDATA = 0,
59 CTRL_REQUEST_CONTROL,
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +053060 CTRL_REQUEST_CONFIG_UPDATE,
61 CTRL_REQUEST_CONFIG_GET,
Baili Feng1a128bd2017-06-21 17:46:23 +080062 CTRL_REQUEST_MAX,
63};
64
65class LocationAPIClientBase;
66
67class LocationAPIRequest {
68public:
69 LocationAPIRequest() {}
70 virtual ~LocationAPIRequest() {}
Baili Feng2df685d2017-07-20 17:02:26 +080071 virtual void onResponse(LocationError /*error*/, uint32_t /*id*/) {}
Baili Feng1a128bd2017-06-21 17:46:23 +080072 virtual void onCollectiveResponse(
73 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
74};
75
76class RequestQueue {
77public:
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +053078 RequestQueue(): mSession(0), mSessionArrayPtr(nullptr) {
Baili Feng1a128bd2017-06-21 17:46:23 +080079 }
80 virtual ~RequestQueue() {
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +053081 reset((uint32_t)0);
Baili Feng1a128bd2017-06-21 17:46:23 +080082 }
Baili Feng2df685d2017-07-20 17:02:26 +080083 void inline setSession(uint32_t session) { mSession = session; }
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +053084 void inline setSessionArrayPtr(uint32_t* ptr) { mSessionArrayPtr = ptr; }
Baili Feng1a128bd2017-06-21 17:46:23 +080085 void reset(uint32_t session) {
86 LocationAPIRequest* request = nullptr;
87 while (!mQueue.empty()) {
88 request = mQueue.front();
89 mQueue.pop();
90 delete request;
91 }
92 mSession = session;
93 }
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +053094 void reset(uint32_t* sessionArrayPtr) {
95 reset((uint32_t)0);
96 mSessionArrayPtr = sessionArrayPtr;
97 }
Baili Feng1a128bd2017-06-21 17:46:23 +080098 void push(LocationAPIRequest* request) {
99 mQueue.push(request);
100 }
101 LocationAPIRequest* pop() {
102 LocationAPIRequest* request = nullptr;
103 if (!mQueue.empty()) {
104 request = mQueue.front();
105 mQueue.pop();
106 }
107 return request;
108 }
109 uint32_t getSession() { return mSession; }
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +0530110 uint32_t* getSessionArrayPtr() { return mSessionArrayPtr; }
Baili Feng1a128bd2017-06-21 17:46:23 +0800111private:
112 uint32_t mSession;
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +0530113 uint32_t* mSessionArrayPtr;
Baili Feng1a128bd2017-06-21 17:46:23 +0800114 std::queue<LocationAPIRequest*> mQueue;
115};
116
117class LocationAPIControlClient {
118public:
119 LocationAPIControlClient();
120 virtual ~LocationAPIControlClient();
121 LocationAPIControlClient(const LocationAPIControlClient&) = delete;
122 LocationAPIControlClient& operator=(const LocationAPIControlClient&) = delete;
123
124 LocationAPIRequest* getRequestBySession(uint32_t session);
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +0530125 LocationAPIRequest* getRequestBySessionArrayPtr(uint32_t* sessionArrayPtr);
Baili Feng1a128bd2017-06-21 17:46:23 +0800126
127 // LocationControlAPI
128 uint32_t locAPIGnssDeleteAidingData(GnssAidingData& data);
129 uint32_t locAPIEnable(LocationTechnologyType techType);
130 void locAPIDisable();
131 uint32_t locAPIGnssUpdateConfig(GnssConfig config);
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +0530132 uint32_t locAPIGnssGetConfig(GnssConfigFlagsMask config);
133 inline LocationControlAPI* getControlAPI() { return mLocationControlAPI; }
Baili Feng1a128bd2017-06-21 17:46:23 +0800134
135 // callbacks
136 void onCtrlResponseCb(LocationError error, uint32_t id);
137 void onCtrlCollectiveResponseCb(size_t count, LocationError* errors, uint32_t* ids);
138
139 inline virtual void onGnssDeleteAidingDataCb(LocationError /*error*/) {}
140 inline virtual void onEnableCb(LocationError /*error*/) {}
141 inline virtual void onDisableCb(LocationError /*error*/) {}
142 inline virtual void onGnssUpdateConfigCb(
143 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +0530144 inline virtual void onGnssGetConfigCb(
145 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
Baili Feng1a128bd2017-06-21 17:46:23 +0800146
147 class GnssDeleteAidingDataRequest : public LocationAPIRequest {
148 public:
149 GnssDeleteAidingDataRequest(LocationAPIControlClient& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800150 inline void onResponse(LocationError error, uint32_t /*id*/) {
Baili Feng1a128bd2017-06-21 17:46:23 +0800151 mAPI.onGnssDeleteAidingDataCb(error);
152 }
153 LocationAPIControlClient& mAPI;
154 };
155
156 class EnableRequest : public LocationAPIRequest {
157 public:
158 EnableRequest(LocationAPIControlClient& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800159 inline void onResponse(LocationError error, uint32_t /*id*/) {
Baili Feng1a128bd2017-06-21 17:46:23 +0800160 mAPI.onEnableCb(error);
161 }
162 LocationAPIControlClient& mAPI;
163 };
164
165 class DisableRequest : public LocationAPIRequest {
166 public:
167 DisableRequest(LocationAPIControlClient& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800168 inline void onResponse(LocationError error, uint32_t /*id*/) {
Baili Feng1a128bd2017-06-21 17:46:23 +0800169 mAPI.onDisableCb(error);
170 }
171 LocationAPIControlClient& mAPI;
172 };
173
174 class GnssUpdateConfigRequest : public LocationAPIRequest {
175 public:
176 GnssUpdateConfigRequest(LocationAPIControlClient& API) : mAPI(API) {}
177 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* ids) {
178 mAPI.onGnssUpdateConfigCb(count, errors, ids);
179 }
180 LocationAPIControlClient& mAPI;
181 };
182
Saurabh Srivastavaeaf7e542018-05-20 19:29:46 +0530183 class GnssGetConfigRequest : public LocationAPIRequest {
184 public:
185 GnssGetConfigRequest(LocationAPIControlClient& API) : mAPI(API) {}
186 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* ids) {
187 mAPI.onGnssGetConfigCb(count, errors, ids);
188 }
189 LocationAPIControlClient& mAPI;
190 };
191
Baili Feng1a128bd2017-06-21 17:46:23 +0800192private:
193 pthread_mutex_t mMutex;
194 LocationControlAPI* mLocationControlAPI;
195 RequestQueue mRequestQueues[CTRL_REQUEST_MAX];
196 bool mEnabled;
197 GnssConfig mConfig;
198};
199
200class LocationAPIClientBase {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800201public:
202 LocationAPIClientBase();
Dante Russoc85c8ff2017-02-28 16:45:47 -0800203 LocationAPIClientBase(const LocationAPIClientBase&) = delete;
204 LocationAPIClientBase& operator=(const LocationAPIClientBase&) = delete;
205
Saurabh Srivastavaa2f2fdc2020-07-10 16:46:16 +0530206 void destroy();
207 void onLocationApiDestroyCompleteCb();
208
Dante Russoc85c8ff2017-02-28 16:45:47 -0800209 void locAPISetCallbacks(LocationCallbacks& locationCallbacks);
Baili Feng2df685d2017-07-20 17:02:26 +0800210 void removeSession(uint32_t session);
Baili Feng1a128bd2017-06-21 17:46:23 +0800211 LocationAPIRequest* getRequestBySession(uint32_t session);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800212
213 // LocationAPI
Saurabh Srivastava66c682f2018-05-20 23:06:12 +0530214 uint32_t locAPIStartTracking(TrackingOptions& trackingOptions);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800215 void locAPIStopTracking();
Saurabh Srivastava66c682f2018-05-20 23:06:12 +0530216 void locAPIUpdateTrackingOptions(TrackingOptions& trackingOptions);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800217
218 int32_t locAPIGetBatchSize();
Saurabh Srivastava66c682f2018-05-20 23:06:12 +0530219 uint32_t locAPIStartSession(
220 uint32_t id, uint32_t sessionMode, TrackingOptions&& trackingOptions);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800221 uint32_t locAPIStopSession(uint32_t id);
Saurabh Srivastava66c682f2018-05-20 23:06:12 +0530222 uint32_t locAPIUpdateSessionOptions(
223 uint32_t id, uint32_t sessionMode, TrackingOptions&& trackingOptions);
Dante Russo34ce2a62017-10-03 14:38:42 -0700224 uint32_t locAPIGetBatchedLocations(uint32_t id, size_t count);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800225
226 uint32_t locAPIAddGeofences(size_t count, uint32_t* ids,
227 GeofenceOption* options, GeofenceInfo* data);
228 void locAPIRemoveGeofences(size_t count, uint32_t* ids);
229 void locAPIModifyGeofences(size_t count, uint32_t* ids, GeofenceOption* options);
230 void locAPIPauseGeofences(size_t count, uint32_t* ids);
231 void locAPIResumeGeofences(size_t count, uint32_t* ids, GeofenceBreachTypeMask* mask);
232 void locAPIRemoveAllGeofences();
233
234 void locAPIGnssNiResponse(uint32_t id, GnssNiResponse response);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800235
236 // callbacks
237 void onResponseCb(LocationError error, uint32_t id);
238 void onCollectiveResponseCb(size_t count, LocationError* errors, uint32_t* ids);
239
Dante Russoc85c8ff2017-02-28 16:45:47 -0800240 void beforeGeofenceBreachCb(GeofenceBreachNotification geofenceBreachNotification);
241
242 inline virtual void onCapabilitiesCb(LocationCapabilitiesMask /*capabilitiesMask*/) {}
243 inline virtual void onGnssNmeaCb(GnssNmeaNotification /*gnssNmeaNotification*/) {}
Mike Cailean75cfd432018-06-07 16:54:35 -0700244 inline virtual void onGnssDataCb(GnssDataNotification /*gnssDataNotification*/) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800245 inline virtual void onGnssMeasurementsCb(
246 GnssMeasurementsNotification /*gnssMeasurementsNotification*/) {}
247
248 inline virtual void onTrackingCb(Location /*location*/) {}
249 inline virtual void onGnssSvCb(GnssSvNotification /*gnssSvNotification*/) {}
250 inline virtual void onStartTrackingCb(LocationError /*error*/) {}
251 inline virtual void onStopTrackingCb(LocationError /*error*/) {}
252 inline virtual void onUpdateTrackingOptionsCb(LocationError /*error*/) {}
253
254 inline virtual void onGnssLocationInfoCb(
255 GnssLocationInfoNotification /*gnssLocationInfoNotification*/) {}
256
Bhavna Sharma686a5c52017-05-02 14:29:46 -0700257 inline virtual void onBatchingCb(size_t /*count*/, Location* /*location*/,
258 BatchingOptions /*batchingOptions*/) {}
259 inline virtual void onBatchingStatusCb(BatchingStatusInfo /*batchingStatus*/,
260 std::list<uint32_t> &/*listOfCompletedTrips*/) {}
261 void beforeBatchingStatusCb(BatchingStatusInfo batchStatus,
262 std::list<uint32_t> & tripCompletedList);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800263 inline virtual void onStartBatchingCb(LocationError /*error*/) {}
264 inline virtual void onStopBatchingCb(LocationError /*error*/) {}
265 inline virtual void onUpdateBatchingOptionsCb(LocationError /*error*/) {}
266 inline virtual void onGetBatchedLocationsCb(LocationError /*error*/) {}
267
268 inline virtual void onGeofenceBreachCb(
269 GeofenceBreachNotification /*geofenceBreachNotification*/) {}
270 inline virtual void onGeofenceStatusCb(
271 GeofenceStatusNotification /*geofenceStatusNotification*/) {}
272 inline virtual void onAddGeofencesCb(
273 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
274 inline virtual void onRemoveGeofencesCb(
275 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
276 inline virtual void onModifyGeofencesCb(
277 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
278 inline virtual void onPauseGeofencesCb(
279 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
280 inline virtual void onResumeGeofencesCb(
281 size_t /*count*/, LocationError* /*errors*/, uint32_t* /*ids*/) {}
282
283 inline virtual void onGnssNiCb(uint32_t /*id*/, GnssNiNotification /*gnssNiNotification*/) {}
284 inline virtual void onGnssNiResponseCb(LocationError /*error*/) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800285
Wei Chen21b9c4e2018-09-19 10:59:28 -0700286 inline virtual void onLocationSystemInfoCb(LocationSystemInfo /*locationSystemInfo*/) {}
287
Saurabh Srivastavaa2f2fdc2020-07-10 16:46:16 +0530288protected:
289 virtual ~LocationAPIClientBase();
290
Dante Russoc85c8ff2017-02-28 16:45:47 -0800291private:
292 // private inner classes
293 typedef struct {
294 uint32_t id;
295 uint32_t trackingSession;
296 uint32_t batchingSession;
297 uint32_t sessionMode;
298 } SessionEntity;
299
Baili Feng2df685d2017-07-20 17:02:26 +0800300 template<typename T>
Dante Russoc85c8ff2017-02-28 16:45:47 -0800301 class BiDict {
302 public:
303 BiDict() {
304 pthread_mutex_init(&mBiDictMutex, nullptr);
305 }
Baili Fengc0a300c2017-06-13 15:26:57 +0800306 virtual ~BiDict() {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800307 pthread_mutex_destroy(&mBiDictMutex);
308 }
309 bool hasId(uint32_t id) {
310 pthread_mutex_lock(&mBiDictMutex);
311 bool ret = (mForwardMap.find(id) != mForwardMap.end());
312 pthread_mutex_unlock(&mBiDictMutex);
313 return ret;
314 }
Baili Feng2df685d2017-07-20 17:02:26 +0800315 bool hasSession(uint32_t session) {
316 pthread_mutex_lock(&mBiDictMutex);
317 bool ret = (mBackwardMap.find(session) != mBackwardMap.end());
318 pthread_mutex_unlock(&mBiDictMutex);
319 return ret;
320 }
321 void set(uint32_t id, uint32_t session, T& ext) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800322 pthread_mutex_lock(&mBiDictMutex);
323 mForwardMap[id] = session;
324 mBackwardMap[session] = id;
Baili Feng2df685d2017-07-20 17:02:26 +0800325 mExtMap[session] = ext;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800326 pthread_mutex_unlock(&mBiDictMutex);
327 }
328 void clear() {
329 pthread_mutex_lock(&mBiDictMutex);
330 mForwardMap.clear();
331 mBackwardMap.clear();
Baili Feng2df685d2017-07-20 17:02:26 +0800332 mExtMap.clear();
Dante Russoc85c8ff2017-02-28 16:45:47 -0800333 pthread_mutex_unlock(&mBiDictMutex);
334 }
335 void rmById(uint32_t id) {
336 pthread_mutex_lock(&mBiDictMutex);
337 mBackwardMap.erase(mForwardMap[id]);
Baili Feng2df685d2017-07-20 17:02:26 +0800338 mExtMap.erase(mForwardMap[id]);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800339 mForwardMap.erase(id);
340 pthread_mutex_unlock(&mBiDictMutex);
341 }
342 void rmBySession(uint32_t session) {
343 pthread_mutex_lock(&mBiDictMutex);
344 mForwardMap.erase(mBackwardMap[session]);
345 mBackwardMap.erase(session);
Baili Feng2df685d2017-07-20 17:02:26 +0800346 mExtMap.erase(session);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800347 pthread_mutex_unlock(&mBiDictMutex);
348 }
349 uint32_t getId(uint32_t session) {
350 pthread_mutex_lock(&mBiDictMutex);
Baili Fengb29778e2017-05-23 14:32:22 +0800351 uint32_t ret = 0;
352 auto it = mBackwardMap.find(session);
353 if (it != mBackwardMap.end()) {
354 ret = it->second;
355 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800356 pthread_mutex_unlock(&mBiDictMutex);
357 return ret;
358 }
359 uint32_t getSession(uint32_t id) {
360 pthread_mutex_lock(&mBiDictMutex);
Baili Fengb29778e2017-05-23 14:32:22 +0800361 uint32_t ret = 0;
362 auto it = mForwardMap.find(id);
363 if (it != mForwardMap.end()) {
364 ret = it->second;
365 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800366 pthread_mutex_unlock(&mBiDictMutex);
367 return ret;
368 }
Baili Feng2df685d2017-07-20 17:02:26 +0800369 T getExtById(uint32_t id) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800370 pthread_mutex_lock(&mBiDictMutex);
Baili Feng2df685d2017-07-20 17:02:26 +0800371 T ret;
372 memset(&ret, 0, sizeof(T));
373 uint32_t session = mForwardMap[id];
374 if (session > 0) {
375 auto it = mExtMap.find(session);
376 if (it != mExtMap.end()) {
377 ret = it->second;
378 }
379 }
380 pthread_mutex_unlock(&mBiDictMutex);
381 return ret;
382 }
383 T getExtBySession(uint32_t session) {
384 pthread_mutex_lock(&mBiDictMutex);
385 T ret;
386 memset(&ret, 0, sizeof(T));
387 auto it = mExtMap.find(session);
388 if (it != mExtMap.end()) {
Baili Fengb29778e2017-05-23 14:32:22 +0800389 ret = it->second;
390 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800391 pthread_mutex_unlock(&mBiDictMutex);
392 return ret;
393 }
394 std::vector<uint32_t> getAllSessions() {
395 std::vector<uint32_t> ret;
396 pthread_mutex_lock(&mBiDictMutex);
397 for (auto it = mBackwardMap.begin(); it != mBackwardMap.end(); it++) {
398 ret.push_back(it->first);
399 }
400 pthread_mutex_unlock(&mBiDictMutex);
401 return ret;
402 }
403 private:
404 pthread_mutex_t mBiDictMutex;
405 // mForwarMap mapping id->session
406 std::map<uint32_t, uint32_t> mForwardMap;
407 // mBackwardMap mapping session->id
408 std::map<uint32_t, uint32_t> mBackwardMap;
Baili Feng2df685d2017-07-20 17:02:26 +0800409 // mExtMap mapping session->ext
410 std::map<uint32_t, T> mExtMap;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800411 };
412
Dante Russoc85c8ff2017-02-28 16:45:47 -0800413 class StartTrackingRequest : public LocationAPIRequest {
414 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800415 StartTrackingRequest(LocationAPIClientBase& API) : mAPI(API) {}
Dante Russo34ce2a62017-10-03 14:38:42 -0700416 inline void onResponse(LocationError error, uint32_t id) {
417 if (error != LOCATION_ERROR_SUCCESS) {
418 mAPI.removeSession(id);
419 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800420 mAPI.onStartTrackingCb(error);
421 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800422 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800423 };
424
425 class StopTrackingRequest : public LocationAPIRequest {
426 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800427 StopTrackingRequest(LocationAPIClientBase& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800428 inline void onResponse(LocationError error, uint32_t id) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800429 mAPI.onStopTrackingCb(error);
Baili Feng2df685d2017-07-20 17:02:26 +0800430 if (error == LOCATION_ERROR_SUCCESS) {
431 mAPI.removeSession(id);
432 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800433 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800434 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800435 };
436
437 class UpdateTrackingOptionsRequest : public LocationAPIRequest {
438 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800439 UpdateTrackingOptionsRequest(LocationAPIClientBase& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800440 inline void onResponse(LocationError error, uint32_t /*id*/) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800441 mAPI.onUpdateTrackingOptionsCb(error);
442 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800443 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800444 };
445
446 class StartBatchingRequest : public LocationAPIRequest {
447 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800448 StartBatchingRequest(LocationAPIClientBase& API) : mAPI(API) {}
Dante Russo34ce2a62017-10-03 14:38:42 -0700449 inline void onResponse(LocationError error, uint32_t id) {
450 if (error != LOCATION_ERROR_SUCCESS) {
451 mAPI.removeSession(id);
452 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800453 mAPI.onStartBatchingCb(error);
454 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800455 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800456 };
457
458 class StopBatchingRequest : public LocationAPIRequest {
459 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800460 StopBatchingRequest(LocationAPIClientBase& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800461 inline void onResponse(LocationError error, uint32_t id) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800462 mAPI.onStopBatchingCb(error);
Baili Feng2df685d2017-07-20 17:02:26 +0800463 if (error == LOCATION_ERROR_SUCCESS) {
464 mAPI.removeSession(id);
465 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800466 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800467 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800468 };
469
470 class UpdateBatchingOptionsRequest : public LocationAPIRequest {
471 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800472 UpdateBatchingOptionsRequest(LocationAPIClientBase& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800473 inline void onResponse(LocationError error, uint32_t /*id*/) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800474 mAPI.onUpdateBatchingOptionsCb(error);
475 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800476 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800477 };
478
479 class GetBatchedLocationsRequest : public LocationAPIRequest {
480 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800481 GetBatchedLocationsRequest(LocationAPIClientBase& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800482 inline void onResponse(LocationError error, uint32_t /*id*/) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800483 mAPI.onGetBatchedLocationsCb(error);
484 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800485 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800486 };
487
488 class AddGeofencesRequest : public LocationAPIRequest {
489 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800490 AddGeofencesRequest(LocationAPIClientBase& API) : mAPI(API) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800491 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* sessions) {
492 uint32_t *ids = (uint32_t*)malloc(sizeof(uint32_t) * count);
493 for (size_t i = 0; i < count; i++) {
494 ids[i] = mAPI.mGeofenceBiDict.getId(sessions[i]);
495 }
Nilesh Gharde0df749e2019-08-29 14:24:46 +0530496 LOC_LOGD("%s:]Returned geofence-id: %d in add geofence", __FUNCTION__, *ids);
Dante Russoc85c8ff2017-02-28 16:45:47 -0800497 mAPI.onAddGeofencesCb(count, errors, ids);
498 free(ids);
499 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800500 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800501 };
502
503 class RemoveGeofencesRequest : public LocationAPIRequest {
504 public:
Kevin Tang63c4d2f2017-10-20 13:26:11 -0700505 RemoveGeofencesRequest(LocationAPIClientBase& API,
506 BiDict<GeofenceBreachTypeMask>* removedGeofenceBiDict) :
507 mAPI(API), mRemovedGeofenceBiDict(removedGeofenceBiDict) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800508 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* sessions) {
Kevin Tang63c4d2f2017-10-20 13:26:11 -0700509 if (nullptr != mRemovedGeofenceBiDict) {
510 uint32_t *ids = (uint32_t*)malloc(sizeof(uint32_t) * count);
511 for (size_t i = 0; i < count; i++) {
512 ids[i] = mRemovedGeofenceBiDict->getId(sessions[i]);
513 }
Nilesh Gharde0df749e2019-08-29 14:24:46 +0530514 LOC_LOGD("%s:]Returned geofence-id: %d in remove geofence", __FUNCTION__, *ids);
Kevin Tang63c4d2f2017-10-20 13:26:11 -0700515 mAPI.onRemoveGeofencesCb(count, errors, ids);
516 free(ids);
517 delete(mRemovedGeofenceBiDict);
518 } else {
519 LOC_LOGE("%s:%d] Unable to access removed geofences data.", __FUNCTION__, __LINE__);
520 }
Dante Russoc85c8ff2017-02-28 16:45:47 -0800521 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800522 LocationAPIClientBase& mAPI;
Kevin Tang63c4d2f2017-10-20 13:26:11 -0700523 BiDict<GeofenceBreachTypeMask>* mRemovedGeofenceBiDict;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800524 };
525
526 class ModifyGeofencesRequest : public LocationAPIRequest {
527 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800528 ModifyGeofencesRequest(LocationAPIClientBase& API) : mAPI(API) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800529 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* sessions) {
530 uint32_t *ids = (uint32_t*)malloc(sizeof(uint32_t) * count);
531 for (size_t i = 0; i < count; i++) {
532 ids[i] = mAPI.mGeofenceBiDict.getId(sessions[i]);
533 }
534 mAPI.onModifyGeofencesCb(count, errors, ids);
535 free(ids);
536 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800537 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800538 };
539
540 class PauseGeofencesRequest : public LocationAPIRequest {
541 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800542 PauseGeofencesRequest(LocationAPIClientBase& API) : mAPI(API) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800543 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* sessions) {
544 uint32_t *ids = (uint32_t*)malloc(sizeof(uint32_t) * count);
545 for (size_t i = 0; i < count; i++) {
546 ids[i] = mAPI.mGeofenceBiDict.getId(sessions[i]);
547 }
548 mAPI.onPauseGeofencesCb(count, errors, ids);
549 free(ids);
550 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800551 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800552 };
553
554 class ResumeGeofencesRequest : public LocationAPIRequest {
555 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800556 ResumeGeofencesRequest(LocationAPIClientBase& API) : mAPI(API) {}
Dante Russoc85c8ff2017-02-28 16:45:47 -0800557 inline void onCollectiveResponse(size_t count, LocationError* errors, uint32_t* sessions) {
558 uint32_t *ids = (uint32_t*)malloc(sizeof(uint32_t) * count);
559 for (size_t i = 0; i < count; i++) {
560 ids[i] = mAPI.mGeofenceBiDict.getId(sessions[i]);
561 }
562 mAPI.onResumeGeofencesCb(count, errors, ids);
563 free(ids);
564 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800565 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800566 };
567
568 class GnssNiResponseRequest : public LocationAPIRequest {
569 public:
Baili Feng1a128bd2017-06-21 17:46:23 +0800570 GnssNiResponseRequest(LocationAPIClientBase& API) : mAPI(API) {}
Baili Feng2df685d2017-07-20 17:02:26 +0800571 inline void onResponse(LocationError error, uint32_t /*id*/) {
Dante Russoc85c8ff2017-02-28 16:45:47 -0800572 mAPI.onGnssNiResponseCb(error);
573 }
Baili Feng1a128bd2017-06-21 17:46:23 +0800574 LocationAPIClientBase& mAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800575 };
576
Dante Russoc85c8ff2017-02-28 16:45:47 -0800577private:
578 pthread_mutex_t mMutex;
579
Dante Russoc85c8ff2017-02-28 16:45:47 -0800580 geofenceBreachCallback mGeofenceBreachCallback;
Bhavna Sharma686a5c52017-05-02 14:29:46 -0700581 batchingStatusCallback mBatchingStatusCallback;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800582
583 LocationAPI* mLocationAPI;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800584
Baili Fengc0a300c2017-06-13 15:26:57 +0800585 RequestQueue mRequestQueues[REQUEST_MAX];
Baili Feng2df685d2017-07-20 17:02:26 +0800586 BiDict<GeofenceBreachTypeMask> mGeofenceBiDict;
587 BiDict<SessionEntity> mSessionBiDict;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800588 int32_t mBatchSize;
Dante Russoc1da12a2017-07-27 10:44:54 -0700589 bool mTracking;
Dante Russoc85c8ff2017-02-28 16:45:47 -0800590};
591
592#endif /* LOCATION_API_CLINET_BASE_H */