1 /*
2 * Copyright (c) 2022-2023 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 "coordination.h"
17
18 #include "coordination_event_manager.h"
19 #include "coordination_sm.h"
20 #include "coordination_util.h"
21 #include "distributed_input_adapter.h"
22 #include "proto.h"
23
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "Coordination" };
29 } // namespace
30
PrepareCoordination()31 void Coordination::PrepareCoordination()
32 {
33 COOR_SM->PrepareCoordination();
34 }
35
UnprepareCoordination()36 void Coordination::UnprepareCoordination()
37 {
38 COOR_SM->UnprepareCoordination();
39 }
40
ActivateCoordination(SessionPtr sess,int32_t userData,const std::string & remoteNetworkId,int32_t startDeviceId)41 int32_t Coordination::ActivateCoordination(SessionPtr sess, int32_t userData,
42 const std::string& remoteNetworkId, int32_t startDeviceId)
43 {
44 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
45 CHKPR(event, RET_ERR);
46 event->type = CoordinationEventManager::EventType::START;
47 event->sess = sess;
48 event->msgId = MessageId::COORDINATION_MESSAGE;
49 event->userData = userData;
50 COOR_EVENT_MGR->AddCoordinationEvent(event);
51 int32_t ret = COOR_SM->ActivateCoordination(remoteNetworkId, startDeviceId);
52 if (ret != RET_OK) {
53 FI_HILOGE("ActivateCoordination failed, ret:%{public}d", ret);
54 COOR_EVENT_MGR->OnErrorMessage(event->type, static_cast<CoordinationMessage>(ret));
55 }
56 return ret;
57 }
58
DeactivateCoordination(SessionPtr sess,int32_t userData,bool isUnchained)59 int32_t Coordination::DeactivateCoordination(SessionPtr sess, int32_t userData, bool isUnchained)
60 {
61 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
62 CHKPR(event, RET_ERR);
63 event->type = CoordinationEventManager::EventType::STOP;
64 event->sess = sess;
65 event->msgId = MessageId::COORDINATION_MESSAGE;
66 event->userData = userData;
67 COOR_EVENT_MGR->AddCoordinationEvent(event);
68 int32_t ret = COOR_SM->DeactivateCoordination(isUnchained);
69 if (ret != RET_OK) {
70 FI_HILOGE("Deactivate coordination failed, ret:%{public}d", ret);
71 COOR_EVENT_MGR->OnErrorMessage(event->type, static_cast<CoordinationMessage>(ret));
72 }
73 return ret;
74 }
75
GetCoordinationState(SessionPtr sess,int32_t userData,const std::string & deviceId)76 int32_t Coordination::GetCoordinationState(SessionPtr sess, int32_t userData, const std::string &deviceId)
77 {
78 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
79 CHKPR(event, RET_ERR);
80 event->type = CoordinationEventManager::EventType::STATE;
81 event->sess = sess;
82 event->msgId = MessageId::COORDINATION_GET_STATE;
83 event->userData = userData;
84 COOR_EVENT_MGR->AddCoordinationEvent(event);
85 int32_t ret = COOR_SM->GetCoordinationState(deviceId);
86 if (ret != RET_OK) {
87 FI_HILOGE("Get coordination state failed");
88 }
89 return ret;
90 }
91
RegisterCoordinationListener(SessionPtr sess)92 int32_t Coordination::RegisterCoordinationListener(SessionPtr sess)
93 {
94 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
95 CHKPR(event, RET_ERR);
96 event->type = CoordinationEventManager::EventType::LISTENER;
97 event->sess = sess;
98 event->msgId = MessageId::COORDINATION_ADD_LISTENER;
99 COOR_EVENT_MGR->AddCoordinationEvent(event);
100 return RET_OK;
101 }
102
UnregisterCoordinationListener(SessionPtr sess)103 int32_t Coordination::UnregisterCoordinationListener(SessionPtr sess)
104 {
105 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
106 CHKPR(event, RET_ERR);
107 event->type = CoordinationEventManager::EventType::LISTENER;
108 event->sess = sess;
109 COOR_EVENT_MGR->RemoveCoordinationEvent(event);
110 return RET_OK;
111 }
112
Dump(int32_t fd)113 void Coordination::Dump(int32_t fd)
114 {
115 COOR_SM->Dump(fd);
116 }
117
CreateICoordination(IContext * context)118 ICoordination* CreateICoordination(IContext *context)
119 {
120 CHKPP(context);
121 COOR_EVENT_MGR->SetIContext(context);
122 ICoordination *coord = new (std::nothrow) Coordination();
123 CHKPP(coord);
124 return coord;
125 }
126 } // namespace DeviceStatus
127 } // namespace Msdp
128 } // namespace OHOS