• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "mmi_service.h"
17 
18 #include <cinttypes>
19 #include <csignal>
20 #include <parameters.h>
21 #include <sys/signalfd.h>
22 #include "dfx_hisysevent.h"
23 #ifdef OHOS_RSS_CLIENT
24 #include <unordered_map>
25 #endif
26 
27 #include "anr_manager.h"
28 #ifdef OHOS_BUILD_ENABLE_COOPERATE
29 #include "cooperate_event_manager.h"
30 #endif // OHOS_BUILD_ENABLE_COOPERATE
31 #include "event_dump.h"
32 #ifdef OHOS_BUILD_ENABLE_COOPERATE
33 #include "input_device_cooperate_sm.h"
34 #endif // OHOS_BUILD_ENABLE_COOPERATE
35 #include "input_device_manager.h"
36 #include "input_windows_manager.h"
37 #include "i_pointer_drawing_manager.h"
38 #include "key_map_manager.h"
39 #include "mmi_log.h"
40 #include "string_ex.h"
41 #include "util_ex.h"
42 #include "util_napi_error.h"
43 #include "multimodal_input_connect_def_parcel.h"
44 #ifdef OHOS_RSS_CLIENT
45 #include "res_sched_client.h"
46 #include "res_type.h"
47 #include "system_ability_definition.h"
48 #endif
49 #include "permission_helper.h"
50 #include "timer_manager.h"
51 #include "input_device_manager.h"
52 #include "util.h"
53 
54 namespace OHOS {
55 namespace MMI {
56 namespace {
57 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MMIService" };
58 const std::string DEF_INPUT_SEAT = "seat0";
59 } // namespace
60 
61 const bool REGISTER_RESULT =
62     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<MMIService>::GetInstance().get());
63 
64 struct mmi_epoll_event {
65     int32_t fd { 0 };
66     EpollEventType event_type { EPOLL_EVENT_BEGIN };
67 };
68 
69 template<class ...Ts>
CheckDefineOutput(const char * fmt,Ts...args)70 void CheckDefineOutput(const char* fmt, Ts... args)
71 {
72     using namespace OHOS::MMI;
73     CHKPV(fmt);
74     char buf[MAX_PACKET_BUF_SIZE] = {};
75     int32_t ret = snprintf_s(buf, MAX_PACKET_BUF_SIZE, MAX_PACKET_BUF_SIZE - 1, fmt, args...);
76     if (ret == -1) {
77         KMSG_LOGI("Call snprintf_s failed.ret = %d", ret);
78         return;
79     }
80     KMSG_LOGI("%s", buf);
81     MMI_HILOGI("%{public}s", buf);
82 }
83 
CheckDefine()84 static void CheckDefine()
85 {
86     CheckDefineOutput("ChkDefs:");
87 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
88     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER_DRAWING");
89 #endif
90 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
91     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_INTERCEPTOR");
92 #endif
93 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
94     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_KEYBOARD");
95 #endif
96 #ifdef OHOS_BUILD_ENABLE_POINTER
97     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_POINTER");
98 #endif
99 #ifdef OHOS_BUILD_ENABLE_TOUCH
100     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_TOUCH");
101 #endif
102 #ifdef OHOS_BUILD_ENABLE_MONITOR
103     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_MONITOR");
104 #endif
105 #ifdef OHOS_BUILD_ENABLE_COOPERATE
106     CheckDefineOutput("%-40s", "OHOS_BUILD_ENABLE_COOPERATE");
107 #endif
108 }
109 
MMIService()110 MMIService::MMIService() : SystemAbility(MULTIMODAL_INPUT_CONNECT_SERVICE_ID, true) {}
111 
~MMIService()112 MMIService::~MMIService() {}
113 
AddEpoll(EpollEventType type,int32_t fd)114 int32_t MMIService::AddEpoll(EpollEventType type, int32_t fd)
115 {
116     if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
117         MMI_HILOGE("Invalid param type");
118         return RET_ERR;
119     }
120     if (fd < 0) {
121         MMI_HILOGE("Invalid param fd_");
122         return RET_ERR;
123     }
124     if (mmiFd_ < 0) {
125         MMI_HILOGE("Invalid param mmiFd_");
126         return RET_ERR;
127     }
128     auto eventData = static_cast<mmi_epoll_event*>(malloc(sizeof(mmi_epoll_event)));
129     if (!eventData) {
130         MMI_HILOGE("Malloc failed");
131         return RET_ERR;
132     }
133     eventData->fd = fd;
134     eventData->event_type = type;
135     MMI_HILOGI("userdata:[fd:%{public}d,type:%{public}d]", eventData->fd, eventData->event_type);
136 
137     struct epoll_event ev = {};
138     ev.events = EPOLLIN;
139     ev.data.ptr = eventData;
140     auto ret = EpollCtl(fd, EPOLL_CTL_ADD, ev, mmiFd_);
141     if (ret < 0) {
142         free(eventData);
143         eventData = nullptr;
144         ev.data.ptr = nullptr;
145         return ret;
146     }
147     return RET_OK;
148 }
149 
DelEpoll(EpollEventType type,int32_t fd)150 int32_t MMIService::DelEpoll(EpollEventType type, int32_t fd)
151 {
152     if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
153         MMI_HILOGE("Invalid param type");
154         return RET_ERR;
155     }
156     if (fd < 0) {
157         MMI_HILOGE("Invalid param fd_");
158         return RET_ERR;
159     }
160     if (mmiFd_ < 0) {
161         MMI_HILOGE("Invalid param mmiFd_");
162         return RET_ERR;
163     }
164     struct epoll_event ev = {};
165     auto ret = EpollCtl(fd, EPOLL_CTL_DEL, ev, mmiFd_);
166     if (ret < 0) {
167         MMI_HILOGE("DelEpoll failed");
168         return ret;
169     }
170     return RET_OK;
171 }
172 
IsRunning() const173 bool MMIService::IsRunning() const
174 {
175     return (state_ == ServiceRunningState::STATE_RUNNING);
176 }
177 
InitLibinputService()178 bool MMIService::InitLibinputService()
179 {
180     if (!(libinputAdapter_.Init(std::bind(&InputEventHandler::OnEvent, InputHandler, std::placeholders::_1),
181         DEF_INPUT_SEAT))) {
182         MMI_HILOGE("Libinput init, bind failed");
183         return false;
184     }
185     auto inputFd = libinputAdapter_.GetInputFd();
186     auto ret = AddEpoll(EPOLL_EVENT_INPUT, inputFd);
187     if (ret <  0) {
188         MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
189         EpollClose();
190         return false;
191     }
192     MMI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", mmiFd_, inputFd);
193     return true;
194 }
195 
InitService()196 bool MMIService::InitService()
197 {
198     MMI_HILOGD("Server msg handler Init");
199     sMsgHandler_.Init(*this);
200     if (state_ != ServiceRunningState::STATE_NOT_START) {
201         MMI_HILOGE("Service running status is not enabled");
202         return false;
203     }
204     if (EpollCreat(MAX_EVENT_SIZE) < 0) {
205         MMI_HILOGE("Create epoll failed");
206         return false;
207     }
208     auto ret = AddEpoll(EPOLL_EVENT_SOCKET, epollFd_);
209     if (ret <  0) {
210         MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
211         EpollClose();
212         return false;
213     }
214     if (!(Publish(this))) {
215         MMI_HILOGE("Service initialization failed");
216         return false;
217     }
218     MMI_HILOGI("AddEpoll, epollfd:%{public}d,fd:%{public}d", mmiFd_, epollFd_);
219     return true;
220 }
221 
InitDelegateTasks()222 bool MMIService::InitDelegateTasks()
223 {
224     CALL_DEBUG_ENTER;
225     if (!delegateTasks_.Init()) {
226         MMI_HILOGE("The delegate task init failed");
227         return false;
228     }
229     auto ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd());
230     if (ret <  0) {
231         MMI_HILOGE("AddEpoll error ret:%{public}d", ret);
232         EpollClose();
233         return false;
234     }
235     MMI_HILOGI("AddEpoll, epollfd:%{public}d,fd:%{public}d", mmiFd_, delegateTasks_.GetReadFd());
236     return true;
237 }
238 
Init()239 int32_t MMIService::Init()
240 {
241     CheckDefine();
242     MMI_HILOGD("WindowsManager Init");
243     WinMgr->Init(*this);
244     MMI_HILOGD("ANRManager Init");
245     ANRMgr->Init(*this);
246     MMI_HILOGD("PointerDrawingManager Init");
247 #ifdef OHOS_BUILD_ENABLE_POINTER
248     if (!IPointerDrawingManager::GetInstance()->Init()) {
249         MMI_HILOGE("Pointer draw init failed");
250         return POINTER_DRAW_INIT_FAIL;
251     }
252 #endif // OHOS_BUILD_ENABLE_POINTER
253     mmiFd_ = EpollCreat(MAX_EVENT_SIZE);
254     if (mmiFd_ < 0) {
255         MMI_HILOGE("Create epoll failed");
256         return EPOLL_CREATE_FAIL;
257     }
258 #ifdef OHOS_BUILD_ENABLE_COOPERATE
259     InputDevCooSM->Init(std::bind(&DelegateTasks::PostAsyncTask, &delegateTasks_, std::placeholders::_1));
260 #endif // OHOS_BUILD_ENABLE_COOPERATE
261     MMI_HILOGD("Input msg handler init");
262     InputHandler->Init(*this);
263     if (!InitLibinputService()) {
264         MMI_HILOGE("Libinput init failed");
265         return LIBINPUT_INIT_FAIL;
266     }
267     if (!InitDelegateTasks()) {
268         MMI_HILOGE("Delegate tasks init failed");
269         return ETASKS_INIT_FAIL;
270     }
271     SetRecvFun(std::bind(&ServerMsgHandler::OnMsgHandler, &sMsgHandler_, std::placeholders::_1,
272         std::placeholders::_2));
273     KeyMapMgr->GetConfigKeyValue("default_keymap", KeyMapMgr->GetDefaultKeyId());
274     OHOS::system::SetParameter(INPUT_POINTER_DEVICE, "false");
275     if (!InitService()) {
276         MMI_HILOGE("Saservice init failed");
277         return SASERVICE_INIT_FAIL;
278     }
279     MMI_HILOGI("Set para input.pointer.device false");
280     return RET_OK;
281 }
282 
OnStart()283 void MMIService::OnStart()
284 {
285     int sleepSeconds = 3;
286     sleep(sleepSeconds);
287     CHK_PID_AND_TID();
288     int32_t ret = Init();
289     if (RET_OK != ret) {
290         MMI_HILOGE("Init mmi_service failed");
291         return;
292     }
293     state_ = ServiceRunningState::STATE_RUNNING;
294     MMI_HILOGD("Started successfully");
295     AddReloadDeviceTimer();
296     t_ = std::thread(std::bind(&MMIService::OnThread, this));
297 #ifdef OHOS_RSS_CLIENT
298     AddSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
299 #endif
300     t_.join();
301 }
302 
OnStop()303 void MMIService::OnStop()
304 {
305     CHK_PID_AND_TID();
306     UdsStop();
307     libinputAdapter_.Stop();
308     state_ = ServiceRunningState::STATE_NOT_START;
309 #ifdef OHOS_RSS_CLIENT
310     RemoveSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
311 #endif
312 }
313 
AllocSocketFd(const std::string & programName,const int32_t moduleType,int32_t & toReturnClientFd,int32_t & tokenType)314 int32_t MMIService::AllocSocketFd(const std::string &programName, const int32_t moduleType,
315     int32_t &toReturnClientFd, int32_t &tokenType)
316 {
317     MMI_HILOGI("Enter, programName:%{public}s,moduleType:%{public}d", programName.c_str(), moduleType);
318 
319     toReturnClientFd = IMultimodalInputConnect::INVALID_SOCKET_FD;
320     int32_t serverFd = IMultimodalInputConnect::INVALID_SOCKET_FD;
321     int32_t pid = GetCallingPid();
322     int32_t uid = GetCallingUid();
323     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&UDSServer::AddSocketPairInfo, this,
324         programName, moduleType, uid, pid, serverFd, std::ref(toReturnClientFd), tokenType));
325     DfxHisysevent::ClientConnectData data = {
326         .pid = pid,
327         .uid = uid,
328         .moduleType = moduleType,
329         .programName = programName,
330         .serverFd = serverFd
331     };
332     if (ret != RET_OK) {
333         MMI_HILOGE("Call AddSocketPairInfo failed,return %{public}d", ret);
334         DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
335         return RET_ERR;
336     }
337     MMI_HILOGIK("Leave, programName:%{public}s,moduleType:%{public}d,alloc success",
338         programName.c_str(), moduleType);
339     DfxHisysevent::OnClientConnect(data, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
340     return RET_OK;
341 }
342 
AddInputEventFilter(sptr<IEventFilter> filter)343 int32_t MMIService::AddInputEventFilter(sptr<IEventFilter> filter)
344 {
345     CALL_INFO_TRACE;
346 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
347     CHKPR(filter, ERROR_NULL_POINTER);
348     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&ServerMsgHandler::AddInputEventFilter,
349         &sMsgHandler_, filter));
350     if (ret != RET_OK) {
351         MMI_HILOGE("Add event filter failed,return %{public}d", ret);
352         return ret;
353     }
354 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
355     return RET_OK;
356 }
357 
OnConnected(SessionPtr s)358 void MMIService::OnConnected(SessionPtr s)
359 {
360     CHKPV(s);
361     MMI_HILOGI("fd:%{public}d", s->GetFd());
362 }
363 
OnDisconnected(SessionPtr s)364 void MMIService::OnDisconnected(SessionPtr s)
365 {
366     CHKPV(s);
367     MMI_HILOGW("Enter, session desc:%{public}s, fd:%{public}d", s->GetDescript().c_str(), s->GetFd());
368 #ifdef OHOS_BUILD_ENABLE_POINTER
369     IPointerDrawingManager::GetInstance()->DeletePointerVisible(s->GetPid());
370 #endif // OHOS_BUILD_ENABLE_POINTER
371 }
372 
SetPointerVisible(bool visible)373 int32_t MMIService::SetPointerVisible(bool visible)
374 {
375     CALL_DEBUG_ENTER;
376 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
377     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::SetPointerVisible,
378         IPointerDrawingManager::GetInstance(), GetCallingPid(), visible));
379     if (ret != RET_OK) {
380         MMI_HILOGE("Set pointer visible failed,return %{public}d", ret);
381         return ret;
382     }
383 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
384     return RET_OK;
385 }
386 
387 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
CheckPointerVisible(bool & visible)388 int32_t MMIService::CheckPointerVisible(bool &visible)
389 {
390     visible = IPointerDrawingManager::GetInstance()->IsPointerVisible();
391     return RET_OK;
392 }
393 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
394 
IsPointerVisible(bool & visible)395 int32_t MMIService::IsPointerVisible(bool &visible)
396 {
397     CALL_DEBUG_ENTER;
398 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
399     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::CheckPointerVisible, this, std::ref(visible)));
400     if (ret != RET_OK) {
401         MMI_HILOGE("Is pointer visible failed,return %{public}d", ret);
402         return RET_ERR;
403     }
404 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
405     return RET_OK;
406 }
407 
SetPointerSpeed(int32_t speed)408 int32_t MMIService::SetPointerSpeed(int32_t speed)
409 {
410     CALL_DEBUG_ENTER;
411 #ifdef OHOS_BUILD_ENABLE_POINTER
412     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MouseEventNormalize::SetPointerSpeed,
413         MouseEventHdr, speed));
414     if (ret != RET_OK) {
415         MMI_HILOGE("Set pointer speed failed,return %{public}d", ret);
416         return RET_ERR;
417     }
418 #endif // OHOS_BUILD_ENABLE_POINTER
419     return RET_OK;
420 }
421 
422 #ifdef OHOS_BUILD_ENABLE_POINTER
ReadPointerSpeed(int32_t & speed)423 int32_t MMIService::ReadPointerSpeed(int32_t &speed)
424 {
425     speed = MouseEventHdr->GetPointerSpeed();
426     return RET_OK;
427 }
428 #endif // OHOS_BUILD_ENABLE_POINTER
429 
GetPointerSpeed(int32_t & speed)430 int32_t MMIService::GetPointerSpeed(int32_t &speed)
431 {
432     CALL_DEBUG_ENTER;
433 #ifdef OHOS_BUILD_ENABLE_POINTER
434     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::ReadPointerSpeed, this, std::ref(speed)));
435     if (ret != RET_OK) {
436         MMI_HILOGE("Get pointer speed failed,return %{public}d", ret);
437         return RET_ERR;
438     }
439 #endif // OHOS_BUILD_ENABLE_POINTER
440     return RET_OK;
441 }
442 
SetPointerStyle(int32_t windowId,int32_t pointerStyle)443 int32_t MMIService::SetPointerStyle(int32_t windowId, int32_t pointerStyle)
444 {
445     CALL_DEBUG_ENTER;
446 #ifdef OHOS_BUILD_ENABLE_POINTER
447     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::SetPointerStyle,
448         IPointerDrawingManager::GetInstance(), GetCallingPid(), windowId, pointerStyle));
449     if (ret != RET_OK) {
450         MMI_HILOGE("Set pointer style failed,return %{public}d", ret);
451         return ret;
452     }
453 #endif // OHOS_BUILD_ENABLE_POINTER
454     return RET_OK;
455 }
456 
GetPointerStyle(int32_t windowId,int32_t & pointerStyle)457 int32_t MMIService::GetPointerStyle(int32_t windowId, int32_t &pointerStyle)
458 {
459     CALL_DEBUG_ENTER;
460 #ifdef OHOS_BUILD_ENABLE_POINTER
461     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&IPointerDrawingManager::GetPointerStyle,
462         IPointerDrawingManager::GetInstance(), GetCallingPid(), windowId, std::ref(pointerStyle)));
463     if (ret != RET_OK) {
464         MMI_HILOGE("Get pointer style failed,return %{public}d", ret);
465         return ret;
466     }
467 #endif // OHOS_BUILD_ENABLE_POINTER
468     return RET_OK;
469 }
470 
OnSupportKeys(int32_t pid,int32_t userData,int32_t deviceId,std::vector<int32_t> & keys)471 int32_t MMIService::OnSupportKeys(int32_t pid, int32_t userData, int32_t deviceId, std::vector<int32_t> &keys)
472 {
473     CALL_DEBUG_ENTER;
474     auto sess = GetSession(GetClientFd(pid));
475     CHKPR(sess, RET_ERR);
476     std::vector<bool> keystroke;
477     int32_t ret = InputDevMgr->SupportKeys(deviceId, keys, keystroke);
478     if (keystroke.size() > MAX_SUPPORT_KEY) {
479         MMI_HILOGE("Device exceeds the max range");
480         return RET_ERR;
481     }
482     if (ret != RET_OK) {
483         MMI_HILOGE("Device id not support");
484         return ret;
485     }
486 
487     NetPacket pkt(MmiMessageId::INPUT_DEVICE_SUPPORT_KEYS);
488     pkt << userData << keystroke.size();
489     for (const bool &item : keystroke) {
490         pkt << item;
491     }
492 
493     if (pkt.ChkRWError()) {
494         MMI_HILOGE("Packet write support keys info failed");
495         return RET_ERR;
496     }
497     if (!sess->SendMsg(pkt)) {
498         MMI_HILOGE("Sending failed");
499         return MSG_SEND_FAIL;
500     }
501     return RET_OK;
502 }
503 
SupportKeys(int32_t userData,int32_t deviceId,std::vector<int32_t> & keys)504 int32_t MMIService::SupportKeys(int32_t userData, int32_t deviceId, std::vector<int32_t> &keys)
505 {
506     CALL_DEBUG_ENTER;
507     int32_t pid = GetCallingPid();
508     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnSupportKeys, this,
509         pid, userData, deviceId, keys));
510     if (ret != RET_OK) {
511         MMI_HILOGE("Support keys info process failed, ret:%{public}d", ret);
512         return ret;
513     }
514     return RET_OK;
515 }
516 
OnGetDeviceIds(int32_t pid,int32_t userData)517 int32_t MMIService::OnGetDeviceIds(int32_t pid, int32_t userData)
518 {
519     CALL_DEBUG_ENTER;
520     auto sess = GetSession(GetClientFd(pid));
521     CHKPR(sess, RET_ERR);
522     std::vector<int32_t> ids = InputDevMgr->GetInputDeviceIds();
523     if (ids.size() > MAX_INPUT_DEVICE) {
524         MMI_HILOGE("Device exceeds the max range");
525         return RET_ERR;
526     }
527     NetPacket pkt(MmiMessageId::INPUT_DEVICE_IDS);
528     pkt << userData << ids;
529     if (pkt.ChkRWError()) {
530         MMI_HILOGE("Packet write data failed");
531         return RET_ERR;
532     }
533     if (!sess->SendMsg(pkt)) {
534         MMI_HILOGE("Sending failed");
535         return MSG_SEND_FAIL;
536     }
537     return RET_OK;
538 }
539 
GetDeviceIds(int32_t userData)540 int32_t MMIService::GetDeviceIds(int32_t userData)
541 {
542     CALL_DEBUG_ENTER;
543     int32_t pid = GetCallingPid();
544     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetDeviceIds, this, pid, userData));
545     if (ret != RET_OK) {
546         MMI_HILOGE("Get deviceids failed, ret:%{public}d", ret);
547         return RET_ERR;
548     }
549     return RET_OK;
550 }
551 
OnGetDevice(int32_t pid,int32_t userData,int32_t deviceId)552 int32_t MMIService::OnGetDevice(int32_t pid, int32_t userData, int32_t deviceId)
553 {
554     CALL_DEBUG_ENTER;
555     auto sess = GetSession(GetClientFd(pid));
556     CHKPR(sess, RET_ERR);
557     std::shared_ptr<InputDevice> inputDevice = std::make_shared<InputDevice>();
558     if (InputDevMgr->GetInputDevice(deviceId) == nullptr) {
559         MMI_HILOGE("Input device not found");
560         return COMMON_PARAMETER_ERROR;
561     }
562     inputDevice = InputDevMgr->GetInputDevice(deviceId);
563     NetPacket pkt(MmiMessageId::INPUT_DEVICE);
564     pkt << userData << inputDevice->GetId() << inputDevice->GetName() << inputDevice->GetType()
565         << inputDevice->GetBus() << inputDevice->GetProduct() << inputDevice->GetVendor()
566         << inputDevice->GetVersion() << inputDevice->GetPhys() << inputDevice->GetUniq()
567         << inputDevice->GetAxisInfo().size();
568     for (auto &axis : inputDevice->GetAxisInfo()) {
569         pkt << axis.GetAxisType() << axis.GetMinimum() << axis.GetMaximum()
570             << axis.GetFuzz() << axis.GetFlat() << axis.GetResolution();
571     }
572     if (pkt.ChkRWError()) {
573         MMI_HILOGE("Packet write input device info failed");
574         return RET_ERR;
575     }
576     if (!sess->SendMsg(pkt)) {
577         MMI_HILOGE("Sending failed");
578         return MSG_SEND_FAIL;
579     }
580     return RET_OK;
581 }
582 
GetDevice(int32_t userData,int32_t deviceId)583 int32_t MMIService::GetDevice(int32_t userData, int32_t deviceId)
584 {
585     CALL_DEBUG_ENTER;
586     int32_t pid = GetCallingPid();
587     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetDevice, this, pid, userData, deviceId));
588     if (ret != RET_OK) {
589         MMI_HILOGE("Get input device info failed, ret:%{public}d", ret);
590         return ret;
591     }
592     return RET_OK;
593 }
594 
OnRegisterDevListener(int32_t pid)595 int32_t MMIService::OnRegisterDevListener(int32_t pid)
596 {
597     auto sess = GetSession(GetClientFd(pid));
598     CHKPR(sess, RET_ERR);
599     InputDevMgr->AddDevListener(sess, [sess](int32_t id, const std::string &type) {
600         CALL_DEBUG_ENTER;
601         CHKPV(sess);
602         NetPacket pkt(MmiMessageId::ADD_INPUT_DEVICE_LISTENER);
603         pkt << type << id;
604         if (pkt.ChkRWError()) {
605             MMI_HILOGE("Packet write data failed");
606             return;
607         }
608         if (!sess->SendMsg(pkt)) {
609             MMI_HILOGE("Sending failed");
610             return;
611         }
612     });
613     return RET_OK;
614 }
615 
RegisterDevListener()616 int32_t MMIService::RegisterDevListener()
617 {
618     CALL_DEBUG_ENTER;
619     int32_t pid = GetCallingPid();
620     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnRegisterDevListener, this, pid));
621     if (ret != RET_OK) {
622         MMI_HILOGE("Register device listener failed, ret:%{public}d", ret);
623         return RET_ERR;
624     }
625     return RET_OK;
626 }
627 
OnUnregisterDevListener(int32_t pid)628 int32_t MMIService::OnUnregisterDevListener(int32_t pid)
629 {
630     auto sess = GetSession(GetClientFd(pid));
631     InputDevMgr->RemoveDevListener(sess);
632     return RET_OK;
633 }
634 
UnregisterDevListener()635 int32_t MMIService::UnregisterDevListener()
636 {
637     CALL_DEBUG_ENTER;
638     int32_t pid = GetCallingPid();
639     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnUnregisterDevListener, this, pid));
640     if (ret != RET_OK) {
641         MMI_HILOGE("Unregister device listener failed failed, ret:%{public}d", ret);
642         return RET_ERR;
643     }
644     return RET_OK;
645 }
646 
OnGetKeyboardType(int32_t pid,int32_t userData,int32_t deviceId)647 int32_t MMIService::OnGetKeyboardType(int32_t pid, int32_t userData, int32_t deviceId)
648 {
649     auto sess = GetSession(GetClientFd(pid));
650     CHKPR(sess, RET_ERR);
651     int32_t keyboardType = 0;
652     int32_t ret = InputDevMgr->GetKeyboardType(deviceId, keyboardType);
653     if (ret != RET_OK) {
654         MMI_HILOGE("GetKeyboardType call failed");
655         return ret;
656     }
657     NetPacket pkt(MmiMessageId::INPUT_DEVICE_KEYBOARD_TYPE);
658     pkt << userData << keyboardType;
659     if (pkt.ChkRWError()) {
660         MMI_HILOGE("Packet write keyboard type failed");
661         return RET_ERR;
662     }
663     if (!sess->SendMsg(pkt)) {
664         MMI_HILOGE("Failed to send the keyboard package");
665         return MSG_SEND_FAIL;
666     }
667     return RET_OK;
668 }
669 
GetKeyboardType(int32_t userData,int32_t deviceId)670 int32_t MMIService::GetKeyboardType(int32_t userData, int32_t deviceId)
671 {
672     CALL_DEBUG_ENTER;
673     int32_t pid = GetCallingPid();
674     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnGetKeyboardType, this,
675         pid, userData, deviceId));
676     if (ret != RET_OK) {
677         MMI_HILOGE("Get keyboard type failed, ret:%{public}d", ret);
678         return ret;
679     }
680     return RET_OK;
681 }
682 
683 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
CheckAddInput(int32_t pid,InputHandlerType handlerType,HandleEventType eventType)684 int32_t MMIService::CheckAddInput(int32_t pid, InputHandlerType handlerType,
685     HandleEventType eventType)
686 {
687     auto sess = GetSessionByPid(pid);
688     CHKPR(sess, ERROR_NULL_POINTER);
689     return sMsgHandler_.OnAddInputHandler(sess, handlerType, eventType);
690 }
691 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
692 
AddInputHandler(InputHandlerType handlerType,HandleEventType eventType)693 int32_t MMIService::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType)
694 {
695     CALL_INFO_TRACE;
696 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
697     int32_t pid = GetCallingPid();
698     int32_t ret = delegateTasks_.PostSyncTask(
699         std::bind(&MMIService::CheckAddInput, this, pid, handlerType, eventType));
700     if (ret != RET_OK) {
701         MMI_HILOGE("Add input handler failed, ret:%{public}d", ret);
702         return RET_ERR;
703     }
704 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
705     return RET_OK;
706 }
707 
708 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
CheckRemoveInput(int32_t pid,InputHandlerType handlerType,HandleEventType eventType)709 int32_t MMIService::CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType)
710 {
711     auto sess = GetSessionByPid(pid);
712     CHKPR(sess, ERROR_NULL_POINTER);
713     return sMsgHandler_.OnRemoveInputHandler(sess, handlerType, eventType);
714 }
715 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
716 
RemoveInputHandler(InputHandlerType handlerType,HandleEventType eventType)717 int32_t MMIService::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType)
718 {
719     CALL_INFO_TRACE;
720 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
721     int32_t pid = GetCallingPid();
722     int32_t ret = delegateTasks_.PostSyncTask(
723         std::bind(&MMIService::CheckRemoveInput, this, pid, handlerType, eventType));
724     if (ret != RET_OK) {
725         MMI_HILOGE("Remove input handler failed, ret:%{public}d", ret);
726         return RET_ERR;
727     }
728 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
729     return RET_OK;
730 }
731 
732 #ifdef OHOS_BUILD_ENABLE_MONITOR
CheckMarkConsumed(int32_t pid,int32_t eventId)733 int32_t MMIService::CheckMarkConsumed(int32_t pid, int32_t eventId)
734 {
735     auto sess = GetSessionByPid(pid);
736     CHKPR(sess, ERROR_NULL_POINTER);
737     return sMsgHandler_.OnMarkConsumed(sess, eventId);
738 }
739 #endif // OHOS_BUILD_ENABLE_MONITOR
740 
MarkEventConsumed(int32_t eventId)741 int32_t MMIService::MarkEventConsumed(int32_t eventId)
742 {
743     CALL_INFO_TRACE;
744 #ifdef OHOS_BUILD_ENABLE_MONITOR
745     int32_t pid = GetCallingPid();
746     int32_t ret = delegateTasks_.PostSyncTask(
747         std::bind(&MMIService::CheckMarkConsumed, this, pid, eventId));
748     if (ret != RET_OK) {
749         MMI_HILOGE("Mark event consumed failed, ret:%{public}d", ret);
750         return RET_ERR;
751     }
752 #endif // OHOS_BUILD_ENABLE_MONITOR
753     return RET_OK;
754 }
755 
MoveMouseEvent(int32_t offsetX,int32_t offsetY)756 int32_t MMIService::MoveMouseEvent(int32_t offsetX, int32_t offsetY)
757 {
758     CALL_DEBUG_ENTER;
759 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
760     int32_t ret = delegateTasks_.PostSyncTask(
761         std::bind(&ServerMsgHandler::OnMoveMouse, &sMsgHandler_, offsetX, offsetY));
762     if (ret != RET_OK) {
763         MMI_HILOGE("The movemouse event processed failed, ret:%{public}d", ret);
764         return RET_ERR;
765     }
766 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
767     return RET_OK;
768 }
769 
InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)770 int32_t MMIService::InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
771 {
772     CALL_INFO_TRACE;
773 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
774     int32_t ret = delegateTasks_.PostSyncTask(
775         std::bind(&MMIService::CheckInjectKeyEvent, this, keyEvent));
776     if (ret != RET_OK) {
777         MMI_HILOGE("Inject key event failed, ret:%{public}d", ret);
778         return RET_ERR;
779     }
780 #endif // OHOS_BUILD_ENABLE_KEYBOARD
781     return RET_OK;
782 }
783 
784 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)785 int32_t MMIService::CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
786 {
787     CHKPR(keyEvent, ERROR_NULL_POINTER);
788     return sMsgHandler_.OnInjectKeyEvent(keyEvent);
789 }
790 #endif // OHOS_BUILD_ENABLE_KEYBOARD
791 
792 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)793 int32_t MMIService::CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
794 {
795     CHKPR(pointerEvent, ERROR_NULL_POINTER);
796     return sMsgHandler_.OnInjectPointerEvent(pointerEvent);
797 }
798 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
799 
InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)800 int32_t MMIService::InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
801 {
802     CALL_INFO_TRACE;
803 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
804     int32_t ret = delegateTasks_.PostSyncTask(
805         std::bind(&MMIService::CheckInjectPointerEvent, this, pointerEvent));
806     if (ret != RET_OK) {
807         MMI_HILOGE("Inject pointer event failed, ret:%{public}d", ret);
808         return RET_ERR;
809     }
810 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
811     return RET_OK;
812 }
813 
814 #ifdef OHOS_RSS_CLIENT
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)815 void MMIService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
816 {
817     if (systemAbilityId == RES_SCHED_SYS_ABILITY_ID) {
818         int sleepSeconds = 1;
819         sleep(sleepSeconds);
820         uint64_t tid = tid_.load();
821         std::unordered_map<std::string, std::string> payload;
822         payload["uid"] = std::to_string(getuid());
823         payload["pid"] = std::to_string(getpid());
824         ResourceSchedule::ResSchedClient::GetInstance().ReportData(
825             ResourceSchedule::ResType::RES_TYPE_REPORT_MMI_PROCESS, tid, payload);
826     }
827 }
828 #endif
829 
SubscribeKeyEvent(int32_t subscribeId,const std::shared_ptr<KeyOption> option)830 int32_t MMIService::SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr<KeyOption> option)
831 {
832     CALL_INFO_TRACE;
833 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
834     int32_t pid = GetCallingPid();
835     int32_t ret = delegateTasks_.PostSyncTask(
836         std::bind(&ServerMsgHandler::OnSubscribeKeyEvent, &sMsgHandler_, this, pid, subscribeId, option));
837     if (ret != RET_OK) {
838         MMI_HILOGE("The subscribe key event processed failed, ret:%{public}d", ret);
839         return RET_ERR;
840     }
841 #endif // OHOS_BUILD_ENABLE_KEYBOARD
842     return RET_OK;
843 }
844 
UnsubscribeKeyEvent(int32_t subscribeId)845 int32_t MMIService::UnsubscribeKeyEvent(int32_t subscribeId)
846 {
847     CALL_INFO_TRACE;
848 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
849     int32_t pid = GetCallingPid();
850     int32_t ret = delegateTasks_.PostSyncTask(
851         std::bind(&ServerMsgHandler::OnUnsubscribeKeyEvent, &sMsgHandler_, this, pid, subscribeId));
852     if (ret != RET_OK) {
853         MMI_HILOGE("The unsubscribe key event processed failed, ret:%{public}d", ret);
854         return RET_ERR;
855     }
856 #endif // OHOS_BUILD_ENABLE_KEYBOARD
857     return RET_OK;
858 }
859 
SetAnrObserver()860 int32_t MMIService::SetAnrObserver()
861 {
862     CALL_DEBUG_ENTER;
863     int32_t pid = GetCallingPid();
864     int32_t ret = delegateTasks_.PostSyncTask(
865         std::bind(&ANRManager::SetANRNoticedPid, ANRMgr, pid));
866     if (ret != RET_OK) {
867         MMI_HILOGE("Set ANRNoticed pid failed, ret:%{public}d", ret);
868         return RET_ERR;
869     }
870     return RET_OK;
871 }
872 
GetFunctionKeyState(int32_t funcKey,bool & state)873 int32_t MMIService::GetFunctionKeyState(int32_t funcKey, bool &state)
874 {
875     CALL_INFO_TRACE;
876 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
877     int32_t ret = delegateTasks_.PostSyncTask(
878         std::bind(&ServerMsgHandler::OnGetFunctionKeyState, &sMsgHandler_, funcKey, std::ref(state)));
879     if (ret != RET_OK) {
880         MMI_HILOGE("Failed to get the keyboard status, ret:%{public}d", ret);
881         return RET_ERR;
882     }
883 #else
884     MMI_HILOGD("Function not supported");
885 #endif // OHOS_BUILD_ENABLE_KEYBOARD
886     return RET_OK;
887 }
888 
SetFunctionKeyState(int32_t funcKey,bool enable)889 int32_t MMIService::SetFunctionKeyState(int32_t funcKey, bool enable)
890 {
891     CALL_INFO_TRACE;
892 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
893     int32_t ret = delegateTasks_.PostSyncTask(
894         std::bind(&ServerMsgHandler::OnSetFunctionKeyState, &sMsgHandler_, funcKey, enable));
895     if (ret != RET_OK) {
896         MMI_HILOGE("Failed to update the keyboard status, ret:%{public}d", ret);
897         return RET_ERR;
898     }
899 #else
900     MMI_HILOGD("Function not supported");
901 #endif // OHOS_BUILD_ENABLE_KEYBOARD
902     return RET_OK;
903 }
904 
OnDelegateTask(epoll_event & ev)905 void MMIService::OnDelegateTask(epoll_event& ev)
906 {
907     if ((ev.events & EPOLLIN) == 0) {
908         MMI_HILOGW("Not epollin");
909         return;
910     }
911     DelegateTasks::TaskData data = {};
912     auto res = read(delegateTasks_.GetReadFd(), &data, sizeof(data));
913     if (res == -1) {
914         MMI_HILOGW("Read failed erron:%{public}d", errno);
915     }
916     MMI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ",std:%{public}" PRId64 ""
917         ",taskId:%{public}d", GetThisThreadId(), data.tid, data.taskId);
918     delegateTasks_.ProcessTasks();
919 }
920 
OnThread()921 void MMIService::OnThread()
922 {
923     SetThreadName(std::string("mmi_service"));
924     uint64_t tid = GetThisThreadId();
925     delegateTasks_.SetWorkerThreadId(tid);
926     MMI_HILOGI("Main worker thread start. tid:%{public}" PRId64 "", tid);
927 #ifdef OHOS_RSS_CLIENT
928     tid_.store(tid);
929 #endif
930     libinputAdapter_.RetriggerHotplugEvents();
931     libinputAdapter_.ProcessPendingEvents();
932     while (state_ == ServiceRunningState::STATE_RUNNING) {
933         epoll_event ev[MAX_EVENT_SIZE] = {};
934         int32_t timeout = TimerMgr->CalcNextDelay();
935         MMI_HILOGD("timeout:%{public}d", timeout);
936         int32_t count = EpollWait(ev[0], MAX_EVENT_SIZE, timeout, mmiFd_);
937         for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) {
938             auto mmiEd = reinterpret_cast<mmi_epoll_event*>(ev[i].data.ptr);
939             CHKPC(mmiEd);
940             if (mmiEd->event_type == EPOLL_EVENT_INPUT) {
941                 libinputAdapter_.EventDispatch(ev[i]);
942             } else if (mmiEd->event_type == EPOLL_EVENT_SOCKET) {
943                 OnEpollEvent(ev[i]);
944             } else if (mmiEd->event_type == EPOLL_EVENT_SIGNAL) {
945                 OnSignalEvent(mmiEd->fd);
946             } else if (mmiEd->event_type == EPOLL_EVENT_ETASK) {
947                 OnDelegateTask(ev[i]);
948             } else {
949                 MMI_HILOGW("Unknown epoll event type:%{public}d", mmiEd->event_type);
950             }
951         }
952         TimerMgr->ProcessTimers();
953         if (state_ != ServiceRunningState::STATE_RUNNING) {
954             break;
955         }
956     }
957     MMI_HILOGI("Main worker thread stop. tid:%{public}" PRId64 "", tid);
958 }
959 
InitSignalHandler()960 bool MMIService::InitSignalHandler()
961 {
962     CALL_DEBUG_ENTER;
963     sigset_t mask = {0};
964     int32_t retCode = sigfillset(&mask);
965     if (retCode < 0) {
966         MMI_HILOGE("Fill signal set failed:%{public}d", errno);
967         return false;
968     }
969 
970     retCode = sigprocmask(SIG_SETMASK, &mask, nullptr);
971     if (retCode < 0) {
972         MMI_HILOGE("Sigprocmask failed:%{public}d", errno);
973         return false;
974     }
975 
976     int32_t fdSignal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
977     if (fdSignal < 0) {
978         MMI_HILOGE("Signal fd failed:%{public}d", errno);
979         return false;
980     }
981 
982     retCode = AddEpoll(EPOLL_EVENT_SIGNAL, fdSignal);
983     if (retCode < 0) {
984         MMI_HILOGE("AddEpoll signalFd failed:%{public}d", retCode);
985         close(fdSignal);
986         return false;
987     }
988     return true;
989 }
990 
OnSignalEvent(int32_t signalFd)991 void MMIService::OnSignalEvent(int32_t signalFd)
992 {
993     CALL_DEBUG_ENTER;
994     signalfd_siginfo sigInfo;
995     int32_t size = ::read(signalFd, &sigInfo, sizeof(signalfd_siginfo));
996     if (size != static_cast<int32_t>(sizeof(signalfd_siginfo))) {
997         MMI_HILOGE("Read signal info failed, invalid size:%{public}d,errno:%{public}d", size, errno);
998         return;
999     }
1000     int32_t signo = static_cast<int32_t>(sigInfo.ssi_signo);
1001     MMI_HILOGD("Receive signal:%{public}d", signo);
1002     switch (signo) {
1003         case SIGINT:
1004         case SIGQUIT:
1005         case SIGILL:
1006         case SIGABRT:
1007         case SIGBUS:
1008         case SIGFPE:
1009         case SIGKILL:
1010         case SIGSEGV:
1011         case SIGTERM: {
1012             state_ = ServiceRunningState::STATE_EXIT;
1013             break;
1014         }
1015         default: {
1016             break;
1017         }
1018     }
1019 }
1020 
AddReloadDeviceTimer()1021 void MMIService::AddReloadDeviceTimer()
1022 {
1023     CALL_DEBUG_ENTER;
1024     TimerMgr->AddTimer(2000, 2, [this]() {
1025         auto deviceIds = InputDevMgr->GetInputDeviceIds();
1026         if (deviceIds.empty()) {
1027             libinputAdapter_.ReloadDevice();
1028         }
1029     });
1030 }
1031 
Dump(int32_t fd,const std::vector<std::u16string> & args)1032 int32_t MMIService::Dump(int32_t fd, const std::vector<std::u16string> &args)
1033 {
1034     CALL_DEBUG_ENTER;
1035     if (fd < 0) {
1036         MMI_HILOGE("The fd is invalid");
1037         return DUMP_PARAM_ERR;
1038     }
1039     if (args.empty()) {
1040         MMI_HILOGE("The args cannot be empty");
1041         mprintf(fd, "args cannot be empty\n");
1042         MMIEventDump->DumpHelp(fd);
1043         return DUMP_PARAM_ERR;
1044     }
1045     std::vector<std::string> argList = { "" };
1046     std::transform(args.begin(), args.end(), std::back_inserter(argList),
1047         [](const std::u16string &arg) {
1048         return Str16ToStr8(arg);
1049     });
1050     MMIEventDump->ParseCommand(fd, argList);
1051     return RET_OK;
1052 }
1053 
RegisterCooperateListener()1054 int32_t MMIService::RegisterCooperateListener()
1055 {
1056     CALL_DEBUG_ENTER;
1057 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1058     int32_t pid = GetCallingPid();
1059     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnRegisterCooperateListener, this, pid));
1060     if (ret != RET_OK) {
1061         MMI_HILOGE("OnRegisterCooperateListener failed, ret:%{public}d", ret);
1062         return RET_ERR;
1063     }
1064 #endif // OHOS_BUILD_ENABLE_COOPERATE
1065     return RET_OK;
1066 }
1067 
UnregisterCooperateListener()1068 int32_t MMIService::UnregisterCooperateListener()
1069 {
1070     CALL_DEBUG_ENTER;
1071 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1072     int32_t pid = GetCallingPid();
1073     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnUnregisterCooperateListener, this, pid));
1074     if (ret != RET_OK) {
1075         MMI_HILOGE("OnUnregisterCooperateListener failed, ret:%{public}d", ret);
1076         return RET_ERR;
1077     }
1078 #endif // OHOS_BUILD_ENABLE_COOPERATE
1079     return RET_OK;
1080 }
1081 
EnableInputDeviceCooperate(int32_t userData,bool enabled)1082 int32_t MMIService::EnableInputDeviceCooperate(int32_t userData, bool enabled)
1083 {
1084     CALL_DEBUG_ENTER;
1085 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1086     int32_t pid = GetCallingPid();
1087     int32_t ret = delegateTasks_.PostSyncTask(
1088         std::bind(&MMIService::OnEnableInputDeviceCooperate, this, pid, userData, enabled));
1089     if (ret != RET_OK) {
1090         MMI_HILOGE("OnEnableInputDeviceCooperate failed, ret:%{public}d", ret);
1091         return ret;
1092     }
1093 #else
1094     (void)(userData);
1095     (void)(enabled);
1096 #endif // OHOS_BUILD_ENABLE_COOPERATE
1097     return RET_OK;
1098 }
1099 
StartInputDeviceCooperate(int32_t userData,const std::string & sinkDeviceId,int32_t srcInputDeviceId)1100 int32_t MMIService::StartInputDeviceCooperate(int32_t userData, const std::string &sinkDeviceId,
1101     int32_t srcInputDeviceId)
1102 {
1103     CALL_DEBUG_ENTER;
1104 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1105     int32_t pid = GetCallingPid();
1106     int32_t ret = delegateTasks_.PostSyncTask(
1107         std::bind(&MMIService::OnStartInputDeviceCooperate, this, pid, userData, sinkDeviceId, srcInputDeviceId));
1108     if (ret != RET_OK) {
1109         MMI_HILOGE("OnStartInputDeviceCooperate failed, ret:%{public}d", ret);
1110         return ret;
1111     }
1112 #else
1113     (void)(userData);
1114     (void)(sinkDeviceId);
1115     (void)(srcInputDeviceId);
1116 #endif // OHOS_BUILD_ENABLE_COOPERATE
1117     return RET_OK;
1118 }
1119 
StopDeviceCooperate(int32_t userData)1120 int32_t MMIService::StopDeviceCooperate(int32_t userData)
1121 {
1122     CALL_DEBUG_ENTER;
1123 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1124     int32_t pid = GetCallingPid();
1125     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&MMIService::OnStopDeviceCooperate, this, pid, userData));
1126     if (ret != RET_OK) {
1127         MMI_HILOGE("OnStopDeviceCooperate failed, ret:%{public}d", ret);
1128         return ret;
1129     }
1130 #else
1131     (void)(userData);
1132 #endif // OHOS_BUILD_ENABLE_COOPERATE
1133     return RET_OK;
1134 }
1135 
GetInputDeviceCooperateState(int32_t userData,const std::string & deviceId)1136 int32_t MMIService::GetInputDeviceCooperateState(int32_t userData, const std::string &deviceId)
1137 {
1138     CALL_DEBUG_ENTER;
1139 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1140     int32_t pid = GetCallingPid();
1141     int32_t ret = delegateTasks_.PostSyncTask(
1142         std::bind(&MMIService::OnGetInputDeviceCooperateState, this, pid, userData, deviceId));
1143     if (ret != RET_OK) {
1144         MMI_HILOGE("OnGetInputDeviceCooperateState failed, ret:%{public}d", ret);
1145         return RET_ERR;
1146     }
1147 #else
1148     (void)(userData);
1149     (void)(deviceId);
1150     MMI_HILOGW("Get input device cooperate state does not support");
1151 #endif // OHOS_BUILD_ENABLE_COOPERATE
1152     return RET_OK;
1153 }
1154 
SetInputDevice(const std::string & dhid,const std::string & screenId)1155 int32_t MMIService::SetInputDevice(const std::string& dhid, const std::string& screenId)
1156 {
1157     CALL_DEBUG_ENTER;
1158 #ifdef OHOS_BUILD_ENABLE_COOPERATE
1159     int32_t ret = delegateTasks_.PostSyncTask(std::bind(&InputDeviceManager::SetInputDevice,
1160         InputDevMgr, dhid, screenId));
1161     if (ret != RET_OK) {
1162         MMI_HILOGE("Set input device screen id failed,return %{public}d", ret);
1163         return ret;
1164     }
1165 #else
1166     (void)(dhid);
1167     (void)(screenId);
1168     MMI_HILOGW("Enable input device cooperate does not support");
1169 #endif // OHOS_BUILD_ENABLE_COOPERATE
1170     return RET_OK;
1171 }
1172 
1173 #ifdef OHOS_BUILD_ENABLE_COOPERATE
OnRegisterCooperateListener(int32_t pid)1174 int32_t MMIService::OnRegisterCooperateListener(int32_t pid)
1175 {
1176     CALL_DEBUG_ENTER;
1177     auto sess = GetSession(GetClientFd(pid));
1178     CHKPR(sess, RET_ERR);
1179     sptr<CooperateEventManager::EventInfo> event = new (std::nothrow) CooperateEventManager::EventInfo();
1180     CHKPR(event, RET_ERR);
1181     event->type = CooperateEventManager::EventType::LISTENER;
1182     event->sess = sess;
1183     event->msgId = MmiMessageId::COOPERATION_ADD_LISTENER;
1184     CooperateEventMgr->AddCooperationEvent(event);
1185     return RET_OK;
1186 }
1187 
OnUnregisterCooperateListener(int32_t pid)1188 int32_t MMIService::OnUnregisterCooperateListener(int32_t pid)
1189 {
1190     CALL_DEBUG_ENTER;
1191     auto sess = GetSession(GetClientFd(pid));
1192     sptr<CooperateEventManager::EventInfo> event = new (std::nothrow) CooperateEventManager::EventInfo();
1193     CHKPR(event, RET_ERR);
1194     event->type = CooperateEventManager::EventType::LISTENER;
1195     event->sess = sess;
1196     CooperateEventMgr->RemoveCooperationEvent(event);
1197     return RET_OK;
1198 }
1199 
OnEnableInputDeviceCooperate(int32_t pid,int32_t userData,bool enabled)1200 int32_t MMIService::OnEnableInputDeviceCooperate(int32_t pid, int32_t userData, bool enabled)
1201 {
1202     CALL_DEBUG_ENTER;
1203     InputDevCooSM->EnableInputDeviceCooperate(enabled);
1204     std::string deviceId =  "";
1205     CooperationMessage msg =
1206         enabled ? CooperationMessage::OPEN_SUCCESS : CooperationMessage::CLOSE_SUCCESS;
1207     NetPacket pkt(MmiMessageId::COOPERATION_MESSAGE);
1208     pkt << userData << deviceId << static_cast<int32_t>(msg);
1209     if (pkt.ChkRWError()) {
1210         MMI_HILOGE("Packet write data failed");
1211         return RET_ERR;
1212     }
1213     auto sess = GetSession(GetClientFd(pid));
1214     CHKPR(sess, RET_ERR);
1215     if (!sess->SendMsg(pkt)) {
1216         MMI_HILOGE("Sending failed");
1217         return MSG_SEND_FAIL;
1218     }
1219     return RET_OK;
1220 }
1221 
OnStartInputDeviceCooperate(int32_t pid,int32_t userData,const std::string & sinkDeviceId,int32_t srcInputDeviceId)1222 int32_t MMIService::OnStartInputDeviceCooperate(int32_t pid, int32_t userData, const std::string &sinkDeviceId,
1223     int32_t srcInputDeviceId)
1224 {
1225     CALL_DEBUG_ENTER;
1226     auto sess = GetSession(GetClientFd(pid));
1227     CHKPR(sess, RET_ERR);
1228     sptr<CooperateEventManager::EventInfo> event = new (std::nothrow) CooperateEventManager::EventInfo();
1229     CHKPR(event, RET_ERR);
1230     event->type = CooperateEventManager::EventType::START;
1231     event->sess = sess;
1232     event->msgId = MmiMessageId::COOPERATION_MESSAGE;
1233     event->userData = userData;
1234     CooperateEventMgr->AddCooperationEvent(event);
1235     int32_t ret = InputDevCooSM->StartInputDeviceCooperate(sinkDeviceId, srcInputDeviceId);
1236     if (ret != RET_OK) {
1237         MMI_HILOGE("OnStartInputDeviceCooperate failed, ret:%{public}d", ret);
1238         CooperateEventMgr->OnErrorMessage(event->type, CooperationMessage(ret));
1239         return ret;
1240     }
1241     return RET_OK;
1242 }
1243 
OnStopDeviceCooperate(int32_t pid,int32_t userData)1244 int32_t MMIService::OnStopDeviceCooperate(int32_t pid, int32_t userData)
1245 {
1246     CALL_DEBUG_ENTER;
1247     auto sess = GetSession(GetClientFd(pid));
1248     CHKPR(sess, RET_ERR);
1249     sptr<CooperateEventManager::EventInfo> event = new (std::nothrow) CooperateEventManager::EventInfo();
1250     CHKPR(event, RET_ERR);
1251     event->type = CooperateEventManager::EventType::STOP;
1252     event->sess = sess;
1253     event->msgId = MmiMessageId::COOPERATION_MESSAGE;
1254     event->userData = userData;
1255     CooperateEventMgr->AddCooperationEvent(event);
1256     int32_t ret = InputDevCooSM->StopInputDeviceCooperate();
1257     if (ret != RET_OK) {
1258         MMI_HILOGE("OnStopDeviceCooperate failed, ret:%{public}d", ret);
1259         CooperateEventMgr->OnErrorMessage(event->type, CooperationMessage(ret));
1260         return ret;
1261     }
1262     return RET_OK;
1263 }
1264 
OnGetInputDeviceCooperateState(int32_t pid,int32_t userData,const std::string & deviceId)1265 int32_t MMIService::OnGetInputDeviceCooperateState(int32_t pid, int32_t userData, const std::string &deviceId)
1266 {
1267     CALL_DEBUG_ENTER;
1268     auto sess = GetSession(GetClientFd(pid));
1269     CHKPR(sess, RET_ERR);
1270     sptr<CooperateEventManager::EventInfo> event = new (std::nothrow) CooperateEventManager::EventInfo();
1271     CHKPR(event, RET_ERR);
1272     event->type = CooperateEventManager::EventType::STATE;
1273     event->sess = sess;
1274     event->msgId = MmiMessageId::COOPERATION_GET_STATE;
1275     event->userData = userData;
1276     CooperateEventMgr->AddCooperationEvent(event);
1277     InputDevCooSM->GetCooperateState(deviceId);
1278     return RET_OK;
1279 }
1280 #endif // OHOS_BUILD_ENABLE_COOPERATE
1281 } // namespace MMI
1282 } // namespace OHOS
1283