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