revise bluetooth actions(changedCB)
authorYoungjae Shin <yj99.shin@samsung.com>
Fri, 6 Mar 2020 12:23:42 +0000 (21:23 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 19 Mar 2020 04:30:37 +0000 (13:30 +0900)
bluetooth/BtAction.cpp
bluetooth/BtAction.h
bluetooth/BtActionAudioConnect.cpp
bluetooth/BtActionAudioConnect.h
bluetooth/BtActionPower.cpp
bluetooth/BtActionPower.h
bluetooth/BtFactory.cpp

index 7f77521..4078760 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 #include "BtAction.h"
+
+#include <bluetooth.h>
 #include <modes_errors.h>
 
 MODES_NAMESPACE_USE;
@@ -31,7 +33,7 @@ BtAction::~BtAction()
        bt_deinitialize();
 }
 
-bt_adapter_state_e BtAction::getAdapterStatus()
+bool BtAction::getAdapterStatus()
 {
        bt_adapter_state_e state;
        int btRet = bt_adapter_get_state(&state);
@@ -39,7 +41,10 @@ bt_adapter_state_e BtAction::getAdapterStatus()
                ERR("bt_adapter_get_state() Fail(%d)", btRet);
        }
 
-       return state;
+       if (state != BT_ADAPTER_ENABLED)
+               return false;
+       else
+               return true;
 }
 
 int BtAction::set(bool val)
index 6d33ddf..c894dc8 100644 (file)
@@ -16,7 +16,6 @@
 #pragma once
 
 #include <string>
-#include <bluetooth.h>
 #include <PluginAction.h>
 #include "plugin-log.h"
 #include "plugin-def.h"
@@ -30,7 +29,7 @@ public:
 
        virtual int set(bool val);
        virtual int set(const std::string &val);
-       bt_adapter_state_e getAdapterStatus();
+       bool getAdapterStatus();
 };
 
 MODES_NAMESPACE_END
index 1b9d7da..9ca30db 100644 (file)
@@ -23,7 +23,7 @@ MODES_NAMESPACE_USE;
 const std::string BtActionAudioConnect::NAME = "audioConnect";
 
 BtActionAudioConnect::BtActionAudioConnect()
-       : BtAction(NAME), cb(NULL), cbData(NULL)
+       : BtAction(NAME), cb(nullptr), cbData(nullptr)
 {
        bt_audio_initialize();
 }
