• 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 
16 #include "mtp/usb_event_subscriber.h"
17 
18 namespace OHOS {
19 namespace StorageDaemon {
20 constexpr const char *BUS_NUM_KEY = "busNum";
21 constexpr const char *DEV_ADDRESS_KEY = "devAddress";
22 
UsbEventSubscriber(const EventFwk::CommonEventSubscribeInfo & info)23 UsbEventSubscriber::UsbEventSubscriber(const EventFwk::CommonEventSubscribeInfo &info)
24     : EventFwk::CommonEventSubscriber(info)
25 {}
26 
27 std::shared_ptr<UsbEventSubscriber> usbEventSubscriber_ = nullptr;
SubscribeCommonEvent(void)28 void UsbEventSubscriber::SubscribeCommonEvent(void)
29 {
30     if (usbEventSubscriber_ == nullptr) {
31         EventFwk::MatchingSkills matchingSkills;
32         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_STATE);
33         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
34         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
35         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
36         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_ACCESSORY_ATTACHED);
37         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USB_ACCESSORY_DETACHED);
38         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
39         usbEventSubscriber_ = std::make_shared<UsbEventSubscriber>(subscribeInfo);
40         if (!EventFwk::CommonEventManager::SubscribeCommonEvent(usbEventSubscriber_)) {
41             usbEventSubscriber_ = nullptr;
42             LOGE("UsbEventSubscriber subscribe common event failed.");
43         }
44     }
45 }
46 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)47 void UsbEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
48 {
49     LOGI("UsbEventSubscriber::OnReceiveEvent.");
50     auto want = data.GetWant();
51     std::string action = want.GetAction();
52     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_STATE) {
53         LOGI("OnReceiveEvent COMMON_EVENT_USB_STATE, data=%{public}s", data.GetData().c_str());
54     }
55     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED) {
56         LOGI("OnReceiveEvent COMMON_EVENT_USB_PORT_CHANGED, data=%{public}s", data.GetData().c_str());
57     }
58     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_ACCESSORY_ATTACHED) {
59         LOGI("OnReceiveEvent COMMON_EVENT_USB_ACCESSORY_ATTACHED, data=%{public}s", data.GetData().c_str());
60     }
61     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_ACCESSORY_DETACHED) {
62         LOGI("OnReceiveEvent COMMON_EVENT_USB_ACCESSORY_DETACHED, data=%{public}s", data.GetData().c_str());
63     }
64     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED) {
65         LOGI("OnReceiveEvent COMMON_EVENT_USB_DEVICE_ATTACHED, data=%{public}s", data.GetData().c_str());
66 #ifdef SUPPORT_OPEN_SOURCE_MTP_DEVICE
67         DelayedSingleton<MtpDeviceMonitor>::GetInstance()->MountMtpDeviceByBroadcast();
68 #endif
69     }
70     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED) {
71         LOGI("OnReceiveEvent COMMON_EVENT_USB_DEVICE_DETACHED, data=%{public}s", data.GetData().c_str());
72 #ifdef SUPPORT_OPEN_SOURCE_MTP_DEVICE
73         uint8_t devNum = 0;
74         uint32_t busLoc = 0;
75         GetValueFromUsbDataInfo(data.GetData(), devNum, busLoc);
76         DelayedSingleton<MtpDeviceMonitor>::GetInstance()->UmountDetachedMtpDevice(devNum, busLoc);
77 #endif
78     }
79 }
80 
GetValueFromUsbDataInfo(const std::string & jsonStr,uint8_t & devNum,uint32_t & busLoc)81 void UsbEventSubscriber::GetValueFromUsbDataInfo(const std::string &jsonStr, uint8_t &devNum, uint32_t &busLoc)
82 {
83     cJSON *usbJson = cJSON_Parse(jsonStr.c_str());
84     if (usbJson == nullptr) {
85         LOGE("GetValueFromUsbDataInfo failed, parse json object is nullptr.");
86         return;
87     }
88     cJSON *busLocObj = cJSON_GetObjectItemCaseSensitive(usbJson, DEV_ADDRESS_KEY);
89     if (busLocObj != nullptr && cJSON_IsNumber(busLocObj)) {
90         devNum = static_cast<uint8_t>(busLocObj->valueint);
91     }
92     cJSON *devNumObj = cJSON_GetObjectItemCaseSensitive(usbJson, BUS_NUM_KEY);
93     if (devNumObj != nullptr && cJSON_IsNumber(devNumObj)) {
94         busLoc = static_cast<uint32_t>(devNumObj->valueint);
95     }
96     cJSON_Delete(usbJson);
97 }
98 }  // namespace StorageDaemon
99 }  // namespace OHOS