summaryrefslogtreecommitdiff
path: root/location/LocationAPI.cpp
diff options
context:
space:
mode:
authorHoss Zhou <zhouh@codeaurora.org>2019-04-04 14:22:23 +0800
committerHoss Zhou <zhouh@codeaurora.org>2019-04-10 13:52:29 +0800
commit313284b9ddbad37302a90bd53521f1046ba91834 (patch)
treea7c258ea0c689015aec02ccb43fcd8510faca67a /location/LocationAPI.cpp
parenta6c61bde6a815aa723fd3dc144875e5758349955 (diff)
downloadgps-313284b9ddbad37302a90bd53521f1046ba91834.tar.gz
fix CFI issues
Change-Id: I5b94804c381f1d26196ddf6dee97adbee36dc128 CRs-Fixed: 2353650
Diffstat (limited to 'location/LocationAPI.cpp')
-rw-r--r--location/LocationAPI.cpp63
1 files changed, 29 insertions, 34 deletions
diff --git a/location/LocationAPI.cpp b/location/LocationAPI.cpp
index f3088bc..a43178d 100644
--- a/location/LocationAPI.cpp
+++ b/location/LocationAPI.cpp
@@ -34,8 +34,11 @@
#include <log_util.h>
#include <pthread.h>
#include <map>
+#include <loc_misc_utils.h>
-typedef void* (getLocationInterface)();
+typedef const GnssInterface* (getGnssInterface)();
+typedef const GeofenceInterface* (getGeofenceInterface)();
+typedef const BatchingInterface* (getBatchingInterface)();
typedef struct {
// bit mask of the adpaters that we need to wait for the removeClientCompleteCallback
@@ -66,6 +69,17 @@ static bool gGnssLoadFailed = false;
static bool gBatchingLoadFailed = false;
static bool gGeofenceLoadFailed = false;
+template <typename T1, typename T2>
+static const T1* loadLocationInterface(const char* library, const char* name) {
+ void* libhandle = nullptr;
+ T2* getter = (T2*)dlGetSymFromLib(libhandle, library, name);
+ if (nullptr == getter) {
+ return (const T1*) getter;
+ }else {
+ return (*getter)();
+ }
+}
+
static bool isGnssClient(LocationCallbacks& locationCallbacks)
{
return (locationCallbacks.gnssNiCb != nullptr ||
@@ -85,32 +99,6 @@ static bool isGeofenceClient(LocationCallbacks& locationCallbacks)
locationCallbacks.geofenceStatusCb != nullptr);
}
-static void* loadLocationInterface(const char* library, const char* name)
-{
- LOC_LOGD("%s]: loading %s::%s ...", __func__, library, name);
- if (NULL == library || NULL == name) {
- return NULL;
- }
- getLocationInterface* getter = NULL;
- const char *error = NULL;
- dlerror();
- void *handle = dlopen(library, RTLD_NOW);
- if (NULL == handle || (error = dlerror()) != NULL) {
- LOC_LOGW("dlopen for %s failed, error = %s", library, error);
- } else {
- getter = (getLocationInterface*)dlsym(handle, name);
- if ((error = dlerror()) != NULL) {
- LOC_LOGW("dlsym for %s::%s failed, error = %s", library, name, error);
- getter = NULL;
- }
- }
-
- if (NULL == getter) {
- return (void*)getter;
- } else {
- return (*getter)();
- }
-}
void LocationAPI::onRemoveClientCompleteCb (LocationAdapterTypeMask adapterType)
{
@@ -170,7 +158,8 @@ LocationAPI::createInstance(LocationCallbacks& locationCallbacks)
if (isGnssClient(locationCallbacks)) {
if (NULL == gData.gnssInterface && !gGnssLoadFailed) {
gData.gnssInterface =
- (GnssInterface*)loadLocationInterface("libgnss.so", "getGnssInterface");
+ (GnssInterface*)loadLocationInterface<GnssInterface,
+ getGnssInterface>("libgnss.so", "getGnssInterface");
if (NULL == gData.gnssInterface) {
gGnssLoadFailed = true;
LOC_LOGW("%s:%d]: No gnss interface available", __func__, __LINE__);
@@ -190,7 +179,8 @@ LocationAPI::createInstance(LocationCallbacks& locationCallbacks)
if (isBatchingClient(locationCallbacks)) {
if (NULL == gData.batchingInterface && !gBatchingLoadFailed) {
gData.batchingInterface =
- (BatchingInterface*)loadLocationInterface("libbatching.so", "getBatchingInterface");
+ (BatchingInterface*)loadLocationInterface<BatchingInterface,
+ getBatchingInterface>("libbatching.so", "getBatchingInterface");
if (NULL == gData.batchingInterface) {
gBatchingLoadFailed = true;
LOC_LOGW("%s:%d]: No batching interface available", __func__, __LINE__);
@@ -210,7 +200,8 @@ LocationAPI::createInstance(LocationCallbacks& locationCallbacks)
if (isGeofenceClient(locationCallbacks)) {
if (NULL == gData.geofenceInterface && !gGeofenceLoadFailed) {
gData.geofenceInterface =
- (GeofenceInterface*)loadLocationInterface("libgeofencing.so", "getGeofenceInterface");
+ (GeofenceInterface*)loadLocationInterface<GeofenceInterface,
+ getGeofenceInterface>("libgeofencing.so", "getGeofenceInterface");
if (NULL == gData.geofenceInterface) {
gGeofenceLoadFailed = true;
LOC_LOGW("%s:%d]: No geofence interface available", __func__, __LINE__);
@@ -324,7 +315,8 @@ LocationAPI::updateCallbacks(LocationCallbacks& locationCallbacks)
if (isGnssClient(locationCallbacks)) {
if (NULL == gData.gnssInterface && !gGnssLoadFailed) {
gData.gnssInterface =
- (GnssInterface*)loadLocationInterface("libgnss.so", "getGnssInterface");
+ (GnssInterface*)loadLocationInterface<GnssInterface,
+ getGnssInterface>("libgnss.so", "getGnssInterface");
if (NULL == gData.gnssInterface) {
gGnssLoadFailed = true;
LOC_LOGW("%s:%d]: No gnss interface available", __func__, __LINE__);
@@ -341,7 +333,8 @@ LocationAPI::updateCallbacks(LocationCallbacks& locationCallbacks)
if (isBatchingClient(locationCallbacks)) {
if (NULL == gData.batchingInterface && !gBatchingLoadFailed) {
gData.batchingInterface =
- (BatchingInterface*)loadLocationInterface("libbatching.so", "getBatchingInterface");
+ (BatchingInterface*)loadLocationInterface<BatchingInterface,
+ getBatchingInterface>("libbatching.so", "getBatchingInterface");
if (NULL == gData.batchingInterface) {
gBatchingLoadFailed = true;
LOC_LOGW("%s:%d]: No batching interface available", __func__, __LINE__);
@@ -358,7 +351,8 @@ LocationAPI::updateCallbacks(LocationCallbacks& locationCallbacks)
if (isGeofenceClient(locationCallbacks)) {
if (NULL == gData.geofenceInterface && !gGeofenceLoadFailed) {
gData.geofenceInterface =
- (GeofenceInterface*)loadLocationInterface("libgeofencing.so", "getGeofenceInterface");
+ (GeofenceInterface*)loadLocationInterface<GeofenceInterface,
+ getGeofenceInterface>("libgeofencing.so", "getGeofenceInterface");
if (NULL == gData.geofenceInterface) {
gGeofenceLoadFailed = true;
LOC_LOGW("%s:%d]: No geofence interface available", __func__, __LINE__);
@@ -606,7 +600,8 @@ LocationControlAPI::createInstance(LocationControlCallbacks& locationControlCall
if (nullptr != locationControlCallbacks.responseCb && NULL == gData.controlAPI) {
if (NULL == gData.gnssInterface && !gGnssLoadFailed) {
gData.gnssInterface =
- (GnssInterface*)loadLocationInterface("libgnss.so", "getGnssInterface");
+ (GnssInterface*)loadLocationInterface<GnssInterface,
+ getGnssInterface>("libgnss.so", "getGnssInterface");
if (NULL == gData.gnssInterface) {
gGnssLoadFailed = true;
LOC_LOGW("%s:%d]: No gnss interface available", __func__, __LINE__);