• 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 #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 
92     StorageManager::VolumeCore vc(info->GetVolumeId(), info->GetVolumeType(),
93                                   info->GetDiskId(), info->GetState());
94     storageManager_->NotifyVolumeCreated(vc);
95 
96     return E_OK;
97 }
98 
NotifyVolumeMounted(std::shared_ptr<VolumeInfo> volumeInfo)99 int32_t StorageManagerClient::NotifyVolumeMounted(std::shared_ptr<VolumeInfo> volumeInfo)
100 {
101     if (GetClient() != E_OK) {
102         return E_SERVICE_IS_NULLPTR;
103     }
104 
105     std::shared_ptr<ExternalVolumeInfo> info = std::static_pointer_cast<ExternalVolumeInfo>(volumeInfo);
106     storageManager_->NotifyVolumeMounted(info->GetVolumeId(), info->GetFsType(), info->GetFsUuid(),
107                                          info->GetMountPath(), info->GetFsLabel());
108 
109     return E_OK;
110 }
111 
NotifyVolumeDestroyed(std::string volId)112 int32_t StorageManagerClient::NotifyVolumeDestroyed(std::string volId)
113 
114 {
115     if (GetClient() != E_OK) {
116         return E_SERVICE_IS_NULLPTR;
117     }
118 
119     storageManager_->NotifyVolumeDestroyed(volId);
120 
121     return E_OK;
122 }
123 } // StorageDaemon
124 } // OHOS
125