• 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 <sys/time.h>
17 #include "networkslicemanager.h"
18 #include "hisysevent.h"
19 #include "netmgr_ext_log_wrapper.h"
20 #include "system_ability_definition.h"
21 #include "hisysevent.h"
22 #include "net_manager_constants.h"
23 #include "networkslicemsgcenter.h"
24 #include "networkslice_kernel_proxy.h"
25 #include "bundle_mgr_proxy.h"
26 #include "bundle_mgr_client.h"
27 #include "iservice_registry.h"
28 #include "pdp_profile_data.h"
29 #include "core_manager_inner.h"
30 #include "os_account_manager.h"
31 #include "state_utils.h"
32 #include "core_service_client.h"
33 #include "networkslice_service.h"
34 
35 static std::string APN_DATA_DB_SELECTION = "datashare:///com.ohos.pdpprofileability/net/pdp_profile";
36 static std::string APN_DATA_DB_URI = "datashare:///com.ohos.pdpprofileability";
37 constexpr int MCC_LEN = 3;
38 constexpr int SLICE_MAXSIZE = 6;
39 constexpr int SLICE_SA_ID = 8301;
40 namespace OHOS {
41 namespace NetManagerStandard {
42 NetworkSliceService NetworkSliceService::instance_; /* NOLINT */
43 
44 const bool REGISTER_LOCAL_RESULT_NETWORKSLICE =
45     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetworkSliceService>::GetInstance().get());
46 
NetworkSliceService()47 NetworkSliceService::NetworkSliceService()
48     : SystemAbility(SLICE_SA_ID, true), isRegistered_(false), state_(STATE_STOPPED)
49 {}
50 
~NetworkSliceService()51 NetworkSliceService::~NetworkSliceService()
52 {
53     state_ = STATE_STOPPED;
54     isRegistered_ = false;
55 }
56 
GetInstance()57 NetworkSliceService &NetworkSliceService::GetInstance()
58 {
59     return instance_;
60 }
61 
OnStart()62 void NetworkSliceService::OnStart()
63 {
64     NETMGR_EXT_LOG_I("NetworkSliceService OnStart begin");
65     if (state_ == STATE_RUNNING) {
66         NETMGR_EXT_LOG_I("NetworkSliceService the state is already running");
67         return;
68     }
69     if (!Init()) {
70         NETMGR_EXT_LOG_E("NetworkSliceService init failed");
71         return;
72     }
73     state_ = STATE_RUNNING;
74     NETMGR_EXT_LOG_I("NetworkSliceService OnStart end");
75 }
76 
OnStop()77 void NetworkSliceService::OnStop()
78 {
79     NETMGR_EXT_LOG_I("NetworkSliceService OnStop begin");
80     if (state_ == STATE_STOPPED) {
81         return;
82     }
83     state_ = STATE_STOPPED;
84     isRegistered_ = false;
85 }
86 
Init()87 bool NetworkSliceService::Init()
88 {
89     NETMGR_EXT_LOG_I("NetworkSliceService init start");
90     if (!REGISTER_LOCAL_RESULT_NETWORKSLICE) {
91         NETMGR_EXT_LOG_I("Register to local sa manager failed");
92         isRegistered_ = false;
93         return false;
94     }
95 
96     if (isRegistered_) {
97         return true;
98     }
99 
100     if (!Publish(DelayedSingleton<NetworkSliceService>::GetInstance().get())) {
101         NETMGR_EXT_LOG_I("Register to sa manager failed");
102         return false;
103     }
104     isRegistered_ = true;
105     InitModule();
106     NETMGR_EXT_LOG_I("Init success");
107     return true;
108 }
109 
InitModule()110 void NetworkSliceService::InitModule()
111 {
112     NETMGR_EXT_LOG_I("NetworkSliceService InitModule");
113     DelayedSingleton<NetworkSliceManager>::GetInstance()->Init();
114 }
115 
UpdateNetworkSliceApn()116 bool NetworkSliceService::UpdateNetworkSliceApn()
117 {
118     NETMGR_EXT_LOG_I("NetworkSliceService UpdateNetworkSliceApn");
119     sptr<ISystemAbilityManager> networksliceManager =
120         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121     if (networksliceManager == nullptr) {
122         NETMGR_EXT_LOG_I("UpdateNetworkSliceApn networksliceManager == nullptr");
123         return false;
124     }
125     sptr<IRemoteObject> remoteObj = networksliceManager->GetSystemAbility(SLICE_SA_ID);
126     if (remoteObj == nullptr) {
127         NETMGR_EXT_LOG_I("UpdateNetworkSliceApn remoteObj == nullptr");
128         return false;
129     }
130     std::shared_ptr<DataShare::DataShareHelper> networksliceApnHelper =
131         OHOS::DataShare::DataShareHelper::Creator(remoteObj, APN_DATA_DB_URI);
132     if (networksliceApnHelper == nullptr) {
133         NETMGR_EXT_LOG_I("UpdateNetworkSliceApn networksliceApnHelper == nullptr");
134         return false;
135     }
136 
137     DataShare::DataSharePredicates predicates;
138     std::u16string operatorNumeric;
139     int32_t slotId = StateUtils::GetPrimarySlotId();
140     Telephony::CoreServiceClient::GetInstance().GetSimOperatorNumeric(slotId, operatorNumeric);
141     std::string plmn = Str16ToStr8(operatorNumeric);
142     NETMGR_EXT_LOG_I("UpdateNetworkSliceApn plmn = %{public}s", plmn.c_str());
143     bool writeResult = false;
144     for (int i = 1; i <= SLICE_MAXSIZE; i++) {
145         std::string apntype = "snssai";
146         apntype += std::to_string(i);
147         predicates.EqualTo(OHOS::Telephony::PdpProfileData::MCCMNC, plmn)\
148             ->EqualTo(OHOS::Telephony::PdpProfileData::APN_TYPES, apntype);
149         Uri networksliceUrl(APN_DATA_DB_SELECTION);
150         networksliceApnHelper->Delete(networksliceUrl, predicates);
151         if (WriteNetworkSliceApnToDb(networksliceApnHelper, apntype) == true) {
152             writeResult = true;
153         }
154     }
155     networksliceApnHelper->Release();
156     return writeResult;
157 }
158 
WriteNetworkSliceApnToDb(std::shared_ptr<DataShare::DataShareHelper> networksliceApnHelper,std::string apntype)159 int NetworkSliceService::WriteNetworkSliceApnToDb(std::shared_ptr<DataShare::DataShareHelper> networksliceApnHelper,
160     std::string apntype)
161 {
162     std::u16string operatorNumeric;
163     int32_t slotId = StateUtils::GetPrimarySlotId();
164     Telephony::CoreServiceClient::GetInstance().GetSimOperatorNumeric(slotId, operatorNumeric);
165     std::string plmn = Str16ToStr8(operatorNumeric);
166     NETMGR_EXT_LOG_I("WriteNetworkSliceApnToDb plmn = %{public}s", plmn.c_str());
167     Uri networksliceUrl(APN_DATA_DB_SELECTION);
168     DataShare::DataShareValuesBucket value;
169     value.Put(Telephony::PdpProfileData::MCCMNC, plmn);
170     value.Put(Telephony::PdpProfileData::MCC, plmn.substr(0, MCC_LEN));
171     value.Put(Telephony::PdpProfileData::MNC, plmn.substr(MCC_LEN));
172     value.Put(Telephony::PdpProfileData::AUTH_TYPE, "0");
173     value.Put(Telephony::PdpProfileData::EDITED_STATUS, "1");
174     value.Put(Telephony::PdpProfileData::OPKEY, plmn);
175     value.Put(Telephony::PdpProfileData::APN_TYPES, apntype);
176     value.Put(Telephony::PdpProfileData::BEARING_SYSTEM_TYPE, "0");
177     value.Put(Telephony::PdpProfileData::IS_ROAMING_APN, "0");
178     std::string apnName = apntype;
179     if (!apnName.empty()) {
180         value.Put(Telephony::PdpProfileData::APN, apnName.c_str());
181     }
182     value.Put(Telephony::PdpProfileData::PROFILE_NAME, apntype);
183     value.Put(Telephony::PdpProfileData::APN_PROTOCOL, "IPV4V6");
184     value.Put(Telephony::PdpProfileData::APN_ROAM_PROTOCOL, "IPV4V6");
185     return networksliceApnHelper->Insert(networksliceUrl, value);
186 }
187 
SetNetworkSliceUePolicy(std::vector<uint8_t> buffer)188 int32_t NetworkSliceService::SetNetworkSliceUePolicy(std::vector<uint8_t> buffer)
189 {
190     NETMGR_EXT_LOG_I("NetworkSliceService::SetNetworkSliceUePolicy");
191     std::shared_ptr<std::vector<uint8_t>> msg = std::make_shared<std::vector<uint8_t>>(buffer);
192     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_HANDLE_UE_POLICY, msg);
193     return NETMANAGER_EXT_SUCCESS;
194 }
195 
NetworkSliceAllowedNssaiRpt(std::vector<uint8_t> buffer)196 int32_t NetworkSliceService::NetworkSliceAllowedNssaiRpt(std::vector<uint8_t> buffer)
197 {
198     NETMGR_EXT_LOG_I("NetworkSliceService::NetworkSliceAllowedNssaiRpt");
199     std::shared_ptr<std::vector<uint8_t>> msg = std::make_shared<std::vector<uint8_t>>(buffer);
200     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_HANDLE_ALLOWED_NSSAI, msg);
201     return NETMANAGER_EXT_SUCCESS;
202 }
203 
NetworkSliceEhplmnRpt(std::vector<uint8_t> buffer)204 int32_t NetworkSliceService::NetworkSliceEhplmnRpt(std::vector<uint8_t> buffer)
205 {
206     NETMGR_EXT_LOG_I("NetworkSliceService::NetworkSliceEhplmnRpt");
207     std::shared_ptr<std::vector<uint8_t>> msg = std::make_shared<std::vector<uint8_t>>(buffer);
208     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_HANDLE_EHPLMN, msg);
209     return NETMANAGER_EXT_SUCCESS;
210 }
211 
NetworkSliceInitUePolicy()212 int32_t NetworkSliceService::NetworkSliceInitUePolicy()
213 {
214     NETMGR_EXT_LOG_I("NetworkSliceService::InitUePolicy");
215     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_INIT_UE_POLICY);
216     return NETMANAGER_EXT_SUCCESS;
217 }
218 
NetworkSliceGetRSDByAppDescriptor(std::shared_ptr<GetSlicePara> & getSlicePara)219 int32_t NetworkSliceService::NetworkSliceGetRSDByAppDescriptor(std::shared_ptr<GetSlicePara>& getSlicePara)
220 {
221     NETMGR_EXT_LOG_I("NetworkSliceService::GetRSDByAppDescriptor");
222     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_GET_SLICE_PARA, getSlicePara);
223     return NETMANAGER_EXT_SUCCESS;
224 }
225 
RecvKernelData(void * rcvMsg,int32_t dataLen)226 int32_t NetworkSliceService::RecvKernelData(void* rcvMsg, int32_t dataLen)
227 {
228     NETMGR_EXT_LOG_I("NetworkSliceService::RecvKernelData");
229     if (rcvMsg == nullptr || dataLen <= 0) {
230         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
231     }
232     KernelMsgNS *kernelMsg = reinterpret_cast<KernelMsgNS *>(rcvMsg);
233     if (!kernelMsg) {
234         NETMGR_EXT_LOG_I("NetworkSliceService RecvKernelData kernelMsg is null");
235         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
236     }
237 
238     int32_t buflen = dataLen - sizeof(short) - sizeof(short);
239     if (buflen <= 0) {
240         NETMGR_EXT_LOG_I("NetworkSliceServiceKernel msg buff is null.");
241         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
242     }
243     short len = (static_cast<unsigned char>(kernelMsg->buf[3]) << 8) |
244                 (static_cast<unsigned char>(kernelMsg->buf[2]));
245     std::vector<uint8_t> msgData;
246     msgData.resize(len);
247     std::copy(kernelMsg->buf, kernelMsg->buf + len, msgData.data());
248     for (int i = 0; i < (int)msgData.size(); ++i) {
249         NETMGR_EXT_LOG_I("NetworkSliceService msgData[%{public}d] = %{public}d", i, msgData[i]);
250     }
251     std::shared_ptr<std::vector<uint8_t>> msg = std::make_shared<std::vector<uint8_t>>(msgData);
252     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_KERNEL_IP_ADDR_REPORT, msg);
253     return NETMANAGER_EXT_SUCCESS;
254 }
255 
GetBundleNameForUid(int32_t uid,std::string & bundleName)256 int NetworkSliceService::GetBundleNameForUid(int32_t uid, std::string &bundleName)
257 {
258     auto systemAbilityManager =
259         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
260     if (!systemAbilityManager) {
261         NETMGR_EXT_LOG_E("GetBundleNameForUid failed, system ability manager is null");
262         return NETMANAGER_EXT_ERR_INTERNAL;
263     }
264     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
265     if (!remoteObject) {
266         NETMGR_EXT_LOG_E("GetBundleNameForUid failed, bundle manager service is not ready");
267         return NETMANAGER_EXT_ERR_INTERNAL;
268     }
269     sptr<AppExecFwk::IBundleMgr> iBundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
270     if (!iBundleMgr) {
271         NETMGR_EXT_LOG_E("GetBundleNameForUid failed, bundle manager is null");
272         return NETMANAGER_EXT_ERR_INTERNAL;
273     }
274     return iBundleMgr->GetNameForUid(uid, bundleName);
275 }
276 
BindToNetwork(std::map<std::string,std::string> buffer)277 int32_t NetworkSliceService::BindToNetwork(std::map<std::string, std::string> buffer)
278 {
279     NETMGR_EXT_LOG_I("NetworkSliceService::BindToNetwork");
280     std::shared_ptr<std::map<std::string, std::string>> msg =
281         std::make_shared<std::map<std::string, std::string>>(buffer);
282     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_BIND_TO_NETWORK, msg);
283     return NETMANAGER_EXT_SUCCESS;
284 }
285 
DelBindToNetwork(std::map<std::string,std::string> buffer)286 int32_t NetworkSliceService::DelBindToNetwork(std::map<std::string, std::string> buffer)
287 {
288     NETMGR_EXT_LOG_I("NetworkSliceService::DelBindToNetwork");
289     std::shared_ptr<std::map<std::string, std::string>> msg =
290         std::make_shared<std::map<std::string, std::string>>(buffer);
291     Singleton<NetworkSliceMsgCenter>::GetInstance().Publish(EVENT_DEL_BIND_TO_NETWORK, msg);
292     return NETMANAGER_EXT_SUCCESS;
293 }
294 
GetUidByBundleName(const std::string & bundleName)295 int32_t NetworkSliceService::GetUidByBundleName(const std::string& bundleName)
296 {
297     AppExecFwk::BundleInfo info;
298     AppExecFwk::BundleMgrClient client;
299     if (!client.GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_DEFAULT, info,
300         AppExecFwk::Constants::ALL_USERID)) {
301         NETMGR_EXT_LOG_E("Failed to query uid from bms");
302     } else {
303         NETMGR_EXT_LOG_D("Succ to get uid=%{public}d", info.uid);
304     }
305     return info.uid;
306 }
307 
GetUidsByBundleName(const std::string & bundleName)308 std::set<int32_t> NetworkSliceService::GetUidsByBundleName(const std::string& bundleName)
309 {
310     AppExecFwk::BundleInfo info;
311     AppExecFwk::BundleMgrClient client;
312     if (!client.GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_DEFAULT, info,
313         AppExecFwk::Constants::ALL_USERID)) {
314         NETMGR_EXT_LOG_E("Failed to query uid from bms");
315     } else {
316         NETMGR_EXT_LOG_D("Succ to get uid=%{public}d", info.uid);
317     }
318     int32_t bundleId = info.uid % 200000;
319     std::vector<AccountSA::OsAccountInfo> osAccountInfos;
320     AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos);
321     std::set<int32_t> uids;
322     for (int i = 0; i < (int)osAccountInfos.size(); ++i) {
323         int32_t uid = osAccountInfos[i].GetLocalId() * 200000 + bundleId % 200000;
324         uids.insert(uid);
325     }
326     return uids;
327 }
328 
GetRouteSelectionDescriptorByDNN(std::string dnn,std::string & snssai,uint8_t & sscMode)329 int32_t NetworkSliceService::GetRouteSelectionDescriptorByDNN(std::string dnn, std::string& snssai, uint8_t& sscMode)
330 {
331     NETMGR_EXT_LOG_I("NetworkSliceService::GetRSDByAppDescriptor");
332     DelayedSingleton<NetworkSliceManager>::GetInstance()->GetRouteSelectionDescriptorByDNN(dnn, snssai, sscMode);
333     return NETMANAGER_EXT_SUCCESS;
334 }
335 
GetRSDByNetCap(int32_t netcap,std::map<std::string,std::string> & networkSliceParas)336 int32_t NetworkSliceService::GetRSDByNetCap(int32_t netcap, std::map<std::string, std::string>& networkSliceParas)
337 {
338     NETMGR_EXT_LOG_I("NetworkSliceService::GetRSDByNetCap");
339     DelayedSingleton<HwNetworkSliceManager>::GetInstance()->GetRSDByNetCap(netcap, networkSliceParas);
340     return NETMANAGER_EXT_SUCCESS;
341 }
342 
SetSaState(bool isSaState)343 int32_t NetworkSliceService::SetSaState(bool isSaState)
344 {
345     NETMGR_EXT_LOG_I("NetworkSliceService::SetSaState");
346     DelayedSingleton<NetworkSliceManager>::GetInstance()->SetSaState(isSaState);
347     return NETMANAGER_EXT_SUCCESS;
348 }
349 } // namespace NetManagerStandard
350 } // namespace OHOS
351