• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2026 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 <thread>
17 
18 #include "iservice_registry.h"
19 #include "net_manager_constants.h"
20 #include "netmgr_ext_log_wrapper.h"
21 #include "system_ability_definition.h"
22 #include "networkslice_client.h"
23 
24 using namespace OHOS::HiviewDFX;
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 constexpr int SLICE_SA_ID = 8301;
NetworkSliceClient()29 NetworkSliceClient::NetworkSliceClient() : networksliceService_(nullptr), deathRecipient_(nullptr) {}
30 
~NetworkSliceClient()31 NetworkSliceClient::~NetworkSliceClient()
32 {
33     DlCloseRemoveDeathRecipient();
34 }
35 
SetNetworkSliceUePolicy(std::vector<uint8_t> buffer)36 int32_t NetworkSliceClient::SetNetworkSliceUePolicy(std::vector<uint8_t> buffer)
37 {
38     NETMGR_EXT_LOG_I("NetworkSliceClient::SetNetworkSliceUePolicy, buffersize = %{public}d", int(buffer.size()));
39     sptr<INetworkSliceService> proxy = GetProxy();
40     if (proxy == nullptr) {
41         NETMGR_EXT_LOG_E("GetNetworkSliceService proxy is nullptr");
42         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
43     }
44     return proxy->SetNetworkSliceUePolicy(buffer);
45 }
46 
NetworkSliceInitUePolicy()47 int32_t NetworkSliceClient::NetworkSliceInitUePolicy()
48 {
49     NETMGR_EXT_LOG_I("NetworkSliceClient::NetworkSliceInitUePolicy");
50     sptr<INetworkSliceService> proxy = GetProxy();
51     if (proxy == nullptr) {
52         NETMGR_EXT_LOG_E("GetNetworkSliceService proxy is nullptr");
53         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
54     }
55     return proxy->NetworkSliceInitUePolicy();
56 }
57 
NetworkSliceAllowedNssaiRpt(std::vector<uint8_t> buffer)58 int32_t NetworkSliceClient::NetworkSliceAllowedNssaiRpt(std::vector<uint8_t> buffer)
59 {
60     NETMGR_EXT_LOG_I("NetworkSliceClient::NetworkSliceAllowedNssaiRpt");
61     sptr<INetworkSliceService> proxy = GetProxy();
62     if (proxy == nullptr) {
63         NETMGR_EXT_LOG_E("NetworkSliceAllowedNssaiRpt proxy is nullptr");
64         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
65     }
66     return proxy->NetworkSliceAllowedNssaiRpt(buffer);
67 }
68 
NetworkSliceEhplmnRpt(std::vector<uint8_t> buffer)69 int32_t NetworkSliceClient::NetworkSliceEhplmnRpt(std::vector<uint8_t> buffer)
70 {
71     NETMGR_EXT_LOG_I("NetworkSliceClient::NetworkSliceEhplmnRpt");
72     sptr<INetworkSliceService> proxy = GetProxy();
73     if (proxy == nullptr) {
74         NETMGR_EXT_LOG_E("NetworkSliceEhplmnRpt proxy is nullptr");
75         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
76     }
77     return proxy->NetworkSliceEhplmnRpt(buffer);
78 }
79 
GetRouteSelectionDescriptorByDNN(std::string dnn,std::string & snssai,uint8_t & sscMode)80 int32_t NetworkSliceClient::GetRouteSelectionDescriptorByDNN(std::string dnn, std::string& snssai, uint8_t& sscMode)
81 {
82     NETMGR_EXT_LOG_I("NetworkSliceClient::GetRouteSelectionDescriptorByDNN");
83     sptr<INetworkSliceService> proxy = GetProxy();
84     if (proxy == nullptr) {
85         NETMGR_EXT_LOG_E("GetRouteSelectionDescriptorByDNN proxy is nullptr");
86         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
87     }
88     return proxy->GetRouteSelectionDescriptorByDNN(dnn, snssai, sscMode);
89 }
90 
GetRSDByNetCap(int32_t netcap,std::map<std::string,std::string> & networkSliceParas)91 int32_t NetworkSliceClient::GetRSDByNetCap(int32_t netcap, std::map<std::string, std::string>& networkSliceParas)
92 {
93     NETMGR_EXT_LOG_I("NetworkSliceClient::GetRSDByNetCap");
94     sptr<INetworkSliceService> proxy = GetProxy();
95     if (proxy == nullptr) {
96         NETMGR_EXT_LOG_E("GetRSDByNetCap proxy is nullptr");
97         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
98     }
99     return proxy->GetRSDByNetCap(netcap, networkSliceParas);
100 }
101 
SetSaState(bool isSaState)102 int32_t NetworkSliceClient::SetSaState(bool isSaState)
103 {
104     NETMGR_EXT_LOG_I("NetworkSliceClient::SetSaState");
105     sptr<INetworkSliceService> proxy = GetProxy();
106     if (proxy == nullptr) {
107         NETMGR_EXT_LOG_E("SetSaState proxy is nullptr");
108         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
109     }
110     return proxy->SetSaState(isSaState);
111 }
112 
GetProxy()113 sptr<INetworkSliceService> NetworkSliceClient::GetProxy()
114 {
115     std::lock_guard lock(mutex_);
116     if (networksliceService_) {
117         NETMGR_EXT_LOG_D("get proxy is ok");
118         return networksliceService_;
119     }
120     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121     if (sam == nullptr) {
122         NETMGR_EXT_LOG_E("GetProxy, get SystemAbilityManager failed");
123         return nullptr;
124     }
125     sptr<IRemoteObject> remote = sam->CheckSystemAbility(SLICE_SA_ID);
126     if (remote == nullptr) {
127         NETMGR_EXT_LOG_E("get Remote service failed");
128         return nullptr;
129     }
130     deathRecipient_ = sptr<NetworkSliceDeathRecipient>::MakeSptr(*this);
131     if (deathRecipient_ == nullptr) {
132         NETMGR_EXT_LOG_E("Recipient new failed!");
133     }
134     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
135         NETMGR_EXT_LOG_E("add death recipient failed");
136         return nullptr;
137     }
138     networksliceService_ = iface_cast<INetworkSliceService>(remote);
139     if (networksliceService_ == nullptr) {
140         NETMGR_EXT_LOG_E("get Remote service proxy failed");
141         return nullptr;
142     }
143     return networksliceService_;
144 }
145 
OnRemoteDied(const wptr<IRemoteObject> & remote)146 void NetworkSliceClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
147 {
148     NETMGR_EXT_LOG_I("NetworkSlice OnRemoteDied");
149     if (remote == nullptr) {
150         NETMGR_EXT_LOG_E("OnRemoteDied failed, remote is nullptr");
151         return;
152     }
153     std::lock_guard<std::mutex> lock(mutex_);
154     if (networksliceService_ == nullptr) {
155         NETMGR_EXT_LOG_E("OnRemoteDied proxy_ is nullptr");
156         return;
157     }
158     sptr<IRemoteObject> serviceRemote = networksliceService_->AsObject();
159     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
160         serviceRemote->RemoveDeathRecipient(deathRecipient_);
161         networksliceService_ = nullptr;
162         NETMGR_EXT_LOG_E("on remote died");
163     }
164 }
165 
DlCloseRemoveDeathRecipient()166 void NetworkSliceClient::DlCloseRemoveDeathRecipient()
167 {
168     sptr<INetworkSliceService> proxy = GetProxy();
169     if (proxy == nullptr) {
170         NETMGR_EXT_LOG_E("proxy is nullptr");
171         return;
172     }
173 
174     auto serviceRemote = proxy->AsObject();
175     if (serviceRemote == nullptr) {
176         NETMGR_EXT_LOG_E("serviceRemote is nullptr");
177         return;
178     }
179 
180     serviceRemote->RemoveDeathRecipient(deathRecipient_);
181     NETMGR_EXT_LOG_I("RemoveDeathRecipient success");
182 }
183 
184 } // namespace NetManagerStandard
185 } // namespace OHOS
186