• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_subscriber.h"
23 #include "mtp_medialibrary_manager.h"
24 #include "os_account_manager.h"
25 #include "parameter.h"
26 #include "parameters.h"
27 #include "usb_srv_client.h"
28 #include "usb_srv_support.h"
29 
30 #include <thread>
31 
32 namespace OHOS {
33 namespace Media {
34 namespace {
35     static std::mutex mutex_;
36     std::shared_ptr<MtpService> mtpServicePtr = nullptr;
37     std::atomic<bool> isMtpServiceRunning = false;
38     const std::string KEY_CUST = "const.cust.custPath";
39     const std::string CUST_DEFAULT = "phone";
40     const std::string CUST_TOBBASIC = "tobbasic";
41     const std::string CUST_HWIT = "hwit";
42     const char *MTP_SERVER_DISABLE = "persist.edm.mtp_server_disable";
43 } // namespace
44 
GetInstance()45 MtpManager &MtpManager::GetInstance()
46 {
47     static MtpManager instance;
48     return instance;
49 }
50 
GetMtpService()51 std::shared_ptr<MtpService> GetMtpService()
52 {
53     static std::once_flag oc;
54     std::call_once(oc, []() {
55         mtpServicePtr = std::make_shared<MtpService>();
56     });
57     return mtpServicePtr;
58 }
59 
Init()60 void MtpManager::Init()
61 {
62     std::thread([]() {
63         MEDIA_INFO_LOG("MtpManager Init");
64         // IT管控 PC - tobasic/hwit 不启动MTP服务 start
65         std::string cust = OHOS::system::GetParameter(KEY_CUST, CUST_DEFAULT);
66         bool cond = (cust.find(CUST_TOBBASIC) != std::string::npos || cust.find(CUST_HWIT) != std::string::npos);
67         CHECK_AND_RETURN_INFO_LOG(!cond, "MtpManager Init Return cust = [%{public}s]", cust.c_str());
68         // IT管控 PC - tobasic/hwit 不启动MTP服务 end
69         bool result = MtpSubscriber::Subscribe();
70         MEDIA_INFO_LOG("MtpManager Subscribe result = %{public}d", result);
71         // param 监听注册
72         MtpManager::GetInstance().RegisterMtpParamListener();
73 
74         int32_t funcs = 0;
75         int ret = OHOS::USB::UsbSrvClient::GetInstance().GetCurrentFunctions(funcs);
76         MEDIA_INFO_LOG("MtpManager Init GetCurrentFunctions = %{public}d ret = %{public}d", funcs, ret);
77         CHECK_AND_RETURN_LOG(ret == 0, "GetCurrentFunctions failed");
78         uint32_t unsignedfuncs = static_cast<uint32_t>(funcs);
79         if (unsignedfuncs & USB::UsbSrvSupport::Function::FUNCTION_MTP) {
80             std::string param(MTP_SERVER_DISABLE);
81             bool mtpDisable = system::GetBoolParameter(param, false);
82             if (mtpDisable) {
83                 MEDIA_INFO_LOG("MtpManager Init MTP Manager persist.edm.mtp_server_disable = true");
84             } else {
85                 MEDIA_INFO_LOG("MtpManager Init USB MTP connected");
86                 MtpManager::GetInstance().StartMtpService(MtpMode::MTP_MODE);
87             }
88             return;
89         }
90         if (unsignedfuncs & USB::UsbSrvSupport::Function::FUNCTION_PTP) {
91             MtpManager::GetInstance().StartMtpService(MtpMode::PTP_MODE);
92             return;
93         }
94         if (unsignedfuncs & USB::UsbSrvSupport::Function::FUNCTION_HDC) {
95             MtpManager::GetInstance().StopMtpService();
96         }
97         MEDIA_INFO_LOG("MtpManager Init success end");
98     }).detach();
99     MtpManager::GetInstance().RemoveMtpParamListener();
100 }
101 
StartMtpService(const MtpMode mode)102 void MtpManager::StartMtpService(const MtpMode mode)
103 {
104     MEDIA_INFO_LOG("MtpManager::StartMtpService is called");
105     bool isForeground = true;
106     OHOS::ErrCode errCode = OHOS::AccountSA::OsAccountManager::IsOsAccountForeground(isForeground);
107     // not current user foreground, return
108     bool cond = (errCode == ERR_OK && !isForeground);
109     CHECK_AND_RETURN_LOG(!cond,
110         "StartMtpService errCode = %{public}d isForeground %{public}d", errCode, isForeground);
111     {
112         std::unique_lock lock(mutex_);
113         CHECK_AND_RETURN_INFO_LOG(!isMtpServiceRunning.load(),
114             "MtpManager::StartMtpService -- service is already running");
115         auto service = GetMtpService();
116         CHECK_AND_RETURN_LOG(service != nullptr, "MtpManager mtpServicePtr is nullptr");
117         if (mtpMode_ != MtpMode::NONE_MODE) {
118             service->StopService();
119         }
120         mtpMode_ = mode;
121         if (mode == MtpMode::MTP_MODE) {
122             MtpFileObserver::GetInstance().StartFileInotify();
123             MtpStoreObserver::StartObserver();
124         }
125         service->StartService();
126         isMtpServiceRunning = true;
127     }
128 }
129 
StopMtpService()130 void MtpManager::StopMtpService()
131 {
132     MEDIA_INFO_LOG("MtpManager::StopMtpService is called");
133     {
134         std::unique_lock lock(mutex_);
135         CHECK_AND_RETURN_INFO_LOG(isMtpServiceRunning.load(),
136             "MtpManager::StopMtpService -- service is already stopped");
137         auto service = GetMtpService();
138         CHECK_AND_RETURN_LOG(service != nullptr, "MtpManager mtpServicePtr is nullptr");
139         mtpMode_ = MtpMode::NONE_MODE;
140         service->StopService();
141         isMtpServiceRunning = false;
142     }
143 }
144 
RegisterMtpParamListener()145 void MtpManager::RegisterMtpParamListener()
146 {
147     MEDIA_INFO_LOG("RegisterMTPParamListener");
148     WatchParameter(MTP_SERVER_DISABLE, OnMtpParamDisableChanged, this);
149 }
150 
RemoveMtpParamListener()151 void MtpManager::RemoveMtpParamListener()
152 {
153     MEDIA_INFO_LOG("RemoveMtpParamListener");
154     RemoveParameterWatcher(MTP_SERVER_DISABLE, OnMtpParamDisableChanged, this);
155 }
156 
OnMtpParamDisableChanged(const char * key,const char * value,void * context)157 void MtpManager::OnMtpParamDisableChanged(const char *key, const char *value, void *context)
158 {
159     bool cond = (key == nullptr || value == nullptr);
160     CHECK_AND_RETURN_LOG(!cond, "OnMtpParamDisableChanged return invalid value");
161     MEDIA_INFO_LOG("OnMTPParamDisable, key = %{public}s, value = %{public}s", key, value);
162     CHECK_AND_RETURN_INFO_LOG(strcmp(key, MTP_SERVER_DISABLE) == 0, "event key mismatch");
163     MtpManager *instance = reinterpret_cast<MtpManager*>(context);
164     std::string param(MTP_SERVER_DISABLE);
165     bool mtpDisable = system::GetBoolParameter(param, false);
166     if (!mtpDisable) {
167         int32_t funcs = 0;
168         int ret = OHOS::USB::UsbSrvClient::GetInstance().GetCurrentFunctions(funcs);
169         MEDIA_INFO_LOG("OnMtpparamDisableChanged GetCurrentFunction = %{public}d ret = %{public}d", funcs, ret);
170         CHECK_AND_RETURN_INFO_LOG(ret != 0, "OnMtpparamDisableChanged GetCurrentFunction failed");
171         uint32_t unsignedFuncs = static_cast<uint32_t>(funcs);
172         if (unsignedFuncs && USB::UsbSrvSupport::Function::FUNCTION_MTP) {
173             instance->StartMtpService(MtpMode::MTP_MODE);
174             return;
175         }
176     } else {
177         MEDIA_INFO_LOG("MTP Manager not init");
178         instance->StopMtpService();
179     }
180 }
181 } // namespace Media
182 } // namespace OHOS
183