1 /*
2 * Copyright (C) 2024 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 #define MLOG_TAG "MtpManager"
16
17 #include "mtp_manager.h"
18 #include "media_log.h"
19 #include "mtp_file_observer.h"
20 #include "mtp_service.h"
21 #include "mtp_store_observer.h"
22 #include "mtp_medialibrary_manager.h"
23 #include "os_account_manager.h"
24 #include "parameter.h"
25 #include "parameters.h"
26 #include "usb_srv_client.h"
27 #include "usb_srv_support.h"
28
29 #include <thread>
30
31 namespace OHOS {
32 namespace Media {
33 namespace {
34 static std::mutex mutex_;
35 std::shared_ptr<MtpService> mtpServicePtr = nullptr;
36 std::atomic<bool> isMtpServiceRunning = false;
37 } // namespace
38 // LCOV_EXCL_START
GetInstance()39 MtpManager &MtpManager::GetInstance()
40 {
41 static MtpManager instance;
42 return instance;
43 }
44
GetMtpService()45 std::shared_ptr<MtpService> GetMtpService()
46 {
47 static std::once_flag oc;
48 std::call_once(oc, []() {
49 mtpServicePtr = std::make_shared<MtpService>();
50 });
51 return mtpServicePtr;
52 }
53
StartMtpService(const uint32_t mode)54 void StartMtpService(const uint32_t mode)
55 {
56 MEDIA_INFO_LOG("MtpManager::StartMtpService is called");
57 bool isForeground = true;
58 OHOS::ErrCode errCode = OHOS::AccountSA::OsAccountManager::IsOsAccountForeground(isForeground);
59 // not current user foreground, return
60 bool cond = (errCode == ERR_OK && !isForeground);
61 CHECK_AND_RETURN_LOG(!cond,
62 "StartMtpService errCode = %{public}d isForeground %{public}d", errCode, isForeground);
63 {
64 std::unique_lock lock(mutex_);
65 CHECK_AND_RETURN_INFO_LOG(!isMtpServiceRunning.load(),
66 "MtpManager::StartMtpService -- service is already running");
67 auto service = GetMtpService();
68 CHECK_AND_RETURN_LOG(service != nullptr, "MtpManager mtpServicePtr is nullptr");
69 if (MtpManager::GetInstance().mtpMode_ != MtpManager::MtpMode::NONE_MODE) {
70 MtpDfxReporter::GetInstance().NotifyDoDfXReporter(static_cast<int32_t>(MtpManager::GetInstance().mtpMode_));
71 service->StopService();
72 }
73 MtpManager::GetInstance().mtpMode_ = static_cast<MtpManager::MtpMode>(mode);
74 if (static_cast<MtpManager::MtpMode>(mode) == MtpManager::MtpMode::MTP_MODE) {
75 MtpFileObserver::GetInstance().StartFileInotify();
76 MtpStoreObserver::StartObserver();
77 }
78 service->StartService();
79 isMtpServiceRunning = true;
80 }
81 }
82
StopMtpService()83 void StopMtpService()
84 {
85 MEDIA_INFO_LOG("MtpManager::StopMtpService is called");
86 {
87 std::unique_lock lock(mutex_);
88 CHECK_AND_RETURN_INFO_LOG(isMtpServiceRunning.load(),
89 "MtpManager::StopMtpService -- service is already stopped");
90 auto service = GetMtpService();
91 CHECK_AND_RETURN_LOG(service != nullptr, "MtpManager mtpServicePtr is nullptr");
92 MtpDfxReporter::GetInstance().NotifyDoDfXReporter(static_cast<int32_t>(MtpManager::GetInstance().mtpMode_));
93 MtpManager::GetInstance().mtpMode_ = MtpManager::MtpMode::NONE_MODE;
94 service->StopService();
95 isMtpServiceRunning = false;
96 }
97 }
98 // LCOV_EXCL_STOP
99 } // namespace Media
100 } // namespace OHOS
101