1 /*
2 * Copyright (c) 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 "cooperate_server.h"
17
18 #include <chrono>
19
20 #include "cooperate_params.h"
21 #include "default_params.h"
22 #include "devicestatus_define.h"
23 #include "utility.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "CooperateServer"
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace {
32 constexpr int32_t REPEAT_ONCE { 1 };
33 constexpr int32_t DEFAULT_UNLOAD_COOLING_TIME_MS { 60000 };
34 constexpr int32_t SYNC_TASK_TIMEOUT_DURATION { 2500 };
35 }
36
CooperateServer(IContext * context)37 CooperateServer::CooperateServer(IContext *context)
38 : context_(context)
39 {}
40
Enable(CallingContext & context,MessageParcel & data,MessageParcel & reply)41 int32_t CooperateServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
42 {
43 CALL_DEBUG_ENTER;
44 DefaultParam param;
45 if (!param.Unmarshalling(data)) {
46 FI_HILOGE("DefaultParam::Unmarshalling fail");
47 return RET_ERR;
48 }
49 CHKPR(context_, RET_ERR);
50 if (unloadTimerId_ >= 0) {
51 context_->GetTimerManager().RemoveTimer(unloadTimerId_);
52 }
53 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
54 CHKPR(cooperate, RET_ERR);
55 cooperate->Enable(context.tokenId, context.pid, param.userData);
56 return RET_OK;
57 }
58
Disable(CallingContext & context,MessageParcel & data,MessageParcel & reply)59 int32_t CooperateServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
60 {
61 CALL_DEBUG_ENTER;
62 DefaultParam param;
63 if (!param.Unmarshalling(data)) {
64 FI_HILOGE("DefaultParam::Unmarshalling fail");
65 return RET_ERR;
66 }
67 CHKPR(context_, RET_ERR);
68 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
69 CHKPR(cooperate, RET_ERR);
70 cooperate->Disable(context.pid, param.userData);
71 unloadTimerId_ = context_->GetTimerManager().AddTimer(DEFAULT_UNLOAD_COOLING_TIME_MS, REPEAT_ONCE,
72 []() {
73 FI_HILOGI("Unload \'cooperate\' module");
74 });
75 if (unloadTimerId_ < 0) {
76 FI_HILOGE("AddTimer failed, will not unload Cooperate");
77 }
78 return RET_OK;
79 }
80
Start(CallingContext & context,MessageParcel & data,MessageParcel & reply)81 int32_t CooperateServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
82 {
83 CALL_DEBUG_ENTER;
84 StartCooperateParam param;
85 if (!param.Unmarshalling(data)) {
86 FI_HILOGE("StartCooperateParam::Unmarshalling fail");
87 return RET_ERR;
88 }
89 CHKPR(context_, RET_ERR);
90 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
91 CHKPR(cooperate, RET_ERR);
92 return cooperate->Start(context.pid, param.userData, param.remoteNetworkId, param.startDeviceId);
93 }
94
Stop(CallingContext & context,MessageParcel & data,MessageParcel & reply)95 int32_t CooperateServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
96 {
97 CALL_DEBUG_ENTER;
98 StopCooperateParam param;
99 if (!param.Unmarshalling(data)) {
100 FI_HILOGE("StopCooperateParam::Unmarshalling fail");
101 return RET_ERR;
102 }
103 CHKPR(context_, RET_ERR);
104 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
105 CHKPR(cooperate, RET_ERR);
106 return cooperate->Stop(context.pid, param.userData, param.isUnchained);
107 }
108
AddWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)109 int32_t CooperateServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
110 {
111 CALL_DEBUG_ENTER;
112 CHKPR(context_, RET_ERR);
113 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
114 CHKPR(cooperate, RET_ERR);
115 switch (id) {
116 case CooperateRequestID::REGISTER_LISTENER: {
117 return cooperate->RegisterListener(context.pid);
118 }
119 case CooperateRequestID::REGISTER_HOTAREA_LISTENER: {
120 return cooperate->RegisterHotAreaListener(context.pid);
121 }
122 case CooperateRequestID::REGISTER_EVENT_LISTENER: {
123 RegisterEventListenerParam param;
124 if (!param.Unmarshalling(data)) {
125 FI_HILOGE("RegisterEventListenerParam::Unmarshalling fail");
126 return RET_ERR;
127 }
128 return cooperate->RegisterEventListener(context.pid, param.networkId);
129 }
130 default: {
131 FI_HILOGE("Unexpected request ID (%{public}u)", id);
132 return RET_ERR;
133 }
134 }
135 }
136
RemoveWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)137 int32_t CooperateServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
138 {
139 CALL_DEBUG_ENTER;
140 CHKPR(context_, RET_ERR);
141 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
142 CHKPR(cooperate, RET_ERR);
143 switch (id) {
144 case CooperateRequestID::UNREGISTER_LISTENER: {
145 return cooperate->UnregisterListener(context.pid);
146 }
147 case CooperateRequestID::UNREGISTER_HOTAREA_LISTENER: {
148 return cooperate->UnregisterHotAreaListener(context.pid);
149 }
150 case CooperateRequestID::UNREGISTER_EVENT_LISTENER: {
151 UnregisterEventListenerParam param;
152 if (!param.Unmarshalling(data)) {
153 FI_HILOGE("UnregisterEventListenerParam::Unmarshalling fail");
154 return RET_ERR;
155 }
156 return cooperate->UnregisterEventListener(context.pid, param.networkId);
157 }
158 default: {
159 FI_HILOGE("Unexpected request ID (%{public}u)", id);
160 return RET_ERR;
161 }
162 }
163 }
164
SetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)165 int32_t CooperateServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
166 {
167 CALL_DEBUG_ENTER;
168 return RET_ERR;
169 }
170
GetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)171 int32_t CooperateServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
172 {
173 CALL_DEBUG_ENTER;
174 CHKPR(context_, RET_ERR);
175 ICooperate* cooperate = context_->GetPluginManager().LoadCooperate();
176 CHKPR(cooperate, RET_ERR);
177 auto enterStamp = std::chrono::steady_clock::now();
178 auto checkParcelValid = [&enterStamp] () {
179 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
180 std::chrono::steady_clock::now() - enterStamp).count();
181 return duration < SYNC_TASK_TIMEOUT_DURATION;
182 };
183 switch (id) {
184 case CooperateRequestID::GET_COOPERATE_STATE: {
185 GetCooperateStateParam param;
186 if (!param.Unmarshalling(data)) {
187 FI_HILOGE("GetCooperateStateParam::Unmarshalling fail");
188 return RET_ERR;
189 }
190 return cooperate->GetCooperateState(context.pid, param.userData, param.networkId);
191 }
192 case CooperateRequestID::GET_COOPERATE_STATE_SYNC: {
193 GetCooperateStateSyncParam param;
194 if (!param.Unmarshalling(data)) {
195 FI_HILOGE("GetCooperateStateParam::Unmarshalling fail");
196 return RET_ERR;
197 }
198 bool state { false };
199 if (cooperate->GetCooperateState(param.udId, state) != RET_OK) {
200 FI_HILOGE("GetCooperateState failed");
201 return RET_ERR;
202 }
203 FI_HILOGI("GetCooperateState for udId:%{public}s successfully, state:%{public}s",
204 Utility::Anonymize(param.udId).c_str(), state ? "true" : "false");
205 if (!checkParcelValid()) {
206 FI_HILOGE("CheckParcelValid failed");
207 return RET_ERR;
208 }
209 if (!BooleanReply(state).Marshalling(reply)) {
210 FI_HILOGE("Marshalling state failed");
211 return RET_ERR;
212 }
213 return RET_OK;
214 }
215 default: {
216 FI_HILOGE("Unexpected request ID (%{public}u)", id);
217 return RET_ERR;
218 }
219 }
220 }
221
Control(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)222 int32_t CooperateServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
223 {
224 CALL_DEBUG_ENTER;
225 return RET_ERR;
226 }
227 } // namespace DeviceStatus
228 } // namespace Msdp
229 } // namespace OHOS