Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2017 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 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 29 | #define LOG_NDDEBUG 0 |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 30 | #define LOG_TAG "LocSvc_APIClientBase" |
| 31 | |
Naresh Munagala | ea05170 | 2017-06-20 22:18:19 +0530 | [diff] [blame] | 32 | #include <platform_lib_log_util.h> |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 33 | #include <loc_cfg.h> |
| 34 | #include "LocationAPIClientBase.h" |
| 35 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 36 | #define FLP_CONF_FILE "/etc/flp.conf" |
| 37 | #define GEOFENCE_SESSION_ID 0xFFFFFFFF |
| 38 | #define CONFIG_SESSION_ID 0xFFFFFFFF |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 39 | |
Baili Feng | 1a128bd | 2017-06-21 17:46:23 +0800 | [diff] [blame] | 40 | // LocationAPIControlClient |
| 41 | LocationAPIControlClient::LocationAPIControlClient() : |
| 42 | mEnabled(false) |
| 43 | { |
| 44 | pthread_mutex_init(&mMutex, nullptr); |
| 45 | |
| 46 | for (int i = 0; i < CTRL_REQUEST_MAX; i++) { |
| 47 | mRequestQueues[i].reset(0); |
| 48 | } |
| 49 | |
| 50 | memset(&mConfig, 0, sizeof(GnssConfig)); |
| 51 | |
| 52 | LocationControlCallbacks locationControlCallbacks; |
| 53 | locationControlCallbacks.size = sizeof(LocationControlCallbacks); |
| 54 | |
| 55 | locationControlCallbacks.responseCb = |
| 56 | [this](LocationError error, uint32_t id) { |
| 57 | onCtrlResponseCb(error, id); |
| 58 | }; |
| 59 | locationControlCallbacks.collectiveResponseCb = |
| 60 | [this](size_t count, LocationError* errors, uint32_t* ids) { |
| 61 | onCtrlCollectiveResponseCb(count, errors, ids); |
| 62 | }; |
| 63 | |
| 64 | mLocationControlAPI = LocationControlAPI::createInstance(locationControlCallbacks); |
| 65 | } |
| 66 | |
| 67 | LocationAPIControlClient::~LocationAPIControlClient() |
| 68 | { |
| 69 | pthread_mutex_lock(&mMutex); |
| 70 | |
| 71 | if (mLocationControlAPI) { |
| 72 | mLocationControlAPI->destroy(); |
| 73 | mLocationControlAPI = nullptr; |
| 74 | } |
| 75 | |
| 76 | for (int i = 0; i < CTRL_REQUEST_MAX; i++) { |
| 77 | mRequestQueues[i].reset(0); |
| 78 | } |
| 79 | |
| 80 | pthread_mutex_unlock(&mMutex); |
| 81 | |
| 82 | pthread_mutex_destroy(&mMutex); |
| 83 | } |
| 84 | |
| 85 | uint32_t LocationAPIControlClient::locAPIGnssDeleteAidingData(GnssAidingData& data) |
| 86 | { |
| 87 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
| 88 | pthread_mutex_lock(&mMutex); |
| 89 | if (mLocationControlAPI) { |
| 90 | uint32_t session = mLocationControlAPI->gnssDeleteAidingData(data); |
| 91 | LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session); |
| 92 | mRequestQueues[CTRL_REQUEST_DELETEAIDINGDATA].reset(session); |
| 93 | mRequestQueues[CTRL_REQUEST_DELETEAIDINGDATA].push(new GnssDeleteAidingDataRequest(*this)); |
| 94 | |
| 95 | retVal = LOCATION_ERROR_SUCCESS; |
| 96 | } |
| 97 | pthread_mutex_unlock(&mMutex); |
| 98 | |
| 99 | return retVal; |
| 100 | } |
| 101 | |
| 102 | uint32_t LocationAPIControlClient::locAPIEnable(LocationTechnologyType techType) |
| 103 | { |
| 104 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
| 105 | pthread_mutex_lock(&mMutex); |
| 106 | if (mEnabled) { |
| 107 | // just return success if already enabled |
| 108 | retVal = LOCATION_ERROR_SUCCESS; |
| 109 | } else if (mLocationControlAPI) { |
| 110 | uint32_t session = mLocationControlAPI->enable(techType); |
| 111 | LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session); |
| 112 | mRequestQueues[CTRL_REQUEST_CONTROL].reset(session); |
| 113 | mRequestQueues[CTRL_REQUEST_CONTROL].push(new EnableRequest(*this)); |
| 114 | retVal = LOCATION_ERROR_SUCCESS; |
| 115 | mEnabled = true; |
| 116 | } else { |
| 117 | LOC_LOGE("%s:%d] failed.", __FUNCTION__, __LINE__); |
| 118 | } |
| 119 | pthread_mutex_unlock(&mMutex); |
| 120 | |
| 121 | return retVal; |
| 122 | } |
| 123 | |
| 124 | void LocationAPIControlClient::locAPIDisable() |
| 125 | { |
| 126 | pthread_mutex_lock(&mMutex); |
| 127 | if (mEnabled && mLocationControlAPI) { |
| 128 | uint32_t session = 0; |
| 129 | session = mRequestQueues[CTRL_REQUEST_CONTROL].getSession(); |
| 130 | if (session > 0) { |
| 131 | mRequestQueues[CTRL_REQUEST_CONTROL].push(new DisableRequest(*this)); |
| 132 | mLocationControlAPI->disable(session); |
| 133 | mEnabled = false; |
| 134 | } else { |
| 135 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, session); |
| 136 | } |
| 137 | } |
| 138 | pthread_mutex_unlock(&mMutex); |
| 139 | } |
| 140 | |
| 141 | uint32_t LocationAPIControlClient::locAPIGnssUpdateConfig(GnssConfig config) |
| 142 | { |
| 143 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
| 144 | if (memcmp(&mConfig, &config, sizeof(GnssConfig)) == 0) { |
| 145 | LOC_LOGV("%s:%d] GnssConfig is identical to previous call", __FUNCTION__, __LINE__); |
| 146 | retVal = LOCATION_ERROR_SUCCESS; |
| 147 | return retVal; |
| 148 | } |
| 149 | |
| 150 | pthread_mutex_lock(&mMutex); |
| 151 | if (mLocationControlAPI) { |
| 152 | |
| 153 | memcpy(&mConfig, &config, sizeof(GnssConfig)); |
| 154 | |
| 155 | uint32_t session = 0; |
| 156 | uint32_t* idArray = mLocationControlAPI->gnssUpdateConfig(config); |
| 157 | LOC_LOGV("%s:%d] gnssUpdateConfig return array: %p", __FUNCTION__, __LINE__, idArray); |
| 158 | if (idArray != nullptr) { |
| 159 | if (mRequestQueues[CTRL_REQUEST_CONFIG].getSession() != CONFIG_SESSION_ID) { |
| 160 | mRequestQueues[CTRL_REQUEST_CONFIG].reset(CONFIG_SESSION_ID); |
| 161 | } |
| 162 | mRequestQueues[CTRL_REQUEST_CONFIG].push(new GnssUpdateConfigRequest(*this)); |
| 163 | retVal = LOCATION_ERROR_SUCCESS; |
| 164 | } |
| 165 | } |
| 166 | pthread_mutex_unlock(&mMutex); |
| 167 | return retVal; |
| 168 | } |
| 169 | |
| 170 | void LocationAPIControlClient::onCtrlResponseCb(LocationError error, uint32_t id) |
| 171 | { |
| 172 | if (error != LOCATION_ERROR_SUCCESS) { |
| 173 | LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, error, id); |
| 174 | } else { |
| 175 | LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, error, id); |
| 176 | } |
| 177 | LocationAPIRequest* request = getRequestBySession(id); |
| 178 | if (request) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 179 | request->onResponse(error, id); |
Baili Feng | 1a128bd | 2017-06-21 17:46:23 +0800 | [diff] [blame] | 180 | delete request; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void LocationAPIControlClient::onCtrlCollectiveResponseCb( |
| 185 | size_t count, LocationError* errors, uint32_t* ids) |
| 186 | { |
| 187 | for (size_t i = 0; i < count; i++) { |
| 188 | if (errors[i] != LOCATION_ERROR_SUCCESS) { |
| 189 | LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, errors[i], ids[i]); |
| 190 | } else { |
| 191 | LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, errors[i], ids[i]); |
| 192 | } |
| 193 | } |
| 194 | LocationAPIRequest* request = nullptr; |
| 195 | pthread_mutex_lock(&mMutex); |
| 196 | if (mRequestQueues[CTRL_REQUEST_CONFIG].getSession() == CONFIG_SESSION_ID) { |
| 197 | request = mRequestQueues[CTRL_REQUEST_CONFIG].pop(); |
| 198 | } |
| 199 | pthread_mutex_unlock(&mMutex); |
| 200 | if (request) { |
| 201 | request->onCollectiveResponse(count, errors, ids); |
| 202 | delete request; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | LocationAPIRequest* LocationAPIControlClient::getRequestBySession(uint32_t session) |
| 207 | { |
| 208 | pthread_mutex_lock(&mMutex); |
| 209 | LocationAPIRequest* request = nullptr; |
| 210 | for (int i = 0; i < CTRL_REQUEST_MAX; i++) { |
| 211 | if (i != CTRL_REQUEST_CONFIG && |
| 212 | mRequestQueues[i].getSession() == session) { |
| 213 | request = mRequestQueues[i].pop(); |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | pthread_mutex_unlock(&mMutex); |
| 218 | return request; |
| 219 | } |
| 220 | |
| 221 | // LocationAPIClientBase |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 222 | LocationAPIClientBase::LocationAPIClientBase() : |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 223 | mGeofenceBreachCallback(nullptr), |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 224 | mBatchingStatusCallback(nullptr), |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 225 | mLocationAPI(nullptr), |
Baili Feng | 1a128bd | 2017-06-21 17:46:23 +0800 | [diff] [blame] | 226 | mBatchSize(-1) |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 227 | { |
| 228 | |
| 229 | // use recursive mutex, in case callback come from the same thread |
| 230 | pthread_mutexattr_t attr; |
| 231 | pthread_mutexattr_init(&attr); |
| 232 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 233 | pthread_mutex_init(&mMutex, &attr); |
| 234 | |
| 235 | for (int i = 0; i < REQUEST_MAX; i++) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 236 | mRequestQueues[i].reset(0); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 237 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void LocationAPIClientBase::locAPISetCallbacks(LocationCallbacks& locationCallbacks) |
| 241 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 242 | pthread_mutex_lock(&mMutex); |
| 243 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 244 | if (locationCallbacks.geofenceBreachCb != nullptr) { |
| 245 | mGeofenceBreachCallback = locationCallbacks.geofenceBreachCb; |
| 246 | locationCallbacks.geofenceBreachCb = |
| 247 | [this](GeofenceBreachNotification geofenceBreachNotification) { |
| 248 | beforeGeofenceBreachCb(geofenceBreachNotification); |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | locationCallbacks.capabilitiesCb = |
| 253 | [this](LocationCapabilitiesMask capabilitiesMask) { |
| 254 | onCapabilitiesCb(capabilitiesMask); |
| 255 | }; |
| 256 | locationCallbacks.responseCb = [this](LocationError error, uint32_t id) { |
| 257 | onResponseCb(error, id); |
| 258 | }; |
| 259 | locationCallbacks.collectiveResponseCb = |
| 260 | [this](size_t count, LocationError* errors, uint32_t* ids) { |
| 261 | onCollectiveResponseCb(count, errors, ids); |
| 262 | }; |
| 263 | |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 264 | if (locationCallbacks.batchingStatusCb != nullptr) { |
| 265 | mBatchingStatusCallback = locationCallbacks.batchingStatusCb; |
| 266 | locationCallbacks.batchingStatusCb = |
| 267 | [this](BatchingStatusInfo batchStatus, std::list<uint32_t> & tripCompletedList) { |
| 268 | beforeBatchingStatusCb(batchStatus, tripCompletedList); |
| 269 | }; |
| 270 | } |
| 271 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 272 | if (mLocationAPI == nullptr ) { |
| 273 | mLocationAPI = LocationAPI::createInstance(locationCallbacks); |
| 274 | } else { |
| 275 | mLocationAPI->updateCallbacks(locationCallbacks); |
| 276 | } |
| 277 | |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 278 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | LocationAPIClientBase::~LocationAPIClientBase() |
| 282 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 283 | pthread_mutex_lock(&mMutex); |
| 284 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 285 | mGeofenceBreachCallback = nullptr; |
| 286 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 287 | if (mLocationAPI) { |
| 288 | mLocationAPI->destroy(); |
| 289 | mLocationAPI = nullptr; |
| 290 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 291 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 292 | for (int i = 0; i < REQUEST_MAX; i++) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 293 | mRequestQueues[i].reset(0); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 294 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 295 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 296 | pthread_mutex_unlock(&mMutex); |
| 297 | |
| 298 | pthread_mutex_destroy(&mMutex); |
| 299 | } |
| 300 | |
| 301 | uint32_t LocationAPIClientBase::locAPIStartTracking(LocationOptions& options) |
| 302 | { |
| 303 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 304 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 305 | if (mLocationAPI) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 306 | uint32_t session = mLocationAPI->startTracking(options); |
| 307 | LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session); |
| 308 | // onResponseCb might be called from other thread immediately after |
| 309 | // startTracking returns, so we are not going to unlock mutex |
| 310 | // until StartTrackingRequest is pushed into mRequestQueues[REQUEST_TRACKING] |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 311 | mRequestQueues[REQUEST_TRACKING].reset(session); |
| 312 | mRequestQueues[REQUEST_TRACKING].push(new StartTrackingRequest(*this)); |
| 313 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 314 | |
| 315 | retVal = LOCATION_ERROR_SUCCESS; |
| 316 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 317 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 318 | |
| 319 | return retVal; |
| 320 | } |
| 321 | |
| 322 | void LocationAPIClientBase::locAPIStopTracking() |
| 323 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 324 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 325 | if (mLocationAPI) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 326 | uint32_t session = 0; |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 327 | session = mRequestQueues[REQUEST_TRACKING].getSession(); |
| 328 | if (session > 0) { |
| 329 | mRequestQueues[REQUEST_TRACKING].push(new StopTrackingRequest(*this)); |
| 330 | mLocationAPI->stopTracking(session); |
| 331 | } else { |
| 332 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, session); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 333 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 334 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 335 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void LocationAPIClientBase::locAPIUpdateTrackingOptions(LocationOptions& options) |
| 339 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 340 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 341 | if (mLocationAPI) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 342 | uint32_t session = 0; |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 343 | session = mRequestQueues[REQUEST_TRACKING].getSession(); |
| 344 | if (session > 0) { |
| 345 | mRequestQueues[REQUEST_TRACKING].push(new UpdateTrackingOptionsRequest(*this)); |
| 346 | mLocationAPI->updateTrackingOptions(session, options); |
| 347 | } else { |
| 348 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, session); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 349 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 350 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 351 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | int32_t LocationAPIClientBase::locAPIGetBatchSize() |
| 355 | { |
| 356 | if (mBatchSize == -1) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 357 | const loc_param_s_type flp_conf_param_table[] = |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 358 | { |
| 359 | {"BATCH_SIZE", &mBatchSize, nullptr, 'n'}, |
| 360 | }; |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 361 | UTIL_READ_CONF(FLP_CONF_FILE, flp_conf_param_table); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 362 | if (mBatchSize < 0) { |
| 363 | // set mBatchSize to 0 if we got an illegal value from config file |
| 364 | mBatchSize = 0; |
| 365 | } |
| 366 | } |
| 367 | return mBatchSize; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | uint32_t LocationAPIClientBase::locAPIStartSession(uint32_t id, uint32_t sessionMode, |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 372 | LocationOptions& locationOptions) |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 373 | { |
| 374 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 375 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 376 | if (mLocationAPI) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 377 | |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 378 | if (mSessionBiDict.hasId(id)) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 379 | LOC_LOGE("%s:%d] session %d has already started.", __FUNCTION__, __LINE__, id); |
| 380 | retVal = LOCATION_ERROR_ALREADY_STARTED; |
| 381 | } else { |
| 382 | uint32_t trackingSession = 0; |
| 383 | uint32_t batchingSession = 0; |
| 384 | |
| 385 | if (sessionMode == SESSION_MODE_ON_FIX) { |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 386 | trackingSession = mLocationAPI->startTracking(locationOptions); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 387 | LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, trackingSession); |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 388 | mRequestQueues[REQUEST_SESSION].push(new StartTrackingRequest(*this)); |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 389 | } else if ((sessionMode == SESSION_MODE_ON_FULL) || |
| 390 | (sessionMode == SESSION_MODE_ON_TRIP_COMPLETED)) { |
| 391 | // Fill in the batch mode |
| 392 | BatchingOptions batchOptions = {}; |
| 393 | batchOptions.size = sizeof(BatchingOptions); |
| 394 | batchOptions.batchingMode = BATCHING_MODE_ROUTINE; |
| 395 | if (sessionMode == SESSION_MODE_ON_TRIP_COMPLETED) { |
| 396 | batchOptions.batchingMode = BATCHING_MODE_TRIP; |
| 397 | } |
| 398 | |
| 399 | batchingSession = mLocationAPI->startBatching(locationOptions, batchOptions); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 400 | LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, batchingSession); |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 401 | mRequestQueues[REQUEST_SESSION].setSession(batchingSession); |
| 402 | mRequestQueues[REQUEST_SESSION].push(new StartBatchingRequest(*this)); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 405 | uint32_t session = ((sessionMode == SESSION_MODE_ON_FULL || |
| 406 | (sessionMode == SESSION_MODE_ON_TRIP_COMPLETED)) ? |
| 407 | batchingSession : trackingSession); |
| 408 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 409 | SessionEntity entity; |
| 410 | entity.id = id; |
| 411 | entity.trackingSession = trackingSession; |
| 412 | entity.batchingSession = batchingSession; |
| 413 | entity.sessionMode = sessionMode; |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 414 | mSessionBiDict.set(id, session, entity); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 415 | |
| 416 | retVal = LOCATION_ERROR_SUCCESS; |
| 417 | } |
| 418 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 419 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 420 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 421 | |
| 422 | return retVal; |
| 423 | } |
| 424 | |
| 425 | uint32_t LocationAPIClientBase::locAPIStopSession(uint32_t id) |
| 426 | { |
| 427 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 428 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 429 | if (mLocationAPI) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 430 | |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 431 | if (mSessionBiDict.hasId(id)) { |
| 432 | SessionEntity entity = mSessionBiDict.getExtById(id); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 433 | |
| 434 | uint32_t trackingSession = entity.trackingSession; |
| 435 | uint32_t batchingSession = entity.batchingSession; |
| 436 | uint32_t sMode = entity.sessionMode; |
| 437 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 438 | if (sMode == SESSION_MODE_ON_FIX) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 439 | mRequestQueues[REQUEST_SESSION].push(new StopTrackingRequest(*this)); |
| 440 | mLocationAPI->stopTracking(trackingSession); |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 441 | } else if ((sMode == SESSION_MODE_ON_FULL) || |
| 442 | (sMode == SESSION_MODE_ON_TRIP_COMPLETED)) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 443 | mRequestQueues[REQUEST_SESSION].push(new StopBatchingRequest(*this)); |
| 444 | mLocationAPI->stopBatching(batchingSession); |
| 445 | } else { |
| 446 | LOC_LOGE("%s:%d] unknown mode %d.", __FUNCTION__, __LINE__, sMode); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | retVal = LOCATION_ERROR_SUCCESS; |
| 450 | } else { |
| 451 | retVal = LOCATION_ERROR_ID_UNKNOWN; |
| 452 | LOC_LOGE("%s:%d] session %d is not exist.", __FUNCTION__, __LINE__, id); |
| 453 | } |
| 454 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 455 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 456 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 457 | return retVal; |
| 458 | } |
| 459 | |
| 460 | uint32_t LocationAPIClientBase::locAPIUpdateSessionOptions(uint32_t id, uint32_t sessionMode, |
| 461 | LocationOptions& options) |
| 462 | { |
| 463 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 464 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 465 | if (mLocationAPI) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 466 | |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 467 | if (mSessionBiDict.hasId(id)) { |
| 468 | SessionEntity entity = mSessionBiDict.getExtById(id); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 469 | |
| 470 | uint32_t trackingSession = entity.trackingSession; |
| 471 | uint32_t batchingSession = entity.batchingSession; |
| 472 | uint32_t sMode = entity.sessionMode; |
| 473 | |
| 474 | if (sessionMode == SESSION_MODE_ON_FIX) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 475 | // we only add an UpdateTrackingOptionsRequest to mRequestQueues[REQUEST_SESSION], |
| 476 | // even if this update request will stop batching and then start tracking. |
| 477 | mRequestQueues[REQUEST_SESSION].push(new UpdateTrackingOptionsRequest(*this)); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 478 | if (sMode == SESSION_MODE_ON_FIX) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 479 | mLocationAPI->updateTrackingOptions(trackingSession, options); |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 480 | } else if ((sMode == SESSION_MODE_ON_FULL) || |
| 481 | (sMode == SESSION_MODE_ON_TRIP_COMPLETED)) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 482 | // stop batching |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 483 | // batchingSession will be removed from mSessionBiDict soon, |
| 484 | // so we don't need to add a new request to mRequestQueues[REQUEST_SESSION]. |
| 485 | mLocationAPI->stopBatching(batchingSession); |
| 486 | batchingSession = 0; |
| 487 | mRequestQueues[REQUEST_SESSION].setSession(batchingSession); |
| 488 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 489 | // start tracking |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 490 | trackingSession = mLocationAPI->startTracking(options); |
| 491 | LOC_LOGI("%s:%d] start new session: %d", |
| 492 | __FUNCTION__, __LINE__, trackingSession); |
| 493 | } else { |
| 494 | LOC_LOGE("%s:%d] unknown mode %d", __FUNCTION__, __LINE__, sMode); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 495 | } |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 496 | } else if ((sessionMode == SESSION_MODE_ON_FULL) || |
| 497 | (sessionMode == SESSION_MODE_ON_TRIP_COMPLETED)) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 498 | // we only add an UpdateBatchingOptionsRequest to mRequestQueues[REQUEST_SESSION], |
| 499 | // even if this update request will stop tracking and then start batching. |
| 500 | mRequestQueues[REQUEST_SESSION].push(new UpdateBatchingOptionsRequest(*this)); |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 501 | BatchingOptions batchOptions = {}; |
| 502 | batchOptions.size = sizeof(BatchingOptions); |
| 503 | batchOptions.batchingMode = BATCHING_MODE_ROUTINE; |
| 504 | if (sessionMode == SESSION_MODE_ON_TRIP_COMPLETED) { |
| 505 | batchOptions.batchingMode = BATCHING_MODE_TRIP; |
| 506 | } |
| 507 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 508 | if (sMode == SESSION_MODE_ON_FIX) { |
| 509 | // stop tracking |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 510 | // trackingSession will be removed from mSessionBiDict soon, |
| 511 | // so we don't need to add a new request to mRequestQueues[REQUEST_SESSION]. |
| 512 | mLocationAPI->stopTracking(trackingSession); |
| 513 | trackingSession = 0; |
| 514 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 515 | // start batching |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 516 | batchingSession = mLocationAPI->startBatching(options, batchOptions); |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 517 | LOC_LOGI("%s:%d] start new session: %d", |
| 518 | __FUNCTION__, __LINE__, batchingSession); |
| 519 | mRequestQueues[REQUEST_SESSION].setSession(batchingSession); |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 520 | } else if ((sMode == SESSION_MODE_ON_FULL) || |
| 521 | (sMode == SESSION_MODE_ON_TRIP_COMPLETED)) { |
| 522 | mLocationAPI->updateBatchingOptions(batchingSession, options, batchOptions); |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 523 | } else { |
| 524 | LOC_LOGE("%s:%d] unknown mode %d", __FUNCTION__, __LINE__, sMode); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 525 | } |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 526 | |
| 527 | } else { |
| 528 | LOC_LOGE("%s:%d] unknown mode %d.", __FUNCTION__, __LINE__, sessionMode); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 531 | uint32_t session = ((sessionMode == SESSION_MODE_ON_FULL) || |
| 532 | (sessionMode == SESSION_MODE_ON_TRIP_COMPLETED) ? |
| 533 | batchingSession : trackingSession); |
| 534 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 535 | entity.trackingSession = trackingSession; |
| 536 | entity.batchingSession = batchingSession; |
| 537 | entity.sessionMode = sessionMode; |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 538 | // remove the old values from mSessionBiDict before we add a new one. |
| 539 | mSessionBiDict.rmById(id); |
| 540 | mSessionBiDict.set(id, session, entity); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 541 | |
| 542 | retVal = LOCATION_ERROR_SUCCESS; |
| 543 | } else { |
| 544 | retVal = LOCATION_ERROR_ID_UNKNOWN; |
| 545 | LOC_LOGE("%s:%d] session %d is not exist.", __FUNCTION__, __LINE__, id); |
| 546 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 547 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 548 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 549 | return retVal; |
| 550 | } |
| 551 | |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 552 | void LocationAPIClientBase::locAPIGetBatchedLocations(uint32_t id, size_t count) |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 553 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 554 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 555 | if (mLocationAPI) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 556 | uint32_t session = 0; |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 557 | session = mRequestQueues[REQUEST_SESSION].getSession(); |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 558 | if (session > 0) { |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 559 | SessionEntity entity = mSessionBiDict.getExtById(id); |
| 560 | uint32_t batchingSession = entity.batchingSession; |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 561 | mRequestQueues[REQUEST_SESSION].push(new GetBatchedLocationsRequest(*this)); |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 562 | mLocationAPI->getBatchedLocations(batchingSession, count); |
| 563 | } else { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 564 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, session); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 565 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 566 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 567 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | uint32_t LocationAPIClientBase::locAPIAddGeofences( |
| 571 | size_t count, uint32_t* ids, GeofenceOption* options, GeofenceInfo* data) |
| 572 | { |
| 573 | uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE; |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 574 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 575 | if (mLocationAPI) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 576 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() != GEOFENCE_SESSION_ID) { |
| 577 | mRequestQueues[REQUEST_GEOFENCE].reset(GEOFENCE_SESSION_ID); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 578 | } |
| 579 | uint32_t* sessions = mLocationAPI->addGeofences(count, options, data); |
Dante Russo | 2e87b20 | 2017-03-20 15:04:36 -0700 | [diff] [blame] | 580 | if (sessions) { |
| 581 | LOC_LOGI("%s:%d] start new sessions: %p", __FUNCTION__, __LINE__, sessions); |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 582 | mRequestQueues[REQUEST_GEOFENCE].push(new AddGeofencesRequest(*this)); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 583 | |
Dante Russo | 2e87b20 | 2017-03-20 15:04:36 -0700 | [diff] [blame] | 584 | for (size_t i = 0; i < count; i++) { |
| 585 | mGeofenceBiDict.set(ids[i], sessions[i], options[i].breachTypeMask); |
| 586 | } |
| 587 | retVal = LOCATION_ERROR_SUCCESS; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 588 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 589 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 590 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 591 | |
| 592 | return retVal; |
| 593 | } |
| 594 | |
| 595 | void LocationAPIClientBase::locAPIRemoveGeofences(size_t count, uint32_t* ids) |
| 596 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 597 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 598 | if (mLocationAPI) { |
| 599 | uint32_t* sessions = (uint32_t*)malloc(sizeof(uint32_t) * count); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 600 | if (sessions == NULL) { |
| 601 | LOC_LOGE("%s:%d] Failed to allocate %d bytes !", |
| 602 | __FUNCTION__, __LINE__, sizeof(uint32_t) * count); |
Saurabh Srivastava | 2481cf3 | 2017-07-27 17:20:24 +0530 | [diff] [blame] | 603 | pthread_mutex_unlock(&mMutex); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 604 | return; |
| 605 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 606 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 607 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() == GEOFENCE_SESSION_ID) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 608 | size_t j = 0; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 609 | for (size_t i = 0; i < count; i++) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 610 | sessions[j] = mGeofenceBiDict.getSession(ids[i]); |
| 611 | if (sessions[j] > 0) { |
| 612 | j++; |
| 613 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 614 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 615 | if (j > 0) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 616 | mRequestQueues[REQUEST_GEOFENCE].push(new RemoveGeofencesRequest(*this)); |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 617 | mLocationAPI->removeGeofences(j, sessions); |
| 618 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 619 | } else { |
| 620 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, |
| 621 | mRequestQueues[REQUEST_GEOFENCE].getSession()); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 622 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 623 | |
| 624 | free(sessions); |
| 625 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 626 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void LocationAPIClientBase::locAPIModifyGeofences( |
| 630 | size_t count, uint32_t* ids, GeofenceOption* options) |
| 631 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 632 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 633 | if (mLocationAPI) { |
| 634 | uint32_t* sessions = (uint32_t*)malloc(sizeof(uint32_t) * count); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 635 | if (sessions == NULL) { |
| 636 | LOC_LOGE("%s:%d] Failed to allocate %d bytes !", |
| 637 | __FUNCTION__, __LINE__, sizeof(uint32_t) * count); |
Saurabh Srivastava | 2481cf3 | 2017-07-27 17:20:24 +0530 | [diff] [blame] | 638 | pthread_mutex_unlock(&mMutex); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 639 | return; |
| 640 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 641 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 642 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() == GEOFENCE_SESSION_ID) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 643 | size_t j = 0; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 644 | for (size_t i = 0; i < count; i++) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 645 | sessions[j] = mGeofenceBiDict.getSession(ids[i]); |
| 646 | if (sessions[j] > 0) { |
| 647 | mGeofenceBiDict.set(ids[i], sessions[j], options[i].breachTypeMask); |
| 648 | j++; |
| 649 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 650 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 651 | if (j > 0) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 652 | mRequestQueues[REQUEST_GEOFENCE].push(new ModifyGeofencesRequest(*this)); |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 653 | mLocationAPI->modifyGeofences(j, sessions, options); |
| 654 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 655 | } else { |
| 656 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, |
| 657 | mRequestQueues[REQUEST_GEOFENCE].getSession()); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 658 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 659 | |
| 660 | free(sessions); |
| 661 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 662 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | void LocationAPIClientBase::locAPIPauseGeofences(size_t count, uint32_t* ids) |
| 666 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 667 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 668 | if (mLocationAPI) { |
| 669 | uint32_t* sessions = (uint32_t*)malloc(sizeof(uint32_t) * count); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 670 | if (sessions == NULL) { |
| 671 | LOC_LOGE("%s:%d] Failed to allocate %d bytes !", |
| 672 | __FUNCTION__, __LINE__, sizeof(uint32_t) * count); |
Saurabh Srivastava | 2481cf3 | 2017-07-27 17:20:24 +0530 | [diff] [blame] | 673 | pthread_mutex_unlock(&mMutex); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 674 | return; |
| 675 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 676 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 677 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() == GEOFENCE_SESSION_ID) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 678 | size_t j = 0; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 679 | for (size_t i = 0; i < count; i++) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 680 | sessions[j] = mGeofenceBiDict.getSession(ids[i]); |
| 681 | if (sessions[j] > 0) { |
| 682 | j++; |
| 683 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 684 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 685 | if (j > 0) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 686 | mRequestQueues[REQUEST_GEOFENCE].push(new PauseGeofencesRequest(*this)); |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 687 | mLocationAPI->pauseGeofences(j, sessions); |
| 688 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 689 | } else { |
| 690 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, |
| 691 | mRequestQueues[REQUEST_GEOFENCE].getSession()); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 692 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 693 | |
| 694 | free(sessions); |
| 695 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 696 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void LocationAPIClientBase::locAPIResumeGeofences( |
| 700 | size_t count, uint32_t* ids, GeofenceBreachTypeMask* mask) |
| 701 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 702 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 703 | if (mLocationAPI) { |
| 704 | uint32_t* sessions = (uint32_t*)malloc(sizeof(uint32_t) * count); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 705 | if (sessions == NULL) { |
| 706 | LOC_LOGE("%s:%d] Failed to allocate %d bytes !", |
| 707 | __FUNCTION__, __LINE__, sizeof(uint32_t) * count); |
Saurabh Srivastava | 2481cf3 | 2017-07-27 17:20:24 +0530 | [diff] [blame] | 708 | pthread_mutex_unlock(&mMutex); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 709 | return; |
| 710 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 711 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 712 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() == GEOFENCE_SESSION_ID) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 713 | size_t j = 0; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 714 | for (size_t i = 0; i < count; i++) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 715 | sessions[j] = mGeofenceBiDict.getSession(ids[i]); |
| 716 | if (sessions[j] > 0) { |
| 717 | if (mask) { |
| 718 | mGeofenceBiDict.set(ids[i], sessions[j], mask[i]); |
| 719 | } |
| 720 | j++; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 723 | if (j > 0) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 724 | mRequestQueues[REQUEST_GEOFENCE].push(new ResumeGeofencesRequest(*this)); |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 725 | mLocationAPI->resumeGeofences(j, sessions); |
| 726 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 727 | } else { |
| 728 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, |
| 729 | mRequestQueues[REQUEST_GEOFENCE].getSession()); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 730 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 731 | |
| 732 | free(sessions); |
| 733 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 734 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void LocationAPIClientBase::locAPIRemoveAllGeofences() |
| 738 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 739 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 740 | if (mLocationAPI) { |
| 741 | std::vector<uint32_t> sessionsVec = mGeofenceBiDict.getAllSessions(); |
| 742 | size_t count = sessionsVec.size(); |
| 743 | uint32_t* sessions = (uint32_t*)malloc(sizeof(uint32_t) * count); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 744 | if (sessions == NULL) { |
| 745 | LOC_LOGE("%s:%d] Failed to allocate %d bytes !", |
| 746 | __FUNCTION__, __LINE__, sizeof(uint32_t) * count); |
Saurabh Srivastava | 2481cf3 | 2017-07-27 17:20:24 +0530 | [diff] [blame] | 747 | pthread_mutex_unlock(&mMutex); |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 748 | return; |
| 749 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 750 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 751 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() == GEOFENCE_SESSION_ID) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 752 | size_t j = 0; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 753 | for (size_t i = 0; i < count; i++) { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 754 | sessions[j] = sessionsVec[i]; |
| 755 | if (sessions[j] > 0) { |
| 756 | j++; |
| 757 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 758 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 759 | if (j > 0) { |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 760 | mRequestQueues[REQUEST_GEOFENCE].push(new RemoveGeofencesRequest(*this)); |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 761 | mLocationAPI->removeGeofences(j, sessions); |
| 762 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 763 | } else { |
| 764 | LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, |
| 765 | mRequestQueues[REQUEST_GEOFENCE].getSession()); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 766 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 767 | |
| 768 | free(sessions); |
| 769 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 770 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void LocationAPIClientBase::locAPIGnssNiResponse(uint32_t id, GnssNiResponse response) |
| 774 | { |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 775 | pthread_mutex_lock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 776 | if (mLocationAPI) { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 777 | uint32_t session = id; |
| 778 | mLocationAPI->gnssNiResponse(id, response); |
| 779 | LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session); |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 780 | mRequestQueues[REQUEST_NIRESPONSE].reset(session); |
| 781 | mRequestQueues[REQUEST_NIRESPONSE].push(new GnssNiResponseRequest(*this)); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 782 | } |
Baili Feng | b29778e | 2017-05-23 14:32:22 +0800 | [diff] [blame] | 783 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 786 | void LocationAPIClientBase::beforeGeofenceBreachCb( |
| 787 | GeofenceBreachNotification geofenceBreachNotification) |
| 788 | { |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 789 | uint32_t* ids = (uint32_t*)malloc(sizeof(uint32_t) * geofenceBreachNotification.count); |
| 790 | uint32_t* backup = geofenceBreachNotification.ids; |
| 791 | size_t n = geofenceBreachNotification.count; |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 792 | geofenceBreachCallback genfenceCallback = nullptr; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 793 | |
Saurabh Srivastava | 8cf201a | 2017-07-03 17:07:37 +0530 | [diff] [blame] | 794 | if (ids == NULL) { |
| 795 | LOC_LOGE("%s:%d] Failed to alloc %d bytes", |
| 796 | __FUNCTION__, __LINE__, |
| 797 | sizeof(uint32_t) * geofenceBreachNotification.count); |
| 798 | return; |
| 799 | } |
| 800 | |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 801 | pthread_mutex_lock(&mMutex); |
| 802 | if (mGeofenceBreachCallback != nullptr) { |
| 803 | size_t count = 0; |
| 804 | for (size_t i = 0; i < n; i++) { |
| 805 | uint32_t id = mGeofenceBiDict.getId(geofenceBreachNotification.ids[i]); |
| 806 | GeofenceBreachTypeMask type = |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 807 | mGeofenceBiDict.getExtBySession(geofenceBreachNotification.ids[i]); |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 808 | // if type == 0, we will not head into the fllowing block anyway. |
| 809 | // so we don't need to check id and type |
| 810 | if ((geofenceBreachNotification.type == GEOFENCE_BREACH_ENTER && |
| 811 | (type & GEOFENCE_BREACH_ENTER_BIT)) || |
| 812 | (geofenceBreachNotification.type == GEOFENCE_BREACH_EXIT && |
| 813 | (type & GEOFENCE_BREACH_EXIT_BIT)) |
| 814 | ) { |
| 815 | ids[count] = id; |
| 816 | count++; |
| 817 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 818 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 819 | geofenceBreachNotification.count = count; |
| 820 | geofenceBreachNotification.ids = ids; |
| 821 | |
| 822 | genfenceCallback = mGeofenceBreachCallback; |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 823 | } |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 824 | pthread_mutex_unlock(&mMutex); |
| 825 | |
| 826 | if (genfenceCallback != nullptr) { |
| 827 | genfenceCallback(geofenceBreachNotification); |
| 828 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 829 | |
| 830 | // restore ids |
| 831 | geofenceBreachNotification.ids = backup; |
| 832 | geofenceBreachNotification.count = n; |
| 833 | free(ids); |
| 834 | } |
| 835 | |
Bhavna Sharma | 686a5c5 | 2017-05-02 14:29:46 -0700 | [diff] [blame] | 836 | void LocationAPIClientBase::beforeBatchingStatusCb(BatchingStatusInfo batchStatus, |
| 837 | std::list<uint32_t> & tripCompletedList) { |
| 838 | |
| 839 | // map the trip ids to the client ids |
| 840 | std::list<uint32_t> tripCompletedClientIdList; |
| 841 | tripCompletedClientIdList.clear(); |
| 842 | |
| 843 | if (batchStatus.batchingStatus == BATCHING_STATUS_TRIP_COMPLETED) { |
| 844 | for (auto itt = tripCompletedList.begin(); itt != tripCompletedList.end(); itt++) { |
| 845 | if (mSessionBiDict.hasSession(*itt)) { |
| 846 | SessionEntity sessEntity = mSessionBiDict.getExtBySession(*itt); |
| 847 | |
| 848 | if (sessEntity.sessionMode == SESSION_MODE_ON_TRIP_COMPLETED) { |
| 849 | tripCompletedClientIdList.push_back(sessEntity.id); |
| 850 | mSessionBiDict.rmBySession(*itt); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | mBatchingStatusCallback(batchStatus, tripCompletedClientIdList); |
| 857 | } |
| 858 | |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 859 | void LocationAPIClientBase::onResponseCb(LocationError error, uint32_t id) |
| 860 | { |
| 861 | if (error != LOCATION_ERROR_SUCCESS) { |
| 862 | LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, error, id); |
| 863 | } else { |
Baili Feng | 1a128bd | 2017-06-21 17:46:23 +0800 | [diff] [blame] | 864 | LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, error, id); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 865 | } |
| 866 | LocationAPIRequest* request = getRequestBySession(id); |
| 867 | if (request) { |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 868 | request->onResponse(error, id); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 869 | delete request; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | void LocationAPIClientBase::onCollectiveResponseCb( |
| 874 | size_t count, LocationError* errors, uint32_t* ids) |
| 875 | { |
| 876 | for (size_t i = 0; i < count; i++) { |
| 877 | if (errors[i] != LOCATION_ERROR_SUCCESS) { |
| 878 | LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, errors[i], ids[i]); |
| 879 | } else { |
Baili Feng | 1a128bd | 2017-06-21 17:46:23 +0800 | [diff] [blame] | 880 | LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, errors[i], ids[i]); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | LocationAPIRequest* request = nullptr; |
Baili Feng | 1f3ac89 | 2017-05-31 16:22:16 +0800 | [diff] [blame] | 884 | pthread_mutex_lock(&mMutex); |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 885 | if (mRequestQueues[REQUEST_GEOFENCE].getSession() == GEOFENCE_SESSION_ID) { |
| 886 | request = mRequestQueues[REQUEST_GEOFENCE].pop(); |
Baili Feng | 1f3ac89 | 2017-05-31 16:22:16 +0800 | [diff] [blame] | 887 | } |
| 888 | pthread_mutex_unlock(&mMutex); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 889 | if (request) { |
| 890 | request->onCollectiveResponse(count, errors, ids); |
| 891 | delete request; |
| 892 | } |
| 893 | } |
| 894 | |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 895 | void LocationAPIClientBase::removeSession(uint32_t session) { |
| 896 | if (mSessionBiDict.hasSession(session)) { |
| 897 | mSessionBiDict.rmBySession(session); |
| 898 | } |
| 899 | } |
| 900 | |
Baili Feng | 1a128bd | 2017-06-21 17:46:23 +0800 | [diff] [blame] | 901 | LocationAPIRequest* LocationAPIClientBase::getRequestBySession(uint32_t session) |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 902 | { |
| 903 | pthread_mutex_lock(&mMutex); |
| 904 | LocationAPIRequest* request = nullptr; |
| 905 | for (int i = 0; i < REQUEST_MAX; i++) { |
| 906 | if (i != REQUEST_GEOFENCE && |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 907 | i != REQUEST_SESSION && |
Baili Feng | c0a300c | 2017-06-13 15:26:57 +0800 | [diff] [blame] | 908 | mRequestQueues[i].getSession() == session) { |
| 909 | request = mRequestQueues[i].pop(); |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 910 | break; |
| 911 | } |
| 912 | } |
Baili Feng | 2df685d | 2017-07-20 17:02:26 +0800 | [diff] [blame] | 913 | if (request == nullptr) { |
| 914 | // Can't find a request with correct session, |
| 915 | // try to find it from mSessionBiDict |
| 916 | if (mSessionBiDict.hasSession(session)) { |
| 917 | request = mRequestQueues[REQUEST_SESSION].pop(); |
| 918 | } |
| 919 | } |
Dante Russo | c85c8ff | 2017-02-28 16:45:47 -0800 | [diff] [blame] | 920 | pthread_mutex_unlock(&mMutex); |
| 921 | return request; |
| 922 | } |