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 "mms_network_manager.h"
17  
18  #include "core_manager_inner.h"
19  #include "net_conn_client.h"
20  #include "net_specifier.h"
21  #include "telephony_log_wrapper.h"
22  
23  namespace OHOS {
24  namespace Telephony {
25  using namespace NetManagerStandard;
26  static constexpr uint32_t NET_REGISTER_TIMEOUT_MS = 20000;
27  constexpr const char *SIMID_IDENT_PREFIX = "simId";
28  
AcquireNetwork(int32_t slotId,uint8_t requestId)29  int32_t MmsNetworkManager::AcquireNetwork(int32_t slotId, uint8_t requestId)
30  {
31      NetSpecifier netSpecifier;
32      NetAllCapabilities netAllCapabilities;
33      netAllCapabilities.netCaps_.insert(NetCap::NET_CAPABILITY_MMS);
34      netAllCapabilities.bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
35  
36      int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId);
37      TELEPHONY_LOGI("requestId = %{public}d, slot = %{public}d, simId = %{public}d", requestId, slotId, simId);
38      netSpecifier.ident_ = SIMID_IDENT_PREFIX + std::to_string(simId);
39      netSpecifier.netCapabilities_ = netAllCapabilities;
40  
41      sptr<NetSpecifier> specifier = new (std::nothrow) NetSpecifier(netSpecifier);
42      if (specifier == nullptr) {
43          return TELEPHONY_ERR_LOCAL_PTR_NULL;
44      }
45      callback_ = new (std::nothrow) MmsConnCallbackStub();
46      if (callback_ == nullptr) {
47          return TELEPHONY_ERR_LOCAL_PTR_NULL;
48      }
49  
50      int32_t result =
51          NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback_, NET_REGISTER_TIMEOUT_MS);
52      TELEPHONY_LOGI("acquire network result = %{public}d", result);
53      return result;
54  }
55  
ReleaseNetwork(u_int8_t requestId,bool shouldDelayRelease)56  void MmsNetworkManager::ReleaseNetwork(u_int8_t requestId, bool shouldDelayRelease)
57  {
58      TELEPHONY_LOGI("requestId:%{public}d,shouldDelayRelease:%{public}d", requestId, shouldDelayRelease);
59      if (callback_ != nullptr) {
60          TELEPHONY_LOGI("UnregisterNetConnCallback");
61          NetConnClient ::GetInstance().UnregisterNetConnCallback(callback_);
62          callback_ = nullptr;
63      }
64  }
65  
GetOrCreateHttpClient(int32_t slotId)66  std::shared_ptr<MmsNetworkClient> MmsNetworkManager::GetOrCreateHttpClient(int32_t slotId)
67  {
68      if (mmsNetworkClient_ == nullptr) {
69          TELEPHONY_LOGE("mmsNetworkClient_ nullptr");
70          mmsNetworkClient_ = std::make_shared<OHOS::Telephony::MmsNetworkClient>(slotId);
71      }
72      return mmsNetworkClient_;
73  }
74  } // namespace Telephony
75  } // namespace OHOS
76