• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23 #include "utils/disk_utils.h"
24 #include "volume/external_volume_info.h"
25 
26 namespace OHOS {
27 namespace StorageDaemon {
28 constexpr int32_t GET_CLIENT_RETRY_TIMES = 5;
29 constexpr int32_t SLEEP_TIME = 1;
GetClient()30 int32_t StorageManagerClient::GetClient()
31 {
32     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33     int32_t count = 0;
34 
35     while (storageManager_ == nullptr && count++ < GET_CLIENT_RETRY_TIMES) {
36         if (sam == nullptr) {
37             LOGE("get system ability manager error");
38             sleep(SLEEP_TIME);
39             continue;
40         }
41 
42         auto object = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
43         if (object == nullptr) {
44             LOGE("get storage manager object error");
45             sleep(SLEEP_TIME);
46             continue;
47         }
48 
49         storageManager_ = iface_cast<OHOS::StorageManager::IStorageManager>(object);
50         if (storageManager_ == nullptr) {
51             LOGE("iface_cast error");
52             sleep(SLEEP_TIME);
53             continue;
54         }
55     }
56 
57     return storageManager_ == nullptr ? E_SERVICE_IS_NULLPTR : E_OK;
58 }
59 
NotifyDiskCreated(DiskInfo & diskInfo)60 int32_t StorageManagerClient::NotifyDiskCreated(DiskInfo &diskInfo)
61 {
62     if (GetClient() != E_OK) {
63         return E_SERVICE_IS_NULLPTR;
64     }
65 
66     StorageManager::Disk disk(diskInfo.GetId(), diskInfo.GetDevDSize(),
67                               diskInfo.GetSysPath(), diskInfo.GetDevVendor(),
68                               diskInfo.GetDevFlag());
69     storageManager_->NotifyDiskCreated(disk);
70 
71     return E_OK;
72 }
73 
NotifyDiskDestroyed(std::string id)74 int32_t StorageManagerClient::NotifyDiskDestroyed(std::string id)
75 {
76     if (GetClient() != E_OK) {
77         return E_SERVICE_IS_NULLPTR;
78     }
79 
80     storageManager_->NotifyDiskDestroyed(id);
81 
82     return E_OK;
83 }
84 
NotifyVolumeCreated(std::shared_ptr<VolumeInfo> info)85 int32_t StorageManagerClient::NotifyVolumeCreated(std::shared_ptr<VolumeInfo> info)
86 {
87     if (GetClient() != E_OK) {
88         return E_SERVICE_IS_NULLPTR;
89     }
90     if (info == nullptr) {
91         return E_PARAMS_INVALID;
92     }
93 
94     StorageManager::VolumeCore vc(info->GetVolumeId(), info->GetVolumeType(),
95                                   info->GetDiskId(), info->GetState());
96     storageManager_->NotifyVolumeCreated(vc);
97 
98     return E_OK;
99 }
100 
NotifyVolumeMounted(std::shared_ptr<VolumeInfo> volumeInfo)101 int32_t StorageManagerClient::NotifyVolumeMounted(std::shared_ptr<VolumeInfo> volumeInfo)
102 {
103     if (GetClient() != E_OK) {
104         return E_SERVICE_IS_NULLPTR;
105     }
106     if (volumeInfo == nullptr) {
107         return E_PARAMS_INVALID;
108     }
109 
110     std::shared_ptr<ExternalVolumeInfo> info = std::static_pointer_cast<ExternalVolumeInfo>(volumeInfo);
111     storageManager_->NotifyVolumeMounted(info->GetVolumeId(), info->GetFsType(), info->GetFsUuid(),
112                                          info->GetMountPath(), info->GetFsLabel());
113 
114     return E_OK;
115 }
116 
NotifyVolumeStateChanged(std::string volId,StorageManager::VolumeState state)117 int32_t StorageManagerClient::NotifyVolumeStateChanged(std::string volId, StorageManager::VolumeState state)
118 
119 {
120     if (GetClient() != E_OK) {
121         return E_SERVICE_IS_NULLPTR;
122     }
123 
124     storageManager_->NotifyVolumeStateChanged(volId, state);
125 
126     return E_OK;
127 }
128 
NotifyVolumeDamaged(std::shared_ptr<VolumeInfo> volumeInfo)129 int32_t StorageManagerClient::NotifyVolumeDamaged(std::shared_ptr<VolumeInfo> volumeInfo)
130 {
131     if (GetClient() != E_OK) {
132         return E_SERVICE_IS_NULLPTR;
133     }
134     if (volumeInfo == nullptr) {
135         return E_PARAMS_INVALID;
136     }
137 
138     std::shared_ptr<ExternalVolumeInfo> info = std::static_pointer_cast<ExternalVolumeInfo>(volumeInfo);
139     storageManager_->NotifyVolumeDamaged(info->GetVolumeId(), info->GetFsType(), info->GetFsUuid(),
140                                          info->GetMountPath(), info->GetFsLabel());
141     return E_OK;
142 }
143 
NotifyMtpMounted(const std::string & id,const std::string & path,const std::string & desc,const std::string & uuid)144 int32_t StorageManagerClient::NotifyMtpMounted(const std::string &id, const std::string &path, const std::string &desc,
145                                                const std::string &uuid)
146 {
147     LOGI("NotifyMtpMounted: id = %{public}s, path = %{public}s, desc = %{public}s, uuid = %{public}s", id.c_str(),
148         path.c_str(), desc.c_str(), GetAnonyString(uuid).c_str());
149     if (GetClient() != E_OK) {
150         return E_SERVICE_IS_NULLPTR;
151     }
152     if (storageManager_ != nullptr) {
153         storageManager_->NotifyMtpMounted(id, path, desc, uuid);
154     }
155     return E_OK;
156 }
157 
NotifyMtpUnmounted(const std::string & id,const std::string & path,const bool isBadRemove)158 int32_t StorageManagerClient::NotifyMtpUnmounted(const std::string &id, const std::string &path, const bool isBadRemove)
159 {
160     LOGI("NotifyMtpUnmounted: id = %{public}s, path = %{public}s", id.c_str(), path.c_str());
161     if (GetClient() != E_OK) {
162         return E_SERVICE_IS_NULLPTR;
163     }
164     if (storageManager_ != nullptr) {
165         storageManager_->NotifyMtpUnmounted(id, path, isBadRemove);
166     }
167     return E_OK;
168 }
169 } // StorageDaemon
170 } // OHOS
171