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