From 4e3c5d954884786d1faddf7ab4bcb2d9af2fe5fc Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 19 Oct 2017 16:58:51 +0200 Subject: [PATCH] fix(PlatformHelpers): clean code, fix coding style, use cpp logger... --- src/CMakeLists.txt | 26 ++-- src/core/core.cpp | 3 +- src/core/core.h | 4 +- ...lpers.cpp => android-platform-helpers.cpp} | 140 +++++++++--------- .../platform-helpers/platform-helpers.cpp | 75 +++++----- src/core/platform-helpers/platform-helpers.h | 52 ++++--- 6 files changed, 152 insertions(+), 148 deletions(-) rename src/core/platform-helpers/{android-helpers.cpp => android-platform-helpers.cpp} (59%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aa0916e7b..0ea9757d2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -221,47 +221,47 @@ if (APPLE) list(APPEND LINPHONE_OBJC_SOURCE_FILES core/paths/paths-apple.mm) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-apple.h) -elseif(ANDROID) - list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths-android.cpp core/platform-helpers/android-helpers.cpp) +elseif (ANDROID) + list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths-android.cpp core/platform-helpers/android-platform-helpers.cpp) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-android.h) -elseif(WIN32) +elseif (WIN32) list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths-windows.cpp) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-windows.h) -elseif(UNIX) +elseif (UNIX) list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths-linux.cpp) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-linux.h) -endif() +endif () set(LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${BELR_INCLUDE_DIRS} ${LIBXSD_INCLUDE_DIRS}) set(LINPHONE_CXX_OBJECTS_DEFINITIONS "-DLIBLINPHONE_EXPORTS") set(LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${BELR_INCLUDE_DIRS}) -if(SOCI_FOUND) +if (SOCI_FOUND) list(APPEND LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${SOCI_INCLUDE_DIRS} ${SOCI_MYSQL_INCLUDES}) add_definitions(-DSOCI_ENABLED) -endif() +endif () set(LINPHONE_PRIVATE_HEADER_FILES) -foreach(header ${LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES}) +foreach (header ${LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES}) list(APPEND LINPHONE_PRIVATE_HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${header}") -endforeach() +endforeach () set(LINPHONE_PRIVATE_HEADER_FILES ${LINPHONE_PRIVATE_HEADER_FILES} PARENT_SCOPE) bc_apply_compile_flags(LINPHONE_CXX_OBJECTS_SOURCE_FILES STRICT_OPTIONS_CPP STRICT_OPTIONS_CXX) bc_apply_compile_flags(LINPHONE_OBJC_SOURCE_FILES STRICT_OPTIONS_CPP STRICT_OPTIONS_CXX STRICT_OPTIONS_OBJC) -if(ENABLE_STATIC) +if (ENABLE_STATIC) add_library( linphone-cxx-objects-static OBJECT ${LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES} ${LINPHONE_CXX_OBJECTS_SOURCE_FILES} ${LINPHONE_OBJC_SOURCE_FILES} ) target_compile_definitions(linphone-cxx-objects-static PRIVATE ${LINPHONE_CXX_OBJECTS_DEFINITIONS}) target_include_directories(linphone-cxx-objects-static SYSTEM PRIVATE ${LINPHONE_CXX_OBJECTS_INCLUDE_DIRS} ${LINPHONE_INCLUDE_DIRS}) -endif() +endif () -if(ENABLE_SHARED) +if (ENABLE_SHARED) add_library( linphone-cxx-objects OBJECT ${LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES} ${LINPHONE_CXX_OBJECTS_SOURCE_FILES} ${LINPHONE_OBJC_SOURCE_FILES} @@ -269,4 +269,4 @@ if(ENABLE_SHARED) target_compile_definitions(linphone-cxx-objects PRIVATE ${LINPHONE_CXX_OBJECTS_DEFINITIONS}) target_include_directories(linphone-cxx-objects SYSTEM PRIVATE ${LINPHONE_CXX_OBJECTS_INCLUDE_DIRS} ${LINPHONE_INCLUDE_DIRS}) target_compile_options(linphone-cxx-objects PRIVATE "-fPIC") -endif() +endif () diff --git a/src/core/core.cpp b/src/core/core.cpp index ac7de2350..65832c42f 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -19,14 +19,13 @@ #include -#include "address/address.h" #include "chat/chat-room/basic-chat-room.h" #include "core-p.h" #include "db/main-db.h" -#include "linphone/core.h" #include "object/object-p.h" #include "paths/paths.h" +// TODO: Remove me later. #include "private.h" // ============================================================================= diff --git a/src/core/core.h b/src/core/core.h index 1e1c169a5..0aeff72e4 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -22,10 +22,12 @@ #include -#include "chat/chat-room/chat-room.h" +#include "object/object.h" // ============================================================================= +L_DECL_C_STRUCT(LinphoneCore); + LINPHONE_BEGIN_NAMESPACE class ChatRoom; diff --git a/src/core/platform-helpers/android-helpers.cpp b/src/core/platform-helpers/android-platform-helpers.cpp similarity index 59% rename from src/core/platform-helpers/android-helpers.cpp rename to src/core/platform-helpers/android-platform-helpers.cpp index b4fd1b1e6..4aee2bf64 100644 --- a/src/core/platform-helpers/android-helpers.cpp +++ b/src/core/platform-helpers/android-platform-helpers.cpp @@ -1,35 +1,41 @@ /* -linphone -Copyright (C) 2017 Belledonne Communications SARL - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#include "linphone/utils/utils.h" - -#include "private.h" - -#ifdef __ANDROID__ + * android-platform-helpers.h + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ #include +#include "platform-helpers.h" +#include "logger/logger.h" + +// TODO: Remove me later. +#include "private.h" + +// ============================================================================= + +using namespace std; + LINPHONE_BEGIN_NAMESPACE class AndroidPlatformHelpers : public PlatformHelpers { public: - AndroidPlatformHelpers (LinphoneCore *lc, void *system_context); + AndroidPlatformHelpers (LinphoneCore *lc, void *systemContext); + ~AndroidPlatformHelpers (); + void setDnsServers () override; void acquireWifiLock () override; void releaseWifiLock () override; @@ -37,9 +43,9 @@ public: void releaseMcastLock () override; void acquireCpuLock () override; void releaseCpuLock () override; - std::string getDataPath () override; - std::string getConfigPath () override; - ~AndroidPlatformHelpers (); + string getDataPath () override; + string getConfigPath () override; + private: int callVoidMethod (jmethodID id); static jmethodID getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature); @@ -54,40 +60,37 @@ private: jmethodID mGetPowerManagerId; jmethodID mGetDataPathId; jmethodID mGetConfigPathId; - }; -static const char* GetStringUTFChars (JNIEnv* env, jstring string) { - const char *cstring = string ? env->GetStringUTFChars(string, NULL) : NULL; - return cstring; +static const char *GetStringUTFChars (JNIEnv *env, jstring string) { + const char *cstring = string ? env->GetStringUTFChars(string, nullptr) : nullptr; + return cstring; } -static void ReleaseStringUTFChars (JNIEnv* env, jstring string, const char *cstring) { - if (string) env->ReleaseStringUTFChars(string, cstring); +static void ReleaseStringUTFChars (JNIEnv *env, jstring string, const char *cstring) { + if (string) env->ReleaseStringUTFChars(string, cstring); } jmethodID AndroidPlatformHelpers::getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature) { jmethodID id = env->GetMethodID(klass, method, signature); - if (id == 0) { - ms_fatal("Could not find java method '%s %s'", method, signature); - } + if (id == 0) + lFatal() << "Could not find java method: `" << method << ", " << signature << "`."; return id; } -AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *system_context) : PlatformHelpers(lc) { - JNIEnv *env=ms_get_jni_env(); +AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *systemContext) : PlatformHelpers(lc) { + JNIEnv *env = ms_get_jni_env(); jclass klass = env->FindClass("org/linphone/core/tools/AndroidPlatformHelper"); - if (!klass) { - ms_fatal("Could not find java AndroidPlatformHelper class"); - return; - } - jmethodID ctor = env->GetMethodID(klass,"", "(Ljava/lang/Object;)V"); - mJavaHelper = env->NewObject(klass, ctor, (jobject)system_context); + if (!klass) + lFatal() << "Could not find java AndroidPlatformHelper class."; + + jmethodID ctor = env->GetMethodID(klass, "", "(Ljava/lang/Object;)V"); + mJavaHelper = env->NewObject(klass, ctor, (jobject)systemContext); if (!mJavaHelper) { - ms_error("Could not instanciate AndroidPlatformHelper object."); + lError() << "Could not instanciate AndroidPlatformHelper object."; return; } - mJavaHelper = (jobject) env->NewGlobalRef(mJavaHelper); + mJavaHelper = (jobject)env->NewGlobalRef(mJavaHelper); mWifiLockAcquireId = getMethodId(env, klass, "acquireWifiLock", "()V"); mWifiLockReleaseId = getMethodId(env, klass, "releaseWifiLock", "()V"); @@ -100,10 +103,10 @@ AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *system_c mGetDataPathId = getMethodId(env, klass, "getDataPath", "()Ljava/lang/String;"); mGetConfigPathId = getMethodId(env, klass, "getConfigPath", "()Ljava/lang/String;"); - jobject pm = env->CallObjectMethod(mJavaHelper,mGetPowerManagerId); + jobject pm = env->CallObjectMethod(mJavaHelper, mGetPowerManagerId); belle_sip_wake_lock_init(env, pm); - ms_message("AndroidPlatformHelpers is fully initialised"); + lInfo() << "AndroidPlatformHelpers is fully initialised."; } AndroidPlatformHelpers::~AndroidPlatformHelpers () { @@ -111,28 +114,27 @@ AndroidPlatformHelpers::~AndroidPlatformHelpers () { JNIEnv *env = ms_get_jni_env(); belle_sip_wake_lock_uninit(env); env->DeleteGlobalRef(mJavaHelper); - mJavaHelper = NULL; + mJavaHelper = nullptr; } } - void AndroidPlatformHelpers::setDnsServers () { if (!mJavaHelper) return; - JNIEnv *env=ms_get_jni_env(); + JNIEnv *env = ms_get_jni_env(); if (env && mJavaHelper) { - jobjectArray jservers = (jobjectArray)env->CallObjectMethod(mJavaHelper,mGetDnsServersId); - bctbx_list_t *l = NULL; + jobjectArray jservers = (jobjectArray)env->CallObjectMethod(mJavaHelper, mGetDnsServersId); + bctbx_list_t *l = nullptr; if (env->ExceptionCheck()) { env->ExceptionClear(); - ms_error("AndroidPlatformHelpers::setDnsServers() exception"); + lError() << "AndroidPlatformHelpers::setDnsServers() exception."; return; } - if (jservers != NULL) { + if (jservers != nullptr) { int count = env->GetArrayLength(jservers); - for (int i=0; i < count; i++) { - jstring jserver = (jstring) env->GetObjectArrayElement(jservers, i); - const char *str = env->GetStringUTFChars(jserver, NULL); + for (int i = 0; i < count; i++) { + jstring jserver = (jstring)env->GetObjectArrayElement(jservers, i); + const char *str = env->GetStringUTFChars(jserver, nullptr); if (str) { l = bctbx_list_append(l, ms_strdup(str)); env->ReleaseStringUTFChars(jserver, str); @@ -168,28 +170,28 @@ void AndroidPlatformHelpers::releaseCpuLock () { callVoidMethod(mCpuLockReleaseId); } -std::string AndroidPlatformHelpers::getDataPath () { +string AndroidPlatformHelpers::getDataPath () { JNIEnv *env = ms_get_jni_env(); - jstring jdata_path = (jstring)env->CallObjectMethod(mJavaHelper,mGetDataPathId); + jstring jdata_path = (jstring)env->CallObjectMethod(mJavaHelper, mGetDataPathId); const char *data_path = GetStringUTFChars(env, jdata_path); - std::string dataPath = data_path; + string dataPath = data_path; ReleaseStringUTFChars(env, jdata_path, data_path); return dataPath; } -std::string AndroidPlatformHelpers::getConfigPath () { +string AndroidPlatformHelpers::getConfigPath () { JNIEnv *env = ms_get_jni_env(); - jstring jconfig_path = (jstring)env->CallObjectMethod(mJavaHelper,mGetConfigPathId); + jstring jconfig_path = (jstring)env->CallObjectMethod(mJavaHelper, mGetConfigPathId); const char *config_path = GetStringUTFChars(env, jconfig_path); - std::string configPath = config_path; + string configPath = config_path; ReleaseStringUTFChars(env, jconfig_path, config_path); return configPath; } int AndroidPlatformHelpers::callVoidMethod (jmethodID id) { - JNIEnv *env=ms_get_jni_env(); + JNIEnv *env = ms_get_jni_env(); if (env && mJavaHelper) { - env->CallVoidMethod(mJavaHelper,id); + env->CallVoidMethod(mJavaHelper, id); if (env->ExceptionCheck()) { env->ExceptionClear(); return -1; @@ -199,10 +201,8 @@ int AndroidPlatformHelpers::callVoidMethod (jmethodID id) { return -1; } -PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *system_context) { - return new AndroidPlatformHelpers(lc, system_context); +PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext) { + return new AndroidPlatformHelpers(lc, systemContext); } LINPHONE_END_NAMESPACE - -#endif diff --git a/src/core/platform-helpers/platform-helpers.cpp b/src/core/platform-helpers/platform-helpers.cpp index 2ac0516ec..624392072 100644 --- a/src/core/platform-helpers/platform-helpers.cpp +++ b/src/core/platform-helpers/platform-helpers.cpp @@ -1,53 +1,50 @@ /* -linphone -Copyright (C) 2017 Belledonne Communications SARL + * platform-helpers.cpp + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. +#include "platform-helpers.h" -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#include "linphone/utils/utils.h" - -#include "private.h" +// ============================================================================= LINPHONE_BEGIN_NAMESPACE -PlatformHelpers::~PlatformHelpers () {} - StubbedPlatformHelpers::StubbedPlatformHelpers (LinphoneCore *lc) : PlatformHelpers(lc) {} -void StubbedPlatformHelpers::setDnsServers () { -} -void StubbedPlatformHelpers::acquireWifiLock () { -} -void StubbedPlatformHelpers::releaseWifiLock () { -} -void StubbedPlatformHelpers::acquireMcastLock () { -} -void StubbedPlatformHelpers::releaseMcastLock () { -} -void StubbedPlatformHelpers::acquireCpuLock () { -} -void StubbedPlatformHelpers::releaseCpuLock () { -} +void StubbedPlatformHelpers::setDnsServers () {} + +void StubbedPlatformHelpers::acquireWifiLock () {} + +void StubbedPlatformHelpers::releaseWifiLock () {} + +void StubbedPlatformHelpers::acquireMcastLock () {} + +void StubbedPlatformHelpers::releaseMcastLock () {} + +void StubbedPlatformHelpers::acquireCpuLock () {} + +void StubbedPlatformHelpers::releaseCpuLock () {} + std::string StubbedPlatformHelpers::getDataPath () { - return Utils::getEmptyConstRefObject(); + return ""; } + std::string StubbedPlatformHelpers::getConfigPath () { - return Utils::getEmptyConstRefObject(); -} -StubbedPlatformHelpers::~StubbedPlatformHelpers () { + return ""; } LINPHONE_END_NAMESPACE diff --git a/src/core/platform-helpers/platform-helpers.h b/src/core/platform-helpers/platform-helpers.h index 02c312629..25bd23852 100644 --- a/src/core/platform-helpers/platform-helpers.h +++ b/src/core/platform-helpers/platform-helpers.h @@ -1,39 +1,44 @@ /* -linphone -Copyright (C) 2017 Belledonne Communications SARL - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ + * platform-helpers.h + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ #ifndef _PLATFORM_HELPERS_H_ #define _PLATFORM_HELPERS_H_ #include -#include "linphone/core.h" #include "linphone/utils/general.h" +// ============================================================================= + +L_DECL_C_STRUCT(LinphoneCore); + LINPHONE_BEGIN_NAMESPACE /** * This interface aims at abstracting some features offered by the platform, most often mobile platforms. - * A per platform implementation is to be made to implement these features, if available on the platform + * A per platform implementation is to be made to implement these features, if available on the platform. */ class PlatformHelpers { public: - //This method shall retrieve DNS server list from the platform and assign it to the core. + virtual ~PlatformHelpers () = default; + + // This method shall retrieve DNS server list from the platform and assign it to the core. virtual void setDnsServers () = 0; virtual void acquireWifiLock () = 0; virtual void releaseWifiLock () = 0; @@ -43,16 +48,18 @@ public: virtual void releaseCpuLock () = 0; virtual std::string getDataPath () = 0; virtual std::string getConfigPath () = 0; - virtual ~PlatformHelpers (); protected: inline PlatformHelpers (LinphoneCore *lc) : mCore(lc) {} + LinphoneCore *mCore; }; class StubbedPlatformHelpers : public PlatformHelpers { public: StubbedPlatformHelpers (LinphoneCore *lc); + virtual ~StubbedPlatformHelpers () = default; + void setDnsServers () override; void acquireWifiLock () override; void releaseWifiLock () override; @@ -62,10 +69,9 @@ public: void releaseCpuLock () override; std::string getDataPath () override; std::string getConfigPath () override; - virtual ~StubbedPlatformHelpers (); }; -PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *system_context); +PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext); LINPHONE_END_NAMESPACE