• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "continuation_manager/app_device_callback_stub.h"
17 
18 #include <string>
19 
20 #include "continuation_extra_params.h"
21 #include "dtbschedmgr_log.h"
22 #include "ipc_object_stub.h"
23 #include "ipc_types.h"
24 #include "parcel_helper.h"
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 namespace {
29 const std::string TAG = "AppDeviceCallbackStub";
30 }
31 
AppDeviceCallbackStub(const sptr<DmsNotifier> & dmsNotifier)32 AppDeviceCallbackStub::AppDeviceCallbackStub(const sptr<DmsNotifier>& dmsNotifier)
33 {
34     dmsNotifier_ = dmsNotifier;
35 }
36 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t AppDeviceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
38     MessageParcel& reply, MessageOption& option)
39 {
40     HILOGD("code = %{public}u", code);
41     std::u16string descriptor = AppDeviceCallbackInterface::GetDescriptor();
42     std::u16string remoteDescriptor = data.ReadInterfaceToken();
43     if (descriptor != remoteDescriptor) {
44         HILOGE("descriptor check failed");
45         return ERR_INVALID_STATE;
46     }
47     int32_t token = -1;
48     switch (code) {
49         case AppDeviceCallbackInterface::EVENT_DEVICE_CONNECT: {
50             PARCEL_READ_HELPER(data, Int32, token);
51             std::vector<ContinuationResult> continuationResults;
52             if (!ContinuationResult::ReadContinuationResultsFromParcel(data, continuationResults)) {
53                 return ERR_FLATTEN_OBJECT;
54             }
55             int32_t result = OnDeviceConnect(token, continuationResults);
56             return result;
57         }
58         case AppDeviceCallbackInterface::EVENT_DEVICE_DISCONNECT: {
59             PARCEL_READ_HELPER(data, Int32, token);
60             std::vector<ContinuationResult> continuationResults;
61             if (!ContinuationResult::ReadContinuationResultsFromParcel(data, continuationResults)) {
62                 return ERR_FLATTEN_OBJECT;
63             }
64             int32_t result = OnDeviceDisconnect(token, continuationResults);
65             return result;
66         }
67         case AppDeviceCallbackInterface::EVENT_DEVICE_CANCEL: {
68             int32_t result = OnDeviceCancel();
69             return result;
70         }
71         default: {
72             HILOGE("unknown request code, please check");
73             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74         }
75     }
76 }
77 
OnDeviceConnect(int32_t token,const std::vector<ContinuationResult> & continuationResults)78 int32_t AppDeviceCallbackStub::OnDeviceConnect(int32_t token,
79     const std::vector<ContinuationResult>& continuationResults)
80 {
81     HILOGD("called.");
82     if (dmsNotifier_ == nullptr) {
83         HILOGE("dmsNotifier_ is nullptr");
84         return ERR_NULL_OBJECT;
85     }
86     int32_t result = dmsNotifier_->OnDeviceConnect(token, continuationResults);
87     return result;
88 }
89 
OnDeviceDisconnect(int32_t token,const std::vector<ContinuationResult> & continuationResults)90 int32_t AppDeviceCallbackStub::OnDeviceDisconnect(int32_t token,
91     const std::vector<ContinuationResult>& continuationResults)
92 {
93     HILOGD("called.");
94     if (dmsNotifier_ == nullptr) {
95         HILOGE("dmsNotifier_ is nullptr");
96         return ERR_NULL_OBJECT;
97     }
98     int32_t result = dmsNotifier_->OnDeviceDisconnect(token, continuationResults);
99     return result;
100 }
101 
OnDeviceCancel()102 int32_t AppDeviceCallbackStub::OnDeviceCancel()
103 {
104     HILOGD("called.");
105     if (dmsNotifier_ == nullptr) {
106         HILOGE("dmsNotifier_ is nullptr");
107         return ERR_NULL_OBJECT;
108     }
109     int32_t result = dmsNotifier_->OnDeviceCancel();
110     return result;
111 }
112 } // namespace DistributedSchedule
113 } // namespace OHOS