• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "napi_call_manager_utils.h"
17 
18 #include "napi_util.h"
19 #include "system_ability_definition.h"
20 #include "telephony_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 static constexpr const char *PLACE_CALL = "ohos.permission.PLACE_CALL";
25 static constexpr const char *ANSWER_CALL = "ohos.permission.ANSWER_CALL";
26 static constexpr const char *SET_TELEPHONY_STATE = "ohos.permission.SET_TELEPHONY_STATE";
27 static constexpr const char *GET_TELEPHONY_STATE = "ohos.permission.GET_TELEPHONY_STATE";
28 
29 static std::unordered_map<int32_t, const char *> eventNameMap_ = {
30     { CALL_MANAGER_DIAL_CALL, "dial" },
31     { CALL_MANAGER_ANSWER_CALL, "answer" },
32     { CALL_MANAGER_REJECT_CALL, "reject" },
33     { CALL_MANAGER_DISCONNECT_CALL, "hangup" },
34     { CALL_MANAGER_HOLD_CALL, "holdCall" },
35     { CALL_MANAGER_UNHOLD_CALL, "unHoldCall" },
36     { CALL_MANAGER_SWITCH_CALL, "switchCall" },
37     { CALL_MANAGER_GET_CALL_WAITING, "getCallWaitingStatus" },
38     { CALL_MANAGER_SET_CALL_WAITING, "setCallWaiting" },
39     { CALL_MANAGER_GET_CALL_RESTRICTION, "getCallRestrictionStatus" },
40     { CALL_MANAGER_SET_CALL_RESTRICTION, "setCallRestriction" },
41     { CALL_MANAGER_SET_CALL_RESTRICTION_PASSWORD, "setCallRestrictionPassword" },
42     { CALL_MANAGER_GET_CALL_TRANSFER, "getCallTransferInfo" },
43     { CALL_MANAGER_SET_CALL_TRANSFER, "setCallTransfer" },
44     { CALL_MANAGER_CAN_SET_CALL_TRANSFER_TIME, "canSetCallTransferTime" },
45     { CALL_MANAGER_ENABLE_IMS_SWITCH, "enableImsSwitch" },
46     { CALL_MANAGER_DISABLE_IMS_SWITCH, "disableImsSwitch" },
47     { CALL_MANAGER_IS_EMERGENCY_CALL, "isInEmergencyCall" },
48     { CALL_MANAGER_IS_RINGING, "isRinging" },
49     { CALL_MANAGER_MUTE_RINGER, "muteRinger" },
50     { CALL_MANAGER_SET_VONR_STATE, "setVoNRState" },
51     { CALL_MANAGER_GET_VONR_STATE, "getVoNRState" },
52 };
53 
54 static std::unordered_map<int32_t, const char *> eventPermissionMap_ = {
55     { CALL_MANAGER_DIAL_CALL, PLACE_CALL },
56     { CALL_MANAGER_ANSWER_CALL, ANSWER_CALL },
57     { CALL_MANAGER_REJECT_CALL, ANSWER_CALL },
58     { CALL_MANAGER_DISCONNECT_CALL, ANSWER_CALL },
59     { CALL_MANAGER_HOLD_CALL, ANSWER_CALL },
60     { CALL_MANAGER_UNHOLD_CALL, ANSWER_CALL },
61     { CALL_MANAGER_SWITCH_CALL, ANSWER_CALL },
62     { CALL_MANAGER_GET_CALL_WAITING, GET_TELEPHONY_STATE },
63     { CALL_MANAGER_SET_CALL_WAITING, SET_TELEPHONY_STATE },
64     { CALL_MANAGER_GET_CALL_RESTRICTION, GET_TELEPHONY_STATE },
65     { CALL_MANAGER_SET_CALL_RESTRICTION, SET_TELEPHONY_STATE },
66     { CALL_MANAGER_SET_CALL_RESTRICTION_PASSWORD, SET_TELEPHONY_STATE },
67     { CALL_MANAGER_GET_CALL_TRANSFER, GET_TELEPHONY_STATE },
68     { CALL_MANAGER_SET_CALL_TRANSFER, SET_TELEPHONY_STATE },
69     { CALL_MANAGER_CAN_SET_CALL_TRANSFER_TIME, GET_TELEPHONY_STATE },
70     { CALL_MANAGER_ENABLE_IMS_SWITCH, SET_TELEPHONY_STATE },
71     { CALL_MANAGER_DISABLE_IMS_SWITCH, SET_TELEPHONY_STATE },
72     { CALL_MANAGER_IS_EMERGENCY_CALL, SET_TELEPHONY_STATE },
73     { CALL_MANAGER_IS_RINGING, SET_TELEPHONY_STATE },
74     { CALL_MANAGER_MUTE_RINGER, SET_TELEPHONY_STATE },
75     { CALL_MANAGER_SET_VONR_STATE, SET_TELEPHONY_STATE },
76     { CALL_MANAGER_GET_VONR_STATE, GET_TELEPHONY_STATE },
77 };
78 
GetEventName(int32_t eventId)79 std::string NapiCallManagerUtils::GetEventName(int32_t eventId)
80 {
81     std::string result = "";
82     auto iter = eventNameMap_.find(eventId);
83     if (iter == eventNameMap_.end()) {
84         TELEPHONY_LOGE("NapiCallManagerUtils::GetEventName return null.");
85         return result;
86     }
87     result = iter->second;
88     TELEPHONY_LOGI("NapiCallManagerUtils::GetEventName message = %{public}s", result.c_str());
89     return result;
90 }
91 
GetEventPermission(int32_t eventId)92 std::string NapiCallManagerUtils::GetEventPermission(int32_t eventId)
93 {
94     std::string result = "";
95     auto iter = eventPermissionMap_.find(eventId);
96     if (iter == eventPermissionMap_.end()) {
97         TELEPHONY_LOGE("NapiCallManagerUtils::GetEventPermission return null.");
98         return result;
99     }
100     result = iter->second;
101     TELEPHONY_LOGI("NapiCallManagerUtils::GetEventPermission message = %{public}s", result.c_str());
102     return result;
103 }
104 
CreateErrorCodeAndMessageForJs(napi_env env,int32_t errorCode,int32_t eventId)105 napi_value NapiCallManagerUtils::CreateErrorCodeAndMessageForJs(napi_env env, int32_t errorCode, int32_t eventId)
106 {
107     JsError error = {};
108     switch (errorCode) {
109         case TELEPHONY_ERR_PERMISSION_ERR: {
110             std::string funcName = GetEventName(eventId);
111             std::string permission = GetEventPermission(eventId);
112             error = NapiUtil::ConverErrorMessageWithPermissionForJs(errorCode, funcName, permission);
113             break;
114         }
115         default:
116             error = NapiUtil::ConverErrorMessageForJs(errorCode);
117             break;
118     }
119     return CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
120 }
121 
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)122 bool NapiCallManagerUtils::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
123 {
124     napi_valuetype valueType = napi_undefined;
125     napi_typeof(env, value, &valueType);
126     return valueType == targetType;
127 }
128 
CreateUndefined(napi_env env)129 napi_value NapiCallManagerUtils::CreateUndefined(napi_env env)
130 {
131     napi_value result = nullptr;
132     napi_get_undefined(env, &result);
133     return result;
134 }
135 
CreateErrorMessage(napi_env env,std::string msg)136 napi_value NapiCallManagerUtils::CreateErrorMessage(napi_env env, std::string msg)
137 {
138     napi_value result = nullptr;
139     napi_value message = nullptr;
140     napi_create_string_utf8(env, msg.c_str(), msg.length(), &message);
141     napi_create_error(env, nullptr, message, &result);
142     return result;
143 }
144 
CreateErrorMessageWithErrorCode(napi_env env,std::string msg,int32_t errorCode)145 napi_value NapiCallManagerUtils::CreateErrorMessageWithErrorCode(napi_env env, std::string msg, int32_t errorCode)
146 {
147     napi_value result = nullptr;
148     napi_value message = nullptr;
149     NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.length(), &message));
150     napi_value codeValue = nullptr;
151     NAPI_CALL(env, napi_create_int32(env, errorCode, &codeValue));
152     NAPI_CALL(env, napi_create_object(env, &result));
153     NAPI_CALL(env, napi_set_named_property(env, result, "code", codeValue));
154     NAPI_CALL(env, napi_set_named_property(env, result, "message", message));
155     return result;
156 }
157 
ToInt32Value(napi_env env,int32_t value)158 napi_value NapiCallManagerUtils::ToInt32Value(napi_env env, int32_t value)
159 {
160     napi_value staticValue = nullptr;
161     napi_create_int32(env, value, &staticValue);
162     return staticValue;
163 }
164 
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)165 napi_value NapiCallManagerUtils::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
166 {
167     napi_value value = nullptr;
168     napi_get_named_property(env, object, propertyName.c_str(), &value);
169     return value;
170 }
171 
GetStringProperty(napi_env env,napi_value object,const std::string & propertyName)172 std::string NapiCallManagerUtils::GetStringProperty(napi_env env, napi_value object, const std::string &propertyName)
173 {
174     napi_value value = nullptr;
175     napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
176     if (getNameStatus == napi_ok) {
177         char tmpStr[MESSAGE_CONTENT_MAXIMUM_LIMIT + 1] = { 0 };
178         size_t len = 0;
179         napi_status getStringStatus =
180             napi_get_value_string_utf8(env, value, tmpStr, MESSAGE_CONTENT_MAXIMUM_LIMIT, &len);
181         if (getStringStatus == napi_ok && len > 0) {
182             return std::string(tmpStr, len);
183         }
184     }
185     TELEPHONY_LOGW("GetStringProperty failed!");
186     return "";
187 }
188 
GetIntProperty(napi_env env,napi_value object,const std::string & propertyName)189 int32_t NapiCallManagerUtils::GetIntProperty(napi_env env, napi_value object, const std::string &propertyName)
190 {
191     int32_t intValue = 0;
192     napi_value value = nullptr;
193     napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
194     if (getNameStatus == napi_ok) {
195         napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
196         if (getIntStatus == napi_ok) {
197             return intValue;
198         }
199     }
200     TELEPHONY_LOGW("GetIntProperty failed!");
201     return intValue;
202 }
203 
GetUssdIntProperty(napi_env env,napi_value object,const std::string & propertyName,int32_t & result)204 bool NapiCallManagerUtils::GetUssdIntProperty(
205     napi_env env, napi_value object, const std::string &propertyName, int32_t &result)
206 {
207     napi_value value = nullptr;
208     napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
209     if (getNameStatus == napi_ok) {
210         napi_status getIntStatus = napi_get_value_int32(env, value, &result);
211         if (getIntStatus == napi_ok) {
212             return true;
213         }
214     }
215     TELEPHONY_LOGW("GetUssdIntProperty failed!");
216     return false;
217 }
218 
GetUssdStringProperty(napi_env env,napi_value object,const std::string & propertyName,std::string & result)219 bool NapiCallManagerUtils::GetUssdStringProperty(
220     napi_env env, napi_value object, const std::string &propertyName, std::string &result)
221 {
222     napi_value value = nullptr;
223     napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
224     if (getNameStatus == napi_ok) {
225         char tmpStr[MESSAGE_CONTENT_MAXIMUM_LIMIT + 1] = { 0 };
226         size_t len = 0;
227         napi_status getStringStatus =
228             napi_get_value_string_utf8(env, value, tmpStr, MESSAGE_CONTENT_MAXIMUM_LIMIT, &len);
229         if (getStringStatus == napi_ok && len > 0) {
230             result = std::string(tmpStr, len);
231             return true;
232         }
233     }
234     TELEPHONY_LOGW("GetUssdStringProperty failed!");
235     return false;
236 }
237 
GetBoolProperty(napi_env env,napi_value object,const std::string & propertyName)238 bool NapiCallManagerUtils::GetBoolProperty(napi_env env, napi_value object, const std::string &propertyName)
239 {
240     bool boolValue = false;
241     napi_value value = nullptr;
242     napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
243     if (getNameStatus == napi_ok) {
244         napi_status getIntStatus = napi_get_value_bool(env, value, &boolValue);
245         if (getIntStatus == napi_ok) {
246             return boolValue;
247         }
248     }
249     TELEPHONY_LOGW("GetBoolProperty failed!");
250     return boolValue;
251 }
252 
SetPropertyInt32(napi_env env,napi_value object,std::string name,int32_t value)253 void NapiCallManagerUtils::SetPropertyInt32(napi_env env, napi_value object, std::string name, int32_t value)
254 {
255     napi_value peopertyValue = nullptr;
256     napi_create_int32(env, value, &peopertyValue);
257     napi_set_named_property(env, object, name.c_str(), peopertyValue);
258 }
259 
SetPropertyStringUtf8(napi_env env,napi_value object,std::string name,std::string value)260 void NapiCallManagerUtils::SetPropertyStringUtf8(napi_env env, napi_value object, std::string name, std::string value)
261 {
262     napi_value peopertyValue = nullptr;
263     napi_create_string_utf8(env, value.c_str(), value.length(), &peopertyValue);
264     napi_set_named_property(env, object, name.c_str(), peopertyValue);
265 }
266 
SetPropertyBoolean(napi_env env,napi_value object,std::string name,int32_t value)267 void NapiCallManagerUtils::SetPropertyBoolean(napi_env env, napi_value object, std::string name, int32_t value)
268 {
269     napi_value peopertyValue = nullptr;
270     napi_get_boolean(env, value, &peopertyValue);
271     napi_set_named_property(env, object, name.c_str(), peopertyValue);
272 }
273 
CreateEnumConstructor(napi_env env,napi_callback_info info)274 napi_value NapiCallManagerUtils::CreateEnumConstructor(napi_env env, napi_callback_info info)
275 {
276     napi_value thisArg = nullptr;
277     void *data = nullptr;
278     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
279     napi_value global = nullptr;
280     napi_get_global(env, &global);
281     return thisArg;
282 }
283 } // namespace Telephony
284 } // namespace OHOS
285