1 /*
2 * Copyright (c) 2025 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 "MediaMtpServicemanager"
16
17 #include <thread>
18
19 #include "media_mtp_manager.h"
20 #include "media_log.h"
21 #include "media_mtp_service_manager.h"
22 #include "media_mtp_subscriber.h"
23 #include "parameter.h"
24 #include "parameters.h"
25 #include "usb_srv_client.h"
26 #include "usb_srv_support.h"
27
28 #define USB_FUNTION_HDC (1 << 2)
29 #define USB_FUNTION_MTP (1 << 3)
30 #define USB_FUNTION_PTP (1 << 4)
31 #define USB_FUNTION_STORAGE (1 << 9)
32
33 namespace OHOS {
34 namespace Media {
35 namespace {
36 std::atomic<bool> isMtpServiceRunning = false;
37 const std::string KEY_CUST = "const.cust.custPath";
38 const std::string CUST_DEFAULT = "phone";
39 const std::string CUST_TOBBASIC = "tobbasic";
40 const std::string CUST_HWIT = "hwit";
41 const char *MTP_SERVER_DISABLE = "persist.edm.mtp_server_disable";
42 }
43
GetInstance()44 MediaMtpManager &MediaMtpManager::GetInstance()
45 {
46 static MediaMtpManager instance;
47 return instance;
48 }
49
Init()50 void MediaMtpManager::Init()
51 {
52 std::thread([]() {
53 MEDIA_INFO_LOG("MediaMtpManager Init");
54 // IT管控 PC - tobasic/hwit 不启动MTP服务 start
55 std::string cust = OHOS::system::GetParameter(KEY_CUST, CUST_DEFAULT);
56 bool cond = (cust.find(CUST_TOBBASIC) != std::string::npos || cust.find(CUST_HWIT) != std::string::npos);
57 CHECK_AND_RETURN_INFO_LOG(!cond, "MediaMtpManager Init Return cust = [%{public}s]", cust.c_str());
58 // IT管控 PC - tobasic/hwit 不启动MTP服务 end
59 bool result = MediaMtpSubscriber::Subscribe();
60 MEDIA_INFO_LOG("MediaMtpManager Subscribe result = %{public}d", result);
61 // param 监听注册
62 MediaMtpManager::GetInstance().RegisterMtpParamListener();
63
64 int32_t funcs = 0;
65 int ret = OHOS::USB::UsbSrvClient::GetInstance().GetCurrentFunctions(funcs);
66 MEDIA_INFO_LOG("MediaMtpManager Init GetCurrentFunctions = %{public}d ret = %{public}d", funcs, ret);
67 CHECK_AND_RETURN_LOG(ret == 0, "GetCurrentFunctions failed");
68 uint32_t unsignedfuncs = static_cast<uint32_t>(funcs);
69 if (unsignedfuncs & USB::UsbSrvSupport::Function::FUNCTION_MTP) {
70 std::string param(MTP_SERVER_DISABLE);
71 bool mtpDisable = system::GetBoolParameter(param, false);
72 if (mtpDisable) {
73 MEDIA_INFO_LOG("MediaMtpManager Init MTP Manager persist.edm.mtp_server_disable = true");
74 } else {
75 MEDIA_INFO_LOG("MediaMtpManager Init USB MTP connected");
76 MediaMtpServiceManager::StartMtpService(MtpMode::MTP_MODE);
77 }
78 return;
79 }
80 if (unsignedfuncs & USB::UsbSrvSupport::Function::FUNCTION_PTP) {
81 MediaMtpServiceManager::StartMtpService(MtpMode::PTP_MODE);
82 return;
83 }
84 if (unsignedfuncs & USB::UsbSrvSupport::Function::FUNCTION_HDC) {
85 MediaMtpServiceManager::StopMtpService();
86 }
87 MEDIA_INFO_LOG("MediaMtpManager Init success end");
88 }).detach();
89 MediaMtpManager::GetInstance().RemoveMtpParamListener();
90 }
91
RegisterMtpParamListener()92 void MediaMtpManager::RegisterMtpParamListener()
93 {
94 MEDIA_INFO_LOG("RegisterMTPParamListener");
95 WatchParameter(MTP_SERVER_DISABLE, OnMtpParamDisableChanged, this);
96 }
97
RemoveMtpParamListener()98 void MediaMtpManager::RemoveMtpParamListener()
99 {
100 MEDIA_INFO_LOG("RemoveMtpParamListener");
101 RemoveParameterWatcher(MTP_SERVER_DISABLE, OnMtpParamDisableChanged, this);
102 }
103
OnMtpParamDisableChanged(const char * key,const char * value,void * context)104 void MediaMtpManager::OnMtpParamDisableChanged(const char *key, const char *value, void *context)
105 {
106 bool cond = (key == nullptr || value == nullptr);
107 CHECK_AND_RETURN_LOG(!cond, "OnMtpParamDisableChanged return invalid value");
108 MEDIA_INFO_LOG("OnMTPParamDisable, key = %{public}s, value = %{public}s", key, value);
109 CHECK_AND_RETURN_INFO_LOG(strcmp(key, MTP_SERVER_DISABLE) == 0, "event key mismatch");
110 std::string param(MTP_SERVER_DISABLE);
111 bool mtpDisable = system::GetBoolParameter(param, false);
112 if (!mtpDisable) {
113 int32_t funcs = 0;
114 int ret = OHOS::USB::UsbSrvClient::GetInstance().GetCurrentFunctions(funcs);
115 MEDIA_INFO_LOG("OnMtpparamDisableChanged GetCurrentFunction = %{public}d ret = %{public}d", funcs, ret);
116 CHECK_AND_RETURN_INFO_LOG(ret != 0, "OnMtpparamDisableChanged GetCurrentFunction failed");
117 uint32_t unsignedFuncs = static_cast<uint32_t>(funcs);
118 if (unsignedFuncs && USB::UsbSrvSupport::Function::FUNCTION_MTP) {
119 MediaMtpServiceManager::StartMtpService(MtpMode::MTP_MODE);
120 return;
121 }
122 } else {
123 MEDIA_INFO_LOG("MTP Manager not init");
124 int32_t currentFunctions_ = USB_FUNTION_STORAGE;
125 int ret = OHOS::USB::UsbSrvClient::GetInstance().GetCurrentFunctions(currentFunctions_);
126 if (ret == 0) {
127 currentFunctions_ = static_cast<uint32_t>(currentFunctions_) & (~USB_FUNTION_MTP) & (~USB_FUNTION_PTP);
128 currentFunctions_ = currentFunctions_ == 0 ? USB_FUNTION_STORAGE : currentFunctions_;
129 MEDIA_INFO_LOG("start to execute disconnect task");
130 // 调用内核接口,将MTP或PTP端口切换为HDC接口
131 OHOS::USB::UsbSrvClient::GetInstance().SetCurrentFunctions(currentFunctions_);
132 }
133 MediaMtpServiceManager::StopMtpService();
134 }
135 }
136 } // namespace Media
137 } // namespace OHOS