1 /*
2 * Copyright (C) 2021 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 "emergency_utils.h"
17
18 #include "shortnumberinfo.h"
19
20 #include "cellular_call_config.h"
21 #include "mmi_code_utils.h"
22 #include "module_service_utils.h"
23
24 namespace OHOS {
25 namespace Telephony {
IsEmergencyCall(int32_t slotId,const std::string & phoneNum,bool & enabled)26 int32_t EmergencyUtils::IsEmergencyCall(int32_t slotId, const std::string &phoneNum, bool &enabled)
27 {
28 if (phoneNum.empty()) {
29 TELEPHONY_LOGI("IsEmergencyCall return, phoneNum is empty.");
30 return TELEPHONY_ERR_ARGUMENT_INVALID;
31 }
32
33 if (phoneNum.find('@') != std::string::npos || phoneNum.find("%40") != std::string::npos) {
34 TELEPHONY_LOGI("IsEmergencyCall return, phoneNum is not emergency.");
35 return TELEPHONY_ERR_ARGUMENT_INVALID;
36 }
37
38 if (phoneNum.front() == '+') {
39 TELEPHONY_LOGI("IsEmergencyCall return, phoneNum is not emergency.");
40 return TELEPHONY_ERR_ARGUMENT_INVALID;
41 }
42
43 return IsEmergencyCallProcessing(slotId, phoneNum, enabled);
44 }
45
IsEmergencyCallProcessing(int32_t slotId,const std::string & formatString,bool & enabled)46 int32_t EmergencyUtils::IsEmergencyCallProcessing(int32_t slotId, const std::string &formatString, bool &enabled)
47 {
48 enabled = true;
49 TELEPHONY_LOGI("IsEmergencyCallProcessing entry.");
50 std::vector<std::string> emergencyNumList = {
51 "110", "120", "119", "122", "112", "000", "911", "08", "118", "999",
52 };
53 if (std::find(emergencyNumList.begin(), emergencyNumList.end(), formatString) != emergencyNumList.end()) {
54 TELEPHONY_LOGI("IsEmergencyCallProcessing, Complies with local configuration data.");
55 return TELEPHONY_ERR_SUCCESS;
56 }
57 CellularCallConfig config;
58 ModuleServiceUtils dependDataObtain;
59 std::string countryIsoCode = dependDataObtain.GetNetworkCountryCode(slotId);
60 std::vector<EmergencyCall> eccCallList = config.GetEccCallList(slotId);
61 std::string mcc = config.GetMcc(slotId);
62 for (auto it = eccCallList.begin(); it != eccCallList.end(); it++) {
63 if (mcc == it->mcc && formatString == it->eccNum) {
64 TELEPHONY_LOGI("IsEmergencyCallProcessing, Complies with sim data.");
65 return TELEPHONY_ERR_SUCCESS;
66 }
67 }
68 if (!countryIsoCode.empty()) {
69 TELEPHONY_LOGI("IsEmergencyCallProcessing countryIsoCode is not empty");
70 i18n::phonenumbers::ShortNumberInfo shortNumberInfo;
71 transform(countryIsoCode.begin(), countryIsoCode.end(), countryIsoCode.begin(), ::toupper);
72 enabled = shortNumberInfo.IsEmergencyNumber(formatString, countryIsoCode);
73 return TELEPHONY_ERR_SUCCESS;
74 }
75 TELEPHONY_LOGI("IsEmergencyCallProcessing, not an emergency number.");
76 enabled = false;
77 return TELEPHONY_ERR_SUCCESS;
78 }
79 } // namespace Telephony
80 } // namespace OHOS
81