1 /*
2  * Copyright (c) 2021 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 "ipc/storage_manager_client.h"
17 
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 #include <unistd.h>
21 
22 #include "disk.h"
23 #include "storage_service_errno.h"
24 #include "storage_service_log.h"
25 #include "volume/external_volume_info.h"
26 
27 namespace OHOS {
28 namespace StorageDaemon {
29 static constexpr int32_t GET_CLIENT_RETRY_TIMES = 5;
30 static constexpr int32_t SLEEP_TIME = 1;
GetClient()31 int32_t StorageManagerClient::GetClient()
32 {
33     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34     int32_t count = 0;
35 
36     while (storageManager_ == nullptr && count++ < GET_CLIENT_RETRY_TIMES) {
37         if (sam == nullptr) {
38             LOGE("get system ability manager error");
39             sleep(SLEEP_TIME);
40             continue;
41         }
42 
43         auto object = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
44         if (object == nullptr) {
45             LOGE("get storage manager object error");
46             sleep(SLEEP_TIME);
47             continue;
48         }
49 
50         storageManager_ = iface_cast<OHOS::StorageManager::IStorageManager>(object);
51         if (storageManager_ == nullptr) {
52             LOGE("iface_cast error");
53             sleep(SLEEP_TIME);
54             continue;
55         }
56     }
57 
58     return storageManager_ == nullptr ? E_SERVICE_IS_NULLPTR : E_OK;
59 }
60 
NotifyDiskCreated(DiskInfo & diskInfo)61 int32_t StorageManagerClient::NotifyDiskCreated(DiskInfo &diskInfo)
62 {
63     if (GetClient() != E_OK) {
64         return E_SERVICE_IS_NULLPTR;
65     }
66 
67     StorageManager::Disk disk(diskInfo.GetId(), diskInfo.GetDevDSize(),
68                               diskInfo.GetSysPath(), diskInfo.GetDevVendor(),
69                               diskInfo.GetDevFlag());
70     storageManager_->NotifyDiskCreated(disk);
71 
72     return E_OK;
73 }
74 
NotifyDiskDestroyed(std::string id)75 int32_t StorageManagerClient::NotifyDiskDestroyed(std::string id)
76 {
77     if (GetClient() != E_OK) {
78         return E_SERVICE_IS_NULLPTR;
79     }
80 
81     storageManager_->NotifyDiskDestroyed(id);
82 
83     return E_OK;
84 }
85 
NotifyVolumeCreated(std::shared_ptr<VolumeInfo> info)86 int32_t StorageManagerClient::NotifyVolumeCreated(std::shared_ptr<VolumeInfo> info)
87 {
88     if (GetClient() != E_OK) {
89         return E_SERVICE_IS_NULLPTR;
90     }
91     if (info == nullptr) {
92         return E_PARAMS_INVAL;
93     }
94 
95     StorageManager::VolumeCore vc(info->GetVolumeId(), info->GetVolumeType(),
96                                   info->GetDiskId(), info->GetState());
97     storageManager_->NotifyVolumeCreated(vc);
98 
99     return E_OK;
100 }
101 
NotifyVolumeMounted(std::shared_ptr<VolumeInfo> volumeInfo)102 int32_t StorageManagerClient::NotifyVolumeMounted(std::shared_ptr<VolumeInfo> volumeInfo)
103 {
104     if (GetClient() != E_OK) {
105         return E_SERVICE_IS_NULLPTR;
106     }
107     if (volumeInfo == nullptr) {
108         return E_PARAMS_INVAL;
109     }
110 
111     std::shared_ptr<ExternalVolumeInfo> info = std::static_pointer_cast<ExternalVolumeInfo>(volumeInfo);
112     storageManager_->NotifyVolumeMounted(info->GetVolumeId(), info->GetFsType(), info->GetFsUuid(),
113                                          info->GetMountPath(), info->GetFsLabel());
114 
115     return E_OK;
116 }
117 
NotifyVolumeStateChanged(std::string volId,StorageManager::VolumeState state)118 int32_t StorageManagerClient::NotifyVolumeStateChanged(std::string volId, StorageManager::VolumeState state)
119 
120 {
121     if (GetClient() != E_OK) {
122         return E_SERVICE_IS_NULLPTR;
123     }
124 
125     storageManager_->NotifyVolumeStateChanged(volId, state);
126 
127     return E_OK;
128 }
129 } // StorageDaemon
130 } // OHOS
131