diff --git a/frameworks/inner/ipc/common/bt_def.h b/frameworks/inner/ipc/common/bt_def.h index db04e00f8cba114a001b2e01197dbf6bf502e041..fb513c324fb6f924b20edf1cf4caf1e4df38d604 100644 --- a/frameworks/inner/ipc/common/bt_def.h +++ b/frameworks/inner/ipc/common/bt_def.h @@ -342,6 +342,8 @@ enum class GattPermission : uint16_t { enum class GattConnectionPriority : int { BALANCED, HIGH, LOW_POWER }; enum GattStatus { + AUTHENTICATION_FAILED = -33, + GATT_CONGESTION = -32, EMPTY_FILTER = -31, MAX_FILTERS = -30, INVALID_REMOTE_DEVICE = -29, diff --git a/frameworks/inner/src/bluetooth_gatt_client.cpp b/frameworks/inner/src/bluetooth_gatt_client.cpp index 84e2958b288484750781445c75c6a5feb81ffdf9..8540a5a18e8c586f73f1f1726969176c6f70c43e 100644 --- a/frameworks/inner/src/bluetooth_gatt_client.cpp +++ b/frameworks/inner/src/bluetooth_gatt_client.cpp @@ -797,12 +797,12 @@ int GattClient::ReadCharacteristic(GattCharacteristic &characteristic) std::lock_guard lock(pimpl->connStateMutex_); if (pimpl->connectionState_ != static_cast(BTConnectState::CONNECTED) || !pimpl->isRegisterSucceeded_) { HILOGE("Request not supported"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED; } std::lock_guard lck(pimpl->requestInformation_.mutex_); if (pimpl->requestInformation_.doing_) { HILOGE("Remote device busy"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_OPERATION_BUSY; } int result = GattStatus::GATT_FAILURE; HILOGI("applicationId: %{public}d, handle: 0x%{public}04X", pimpl->applicationId_, characteristic.GetHandle()); @@ -834,12 +834,12 @@ int GattClient::ReadDescriptor(GattDescriptor &descriptor) std::lock_guard lck(pimpl->connStateMutex_); if (pimpl->connectionState_ != static_cast(BTConnectState::CONNECTED) || !pimpl->isRegisterSucceeded_) { HILOGE("Request not supported"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED; } std::lock_guard lock(pimpl->requestInformation_.mutex_); if (pimpl->requestInformation_.doing_) { HILOGE("Remote device busy"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_OPERATION_BUSY; } int result = BT_ERR_INTERNAL_ERROR; HILOGI("applicationId: %{public}d, handle: 0x%{public}04X", pimpl->applicationId_, descriptor.GetHandle()); @@ -903,12 +903,12 @@ int GattClient::SetNotifyCharacteristicInner(GattCharacteristic &characteristic, std::lock_guard lockConn(pimpl->connStateMutex_); if (pimpl->connectionState_ != static_cast(BTConnectState::CONNECTED)) { HILOGE("Request not supported"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED; } std::lock_guard lock(pimpl->requestInformation_.mutex_); if (pimpl->requestInformation_.doing_) { HILOGI("Remote device busy"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_OPERATION_BUSY; } auto descriptor = characteristic.GetDescriptor(UUID::FromString("00002902-0000-1000-8000-00805F9B34FB")); if (descriptor == nullptr) { @@ -996,7 +996,7 @@ int GattClient::WriteCharacteristic(GattCharacteristic &characteristic, std::vec std::lock_guard lockConn(pimpl->connStateMutex_); if (pimpl->connectionState_ != static_cast(BTConnectState::CONNECTED)) { HILOGE("Request not supported"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED; } size_t length = value.size(); HILOGD("length:%{public}zu", length); @@ -1007,7 +1007,7 @@ int GattClient::WriteCharacteristic(GattCharacteristic &characteristic, std::vec std::lock_guard lock(pimpl->requestInformation_.mutex_); if (pimpl->requestInformation_.doing_) { HILOGE("Remote device busy"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_OPERATION_BUSY; } BluetoothGattCharacteristic character( bluetooth::Characteristic(characteristic.GetHandle(), value.data(), length)); @@ -1050,7 +1050,7 @@ int GattClient::WriteDescriptor(GattDescriptor &descriptor) std::lock_guard lck(pimpl->connStateMutex_); if (pimpl->connectionState_ != static_cast(BTConnectState::CONNECTED) || !pimpl->isRegisterSucceeded_) { HILOGE("Request not supported"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED; } size_t length = 0; auto &characterValue = descriptor.GetValue(&length); @@ -1061,7 +1061,7 @@ int GattClient::WriteDescriptor(GattDescriptor &descriptor) std::lock_guard lock(pimpl->requestInformation_.mutex_); if (pimpl->requestInformation_.doing_) { HILOGE("Remote device busy"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_OPERATION_BUSY; } int result = BT_ERR_INTERNAL_ERROR; sptr proxy = GetRemoteProxy(PROFILE_GATT_CLIENT); @@ -1141,12 +1141,12 @@ int GattClient::ReadRemoteRssiValue() std::lock_guard lock(pimpl->connStateMutex_); if (pimpl->connectionState_ != static_cast(BTConnectState::CONNECTED) || !pimpl->isRegisterSucceeded_) { HILOGE("Request not supported"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED; } std::lock_guard lck(pimpl->requestInformation_.mutex_); if (pimpl->requestInformation_.doing_) { HILOGE("Remote device busy"); - return BT_ERR_INTERNAL_ERROR; + return BT_ERR_OPERATION_BUSY; } int result = GattStatus::GATT_FAILURE; HILOGI("applicationId: %{public}d", pimpl->applicationId_); diff --git a/frameworks/js/napi/include/napi_bluetooth_gatt_client.h b/frameworks/js/napi/include/napi_bluetooth_gatt_client.h index bd6dd933820afaf8377d6c7c70372129c4fb0920..b652f0286622b87855f587ad5cd7cc8773bbda2e 100644 --- a/frameworks/js/napi/include/napi_bluetooth_gatt_client.h +++ b/frameworks/js/napi/include/napi_bluetooth_gatt_client.h @@ -58,6 +58,7 @@ public: static napi_value SetBLEMtuSize(napi_env env, napi_callback_info info); static napi_value GetRssiValue(napi_env env, napi_callback_info info); static napi_value GetDeviceName(napi_env env, napi_callback_info info); + static int GattStatusFromService(int status); std::shared_ptr &GetClient() { @@ -87,6 +88,8 @@ public: static thread_local napi_ref consRef_; + // napi (first) <-> service (second) + static const std::vector> g_gattStatusSrvToNapi; private: std::shared_ptr client_ = nullptr; std::shared_ptr callback_; diff --git a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client.cpp b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client.cpp index 6f5c3459eaef08c7d8743b072f9cfdd240d65f22..b8a43cce2295bb8f787b0b1d96b3dd2c05b2fba4 100644 --- a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client.cpp +++ b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client.cpp @@ -37,6 +37,15 @@ using namespace std; thread_local napi_ref NapiGattClient::consRef_ = nullptr; +const std::vector> NapiGattClient::g_gattStatusSrvToNapi = { + { Bluetooth::BT_NO_ERROR, GATT_SUCCESS }, + { Bluetooth::BT_ERR_GATT_WRITE_NOT_PERMITTED, WRITE_NOT_PERMITTED }, + { Bluetooth::BT_ERR_GATT_READ_NOT_PERMITTED, READ_NOT_PERMITTED }, + { Bluetooth::BT_ERR_GATT_CONNECTION_CONGESTED, GATT_CONGESTION }, + { Bluetooth::BT_ERR_GATT_CONNECTION_NOT_ENCRYPTED, INSUFFICIENT_ENCRYPTION }, + { Bluetooth::BT_ERR_GATT_CONNECTION_NOT_AUTHENTICATED, AUTHENTICATION_FAILED }, + { Bluetooth::BT_ERR_GATT_CONNECTION_NOT_AUTHORIZED, INSUFFICIENT_AUTHORIZATION }, +}; static napi_status CheckCreateGattClientDeviceParams(napi_env env, napi_callback_info info, napi_value &outResult) { size_t expectedArgsCount = ARGS_SIZE_ONE; @@ -338,6 +347,7 @@ napi_value NapiGattClient::ReadCharacteristicValue(napi_env env, napi_callback_i int ret = BT_ERR_INTERNAL_ERROR; if (gattClient) { ret = gattClient->ReadCharacteristic(*character); + ret = GattStatusFromService(ret); } return NapiAsyncWorkRet(ret); }; @@ -391,6 +401,7 @@ napi_value NapiGattClient::ReadDescriptorValue(napi_env env, napi_callback_info int ret = BT_ERR_INTERNAL_ERROR; if (gattClient) { ret = gattClient->ReadDescriptor(*descriptor); + ret = GattStatusFromService(ret); } return NapiAsyncWorkRet(ret); }; @@ -524,6 +535,7 @@ napi_value NapiGattClient::GetRssiValue(napi_env env, napi_callback_info info) int ret = BT_ERR_INTERNAL_ERROR; if (gattClient) { ret = gattClient->ReadRemoteRssiValue(); + ret = GattStatusFromService(ret); } return NapiAsyncWorkRet(ret); }; @@ -568,6 +580,27 @@ napi_value NapiGattClient::GetDeviceName(napi_env env, napi_callback_info info) return asyncWork->GetRet(); } +int NapiGattClient::GattStatusFromService(int status) +{ + // if status is from napi, do not deal with. + if (status > 0) { + return status; + } + int ret = BT_ERR_INTERNAL_ERROR; + // statusCode srv -> napi + auto iter = g_gattStatusSrvToNapi.begin(); + for (; iter != g_gattStatusSrvToNapi.end(); iter++) { + if (iter->second == status) { + ret = iter->first; // transfer to napi errorCode. + break; + } + } + if (iter == g_gattStatusSrvToNapi.end()) { + HILOGW("Unsupported error code conversion, status: %{public}d", status); + } + return ret; +} + #ifdef BLUETOOTH_API_SINCE_10 static napi_status CheckWriteCharacteristicValueEx(napi_env env, napi_callback_info info, @@ -616,6 +649,7 @@ napi_value NapiGattClient::WriteCharacteristicValueEx(napi_env env, napi_callbac int ret = BT_ERR_INTERNAL_ERROR; if (gattClient) { ret = gattClient->WriteCharacteristic(*character); + ret = GattStatusFromService(ret); } return NapiAsyncWorkRet(ret); }; @@ -672,6 +706,7 @@ napi_value NapiGattClient::WriteDescriptorValueEx(napi_env env, napi_callback_in int ret = BT_ERR_INTERNAL_ERROR; if (gattClient) { ret = gattClient->WriteDescriptor(*descriptor); + ret = GattStatusFromService(ret); } return NapiAsyncWorkRet(ret); }; @@ -728,6 +763,7 @@ static napi_value setCharacteristicChangeInner(napi_env env, napi_callback_info } else { ret = gattClient->SetIndicateCharacteristic(*character, enable); } + ret = NapiGattClient::GattStatusFromService(ret); } return NapiAsyncWorkRet(ret); }; diff --git a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp index 5e9003741ffe9111d9ac36092d853b1da0a6b9fb..02059b84a13a501ddb17be8e1a89e40e657a5c31 100644 --- a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp +++ b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp @@ -43,6 +43,7 @@ void NapiGattClientCallback::OnCharacteristicChanged(const GattCharacteristic &c void NapiGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) { HILOGI("UUID: %{public}s, ret: %{public}d", characteristic.GetUuid().ToString().c_str(), ret); + ret = NapiGattClient::GattStatusFromService(ret); auto napiCharacter = std::make_shared(characteristic); AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_CLIENT_READ_CHARACTER, napiCharacter, ret); } @@ -50,6 +51,7 @@ void NapiGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic void NapiGattClientCallback::OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) { HILOGI("UUID: %{public}s, ret: %{public}d", descriptor.GetUuid().ToString().c_str(), ret); + ret = NapiGattClient::GattStatusFromService(ret); auto napiDescriptor = std::make_shared(descriptor); AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_CLIENT_READ_DESCRIPTOR, napiDescriptor, ret); } @@ -69,6 +71,7 @@ void NapiGattClientCallback::OnServicesDiscovered(int status) void NapiGattClientCallback::OnReadRemoteRssiValueResult(int rssi, int ret) { HILOGI("rssi: %{public}d, ret: %{public}d", rssi, ret); + ret = NapiGattClient::GattStatusFromService(ret); auto napiRssi = std::make_shared(rssi); AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_CLIENT_READ_REMOTE_RSSI_VALUE, napiRssi, ret); } @@ -77,6 +80,7 @@ void NapiGattClientCallback::OnCharacteristicWriteResult(const GattCharacteristi { #ifdef BLUETOOTH_API_SINCE_10 HILOGI("UUID: %{public}s, ret: %{public}d", characteristic.GetUuid().ToString().c_str(), ret); + ret = NapiGattClient::GattStatusFromService(ret); auto napiCharacter = std::make_shared(characteristic); AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_CLIENT_WRITE_CHARACTER, napiCharacter, ret); #endif @@ -86,6 +90,7 @@ void NapiGattClientCallback::OnDescriptorWriteResult(const GattDescriptor &descr { #ifdef BLUETOOTH_API_SINCE_10 HILOGI("UUID: %{public}s, ret: %{public}d", descriptor.GetUuid().ToString().c_str(), ret); + ret = NapiGattClient::GattStatusFromService(ret); auto napiDescriptor = std::make_shared(descriptor); AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_CLIENT_WRITE_DESCRIPTOR, napiDescriptor, ret); #endif @@ -95,6 +100,7 @@ void NapiGattClientCallback::OnSetNotifyCharacteristic(const GattCharacteristic { #ifdef BLUETOOTH_API_SINCE_10 HILOGI("UUID: %{public}s, status: %{public}d", characteristic.GetUuid().ToString().c_str(), status); + status = NapiGattClient::GattStatusFromService(status); AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_CLIENT_ENABLE_CHARACTER_CHANGED, nullptr, status); #endif } diff --git a/interfaces/inner_api/include/bluetooth_def.h b/interfaces/inner_api/include/bluetooth_def.h index a111f4506cfd0e8742585d96ba73431fcaa63a4b..64e88fc96bf43c6d008f3573c8c5ec923331bccd 100644 --- a/interfaces/inner_api/include/bluetooth_def.h +++ b/interfaces/inner_api/include/bluetooth_def.h @@ -320,6 +320,10 @@ enum class GattConnectionPriority : int { }; enum GattStatus { + AUTHENTICATION_FAILED = -33, + GATT_CONGESTION = -32, + EMPTY_FILTER = -31, + MAX_FILTERS = -30, INVALID_REMOTE_DEVICE = -29, INCLUDE_SERVICE_NOT_FOUND, REFERENCED_BY_OTHER_SERVICE, diff --git a/interfaces/inner_api/include/bluetooth_errorcode.h b/interfaces/inner_api/include/bluetooth_errorcode.h index 286d8a669d9f3c527afa574ec43addd4f5f01008..197ec1c8e60a612977c2cd5a203ae67c563b0d70 100644 --- a/interfaces/inner_api/include/bluetooth_errorcode.h +++ b/interfaces/inner_api/include/bluetooth_errorcode.h @@ -42,6 +42,7 @@ enum BtErrCode { BT_ERR_UNAVAILABLE_PROXY = BT_ERR_BASE_SYSCAP + 8, BT_ERR_DIALOG_FOR_USER_CONFIRM = BT_ERR_BASE_SYSCAP + 9, BT_ERR_MAX_RESOURCES = BT_ERR_BASE_SYSCAP + 10, + BT_ERR_OPERATION_BUSY = BT_ERR_BASE_SYSCAP + 11, BT_ERR_INTERNAL_ERROR = BT_ERR_BASE_SYSCAP + 99, BT_ERR_IPC_TRANS_FAILED = BT_ERR_BASE_SYSCAP + 100, @@ -49,6 +50,11 @@ enum BtErrCode { BT_ERR_GATT_READ_NOT_PERMITTED = BT_ERR_BASE_SYSCAP + 1000, BT_ERR_GATT_WRITE_NOT_PERMITTED = BT_ERR_BASE_SYSCAP + 1001, BT_ERR_GATT_MAX_SERVER = BT_ERR_BASE_SYSCAP + 1002, + BT_ERR_GATT_CONNECTION_NOT_ESTABILISHED = BT_ERR_BASE_SYSCAP + 1003, + BT_ERR_GATT_CONNECTION_CONGESTED = BT_ERR_BASE_SYSCAP + 1004, + BT_ERR_GATT_CONNECTION_NOT_ENCRYPTED = BT_ERR_BASE_SYSCAP + 1005, + BT_ERR_GATT_CONNECTION_NOT_AUTHENTICATED = BT_ERR_BASE_SYSCAP + 1006, + BT_ERR_GATT_CONNECTION_NOT_AUTHORIZED = BT_ERR_BASE_SYSCAP + 1007, BT_ERR_SPP_SERVER_STATE = BT_ERR_BASE_SYSCAP + 1050, BT_ERR_SPP_BUSY = BT_ERR_BASE_SYSCAP + 1051,