diff --git a/frameworks/js/napi/src/cellular_data_handler.cpp b/frameworks/js/napi/src/cellular_data_handler.cpp index d117bb1cbfabc12291397cbd3937bdc02becea15..24d453260c3c4f10913a4c935e0934a8dda1aabb 100644 --- a/frameworks/js/napi/src/cellular_data_handler.cpp +++ b/frameworks/js/napi/src/cellular_data_handler.cpp @@ -1653,6 +1653,7 @@ void CellularDataHandler::HandleSimAccountLoaded() } else { ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION); } + DelayedRefSingleton::GetInstance().HandleSimAccountChanged(); } void CellularDataHandler::CreateApnItem() diff --git a/frameworks/js/napi/src/cellular_data_service.cpp b/frameworks/js/napi/src/cellular_data_service.cpp index d570cd6719f3f85c482be774cdd721adc43136f1..040395485f431a7f6e23224c13a2908558e5699d 100644 --- a/frameworks/js/napi/src/cellular_data_service.cpp +++ b/frameworks/js/napi/src/cellular_data_service.cpp @@ -397,6 +397,9 @@ int32_t CellularDataService::ReleaseNet(const NetRequest &request) int32_t simId = std::stoi(requestIdent); int32_t slotId = CoreManagerInner::GetInstance().GetSlotId(simId); std::shared_ptr cellularDataController = GetCellularDataController(slotId); + if (cellularDataController == nullptr) { + cellularDataController = GetCellularDataControllerForce(simId); + } if (cellularDataController == nullptr) { return CELLULAR_DATA_INVALID_PARAM; } @@ -417,6 +420,9 @@ int32_t CellularDataService::RemoveUid(const NetRequest &request) int32_t simId = atoi(requestIdent.c_str()); int32_t slotId = CoreManagerInner::GetInstance().GetSlotId(simId); std::shared_ptr cellularDataController = GetCellularDataController(slotId); + if (cellularDataController == nullptr) { + cellularDataController = GetCellularDataControllerForce(simId); + } if (cellularDataController == nullptr) { return CELLULAR_DATA_INVALID_PARAM; } @@ -757,6 +763,23 @@ std::shared_ptr CellularDataService::GetCellularDataCont return item->second; } +std::shared_ptr CellularDataService::GetCellularDataControllerForce(int32_t simId) +{ + TELEPHONY_LOGI("GetCellularDataControllerForce, enter"); + if (!isInitSuccess_) { + return nullptr; + } + + std::lock_guard guard(mapLock_); + for (auto it = cellularDataControllers_.begin(); it != cellularDataControllers_.end(); ++it) { + if (it->second != nullptr && it->second->GetSimId() == simId) { + TELEPHONY_LOGI("GetCellularDataControllerForce, simId=%{public}d", it->second->GetSimId()); + return it->second; + } + } + return nullptr; +} + int32_t CellularDataService::EstablishAllApnsIfConnectable(const int32_t slotId) { if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) { @@ -1020,5 +1043,16 @@ int32_t CellularDataService::GetActiveApnName(std::string &apnName) } return 0; } + +void CellularDataService::HandleSimAccountChanged() +{ + std::lock_guard guard(mapLock_); + for (auto it = cellularDataControllers_.begin(); it != cellularDataControllers_.end(); ++it) { + if (it->second == nullptr) { + continue; + } + it->second->UpdateSimIdInfo(); + } +} } // namespace Telephony } // namespace OHOS diff --git a/services/include/cellular_data_controller.h b/services/include/cellular_data_controller.h index d8429e70e0eed8c6758fdc18b7dc2aff87e94399..b21b13818514ed37862a9d6002bfb21a5339b8d2 100644 --- a/services/include/cellular_data_controller.h +++ b/services/include/cellular_data_controller.h @@ -62,6 +62,8 @@ public: bool IsSupportDunApn(); bool GetDefaultActReportInfo(ApnActivateReportInfo &info); bool GetInternalActReportInfo(ApnActivateReportInfo &info); + void UpdateSimIdInfo(); + int32_t GetSimId(); private: void RegisterEvents(); @@ -71,6 +73,7 @@ private: std::shared_ptr cellularDataHandler_; sptr systemAbilityListener_ = nullptr; const int32_t slotId_; + int32_t simId_; private: class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { diff --git a/services/include/cellular_data_service.h b/services/include/cellular_data_service.h index c18af7b780334fe06702057b71146784ba98acfb..373fb17ff297862e9589a71d5e7c211798dcff57 100644 --- a/services/include/cellular_data_service.h +++ b/services/include/cellular_data_service.h @@ -104,6 +104,7 @@ public: int32_t GetNetworkSliceAllowedNssai(int32_t slotId, const std::vector& buffer) override; int32_t GetNetworkSliceEhplmn(int32_t slotId) override; int32_t GetActiveApnName(std::string &apnName) override; + void HandleSimAccountChanged(); private: bool Init(); @@ -113,6 +114,7 @@ private: void ClearCellularDataControllers(); void AddCellularDataControllers(int32_t slotId, std::shared_ptr cellularDataController); std::shared_ptr GetCellularDataController(int32_t slotId); + std::shared_ptr GetCellularDataControllerForce(int32_t simId); private: std::map> cellularDataControllers_; diff --git a/services/src/cellular_data_controller.cpp b/services/src/cellular_data_controller.cpp index a12aaffb95bbbae2e9273a8d93fbe1693fe5cbe1..702aa96020145d2cbec70df1b8ceff1aad7b48d8 100644 --- a/services/src/cellular_data_controller.cpp +++ b/services/src/cellular_data_controller.cpp @@ -529,5 +529,15 @@ bool CellularDataController::GetInternalActReportInfo(ApnActivateReportInfo &inf return true; } +void CellularDataController::UpdateSimIdInfo() +{ + simId_ = CoreManagerInner::GetInstance().GetSimId(slotId_); +} + +int32_t CellularDataController::GetSimId() +{ + return simId_; +} + } // namespace Telephony } // namespace OHOS diff --git a/test/cellular_data_handler_test.cpp b/test/cellular_data_handler_test.cpp index 0294f240a44378af542ea16626a0153fe43bb097..3fdc84960c6464e68e483373e44ea1ef0c0f5e29 100644 --- a/test/cellular_data_handler_test.cpp +++ b/test/cellular_data_handler_test.cpp @@ -1159,5 +1159,21 @@ HWTEST_F(CellularDataHandlerTest, IsCellularDataEnabledTest001, Function | Mediu cellularDataHandler->dataSwitchSettings_->lastQryRet_ = TELEPHONY_ERR_SUCCESS; EXPECT_EQ(cellularDataHandler->IsCellularDataEnabled(isDataEnabled), TELEPHONY_ERR_SUCCESS); } + +/** +@tc.number Telephony_HandleSimAccountLoaded001 +@tc.name HandleSimAccountLoaded +@tc.desc Function test +*/ +HWTEST_F(CellularDataHandlerTest, HandleSimAccountLoaded001, Function | MediumTest | Level1) +{ + int32_t slotId = 0; + EventFwk::MatchingSkills matchingSkills; + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto cellularDataHandler = std::make_shared(subscriberInfo, slotId); + cellularDataHandler->Init(); + cellularDataHandler->HandleSimAccountLoaded(); + EXPECT_FALSE(cellularDataHandler->connectionManager_ == nullptr); +} } // namespace Telephony } // namespace OHOS \ No newline at end of file diff --git a/test/cellular_data_service_test.cpp b/test/cellular_data_service_test.cpp index ab8888f3e1804360da4e4e1ffcf4ab37004d70af..a77e2ec8a50f86688c97731efffe6245c22be3b7 100644 --- a/test/cellular_data_service_test.cpp +++ b/test/cellular_data_service_test.cpp @@ -509,5 +509,46 @@ HWTEST_F(CellularDataServiceTest, ReleaseNet_001, TestSize.Level1) ASSERT_EQ(CELLULAR_DATA_INVALID_PARAM, service->RequestNet(request)); ASSERT_EQ(CELLULAR_DATA_INVALID_PARAM, service->ReleaseNet(request)); } + +/** + * @tc.number CellularDataService_ReleaseNet_001 + * @tc.name test function branch + * @tc.desc Function test + */ +HWTEST_F(CellularDataServiceTest, CellularDataService_ReleaseNet_001, TestSize.Level1) +{ + DataAccessToken token; + service->OnStart(); + service->isInitSuccess_ = false; + NetRequest request; + request.ident = "simId2"; + ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->ReleaseNet(request)); +} + +/** + * @tc.number CellularDataService_ReleaseNet_002 + * @tc.name test function branch + * @tc.desc Function test + */ +HWTEST_F(CellularDataServiceTest, CellularDataService_ReleaseNet_002, TestSize.Level1) +{ + DataAccessToken token; + service->OnStart(); + service->isInitSuccess_ = true; + service->cellularDataControllers_.clear(); + std::shared_ptr cellularDataController = std::make_shared(0); + service->cellularDataControllers_.insert( + std::pair>(0, cellularDataController)); + + service->HandleSimAccountChanged(); + auto it = service->cellularDataControllers_.begin(); + int32_t simId = it->second->GetSimId(); + + NetRequest request; + std::string str = "simId" + std::to_string(simId); + request.ident = str; + ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->ReleaseNet(request)); + service->cellularDataControllers_.clear(); +} } // namespace Telephony } // namespace OHOS \ No newline at end of file