@@ -35,7 +35,7 @@ BtActionAudioConnect::~BtActionAudioConnect()
 
 int BtActionAudioConnect::set(const std::string &val)
 {
-       if (!isBtAdapterEnabled()) {
+       if (!getAdapterStatus()) {
                ERR("BT is disabled");
                return MODES_ERROR_SYSTEM;
        }
@@ -49,7 +49,7 @@ int BtActionAudioConnect::set(const std::string &val)
                bt_major_device_class_e devClass = devInfo->bt_class.major_device_class;
                bt_adapter_free_device_info(devInfo);
 
-               return btAudioConnect(devClass, val.c_str());
+               return connectAudio(devClass, val.c_str());
        } else if (BT_ERROR_REMOTE_DEVICE_NOT_BONDED == ret) {
                DBG("BT device(%s) is not bonded", val.c_str());
        } else {
@@ -57,7 +57,7 @@ int BtActionAudioConnect::set(const std::string &val)
                return MODES_ERROR_SYSTEM;
        }
 
-       ret = bt_device_set_bond_created_cb(btDeviceBondCreatedCb, this);
+       ret = bt_device_set_bond_created_cb(bondCreatedCB, this);
        if (ret != BT_ERROR_NONE) {
                ERR("bt_device_set_bond_created_cb(%s) Fail(%s)", val.c_str(), get_error_message(ret));
                return MODES_ERROR_SYSTEM;
@@ -90,11 +90,11 @@ int BtActionAudioConnect::parse(const std::string &archive)
        return MODES_ERROR_NONE;
 }
 
-int BtActionAudioConnect::setChangedCallback(valueChangedCB callback, void * userData)
+int BtActionAudioConnect::setChangedCallback(valueChangedCB callback, void *userData)
 {
-       RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+       RETV_IF(nullptr == callback, MODES_ERROR_INVALID_PARAMETER);
 
-       int ret = bt_audio_set_connection_state_changed_cb(connectionStateChangedCb, userData);
+       int ret = bt_audio_set_connection_state_changed_cb(connStateChangedCb, this);
        if (BT_ERROR_NONE != ret) {
                ERR("bt_audio_set_connection_state_changed_cb() Fail(%s)", get_error_message(ret));
                return MODES_ERROR_SYSTEM;
@@ -106,49 +106,37 @@ int BtActionAudioConnect::setChangedCallback(valueChangedCB callback, void * use
        return MODES_ERROR_NONE;
 }
 
-void BtActionAudioConnect::unSetChangedCallback(valueChangedCB callback, void * userData)
+void BtActionAudioConnect::unSetChangedCallback(valueChangedCB callback, void *userData)
 {
-       RET_IF(NULL == callback);
+       RET_IF(nullptr == callback);
 
        int ret = bt_audio_unset_connection_state_changed_cb();
        if (BT_ERROR_NONE != ret)
                ERR("bt_audio_unset_connection_state_changed_cb() Fail(%s)", get_error_message(ret));
 
-       cb = NULL;
-       cbData = NULL;
+       cb = nullptr;
+       cbData = nullptr;
 }
 
-bool BtActionAudioConnect::isBtAdapterEnabled()
-{
-       bt_adapter_state_e state;
-       int btRet = bt_adapter_get_state(&state);
-       if (BT_ERROR_NONE != btRet) {
-               ERR("bt_adapter_get_state() Fail(%d)", btRet);
-               return false;
-       }
-
-       if (state != BT_ADAPTER_ENABLED)
-               return false;
-       else
-               return true;
-}
-
-void BtActionAudioConnect::connectionStateChangedCb(int result, bool connected,
-       const char *address, bt_audio_profile_type_e type, void *userData)
+void BtActionAudioConnect::connStateChangedCb(int result, bool connected, const char *address,
+               bt_audio_profile_type_e type, void *userData)
 {
        BtActionAudioConnect *action = (BtActionAudioConnect*)userData;
 
+       RET_IF(nullptr == userData);
+       RETM_IF(true == connected, "BT Device(%s) is Connected", address);
+
        INFO("BT device(%s) connection(%d)", address, connected);
        if (BT_ERROR_NONE != result) {
                ERR("connectionStateChangedCb() Fail(%d)", result);
                return;
        }
 
-       if (false == connected && action->requestVal == address)
+       if (action->cb && address == action->requestVal)
                action->cb(action->cbData);
 }
 
-void BtActionAudioConnect::btDeviceBondCreatedCb(int result, bt_device_info_s *devInfo, void *userData)
+void BtActionAudioConnect::bondCreatedCB(int result, bt_device_info_s *devInfo, void *userData)
 {
        if (BT_ERROR_NONE != result) {
                ERR("btDeviceBondCreatedCb Fail(%s)", get_error_message(result));
@@ -156,24 +144,22 @@ void BtActionAudioConnect::btDeviceBondCreatedCb(int result, bt_device_info_s *d
        }
 
        BtActionAudioConnect *action = (BtActionAudioConnect*)userData;
-       printBtDeviceInfo(devInfo);
        if (action->requestVal != devInfo->remote_address) {
                ERR("Unknown BT device(%s)", devInfo->remote_address);
                return;
        } else {
                bt_device_unset_bond_created_cb();
        }
+       action->printBtDeviceInfo(devInfo);
 
-       int ret = btAudioConnect(devInfo->bt_class.major_device_class, devInfo->remote_address);
-       if (BT_ERROR_NONE != ret) {
-               ERR("btAudioConnect() Fail(%d)", ret);
+       int ret = action->connectAudio(devInfo->bt_class.major_device_class, devInfo->remote_address);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("connectAudio(%s) Fail(%d)", devInfo->remote_address, ret);
                return;
        }
-
-       DBG("BtActionAudioConnect(%s, %s) Success!", devInfo->remote_name, devInfo->remote_address);
 }
 
-int BtActionAudioConnect::btAudioConnect(bt_major_device_class_e devClass, const char *address)
+int BtActionAudioConnect::connectAudio(bt_major_device_class_e devClass, const char *address)
 {
        if (BT_MAJOR_DEVICE_CLASS_AUDIO_VIDEO != devClass) {
                ERR("BT device(%s) is not AUDIO MAJOR Class", address);
@@ -191,7 +177,7 @@ int BtActionAudioConnect::btAudioConnect(bt_major_device_class_e devClass, const
 
 void BtActionAudioConnect::printBtDeviceInfo(bt_device_info_s *devInfo)
 {
-       RET_IF(devInfo == NULL);
+       RET_IF(nullptr == devInfo);
 
        DBG("BT Bonded Device Info ========================================");
        DBG("remote_address    : [%s]", devInfo->remote_address);
index f0c3050..cf0586c 100644 (file)
@@ -16,6 +16,7 @@
 #pragma once
 
 #include <string>
+#include <bluetooth.h>
 #include "BtAction.h"
 
 MODES_NAMESPACE_BEGIN
@@ -33,11 +34,10 @@ public:
        int setChangedCallback(valueChangedCB callback, void *userData);
        void unSetChangedCallback(valueChangedCB callback, void *userData);
 private:
-       bool isBtAdapterEnabled();
-       static void connectionStateChangedCb(int result, bool connected, const char *address, bt_audio_profile_type_e type, void *userData);
-       static void btDeviceBondCreatedCb(int result, bt_device_info_s *devInfo, void *userData);
-       static int btAudioConnect(bt_major_device_class_e devClass, const char *address);
-       static void printBtDeviceInfo(bt_device_info_s *devInfo);
+       static void connStateChangedCb(int result, bool connected, const char *address, bt_audio_profile_type_e type, void *userData);
+       static void bondCreatedCB(int result, bt_device_info_s *devInfo, void *userData);
+       int connectAudio(bt_major_device_class_e devClass, const char *address);
+       void printBtDeviceInfo(bt_device_info_s *devInfo);
        valueChangedCB cb;
        void *cbData;
        std::string requestVal;
index 65e50d0..cf7c731 100644 (file)
 #include "plugin-log.h"
 
 MODES_NAMESPACE_USE;
-#define SAFE_POINTER_ASSIGN( arg1, arg2) if ( arg1 != NULL ) { *arg1 = arg2; }
 
 const std::string BtActionPower::NAME = "power";
 
 BtActionPower::BtActionPower()
-       : BtAction(NAME), cb(NULL), cbData(NULL), undoVal(false), requestVal(false)
+       : BtAction(NAME), requestVal(false), oldVal(false), cb(nullptr), cbData(nullptr)
 {
 }
 
 int BtActionPower::set(bool val)
 {
+       requestVal = val;
        if (val) {
-               undoVal = false;
+               oldVal = false;
                int ret = bt_adapter_enable();
                if (BT_ERROR_NONE != ret) {
                        if (BT_ERROR_ALREADY_DONE == ret) {
-                               undoVal = true;
+                               oldVal = true;
                        } else {
                                ERR("bt_adapter_enable() Fail(%d)", ret);
                                return MODES_ERROR_SYSTEM;
                        }
                }
        } else {
-               undoVal = true;
+               oldVal = true;
                int ret = bt_adapter_disable();
                if (BT_ERROR_NONE != ret) {
                        if (BT_ERROR_NOT_ENABLED == ret) {
-                               undoVal = false;
+                               oldVal = false;
                        } else {
                                ERR("bt_adapter_disable() Fail(%d)", ret);
                                return MODES_ERROR_SYSTEM;
@@ -57,37 +57,36 @@ int BtActionPower::set(bool val)
                }
        }
 
-       requestVal = val;
-       INFO("BT power changed to (%s) from (%s)", val ? "On" : "Off", undoVal ? "On" : "Off");
+       INFO("BT power (%s -> %s)", val ? "On" : "Off", oldVal ? "On" : "Off");
        return MODES_ERROR_NONE;
 }
 
 void BtActionPower::undo()
 {
-       set(undoVal);
+       set(oldVal);
 }
 
 std::string BtActionPower::serialize()
 {
        std::ostringstream ostr;
 
-       ostr << undoVal;
+       ostr << oldVal;
        return ostr.str();
 }
 
 int BtActionPower::parse(const std::string &archive)
 {
        std::istringstream iss(archive);
-       iss >> undoVal;
+       iss >> oldVal;
 
        return MODES_ERROR_NONE;
 }
 
 int BtActionPower::setChangedCallback(valueChangedCB callback, void *userData)
 {
-       RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+       RETV_IF(nullptr == callback, MODES_ERROR_INVALID_PARAMETER);
 
-       int ret = bt_adapter_set_state_changed_cb(btStateChangedCB, this);
+       int ret = bt_adapter_set_state_changed_cb(stateChangedCB, this);
        if (BT_ERROR_NONE != ret) {
                ERR("bt_adapter_set_state_changed_cb() Fail(%s)", get_error_message(ret));
                return MODES_ERROR_SYSTEM;
@@ -96,28 +95,26 @@ int BtActionPower::setChangedCallback(valueChangedCB callback, void *userData)
        cb = callback;
        cbData = userData;
 
-       DBG("BtActionPower setChangedCallback() Success");
        return MODES_ERROR_NONE;
 }
+
 void BtActionPower::unSetChangedCallback(valueChangedCB callback, void *userData)
 {
-       RET_IF(NULL == callback);
+       RET_IF(nullptr == callback);
 
        int ret = bt_adapter_unset_state_changed_cb();
        if (BT_ERROR_NONE != ret)
                ERR("bt_adapter_unset_state_changed_cb() Fail(%s)", get_error_message(ret));
 
-       cb = NULL;
-       cbData = NULL;
-
-       DBG("BtActionPower unSetChangedCallback() Success");
+       cb = nullptr;
+       cbData = nullptr;
 }
 
-void BtActionPower::btStateChangedCB(int result, bt_adapter_state_e state, void *user_data)
+void BtActionPower::stateChangedCB(int result, bt_adapter_state_e state, void *userData)
 {
-       BtActionPower *action = (BtActionPower*)user_data;
+       BtActionPower *action = (BtActionPower*)userData;
 
-       RET_IF(NULL == user_data);
+       RET_IF(nullptr == userData);
 
        bool changedVal = action->requestVal;
        if (state == BT_ADAPTER_ENABLED)
@@ -129,6 +126,6 @@ void BtActionPower::btStateChangedCB(int result, bt_adapter_state_e state, void
 
        INFO("state:%d", state);
 
-       if (changedVal != action->requestVal)
+       if (action->cb && changedVal != action->requestVal)
                action->cb(action->cbData);
 }
index 00165e2..cf8b621 100644 (file)
@@ -16,6 +16,7 @@
 #pragma once
 
 #include <string>
+#include <bluetooth.h>
 #include "BtAction.h"
 
 MODES_NAMESPACE_BEGIN
@@ -32,11 +33,11 @@ public:
        int setChangedCallback(valueChangedCB callback, void *userData);
        void unSetChangedCallback(valueChangedCB callback, void *userData);
 private:
-       static void btStateChangedCB(int result, bt_adapter_state_e state, void *user_data);
+       static void stateChangedCB(int result, bt_adapter_state_e state, void *user_data);
+       bool requestVal;
+       bool oldVal;
        valueChangedCB cb;
        void *cbData;
-       bool undoVal;
-       bool requestVal;
 };
 
 MODES_NAMESPACE_END
index ecfefea..9df05dd 100644 (file)
@@ -32,7 +32,7 @@ BtFactory::BtFactory()
 BtAction* BtFactory::createAction(const std::string &key)
 {
        auto search = actionMap.find(key);
-       if (search == actionMap.end()) {
+       if (actionMap.end() == search) {
                ERR("No BtAction(%s)", key.c_str());
                return nullptr;
        }