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