diff --git a/utils/Android.mk b/utils/Android.mk index ec6bf058..38876965 100644 --- a/utils/Android.mk +++ b/utils/Android.mk @@ -9,6 +9,7 @@ include $(CLEAR_VARS) ## Libs LOCAL_SHARED_LIBRARIES := \ + libdl \ libutils \ libcutils \ liblog \ diff --git a/utils/Makefile.am b/utils/Makefile.am index 33afc1be..50e1e9a1 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -61,7 +61,7 @@ libgps_utils_la_LDFLAGS = -Wl,-z,defs -lpthread -shared -version-info 1:0:0 libgps_utils_la_CPPFLAGS = $(AM_CFLAGS) $(AM_CPPFLAGS) endif -libgps_utils_la_LIBADD = $(CUTILS_LIBS) +libgps_utils_la_LIBADD = $(CUTILS_LIBS) -ldl #Create and Install libraries lib_LTLIBRARIES = libgps_utils.la diff --git a/utils/loc_misc_utils.cpp b/utils/loc_misc_utils.cpp index b7c84061..70fdbc3a 100644 --- a/utils/loc_misc_utils.cpp +++ b/utils/loc_misc_utils.cpp @@ -30,6 +30,7 @@ #define LOG_TAG "LocSvc_misc_utils" #include #include +#include #include #include #include @@ -112,3 +113,33 @@ void loc_util_trim_space(char *org_string) err: return; } + +inline void logDlError(const char* failedCall) { + const char * err = dlerror(); + LOC_LOGe("%s error: %s", failedCall, (nullptr == err) ? "unknown" : err); +} + +void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName) +{ + void* sym = nullptr; + if ((nullptr != libHandle || nullptr != libName) && nullptr != symName) { + if (nullptr == libHandle) { + libHandle = dlopen(libName, RTLD_NOW); + if (nullptr == libHandle) { + logDlError("dlopen"); + } + } + // NOT else, as libHandle gets assigned 5 line above + if (nullptr != libHandle) { + sym = dlsym(libHandle, symName); + if (nullptr == sym) { + logDlError("dlsym"); + } + } + } else { + LOC_LOGe("Either libHandle (%p) or libName (%p) must not be null; " + "symName (%p) can not be null.", libHandle, libName, symName); + } + + return sym; +} diff --git a/utils/loc_misc_utils.h b/utils/loc_misc_utils.h index 7d66d842..fad1b6db 100644 --- a/utils/loc_misc_utils.h +++ b/utils/loc_misc_utils.h @@ -92,6 +92,34 @@ SIDE EFFECTS N/A ===========================================================================*/ void loc_util_trim_space(char *org_string); + +/*=========================================================================== +FUNCTION dlGetSymFromLib + +DESCRIPTION + Handy function to get a pointer to a symbol from a library. + + If libHandle is not null, it will be used as the handle to the library. In + that case libName wll not be used; + libHandle is an in / out parameter. + If libHandle is null, libName will be used to dlopen. + Either libHandle or libName must not be nullptr. + symName must not be null. + +DEPENDENCIES + N/A + +RETURN VALUE + pointer to symName. Could be nullptr if + Parameters are incorrect; or + libName can not be opened; or + symName can not be found. + +SIDE EFFECTS + N/A +===========================================================================*/ +void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName); + #ifdef __cplusplus } #endif