From e6abf61e1bb4d14479fbd6ffbce5cf9f87952e10 Mon Sep 17 00:00:00 2001 From: lee Date: Sat, 9 Dec 2023 16:40:09 +0800 Subject: [PATCH] Add factoryReset bluetooth feature Signed-off-by: lee --- .../inner/ipc/include/bluetooth_host_proxy.h | 2 +- .../inner/ipc/interface/i_bluetooth_host.h | 2 +- .../inner/ipc/src/bluetooth_host_proxy.cpp | 19 ++++++++----------- frameworks/inner/src/bluetooth_host.cpp | 8 ++++++-- .../napi/src/access/napi_bluetooth_access.cpp | 17 +++++++++++++++++ .../napi/src/access/napi_bluetooth_access.h | 4 ++++ interfaces/inner_api/include/bluetooth_host.h | 2 +- 7 files changed, 38 insertions(+), 16 deletions(-) diff --git a/frameworks/inner/ipc/include/bluetooth_host_proxy.h b/frameworks/inner/ipc/include/bluetooth_host_proxy.h index 08007f4d..e11e526c 100644 --- a/frameworks/inner/ipc/include/bluetooth_host_proxy.h +++ b/frameworks/inner/ipc/include/bluetooth_host_proxy.h @@ -35,7 +35,7 @@ public: int32_t DisableBt() override; sptr GetProfile(const std::string &name) override; sptr GetBleRemote(const std::string &name) override; - bool BluetoothFactoryReset() override; + int32_t BluetoothFactoryReset() override; int32_t GetBtState(int &state) override; std::string GetLocalAddress() override; int32_t DisableBle() override; diff --git a/frameworks/inner/ipc/interface/i_bluetooth_host.h b/frameworks/inner/ipc/interface/i_bluetooth_host.h index d77a0c5e..0d625fce 100644 --- a/frameworks/inner/ipc/interface/i_bluetooth_host.h +++ b/frameworks/inner/ipc/interface/i_bluetooth_host.h @@ -59,7 +59,7 @@ public: virtual int32_t DisableBt() = 0; virtual sptr GetProfile(const std::string &name) = 0; virtual sptr GetBleRemote(const std::string &name) = 0; - virtual bool BluetoothFactoryReset() = 0; + virtual int32_t BluetoothFactoryReset() = 0; virtual int32_t GetBtState(int &state) = 0; virtual std::string GetLocalAddress() = 0; virtual int32_t DisableBle() = 0; diff --git a/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp b/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp index 3188622a..59c3fd75 100644 --- a/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp +++ b/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp @@ -141,21 +141,18 @@ sptr BluetoothHostProxy::GetBleRemote(const std::string &name) return reply.ReadRemoteObject(); } -bool BluetoothHostProxy::BluetoothFactoryReset() +int32_t BluetoothHostProxy::BluetoothFactoryReset() { MessageParcel data; - if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) { - HILOGE("BluetoothHostProxy::BluetoothFactoryReset WriteInterfaceToken error"); - return false; - } + CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor()), + BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error"); + MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; - int32_t error = InnerTransact(BluetoothHostInterfaceCode::BT_FACTORY_RESET, option, data, reply); - if (error != NO_ERROR) { - HILOGE("BluetoothHostProxy::BluetoothFactoryReset done fail, error: %{public}d", error); - return false; - } - return reply.ReadBool(); + int32_t ret = InnerTransact(BluetoothHostInterfaceCode::BT_FACTORY_RESET, option, data, reply); + CHECK_AND_RETURN_LOG_RET(ret == BT_NO_ERROR, BT_ERR_IPC_TRANS_FAILED, "error: %{public}d", ret); + + return reply.ReadInt32(); } int32_t BluetoothHostProxy::GetBtState(int &state) diff --git a/frameworks/inner/src/bluetooth_host.cpp b/frameworks/inner/src/bluetooth_host.cpp index aabc2ead..63608b2b 100644 --- a/frameworks/inner/src/bluetooth_host.cpp +++ b/frameworks/inner/src/bluetooth_host.cpp @@ -484,10 +484,14 @@ int BluetoothHost::GetBtState(int &state) const return ret; } -bool BluetoothHost::BluetoothFactoryReset() +int BluetoothHost::BluetoothFactoryReset() { HILOGD("enter"); - CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), false, "bluetooth is off."); + constexpr const char* BLUETOOTH_FACTORY_RESET_KEY = "persist.bluetooth.factoryreset"; + int ret = SetParameter(BLUETOOTH_FACTORY_RESET_KEY, "true"); + CHECK_AND_RETURN_LOG_RET(ret == 0, BT_ERR_INTERNAL_ERROR, "SetParameter failed"); + + CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_NO_ERROR, "bluetooth is off."); sptr proxy = GetRemoteProxy(BLUETOOTH_HOST); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "proxy is nullptr"); diff --git a/frameworks/js/napi/src/access/napi_bluetooth_access.cpp b/frameworks/js/napi/src/access/napi_bluetooth_access.cpp index 7ff2e692..ce358bee 100644 --- a/frameworks/js/napi/src/access/napi_bluetooth_access.cpp +++ b/frameworks/js/napi/src/access/napi_bluetooth_access.cpp @@ -42,6 +42,7 @@ napi_value NapiAccess::DefineAccessJSFunction(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("enableBluetooth", EnableBluetooth), DECLARE_NAPI_FUNCTION("disableBluetooth", DisableBluetooth), #ifdef BLUETOOTH_API_SINCE_10 + DECLARE_NAPI_FUNCTION("factoryReset", FactoryReset), DECLARE_NAPI_FUNCTION("on", RegisterAccessObserver), DECLARE_NAPI_FUNCTION("off", DeregisterAccessObserver), #endif @@ -216,5 +217,21 @@ napi_value NapiAccess::DeregisterAccessObserver(napi_env env, napi_callback_info NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM); return NapiGetUndefinedRet(env); } + +#ifdef BLUETOOTH_API_SINCE_10 +napi_value NapiAccess::FactoryReset(napi_env env, napi_callback_info info) +{ + HILOGD("enter"); + auto func = []() { + int32_t ret = BluetoothHost::GetDefaultHost().BluetoothFactoryReset(); + HILOGI("factoryReset ret: %{public}d", ret); + return NapiAsyncWorkRet(ret); + }; + auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK); + NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR); + asyncWork->Run(); + return asyncWork->GetRet(); +} +#endif } // namespace Bluetooth } // namespace OHOS diff --git a/frameworks/js/napi/src/access/napi_bluetooth_access.h b/frameworks/js/napi/src/access/napi_bluetooth_access.h index f226ab5e..35f07fca 100644 --- a/frameworks/js/napi/src/access/napi_bluetooth_access.h +++ b/frameworks/js/napi/src/access/napi_bluetooth_access.h @@ -29,6 +29,10 @@ public: static napi_value GetState(napi_env env, napi_callback_info info); static napi_value RegisterAccessObserver(napi_env env, napi_callback_info info); static napi_value DeregisterAccessObserver(napi_env env, napi_callback_info info); + +#ifdef BLUETOOTH_API_SINCE_10 + static napi_value FactoryReset(napi_env env, napi_callback_info info); +#endif private: static napi_value AccessPropertyValueInit(napi_env env, napi_value exports); static napi_value StateChangeInit(napi_env env); diff --git a/interfaces/inner_api/include/bluetooth_host.h b/interfaces/inner_api/include/bluetooth_host.h index 46050d16..62ddb84b 100644 --- a/interfaces/inner_api/include/bluetooth_host.h +++ b/interfaces/inner_api/include/bluetooth_host.h @@ -350,7 +350,7 @@ public: * returns false if the operation fails. * @since 6 */ - bool BluetoothFactoryReset(); + int BluetoothFactoryReset(); /** * @brief Get profile service ID list. -- Gitee