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 "tel_ril_base.h"
17 
18 #include "core_service_hisysevent.h"
19 
20 namespace OHOS {
21 namespace Telephony {
22 std::atomic_int TelRilBase::nextSerialId_(1);
23 std::unordered_map<int32_t, std::shared_ptr<TelRilRequest>> TelRilBase::requestMap_;
24 std::mutex TelRilBase::requestLock_;
25 std::shared_ptr<TelRilHandler> TelRilBase::handler_;
26 
TelRilBase(int32_t slotId,sptr<HDI::Ril::V1_3::IRil> rilInterface,std::shared_ptr<ObserverHandler> observerHandler,std::shared_ptr<TelRilHandler> handler)27 TelRilBase::TelRilBase(int32_t slotId, sptr<HDI::Ril::V1_3::IRil> rilInterface,
28     std::shared_ptr<ObserverHandler> observerHandler, std::shared_ptr<TelRilHandler> handler)
29     : observerHandler_(observerHandler), rilInterface_(rilInterface), slotId_(slotId)
30 {
31     handler_ = handler;
32 }
33 
ResetRilInterface(sptr<HDI::Ril::V1_3::IRil> rilInterface)34 void TelRilBase::ResetRilInterface(sptr<HDI::Ril::V1_3::IRil> rilInterface)
35 {
36     rilInterface_ = rilInterface;
37 }
38 
CreateTelRilRequest(const AppExecFwk::InnerEvent::Pointer & result)39 std::shared_ptr<TelRilRequest> TelRilBase::CreateTelRilRequest(const AppExecFwk::InnerEvent::Pointer &result)
40 {
41     std::shared_ptr<TelRilRequest> telRilRequest = std::make_shared<TelRilRequest>(GetNextSerialId(), result);
42     std::lock_guard<std::mutex> lockRequest(TelRilBase::requestLock_);
43     TelRilBase::requestMap_.insert(std::make_pair(telRilRequest->serialId_, telRilRequest));
44     TELEPHONY_LOGD("CreateTelRilRequest serialId : %{public}d", static_cast<int32_t>(telRilRequest->serialId_));
45     if (handler_ != nullptr) {
46         handler_->ApplyRunningLock(TelRilHandler::NORMAL_RUNNING_LOCK);
47     } else {
48         TELEPHONY_LOGE("handler_ is nullptr!!!");
49     }
50     return telRilRequest;
51 }
52 
GetNextSerialId(void)53 int32_t TelRilBase::GetNextSerialId(void)
54 {
55     if (nextSerialId_ >= INT32_MAX) {
56         nextSerialId_ = 1;
57     }
58     return nextSerialId_++;
59 }
60 
FindTelRilRequest(const RadioResponseInfo & responseInfo)61 std::shared_ptr<TelRilRequest> TelRilBase::FindTelRilRequest(const RadioResponseInfo &responseInfo)
62 {
63     int32_t serial = responseInfo.serial;
64     std::shared_ptr<TelRilRequest> telRilRequest = nullptr;
65     std::lock_guard<std::mutex> lockRequest(TelRilBase::requestLock_);
66     auto iter = TelRilBase::requestMap_.find(serial);
67     if (iter == TelRilBase::requestMap_.end()) {
68         TELEPHONY_LOGD("FindTelRilRequest not found serial:%{public}d", serial);
69     } else {
70         telRilRequest = iter->second;
71         if (handler_ != nullptr) {
72             handler_->ReduceRunningLock(TelRilHandler::NORMAL_RUNNING_LOCK);
73         }
74     }
75     if (telRilRequest == nullptr) {
76         TELEPHONY_LOGE("Unexpected ack response! sn: %{public}d", serial);
77         return telRilRequest;
78     }
79     // Remove telRilRequest from map.
80     TelRilBase::requestMap_.erase(serial);
81     return telRilRequest;
82 }
83 
GetSerialId(const AppExecFwk::InnerEvent::Pointer & response)84 int32_t TelRilBase::GetSerialId(const AppExecFwk::InnerEvent::Pointer &response)
85 {
86     if (rilInterface_ == nullptr) {
87         TELEPHONY_LOGE("ERROR : rilInterface_ == nullptr !!!");
88         return -TELEPHONY_ERR_ARGUMENT_INVALID;
89     }
90     std::shared_ptr<TelRilRequest> telRilRequest = CreateTelRilRequest(response);
91     if (telRilRequest == nullptr) {
92         TELEPHONY_LOGE("telRilRequest is nullptr");
93         return -TELEPHONY_ERR_LOCAL_PTR_NULL;
94     }
95     return telRilRequest->serialId_;
96 }
97 
DfxWriteCallFaultEvent(std::shared_ptr<TelRilRequest> telRilRequest,const int32_t error)98 void TelRilBase::DfxWriteCallFaultEvent(std::shared_ptr<TelRilRequest> telRilRequest, const int32_t error)
99 {
100     if (telRilRequest == nullptr || telRilRequest->pointer_ == nullptr) {
101         TELEPHONY_LOGE("telRilRequest or telRilRequest->pointer_  is nullptr");
102         return;
103     }
104     uint32_t eventId = telRilRequest->pointer_->GetInnerEventId();
105     switch (eventId) {
106         case RadioEvent::RADIO_DIAL:
107             CoreServiceHiSysEvent::WriteDialCallFaultEvent(slotId_,
108                 static_cast<int32_t>(CallErrorCode::CALL_ERROR_RADIO_RESPONSEINFO_ERROR),
109                 "ErrType " + std::to_string(error));
110             break;
111         case RadioEvent::RADIO_ACCEPT_CALL:
112             CoreServiceHiSysEvent::WriteAnswerCallFaultEvent(slotId_,
113                 static_cast<int32_t>(CallErrorCode::CALL_ERROR_RADIO_RESPONSEINFO_ERROR),
114                 "ErrType " + std::to_string(error));
115             break;
116         case RadioEvent::RADIO_REJECT_CALL:
117         case RadioEvent::RADIO_HANGUP_CONNECT:
118             CoreServiceHiSysEvent::WriteHangUpFaultEvent(slotId_,
119                 static_cast<int32_t>(CallErrorCode::CALL_ERROR_RADIO_RESPONSEINFO_ERROR),
120                 "ErrType " + std::to_string(error));
121             break;
122         default:
123             break;
124     }
125 }
126 
ErrorResponse(std::shared_ptr<TelRilRequest> telRilRequest,const RadioResponseInfo & responseInfo)127 int32_t TelRilBase::ErrorResponse(
128     std::shared_ptr<TelRilRequest> telRilRequest, const RadioResponseInfo &responseInfo)
129 {
130     std::shared_ptr<RadioResponseInfo> respInfo = std::make_shared<RadioResponseInfo>();
131     if (telRilRequest != nullptr && telRilRequest->pointer_ != nullptr) {
132         const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler = telRilRequest->pointer_->GetOwner();
133         if (handler == nullptr) {
134             TELEPHONY_LOGE("ERROR : ErrorResponse --> handler == nullptr !!!");
135             return TELEPHONY_ERR_LOCAL_PTR_NULL;
136         }
137         uint32_t eventId = telRilRequest->pointer_->GetInnerEventId();
138         respInfo->serial = responseInfo.serial;
139         respInfo->error = responseInfo.error;
140         respInfo->flag = telRilRequest->pointer_->GetParam();
141         DfxWriteCallFaultEvent(telRilRequest, static_cast<int32_t>(responseInfo.error));
142         TelEventHandler::SendTelEvent(handler, eventId, respInfo);
143         return static_cast<int32_t>(responseInfo.error);
144     } else {
145         TELEPHONY_LOGE("ERROR : telRilRequest  or telRilRequest->pointer_ is null !!!");
146     }
147     return TELEPHONY_ERR_LOCAL_PTR_NULL;
148 }
149 } // namespace Telephony
150 } // namespace OHOS
151