1 /*
2 * Copyright (C) 2022 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 #include "mtp_event.h"
16 #include <numeric>
17 #include <unistd.h>
18 #include "media_log.h"
19 #include "media_mtp_utils.h"
20 #include "mtp_packet.h"
21 #include "mtp_packet_tools.h"
22 using namespace std;
23 namespace OHOS {
24 namespace Media {
MtpEvent(const std::shared_ptr<MtpOperationContext> & context)25 MtpEvent::MtpEvent(const std::shared_ptr<MtpOperationContext> &context)
26 {
27 if (context != nullptr) {
28 mtpContextPtr_ = context;
29 }
30 }
31
~MtpEvent()32 MtpEvent::~MtpEvent()
33 {
34 }
35
SendObjectAdded(const std::string & path)36 void MtpEvent::SendObjectAdded(const std::string &path)
37 {
38 handleptr_ = make_shared<MtpOperationUtils>(mtpContextPtr_);
39 uint32_t handle{0};
40 int i{0};
41 while (i < MTP_SEND_ADD_TIMES) {
42 if (handleptr_->GetHandleByPaths(path, handle) == E_SUCCESS) {
43 mtpContextPtr_->eventHandle = handle;
44 SendEvent(MTP_EVENT_OBJECT_ADDED_CODE);
45 return;
46 }
47 i++;
48 usleep(MTP_SEND_ADD);
49 MEDIA_DEBUG_LOG("MtpEvent::sendObjectAdded try %{public}d times", i);
50 }
51 }
52
SendObjectRemoved(const std::string & path)53 void MtpEvent::SendObjectRemoved(const std::string &path)
54 {
55 handleptr_ = make_shared<MtpOperationUtils>(mtpContextPtr_);
56 uint32_t handle{0};
57 int i{0};
58 while (i < MTP_SEND_ADD_TIMES) {
59 if (handleptr_->GetHandleByPaths(path, handle) == E_SUCCESS) {
60 mtpContextPtr_->eventHandle = handle;
61 SendEvent(MTP_EVENT_OBJECT_REMOVED_CODE);
62 return;
63 }
64 i++;
65 usleep(MTP_SEND_ADD);
66 MEDIA_DEBUG_LOG("MtpEvent::sendObjectRemoved try %{public}d times", i);
67 }
68 }
69
SendObjectInfoChanged(const std::string & path)70 void MtpEvent::SendObjectInfoChanged(const std::string &path)
71 {
72 handleptr_ = make_shared<MtpOperationUtils>(mtpContextPtr_);
73 uint32_t handle{0};
74 int i{0};
75 while (i < MTP_SEND_ADD_TIMES) {
76 if (handleptr_->GetHandleByPaths(path, handle) == E_SUCCESS) {
77 mtpContextPtr_->eventHandle = handle;
78 SendEvent(MTP_EVENT_OBJECT_INFO_CHANGED_CODE);
79 return;
80 }
81 i++;
82 usleep(MTP_SEND_ADD);
83 MEDIA_DEBUG_LOG("MtpEvent::sendObjectInfoChanged try %{public}d times", i);
84 }
85 }
86
SendDevicePropertyChanged()87 void MtpEvent::SendDevicePropertyChanged()
88 {
89 mtpContextPtr_->eventProperty = MTP_DEVICE_PROPERTY_BATTERY_LEVEL_CODE;
90 SendEvent(MTP_EVENT_DEVICE_PROP_CHANGED_CODE);
91 }
92
SendEvent(const int32_t & code)93 void MtpEvent::SendEvent(const int32_t &code)
94 {
95 shared_ptr<PayloadData> eventPayloadData;
96
97 uint16_t responseCode = EventPayloadData(code, eventPayloadData);
98 if (responseCode == MTP_UNDEFINED_CODE) {
99 MEDIA_DEBUG_LOG("Mtp Event GetPayloadData Error");
100 }
101 shared_ptr<HeaderData> eventHeaderData =
102 make_shared<HeaderData>(EVENT_CONTAINER_TYPE, code, HeaderData::sTransactionID_);
103 shared_ptr<MtpPacket> eventPacketPtr = std::make_shared<MtpPacket>(mtpContextPtr_, mtpContextPtr_->mtpDriver);
104 eventPacketPtr->Init(eventHeaderData, eventPayloadData);
105 int errorCode = eventPacketPtr->Maker(true);
106 if (errorCode != MTP_SUCCESS) {
107 MEDIA_ERR_LOG("MtpEvent::SendEvent responsePacket Maker err: %{public}d", errorCode);
108 return;
109 }
110 errorCode = eventPacketPtr->Write();
111 if (errorCode != MTP_SUCCESS) {
112 MEDIA_ERR_LOG("MtpEvent::SendEvent responsePacket Write err: %{public}d", errorCode);
113 return;
114 }
115 }
116
EventPayloadData(const uint16_t code,shared_ptr<PayloadData> & data)117 uint16_t MtpEvent::EventPayloadData(const uint16_t code, shared_ptr<PayloadData> &data)
118 {
119 uint16_t responseCode = MTP_UNDEFINED_CODE;
120 if (handleptr_ == nullptr) {
121 handleptr_ = make_shared<MtpOperationUtils>(mtpContextPtr_);
122 }
123 switch (code) {
124 case MTP_EVENT_OBJECT_ADDED_CODE:
125 case MTP_EVENT_OBJECT_REMOVED_CODE:
126 case MTP_EVENT_OBJECT_INFO_CHANGED_CODE:
127 responseCode = handleptr_->ObjectEvent(data, mtpContextPtr_->eventHandle);
128 break;
129 case MTP_EVENT_DEVICE_PROP_CHANGED_CODE:
130 responseCode = handleptr_->ObjectEvent(data, mtpContextPtr_->eventProperty);
131 break;
132 default:
133 break;
134 }
135 return responseCode;
136 }
137 } // namespace Media
138 } // namespace OHOS