summaryrefslogtreecommitdiff
path: root/location/LocationAPIClientBase.cpp
diff options
context:
space:
mode:
authorBaili Feng <bailif@codeaurora.org>2017-06-21 17:46:23 +0800
committerBaili Feng <bailif@codeaurora.org>2017-06-27 19:04:00 +0800
commit1a128bd7cdcaecd2ba0528dd4e5fa396f3e49662 (patch)
tree7a54b29a4e7588765203a4a74e74a060ff7c5a2b /location/LocationAPIClientBase.cpp
parent1e6d1c02ce85a395313b90b2c25999da8681f997 (diff)
downloadgps-1a128bd7cdcaecd2ba0528dd4e5fa396f3e49662.tar.gz
Split control apis to a separate class
Move control apis to LocationAPIControlClient. There should be only one instance of it per process. Change-Id: I5969e6b3ad45f3fc5bfe4f4cd543a077c540c3ea CRs-Fixed: 2064389
Diffstat (limited to 'location/LocationAPIClientBase.cpp')
-rw-r--r--location/LocationAPIClientBase.cpp335
1 files changed, 186 insertions, 149 deletions
diff --git a/location/LocationAPIClientBase.cpp b/location/LocationAPIClientBase.cpp
index dcf1971..bf1a8ad 100644
--- a/location/LocationAPIClientBase.cpp
+++ b/location/LocationAPIClientBase.cpp
@@ -37,12 +37,192 @@
#define GEOFENCE_SESSION_ID 0xFFFFFFFF
#define CONFIG_SESSION_ID 0xFFFFFFFF
+// LocationAPIControlClient
+LocationAPIControlClient::LocationAPIControlClient() :
+ mEnabled(false)
+{
+ pthread_mutex_init(&mMutex, nullptr);
+
+ for (int i = 0; i < CTRL_REQUEST_MAX; i++) {
+ mRequestQueues[i].reset(0);
+ }
+
+ memset(&mConfig, 0, sizeof(GnssConfig));
+
+ LocationControlCallbacks locationControlCallbacks;
+ locationControlCallbacks.size = sizeof(LocationControlCallbacks);
+
+ locationControlCallbacks.responseCb =
+ [this](LocationError error, uint32_t id) {
+ onCtrlResponseCb(error, id);
+ };
+ locationControlCallbacks.collectiveResponseCb =
+ [this](size_t count, LocationError* errors, uint32_t* ids) {
+ onCtrlCollectiveResponseCb(count, errors, ids);
+ };
+
+ mLocationControlAPI = LocationControlAPI::createInstance(locationControlCallbacks);
+}
+
+LocationAPIControlClient::~LocationAPIControlClient()
+{
+ pthread_mutex_lock(&mMutex);
+
+ if (mLocationControlAPI) {
+ mLocationControlAPI->destroy();
+ mLocationControlAPI = nullptr;
+ }
+
+ for (int i = 0; i < CTRL_REQUEST_MAX; i++) {
+ mRequestQueues[i].reset(0);
+ }
+
+ pthread_mutex_unlock(&mMutex);
+
+ pthread_mutex_destroy(&mMutex);
+}
+
+uint32_t LocationAPIControlClient::locAPIGnssDeleteAidingData(GnssAidingData& data)
+{
+ uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE;
+ pthread_mutex_lock(&mMutex);
+ if (mLocationControlAPI) {
+ uint32_t session = mLocationControlAPI->gnssDeleteAidingData(data);
+ LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session);
+ mRequestQueues[CTRL_REQUEST_DELETEAIDINGDATA].reset(session);
+ mRequestQueues[CTRL_REQUEST_DELETEAIDINGDATA].push(new GnssDeleteAidingDataRequest(*this));
+
+ retVal = LOCATION_ERROR_SUCCESS;
+ }
+ pthread_mutex_unlock(&mMutex);
+
+ return retVal;
+}
+
+uint32_t LocationAPIControlClient::locAPIEnable(LocationTechnologyType techType)
+{
+ uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE;
+ pthread_mutex_lock(&mMutex);
+ if (mEnabled) {
+ // just return success if already enabled
+ retVal = LOCATION_ERROR_SUCCESS;
+ } else if (mLocationControlAPI) {
+ uint32_t session = mLocationControlAPI->enable(techType);
+ LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session);
+ mRequestQueues[CTRL_REQUEST_CONTROL].reset(session);
+ mRequestQueues[CTRL_REQUEST_CONTROL].push(new EnableRequest(*this));
+ retVal = LOCATION_ERROR_SUCCESS;
+ mEnabled = true;
+ } else {
+ LOC_LOGE("%s:%d] failed.", __FUNCTION__, __LINE__);
+ }
+ pthread_mutex_unlock(&mMutex);
+
+ return retVal;
+}
+
+void LocationAPIControlClient::locAPIDisable()
+{
+ pthread_mutex_lock(&mMutex);
+ if (mEnabled && mLocationControlAPI) {
+ uint32_t session = 0;
+ session = mRequestQueues[CTRL_REQUEST_CONTROL].getSession();
+ if (session > 0) {
+ mRequestQueues[CTRL_REQUEST_CONTROL].push(new DisableRequest(*this));
+ mLocationControlAPI->disable(session);
+ mEnabled = false;
+ } else {
+ LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, session);
+ }
+ }
+ pthread_mutex_unlock(&mMutex);
+}
+
+uint32_t LocationAPIControlClient::locAPIGnssUpdateConfig(GnssConfig config)
+{
+ uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE;
+ if (memcmp(&mConfig, &config, sizeof(GnssConfig)) == 0) {
+ LOC_LOGV("%s:%d] GnssConfig is identical to previous call", __FUNCTION__, __LINE__);
+ retVal = LOCATION_ERROR_SUCCESS;
+ return retVal;
+ }
+
+ pthread_mutex_lock(&mMutex);
+ if (mLocationControlAPI) {
+
+ memcpy(&mConfig, &config, sizeof(GnssConfig));
+
+ uint32_t session = 0;
+ uint32_t* idArray = mLocationControlAPI->gnssUpdateConfig(config);
+ LOC_LOGV("%s:%d] gnssUpdateConfig return array: %p", __FUNCTION__, __LINE__, idArray);
+ if (idArray != nullptr) {
+ if (mRequestQueues[CTRL_REQUEST_CONFIG].getSession() != CONFIG_SESSION_ID) {
+ mRequestQueues[CTRL_REQUEST_CONFIG].reset(CONFIG_SESSION_ID);
+ }
+ mRequestQueues[CTRL_REQUEST_CONFIG].push(new GnssUpdateConfigRequest(*this));
+ retVal = LOCATION_ERROR_SUCCESS;
+ }
+ }
+ pthread_mutex_unlock(&mMutex);
+ return retVal;
+}
+
+void LocationAPIControlClient::onCtrlResponseCb(LocationError error, uint32_t id)
+{
+ if (error != LOCATION_ERROR_SUCCESS) {
+ LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, error, id);
+ } else {
+ LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, error, id);
+ }
+ LocationAPIRequest* request = getRequestBySession(id);
+ if (request) {
+ request->onResponse(error);
+ delete request;
+ }
+}
+
+void LocationAPIControlClient::onCtrlCollectiveResponseCb(
+ size_t count, LocationError* errors, uint32_t* ids)
+{
+ for (size_t i = 0; i < count; i++) {
+ if (errors[i] != LOCATION_ERROR_SUCCESS) {
+ LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
+ } else {
+ LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
+ }
+ }
+ LocationAPIRequest* request = nullptr;
+ pthread_mutex_lock(&mMutex);
+ if (mRequestQueues[CTRL_REQUEST_CONFIG].getSession() == CONFIG_SESSION_ID) {
+ request = mRequestQueues[CTRL_REQUEST_CONFIG].pop();
+ }
+ pthread_mutex_unlock(&mMutex);
+ if (request) {
+ request->onCollectiveResponse(count, errors, ids);
+ delete request;
+ }
+}
+
+LocationAPIRequest* LocationAPIControlClient::getRequestBySession(uint32_t session)
+{
+ pthread_mutex_lock(&mMutex);
+ LocationAPIRequest* request = nullptr;
+ for (int i = 0; i < CTRL_REQUEST_MAX; i++) {
+ if (i != CTRL_REQUEST_CONFIG &&
+ mRequestQueues[i].getSession() == session) {
+ request = mRequestQueues[i].pop();
+ break;
+ }
+ }
+ pthread_mutex_unlock(&mMutex);
+ return request;
+}
+
+// LocationAPIClientBase
LocationAPIClientBase::LocationAPIClientBase() :
mGeofenceBreachCallback(nullptr),
mLocationAPI(nullptr),
- mLocationControlAPI(nullptr),
- mBatchSize(-1),
- mEnabled(false)
+ mBatchSize(-1)
{
// use recursive mutex, in case callback come from the same thread
@@ -54,8 +234,6 @@ LocationAPIClientBase::LocationAPIClientBase() :
for (int i = 0; i < REQUEST_MAX; i++) {
mRequestQueues[i].reset(0);
}
-
- memset(&mConfig, 0, sizeof(GnssConfig));
}
void LocationAPIClientBase::locAPISetCallbacks(LocationCallbacks& locationCallbacks)
@@ -88,22 +266,6 @@ void LocationAPIClientBase::locAPISetCallbacks(LocationCallbacks& locationCallba
mLocationAPI->updateCallbacks(locationCallbacks);
}
- if (mLocationControlAPI == nullptr) {
- LocationControlCallbacks locationControlCallbacks;
- locationControlCallbacks.size = sizeof(LocationControlCallbacks);
-
- locationControlCallbacks.responseCb =
- [this](LocationError error, uint32_t id) {
- onCtrlResponseCb(error, id);
- };
- locationControlCallbacks.collectiveResponseCb =
- [this](size_t count, LocationError* errors, uint32_t* ids) {
- onCtrlCollectiveResponseCb(count, errors, ids);
- };
-
- mLocationControlAPI = LocationControlAPI::createInstance(locationControlCallbacks);
- }
-
pthread_mutex_unlock(&mMutex);
}
@@ -117,10 +279,6 @@ LocationAPIClientBase::~LocationAPIClientBase()
mLocationAPI->destroy();
mLocationAPI = nullptr;
}
- if (mLocationControlAPI) {
- mLocationControlAPI->destroy();
- mLocationControlAPI = nullptr;
- }
for (int i = 0; i < REQUEST_MAX; i++) {
mRequestQueues[i].reset(0);
@@ -581,89 +739,6 @@ void LocationAPIClientBase::locAPIGnssNiResponse(uint32_t id, GnssNiResponse res
pthread_mutex_unlock(&mMutex);
}
-uint32_t LocationAPIClientBase::locAPIGnssDeleteAidingData(GnssAidingData& data)
-{
- uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE;
- pthread_mutex_lock(&mMutex);
- if (mLocationControlAPI) {
- uint32_t session = mLocationControlAPI->gnssDeleteAidingData(data);
- LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session);
- mRequestQueues[REQUEST_DELETEAIDINGDATA].reset(session);
- mRequestQueues[REQUEST_DELETEAIDINGDATA].push(new GnssDeleteAidingDataRequest(*this));
-
- retVal = LOCATION_ERROR_SUCCESS;
- }
- pthread_mutex_unlock(&mMutex);
-
- return retVal;
-}
-
-uint32_t LocationAPIClientBase::locAPIEnable(LocationTechnologyType techType)
-{
- uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE;
- pthread_mutex_lock(&mMutex);
- if (mEnabled) {
- // just return success if already enabled
- retVal = LOCATION_ERROR_SUCCESS;
- } else if (mLocationControlAPI) {
- uint32_t session = mLocationControlAPI->enable(techType);
- LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session);
- mRequestQueues[REQUEST_CONTROL].reset(session);
- mRequestQueues[REQUEST_CONTROL].push(new EnableRequest(*this));
- retVal = LOCATION_ERROR_SUCCESS;
- mEnabled = true;
- }
- pthread_mutex_unlock(&mMutex);
-
- return retVal;
-}
-
-void LocationAPIClientBase::locAPIDisable()
-{
- pthread_mutex_lock(&mMutex);
- if (mEnabled && mLocationControlAPI) {
- uint32_t session = 0;
- session = mRequestQueues[REQUEST_CONTROL].getSession();
- if (session > 0) {
- mRequestQueues[REQUEST_CONTROL].push(new DisableRequest(*this));
- mLocationControlAPI->disable(session);
- mEnabled = false;
- } else {
- LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, session);
- }
- }
- pthread_mutex_unlock(&mMutex);
-}
-
-uint32_t LocationAPIClientBase::locAPIGnssUpdateConfig(GnssConfig config)
-{
- uint32_t retVal = LOCATION_ERROR_GENERAL_FAILURE;
- if (memcmp(&mConfig, &config, sizeof(GnssConfig)) == 0) {
- LOC_LOGV("%s:%d] GnssConfig is identical to previous call", __FUNCTION__, __LINE__);
- retVal = LOCATION_ERROR_SUCCESS;
- return retVal;
- }
-
- pthread_mutex_lock(&mMutex);
- if (mLocationControlAPI) {
-
- memcpy(&mConfig, &config, sizeof(GnssConfig));
-
- uint32_t session = 0;
- uint32_t* idArray = mLocationControlAPI->gnssUpdateConfig(config);
- LOC_LOGV("%s:%d] gnssUpdateConfig return array: %p", __FUNCTION__, __LINE__, idArray);
- if (idArray != nullptr) {
- if (mRequestQueues[REQUEST_CONFIG].getSession() != CONFIG_SESSION_ID) {
- mRequestQueues[REQUEST_CONFIG].reset(CONFIG_SESSION_ID);
- }
- mRequestQueues[REQUEST_CONFIG].push(new GnssUpdateConfigRequest(*this));
- retVal = LOCATION_ERROR_SUCCESS;
- }
- }
- pthread_mutex_unlock(&mMutex);
- return retVal;
-}
-
void LocationAPIClientBase::beforeGeofenceBreachCb(
GeofenceBreachNotification geofenceBreachNotification)
{
@@ -712,7 +787,7 @@ void LocationAPIClientBase::onResponseCb(LocationError error, uint32_t id)
if (error != LOCATION_ERROR_SUCCESS) {
LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, error, id);
} else {
- LOC_LOGV("%s:%d] error: %d id: %d", __FUNCTION__, __LINE__, error, id);
+ LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, error, id);
}
LocationAPIRequest* request = getRequestBySession(id);
if (request) {
@@ -728,7 +803,7 @@ void LocationAPIClientBase::onCollectiveResponseCb(
if (errors[i] != LOCATION_ERROR_SUCCESS) {
LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
} else {
- LOC_LOGV("%s:%d] error: %d id: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
+ LOC_LOGV("%s:%d] SUCCESS: %d id: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
}
}
LocationAPIRequest* request = nullptr;
@@ -743,50 +818,12 @@ void LocationAPIClientBase::onCollectiveResponseCb(
}
}
-void LocationAPIClientBase::onCtrlResponseCb(LocationError error, uint32_t id)
-{
- if (error != LOCATION_ERROR_SUCCESS) {
- LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, error, id);
- } else {
- LOC_LOGV("%s:%d] error: %d id: %d", __FUNCTION__, __LINE__, error, id);
- }
- LocationAPIRequest* request = getRequestBySession(id);
- if (request) {
- request->onResponse(error);
- delete request;
- }
-}
-
-void LocationAPIClientBase::onCtrlCollectiveResponseCb(
- size_t count, LocationError* errors, uint32_t* ids)
-{
- for (size_t i = 0; i < count; i++) {
- if (errors[i] != LOCATION_ERROR_SUCCESS) {
- LOC_LOGE("%s:%d] ERROR: %d ID: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
- } else {
- LOC_LOGV("%s:%d] error: %d id: %d", __FUNCTION__, __LINE__, errors[i], ids[i]);
- }
- }
- LocationAPIRequest* request = nullptr;
- pthread_mutex_lock(&mMutex);
- if (mRequestQueues[REQUEST_CONFIG].getSession() == CONFIG_SESSION_ID) {
- request = mRequestQueues[REQUEST_CONFIG].pop();
- }
- pthread_mutex_unlock(&mMutex);
- if (request) {
- request->onCollectiveResponse(count, errors, ids);
- delete request;
- }
-}
-
-LocationAPIClientBase::LocationAPIRequest*
-LocationAPIClientBase::getRequestBySession(uint32_t session)
+LocationAPIRequest* LocationAPIClientBase::getRequestBySession(uint32_t session)
{
pthread_mutex_lock(&mMutex);
LocationAPIRequest* request = nullptr;
for (int i = 0; i < REQUEST_MAX; i++) {
if (i != REQUEST_GEOFENCE &&
- i != REQUEST_CONFIG &&
mRequestQueues[i].getSession() == session) {
request = mRequestQueues[i].pop();
break;