1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_error"
17 #endif
18 
19 #include "napi_bluetooth_error.h"
20 
21 #include "bluetooth_errorcode.h"
22 #include "napi_bluetooth_utils.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 
27 static std::map<int32_t, std::string> napiErrMsgMap {
28     { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "Service stoped." },
29     { BtErrCode::BT_ERR_UNBONDED_DEVICE, "Device is not bonded." },
30     { BtErrCode::BT_ERR_INVALID_STATE, "Bluetooth disabled." },
31     { BtErrCode::BT_ERR_PROFILE_DISABLED, "Profile not supported." },
32     { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "Device not connected." },
33     { BtErrCode::BT_ERR_MAX_CONNECTION, "The maximum number of connections has been reached." },
34     { BtErrCode::BT_ERR_INTERNAL_ERROR, "Operation failed" },
35     { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "IPC failed." },
36     { BtErrCode::BT_ERR_UNAVAILABLE_PROXY, "The value of proxy is a null pointer." },
37     { BtErrCode::BT_ERR_PERMISSION_FAILED, "Permission denied." },
38     { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "Non-system applications are not allowed to use system APIs."},
39     { BtErrCode::BT_ERR_PROHIBITED_BY_EDM, "Bluetooth is prohibited by EDM."},
40     { BtErrCode::BT_ERR_INVALID_PARAM, "Invalid parameter." },
41     { BtErrCode::BT_ERR_API_NOT_SUPPORT, "Capability is not supported." },
42     { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "Gatt read forbiden." },
43     { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "Gatt write forbiden." },
44     { BtErrCode::BT_ERR_GATT_MAX_SERVER, "Max gatt server." },
45     { BtErrCode::BT_ERR_SPP_SERVER_STATE, "SPP server not running." },
46     { BtErrCode::BT_ERR_SPP_BUSY, "SPP translate busy." },
47     { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "Device is not inquired." },
48     { BtErrCode::BT_ERR_SPP_IO, "SPP IO error." },
49     { BtErrCode::BT_ERR_NO_ACTIVE_HFP_DEVICE, "Active hfp device is not exist." },
50     { BtErrCode::BT_ERR_NULL_HFP_STATE_MACHINE, "Hfp state machine is not null." },
51     { BtErrCode::BT_ERR_HFP_NOT_CONNECT, "Hfp is not connected." },
52     { BtErrCode::BT_ERR_SCO_HAS_BEEN_CONNECTED, "Sco has been connected." },
53     { BtErrCode::BT_ERR_VR_HAS_BEEN_STARTED, "Voice recognition has been started." },
54     { BtErrCode::BT_ERR_AUDIO_NOT_IDLE, "Audio is not idle." },
55     { BtErrCode::BT_ERR_VIRTUAL_CALL_NOT_STARTED, "Virtual call is not started." },
56     { BtErrCode::BT_ERR_DISCONNECT_SCO_FAILED, "Disconnect sco failed." },
57 };
58 
GetNapiErrMsg(const napi_env & env,const int32_t errCode)59 std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode)
60 {
61     auto iter = napiErrMsgMap.find(errCode);
62     if (iter != napiErrMsgMap.end()) {
63         std::string errMessage = "BussinessError ";
64         errMessage.append(std::to_string(errCode)).append(": ").append(iter->second);
65         return errMessage;
66     }
67     return "Inner error.";
68 }
69 
HandleSyncErr(const napi_env & env,int32_t errCode)70 void HandleSyncErr(const napi_env &env, int32_t errCode)
71 {
72     if (errCode == BtErrCode::BT_NO_ERROR) {
73         return;
74     }
75     std::string errMsg = GetNapiErrMsg(env, errCode);
76     if (errMsg != "") {
77         napi_throw_error(env, std::to_string(errCode).c_str(), errMsg.c_str());
78     }
79 }
80 }
81 }