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 #include "usbd_wrapper.h"
28 
29 using namespace OHOS;
30 using namespace std;
31 #define HDF_LOG_TAG usbd_load_usb_service
32 
33 namespace OHOS {
34 namespace HDI {
35 namespace Usb {
36 namespace V1_1 {
OnDemandLoadCallback()37 OnDemandLoadCallback::OnDemandLoadCallback()
38 {
39     HDF_LOGI("%{public}s:construct", __func__);
40 }
41 
~OnDemandLoadCallback()42 OnDemandLoadCallback::~OnDemandLoadCallback()
43 {
44     HDF_LOGI("%{public}s:deconstruct", __func__);
45 }
46 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)47 void OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
48 {
49     loading_ = false;
50     HDF_LOGI("%{public}s: OnLoadSystemAbilitySuccess systemAbilityId: %{public}d", __func__, systemAbilityId);
51 }
52 
OnLoadSystemAbilityFail(int32_t systemAbilityId)53 void OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
54 {
55     loading_ = false;
56     HDF_LOGI("%{public}s: OnLoadSystemAbilityFail systemAbilityId: %{public}d", __func__, systemAbilityId);
57 }
58 
UsbdLoadService(int32_t saId)59 UsbdLoadService::UsbdLoadService(int32_t saId) : saId_(saId)
60 {
61     HDF_LOGI("%{public}s:construct", __func__);
62 }
63 
~UsbdLoadService()64 UsbdLoadService::~UsbdLoadService()
65 {
66     HDF_LOGI("%{public}s:deconstruct", __func__);
67 }
LoadService()68 int32_t UsbdLoadService::LoadService()
69 {
70     if (loadCallback_ != nullptr && loadCallback_->loading_) {
71         HDF_LOGW("%{public}s:sa is loading", __func__);
72         return HDF_SUCCESS;
73     }
74 
75     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76     if (samgr == nullptr) {
77         HDF_LOGE("%{public}s:GetSystemAbilityManager failed", __func__);
78         return HDF_FAILURE;
79     }
80     auto saObj = samgr->CheckSystemAbility(saId_);
81     if (saObj != nullptr) {
82         return HDF_SUCCESS;
83     }
84 
85     if (loadCallback_ == nullptr) {
86         loadCallback_ = new (std::nothrow) OnDemandLoadCallback();
87         if (loadCallback_ == nullptr) {
88             HDF_LOGE("create OnDemandLoadCallback failed");
89             return HDF_DEV_ERR_NO_MEMORY;
90         }
91     }
92 
93     loadCallback_->loading_ = true;
94     int32_t result = samgr->LoadSystemAbility(saId_, loadCallback_);
95     if (result != ERR_OK) {
96         HDF_LOGE("LoadSystemAbility failed");
97         loadCallback_->loading_ = false;
98         return HDF_FAILURE;
99     }
100     return HDF_SUCCESS;
101 }
102 } // namespace V1_1
103 } // namespace Usb
104 } // namespace HDI
105 } // namespace OHOS
106