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 "continuation_manager/app_device_callback_stub.h"
17 
18 #include <string>
19 
20 #include "base/continuationmgr_log.h"
21 #include "base/parcel_helper.h"
22 #include "distributed_ability_manager_service.h"
23 #include "ipc_object_stub.h"
24 #include "ipc_types.h"
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 namespace {
29 const std::string TAG = "AppDeviceCallbackStub";
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t AppDeviceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
33     MessageParcel& reply, MessageOption& option)
34 {
35     HILOGD("code = %{public}u", code);
36     std::u16string descriptor = AppDeviceCallbackInterface::GetDescriptor();
37     std::u16string remoteDescriptor = data.ReadInterfaceToken();
38     if (descriptor != remoteDescriptor) {
39         HILOGE("descriptor check failed");
40         return ERR_INVALID_STATE;
41     }
42     int32_t token = -1;
43     switch (code) {
44         case static_cast<uint32_t>(IDRequestCallbackInterfaceCode::EVENT_DEVICE_CONNECT): {
45             PARCEL_READ_HELPER(data, Int32, token);
46             std::vector<ContinuationResult> continuationResults;
47             if (!ContinuationResult::ReadContinuationResultsFromParcel(data, continuationResults)) {
48                 return ERR_FLATTEN_OBJECT;
49             }
50             int32_t result = OnDeviceConnect(token, continuationResults);
51             return result;
52         }
53         case static_cast<uint32_t>(IDRequestCallbackInterfaceCode::EVENT_DEVICE_DISCONNECT): {
54             PARCEL_READ_HELPER(data, Int32, token);
55             std::vector<ContinuationResult> continuationResults;
56             if (!ContinuationResult::ReadContinuationResultsFromParcel(data, continuationResults)) {
57                 return ERR_FLATTEN_OBJECT;
58             }
59             int32_t result = OnDeviceDisconnect(token, continuationResults);
60             return result;
61         }
62         case static_cast<uint32_t>(IDRequestCallbackInterfaceCode::EVENT_DEVICE_CANCEL): {
63             int32_t result = OnDeviceCancel();
64             return result;
65         }
66         default: {
67             HILOGE("unknown request code, please check");
68             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
69         }
70     }
71 }
72 
OnDeviceConnect(int32_t token,const std::vector<ContinuationResult> & continuationResults)73 int32_t AppDeviceCallbackStub::OnDeviceConnect(int32_t token,
74     const std::vector<ContinuationResult>& continuationResults)
75 {
76     HILOGD("called.");
77     return DistributedAbilityManagerService::GetInstance().OnDeviceConnect(token, continuationResults);
78 }
79 
OnDeviceDisconnect(int32_t token,const std::vector<ContinuationResult> & continuationResults)80 int32_t AppDeviceCallbackStub::OnDeviceDisconnect(int32_t token,
81     const std::vector<ContinuationResult>& continuationResults)
82 {
83     HILOGD("called.");
84     return DistributedAbilityManagerService::GetInstance().OnDeviceDisconnect(token, continuationResults);
85 }
86 
OnDeviceCancel()87 int32_t AppDeviceCallbackStub::OnDeviceCancel()
88 {
89     HILOGD("called.");
90     return DistributedAbilityManagerService::GetInstance().OnDeviceCancel();
91 }
92 } // namespace DistributedSchedule
93 } // namespace OHOS