1 /*
2 * Copyright (C) 2023 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 "data_request.h"
17
18 #include "core_manager_inner.h"
19 #include "net_conn_constants.h"
20 #include "network_search_types.h"
21 #include "telephony_log_wrapper.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 using namespace NetManagerStandard;
26 constexpr static const int32_t WAIT_TIME_SECOND = 30;
27
DataRequest(int32_t slotId)28 DataRequest::DataRequest(int32_t slotId) : slotId_(slotId) {}
29
~DataRequest()30 DataRequest::~DataRequest() {}
31
HttpRequest(int32_t slotId,const std::string & method,std::shared_ptr<MmsNetworkManager> netMgr,const std::string & contentUrl,const std::string & pduDir)32 int32_t DataRequest::HttpRequest(int32_t slotId, const std::string &method, std::shared_ptr<MmsNetworkManager> netMgr,
33 const std::string &contentUrl, const std::string &pduDir)
34 {
35 if (netMgr == nullptr) {
36 TELEPHONY_LOGE("netMgr is nullptr");
37 return TELEPHONY_ERR_LOCAL_PTR_NULL;
38 }
39 std::shared_ptr<MmsNetworkClient> netClient = netMgr->GetOrCreateHttpClient(slotId);
40 if (netClient == nullptr) {
41 TELEPHONY_LOGE("netClient is nullptr");
42 return TELEPHONY_ERR_LOCAL_PTR_NULL;
43 }
44 return netClient->Execute(method, contentUrl, pduDir);
45 }
46
ExecuteMms(const std::string & method,std::shared_ptr<MmsNetworkManager> mmsNetworkMgr,const std::string & contentUrl,const std::string & pduDir)47 int32_t DataRequest::ExecuteMms(const std::string &method, std::shared_ptr<MmsNetworkManager> mmsNetworkMgr,
48 const std::string &contentUrl, const std::string &pduDir)
49 {
50 bool attached = CoreManagerInner::GetInstance().GetPsRegState(slotId_) ==
51 static_cast<int32_t>(RegServiceState::REG_STATE_IN_SERVICE);
52 if (!attached) {
53 TELEPHONY_LOGE("Slot%{public}d: attached is false", slotId_);
54 return TELEPHONY_ERR_MMS_PS_NOT_ATTACHED;
55 }
56
57 std::unique_lock<std::mutex> lck(ctx_);
58 if (mmsNetworkMgr->AcquireNetwork(slotId_, GetRequestId()) != NETMANAGER_SUCCESS) {
59 TELEPHONY_LOGE("acquire network fail");
60 return TELEPHONY_ERR_MMS_FAIL_DATA_NETWORK_ERROR;
61 }
62
63 networkReady_ = false;
64 while (!networkReady_) {
65 TELEPHONY_LOGI("wait(), networkReady = false");
66 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
67 break;
68 }
69 }
70
71 if (!networkReady_) {
72 TELEPHONY_LOGE("acquire network fail");
73 mmsNetworkMgr->ReleaseNetwork(GetRequestId(), false);
74 return TELEPHONY_ERR_LOCAL_PTR_NULL;
75 }
76
77 int32_t executeResult = HttpRequest(slotId_, method, mmsNetworkMgr, contentUrl, pduDir);
78 mmsNetworkMgr->ReleaseNetwork(GetRequestId(), false);
79 return executeResult;
80 }
81
GetRequestId()82 uint8_t DataRequest::GetRequestId()
83 {
84 return requestId_;
85 }
86
CreateRequestId()87 void DataRequest::CreateRequestId()
88 {
89 requestId_++;
90 }
91 } // namespace Telephony
92 } // namespace OHOS