1 /* 2 * Copyright (c) 2024 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 "start_test_server.h" 17 18 #include "iremote_broker.h" 19 #include "hilog/log.h" 20 #include "iremote_broker.h" 21 #include "iservice_registry.h" 22 #include "test_server_interface_proxy.h" 23 #include "system_ability_definition.h" 24 25 namespace OHOS::testserver { 26 static constexpr int32_t TESTSERVER_LOAD_TIMEOUT_MS = 1000; // ms 27 GetInstance()28 StartTestServer &StartTestServer::GetInstance() 29 { 30 static StartTestServer startTestServer; 31 return startTestServer; 32 } 33 34 class TestServerLoadCallback : public SystemAbilityLoadCallbackStub { 35 public: TestServerLoadCallback(int32_t systemAbilityId)36 explicit TestServerLoadCallback(int32_t systemAbilityId) : systemAbilityId_(systemAbilityId){}; 37 InitLoadState()38 void InitLoadState() 39 { 40 std::unique_lock<std::mutex> lock(locatorMutex_); 41 loadState_ = false; 42 } 43 WaitLoadStateChange(int32_t systemAbilityId)44 bool WaitLoadStateChange(int32_t systemAbilityId) 45 { 46 std::unique_lock<std::mutex> lock(locatorMutex_); 47 auto wait = locatorCond_.wait_for(lock, std::chrono::milliseconds(TESTSERVER_LOAD_TIMEOUT_MS), [this] { 48 return loadState_ == true; 49 }); 50 if (!wait) { 51 return false; 52 } 53 return true; 54 } 55 GetTestServerObject()56 sptr<IRemoteObject> GetTestServerObject() 57 { 58 return remoteObject_; 59 } 60 61 private: OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)62 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override 63 { 64 if (systemAbilityId == systemAbilityId_) { 65 std::unique_lock<std::mutex> lock(locatorMutex_); 66 loadState_ = true; 67 remoteObject_ = remoteObject; 68 locatorCond_.notify_one(); 69 } 70 } 71 OnLoadSystemAbilityFail(int32_t systemAbilityId)72 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override 73 { 74 std::unique_lock<std::mutex> lock(locatorMutex_); 75 loadState_ = false; 76 locatorCond_.notify_one(); 77 } 78 79 int32_t systemAbilityId_; 80 std::condition_variable locatorCond_; 81 std::mutex locatorMutex_; 82 bool loadState_ = false; 83 sptr<IRemoteObject> remoteObject_ = nullptr; 84 }; 85 LoadTestServer()86 sptr<ITestServerInterface> StartTestServer::LoadTestServer() 87 { 88 const int32_t systemAbilityId = TEST_SERVER_SA_ID; 89 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 90 if (samgr == nullptr) { 91 return nullptr; 92 } 93 auto object = samgr->CheckSystemAbility(systemAbilityId); 94 if (object != nullptr) { 95 remoteObject_ = object; 96 } else { 97 auto testServerLoadCallback = sptr<TestServerLoadCallback>(new TestServerLoadCallback(systemAbilityId)); 98 testServerLoadCallback->InitLoadState(); 99 int32_t ret = samgr->LoadSystemAbility(systemAbilityId, testServerLoadCallback); 100 if (ret != ERR_NONE) { 101 return nullptr; 102 } 103 if (testServerLoadCallback->WaitLoadStateChange(systemAbilityId)) { 104 remoteObject_ = testServerLoadCallback->GetTestServerObject(); 105 } 106 } 107 if (remoteObject_ == nullptr) { 108 return nullptr; 109 } 110 sptr<ITestServerInterface> iTestServerInterface = iface_cast<TestServerInterfaceProxy>(remoteObject_); 111 if (iTestServerInterface == nullptr) { 112 return nullptr; 113 } 114 return iTestServerInterface; 115 } 116 117 } // namespace OHOS::testserver