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