• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "usbd_load_usb_service.h"
17 
18 #include <cstdlib>
19 #include <iostream>
20 #include <unistd.h>
21 
22 #include "hdf_base.h"
23 #include "iservice_registry.h"
24 #include "osal_thread.h"
25 #include "osal_time.h"
26 #include "securec.h"
27 
28 using namespace OHOS;
29 using namespace std;
30 #define HDF_LOG_TAG usbd_load_usb_service
31 
32 namespace OHOS {
33 namespace HDI {
34 namespace Usb {
35 namespace V1_0 {
OnDemandLoadCallback()36 OnDemandLoadCallback::OnDemandLoadCallback() {}
37 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)38 void OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
39 {
40     loading_ = false;
41     HDF_LOGI("%{public}s: OnLoadSystemAbilitySuccess systemAbilityId: %{public}d", __func__, systemAbilityId);
42 }
43 
OnLoadSystemAbilityFail(int32_t systemAbilityId)44 void OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
45 {
46     loading_ = false;
47     HDF_LOGI("%{public}s: OnLoadSystemAbilityFail systemAbilityId: %{public}d", __func__, systemAbilityId);
48 }
49 
LoadService()50 int32_t UsbdLoadService::LoadService()
51 {
52     if (loadCallback_ != nullptr && loadCallback_->loading_) {
53         HDF_LOGW("%{public}s:sa is loading", __func__);
54         return HDF_SUCCESS;
55     }
56 
57     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58     if (samgr == nullptr) {
59         HDF_LOGE("%{public}s:GetSystemAbilityManager failed", __func__);
60         return HDF_FAILURE;
61     }
62     auto saObj = samgr->CheckSystemAbility(saId_);
63     if (saObj != nullptr) {
64         return HDF_SUCCESS;
65     }
66 
67     if (loadCallback_ == nullptr) {
68         loadCallback_ = new (std::nothrow) OnDemandLoadCallback();
69         if (loadCallback_ == nullptr) {
70             HDF_LOGE("create OnDemandLoadCallback failed");
71             return HDF_DEV_ERR_NO_MEMORY;
72         }
73     }
74 
75     loadCallback_->loading_ = true;
76     int32_t result = samgr->LoadSystemAbility(saId_, loadCallback_);
77     if (result != ERR_OK) {
78         HDF_LOGE("LoadSystemAbility failed");
79         loadCallback_->loading_ = false;
80         return HDF_FAILURE;
81     }
82     return HDF_SUCCESS;
83 }
84 } // namespace V1_0
85 } // namespace Usb
86 } // namespace HDI
87 } // namespace OHOS
88