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,std::string & pduDir,const std::string & ua,const std::string & uaprof)32  int32_t DataRequest::HttpRequest(int32_t slotId, const std::string &method, std::shared_ptr<MmsNetworkManager> netMgr,
33      const std::string &contentUrl, std::string &pduDir, const std::string &ua, const std::string &uaprof)
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, ua, uaprof);
45  }
46  
ExecuteMms(const std::string & method,std::shared_ptr<MmsNetworkManager> mmsNetworkMgr,const std::string & contentUrl,std::string & pduDir,const std::string & ua,const std::string & uaprof)47  int32_t DataRequest::ExecuteMms(const std::string &method, std::shared_ptr<MmsNetworkManager> mmsNetworkMgr,
48      const std::string &contentUrl, std::string &pduDir, const std::string &ua, const std::string &uaprof)
49  {
50      std::unique_lock<std::mutex> lck(ctx_);
51      if (mmsNetworkMgr->AcquireNetwork(slotId_, GetRequestId()) != NETMANAGER_SUCCESS) {
52          TELEPHONY_LOGE("acquire network fail");
53          return TELEPHONY_ERR_MMS_FAIL_DATA_NETWORK_ERROR;
54      }
55  
56      networkReady_ = false;
57      while (!networkReady_) {
58          TELEPHONY_LOGI("wait(), networkReady = false");
59          if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
60              break;
61          }
62      }
63  
64      if (!networkReady_) {
65          TELEPHONY_LOGE("acquire network fail");
66          mmsNetworkMgr->ReleaseNetwork(GetRequestId(), false);
67          return TELEPHONY_ERR_LOCAL_PTR_NULL;
68      }
69  
70      int32_t executeResult = HttpRequest(slotId_, method, mmsNetworkMgr, contentUrl, pduDir, ua, uaprof);
71      mmsNetworkMgr->ReleaseNetwork(GetRequestId(), false);
72      return executeResult;
73  }
74  
GetRequestId()75  uint8_t DataRequest::GetRequestId()
76  {
77      return requestId_;
78  }
79  
CreateRequestId()80  void DataRequest::CreateRequestId()
81  {
82      requestId_++;
83  }
84  } // namespace Telephony
85  } // namespace OHOS